three-base 2.0.71 → 2.0.73

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.
@@ -13,20 +13,20 @@ export default class InstancedMeshBase extends MeshBase {
13
13
  static _scale: Vector3;
14
14
  static _rotation: Euler;
15
15
  static _Quaternion: Quaternion;
16
- static matrix: Matrix4;
16
+ static _matrix: Matrix4;
17
17
  instanceMatrix: InstancedBufferAttribute;
18
18
  instanceColor: InstancedBufferAttribute;
19
19
  loadModel(url: string, filename?: string): Promise<void>;
20
20
  computeBoundingSphere: import("lodash").DebouncedFunc<() => void>;
21
- setCount(count?: number): void;
22
- setMatrixAt(index: number, matrix: Matrix4): void;
21
+ setCount(count?: number): Promise<void>;
22
+ setMatrixAt(index: number, matrix: Matrix4): Promise<void>;
23
23
  setTRS(index: number, { position, rotation, scale }: {
24
24
  position?: Vector3;
25
25
  rotation?: Euler;
26
26
  scale?: Vector3;
27
- }): void;
28
- setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined): void;
27
+ }): Promise<void>;
28
+ setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined): Promise<void>;
29
29
  updateMatrixWorld(force?: boolean): void;
30
- addEventListener(name: string, callback: (e: any) => void): void;
30
+ addEventListener(name: string, callback: (e: any) => void): Promise<void>;
31
31
  removeEventListener(name: string, callback: (e: any) => void): void;
32
32
  }
