@soonspacejs/plugin-clipping-controls 2.13.17 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import SoonSpace, { IColor, AxisType, Position, Rotation } from 'soonspacejs';
2
- import type { BaseObject3D, BaseMesh } from 'soonspacejs';
1
+ import { default as SoonSpace, IColor, AxisType, Position, Rotation, BaseObject3D, BaseMesh } from 'soonspacejs';
3
2
  export type ClipPlaneName = AxisType | `-${AxisType}`;
4
3
  export interface SceneClippingOptions {
5
4
  axis: AxisType;
package/dist/index.esm.js CHANGED
@@ -1 +1,186 @@
1
- import e from"soonspacejs";import{Vector3 as s,Plane as i,Box3 as n}from"three";class a{constructor(e){this.ssp=e,this.scenePlaneHelpers={x:null,y:null,z:null,"-x":null,"-y":null,"-z":null}}addPlaneHelper(e,s,i){const n=this.ssp.addPlaneHelper(Object.assign(Object.assign({},s),{opacity:.3}));this.scenePlaneHelpers[e]=n,i&&this.ssp.addObject(n,i)}sceneClipping(n){var a,l,r;const{axis:t,clipPoint:o,isForward:p=!0,openHelper:c=!0}=n,d=p?t:`-${t}`,h=p?o-1:1-o,w=`scene_clippingPlaneHelper_${d}`;if(c)if(this.scenePlaneHelpers[d])switch(d){case"x":case"-x":null===(a=this.scenePlaneHelpers[d])||void 0===a||a.position.setX(h);break;case"y":case"-y":null===(l=this.scenePlaneHelpers[d])||void 0===l||l.position.setY(h);break;case"z":case"-z":null===(r=this.scenePlaneHelpers[d])||void 0===r||r.position.setZ(h)}else{const i=e.utils.getBoundingBox(this.ssp.viewport.scene),n=i.getSize(new s);let a=0,l=0,r="green";const o=i.getCenter(new s).clone(),p={x:0,y:0,z:0};switch(t){case"x":a=n.z,l=n.y,r="green",o.x=h,p.y=Math.PI/2;break;case"y":a=n.x,l=n.z,r="blue",o.y=h,p.x=Math.PI/2;break;case"z":a=n.x,l=n.y,o.z=h,r="red"}this.addPlaneHelper(d,{id:w,width:a,height:l,color:r,position:o,rotation:p})}else this.scenePlaneHelpers[d]&&(this.ssp.removeHelperById(w),this.scenePlaneHelpers[d]=null);const x={x:new s(-1,0,0),"-x":new s(1,0,0),y:new s(0,-1,0),"-y":new s(0,1,0),z:new s(0,0,-1),"-z":new s(0,0,1)},g=x[d][t],y=this.ssp.viewport.renderer.clippingPlanes.find(e=>e.normal[t]===g);if(y)y.constant=o;else{this.ssp.viewport.renderer.clippingPlanes.push(new i(x[d],o))}this.ssp.signals.sceneChanged.dispatch()}removeSceneClipping(e,s=!0){const i=e?[{axis:e,isForward:s}]:[{axis:"x",isForward:!0},{axis:"x",isForward:!1},{axis:"y",isForward:!0},{axis:"y",isForward:!1},{axis:"z",isForward:!0},{axis:"z",isForward:!1}];for(const{axis:e,isForward:s}of i){const i=s?-1:1,n=s?e:`-${e}`,a=this.ssp.viewport.renderer.clippingPlanes,l=a.findIndex(s=>s.normal[e]===i);a.splice(l,1);const r=`scene_clippingPlaneHelper_${n}`;this.ssp.removeHelperById(r),this.scenePlaneHelpers[n]=null}this.ssp.signals.sceneChanged.dispatch()}modelClipping(e,a){const{axis:l,clipPercent:r,isForward:t=!0,openHelper:o=!0}=a,p=t?l:`-${l}`,c=(new n).setFromObject(e);c.max.add(new s(2,2,2)),c.min.sub(new s(2,2,2));const d=c.getSize(new s),h=t?c.max[l]-r/100*d[l]+1:c.min[l]+r/100*d[l]-1,w=t?h+1:h-1,x=`model_${e.sid}_clippingPlaneHelper_${p}`,g=this.ssp.getObjectById(x);if(o)if(g)switch(l){case"x":null==g||g.position.setX(w);break;case"y":null==g||g.position.setY(w);break;case"z":null==g||g.position.setZ(w)}else{const e=c.getSize(new s);let i=0,n=0,a="green";const r=c.getCenter(new s).clone(),t={x:0,y:0,z:0};switch(l){case"x":i=e.z,n=e.y,a="green",r.x=w,t.y=Math.PI/2;break;case"y":i=e.x,n=e.z,a="blue",r.y=w,t.x=Math.PI/2;break;case"z":i=e.x,n=e.y,r.z=w,a="red"}this.addPlaneHelper(p,{id:x,width:i,height:n,color:a,position:r,rotation:t})}else g&&this.ssp.removeObjectById(x);const y={x:new s(-1,0,0),y:new s(0,-1,0),z:new s(0,0,-1),"-x":new s(1,0,0),"-y":new s(0,1,0),"-z":new s(0,0,1)},P=y[p][l];e.traverse(e=>{if(e.material){const s=e,n=e=>{e.clippingPlanes||(e.clippingPlanes=[]);const s=e.clippingPlanes.find(e=>e.normal[l]===P);if(s)s.normal=y[p],s.constant=h,t||s.negate();else{const s=new i(y[p],h);t||s.negate(),e.clippingPlanes.push(s)}};Array.isArray(s.material)?s.material.forEach(e=>n(e)):n(s.material)}}),this.ssp.signals.sceneChanged.dispatch()}removeModelClipping(e,s,i=!0){const n=e=>{if(s){if(e.clippingPlanes){const i=e.clippingPlanes.findIndex(e=>1===Math.abs(e.normal[s]));e.clippingPlanes.splice(i,1)}}else e.clippingPlanes=[]};e.traverse(e=>{if(e.material){const s=e;Array.isArray(s.material)?s.material.forEach(e=>n(e)):n(s.material)}});const a=s?[{axis:s,isForward:i}]:[{axis:"x",isForward:!0},{axis:"x",isForward:!1},{axis:"y",isForward:!0},{axis:"y",isForward:!1},{axis:"z",isForward:!0},{axis:"z",isForward:!1}];for(const{axis:s,isForward:i}of a){const n=i?s:`-${s}`,a=`model_${e.sid}_clippingPlaneHelper_${n}`;this.ssp.removeHelperById(a)}this.ssp.signals.sceneChanged.dispatch()}}export{a as default};
1
+ import F from "soonspacejs";
2
+ import { Vector3 as a, Plane as b, Box3 as v } from "three";
3
+ class N {
4
+ constructor(r) {
5
+ this.ssp = r, this.scenePlaneHelpers = {
6
+ x: null,
7
+ y: null,
8
+ z: null,
9
+ "-x": null,
10
+ "-y": null,
11
+ "-z": null
12
+ };
13
+ }
14
+ scenePlaneHelpers;
15
+ addPlaneHelper(r, l, n) {
16
+ const t = this.ssp.addPlaneHelper({
17
+ ...l,
18
+ opacity: 0.3
19
+ });
20
+ this.scenePlaneHelpers[r] = t, n && this.ssp.addObject(t, n);
21
+ }
22
+ sceneClipping(r) {
23
+ const {
24
+ axis: l,
25
+ clipPoint: n,
26
+ isForward: t = !0,
27
+ openHelper: c = !0
28
+ } = r, e = t ? l : `-${l}`, s = t ? n - 1 : -n + 1, i = `scene_clippingPlaneHelper_${e}`;
29
+ if (c)
30
+ if (this.scenePlaneHelpers[e])
31
+ switch (e) {
32
+ case "x":
33
+ case "-x":
34
+ this.scenePlaneHelpers[e]?.position.setX(s);
35
+ break;
36
+ case "y":
37
+ case "-y":
38
+ this.scenePlaneHelpers[e]?.position.setY(s);
39
+ break;
40
+ case "z":
41
+ case "-z":
42
+ this.scenePlaneHelpers[e]?.position.setZ(s);
43
+ break;
44
+ }
45
+ else {
46
+ const P = F.utils.getBoundingBox(this.ssp.viewport.scene), d = P.getSize(new a()), z = P.getCenter(new a());
47
+ let u = 0, p = 0, g = "green";
48
+ const x = z.clone(), o = { x: 0, y: 0, z: 0 };
49
+ switch (l) {
50
+ case "x":
51
+ u = d.z, p = d.y, g = "green", x.x = s, o.y = Math.PI / 2;
52
+ break;
53
+ case "y":
54
+ u = d.x, p = d.z, g = "blue", x.y = s, o.x = Math.PI / 2;
55
+ break;
56
+ case "z":
57
+ u = d.x, p = d.y, x.z = s, g = "red";
58
+ break;
59
+ }
60
+ this.addPlaneHelper(e, {
61
+ id: i,
62
+ width: u,
63
+ height: p,
64
+ color: g,
65
+ position: x,
66
+ rotation: o
67
+ });
68
+ }
69
+ else this.scenePlaneHelpers[e] && (this.ssp.removeHelperById(i), this.scenePlaneHelpers[e] = null);
70
+ const w = {
71
+ x: new a(-1, 0, 0),
72
+ "-x": new a(1, 0, 0),
73
+ y: new a(0, -1, 0),
74
+ "-y": new a(0, 1, 0),
75
+ z: new a(0, 0, -1),
76
+ "-z": new a(0, 0, 1)
77
+ }, y = w[e][l], h = this.ssp.viewport.renderer.clippingPlanes.find((P) => P.normal[l] === y);
78
+ h ? h.constant = n : this.ssp.viewport.renderer.clippingPlanes.push(new b(w[e], n)), this.ssp.signals.sceneChanged.dispatch();
79
+ }
80
+ removeSceneClipping(r, l = !0) {
81
+ const n = r ? [{ axis: r, isForward: l }] : [{ axis: "x", isForward: !0 }, { axis: "x", isForward: !1 }, { axis: "y", isForward: !0 }, { axis: "y", isForward: !1 }, { axis: "z", isForward: !0 }, { axis: "z", isForward: !1 }];
82
+ for (const { axis: t, isForward: c } of n) {
83
+ const e = c ? -1 : 1, s = c ? t : `-${t}`, i = this.ssp.viewport.renderer.clippingPlanes, w = i.findIndex((h) => h.normal[t] === e);
84
+ i.splice(w, 1);
85
+ const y = `scene_clippingPlaneHelper_${s}`;
86
+ this.ssp.removeHelperById(y), this.scenePlaneHelpers[s] = null;
87
+ }
88
+ this.ssp.signals.sceneChanged.dispatch();
89
+ }
90
+ modelClipping(r, l) {
91
+ const {
92
+ axis: n,
93
+ clipPercent: t,
94
+ isForward: c = !0,
95
+ openHelper: e = !0
96
+ } = l, s = c ? n : `-${n}`, i = new v().setFromObject(r);
97
+ i.max.add(new a(2, 2, 2)), i.min.sub(new a(2, 2, 2));
98
+ const w = i.getSize(new a()), y = c ? i.max[n] - t / 100 * w[n] + 1 : i.min[n] + t / 100 * w[n] - 1, h = c ? y + 1 : y - 1, P = `model_${r.sid}_clippingPlaneHelper_${s}`, d = this.ssp.getObjectById(P);
99
+ if (e)
100
+ if (d)
101
+ switch (n) {
102
+ case "x":
103
+ d?.position.setX(h);
104
+ break;
105
+ case "y":
106
+ d?.position.setY(h);
107
+ break;
108
+ case "z":
109
+ d?.position.setZ(h);
110
+ break;
111
+ }
112
+ else {
113
+ const p = i.getSize(new a()), g = i.getCenter(new a());
114
+ let x = 0, o = 0, f = "green";
115
+ const m = g.clone(), H = { x: 0, y: 0, z: 0 };
116
+ switch (n) {
117
+ case "x":
118
+ x = p.z, o = p.y, f = "green", m.x = h, H.y = Math.PI / 2;
119
+ break;
120
+ case "y":
121
+ x = p.x, o = p.z, f = "blue", m.y = h, H.x = Math.PI / 2;
122
+ break;
123
+ case "z":
124
+ x = p.x, o = p.y, m.z = h, f = "red";
125
+ break;
126
+ }
127
+ this.addPlaneHelper(s, {
128
+ id: P,
129
+ width: x,
130
+ height: o,
131
+ color: f,
132
+ position: m,
133
+ rotation: H
134
+ });
135
+ }
136
+ else d && this.ssp.removeObjectById(P);
137
+ const z = {
138
+ x: new a(-1, 0, 0),
139
+ y: new a(0, -1, 0),
140
+ z: new a(0, 0, -1),
141
+ "-x": new a(1, 0, 0),
142
+ "-y": new a(0, 1, 0),
143
+ "-z": new a(0, 0, 1)
144
+ }, u = z[s][n];
145
+ r.traverse((p) => {
146
+ if (p.material) {
147
+ const g = p, x = (o) => {
148
+ o.clippingPlanes || (o.clippingPlanes = []);
149
+ const f = o.clippingPlanes.find((m) => m.normal[n] === u);
150
+ if (f)
151
+ f.normal = z[s], f.constant = y, c || f.negate();
152
+ else {
153
+ const m = new b(z[s], y);
154
+ c || m.negate(), o.clippingPlanes.push(m);
155
+ }
156
+ };
157
+ Array.isArray(g.material) ? g.material.forEach((o) => x(o)) : x(g.material);
158
+ }
159
+ }), this.ssp.signals.sceneChanged.dispatch();
160
+ }
161
+ removeModelClipping(r, l, n = !0) {
162
+ const t = (e) => {
163
+ if (!l)
164
+ e.clippingPlanes = [];
165
+ else if (e.clippingPlanes) {
166
+ const s = e.clippingPlanes.findIndex((i) => Math.abs(i.normal[l]) === 1);
167
+ e.clippingPlanes.splice(s, 1);
168
+ }
169
+ };
170
+ r.traverse((e) => {
171
+ if (e.material) {
172
+ const s = e;
173
+ Array.isArray(s.material) ? s.material.forEach((i) => t(i)) : t(s.material);
174
+ }
175
+ });
176
+ const c = l ? [{ axis: l, isForward: n }] : [{ axis: "x", isForward: !0 }, { axis: "x", isForward: !1 }, { axis: "y", isForward: !0 }, { axis: "y", isForward: !1 }, { axis: "z", isForward: !0 }, { axis: "z", isForward: !1 }];
177
+ for (const { axis: e, isForward: s } of c) {
178
+ const i = s ? e : `-${e}`, w = `model_${r.sid}_clippingPlaneHelper_${i}`;
179
+ this.ssp.removeHelperById(w);
180
+ }
181
+ this.ssp.signals.sceneChanged.dispatch();
182
+ }
183
+ }
184
+ export {
185
+ N as default
186
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@soonspacejs/plugin-clipping-controls",
3
3
  "pluginName": "ClippingControlsPlugin",
4
- "version": "2.13.17",
4
+ "version": "2.14.0",
5
5
  "description": "Clipping controls plugin for SoonSpace.js",
6
6
  "main": "dist/index.esm.js",
7
7
  "module": "dist/index.esm.js",
@@ -13,8 +13,8 @@
13
13
  ],
14
14
  "author": "xunwei",
15
15
  "license": "UNLICENSED",
16
- "gitHead": "8afa5fda9a0ef2c080060e7d8e71a8bf168fd4d2",
16
+ "gitHead": "4c85e8b7b8ad24ccb9b42f3a1826bca377c42a6d",
17
17
  "peerDependencies": {
18
- "soonspacejs": "2.13.17"
18
+ "soonspacejs": "2.14.0"
19
19
  }
20
20
  }