three-zoo 0.11.3 → 0.11.4

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,4 +1,7 @@
1
1
  export { DualFovCamera } from "./DualFovCamera";
2
+ export { InstancedMeshGroup } from "./instancedMeshPool/InstancedMeshGroup";
3
+ export { InstancedMeshInstance } from "./instancedMeshPool/InstancedMeshInstance";
4
+ export { InstancedMeshPool } from "./instancedMeshPool/InstancedMeshPool";
2
5
  export { BasicToPhysicalConverter } from "./materialConverters/BasicToPhysicalConverter";
3
6
  export { StandardToBasicConverter } from "./materialConverters/StandardToBasicConverter";
4
7
  export { StandardToLambertConverter } from "./materialConverters/StandardToLambertConverter";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { PerspectiveCamera, MathUtils, Vector3, MeshPhysicalMaterial, Color, MeshBasicMaterial, MeshLambertMaterial, MeshPhongMaterial, MeshToonMaterial, Mesh, BufferAttribute, AnimationMixer, HemisphereLight, RGBAFormat, DirectionalLight, Box3, Spherical } from 'three';
1
+ import { PerspectiveCamera, MathUtils, Vector3, Object3D, Matrix4, Quaternion, InstancedMesh, MeshPhysicalMaterial, Color, MeshBasicMaterial, MeshLambertMaterial, MeshPhongMaterial, MeshToonMaterial, Mesh, BufferAttribute, AnimationMixer, HemisphereLight, RGBAFormat, DirectionalLight, Box3, Spherical } from 'three';
2
2
 
3
3
  /** Default horizontal field of view in degrees */
4
4
  const DEFAULT_HORIZONTAL_FOV = 90;
@@ -242,6 +242,181 @@ class DualFovCamera extends PerspectiveCamera {
242
242
  }
243
243
  }
244
244
 
