@woosh/meep-engine 2.33.24 → 2.33.28

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.
Files changed (126) hide show
  1. package/core/binary/BitSet.js +151 -167
  2. package/core/binary/BitSet.spec.js +99 -1
  3. package/core/binary/ValidatingBitSetWrapper.js +81 -0
  4. package/core/bvh2/aabb3/AABB3.js +3 -10
  5. package/core/bvh2/binary/IndexedBinaryBVH.js +1 -1
  6. package/core/bvh2/traversal/ThreeClippingPlaneComputingBVHVisitor.js +5 -1
  7. package/core/bvh2/traversal/__detailed_box_volume_intersection.js +22 -5
  8. package/core/cache/Cache.d.ts +4 -2
  9. package/core/cache/Cache.js +5 -2
  10. package/core/collection/HashMap.d.ts +9 -2
  11. package/core/collection/HashSet.d.ts +7 -2
  12. package/core/collection/HashSet.js +7 -2
  13. package/core/collection/array/binarySearchHighIndex.js +1 -1
  14. package/core/collection/array/copyArray.js +2 -2
  15. package/core/collection/array/typed/typed_array_copy.js +1 -1
  16. package/core/geom/3d/frustum/array_setFromProjectionMatrix.js +3 -3
  17. package/core/geom/3d/normal/hemioct/unit_hemioct.spec.js +1 -1
  18. package/core/geom/3d/shape/AbstractShape3D.js +37 -1
  19. package/core/geom/3d/shape/TransformedShape3D.js +121 -10
  20. package/core/geom/3d/shape/TransformedShape3D.spec.js +33 -0
  21. package/core/geom/3d/shape/UnionShape3D.js +255 -0
  22. package/core/geom/3d/shape/UnitCubeShape3D.js +88 -0
  23. package/core/geom/3d/shape/UnitCubeShape3D.spec.js +42 -0
  24. package/core/geom/3d/shape/UnitSphereShape3D.js +39 -0
  25. package/core/geom/3d/shape/util/compute_signed_distance_gradient_by_sampling.js +48 -0
  26. package/core/geom/3d/shape/util/shape_to_visual_entity.js +159 -0
  27. package/core/geom/random/randomPointInSphere.js +3 -2
  28. package/core/json/resolvePathByArray.js +2 -2
  29. package/core/model/ObservedBoolean.d.ts +2 -0
  30. package/core/parser/simple/Token.d.ts +13 -0
  31. package/core/parser/simple/Token.js +61 -27
  32. package/{engine/graphics/render/forward_plus/materials → core/primitives/strings}/insert_after.js +3 -0
  33. package/core/primitives/strings/insert_after_or_replace.js +27 -0
  34. package/editor/process/symbolic/makeHelperBoxGeometry.js +44 -0
  35. package/editor/tools/FoliagePaintTool.js +4 -4
  36. package/editor/view/ecs/EntityEditor.js +2 -2
  37. package/editor/view/ecs/components/particles/ParticleEmitterController.js +2 -2
  38. package/engine/EngineHarness.js +1 -0
  39. package/engine/animation/AnimationUtils.js +5 -5
  40. package/engine/asset/loaders/GLTFAssetLoader.js +52 -1
  41. package/engine/asset/loaders/image/ImageDecoderWorker.js +4 -3
  42. package/engine/asset/loaders/image/ImageRGBADataAsset.js +3 -2
  43. package/engine/asset/loaders/image/ImageRGBADataLoader.js +3 -3
  44. package/engine/asset/loaders/image/codec/NativeImageDecoder.js +2 -1
  45. package/engine/asset/loaders/image/png/PNG.js +113 -6
  46. package/engine/asset/loaders/image/png/PNGReader.js +5 -0
  47. package/engine/asset/loaders/material/bitmap2sampler_gl.js +1 -1
  48. package/engine/asset/loaders/material/computeImageBitmapEquality.js +6 -1
  49. package/engine/asset/loaders/material/computeTextureEquality.js +40 -49
  50. package/engine/ecs/EntityBuilder.js +25 -5
  51. package/engine/ecs/EntityComponentDataset.js +1 -1
  52. package/engine/ecs/attachment/AttachmentBinding.js +8 -0
  53. package/engine/ecs/attachment/AttachmentSystem.js +4 -0
  54. package/engine/ecs/attachment/TransformAttachmentBinding.js +14 -44
  55. package/engine/ecs/components/Tag.js +27 -6
  56. package/engine/ecs/foliage/InstancedFoliage.js +277 -266
  57. package/engine/ecs/foliage/ecs/Foliage2System.js +7 -7
  58. package/engine/ecs/foliage/ecs/InstancedMeshComponent.js +37 -0
  59. package/engine/ecs/foliage/ecs/{Foliage2.js → InstancedMeshLayer.js} +3 -45
  60. package/engine/ecs/foliage/ecs/InstancedMeshSerializationAdapter.js +6 -5
  61. package/engine/ecs/foliage/ecs/InstancedMeshUtils.js +7 -6
  62. package/engine/ecs/fow/FogOfWar.js +4 -0
  63. package/engine/ecs/fow/FogOfWarSystem.js +2 -0
  64. package/engine/ecs/gui/hud/HeadsUpDisplaySystem.js +2 -0
  65. package/engine/ecs/storage/binary/BinarySerializationRegistry.js +1 -1
  66. package/engine/ecs/system/AbstractContextSystem.js +1 -1
  67. package/engine/ecs/systems/TagSystem.js +17 -0
  68. package/engine/ecs/terrain/ecs/layers/TerrainLayer.js +12 -16
  69. package/engine/ecs/transform/Transform.js +19 -6
  70. package/engine/graphics/GraphicsEngine.js +11 -6
  71. package/engine/graphics/composit/CompositLayer.js +17 -4
  72. package/engine/graphics/composit/CompositingStages.js +8 -0
  73. package/engine/graphics/composit/LayerCompositer.js +35 -6
  74. package/engine/graphics/ecs/highlight/plugin/OutlineRenderPlugin.js +11 -7
  75. package/engine/graphics/ecs/light/Light.js +2 -0
  76. package/engine/graphics/ecs/light/binding/three/ThreeLightBinding.js +5 -1
  77. package/engine/graphics/ecs/mesh-v2/DrawMode.js +5 -0
  78. package/engine/graphics/ecs/mesh-v2/ShadedGeometry.js +32 -18
  79. package/engine/graphics/geometry/instancing/InstancedMeshGroup.js +14 -72
  80. package/engine/graphics/geometry/instancing/rewriteMaterial.js +70 -0
  81. package/engine/graphics/material/manager/MaterialManager.js +2 -1
  82. package/engine/graphics/micron/render/instanced/AttributeDataTexture.js +6 -4
  83. package/engine/graphics/micron/render/instanced/ThreeInstancedAdapter.js +0 -2
  84. package/engine/graphics/particles/ParticleEmitterLibrary.js +3 -0
  85. package/engine/graphics/particles/ecs/{ParticleEmitterSystem2.js → ParticleEmitterSystem.js} +17 -9
  86. package/engine/graphics/particles/particular/engine/ParticularEngine.js +8 -2
  87. package/engine/graphics/particles/particular/engine/emitter/ParticleEmitter.js +14 -25
  88. package/engine/graphics/particles/particular/engine/emitter/ParticleEmitterFlag.js +5 -0
  89. package/engine/graphics/particles/particular/engine/emitter/ParticlePool.js +90 -2
  90. package/engine/graphics/particles/particular/engine/emitter/ParticlePool.spec.js +7 -7
  91. package/engine/graphics/particles/particular/engine/renderers/billboard/BILLBOARD_PARTICLE_SPECIFICATION.js +15 -12
  92. package/engine/graphics/particles/particular/engine/renderers/billboard/ParticleBillboardMaterial.js +262 -51
  93. package/engine/graphics/particles/particular/engine/shader/ShaderManager.js +81 -48
  94. package/engine/graphics/particles/particular/engine/shader/debugAtlas.js +14 -0
  95. package/engine/graphics/particles/particular/engine/utils/volume/AttributeValue.js +141 -0
  96. package/engine/graphics/particles/particular/engine/utils/volume/ParticleVolume.js +148 -54
  97. package/engine/graphics/particles/particular/engine/utils/volume/lut_to_sampler.js +27 -0
  98. package/engine/graphics/particles/particular/engine/utils/volume/prototypeParticleVolume.js +544 -164
  99. package/engine/graphics/render/buffer/simple-fx/ao/AmbientOcclusionPostProcessEffect.js +15 -10
  100. package/engine/graphics/render/forward_plus/materials/FP_INJECTION_POINT_ACCUMULATION.js +1 -0
  101. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_ACCUMULATION.js +32 -0
  102. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_DECODE_PARS.js +51 -0
  103. package/engine/graphics/render/forward_plus/materials/FP_SHADER_CHUNK_PREAMBLE.js +25 -0
  104. package/engine/graphics/render/forward_plus/materials/FPlusDebugMaterial.js +3 -3
  105. package/engine/graphics/render/forward_plus/materials/ForwardPlusThreeMaterial.js +1 -1
  106. package/engine/graphics/render/forward_plus/materials/fp_build_fragment_shader.js +31 -120
  107. package/engine/graphics/render/forward_plus/materials/fp_build_vertex_lighting_shared.js +35 -0
  108. package/engine/graphics/render/forward_plus/materials/fp_build_vertex_shader.js +4 -5
  109. package/engine/graphics/render/forward_plus/plugin/MaterialTransformer.js +10 -3
  110. package/engine/graphics/render/forward_plus/plugin/ptototypeFPPlugin.js +588 -53
  111. package/engine/graphics/render/forward_plus/prototypeLightManager.js +31 -6
  112. package/engine/graphics/render/forward_plus/query/point_light_inside_volume.js +10 -9
  113. package/engine/graphics/shaders/TerrainShader.js +4 -5
  114. package/engine/graphics/shadows/ShadowMapRenderer.js +8 -2
  115. package/engine/graphics/texture/atlas/ManagedTextureAtlas.js +12 -1
  116. package/engine/graphics/texture/sampler/Sampler2D.js +3 -1
  117. package/engine/graphics/texture/sampler/convertTexture2Sampler2D.js +1 -1
  118. package/engine/graphics/texture/sampler/copy_Sampler2D_channel_data.js +66 -0
  119. package/engine/graphics/util/composeMatrix4.js +33 -23
  120. package/engine/graphics/util/decompose_matrix_4_array.js +61 -0
  121. package/engine/intelligence/behavior/ecs/BehaviorComponent.js +30 -14
  122. package/engine/knowledge/database/StaticKnowledgeDataTable.js +6 -1
  123. package/engine/navigation/ecs/components/Path.js +0 -5
  124. package/package.json +1 -1
  125. package/view/minimap/gl/MinimapWorldGL.js +12 -5
  126. package/core/geom/3d/shape/CompositeShape.js +0 -60