@@ -24,7 +24,7 @@ export default class InstancedMeshBase extends MeshBase {
24
24
  static _scale = new Vector3(1, 1, 1);
25
25
  static _rotation = new Euler();
26
26
  static _Quaternion = new Quaternion();
27
- static matrix = new Matrix4();
27
+ static _matrix = new Matrix4();
28
28
  instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
29
29
  instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
30
30
  // 重构加载模型的方法
@@ -111,16 +111,19 @@ export default class InstancedMeshBase extends MeshBase {
111
111
  }
112
112
  // 更新包围盒
113
113
  computeBoundingSphere = throttle(() => {
114
- this.traverse(m => {
115
- if (m !== this) {
116
- m.computeBoundingSphere();
117
- }
114
+ this.children.map(instancedMesh => {
115
+ instancedMesh.computeBoundingSphere();
118
116
  });
119
117
  }, 1000);
120
118
  //初始实例矩阵的数量
121
- setCount(count = 0) {
119
+ async setCount(count = 0) {
120
+ await this.awaitLoaded();
122
121
  const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
123
122
  const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
123
+ for (let i = 0; i < count; i++) {
124
+ instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i);
125
+ instanceColor.set([1, 1, 1], i);
126
+ }
124
127
  for (let i = 0; i < this.count; i++) {
125
128
  instanceMatrix.copyAt(i, this.instanceMatrix, i);
126
129
  instanceColor.copyAt(i, this.instanceColor, i);
@@ -141,44 +144,47 @@ export default class InstancedMeshBase extends MeshBase {
141
144
  });
142
145
  }
143
146
  // 设置矩阵
144
- setMatrixAt(index, matrix) {
147
+ async setMatrixAt(index, matrix) {
148
+ await this.awaitLoaded();
145
149
  Object.keys(this.meshCollect).forEach(uuid => {
146
150
  const instancedMesh = this.instanceCollect[uuid];
147
151
  const collect = this.meshCollect[uuid];
148
152
  collect.forEach((mesh, i) => {
149
- InstancedMeshBase.matrix.copy(mesh.matrix);
150
- InstancedMeshBase.matrix.premultiply(matrix);
151
- instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
153
+ InstancedMeshBase._matrix.copy(mesh.matrix);
154
+ InstancedMeshBase._matrix.premultiply(matrix);
155
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
152
156
  instancedMesh.instanceMatrix.needsUpdate = true;
153
157
  });
154
158
  });
155
159
  this.computeBoundingSphere();
156
160
  }
157
161
  //设置移动旋转缩放
158
- setTRS(index, { position = InstancedMeshBase._position, rotation = InstancedMeshBase._rotation, scale = InstancedMeshBase._scale }) {
162
+ async setTRS(index, { position = InstancedMeshBase._position, rotation = InstancedMeshBase._rotation, scale = InstancedMeshBase._scale }) {
163
+ await this.awaitLoaded();
159
164
  Object.keys(this.meshCollect).forEach(uuid => {
160
165
  const instancedMesh = this.instanceCollect[uuid];
161
166
  const collect = this.meshCollect[uuid];
162
167
  collect.forEach((mesh, i) => {
163
168
  //获取对应的模型的矩阵
164
- instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
165
- InstancedMeshBase.matrix.premultiply(mesh.matrix.clone().invert());
166
- InstancedMeshBase.matrix.decompose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
169
+ instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
170
+ InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
171
+ InstancedMeshBase._matrix.decompose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
167
172
  InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
168
173
  InstancedMeshBase._position.copy(position);
169
174
  InstancedMeshBase._rotation.copy(rotation);
170
175
  InstancedMeshBase._scale.copy(scale);
171
176
  InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
172
- InstancedMeshBase.matrix.compose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
173
- InstancedMeshBase.matrix.premultiply(mesh.matrix);
174
- instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
177
+ InstancedMeshBase._matrix.compose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
178
+ InstancedMeshBase._matrix.premultiply(mesh.matrix);
179
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
175
180
  instancedMesh.instanceMatrix.needsUpdate = true;
176
181
  });
177
182
  });
178
183
  this.computeBoundingSphere();
179
184
  }
180
185
  // 设置颜色
181
- setColorAt(index, color) {
186
+ async setColorAt(index, color) {
187
+ await this.awaitLoaded();
182
188
  Object.keys(this.meshCollect).forEach(uuid => {
183
189
  const instancedMesh = this.instanceCollect[uuid];
184
190
  const collect = this.meshCollect[uuid];
@@ -200,7 +206,8 @@ export default class InstancedMeshBase extends MeshBase {
200
206
  });
201
207
  }
202
208
  // 事件监听
203
- addEventListener(name, callback) {
209
+ async addEventListener(name, callback) {
210
+ await this.awaitLoaded();
204
211
  super.addEventListener(name, callback);
205
212
  this.children.map(mesh => {
206
213
  mesh.addEventListener(name, callback);
@@ -35,7 +35,7 @@ export default class InstancedMeshBase extends MeshBase {
35
35
  static _scale: Vector3 = new Vector3(1, 1, 1);
36
36
  static _rotation: Euler = new Euler();
37
37
  static _Quaternion: Quaternion = new Quaternion();
38
- static matrix = new Matrix4()
38
+ static _matrix = new Matrix4()
39
39
 
40
40
  instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
41
41
 
@@ -146,22 +146,25 @@ export default class InstancedMeshBase extends MeshBase {
146
146
 
147
147
  }
148
148
 
149
-
150
149
  // 更新包围盒
151
150
  computeBoundingSphere = throttle(() => {
152
- this.traverse(m => {
153
- if (m !== this) {
154
- (m as InstancedMesh).computeBoundingSphere();
155
- }
156
- })
151
+ this.children.map(instancedMesh => {
152
+ instancedMesh.computeBoundingSphere();
153
+ });
157
154
  }, 1000)
158
155
 
159
156
  //初始实例矩阵的数量
160
- setCount(count: number = 0) {
157
+ async setCount(count: number = 0) {
158
+ await this.awaitLoaded();
161
159
 
162
160
  const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
163
161
  const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
164
162
 
163
+ for (let i = 0; i < count; i++) {
164
+ instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i);
165
+ instanceColor.set([1, 1, 1], i);
166
+ }
167
+
165
168
  for (let i = 0; i < this.count; i++) {
166
169
  instanceMatrix.copyAt(i, this.instanceMatrix, i);
167
170
  instanceColor.copyAt(i, this.instanceColor, i);
@@ -189,15 +192,17 @@ export default class InstancedMeshBase extends MeshBase {
189
192
  }
190
193
 
191
194
  // 设置矩阵
192
- setMatrixAt(index: number, matrix: Matrix4) {
195
+ async setMatrixAt(index: number, matrix: Matrix4) {
196
+ await this.awaitLoaded();
197
+
193
198
  Object.keys(this.meshCollect).forEach(uuid => {
194
199
  const instancedMesh = this.instanceCollect[uuid]
195
200
  const collect = this.meshCollect[uuid]
196
201
  collect.forEach((mesh: Mesh, i: number) => {
197
- InstancedMeshBase.matrix.copy(mesh.matrix)
198
- InstancedMeshBase.matrix.premultiply(matrix)
202
+ InstancedMeshBase._matrix.copy(mesh.matrix)
203
+ InstancedMeshBase._matrix.premultiply(matrix)
199
204
 
200
- instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
205
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
201
206
  instancedMesh.instanceMatrix.needsUpdate = true;
202
207
  })
203
208
  })
@@ -206,7 +211,7 @@ export default class InstancedMeshBase extends MeshBase {
206
211
  }
207
212
 
208
213
  //设置移动旋转缩放
209
- setTRS(
214
+ async setTRS(
210
215
  index: number,
211
216
  {
212
217
  position = InstancedMeshBase._position,
@@ -218,17 +223,19 @@ export default class InstancedMeshBase extends MeshBase {
218
223
  scale?: Vector3
219
224
  }
220
225
  ) {
226
+ await this.awaitLoaded();
227
+
221
228
  Object.keys(this.meshCollect).forEach(uuid => {
222
229
  const instancedMesh = this.instanceCollect[uuid]
223
230
  const collect = this.meshCollect[uuid]
224
231
  collect.forEach((mesh: Mesh, i: number) => {
225
232
 
226
233
  //获取对应的模型的矩阵
227
- instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
234
+ instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
228
235
 
229
- InstancedMeshBase.matrix.premultiply(mesh.matrix.clone().invert());
236
+ InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
230
237
 
231
- InstancedMeshBase.matrix.decompose(
238
+ InstancedMeshBase._matrix.decompose(
232
239
  InstancedMeshBase._position,
233
240
  InstancedMeshBase._Quaternion,
234
241
  InstancedMeshBase._scale
@@ -239,15 +246,15 @@ export default class InstancedMeshBase extends MeshBase {
239
246
  InstancedMeshBase._scale.copy(scale);
240
247
  InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
241
248
 
242
- InstancedMeshBase.matrix.compose(
249
+ InstancedMeshBase._matrix.compose(
243
250
  InstancedMeshBase._position,
244
251
  InstancedMeshBase._Quaternion,
245
252
  InstancedMeshBase._scale
246
253
  );
247
254
 
248
- InstancedMeshBase.matrix.premultiply(mesh.matrix);
255
+ InstancedMeshBase._matrix.premultiply(mesh.matrix);
249
256
 
250
- instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
257
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
251
258
  instancedMesh.instanceMatrix.needsUpdate = true;
252
259
  })
253
260
  })
@@ -256,7 +263,9 @@ export default class InstancedMeshBase extends MeshBase {
256
263
  }
257
264
 
258
265
  // 设置颜色
259
- setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined) {
266
+ async setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined) {
267
+ await this.awaitLoaded();
268
+
260
269
  Object.keys(this.meshCollect).forEach(uuid => {
261
270
  const instancedMesh = this.instanceCollect[uuid]
262
271
  const collect = this.meshCollect[uuid]
@@ -280,7 +289,9 @@ export default class InstancedMeshBase extends MeshBase {
280
289
  }
281
290
 
282
291
  // 事件监听
283
- addEventListener(name: string, callback: (e: any) => void) {
292
+ async addEventListener(name: string, callback: (e: any) => void) {
293
+ await this.awaitLoaded();
294
+
284
295
  super.addEventListener(name, callback);
285
296
  this.children.map(mesh => {
286
297
  mesh.addEventListener(name, callback);
package/ThreeBase.d.ts CHANGED
@@ -19,6 +19,7 @@ export default class ThreeBase extends EventDispatcher {
19
19
  setCameraType(cameraType: "perspectiveCamera" | "orthographicCamera"): void;
20
20
  getCamera(): PerspectiveCamera | OrthographicCamera;
21
21
  scene: Scene;
22
+ add(...object: Object3D[]): this;
22
23
  threeMouse: ThreeMouse;
23
24
  controls: OrbitControls;
24
25
  setContainer(div: HTMLDivElement | null): Promise<void>;
package/ThreeBase.js CHANGED
@@ -84,7 +84,7 @@ export default class ThreeBase extends EventDispatcher {
84
84
  perspectiveCamera = new PerspectiveCamera(45, 1, 0.01, 10000);
85
85
  //正交相机
86
86
  orthographicCamera = new OrthographicCamera(-1, 1, 1, -1, 0.01, 10000);
87
- //相机的类型
87
+ //相机的类型 有透视相机和正交相机
88
88
  cameraType = "perspectiveCamera";
89
89
  setCameraType(cameraType) {
90
90
  if (this.cameraType !== cameraType) {
@@ -101,14 +101,22 @@ export default class ThreeBase extends EventDispatcher {
101
101
  }
102
102
  //获取当前使用的的相机
103
103
  getCamera() {
104
- //判断需要使用那个相机
105
- const camera = ((this.cameraType === "perspectiveCamera") ?
106
- this.perspectiveCamera :
107
- this.orthographicCamera);
108
- return camera;
104
+ switch (this.cameraType) {
105
+ case "orthographicCamera":
106
+ return this.orthographicCamera;
107
+ case "perspectiveCamera":
108
+ default:
109
+ return this.perspectiveCamera;
110
+ }
109
111
  }
110
112
  // 场景对象
111
113
  scene = new Scene();
114
+ // 这个方法等效 this.scene.add
115
+ // 想少写2个字母就用这个方法
116
+ add(...object) {
117
+ this.scene.add(...object);
118
+ return this;
119
+ }
112
120
  ///鼠标事件对象
113
121
  threeMouse = new ThreeMouse(this.renderer, this.perspectiveCamera, this.scene);
114
122
  // 轨道控制器
package/ThreeBase.ts CHANGED
@@ -99,7 +99,7 @@ export default class ThreeBase extends EventDispatcher {
99
99
  //正交相机
100
100
  orthographicCamera = new OrthographicCamera(- 1, 1, 1, - 1, 0.01, 10000);
101
101
 
102
- //相机的类型
102
+ //相机的类型 有透视相机和正交相机
103
103
  cameraType: "perspectiveCamera" | "orthographicCamera" = "perspectiveCamera";
104
104
  setCameraType(cameraType: "perspectiveCamera" | "orthographicCamera") {
105
105
  if (this.cameraType !== cameraType) {
@@ -115,16 +115,25 @@ export default class ThreeBase extends EventDispatcher {
115
115
  }
116
116
  //获取当前使用的的相机
117
117
  getCamera() {
118
- //判断需要使用那个相机
119
- const camera = ((this.cameraType === "perspectiveCamera") ?
120
- this.perspectiveCamera :
121
- this.orthographicCamera);
122
- return camera;
118
+ switch (this.cameraType) {
119
+ case "orthographicCamera":
120
+ return this.orthographicCamera;
121
+ case "perspectiveCamera":
122
+ default:
123
+ return this.perspectiveCamera;
124
+ }
123
125
  }
124
126
 
125
127
  // 场景对象
126
128
  scene = new Scene();
127
129
 
130
+ // 这个方法等效 this.scene.add
131
+ // 想少写2个字母就用这个方法
132
+ add(...object: Object3D[]) {
133
+ this.scene.add(...object);
134
+ return this;
135
+ }
136
+
128
137
  ///鼠标事件对象
129
138
  threeMouse = new ThreeMouse(this.renderer, this.perspectiveCamera, this.scene);
130
139
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "2.0.71",
3
+ "version": "2.0.73",
4
4
  "description": "three 3d three-base",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"