245
+ class InstancedMeshGroup extends Object3D {
246
+ constructor(_private_instances) {
247
+ super();
248
+ this._private_instances = _private_instances;
249
+ }
250
+ flushTransform() {
251
+ this.updateWorldMatrix(true, false);
252
+ for (const instance of this._private_instances) {
253
+ instance.setTransform(this.matrixWorld);
254
+ }
255
+ }
256
+ destroy() {
257
+ for (const instance of this._private_instances) {
258
+ instance.destroy();
259
+ }
260
+ }
261
+ }
262
+
263
+ class InstancedMeshInstance {
264
+ constructor(pool, entry, index) {
265
+ this._private_pool = pool;
266
+ this["entry"] = entry;
267
+ this.index = index;
268
+ this._private_matrix = new Matrix4();
269
+ this._private_position = new Vector3();
270
+ this._private_quaternion = new Quaternion();
271
+ this._private_scale = new Vector3(1, 1, 1);
272
+ }
273
+ setPosition(v) {
274
+ this._private_position.copy(v);
275
+ this._private_apply();
276
+ return this;
277
+ }
278
+ setPosition3f(x, y, z) {
279
+ this._private_position.set(x, y, z);
280
+ this._private_apply();
281
+ return this;
282
+ }
283
+ setQuaternion(q) {
284
+ this._private_quaternion.copy(q);
285
+ this._private_apply();
286
+ return this;
287
+ }
288
+ setQuaternion4f(x, y, z, w) {
289
+ this._private_quaternion.set(x, y, z, w);
290
+ this._private_apply();
291
+ return this;
292
+ }
293
+ setScale(v) {
294
+ this._private_scale.copy(v);
295
+ this._private_apply();
296
+ return this;
297
+ }
298
+ setScale3f(x, y, z) {
299
+ this._private_scale.set(x, y, z);
300
+ this._private_apply();
301
+ return this;
302
+ }
303
+ setTransform(m) {
304
+ m.decompose(this._private_position, this._private_quaternion, this._private_scale);
305
+ this._private_apply();
306
+ return this;
307
+ }
308
+ destroy() {
309
+ if (this.index === -1)
310
+ return;
311
+ this._private_pool.deallocate(this);
312
+ }
313
+ _private_apply() {
314
+ if (this.index === -1)
315
+ return;
316
+ this._private_matrix.compose(this._private_position, this._private_quaternion, this._private_scale);
317
+ this["entry"].mesh.setMatrixAt(this.index, this._private_matrix);
318
+ this._private_pool["notifyUpdate"](this["entry"]);
319
+ }
320
+ }
321
+
322
+ const TEMP_ZERO_MATRIX = new Matrix4().makeScale(0, 0, 0);
323
+ class InstancedMeshPool {
324
+ constructor(options) {
325
+ var _a, _b;
326
+ this._private_scene = options.scene;
327
+ this._private_initialCapacity = (_a = options.initialCapacity) !== null && _a !== void 0 ? _a : 16;
328
+ this._private_capacityStep = (_b = options.capacityStep) !== null && _b !== void 0 ? _b : this._private_initialCapacity;
329
+ this._private_meshes = new Map();
330
+ }
331
+ allocate(geometry, material) {
332
+ const entry = this._private_getOrCreateEntry(geometry, material);
333
+ if (entry.freeIndices.length === 0) {
334
+ this._private_growEntry(entry);
335
+ }
336
+ const index = entry.freeIndices.pop();
337
+ const instance = new InstancedMeshInstance(this, entry, index);
338
+ // identity transform
339
+ instance.setScale3f(1, 1, 1);
340
+ entry.instances.add(instance);
341
+ this._private_updateMeshCount(entry);
342
+ return instance;
343
+ }
344
+ deallocate(instance) {
345
+ if (instance.index === -1)
346
+ return;
347
+ const entry = instance["entry"];
348
+ entry.instances.delete(instance);
349
+ entry.freeIndices.push(instance.index);
350
+ // hide instance
351
+ entry.mesh.setMatrixAt(instance.index, TEMP_ZERO_MATRIX);
352
+ instance.index = -1;
353
+ this._private_updateMeshCount(entry);
354
+ this["notifyUpdate"](entry);
355
+ }
356
+ _private_getOrCreateEntry(geometry, material) {
357
+ const key = `${geometry.uuid}:${material.uuid}`;
358
+ let entry = this._private_meshes.get(key);
359
+ if (!entry) {
360
+ const mesh = new InstancedMesh(geometry, material, this._private_initialCapacity);
361
+ mesh.count = 0;
362
+ mesh.frustumCulled = false;
363
+ this._private_scene.add(mesh);
364
+ entry = {
365
+ mesh,
366
+ geometry,
367
+ material,
368
+ capacity: this._private_initialCapacity,
369
+ instances: new Set(),
370
+ freeIndices: this._private_createIndexRange(0, this._private_initialCapacity),
371
+ };
372
+ this._private_meshes.set(key, entry);
373
+ }
374
+ return entry;
375
+ }
376
+ _private_growEntry(entry) {
377
+ const newCapacity = entry.capacity + this._private_capacityStep;
378
+ const newMesh = new InstancedMesh(entry.geometry, entry.material, newCapacity);
379
+ newMesh.frustumCulled = false;
380
+ // copy existing matrix data
381
+ const oldArray = entry.mesh.instanceMatrix.array;
382
+ const newArray = newMesh.instanceMatrix.array;
383
+ newArray.set(oldArray);
384
+ newMesh.instanceMatrix.needsUpdate = true;
385
+ // swap meshes
386
+ this._private_scene.remove(entry.mesh);
387
+ entry.mesh.dispose();
388
+ this._private_scene.add(newMesh);
389
+ // add new free indices
390
+ entry.freeIndices.push(...this._private_createIndexRange(entry.capacity, newCapacity));
391
+ entry.mesh = newMesh;
392
+ entry.capacity = newCapacity;
393
+ this._private_updateMeshCount(entry);
394
+ }
395
+ _private_updateMeshCount(entry) {
396
+ if (entry.instances.size === 0) {
397
+ entry.mesh.count = 0;
398
+ return;
399
+ }
400
+ let maxIndex = -1;
401
+ for (const inst of entry.instances) {
402
+ if (inst.index > maxIndex) {
403
+ maxIndex = inst.index;
404
+ }
405
+ }
406
+ entry.mesh.count = maxIndex + 1;
407
+ }
408
+ _private_createIndexRange(start, end) {
409
+ const result = [];
410
+ for (let i = start; i < end; i++) {
411
+ result.push(i);
412
+ }
413
+ return result;
414
+ }
415
+ ["notifyUpdate"](entry) {
416
+ entry.mesh.instanceMatrix.needsUpdate = true;
417
+ }
418
+ }
419
+
245
420
  class BasicToPhysicalConverter {
246
421
  static convert(material, options = {}) {
247
422
  const config = Object.assign({ preserveName: true, copyUserData: true, disposeOriginal: false }, options);
@@ -1765,5 +1940,5 @@ class Sun extends DirectionalLight {
1765
1940
  }
1766
1941
  }
1767
1942
 
1768
- export { BasicToPhysicalConverter, DualFovCamera, SceneTraversal, SkinnedMeshBaker, SkyLight, StandardToBasicConverter, StandardToLambertConverter, StandardToPhongConverter, StandardToPhysicalConverter, StandardToToonConverter, Sun };
1943
+ export { BasicToPhysicalConverter, DualFovCamera, InstancedMeshGroup, InstancedMeshInstance, InstancedMeshPool, SceneTraversal, SkinnedMeshBaker, SkyLight, StandardToBasicConverter, StandardToLambertConverter, StandardToPhongConverter, StandardToPhysicalConverter, StandardToToonConverter, Sun };
1769
1944
  //# sourceMappingURL=index.js.map