@@ -17,6 +17,7 @@ import { deserializeBinaryNodeFromBinaryBuffer } from "../../../core/bvh2/serial
17
17
  import { computeGeometryBoundingSphereMiniball } from "../../graphics/geometry/buffered/computeGeometryBoundingSphereMiniball.js";
18
18
  import { serializeRowFirstTable } from "../../../core/collection/table/serializeRowFirstTable.js";
19
19
  import { deserializeRowFirstTable } from "../../../core/collection/table/deserializeRowFirstTable.js";
20
+ import { assert } from "../../../core/assert.js";
20
21
 
21
22
  /**
22
23
  * @readonly
@@ -28,364 +29,374 @@ const dataSpec = new RowFirstTableSpec([
28
29
  DataType.Float32, DataType.Float32, DataType.Float32 //scale
29
30
  ]);
30
31
 
31
- /**
32
- *
33
- * @constructor
34
- */
35
- function InstancedFoliage() {
36
- /**
37
- * Minimum area on the screen to be occupied by an instance, if instance bounding sphere occupies less than this - it will be culled
38
- * Measured in pixels
39
- * @type {number}
40
- */
41
- this.minScreenArea = 32;
42
-
32
+ export class InstancedFoliage {
43
33
  /**
44
34
  *
45
- * @type {BinaryNode}
35
+ * @constructor
46
36
  */
47
- this.bvh = new BinaryNode();
48
- this.bvh.setNegativelyInfiniteBounds();
37
+ constructor() {
38
+ /**
39
+ * Minimum area on the screen to be occupied by an instance, if instance bounding sphere occupies less than this - it will be culled
40
+ * Measured in pixels
41
+ * @type {number}
42
+ */
43
+ this.minScreenArea = 32;
44
+
45
+ /**
46
+ *
47
+ * @type {BinaryNode}
48
+ */
49
+ this.bvh = new BinaryNode();
50
+ this.bvh.setNegativelyInfiniteBounds();
51
+
52
+ /**
53
+ *
54
+ * @type {RowFirstTable}
55
+ */
56
+ this.data = new RowFirstTable(dataSpec);
57
+
58
+ /**
59
+ *
60
+ * @type {InstancedMeshGroup}
61
+ */
62
+ this.instances = new InstancedMeshGroup();
63
+
64
+ this.geometry = null;
65
+ this.instanceBoundingSphere = new Vector4(0, 0, 0, 0);
66
+
67
+
68
+ this.bvhVisibilityVisitor = new ThreeFrustumsIntersectionBVHVisitor();
69
+
70
+ this.visibilitySetBuilder = new FoliageVisibilitySetBuilder();
71
+ this.bvhVisibilityVisitor.collector = this.visibilitySetBuilder;
72
+
73
+ /**
74
+ *
75
+ * @type {*[]}
76
+ * @private
77
+ */
78
+ this.__garbage = [];
79
+ /**
80
+ *
81
+ * @type {number}
82
+ * @private
83
+ */
84
+ this.__garbagePointer = 0;
85
+ }
49
86
 
50
- /**
51
- *
52
- * @type {RowFirstTable}
53
- */
54
- this.data = new RowFirstTable(dataSpec);
87
+ hash() {
88
+ return this.data.hash();
89
+ }
55
90
 
56
- /**
57
- *
58
- * @type {InstancedMeshGroup}
59
- */
60
- this.instances = new InstancedMeshGroup();
91
+ clear() {
61
92
 
62
- this.geometry = null;
63
- this.instanceBoundingSphere = new Vector4(0, 0, 0, 0);
93
+ this.data.clear();
94
+ this.bvh.reset();
64
95
 
96
+ }
65
97
 
66
- this.bvhVisibilityVisitor = new ThreeFrustumsIntersectionBVHVisitor();
98
+ dispose() {
99
+ this.instances.dispose();
100
+ }
67
101
 
68
- this.visibilitySetBuilder = new FoliageVisibilitySetBuilder();
69
- this.bvhVisibilityVisitor.collector = this.visibilitySetBuilder;
102
+ initialize() {
103
+
104
+ }
70
105
 
71
106
  /**
72
107
  *
73
- * @type {*[]}
74
- * @private
75
- */
76
- this.__garbage = [];
77
- /**
78
- *
79
- * @type {number}
80
- * @private
108
+ * @param {Vector3} position
109
+ * @param {Quaternion} rotation
110
+ * @param {Vector3} scale
81
111
  */
82
- this.__garbagePointer = 0;
83
- }
112
+ add(position, rotation, scale) {
113
+ const index = this.data.length;
84
114
 
85
- InstancedFoliage.prototype.hash = function () {
86
- return this.data.hash();
87
- };
115
+ const encodedRotation = rotation.encodeToUint32();
88
116
 
89
- InstancedFoliage.prototype.clear = function () {
117
+ //update data
118
+ this.data.addRow([
119
+ position.x, position.y, position.z,
120
+ encodedRotation,
121
+ scale.x, scale.y, scale.z
122
+ ]);
90
123
 
91
- this.data.clear();
92
- this.bvh.reset();
124
+ const leaf = new LeafNode(
125
+ index,
126
+ Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY,
127
+ Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY
128
+ );
93
129
 
94
- };
130
+ //compute bounding box for this instance
131
+ this.expandBoundingBoxForInstance(position, rotation, scale, leaf);
95
132
 
96
- InstancedFoliage.prototype.dispose = function () {
97
- this.instances.dispose();
98
- };
133
+ //insert bounding box into the spatial index
134
+ this.bvh.insertNode(leaf);
135
+ }
99
136
 
100
- InstancedFoliage.prototype.initialize = function () {
137
+ /**
138
+ *
139
+ * @param {int} index Instance index
140
+ * @param {Vector3} position Position of instance will be read into this vector
141
+ * @param {Quaternion} rotation Rotation of instance will be read into this quaternion
142
+ * @param {Vector3} scale Scale of the instance will be read into this Vector
143
+ */
144
+ read(index, position, rotation, scale) {
145
+ const elementData = [];
146
+ //read transform data for instance
147
+ this.data.getRow(index, elementData);
101
148
 
102
- };
149
+ const positionX = elementData[0];
150
+ const positionY = elementData[1];
151
+ const positionZ = elementData[2];
103
152
 
104
- /**
105
- *
106
- * @param {Vector3} position
107
- * @param {Quaternion} rotation
108
- * @param {Vector3} scale
109
- */
110
- InstancedFoliage.prototype.add = function (position, rotation, scale) {
111
- const index = this.data.length;
153
+ const encodedRotation = elementData[3];
112
154
 
113
- const encodedRotation = rotation.encodeToUint32();
155
+ const scaleX = elementData[4];
156
+ const scaleY = elementData[5];
157
+ const scaleZ = elementData[6];
114
158
 
115
- //update data
116
- this.data.addRow([
117
- position.x, position.y, position.z,
118
- encodedRotation,
119
- scale.x, scale.y, scale.z
120
- ]);
159
+ position.set(positionX, positionY, positionZ);
121
160
 
122
- const leaf = new LeafNode(
123
- index,
124
- Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY,
125
- Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY
126
- );
161
+ rotation.decodeFromUint32(encodedRotation);
127
162
 
128
- //compute bounding box for this instance
129
- this.expandBoundingBoxForInstance(position, rotation, scale, leaf);
163
+ scale.set(scaleX, scaleY, scaleZ);
164
+ }
130
165
 
131
- //insert bounding box into the spatial index
132
- this.bvh.insertNode(leaf);
133
- };
166
+ /**
167
+ *
168
+ * @param {AABB3} result
169
+ * @param {Vector3} position
170
+ * @param {Quaternion} rotation
171
+ * @param {Vector3} scale
172
+ */
173
+ expandBoundingBoxForInstance(position, rotation, scale, result) {
134
174
 
135
- /**
136
- *
137
- * @param {int} index Instance index
138
- * @param {Vector3} position Position of instance will be read into this vector
139
- * @param {Quaternion} rotation Rotation of instance will be read into this quaternion
140
- * @param {Vector3} scale Scale of the instance will be read into this Vector
141
- */
142
- InstancedFoliage.prototype.read = function (index, position, rotation, scale) {
143
- const elementData = [];
144
- //read transform data for instance
145
- this.data.getRow(index, elementData);
175
+ const vertexData = this.geometry.attributes.position.array;
146
176
 
147
- const positionX = elementData[0];
148
- const positionY = elementData[1];
149
- const positionZ = elementData[2];
177
+ const rXYZ = new Vector3(rotation.x, rotation.y, rotation.z);
150
178
 
151
- const encodedRotation = elementData[3];
179
+ const v = new Vector3(0, 0, 0);
152
180
 
153
- const scaleX = elementData[4];
154
- const scaleY = elementData[5];
155
- const scaleZ = elementData[6];
181
+ const c0 = new Vector3(0, 0, 0);
182
+ const c1 = new Vector3(0, 0, 0);
183
+ const c2 = new Vector3(0, 0, 0);
156
184
 
157
- position.set(positionX, positionY, positionZ);
185
+ for (let i = 0, l = vertexData.length; i < l; i += 3) {
186
+ const x = vertexData[i];
187
+ const y = vertexData[i + 1];
188
+ const z = vertexData[i + 2];
158
189
 
159
- rotation.decodeFromUint32(encodedRotation);
190
+ v.set(x, y, z);
160
191
 
161
- scale.set(scaleX, scaleY, scaleZ);
162
- };
192
+ //transform point
163
193
 
164
- /**
165
- *
166
- * @param {AABB3} result
167
- * @param {Vector3} position
168
- * @param {Quaternion} rotation
169
- * @param {Vector3} scale
170
- */
171
- InstancedFoliage.prototype.expandBoundingBoxForInstance = function (position, rotation, scale, result) {
194
+ //apply scale
195
+ v.multiply(scale);
172
196
 
173
- const vertexData = this.geometry.attributes.position.array;
197
+ //apply rotation
198
+ c1.copy(v).multiplyScalar(rotation.w);
199
+ c0.copy(rXYZ).cross(v).add(c1);
200
+ c2.copy(rXYZ).cross(c0).multiplyScalar(2);
174
201
 
175
- const rXYZ = new Vector3(rotation.x, rotation.y, rotation.z);
202
+ v.add(c2);
176
203
 
177
- const v = new Vector3(0, 0, 0);
204
+ //apply translation
205
+ v.add(position);
178
206
 
179
- const c0 = new Vector3(0, 0, 0);
180
- const c1 = new Vector3(0, 0, 0);
181
- const c2 = new Vector3(0, 0, 0);
207
+ result._expandToFitPoint(v.x, v.y, v.z);
208
+ }
209
+ }
182
210
 
183
- for (let i = 0, l = vertexData.length; i < l; i += 3) {
184
- const x = vertexData[i];
185
- const y = vertexData[i + 1];
186
- const z = vertexData[i + 2];
211
+ computeInstanceBoundingSphere() {
212
+ this.instanceBoundingSphere = computeGeometryBoundingSphereMiniball(this.geometry);
213
+ }
187
214
 
188
- v.set(x, y, z);
215
+ setInstance(geometry, material) {
216
+ if (material === undefined) {
217
+ throw new Error(`Material is undefined`);
218
+ }
189
219
 
190
- //transform point
220
+ if (geometry === undefined) {
221
+ throw new Error(`Geometry is undefined`);
222
+ }
191
223
 
192
- //apply scale
193
- v.multiply(scale);
194
224
 
195
- //apply rotation
196
- c1.copy(v).multiplyScalar(rotation.w);
197
- c0.copy(rXYZ).cross(v).add(c1);
198
- c2.copy(rXYZ).cross(c0).multiplyScalar(2);
225
+ let bufferGeometry;
226
+ if ((geometry instanceof BufferGeometry)) {
227
+ bufferGeometry = geometry;
228
+ } else {
229
+ bufferGeometry = new BufferGeometry();
230
+ bufferGeometry.fromGeometry(geometry);
231
+ }
199
232
 
200
- v.add(c2);
201
233
 
202
- //apply translation
203
- v.add(position);
234
+ this.geometry = bufferGeometry;
204
235
 
205
- result._expandToFitPoint(v.x, v.y, v.z);
206
- }
207
- };
236
+ this.instances.setMaterial(material);
237
+ this.instances.setGeometry(bufferGeometry);
208
238
 
209
- InstancedFoliage.prototype.computeInstanceBoundingSphere = function () {
210
- this.instanceBoundingSphere = computeGeometryBoundingSphereMiniball(this.geometry);
211
- };
239
+ this.computeInstanceBoundingSphere();
212
240
 
213
- InstancedFoliage.prototype.setInstance = function (geometry, material) {
214
- if (material === undefined) {
215
- throw new Error(`Material is undefined`);
216
- }
241
+ //TODO: compute haul for the mesh, to use for fast AABB computation
217
242
 
218
- if (geometry === undefined) {
219
- throw new Error(`Geometry is undefined`);
220
243
  }
221
244
 
245
+ /**
246
+ *
247
+ * @param {BinaryBuffer} buffer
248
+ */
249
+ deserialize(buffer) {
250
+ deserializeRowFirstTable(buffer, this.data);
222
251
 
223
- let bufferGeometry;
224
- if ((geometry instanceof BufferGeometry)) {
225
- bufferGeometry = geometry;
226
- } else {
227
- bufferGeometry = new BufferGeometry();
228
- bufferGeometry.fromGeometry(geometry);
252
+ deserializeBinaryNodeFromBinaryBuffer(this.bvh, buffer, deserializeLeafValueUintVar);
229
253
  }
230
254
 
255
+ /**
256
+ *
257
+ * @param {BinaryBuffer} buffer
258
+ */
259
+ serialize(buffer) {
231
260
 
232
- this.geometry = bufferGeometry;
233
-
234
- this.instances.setMaterial(material);
235
- this.instances.setGeometry(bufferGeometry);
261
+ //serialize data
262
+ serializeRowFirstTable(buffer, this.data);
236
263
 
237
- this.computeInstanceBoundingSphere();
264
+ const numRows = this.data.length;
238
265
 
239
- //TODO: compute haul for the mesh, to use for fast AABB computation
266
+ if (numRows > 2147483647) {
267
+ throw new Error(`Data is too large to be written (length=${numRows})`);
268
+ }
240
269
 
241
- };
242
270
 
243
- /**
244
- *
245
- * @param {BinaryBuffer} buffer
246
- * @returns {number}
247
- */
248
- function deserializeLeafValueUintVar(buffer) {
249
- return buffer.readUintVar();
250
- }
271
+ serializeBinaryNodeToBinaryBuffer(this.bvh, buffer, serializeLeafValueUintVar);
272
+ }
251
273
 
252
- InstancedFoliage.prototype.deserialize = function (buffer) {
253
- deserializeRowFirstTable(buffer, this.data);
274
+ /**
275
+ *
276
+ * @param index
277
+ * @param {int} ref
278
+ */
279
+ __updateVisibility(index, ref) {
280
+ assert.isNonNegativeInteger(ref, 'ref');
281
+
282
+ /**
283
+ *
284
+ * @type {FoliageVisibilitySetBuilder}
285
+ */
286
+ const builder = this.visibilitySetBuilder;
287
+
288
+ /**
289
+ *
290
+ * @type {BitSet}
291
+ */
292
+ const visibleElementSet = builder.visibleSet;
293
+
294
+ if (!visibleElementSet.get(ref)) {
295
+ //no longer visible
296
+ this.__garbage[this.__garbagePointer++] = ref;
297
+ } else {
298
+ //visible, and is already in the group, update the set
299
+ visibleElementSet.set(ref, false);
300
+ }
301
+ }
254
302
 
255
- deserializeBinaryNodeFromBinaryBuffer(this.bvh, buffer, deserializeLeafValueUintVar);
256
- };
303
+ __removeGarbage() {
257
304
 
258
- /**
259
- *
260
- * @param {BinaryBuffer} buffer
261
- * @param {number} value
262
- */
263
- function serializeLeafValueUintVar(buffer, value) {
264
- buffer.writeUintVar(value);
265
- }
305
+ const garbagePointer = this.__garbagePointer;
306
+ const instances = this.instances;
266
307
 
267
- InstancedFoliage.prototype.serialize = function (buffer) {
268
308
 
269
- //serialize data
270
- serializeRowFirstTable(buffer, this.data);
309
+ const garbage = this.__garbage;
271
310
 
272
- const numRows = this.data.length;
311
+ for (let i = 0; i < garbagePointer; i++) {
312
+ const ref = garbage[i];
313
+ instances.remove(ref);
314
+ }
273
315
 
274
- if (numRows > 2147483647) {
275
- throw new Error(`Data is too large to be written (length=${numRows})`);
276
316
  }
277
317
 
278
-
279
- serializeBinaryNodeToBinaryBuffer(this.bvh, buffer, serializeLeafValueUintVar);
280
- };
281
-
282
-
283
- const tempQuaternion = new Quaternion();
284
- const rowData = [];
285
-
286
- /**
287
- *
288
- * @param index
289
- * @param {int} ref
290
- */
291
- InstancedFoliage.prototype.__updateVisibility = function (index, ref) {
292
318
  /**
293
319
  *
294
- * @type {FoliageVisibilitySetBuilder}
320
+ * @param {Frustum[]} frustums
321
+ * @param {function[]} [visibilityFilters]
295
322
  */
296
- const builder = this.visibilitySetBuilder;
297
-
298
- /**
299
- *
300
- * @type {BitSet}
301
- */
302
- const visibleElementSet = builder.visibleSet;
303
-
304
- if (!visibleElementSet.get(ref)) {
305
- //no longer visible
306
- this.__garbage[this.__garbagePointer++] = ref;
307
- } else {
308
- //visible, and is already in the group, update the set
309
- visibleElementSet.set(ref, false);
310
- }
311
- };
312
-
313
- InstancedFoliage.prototype.__removeGarbage = function () {
323
+ update(frustums, visibilityFilters = []) {
324
+ const instances = this.instances;
314
325
 
315
- const garbagePointer = this.__garbagePointer;
316
- const instances = this.instances;
326
+ this.bvhVisibilityVisitor.setFrustums(frustums);
317
327
 
328
+ const visibilitySetBuilder = this.visibilitySetBuilder;
329
+ visibilitySetBuilder.setFilters(visibilityFilters);
330
+ visibilitySetBuilder.initialize();
318
331
 
319
- const garbage = this.__garbage;
332
+ const visibleElementSet = visibilitySetBuilder.visibleSet;
320
333
 
321
- for (let i = 0; i < garbagePointer; i++) {
322
- const ref = garbage[i];
323
- instances.remove(ref);
324
- }
325
-
326
- };
327
-
328
- /**
329
- *
330
- * @param {Frustum[]} frustums
331
- * @param {function[]} [visibilityFilters]
332
- */
333
- InstancedFoliage.prototype.update = function (frustums, visibilityFilters = []) {
334
- const instances = this.instances;
334
+ //build visible set
335
+ traverseBinaryNodeUsingVisitor(this.bvh, this.bvhVisibilityVisitor);
335
336
 
336
- this.bvhVisibilityVisitor.setFrustums(frustums);
337
+ visibilitySetBuilder.finalize();
337
338
 
338
- const visibilitySetBuilder = this.visibilitySetBuilder;
339
- visibilitySetBuilder.setFilters(visibilityFilters);
340
- visibilitySetBuilder.initialize();
339
+ // console.log(`Visible: `, elementsToAdd.size);
341
340
 
342
- const visibleElementSet = visibilitySetBuilder.visibleSet;
341
+ //reset garbage pointer
342
+ this.__garbagePointer = 0;
343
343
 
344
- //build visible set
345
- traverseBinaryNodeUsingVisitor(this.bvh, this.bvhVisibilityVisitor);
344
+ //remove those that are no longer visible
345
+ instances.traverseReferences(this.__updateVisibility, this);
346
346
 
347
- visibilitySetBuilder.finalize();
347
+ //cull instances that are no longer visible
348
+ this.__removeGarbage();
348
349
 
349
- // console.log(`Visible: `, elementsToAdd.size);
350
350
 
351
- //reset garbage pointer
352
- this.__garbagePointer = 0;
351
+ const data = this.data;
353
352
 
354
- //remove those that are no longer visible
355
- instances.traverseReferences(this.__updateVisibility, this);
353
+ //process entities that have become newly visible
354
+ for (let index = visibleElementSet.nextSetBit(0); index !== -1; index = visibleElementSet.nextSetBit(index + 1)) {
355
+ //read transform data for instance
356
+ data.getRow(index, rowData);
356
357
 
357
- //cull instances that are no longer visible
358
- this.__removeGarbage();
358
+ const positionX = rowData[0];
359
+ const positionY = rowData[1];
360
+ const positionZ = rowData[2];
359
361
 
362
+ const encodedRotation = rowData[3];
360
363
 
361
- const data = this.data;
362
-
363
- //process entities that have become newly visible
364
- for (let index = visibleElementSet.nextSetBit(0); index !== -1; index = visibleElementSet.nextSetBit(index + 1)) {
365
- //read transform data for instance
366
- data.getRow(index, rowData);
367
364
 
368
- const positionX = rowData[0];
369
- const positionY = rowData[1];
370
- const positionZ = rowData[2];
365
+ const scaleX = rowData[4];
366
+ const scaleY = rowData[5];
367
+ const scaleZ = rowData[6];
371
368
 
372
- const encodedRotation = rowData[3];
369
+ const i = instances.add(index);
373
370
 
371
+ tempQuaternion.decodeFromUint32(encodedRotation);
374
372
 
375
- const scaleX = rowData[4];
376
- const scaleY = rowData[5];
377
- const scaleZ = rowData[6];
373
+ //apply instance transforms
374
+ instances.setPositionAt(i, positionX, positionY, positionZ);
375
+ instances.setRotationAt(i, tempQuaternion.x, tempQuaternion.y, tempQuaternion.z, tempQuaternion.w);
376
+ instances.setScaleAt(i, scaleX, scaleY, scaleZ);
377
+ }
378
+ }
379
+ }
378
380
 
379
- const i = instances.add(index);
381
+ /**
382
+ *
383
+ * @param {BinaryBuffer} buffer
384
+ * @returns {number}
385
+ */
386
+ function deserializeLeafValueUintVar(buffer) {
387
+ return buffer.readUintVar();
388
+ }
380
389
 
381
- tempQuaternion.decodeFromUint32(encodedRotation);
390
+ /**
391
+ *
392
+ * @param {BinaryBuffer} buffer
393
+ * @param {number} value
394
+ */
395
+ function serializeLeafValueUintVar(buffer, value) {
396
+ buffer.writeUintVar(value);
397
+ }
382
398
 
383
- //apply instance transforms
384
- instances.setPositionAt(i, positionX, positionY, positionZ);
385
- instances.setRotationAt(i, tempQuaternion.x, tempQuaternion.y, tempQuaternion.z, tempQuaternion.w);
386
- instances.setScaleAt(i, scaleX, scaleY, scaleZ);
387
- }
388
- };
389
399
 
400
+ const tempQuaternion = new Quaternion();
401
+ const rowData = [];
390
402
 
391
- export { InstancedFoliage };