three-stdlib 2.28.2 → 2.28.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,27 +7,27 @@ var __publicField = (obj, key, value) => {
7
7
  };
8
8
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
9
9
  const THREE = require("three");
10
+ const ID_ATTR_NAME = "_batch_id_";
10
11
  const _identityMatrix = new THREE.Matrix4();
11
- const _zeroMatrix = new THREE.Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
12
+ const _zeroScaleMatrix = new THREE.Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
12
13
  const batchingParsVertex = (
13
14
  /* glsl */
14
15
  `
15
16
  #ifdef BATCHING
16
- attribute float id;
17
+ attribute float ${ID_ATTR_NAME};
17
18
  uniform highp sampler2D batchingTexture;
18
- uniform int batchingTextureSize;
19
19
  mat4 getBatchingMatrix( const in float i ) {
20
- float j = i * 4.0;
21
- float x = mod( j, float( batchingTextureSize ) );
22
- float y = floor( j / float( batchingTextureSize ) );
23
- float dx = 1.0 / float( batchingTextureSize );
24
- float dy = 1.0 / float( batchingTextureSize );
25
- y = dy * ( y + 0.5 );
26
- vec4 v1 = texture2D( batchingTexture, vec2( dx * ( x + 0.5 ), y ) );
27
- vec4 v2 = texture2D( batchingTexture, vec2( dx * ( x + 1.5 ), y ) );
28
- vec4 v3 = texture2D( batchingTexture, vec2( dx * ( x + 2.5 ), y ) );
29
- vec4 v4 = texture2D( batchingTexture, vec2( dx * ( x + 3.5 ), y ) );
20
+
21
+ int size = textureSize( batchingTexture, 0 ).x;
22
+ int j = int( i ) * 4;
23
+ int x = j % size;
24
+ int y = j / size;
25
+ vec4 v1 = texelFetch( batchingTexture, ivec2( x, y ), 0 );
26
+ vec4 v2 = texelFetch( batchingTexture, ivec2( x + 1, y ), 0 );
27
+ vec4 v3 = texelFetch( batchingTexture, ivec2( x + 2, y ), 0 );
28
+ vec4 v4 = texelFetch( batchingTexture, ivec2( x + 3, y ), 0 );
30
29
  return mat4( v1, v2, v3, v4 );
30
+
31
31
  }
32
32
  #endif
33
33
  `
@@ -36,7 +36,7 @@ const batchingbaseVertex = (
36
36
  /* glsl */
37
37
  `
38
38
  #ifdef BATCHING
39
- mat4 batchingMatrix = getBatchingMatrix( id );
39
+ mat4 batchingMatrix = getBatchingMatrix( ${ID_ATTR_NAME} );
40
40
  #endif
41
41
  `
42
42
  );
@@ -59,6 +59,20 @@ const batchingVertex = (
59
59
  #endif
60
60
  `
61
61
  );
62
+ function copyAttributeData(src, target, targetOffset = 0) {
63
+ const itemSize = target.itemSize;
64
+ if (src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor) {
65
+ const vertexCount = src.count;
66
+ for (let i = 0; i < vertexCount; i++) {
67
+ for (let c = 0; c < itemSize; c++) {
68
+ target.setComponent(i + targetOffset, c, src.getComponent(i, c));
69
+ }
70
+ }
71
+ } else {
72
+ target.array.set(src.array, targetOffset * itemSize);
73
+ }
74
+ target.needsUpdate = true;
75
+ }
62
76
  class BatchedMesh extends THREE.Mesh {
63
77
  constructor(maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material) {
64
78
  super(new THREE.BufferGeometry(), material);
@@ -66,41 +80,34 @@ class BatchedMesh extends THREE.Mesh {
66
80
  __publicField(this, "_vertexCounts");
67
81
  __publicField(this, "_indexStarts");
68
82
  __publicField(this, "_indexCounts");
69
- __publicField(this, "_visibles");
70
- __publicField(this, "_alives");
83
+ __publicField(this, "_reservedRanges");
84
+ __publicField(this, "_visible");
85
+ __publicField(this, "_active");
71
86
  __publicField(this, "_maxGeometryCount");
72
87
  __publicField(this, "_maxVertexCount");
73
88
  __publicField(this, "_maxIndexCount");
74
89
  __publicField(this, "_geometryInitialized");
75
90
  __publicField(this, "_geometryCount");
76
- __publicField(this, "_vertexCount");
77
- __publicField(this, "_indexCount");
78
91
  __publicField(this, "_matrices");
79
- __publicField(this, "_matricesArray");
80
92
  __publicField(this, "_matricesTexture");
81
- __publicField(this, "_matricesTextureSize");
82
93
  __publicField(this, "_customUniforms");
83
94
  this._vertexStarts = [];
84
95
  this._vertexCounts = [];
85
96
  this._indexStarts = [];
86
97
  this._indexCounts = [];
87
- this._visibles = [];
88
- this._alives = [];
98
+ this._reservedRanges = [];
99
+ this._visible = [];
100
+ this._active = [];
89
101
  this._maxGeometryCount = maxGeometryCount;
90
102
  this._maxVertexCount = maxVertexCount;
91
103
  this._maxIndexCount = maxIndexCount;
92
104
  this._geometryInitialized = false;
93
105
  this._geometryCount = 0;
94
- this._vertexCount = 0;
95
- this._indexCount = 0;
96
106
  this._matrices = [];
97
- this._matricesArray = null;
98
107
  this._matricesTexture = null;
99
- this._matricesTextureSize = null;
100
108
  this.frustumCulled = false;
101
109
  this._customUniforms = {
102
- batchingTexture: { value: null },
103
- batchingTextureSize: { value: 0 }
110
+ batchingTexture: { value: null }
104
111
  };
105
112
  this._initMatricesTexture();
106
113
  this._initShader();
@@ -121,158 +128,295 @@ class BatchedMesh extends THREE.Mesh {
121
128
  size = Math.max(size, 4);
122
129
  const matricesArray = new Float32Array(size * size * 4);
123
130
  const matricesTexture = new THREE.DataTexture(matricesArray, size, size, THREE.RGBAFormat, THREE.FloatType);
124
- this._matricesArray = matricesArray;
125
131
  this._matricesTexture = matricesTexture;
126
- this._matricesTextureSize = size;
127
132
  this._customUniforms.batchingTexture.value = this._matricesTexture;
128
- this._customUniforms.batchingTextureSize.value = this._matricesTextureSize;
129
133
  }
130
134
  _initShader() {
131
- const currentOnBeforeCompile = this.material.onBeforeCompile;
135
+ const material = this.material;
136
+ const currentOnBeforeCompile = material.onBeforeCompile;
132
137
  const customUniforms = this._customUniforms;
133
- this.material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {
134
- parameters.vertexShader = parameters.vertexShader.replace("#include <skinning_pars_vertex>", "#include <skinning_pars_vertex>\n" + batchingParsVertex).replace(
135
- "#include <skinnormal_vertex>",
136
- "#include <skinnormal_vertex>\n" + batchingbaseVertex + batchingnormalVertex
137
- ).replace("#include <skinning_vertex>", "#include <skinning_vertex>\n" + batchingVertex);
138
+ material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {
139
+ parameters.vertexShader = parameters.vertexShader.replace("#include <skinning_pars_vertex>", "#include <skinning_pars_vertex>\n" + batchingParsVertex).replace("#include <uv_vertex>", "#include <uv_vertex>\n" + batchingbaseVertex).replace("#include <skinnormal_vertex>", "#include <skinnormal_vertex>\n" + batchingnormalVertex).replace("#include <skinning_vertex>", "#include <skinning_vertex>\n" + batchingVertex);
138
140
  for (const uniformName in customUniforms) {
139
141
  parameters.uniforms[uniformName] = customUniforms[uniformName];
140
142
  }
141
143
  currentOnBeforeCompile.call(this, parameters, renderer);
142
144
  };
143
- this.material.defines = this.material.defines || {};
144
- this.material.defines.BATCHING = false;
145
+ material.defines = material.defines || {};
146
+ material.defines.BATCHING = false;
147
+ }
148
+ _initializeGeometry(reference) {
149
+ const geometry = this.geometry;
150
+ const maxVertexCount = this._maxVertexCount;
151
+ const maxGeometryCount = this._maxGeometryCount;
152
+ const maxIndexCount = this._maxIndexCount;
153
+ if (this._geometryInitialized === false) {
154
+ for (const attributeName in reference.attributes) {
155
+ const srcAttribute = reference.getAttribute(attributeName);
156
+ const { array, itemSize, normalized } = srcAttribute;
157
+ const dstArray = new array.constructor(maxVertexCount * itemSize);
158
+ const dstAttribute = new srcAttribute.constructor(dstArray, itemSize, normalized);
159
+ dstAttribute.setUsage(srcAttribute.usage);
160
+ geometry.setAttribute(attributeName, dstAttribute);
161
+ }
162
+ if (reference.getIndex() !== null) {
163
+ const indexArray = maxVertexCount > 65536 ? new Uint32Array(maxIndexCount) : new Uint16Array(maxIndexCount);
164
+ geometry.setIndex(new THREE.BufferAttribute(indexArray, 1));
165
+ }
166
+ const idArray = maxGeometryCount > 65536 ? new Uint32Array(maxVertexCount) : new Uint16Array(maxVertexCount);
167
+ geometry.setAttribute(ID_ATTR_NAME, new THREE.BufferAttribute(idArray, 1));
168
+ this._geometryInitialized = true;
169
+ }
170
+ }
171
+ // Make sure the geometry is compatible with the existing combined geometry atributes
172
+ _validateGeometry(geometry) {
173
+ if (geometry.getAttribute(ID_ATTR_NAME)) {
174
+ throw new Error(`BatchedMesh: Geometry cannot use attribute "${ID_ATTR_NAME}"`);
175
+ }
176
+ const batchGeometry = this.geometry;
177
+ if (Boolean(geometry.getIndex()) !== Boolean(batchGeometry.getIndex())) {
178
+ throw new Error('BatchedMesh: All geometries must consistently have "index".');
179
+ }
180
+ for (const attributeName in batchGeometry.attributes) {
181
+ if (attributeName === ID_ATTR_NAME) {
182
+ continue;
183
+ }
184
+ if (!geometry.hasAttribute(attributeName)) {
185
+ throw new Error(
186
+ `BatchedMesh: Added geometry missing "${attributeName}". All geometries must have consistent attributes.`
187
+ );
188
+ }
189
+ const srcAttribute = geometry.getAttribute(attributeName);
190
+ const dstAttribute = batchGeometry.getAttribute(attributeName);
191
+ if (srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized) {
192
+ throw new Error("BatchedMesh: All attributes must have a consistent itemSize and normalized value.");
193
+ }
194
+ }
145
195
  }
146
196
  getGeometryCount() {
147
197
  return this._geometryCount;
148
198
  }
149
199
  getVertexCount() {
150
- return this._vertexCount;
200
+ const reservedRanges = this._reservedRanges;
201
+ if (reservedRanges.length === 0) {
202
+ return 0;
203
+ } else {
204
+ const finalRange = reservedRanges[reservedRanges.length - 1];
205
+ return finalRange.vertexStart + finalRange.vertexCount;
206
+ }
151
207
  }
152
208
  getIndexCount() {
153
- return this._indexCount;
209
+ const reservedRanges = this._reservedRanges;
210
+ const geometry = this.geometry;
211
+ if (geometry.getIndex() === null || reservedRanges.length === 0) {
212
+ return 0;
213
+ } else {
214
+ const finalRange = reservedRanges[reservedRanges.length - 1];
215
+ return finalRange.indexStart + finalRange.indexCount;
216
+ }
154
217
  }
155
- applyGeometry(geometry) {
156
- var _a;
157
- if (this._geometryCount >= this._maxGeometryCount)
158
- ;
159
- if (this._geometryInitialized === false) {
160
- for (const attributeName in geometry.attributes) {
161
- const srcAttribute = geometry.getAttribute(attributeName);
162
- const { array, itemSize, normalized } = srcAttribute;
163
- const dstArray = new array.constructor(this._maxVertexCount * itemSize);
164
- const dstAttribute = new srcAttribute.constructor(dstArray, itemSize, normalized);
165
- dstAttribute.setUsage(srcAttribute.usage);
166
- this.geometry.setAttribute(attributeName, dstAttribute);
218
+ addGeometry(geometry, vertexCount = -1, indexCount = -1) {
219
+ this._initializeGeometry(geometry);
220
+ this._validateGeometry(geometry);
221
+ if (this._geometryCount >= this._maxGeometryCount) {
222
+ throw new Error("BatchedMesh: Maximum geometry count reached.");
223
+ }
224
+ const range = {
225
+ vertexStart: -1,
226
+ vertexCount: -1,
227
+ indexStart: -1,
228
+ indexCount: -1
229
+ };
230
+ let lastRange = null;
231
+ const reservedRanges = this._reservedRanges;
232
+ if (this._geometryCount !== 0) {
233
+ lastRange = reservedRanges[reservedRanges.length - 1];
234
+ }
235
+ if (vertexCount === -1) {
236
+ range.vertexCount = geometry.getAttribute("position").count;
237
+ } else {
238
+ range.vertexCount = vertexCount;
239
+ }
240
+ if (lastRange === null) {
241
+ range.vertexStart = 0;
242
+ } else {
243
+ range.vertexStart = lastRange.vertexStart + lastRange.vertexCount;
244
+ }
245
+ if (geometry.getIndex() !== null) {
246
+ if (indexCount === -1) {
247
+ range.indexCount = geometry.getIndex().count;
248
+ } else {
249
+ range.indexCount = indexCount;
167
250
  }
168
- if (geometry.getIndex() !== null) {
169
- const indexArray = this._maxVertexCount > 65536 ? new Uint32Array(this._maxIndexCount) : new Uint16Array(this._maxIndexCount);
170
- this.geometry.setIndex(new THREE.BufferAttribute(indexArray, 1));
251
+ if (lastRange === null) {
252
+ range.indexStart = 0;
253
+ } else {
254
+ range.indexStart = lastRange.indexStart + lastRange.indexCount;
171
255
  }
172
- const idArray = this._maxGeometryCount > 65536 ? new Uint32Array(this._maxVertexCount) : new Uint16Array(this._maxVertexCount);
173
- this.geometry.setAttribute("id", new THREE.BufferAttribute(idArray, 1));
174
- this._geometryInitialized = true;
175
256
  }
176
- const hasIndex = this.geometry.getIndex() !== null;
177
- const dstIndex = this.geometry.getIndex();
178
- const srcIndex = geometry.getIndex();
179
- const srcPositionAttribute = geometry.getAttribute("position");
180
- this._vertexStarts.push(this._vertexCount);
181
- this._vertexCounts.push(srcPositionAttribute.count);
182
- if (hasIndex) {
183
- this._indexStarts.push(this._indexCount);
184
- this._indexCounts.push(srcIndex.count);
257
+ if (range.indexStart !== -1 && range.indexStart + range.indexCount > this._maxIndexCount || range.vertexStart + range.vertexCount > this._maxVertexCount) {
258
+ throw new Error("BatchedMesh: Reserved space request exceeds the maximum buffer size.");
259
+ }
260
+ const indexCounts = this._indexCounts;
261
+ const indexStarts = this._indexStarts;
262
+ const vertexCounts = this._vertexCounts;
263
+ const vertexStarts = this._vertexStarts;
264
+ const visible = this._visible;
265
+ const active = this._active;
266
+ const matricesTexture = this._matricesTexture;
267
+ const matrices = this._matrices;
268
+ const matricesArray = this._matricesTexture.image.data;
269
+ visible.push(true);
270
+ active.push(true);
271
+ const geometryId = this._geometryCount;
272
+ this._geometryCount++;
273
+ matrices.push(new THREE.Matrix4());
274
+ _identityMatrix.toArray(matricesArray, geometryId * 16);
275
+ matricesTexture.needsUpdate = true;
276
+ reservedRanges.push(range);
277
+ vertexStarts.push(range.vertexStart);
278
+ vertexCounts.push(range.vertexCount);
279
+ if (geometry.getIndex() !== null) {
280
+ indexStarts.push(range.indexCount);
281
+ indexCounts.push(range.indexCount);
282
+ }
283
+ const idAttribute = this.geometry.getAttribute(ID_ATTR_NAME);
284
+ for (let i = 0; i < range.vertexCount; i++) {
285
+ idAttribute.setX(range.vertexStart + i, geometryId);
286
+ }
287
+ idAttribute.needsUpdate = true;
288
+ this.setGeometryAt(geometryId, geometry);
289
+ return geometryId;
290
+ }
291
+ /**
292
+ * @deprecated use `addGeometry` instead.
293
+ */
294
+ applyGeometry(geometry) {
295
+ return this.addGeometry(geometry);
296
+ }
297
+ setGeometryAt(id, geometry) {
298
+ if (id >= this._geometryCount) {
299
+ throw new Error("BatchedMesh: Maximum geometry count reached.");
185
300
  }
186
- this._visibles.push(true);
187
- this._alives.push(true);
188
- for (const attributeName in geometry.attributes) {
301
+ this._validateGeometry(geometry);
302
+ const range = this._reservedRanges[id];
303
+ if (geometry.getIndex() !== null && geometry.getIndex().count > range.indexCount || geometry.attributes.position.count > range.vertexCount) {
304
+ throw new Error("BatchedMesh: Reserved space not large enough for provided geometry.");
305
+ }
306
+ const batchGeometry = this.geometry;
307
+ const srcPositionAttribute = geometry.getAttribute("position");
308
+ const hasIndex = batchGeometry.getIndex() !== null;
309
+ const dstIndex = batchGeometry.getIndex();
310
+ const srcIndex = geometry.getIndex();
311
+ const vertexStart = range.vertexStart;
312
+ const vertexCount = range.vertexCount;
313
+ for (const attributeName in batchGeometry.attributes) {
314
+ if (attributeName === ID_ATTR_NAME) {
315
+ continue;
316
+ }
189
317
  const srcAttribute = geometry.getAttribute(attributeName);
190
- const dstAttribute = this.geometry.getAttribute(attributeName);
191
- dstAttribute.array.set(srcAttribute.array, this._vertexCount * dstAttribute.itemSize);
318
+ const dstAttribute = batchGeometry.getAttribute(attributeName);
319
+ copyAttributeData(srcAttribute, dstAttribute, vertexStart);
320
+ const itemSize = srcAttribute.itemSize;
321
+ for (let i = srcAttribute.count, l = vertexCount; i < l; i++) {
322
+ const index = vertexStart + i;
323
+ for (let c = 0; c < itemSize; c++) {
324
+ dstAttribute.setComponent(index, c, 0);
325
+ }
326
+ }
192
327
  dstAttribute.needsUpdate = true;
193
328
  }
329
+ this._vertexCounts[id] = srcPositionAttribute.count;
194
330
  if (hasIndex) {
331
+ const indexStart = range.indexStart;
195
332
  for (let i = 0; i < srcIndex.count; i++) {
196
- dstIndex.setX(this._indexCount + i, this._vertexCount + srcIndex.getX(i));
333
+ dstIndex.setX(indexStart + i, vertexStart + srcIndex.getX(i));
334
+ }
335
+ for (let i = srcIndex.count, l = range.indexCount; i < l; i++) {
336
+ dstIndex.setX(indexStart + i, vertexStart);
197
337
  }
198
- this._indexCount += srcIndex.count;
199
338
  dstIndex.needsUpdate = true;
339
+ this._indexCounts[id] = srcIndex.count;
200
340
  }
201
- const geometryId = this._geometryCount;
202
- this._geometryCount++;
203
- const idAttribute = this.geometry.getAttribute("id");
204
- for (let i = 0; i < srcPositionAttribute.count; i++) {
205
- idAttribute.setX(this._vertexCount + i, geometryId);
206
- }
207
- idAttribute.needsUpdate = true;
208
- this._vertexCount += srcPositionAttribute.count;
209
- this._matrices.push(new THREE.Matrix4());
210
- _identityMatrix.toArray((_a = this._matricesArray) != null ? _a : void 0, geometryId * 16);
211
- this._matricesTexture.needsUpdate = true;
212
- return geometryId;
341
+ return id;
213
342
  }
214
343
  deleteGeometry(geometryId) {
215
- if (geometryId >= this._alives.length || this._alives[geometryId] === false) {
344
+ const active = this._active;
345
+ const matricesTexture = this._matricesTexture;
346
+ const matricesArray = matricesTexture.image.data;
347
+ if (geometryId >= active.length || active[geometryId] === false) {
216
348
  return this;
217
349
  }
218
- this._alives[geometryId] = false;
219
- _zeroMatrix.toArray(this._matricesArray, geometryId * 16);
220
- this._matricesTexture.needsUpdate = true;
350
+ active[geometryId] = false;
351
+ _zeroScaleMatrix.toArray(matricesArray, geometryId * 16);
352
+ matricesTexture.needsUpdate = true;
221
353
  return this;
222
354
  }
223
355
  optimize() {
224
- return this;
356
+ throw new Error("BatchedMesh: Optimize function not implemented.");
225
357
  }
226
358
  setMatrixAt(geometryId, matrix) {
227
- if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {
359
+ const visible = this._visible;
360
+ const active = this._active;
361
+ const matricesTexture = this._matricesTexture;
362
+ const matrices = this._matrices;
363
+ const matricesArray = matricesTexture.image.data;
364
+ if (geometryId >= matrices.length || active[geometryId] === false) {
228
365
  return this;
229
366
  }
230
- this._matrices[geometryId].copy(matrix);
231
- if (this._visibles[geometryId] === true) {
232
- matrix.toArray(this._matricesArray, geometryId * 16);
233
- this._matricesTexture.needsUpdate = true;
367
+ if (visible[geometryId] === true) {
368
+ matrix.toArray(matricesArray, geometryId * 16);
369
+ matricesTexture.needsUpdate = true;
234
370
  }
371
+ matrices[geometryId].copy(matrix);
235
372
  return this;
236
373
  }
237
374
  getMatrixAt(geometryId, matrix) {
238
- if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {
375
+ const matrices = this._matrices;
376
+ const active = this._active;
377
+ if (geometryId >= matrices.length || active[geometryId] === false) {
239
378
  return matrix;
240
379
  }
241
- return matrix.copy(this._matrices[geometryId]);
380
+ return matrix.copy(matrices[geometryId]);
242
381
  }
243
- setVisibleAt(geometryId, visible) {
244
- if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {
382
+ setVisibleAt(geometryId, value) {
383
+ const visible = this._visible;
384
+ const active = this._active;
385
+ const matricesTexture = this._matricesTexture;
386
+ const matrices = this._matrices;
387
+ const matricesArray = matricesTexture.image.data;
388
+ if (geometryId >= visible.length || active[geometryId] === false || visible[geometryId] === value) {
245
389
  return this;
246
390
  }
247
- if (this._visibles[geometryId] === visible) {
248
- return this;
249
- }
250
- if (visible === true) {
251
- this._matrices[geometryId].toArray(this._matricesArray, geometryId * 16);
391
+ if (value === true) {
392
+ matrices[geometryId].toArray(matricesArray, geometryId * 16);
252
393
  } else {
253
- _zeroMatrix.toArray(this._matricesArray, geometryId * 16);
394
+ _zeroScaleMatrix.toArray(matricesArray, geometryId * 16);
254
395
  }
255
- this._matricesTexture.needsUpdate = true;
256
- this._visibles[geometryId] = visible;
396
+ matricesTexture.needsUpdate = true;
397
+ visible[geometryId] = value;
257
398
  return this;
258
399
  }
259
400
  getVisibleAt(geometryId) {
260
- if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {
401
+ const visible = this._visible;
402
+ const active = this._active;
403
+ if (geometryId >= visible.length || active[geometryId] === false) {
261
404
  return false;
262
405
  }
263
- return this._visibles[geometryId];
406
+ return visible[geometryId];
264
407
  }
265
- copy(source) {
266
- super.copy(source);
267
- return this;
408
+ raycast() {
409
+ console.warn("BatchedMesh: Raycast function not implemented.");
410
+ }
411
+ copy() {
412
+ throw new Error("BatchedMesh: Copy function not implemented.");
268
413
  }
269
- toJSON(meta) {
270
- return super.toJSON(meta);
414
+ toJSON() {
415
+ throw new Error("BatchedMesh: toJSON function not implemented.");
271
416
  }
272
417
  dispose() {
273
- var _a;
274
418
  this.geometry.dispose();
275
- (_a = this._matricesTexture) == null ? void 0 : _a.dispose();
419
+ this._matricesTexture.dispose();
276
420
  this._matricesTexture = null;
277
421
  return this;
278
422
  }
@@ -1 +1 @@
1
- {"version":3,"file":"BatchedMesh.cjs","sources":["../../src/objects/BatchedMesh.ts"],"sourcesContent":["import {\n Matrix4,\n Mesh,\n BufferGeometry,\n Material,\n DataTexture,\n IUniform,\n MathUtils,\n RGBAFormat,\n FloatType,\n BufferAttribute,\n} from 'three'\n\nconst _identityMatrix = new Matrix4()\nconst _zeroMatrix = new Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)\n\n// Custom shaders\nconst batchingParsVertex = /* glsl */ `\n#ifdef BATCHING\n\tattribute float id;\n\tuniform highp sampler2D batchingTexture;\n\tuniform int batchingTextureSize;\n\tmat4 getBatchingMatrix( const in float i ) {\n\t\tfloat j = i * 4.0;\n\t\tfloat x = mod( j, float( batchingTextureSize ) );\n\t\tfloat y = floor( j / float( batchingTextureSize ) );\n\t\tfloat dx = 1.0 / float( batchingTextureSize );\n\t\tfloat dy = 1.0 / float( batchingTextureSize );\n\t\ty = dy * ( y + 0.5 );\n\t\tvec4 v1 = texture2D( batchingTexture, vec2( dx * ( x + 0.5 ), y ) );\n\t\tvec4 v2 = texture2D( batchingTexture, vec2( dx * ( x + 1.5 ), y ) );\n\t\tvec4 v3 = texture2D( batchingTexture, vec2( dx * ( x + 2.5 ), y ) );\n\t\tvec4 v4 = texture2D( batchingTexture, vec2( dx * ( x + 3.5 ), y ) );\n\t\treturn mat4( v1, v2, v3, v4 );\n\t}\n#endif\n`\n\nconst batchingbaseVertex = /* glsl */ `\n#ifdef BATCHING\n\tmat4 batchingMatrix = getBatchingMatrix( id );\n#endif\n`\n\nconst batchingnormalVertex = /* glsl */ `\n#ifdef BATCHING\n\tobjectNormal = vec4( batchingMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( batchingMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif\n`\n\nconst batchingVertex = /* glsl */ `\n#ifdef BATCHING\n\ttransformed = ( batchingMatrix * vec4( transformed, 1.0 ) ).xyz;\n#endif\n`\n\n// @TODO: SkinnedMesh support?\n// @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.\n\nclass BatchedMesh extends Mesh<BufferGeometry, Material> {\n _vertexStarts: number[]\n _vertexCounts: number[]\n _indexStarts: number[]\n _indexCounts: number[]\n _visibles: boolean[]\n _alives: boolean[]\n _maxGeometryCount: number\n _maxVertexCount: number\n _maxIndexCount: number\n _geometryInitialized: boolean\n _geometryCount: number\n _vertexCount: number\n _indexCount: number\n _matrices: Matrix4[]\n _matricesArray: Float32Array | null\n _matricesTexture: DataTexture | null\n _matricesTextureSize: number | null\n _customUniforms: Record<string, IUniform>\n\n constructor(\n maxGeometryCount: number,\n maxVertexCount: number,\n maxIndexCount = maxVertexCount * 2,\n material?: Material,\n ) {\n super(new BufferGeometry(), material)\n\n this._vertexStarts = []\n this._vertexCounts = []\n this._indexStarts = []\n this._indexCounts = []\n\n this._visibles = []\n this._alives = []\n\n this._maxGeometryCount = maxGeometryCount\n this._maxVertexCount = maxVertexCount\n this._maxIndexCount = maxIndexCount\n\n this._geometryInitialized = false\n this._geometryCount = 0\n this._vertexCount = 0\n this._indexCount = 0\n\n // Local matrix per geometry by using data texture\n // @TODO: Support uniform parameter per geometry\n\n this._matrices = []\n this._matricesArray = null\n this._matricesTexture = null\n this._matricesTextureSize = null\n\n // @TODO: Calculate the entire binding box and make frustumCulled true\n this.frustumCulled = false\n\n this._customUniforms = {\n batchingTexture: { value: null },\n batchingTextureSize: { value: 0 },\n }\n\n this._initMatricesTexture()\n this._initShader()\n\n this.onBeforeRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = true\n }\n\n // @TODO: Implement frustum culling for each geometry\n }\n\n this.onAfterRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = false\n }\n }\n }\n\n _initMatricesTexture() {\n // layout (1 matrix = 4 pixels)\n // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)\n // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)\n // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)\n // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)\n // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)\n\n let size = Math.sqrt(this._maxGeometryCount * 4) // 4 pixels needed for 1 matrix\n size = MathUtils.ceilPowerOfTwo(size)\n size = Math.max(size, 4)\n\n const matricesArray = new Float32Array(size * size * 4) // 4 floats per RGBA pixel\n const matricesTexture = new DataTexture(matricesArray, size, size, RGBAFormat, FloatType)\n\n this._matricesArray = matricesArray\n this._matricesTexture = matricesTexture\n this._matricesTextureSize = size\n\n this._customUniforms.batchingTexture.value = this._matricesTexture\n this._customUniforms.batchingTextureSize.value = this._matricesTextureSize\n }\n\n _initShader() {\n const currentOnBeforeCompile = this.material.onBeforeCompile\n const customUniforms = this._customUniforms\n\n this.material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {\n // Is this replacement stable across any materials?\n parameters.vertexShader = parameters.vertexShader\n .replace('#include <skinning_pars_vertex>', '#include <skinning_pars_vertex>\\n' + batchingParsVertex)\n .replace(\n '#include <skinnormal_vertex>',\n '#include <skinnormal_vertex>\\n' + batchingbaseVertex + batchingnormalVertex,\n )\n .replace('#include <skinning_vertex>', '#include <skinning_vertex>\\n' + batchingVertex)\n\n for (const uniformName in customUniforms) {\n parameters.uniforms[uniformName] = customUniforms[uniformName]\n }\n\n currentOnBeforeCompile.call(this, parameters, renderer)\n }\n\n this.material.defines = this.material.defines || {}\n this.material.defines.BATCHING = false\n }\n\n getGeometryCount() {\n return this._geometryCount\n }\n\n getVertexCount() {\n return this._vertexCount\n }\n\n getIndexCount() {\n return this._indexCount\n }\n\n applyGeometry(geometry: BufferGeometry) {\n // @TODO: geometry.groups support?\n // @TODO: geometry.drawRange support?\n // @TODO: geometry.mortphAttributes support?\n\n if (this._geometryCount >= this._maxGeometryCount) {\n // @TODO: Error handling\n }\n\n if (this._geometryInitialized === false) {\n for (const attributeName in geometry.attributes) {\n const srcAttribute = geometry.getAttribute(attributeName)\n const { array, itemSize, normalized } = srcAttribute\n\n const dstArray = new (array.constructor as Float32ArrayConstructor)(this._maxVertexCount * itemSize)\n const dstAttribute = new (srcAttribute.constructor as any)(dstArray, itemSize, normalized) as BufferAttribute\n\n // TODO: add usage in @types/three\n // @ts-ignore\n dstAttribute.setUsage(srcAttribute.usage)\n\n this.geometry.setAttribute(attributeName, dstAttribute)\n }\n\n if (geometry.getIndex() !== null) {\n const indexArray =\n this._maxVertexCount > 65536 ? new Uint32Array(this._maxIndexCount) : new Uint16Array(this._maxIndexCount)\n\n this.geometry.setIndex(new BufferAttribute(indexArray, 1))\n }\n\n const idArray =\n this._maxGeometryCount > 65536 ? new Uint32Array(this._maxVertexCount) : new Uint16Array(this._maxVertexCount)\n // @TODO: What if attribute name 'id' is already used?\n this.geometry.setAttribute('id', new BufferAttribute(idArray, 1))\n\n this._geometryInitialized = true\n } else {\n // @TODO: Check if geometry has the same attributes set\n }\n\n const hasIndex = this.geometry.getIndex() !== null\n const dstIndex = this.geometry.getIndex()\n const srcIndex = geometry.getIndex()\n\n // Assuming geometry has position attribute\n const srcPositionAttribute = geometry.getAttribute('position')\n\n this._vertexStarts.push(this._vertexCount)\n this._vertexCounts.push(srcPositionAttribute.count)\n\n if (hasIndex) {\n this._indexStarts.push(this._indexCount)\n this._indexCounts.push(srcIndex!.count)\n }\n\n this._visibles.push(true)\n this._alives.push(true)\n\n // @TODO: Error handling if exceeding maxVertexCount or maxIndexCount\n\n for (const attributeName in geometry.attributes) {\n const srcAttribute = geometry.getAttribute(attributeName)\n const dstAttribute = this.geometry.getAttribute(attributeName)\n ;(dstAttribute.array as Float32Array).set(srcAttribute.array, this._vertexCount * dstAttribute.itemSize)\n dstAttribute.needsUpdate = true\n }\n\n if (hasIndex) {\n for (let i = 0; i < srcIndex!.count; i++) {\n dstIndex!.setX(this._indexCount + i, this._vertexCount + srcIndex!.getX(i))\n }\n\n this._indexCount += srcIndex!.count\n dstIndex!.needsUpdate = true\n }\n\n const geometryId = this._geometryCount\n this._geometryCount++\n\n const idAttribute = this.geometry.getAttribute('id')\n\n for (let i = 0; i < srcPositionAttribute.count; i++) {\n idAttribute.setX(this._vertexCount + i, geometryId)\n }\n\n idAttribute.needsUpdate = true\n\n this._vertexCount += srcPositionAttribute.count\n\n this._matrices.push(new Matrix4())\n _identityMatrix.toArray(this._matricesArray ?? undefined, geometryId * 16)\n this._matricesTexture!.needsUpdate = true\n\n return geometryId\n }\n\n deleteGeometry(geometryId: number) {\n if (geometryId >= this._alives.length || this._alives[geometryId] === false) {\n return this\n }\n\n this._alives[geometryId] = false\n _zeroMatrix.toArray(this._matricesArray!, geometryId * 16)\n this._matricesTexture!.needsUpdate = true\n\n // User needs to call optimize() to pack the data.\n\n return this\n }\n\n optimize() {\n // @TODO: Implement\n\n return this\n }\n\n setMatrixAt(geometryId: number, matrix: Matrix4) {\n // @TODO: Map geometryId to index of the arrays because\n // optimize() can make geometryId mismatch the index\n\n if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {\n return this\n }\n\n this._matrices[geometryId].copy(matrix)\n\n if (this._visibles[geometryId] === true) {\n matrix.toArray(this._matricesArray!, geometryId * 16)\n this._matricesTexture!.needsUpdate = true\n }\n\n return this\n }\n\n getMatrixAt(geometryId: number, matrix: Matrix4) {\n if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {\n return matrix\n }\n\n return matrix.copy(this._matrices[geometryId])\n }\n\n setVisibleAt(geometryId: number, visible: boolean) {\n if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {\n return this\n }\n\n if (this._visibles[geometryId] === visible) {\n return this\n }\n\n if (visible === true) {\n this._matrices[geometryId].toArray(this._matricesArray!, geometryId * 16)\n } else {\n _zeroMatrix.toArray(this._matricesArray!, geometryId * 16)\n }\n\n this._matricesTexture!.needsUpdate = true\n this._visibles[geometryId] = visible\n return this\n }\n\n getVisibleAt(geometryId: number) {\n if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {\n return false\n }\n\n return this._visibles[geometryId]\n }\n\n copy(source: BatchedMesh) {\n // @ts-ignore\n super.copy(source)\n\n // @TODO: Implement\n\n return this\n }\n\n toJSON(meta: any) {\n // @TODO: Implement\n\n return super.toJSON(meta)\n }\n\n dispose() {\n // Assuming the geometry is not shared with other meshes\n this.geometry.dispose()\n\n this._matricesTexture?.dispose()\n this._matricesTexture = null\n\n return this\n }\n}\n\nexport { BatchedMesh }\n"],"names":["Matrix4","Mesh","BufferGeometry","MathUtils","DataTexture","RGBAFormat","FloatType","BufferAttribute"],"mappings":";;;;;;;;;AAaA,MAAM,kBAAkB,IAAIA,MAAAA;AAC5B,MAAM,cAAc,IAAIA,cAAQ,EAAE,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAGpF,MAAM;AAAA;AAAA,EAAgC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBtC,MAAM;AAAA;AAAA,EAAgC;AAAA;AAAA;AAAA;AAAA;AAAA;AAMtC,MAAM;AAAA;AAAA,EAAkC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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U,CAAC;AAAA,EAC/C;AAAA,EAEA,aAAa,YAAoB,SAAkB;AAC7C,QAAA,cAAc,KAAK,UAAU,UAAU,KAAK,QAAQ,UAAU,MAAM,OAAO;AACtE,aAAA;AAAA,IACT;AAEA,QAAI,KAAK,UAAU,UAAU,MAAM,SAAS;AACnC,aAAA;AAAA,IACT;AAEA,QAAI,YAAY,MAAM;AACpB,WAAK,UAAU,UAAU,EAAE,QAAQ,KAAK,gBAAiB,aAAa,EAAE;AAAA,IAAA,OACnE;AACL,kBAAY,QAAQ,KAAK,gBAAiB,aAAa,EAAE;AAAA,IAC3D;AAEA,SAAK,iBAAkB,cAAc;AAChC,SAAA,UAAU,UAAU,IAAI;AACtB,WAAA;AAAA,EACT;AAAA,EAEA,aAAa,YAAoB;AAC3B,QAAA,cAAc,KAAK,UAAU,UAAU,KAAK,QAAQ,UAAU,MAAM,OAAO;AACtE,aAAA;AAAA,IACT;AAEO,WAAA,KAAK,UAAU,UAAU;AAAA,EAClC;AAAA,EAEA,KAAK,QAAqB;AAExB,UAAM,KAAK,MAAM;AAIV,WAAA;AAAA,EACT;AAAA,EAEA,OAAO,MAAW;AAGT,WAAA,MAAM,OAAO,IAAI;AAAA,EAC1B;AAAA,EAEA,UAAU;;AAER,SAAK,SAAS;AAEd,eAAK,qBAAL,mBAAuB;AACvB,SAAK,mBAAmB;AAEjB,WAAA;AAAA,EACT;AACF;;"}
1
+ {"version":3,"file":"BatchedMesh.cjs","sources":["../../src/objects/BatchedMesh.ts"],"sourcesContent":["import {\n Matrix4,\n BufferAttribute,\n InterleavedBufferAttribute,\n Mesh,\n BufferGeometry,\n Material,\n DataTexture,\n IUniform,\n MathUtils,\n RGBAFormat,\n FloatType,\n} from 'three'\n\nconst ID_ATTR_NAME = '_batch_id_'\nconst _identityMatrix = new Matrix4()\nconst _zeroScaleMatrix = new Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)\n\n// Custom shaders\nconst batchingParsVertex = /* glsl */ `\n#ifdef BATCHING\n\tattribute float ${ID_ATTR_NAME};\n\tuniform highp sampler2D batchingTexture;\n\tmat4 getBatchingMatrix( const in float i ) {\n\n\t\tint size = textureSize( batchingTexture, 0 ).x;\n\t\tint j = int( i ) * 4;\n\t\tint x = j % size;\n\t\tint y = j / size;\n\t\tvec4 v1 = texelFetch( batchingTexture, ivec2( x, y ), 0 );\n\t\tvec4 v2 = texelFetch( batchingTexture, ivec2( x + 1, y ), 0 );\n\t\tvec4 v3 = texelFetch( batchingTexture, ivec2( x + 2, y ), 0 );\n\t\tvec4 v4 = texelFetch( batchingTexture, ivec2( x + 3, y ), 0 );\n\t\treturn mat4( v1, v2, v3, v4 );\n\n\t}\n#endif\n`\n\nconst batchingbaseVertex = /* glsl */ `\n#ifdef BATCHING\n\tmat4 batchingMatrix = getBatchingMatrix( ${ID_ATTR_NAME} );\n#endif\n`\n\nconst batchingnormalVertex = /* glsl */ `\n#ifdef BATCHING\n\tobjectNormal = vec4( batchingMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( batchingMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif\n`\n\nconst batchingVertex = /* glsl */ `\n#ifdef BATCHING\n\ttransformed = ( batchingMatrix * vec4( transformed, 1.0 ) ).xyz;\n#endif\n`\n\n// @TODO: SkinnedMesh support?\n// @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.\n\n// copies data from attribute \"src\" into \"target\" starting at \"targetOffset\"\nfunction copyAttributeData(\n src: BufferAttribute | InterleavedBufferAttribute,\n target: BufferAttribute | InterleavedBufferAttribute,\n targetOffset = 0,\n): void {\n const itemSize = target.itemSize\n if (\n (src as InterleavedBufferAttribute).isInterleavedBufferAttribute ||\n src.array.constructor !== target.array.constructor\n ) {\n // use the component getters and setters if the array data cannot\n // be copied directly\n const vertexCount = src.count\n for (let i = 0; i < vertexCount; i++) {\n for (let c = 0; c < itemSize; c++) {\n // @ts-ignore\n target.setComponent(i + targetOffset, c, src.getComponent(i, c))\n }\n }\n } else {\n // faster copy approach using typed array set function\n // @ts-ignore\n target.array.set(src.array, targetOffset * itemSize)\n }\n\n target.needsUpdate = true\n}\n\nclass BatchedMesh extends Mesh<BufferGeometry, Material> {\n _vertexStarts: number[]\n _vertexCounts: number[]\n _indexStarts: number[]\n _indexCounts: number[]\n _reservedRanges: { vertexStart: number; vertexCount: number; indexStart: number; indexCount: number }[]\n _visible: boolean[]\n _active: boolean[]\n _maxGeometryCount: number\n _maxVertexCount: number\n _maxIndexCount: number\n _geometryInitialized: boolean\n _geometryCount: number\n _matrices: Matrix4[]\n _matricesTexture: DataTexture | null\n _customUniforms: Record<string, IUniform>\n\n constructor(\n maxGeometryCount: number,\n maxVertexCount: number,\n maxIndexCount = maxVertexCount * 2,\n material?: Material,\n ) {\n super(new BufferGeometry(), material)\n\n this._vertexStarts = []\n this._vertexCounts = []\n this._indexStarts = []\n this._indexCounts = []\n this._reservedRanges = []\n\n this._visible = []\n this._active = []\n\n this._maxGeometryCount = maxGeometryCount\n this._maxVertexCount = maxVertexCount\n this._maxIndexCount = maxIndexCount\n\n this._geometryInitialized = false\n this._geometryCount = 0\n\n // Local matrix per geometry by using data texture\n // @TODO: Support uniform parameter per geometry\n\n this._matrices = []\n this._matricesTexture = null!\n\n // @TODO: Calculate the entire binding box and make frustumCulled true\n this.frustumCulled = false\n\n this._customUniforms = {\n batchingTexture: { value: null },\n }\n\n this._initMatricesTexture()\n this._initShader()\n\n this.onBeforeRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = true\n }\n\n // @TODO: Implement frustum culling for each geometry\n }\n\n this.onAfterRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = false\n }\n }\n }\n\n _initMatricesTexture(): void {\n // layout (1 matrix = 4 pixels)\n // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)\n // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)\n // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)\n // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)\n // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)\n\n let size = Math.sqrt(this._maxGeometryCount * 4) // 4 pixels needed for 1 matrix\n size = MathUtils.ceilPowerOfTwo(size)\n size = Math.max(size, 4)\n\n const matricesArray = new Float32Array(size * size * 4) // 4 floats per RGBA pixel\n const matricesTexture = new DataTexture(matricesArray, size, size, RGBAFormat, FloatType)\n\n this._matricesTexture = matricesTexture\n this._customUniforms.batchingTexture.value = this._matricesTexture\n }\n\n _initShader(): void {\n const material = this.material\n const currentOnBeforeCompile = material.onBeforeCompile\n const customUniforms = this._customUniforms\n\n material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {\n // Is this replacement stable across any materials?\n parameters.vertexShader = parameters.vertexShader\n .replace('#include <skinning_pars_vertex>', '#include <skinning_pars_vertex>\\n' + batchingParsVertex)\n .replace('#include <uv_vertex>', '#include <uv_vertex>\\n' + batchingbaseVertex)\n .replace('#include <skinnormal_vertex>', '#include <skinnormal_vertex>\\n' + batchingnormalVertex)\n .replace('#include <skinning_vertex>', '#include <skinning_vertex>\\n' + batchingVertex)\n\n for (const uniformName in customUniforms) {\n parameters.uniforms[uniformName] = customUniforms[uniformName]\n }\n\n currentOnBeforeCompile.call(this, parameters, renderer)\n }\n\n material.defines = material.defines || {}\n material.defines.BATCHING = false\n }\n\n _initializeGeometry(reference: BufferGeometry): void {\n // @TODO: geometry.groups support?\n // @TODO: geometry.drawRange support?\n // @TODO: geometry.morphAttributes support?\n\n const geometry = this.geometry\n const maxVertexCount = this._maxVertexCount\n const maxGeometryCount = this._maxGeometryCount\n const maxIndexCount = this._maxIndexCount\n if (this._geometryInitialized === false) {\n for (const attributeName in reference.attributes) {\n const srcAttribute = reference.getAttribute(attributeName)\n const { array, itemSize, normalized } = srcAttribute\n\n const dstArray = new (array.constructor as Float32ArrayConstructor)(maxVertexCount * itemSize)\n const dstAttribute = new (srcAttribute.constructor as any)(dstArray, itemSize, normalized)\n\n // TODO: add usage in @types/three\n // @ts-ignore\n dstAttribute.setUsage(srcAttribute.usage)\n\n geometry.setAttribute(attributeName, dstAttribute)\n }\n\n if (reference.getIndex() !== null) {\n const indexArray = maxVertexCount > 65536 ? new Uint32Array(maxIndexCount) : new Uint16Array(maxIndexCount)\n\n geometry.setIndex(new BufferAttribute(indexArray, 1))\n }\n\n const idArray = maxGeometryCount > 65536 ? new Uint32Array(maxVertexCount) : new Uint16Array(maxVertexCount)\n geometry.setAttribute(ID_ATTR_NAME, new BufferAttribute(idArray, 1))\n\n this._geometryInitialized = true\n }\n }\n\n // Make sure the geometry is compatible with the existing combined geometry atributes\n _validateGeometry(geometry: BufferGeometry): void {\n // check that the geometry doesn't have a version of our reserved id attribute\n if (geometry.getAttribute(ID_ATTR_NAME)) {\n throw new Error(`BatchedMesh: Geometry cannot use attribute \"${ID_ATTR_NAME}\"`)\n }\n\n // check to ensure the geometries are using consistent attributes and indices\n const batchGeometry = this.geometry\n if (Boolean(geometry.getIndex()) !== Boolean(batchGeometry.getIndex())) {\n throw new Error('BatchedMesh: All geometries must consistently have \"index\".')\n }\n\n for (const attributeName in batchGeometry.attributes) {\n if (attributeName === ID_ATTR_NAME) {\n continue\n }\n\n if (!geometry.hasAttribute(attributeName)) {\n throw new Error(\n `BatchedMesh: Added geometry missing \"${attributeName}\". All geometries must have consistent attributes.`,\n )\n }\n\n const srcAttribute = geometry.getAttribute(attributeName)\n const dstAttribute = batchGeometry.getAttribute(attributeName)\n if (srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized) {\n throw new Error('BatchedMesh: All attributes must have a consistent itemSize and normalized value.')\n }\n }\n }\n\n getGeometryCount(): number {\n return this._geometryCount\n }\n\n getVertexCount(): number {\n const reservedRanges = this._reservedRanges\n if (reservedRanges.length === 0) {\n return 0\n } else {\n const finalRange = reservedRanges[reservedRanges.length - 1]\n return finalRange.vertexStart + finalRange.vertexCount\n }\n }\n\n getIndexCount(): number {\n const reservedRanges = this._reservedRanges\n const geometry = this.geometry\n if (geometry.getIndex() === null || reservedRanges.length === 0) {\n return 0\n } else {\n const finalRange = reservedRanges[reservedRanges.length - 1]\n return finalRange.indexStart + finalRange.indexCount\n }\n }\n\n addGeometry(geometry: BufferGeometry, vertexCount = -1, indexCount = -1): number {\n this._initializeGeometry(geometry)\n\n this._validateGeometry(geometry)\n\n // ensure we're not over geometry\n if (this._geometryCount >= this._maxGeometryCount) {\n throw new Error('BatchedMesh: Maximum geometry count reached.')\n }\n\n // get the necessary range fo the geometry\n const range = {\n vertexStart: -1,\n vertexCount: -1,\n indexStart: -1,\n indexCount: -1,\n }\n\n let lastRange = null\n const reservedRanges = this._reservedRanges\n if (this._geometryCount !== 0) {\n lastRange = reservedRanges[reservedRanges.length - 1]\n }\n\n if (vertexCount === -1) {\n range.vertexCount = geometry.getAttribute('position').count\n } else {\n range.vertexCount = vertexCount\n }\n\n if (lastRange === null) {\n range.vertexStart = 0\n } else {\n range.vertexStart = lastRange.vertexStart + lastRange.vertexCount\n }\n\n if (geometry.getIndex() !== null) {\n if (indexCount === -1) {\n range.indexCount = geometry.getIndex()!.count\n } else {\n range.indexCount = indexCount\n }\n\n if (lastRange === null) {\n range.indexStart = 0\n } else {\n range.indexStart = lastRange.indexStart + lastRange.indexCount\n }\n }\n\n if (\n (range.indexStart !== -1 && range.indexStart + range.indexCount > this._maxIndexCount) ||\n range.vertexStart + range.vertexCount > this._maxVertexCount\n ) {\n throw new Error('BatchedMesh: Reserved space request exceeds the maximum buffer size.')\n }\n\n const indexCounts = this._indexCounts\n const indexStarts = this._indexStarts\n const vertexCounts = this._vertexCounts\n const vertexStarts = this._vertexStarts\n\n const visible = this._visible\n const active = this._active\n const matricesTexture = this._matricesTexture\n const matrices = this._matrices\n const matricesArray = this._matricesTexture!.image.data\n\n // push new visibility states\n visible.push(true)\n active.push(true)\n\n // update id\n const geometryId = this._geometryCount\n this._geometryCount++\n\n // initialize matrix information\n matrices.push(new Matrix4())\n _identityMatrix.toArray(matricesArray, geometryId * 16)\n matricesTexture!.needsUpdate = true\n\n // add the reserved range\n reservedRanges.push(range)\n\n // push new geometry data range\n vertexStarts.push(range.vertexStart)\n vertexCounts.push(range.vertexCount)\n\n if (geometry.getIndex() !== null) {\n // push new index range\n indexStarts.push(range.indexCount)\n indexCounts.push(range.indexCount)\n }\n\n // set the id for the geometry\n const idAttribute = this.geometry.getAttribute(ID_ATTR_NAME)\n for (let i = 0; i < range.vertexCount; i++) {\n idAttribute.setX(range.vertexStart + i, geometryId)\n }\n\n idAttribute.needsUpdate = true\n\n // update the geometry\n this.setGeometryAt(geometryId, geometry)\n\n return geometryId\n }\n\n /**\n * @deprecated use `addGeometry` instead.\n */\n applyGeometry(geometry: BufferGeometry): number {\n return this.addGeometry(geometry)\n }\n\n setGeometryAt(id: number, geometry: BufferGeometry): number {\n if (id >= this._geometryCount) {\n throw new Error('BatchedMesh: Maximum geometry count reached.')\n }\n\n this._validateGeometry(geometry)\n\n const range = this._reservedRanges[id]\n if (\n (geometry.getIndex() !== null && geometry.getIndex()!.count > range.indexCount) ||\n geometry.attributes.position.count > range.vertexCount\n ) {\n throw new Error('BatchedMesh: Reserved space not large enough for provided geometry.')\n }\n\n // copy geometry over\n const batchGeometry = this.geometry\n const srcPositionAttribute = geometry.getAttribute('position')\n const hasIndex = batchGeometry.getIndex() !== null\n const dstIndex = batchGeometry.getIndex()!\n const srcIndex = geometry.getIndex()!\n\n // copy attribute data over\n const vertexStart = range.vertexStart\n const vertexCount = range.vertexCount\n for (const attributeName in batchGeometry.attributes) {\n if (attributeName === ID_ATTR_NAME) {\n continue\n }\n\n const srcAttribute = geometry.getAttribute(attributeName)\n const dstAttribute = batchGeometry.getAttribute(attributeName)\n copyAttributeData(srcAttribute, dstAttribute, vertexStart)\n\n // fill the rest in with zeroes\n const itemSize = srcAttribute.itemSize\n for (let i = srcAttribute.count, l = vertexCount; i < l; i++) {\n const index = vertexStart + i\n for (let c = 0; c < itemSize; c++) {\n // @ts-ignore\n dstAttribute.setComponent(index, c, 0)\n }\n }\n\n dstAttribute.needsUpdate = true\n }\n\n this._vertexCounts[id] = srcPositionAttribute.count\n\n if (hasIndex) {\n // fill the rest in with zeroes\n const indexStart = range.indexStart\n\n // copy index data over\n for (let i = 0; i < srcIndex.count; i++) {\n dstIndex.setX(indexStart + i, vertexStart + srcIndex.getX(i))\n }\n\n // fill the rest in with zeroes\n for (let i = srcIndex.count, l = range.indexCount; i < l; i++) {\n dstIndex.setX(indexStart + i, vertexStart)\n }\n\n dstIndex.needsUpdate = true\n this._indexCounts[id] = srcIndex.count\n }\n\n return id\n }\n\n deleteGeometry(geometryId: number): this {\n // Note: User needs to call optimize() afterward to pack the data.\n\n const active = this._active\n const matricesTexture = this._matricesTexture!\n const matricesArray = matricesTexture.image.data\n if (geometryId >= active.length || active[geometryId] === false) {\n return this\n }\n\n active[geometryId] = false\n _zeroScaleMatrix.toArray(matricesArray, geometryId * 16)\n matricesTexture!.needsUpdate = true\n\n return this\n }\n\n optimize(): never {\n throw new Error('BatchedMesh: Optimize function not implemented.')\n }\n\n setMatrixAt(geometryId: number, matrix: Matrix4): this {\n // @TODO: Map geometryId to index of the arrays because\n // optimize() can make geometryId mismatch the index\n\n const visible = this._visible\n const active = this._active\n const matricesTexture = this._matricesTexture!\n const matrices = this._matrices\n const matricesArray = matricesTexture.image.data\n if (geometryId >= matrices.length || active[geometryId] === false) {\n return this\n }\n\n if (visible[geometryId] === true) {\n matrix.toArray(matricesArray, geometryId * 16)\n matricesTexture.needsUpdate = true\n }\n\n matrices[geometryId].copy(matrix)\n\n return this\n }\n\n getMatrixAt(geometryId: number, matrix: Matrix4): Matrix4 {\n const matrices = this._matrices\n const active = this._active\n if (geometryId >= matrices.length || active[geometryId] === false) {\n return matrix\n }\n\n return matrix.copy(matrices[geometryId])\n }\n\n setVisibleAt(geometryId: number, value: boolean): this {\n const visible = this._visible\n const active = this._active\n const matricesTexture = this._matricesTexture!\n const matrices = this._matrices\n const matricesArray = matricesTexture.image.data\n\n // if the geometry is out of range, not active, or visibility state\n // does not change then return early\n if (geometryId >= visible.length || active[geometryId] === false || visible[geometryId] === value) {\n return this\n }\n\n // scale the matrix to zero if it's hidden\n if (value === true) {\n matrices[geometryId].toArray(matricesArray, geometryId * 16)\n } else {\n _zeroScaleMatrix.toArray(matricesArray, geometryId * 16)\n }\n\n matricesTexture.needsUpdate = true\n visible[geometryId] = value\n\n return this\n }\n\n getVisibleAt(geometryId: number): boolean {\n const visible = this._visible\n const active = this._active\n\n // return early if the geometry is out of range or not active\n if (geometryId >= visible.length || active[geometryId] === false) {\n return false\n }\n\n return visible[geometryId]\n }\n\n raycast(): void {\n console.warn('BatchedMesh: Raycast function not implemented.')\n }\n\n copy(): never {\n // super.copy( source );\n\n throw new Error('BatchedMesh: Copy function not implemented.')\n }\n\n toJSON(): never {\n throw new Error('BatchedMesh: toJSON function not implemented.')\n }\n\n dispose(): this {\n // Assuming the geometry is not shared with other meshes\n this.geometry.dispose()\n\n this._matricesTexture!.dispose()\n this._matricesTexture = null!\n\n return this\n }\n}\n\nexport { BatchedMesh }\n"],"names":["Matrix4","Mesh","BufferGeometry","MathUtils","DataTexture","RGBAFormat","FloatType","BufferAttribute"],"mappings":";;;;;;;;;AAcA,MAAM,eAAe;AACrB,MAAM,kBAAkB,IAAIA,MAAAA;AAC5B,MAAM,mBAAmB,IAAIA,cAAQ,EAAE,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAGzF,MAAM;AAAA;AAAA,EAAgC;AAAA;AAAA,mBAEnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBnB,MAAM;AAAA;AAAA,EAAgC;AAAA;AAA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AO;AAC1D,aAAA;AAAA,IACT;AAEA,WAAO,OAAO,KAAK,SAAS,UAAU,CAAC;AAAA,EACzC;AAAA,EAEA,aAAa,YAAoB,OAAsB;AACrD,UAAM,UAAU,KAAK;AACrB,UAAM,SAAS,KAAK;AACpB,UAAM,kBAAkB,KAAK;AAC7B,UAAM,WAAW,KAAK;AAChB,UAAA,gBAAgB,gBAAgB,MAAM;AAIxC,QAAA,cAAc,QAAQ,UAAU,OAAO,UAAU,MAAM,SAAS,QAAQ,UAAU,MAAM,OAAO;AAC1F,aAAA;AAAA,IACT;AAGA,QAAI,UAAU,MAAM;AAClB,eAAS,UAAU,EAAE,QAAQ,eAAe,aAAa,EAAE;AAAA,IAAA,OACtD;AACY,uBAAA,QAAQ,eAAe,aAAa,EAAE;AAAA,IACzD;AAEA,oBAAgB,cAAc;AAC9B,YAAQ,UAAU,IAAI;AAEf,WAAA;AAAA,EACT;AAAA,EAEA,aAAa,YAA6B;AACxC,UAAM,UAAU,KAAK;AACrB,UAAM,SAAS,KAAK;AAGpB,QAAI,cAAc,QAAQ,UAAU,OAAO,UAAU,MAAM,OAAO;AACzD,aAAA;AAAA,IACT;AAEA,WAAO,QAAQ,UAAU;AAAA,EAC3B;AAAA,EAEA,UAAgB;AACd,YAAQ,KAAK,gDAAgD;AAAA,EAC/D;AAAA,EAEA,OAAc;AAGN,UAAA,IAAI,MAAM,6CAA6C;AAAA,EAC/D;AAAA,EAEA,SAAgB;AACR,UAAA,IAAI,MAAM,+CAA+C;AAAA,EACjE;AAAA,EAEA,UAAgB;AAEd,SAAK,SAAS;AAEd,SAAK,iBAAkB;AACvB,SAAK,mBAAmB;AAEjB,WAAA;AAAA,EACT;AACF;;"}
@@ -4,35 +4,45 @@ declare class BatchedMesh extends Mesh<BufferGeometry, Material> {
4
4
  _vertexCounts: number[];
5
5
  _indexStarts: number[];
6
6
  _indexCounts: number[];
7
- _visibles: boolean[];
8
- _alives: boolean[];
7
+ _reservedRanges: {
8
+ vertexStart: number;
9
+ vertexCount: number;
10
+ indexStart: number;
11
+ indexCount: number;
12
+ }[];
13
+ _visible: boolean[];
14
+ _active: boolean[];
9
15
  _maxGeometryCount: number;
10
16
  _maxVertexCount: number;
11
17
  _maxIndexCount: number;
12
18
  _geometryInitialized: boolean;
13
19
  _geometryCount: number;
14
- _vertexCount: number;
15
- _indexCount: number;
16
20
  _matrices: Matrix4[];
17
- _matricesArray: Float32Array | null;
18
21
  _matricesTexture: DataTexture | null;
19
- _matricesTextureSize: number | null;
20
22
  _customUniforms: Record<string, IUniform>;
21
23
  constructor(maxGeometryCount: number, maxVertexCount: number, maxIndexCount?: number, material?: Material);
22
24
  _initMatricesTexture(): void;
23
25
  _initShader(): void;
26
+ _initializeGeometry(reference: BufferGeometry): void;
27
+ _validateGeometry(geometry: BufferGeometry): void;
24
28
  getGeometryCount(): number;
25
29
  getVertexCount(): number;
26
30
  getIndexCount(): number;
31
+ addGeometry(geometry: BufferGeometry, vertexCount?: number, indexCount?: number): number;
32
+ /**
33
+ * @deprecated use `addGeometry` instead.
34
+ */
27
35
  applyGeometry(geometry: BufferGeometry): number;
36
+ setGeometryAt(id: number, geometry: BufferGeometry): number;
28
37
  deleteGeometry(geometryId: number): this;
29
- optimize(): this;
38
+ optimize(): never;
30
39
  setMatrixAt(geometryId: number, matrix: Matrix4): this;
31
40
  getMatrixAt(geometryId: number, matrix: Matrix4): Matrix4;
32
- setVisibleAt(geometryId: number, visible: boolean): this;
41
+ setVisibleAt(geometryId: number, value: boolean): this;
33
42
  getVisibleAt(geometryId: number): boolean;
34
- copy(source: BatchedMesh): this;
35
- toJSON(meta: any): any;
43
+ raycast(): void;
44
+ copy(): never;
45
+ toJSON(): never;
36
46
  dispose(): this;
37
47
  }
38
48
  export { BatchedMesh };
@@ -5,27 +5,27 @@ var __publicField = (obj, key, value) => {
5
5
  return value;
6
6
  };
7
7
  import { Matrix4, Mesh, BufferGeometry, MathUtils, DataTexture, RGBAFormat, FloatType, BufferAttribute } from "three";
8
+ const ID_ATTR_NAME = "_batch_id_";
8
9
  const _identityMatrix = new Matrix4();
9
- const _zeroMatrix = new Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
10
+ const _zeroScaleMatrix = new Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
10
11
  const batchingParsVertex = (
11
12
  /* glsl */
12
13
  `
13
14
  #ifdef BATCHING
14
- attribute float id;
15
+ attribute float ${ID_ATTR_NAME};
15
16
  uniform highp sampler2D batchingTexture;
16
- uniform int batchingTextureSize;
17
17
  mat4 getBatchingMatrix( const in float i ) {
18
- float j = i * 4.0;
19
- float x = mod( j, float( batchingTextureSize ) );
20
- float y = floor( j / float( batchingTextureSize ) );
21
- float dx = 1.0 / float( batchingTextureSize );
22
- float dy = 1.0 / float( batchingTextureSize );
23
- y = dy * ( y + 0.5 );
24
- vec4 v1 = texture2D( batchingTexture, vec2( dx * ( x + 0.5 ), y ) );
25
- vec4 v2 = texture2D( batchingTexture, vec2( dx * ( x + 1.5 ), y ) );
26
- vec4 v3 = texture2D( batchingTexture, vec2( dx * ( x + 2.5 ), y ) );
27
- vec4 v4 = texture2D( batchingTexture, vec2( dx * ( x + 3.5 ), y ) );
18
+
19
+ int size = textureSize( batchingTexture, 0 ).x;
20
+ int j = int( i ) * 4;
21
+ int x = j % size;
22
+ int y = j / size;
23
+ vec4 v1 = texelFetch( batchingTexture, ivec2( x, y ), 0 );
24
+ vec4 v2 = texelFetch( batchingTexture, ivec2( x + 1, y ), 0 );
25
+ vec4 v3 = texelFetch( batchingTexture, ivec2( x + 2, y ), 0 );
26
+ vec4 v4 = texelFetch( batchingTexture, ivec2( x + 3, y ), 0 );
28
27
  return mat4( v1, v2, v3, v4 );
28
+
29
29
  }
30
30
  #endif
31
31
  `
@@ -34,7 +34,7 @@ const batchingbaseVertex = (
34
34
  /* glsl */
35
35
  `
36
36
  #ifdef BATCHING
37
- mat4 batchingMatrix = getBatchingMatrix( id );
37
+ mat4 batchingMatrix = getBatchingMatrix( ${ID_ATTR_NAME} );
38
38
  #endif
39
39
  `
40
40
  );
@@ -57,6 +57,20 @@ const batchingVertex = (
57
57
  #endif
58
58
  `
59
59
  );
60
+ function copyAttributeData(src, target, targetOffset = 0) {
61
+ const itemSize = target.itemSize;
62
+ if (src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor) {
63
+ const vertexCount = src.count;
64
+ for (let i = 0; i < vertexCount; i++) {
65
+ for (let c = 0; c < itemSize; c++) {
66
+ target.setComponent(i + targetOffset, c, src.getComponent(i, c));
67
+ }
68
+ }
69
+ } else {
70
+ target.array.set(src.array, targetOffset * itemSize);
71
+ }
72
+ target.needsUpdate = true;
73
+ }
60
74
  class BatchedMesh extends Mesh {
61
75
  constructor(maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material) {
62
76
  super(new BufferGeometry(), material);
@@ -64,41 +78,34 @@ class BatchedMesh extends Mesh {
64
78
  __publicField(this, "_vertexCounts");
65
79
  __publicField(this, "_indexStarts");
66
80
  __publicField(this, "_indexCounts");
67
- __publicField(this, "_visibles");
68
- __publicField(this, "_alives");
81
+ __publicField(this, "_reservedRanges");
82
+ __publicField(this, "_visible");
83
+ __publicField(this, "_active");
69
84
  __publicField(this, "_maxGeometryCount");
70
85
  __publicField(this, "_maxVertexCount");
71
86
  __publicField(this, "_maxIndexCount");
72
87
  __publicField(this, "_geometryInitialized");
73
88
  __publicField(this, "_geometryCount");
74
- __publicField(this, "_vertexCount");
75
- __publicField(this, "_indexCount");
76
89
  __publicField(this, "_matrices");
77
- __publicField(this, "_matricesArray");
78
90
  __publicField(this, "_matricesTexture");
79
- __publicField(this, "_matricesTextureSize");
80
91
  __publicField(this, "_customUniforms");
81
92
  this._vertexStarts = [];
82
93
  this._vertexCounts = [];
83
94
  this._indexStarts = [];
84
95
  this._indexCounts = [];
85
- this._visibles = [];
86
- this._alives = [];
96
+ this._reservedRanges = [];
97
+ this._visible = [];
98
+ this._active = [];
87
99
  this._maxGeometryCount = maxGeometryCount;
88
100
  this._maxVertexCount = maxVertexCount;
89
101
  this._maxIndexCount = maxIndexCount;
90
102
  this._geometryInitialized = false;
91
103
  this._geometryCount = 0;
92
- this._vertexCount = 0;
93
- this._indexCount = 0;
94
104
  this._matrices = [];
95
- this._matricesArray = null;
96
105
  this._matricesTexture = null;
97
- this._matricesTextureSize = null;
98
106
  this.frustumCulled = false;
99
107
  this._customUniforms = {
100
- batchingTexture: { value: null },
101
- batchingTextureSize: { value: 0 }
108
+ batchingTexture: { value: null }
102
109
  };
103
110
  this._initMatricesTexture();
104
111
  this._initShader();
@@ -119,158 +126,295 @@ class BatchedMesh extends Mesh {
119
126
  size = Math.max(size, 4);
120
127
  const matricesArray = new Float32Array(size * size * 4);
121
128
  const matricesTexture = new DataTexture(matricesArray, size, size, RGBAFormat, FloatType);
122
- this._matricesArray = matricesArray;
123
129
  this._matricesTexture = matricesTexture;
124
- this._matricesTextureSize = size;
125
130
  this._customUniforms.batchingTexture.value = this._matricesTexture;
126
- this._customUniforms.batchingTextureSize.value = this._matricesTextureSize;
127
131
  }
128
132
  _initShader() {
129
- const currentOnBeforeCompile = this.material.onBeforeCompile;
133
+ const material = this.material;
134
+ const currentOnBeforeCompile = material.onBeforeCompile;
130
135
  const customUniforms = this._customUniforms;
131
- this.material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {
132
- parameters.vertexShader = parameters.vertexShader.replace("#include <skinning_pars_vertex>", "#include <skinning_pars_vertex>\n" + batchingParsVertex).replace(
133
- "#include <skinnormal_vertex>",
134
- "#include <skinnormal_vertex>\n" + batchingbaseVertex + batchingnormalVertex
135
- ).replace("#include <skinning_vertex>", "#include <skinning_vertex>\n" + batchingVertex);
136
+ material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {
137
+ parameters.vertexShader = parameters.vertexShader.replace("#include <skinning_pars_vertex>", "#include <skinning_pars_vertex>\n" + batchingParsVertex).replace("#include <uv_vertex>", "#include <uv_vertex>\n" + batchingbaseVertex).replace("#include <skinnormal_vertex>", "#include <skinnormal_vertex>\n" + batchingnormalVertex).replace("#include <skinning_vertex>", "#include <skinning_vertex>\n" + batchingVertex);
136
138
  for (const uniformName in customUniforms) {
137
139
  parameters.uniforms[uniformName] = customUniforms[uniformName];
138
140
  }
139
141
  currentOnBeforeCompile.call(this, parameters, renderer);
140
142
  };
141
- this.material.defines = this.material.defines || {};
142
- this.material.defines.BATCHING = false;
143
+ material.defines = material.defines || {};
144
+ material.defines.BATCHING = false;
145
+ }
146
+ _initializeGeometry(reference) {
147
+ const geometry = this.geometry;
148
+ const maxVertexCount = this._maxVertexCount;
149
+ const maxGeometryCount = this._maxGeometryCount;
150
+ const maxIndexCount = this._maxIndexCount;
151
+ if (this._geometryInitialized === false) {
152
+ for (const attributeName in reference.attributes) {
153
+ const srcAttribute = reference.getAttribute(attributeName);
154
+ const { array, itemSize, normalized } = srcAttribute;
155
+ const dstArray = new array.constructor(maxVertexCount * itemSize);
156
+ const dstAttribute = new srcAttribute.constructor(dstArray, itemSize, normalized);
157
+ dstAttribute.setUsage(srcAttribute.usage);
158
+ geometry.setAttribute(attributeName, dstAttribute);
159
+ }
160
+ if (reference.getIndex() !== null) {
161
+ const indexArray = maxVertexCount > 65536 ? new Uint32Array(maxIndexCount) : new Uint16Array(maxIndexCount);
162
+ geometry.setIndex(new BufferAttribute(indexArray, 1));
163
+ }
164
+ const idArray = maxGeometryCount > 65536 ? new Uint32Array(maxVertexCount) : new Uint16Array(maxVertexCount);
165
+ geometry.setAttribute(ID_ATTR_NAME, new BufferAttribute(idArray, 1));
166
+ this._geometryInitialized = true;
167
+ }
168
+ }
169
+ // Make sure the geometry is compatible with the existing combined geometry atributes
170
+ _validateGeometry(geometry) {
171
+ if (geometry.getAttribute(ID_ATTR_NAME)) {
172
+ throw new Error(`BatchedMesh: Geometry cannot use attribute "${ID_ATTR_NAME}"`);
173
+ }
174
+ const batchGeometry = this.geometry;
175
+ if (Boolean(geometry.getIndex()) !== Boolean(batchGeometry.getIndex())) {
176
+ throw new Error('BatchedMesh: All geometries must consistently have "index".');
177
+ }
178
+ for (const attributeName in batchGeometry.attributes) {
179
+ if (attributeName === ID_ATTR_NAME) {
180
+ continue;
181
+ }
182
+ if (!geometry.hasAttribute(attributeName)) {
183
+ throw new Error(
184
+ `BatchedMesh: Added geometry missing "${attributeName}". All geometries must have consistent attributes.`
185
+ );
186
+ }
187
+ const srcAttribute = geometry.getAttribute(attributeName);
188
+ const dstAttribute = batchGeometry.getAttribute(attributeName);
189
+ if (srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized) {
190
+ throw new Error("BatchedMesh: All attributes must have a consistent itemSize and normalized value.");
191
+ }
192
+ }
143
193
  }
144
194
  getGeometryCount() {
145
195
  return this._geometryCount;
146
196
  }
147
197
  getVertexCount() {
148
- return this._vertexCount;
198
+ const reservedRanges = this._reservedRanges;
199
+ if (reservedRanges.length === 0) {
200
+ return 0;
201
+ } else {
202
+ const finalRange = reservedRanges[reservedRanges.length - 1];
203
+ return finalRange.vertexStart + finalRange.vertexCount;
204
+ }
149
205
  }
150
206
  getIndexCount() {
151
- return this._indexCount;
207
+ const reservedRanges = this._reservedRanges;
208
+ const geometry = this.geometry;
209
+ if (geometry.getIndex() === null || reservedRanges.length === 0) {
210
+ return 0;
211
+ } else {
212
+ const finalRange = reservedRanges[reservedRanges.length - 1];
213
+ return finalRange.indexStart + finalRange.indexCount;
214
+ }
152
215
  }
153
- applyGeometry(geometry) {
154
- var _a;
155
- if (this._geometryCount >= this._maxGeometryCount)
156
- ;
157
- if (this._geometryInitialized === false) {
158
- for (const attributeName in geometry.attributes) {
159
- const srcAttribute = geometry.getAttribute(attributeName);
160
- const { array, itemSize, normalized } = srcAttribute;
161
- const dstArray = new array.constructor(this._maxVertexCount * itemSize);
162
- const dstAttribute = new srcAttribute.constructor(dstArray, itemSize, normalized);
163
- dstAttribute.setUsage(srcAttribute.usage);
164
- this.geometry.setAttribute(attributeName, dstAttribute);
216
+ addGeometry(geometry, vertexCount = -1, indexCount = -1) {
217
+ this._initializeGeometry(geometry);
218
+ this._validateGeometry(geometry);
219
+ if (this._geometryCount >= this._maxGeometryCount) {
220
+ throw new Error("BatchedMesh: Maximum geometry count reached.");
221
+ }
222
+ const range = {
223
+ vertexStart: -1,
224
+ vertexCount: -1,
225
+ indexStart: -1,
226
+ indexCount: -1
227
+ };
228
+ let lastRange = null;
229
+ const reservedRanges = this._reservedRanges;
230
+ if (this._geometryCount !== 0) {
231
+ lastRange = reservedRanges[reservedRanges.length - 1];
232
+ }
233
+ if (vertexCount === -1) {
234
+ range.vertexCount = geometry.getAttribute("position").count;
235
+ } else {
236
+ range.vertexCount = vertexCount;
237
+ }
238
+ if (lastRange === null) {
239
+ range.vertexStart = 0;
240
+ } else {
241
+ range.vertexStart = lastRange.vertexStart + lastRange.vertexCount;
242
+ }
243
+ if (geometry.getIndex() !== null) {
244
+ if (indexCount === -1) {
245
+ range.indexCount = geometry.getIndex().count;
246
+ } else {
247
+ range.indexCount = indexCount;
165
248
  }
166
- if (geometry.getIndex() !== null) {
167
- const indexArray = this._maxVertexCount > 65536 ? new Uint32Array(this._maxIndexCount) : new Uint16Array(this._maxIndexCount);
168
- this.geometry.setIndex(new BufferAttribute(indexArray, 1));
249
+ if (lastRange === null) {
250
+ range.indexStart = 0;
251
+ } else {
252
+ range.indexStart = lastRange.indexStart + lastRange.indexCount;
169
253
  }
170
- const idArray = this._maxGeometryCount > 65536 ? new Uint32Array(this._maxVertexCount) : new Uint16Array(this._maxVertexCount);
171
- this.geometry.setAttribute("id", new BufferAttribute(idArray, 1));
172
- this._geometryInitialized = true;
173
254
  }
174
- const hasIndex = this.geometry.getIndex() !== null;
175
- const dstIndex = this.geometry.getIndex();
176
- const srcIndex = geometry.getIndex();
177
- const srcPositionAttribute = geometry.getAttribute("position");
178
- this._vertexStarts.push(this._vertexCount);
179
- this._vertexCounts.push(srcPositionAttribute.count);
180
- if (hasIndex) {
181
- this._indexStarts.push(this._indexCount);
182
- this._indexCounts.push(srcIndex.count);
255
+ if (range.indexStart !== -1 && range.indexStart + range.indexCount > this._maxIndexCount || range.vertexStart + range.vertexCount > this._maxVertexCount) {
256
+ throw new Error("BatchedMesh: Reserved space request exceeds the maximum buffer size.");
257
+ }
258
+ const indexCounts = this._indexCounts;
259
+ const indexStarts = this._indexStarts;
260
+ const vertexCounts = this._vertexCounts;
261
+ const vertexStarts = this._vertexStarts;
262
+ const visible = this._visible;
263
+ const active = this._active;
264
+ const matricesTexture = this._matricesTexture;
265
+ const matrices = this._matrices;
266
+ const matricesArray = this._matricesTexture.image.data;
267
+ visible.push(true);
268
+ active.push(true);
269
+ const geometryId = this._geometryCount;
270
+ this._geometryCount++;
271
+ matrices.push(new Matrix4());
272
+ _identityMatrix.toArray(matricesArray, geometryId * 16);
273
+ matricesTexture.needsUpdate = true;
274
+ reservedRanges.push(range);
275
+ vertexStarts.push(range.vertexStart);
276
+ vertexCounts.push(range.vertexCount);
277
+ if (geometry.getIndex() !== null) {
278
+ indexStarts.push(range.indexCount);
279
+ indexCounts.push(range.indexCount);
280
+ }
281
+ const idAttribute = this.geometry.getAttribute(ID_ATTR_NAME);
282
+ for (let i = 0; i < range.vertexCount; i++) {
283
+ idAttribute.setX(range.vertexStart + i, geometryId);
284
+ }
285
+ idAttribute.needsUpdate = true;
286
+ this.setGeometryAt(geometryId, geometry);
287
+ return geometryId;
288
+ }
289
+ /**
290
+ * @deprecated use `addGeometry` instead.
291
+ */
292
+ applyGeometry(geometry) {
293
+ return this.addGeometry(geometry);
294
+ }
295
+ setGeometryAt(id, geometry) {
296
+ if (id >= this._geometryCount) {
297
+ throw new Error("BatchedMesh: Maximum geometry count reached.");
183
298
  }
184
- this._visibles.push(true);
185
- this._alives.push(true);
186
- for (const attributeName in geometry.attributes) {
299
+ this._validateGeometry(geometry);
300
+ const range = this._reservedRanges[id];
301
+ if (geometry.getIndex() !== null && geometry.getIndex().count > range.indexCount || geometry.attributes.position.count > range.vertexCount) {
302
+ throw new Error("BatchedMesh: Reserved space not large enough for provided geometry.");
303
+ }
304
+ const batchGeometry = this.geometry;
305
+ const srcPositionAttribute = geometry.getAttribute("position");
306
+ const hasIndex = batchGeometry.getIndex() !== null;
307
+ const dstIndex = batchGeometry.getIndex();
308
+ const srcIndex = geometry.getIndex();
309
+ const vertexStart = range.vertexStart;
310
+ const vertexCount = range.vertexCount;
311
+ for (const attributeName in batchGeometry.attributes) {
312
+ if (attributeName === ID_ATTR_NAME) {
313
+ continue;
314
+ }
187
315
  const srcAttribute = geometry.getAttribute(attributeName);
188
- const dstAttribute = this.geometry.getAttribute(attributeName);
189
- dstAttribute.array.set(srcAttribute.array, this._vertexCount * dstAttribute.itemSize);
316
+ const dstAttribute = batchGeometry.getAttribute(attributeName);
317
+ copyAttributeData(srcAttribute, dstAttribute, vertexStart);
318
+ const itemSize = srcAttribute.itemSize;
319
+ for (let i = srcAttribute.count, l = vertexCount; i < l; i++) {
320
+ const index = vertexStart + i;
321
+ for (let c = 0; c < itemSize; c++) {
322
+ dstAttribute.setComponent(index, c, 0);
323
+ }
324
+ }
190
325
  dstAttribute.needsUpdate = true;
191
326
  }
327
+ this._vertexCounts[id] = srcPositionAttribute.count;
192
328
  if (hasIndex) {
329
+ const indexStart = range.indexStart;
193
330
  for (let i = 0; i < srcIndex.count; i++) {
194
- dstIndex.setX(this._indexCount + i, this._vertexCount + srcIndex.getX(i));
331
+ dstIndex.setX(indexStart + i, vertexStart + srcIndex.getX(i));
332
+ }
333
+ for (let i = srcIndex.count, l = range.indexCount; i < l; i++) {
334
+ dstIndex.setX(indexStart + i, vertexStart);
195
335
  }
196
- this._indexCount += srcIndex.count;
197
336
  dstIndex.needsUpdate = true;
337
+ this._indexCounts[id] = srcIndex.count;
198
338
  }
199
- const geometryId = this._geometryCount;
200
- this._geometryCount++;
201
- const idAttribute = this.geometry.getAttribute("id");
202
- for (let i = 0; i < srcPositionAttribute.count; i++) {
203
- idAttribute.setX(this._vertexCount + i, geometryId);
204
- }
205
- idAttribute.needsUpdate = true;
206
- this._vertexCount += srcPositionAttribute.count;
207
- this._matrices.push(new Matrix4());
208
- _identityMatrix.toArray((_a = this._matricesArray) != null ? _a : void 0, geometryId * 16);
209
- this._matricesTexture.needsUpdate = true;
210
- return geometryId;
339
+ return id;
211
340
  }
212
341
  deleteGeometry(geometryId) {
213
- if (geometryId >= this._alives.length || this._alives[geometryId] === false) {
342
+ const active = this._active;
343
+ const matricesTexture = this._matricesTexture;
344
+ const matricesArray = matricesTexture.image.data;
345
+ if (geometryId >= active.length || active[geometryId] === false) {
214
346
  return this;
215
347
  }
216
- this._alives[geometryId] = false;
217
- _zeroMatrix.toArray(this._matricesArray, geometryId * 16);
218
- this._matricesTexture.needsUpdate = true;
348
+ active[geometryId] = false;
349
+ _zeroScaleMatrix.toArray(matricesArray, geometryId * 16);
350
+ matricesTexture.needsUpdate = true;
219
351
  return this;
220
352
  }
221
353
  optimize() {
222
- return this;
354
+ throw new Error("BatchedMesh: Optimize function not implemented.");
223
355
  }
224
356
  setMatrixAt(geometryId, matrix) {
225
- if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {
357
+ const visible = this._visible;
358
+ const active = this._active;
359
+ const matricesTexture = this._matricesTexture;
360
+ const matrices = this._matrices;
361
+ const matricesArray = matricesTexture.image.data;
362
+ if (geometryId >= matrices.length || active[geometryId] === false) {
226
363
  return this;
227
364
  }
228
- this._matrices[geometryId].copy(matrix);
229
- if (this._visibles[geometryId] === true) {
230
- matrix.toArray(this._matricesArray, geometryId * 16);
231
- this._matricesTexture.needsUpdate = true;
365
+ if (visible[geometryId] === true) {
366
+ matrix.toArray(matricesArray, geometryId * 16);
367
+ matricesTexture.needsUpdate = true;
232
368
  }
369
+ matrices[geometryId].copy(matrix);
233
370
  return this;
234
371
  }
235
372
  getMatrixAt(geometryId, matrix) {
236
- if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {
373
+ const matrices = this._matrices;
374
+ const active = this._active;
375
+ if (geometryId >= matrices.length || active[geometryId] === false) {
237
376
  return matrix;
238
377
  }
239
- return matrix.copy(this._matrices[geometryId]);
378
+ return matrix.copy(matrices[geometryId]);
240
379
  }
241
- setVisibleAt(geometryId, visible) {
242
- if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {
380
+ setVisibleAt(geometryId, value) {
381
+ const visible = this._visible;
382
+ const active = this._active;
383
+ const matricesTexture = this._matricesTexture;
384
+ const matrices = this._matrices;
385
+ const matricesArray = matricesTexture.image.data;
386
+ if (geometryId >= visible.length || active[geometryId] === false || visible[geometryId] === value) {
243
387
  return this;
244
388
  }
245
- if (this._visibles[geometryId] === visible) {
246
- return this;
247
- }
248
- if (visible === true) {
249
- this._matrices[geometryId].toArray(this._matricesArray, geometryId * 16);
389
+ if (value === true) {
390
+ matrices[geometryId].toArray(matricesArray, geometryId * 16);
250
391
  } else {
251
- _zeroMatrix.toArray(this._matricesArray, geometryId * 16);
392
+ _zeroScaleMatrix.toArray(matricesArray, geometryId * 16);
252
393
  }
253
- this._matricesTexture.needsUpdate = true;
254
- this._visibles[geometryId] = visible;
394
+ matricesTexture.needsUpdate = true;
395
+ visible[geometryId] = value;
255
396
  return this;
256
397
  }
257
398
  getVisibleAt(geometryId) {
258
- if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {
399
+ const visible = this._visible;
400
+ const active = this._active;
401
+ if (geometryId >= visible.length || active[geometryId] === false) {
259
402
  return false;
260
403
  }
261
- return this._visibles[geometryId];
404
+ return visible[geometryId];
262
405
  }
263
- copy(source) {
264
- super.copy(source);
265
- return this;
406
+ raycast() {
407
+ console.warn("BatchedMesh: Raycast function not implemented.");
408
+ }
409
+ copy() {
410
+ throw new Error("BatchedMesh: Copy function not implemented.");
266
411
  }
267
- toJSON(meta) {
268
- return super.toJSON(meta);
412
+ toJSON() {
413
+ throw new Error("BatchedMesh: toJSON function not implemented.");
269
414
  }
270
415
  dispose() {
271
- var _a;
272
416
  this.geometry.dispose();
273
- (_a = this._matricesTexture) == null ? void 0 : _a.dispose();
417
+ this._matricesTexture.dispose();
274
418
  this._matricesTexture = null;
275
419
  return this;
276
420
  }
@@ -1 +1 @@
1
- {"version":3,"file":"BatchedMesh.js","sources":["../../src/objects/BatchedMesh.ts"],"sourcesContent":["import {\n Matrix4,\n Mesh,\n BufferGeometry,\n Material,\n DataTexture,\n IUniform,\n MathUtils,\n RGBAFormat,\n FloatType,\n BufferAttribute,\n} from 'three'\n\nconst _identityMatrix = new Matrix4()\nconst _zeroMatrix = new Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)\n\n// Custom shaders\nconst batchingParsVertex = /* glsl */ `\n#ifdef BATCHING\n\tattribute float id;\n\tuniform highp sampler2D batchingTexture;\n\tuniform int batchingTextureSize;\n\tmat4 getBatchingMatrix( const in float i ) {\n\t\tfloat j = i * 4.0;\n\t\tfloat x = mod( j, float( batchingTextureSize ) );\n\t\tfloat y = floor( j / float( batchingTextureSize ) );\n\t\tfloat dx = 1.0 / float( batchingTextureSize );\n\t\tfloat dy = 1.0 / float( batchingTextureSize );\n\t\ty = dy * ( y + 0.5 );\n\t\tvec4 v1 = texture2D( batchingTexture, vec2( dx * ( x + 0.5 ), y ) );\n\t\tvec4 v2 = texture2D( batchingTexture, vec2( dx * ( x + 1.5 ), y ) );\n\t\tvec4 v3 = texture2D( batchingTexture, vec2( dx * ( x + 2.5 ), y ) );\n\t\tvec4 v4 = texture2D( batchingTexture, vec2( dx * ( x + 3.5 ), y ) );\n\t\treturn mat4( v1, v2, v3, v4 );\n\t}\n#endif\n`\n\nconst batchingbaseVertex = /* glsl */ `\n#ifdef BATCHING\n\tmat4 batchingMatrix = getBatchingMatrix( id );\n#endif\n`\n\nconst batchingnormalVertex = /* glsl */ `\n#ifdef BATCHING\n\tobjectNormal = vec4( batchingMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( batchingMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif\n`\n\nconst batchingVertex = /* glsl */ `\n#ifdef BATCHING\n\ttransformed = ( batchingMatrix * vec4( transformed, 1.0 ) ).xyz;\n#endif\n`\n\n// @TODO: SkinnedMesh support?\n// @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.\n\nclass BatchedMesh extends Mesh<BufferGeometry, Material> {\n _vertexStarts: number[]\n _vertexCounts: number[]\n _indexStarts: number[]\n _indexCounts: number[]\n _visibles: boolean[]\n _alives: boolean[]\n _maxGeometryCount: number\n _maxVertexCount: number\n _maxIndexCount: number\n _geometryInitialized: boolean\n _geometryCount: number\n _vertexCount: number\n _indexCount: number\n _matrices: Matrix4[]\n _matricesArray: Float32Array | null\n _matricesTexture: DataTexture | null\n _matricesTextureSize: number | null\n _customUniforms: Record<string, IUniform>\n\n constructor(\n maxGeometryCount: number,\n maxVertexCount: number,\n maxIndexCount = maxVertexCount * 2,\n material?: Material,\n ) {\n super(new BufferGeometry(), material)\n\n this._vertexStarts = []\n this._vertexCounts = []\n this._indexStarts = []\n this._indexCounts = []\n\n this._visibles = []\n this._alives = []\n\n this._maxGeometryCount = maxGeometryCount\n this._maxVertexCount = maxVertexCount\n this._maxIndexCount = maxIndexCount\n\n this._geometryInitialized = false\n this._geometryCount = 0\n this._vertexCount = 0\n this._indexCount = 0\n\n // Local matrix per geometry by using data texture\n // @TODO: Support uniform parameter per geometry\n\n this._matrices = []\n this._matricesArray = null\n this._matricesTexture = null\n this._matricesTextureSize = null\n\n // @TODO: Calculate the entire binding box and make frustumCulled true\n this.frustumCulled = false\n\n this._customUniforms = {\n batchingTexture: { value: null },\n batchingTextureSize: { value: 0 },\n }\n\n this._initMatricesTexture()\n this._initShader()\n\n this.onBeforeRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = true\n }\n\n // @TODO: Implement frustum culling for each geometry\n }\n\n this.onAfterRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = false\n }\n }\n }\n\n _initMatricesTexture() {\n // layout (1 matrix = 4 pixels)\n // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)\n // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)\n // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)\n // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)\n // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)\n\n let size = Math.sqrt(this._maxGeometryCount * 4) // 4 pixels needed for 1 matrix\n size = MathUtils.ceilPowerOfTwo(size)\n size = Math.max(size, 4)\n\n const matricesArray = new Float32Array(size * size * 4) // 4 floats per RGBA pixel\n const matricesTexture = new DataTexture(matricesArray, size, size, RGBAFormat, FloatType)\n\n this._matricesArray = matricesArray\n this._matricesTexture = matricesTexture\n this._matricesTextureSize = size\n\n this._customUniforms.batchingTexture.value = this._matricesTexture\n this._customUniforms.batchingTextureSize.value = this._matricesTextureSize\n }\n\n _initShader() {\n const currentOnBeforeCompile = this.material.onBeforeCompile\n const customUniforms = this._customUniforms\n\n this.material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {\n // Is this replacement stable across any materials?\n parameters.vertexShader = parameters.vertexShader\n .replace('#include <skinning_pars_vertex>', '#include <skinning_pars_vertex>\\n' + batchingParsVertex)\n .replace(\n '#include <skinnormal_vertex>',\n '#include <skinnormal_vertex>\\n' + batchingbaseVertex + batchingnormalVertex,\n )\n .replace('#include <skinning_vertex>', '#include <skinning_vertex>\\n' + batchingVertex)\n\n for (const uniformName in customUniforms) {\n parameters.uniforms[uniformName] = customUniforms[uniformName]\n }\n\n currentOnBeforeCompile.call(this, parameters, renderer)\n }\n\n this.material.defines = this.material.defines || {}\n this.material.defines.BATCHING = false\n }\n\n getGeometryCount() {\n return this._geometryCount\n }\n\n getVertexCount() {\n return this._vertexCount\n }\n\n getIndexCount() {\n return this._indexCount\n }\n\n applyGeometry(geometry: BufferGeometry) {\n // @TODO: geometry.groups support?\n // @TODO: geometry.drawRange support?\n // @TODO: geometry.mortphAttributes support?\n\n if (this._geometryCount >= this._maxGeometryCount) {\n // @TODO: Error handling\n }\n\n if (this._geometryInitialized === false) {\n for (const attributeName in geometry.attributes) {\n const srcAttribute = geometry.getAttribute(attributeName)\n const { array, itemSize, normalized } = srcAttribute\n\n const dstArray = new (array.constructor as Float32ArrayConstructor)(this._maxVertexCount * itemSize)\n const dstAttribute = new (srcAttribute.constructor as any)(dstArray, itemSize, normalized) as BufferAttribute\n\n // TODO: add usage in @types/three\n // @ts-ignore\n dstAttribute.setUsage(srcAttribute.usage)\n\n this.geometry.setAttribute(attributeName, dstAttribute)\n }\n\n if (geometry.getIndex() !== null) {\n const indexArray =\n this._maxVertexCount > 65536 ? new Uint32Array(this._maxIndexCount) : new Uint16Array(this._maxIndexCount)\n\n this.geometry.setIndex(new BufferAttribute(indexArray, 1))\n }\n\n const idArray =\n this._maxGeometryCount > 65536 ? new Uint32Array(this._maxVertexCount) : new Uint16Array(this._maxVertexCount)\n // @TODO: What if attribute name 'id' is already used?\n this.geometry.setAttribute('id', new BufferAttribute(idArray, 1))\n\n this._geometryInitialized = true\n } else {\n // @TODO: Check if geometry has the same attributes set\n }\n\n const hasIndex = this.geometry.getIndex() !== null\n const dstIndex = this.geometry.getIndex()\n const srcIndex = geometry.getIndex()\n\n // Assuming geometry has position attribute\n const srcPositionAttribute = geometry.getAttribute('position')\n\n this._vertexStarts.push(this._vertexCount)\n this._vertexCounts.push(srcPositionAttribute.count)\n\n if (hasIndex) {\n this._indexStarts.push(this._indexCount)\n this._indexCounts.push(srcIndex!.count)\n }\n\n this._visibles.push(true)\n this._alives.push(true)\n\n // @TODO: Error handling if exceeding maxVertexCount or maxIndexCount\n\n for (const attributeName in geometry.attributes) {\n const srcAttribute = geometry.getAttribute(attributeName)\n const dstAttribute = this.geometry.getAttribute(attributeName)\n ;(dstAttribute.array as Float32Array).set(srcAttribute.array, this._vertexCount * dstAttribute.itemSize)\n dstAttribute.needsUpdate = true\n }\n\n if (hasIndex) {\n for (let i = 0; i < srcIndex!.count; i++) {\n dstIndex!.setX(this._indexCount + i, this._vertexCount + srcIndex!.getX(i))\n }\n\n this._indexCount += srcIndex!.count\n dstIndex!.needsUpdate = true\n }\n\n const geometryId = this._geometryCount\n this._geometryCount++\n\n const idAttribute = this.geometry.getAttribute('id')\n\n for (let i = 0; i < srcPositionAttribute.count; i++) {\n idAttribute.setX(this._vertexCount + i, geometryId)\n }\n\n idAttribute.needsUpdate = true\n\n this._vertexCount += srcPositionAttribute.count\n\n this._matrices.push(new Matrix4())\n _identityMatrix.toArray(this._matricesArray ?? undefined, geometryId * 16)\n this._matricesTexture!.needsUpdate = true\n\n return geometryId\n }\n\n deleteGeometry(geometryId: number) {\n if (geometryId >= this._alives.length || this._alives[geometryId] === false) {\n return this\n }\n\n this._alives[geometryId] = false\n _zeroMatrix.toArray(this._matricesArray!, geometryId * 16)\n this._matricesTexture!.needsUpdate = true\n\n // User needs to call optimize() to pack the data.\n\n return this\n }\n\n optimize() {\n // @TODO: Implement\n\n return this\n }\n\n setMatrixAt(geometryId: number, matrix: Matrix4) {\n // @TODO: Map geometryId to index of the arrays because\n // optimize() can make geometryId mismatch the index\n\n if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {\n return this\n }\n\n this._matrices[geometryId].copy(matrix)\n\n if (this._visibles[geometryId] === true) {\n matrix.toArray(this._matricesArray!, geometryId * 16)\n this._matricesTexture!.needsUpdate = true\n }\n\n return this\n }\n\n getMatrixAt(geometryId: number, matrix: Matrix4) {\n if (geometryId >= this._matrices.length || this._alives[geometryId] === false) {\n return matrix\n }\n\n return matrix.copy(this._matrices[geometryId])\n }\n\n setVisibleAt(geometryId: number, visible: boolean) {\n if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {\n return this\n }\n\n if (this._visibles[geometryId] === visible) {\n return this\n }\n\n if (visible === true) {\n this._matrices[geometryId].toArray(this._matricesArray!, geometryId * 16)\n } else {\n _zeroMatrix.toArray(this._matricesArray!, geometryId * 16)\n }\n\n this._matricesTexture!.needsUpdate = true\n this._visibles[geometryId] = visible\n return this\n }\n\n getVisibleAt(geometryId: number) {\n if (geometryId >= this._visibles.length || this._alives[geometryId] === false) {\n return false\n }\n\n return this._visibles[geometryId]\n }\n\n copy(source: BatchedMesh) {\n // @ts-ignore\n super.copy(source)\n\n // @TODO: Implement\n\n return this\n }\n\n toJSON(meta: any) {\n // @TODO: Implement\n\n return super.toJSON(meta)\n }\n\n dispose() {\n // Assuming the geometry is not shared with other meshes\n this.geometry.dispose()\n\n this._matricesTexture?.dispose()\n this._matricesTexture = null\n\n return this\n }\n}\n\nexport { BatchedMesh }\n"],"names":[],"mappings":";;;;;;;AAaA,MAAM,kBAAkB,IAAI;AAC5B,MAAM,cAAc,IAAI,QAAQ,EAAE,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAGpF,MAAM;AAAA;AAAA,EAAgC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBtC,MAAM;AAAA;AAAA,EAAgC;AAAA;AAAA;AAAA;AAAA;AAAA;AAMtC,MAAM;AAAA;AAAA,EAAkC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASxC,MAAM;AAAA;AAAA,EAA4B;AAAA;AAAA;AAAA;AAAA;AAAA;AASlC,MAAM,oBAAoB,KAA+B;AAAA,EAoBvD,YACE,kBACA,gBACA,gBAAgB,iBAAiB,GACjC,UACA;AACM,UAAA,IAAI,kBAAkB,QAAQ;AAzBtC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAUE,SAAK,gBAAgB;AACrB,SAAK,gBAAgB;AACrB,S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,cAAc;AAChC,SAAA,UAAU,UAAU,IAAI;AACtB,WAAA;AAAA,EACT;AAAA,EAEA,aAAa,YAAoB;AAC3B,QAAA,cAAc,KAAK,UAAU,UAAU,KAAK,QAAQ,UAAU,MAAM,OAAO;AACtE,aAAA;AAAA,IACT;AAEO,WAAA,KAAK,UAAU,UAAU;AAAA,EAClC;AAAA,EAEA,KAAK,QAAqB;AAExB,UAAM,KAAK,MAAM;AAIV,WAAA;AAAA,EACT;AAAA,EAEA,OAAO,MAAW;AAGT,WAAA,MAAM,OAAO,IAAI;AAAA,EAC1B;AAAA,EAEA,UAAU;;AAER,SAAK,SAAS;AAEd,eAAK,qBAAL,mBAAuB;AACvB,SAAK,mBAAmB;AAEjB,WAAA;AAAA,EACT;AACF;"}
1
+ {"version":3,"file":"BatchedMesh.js","sources":["../../src/objects/BatchedMesh.ts"],"sourcesContent":["import {\n Matrix4,\n BufferAttribute,\n InterleavedBufferAttribute,\n Mesh,\n BufferGeometry,\n Material,\n DataTexture,\n IUniform,\n MathUtils,\n RGBAFormat,\n FloatType,\n} from 'three'\n\nconst ID_ATTR_NAME = '_batch_id_'\nconst _identityMatrix = new Matrix4()\nconst _zeroScaleMatrix = new Matrix4().set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)\n\n// Custom shaders\nconst batchingParsVertex = /* glsl */ `\n#ifdef BATCHING\n\tattribute float ${ID_ATTR_NAME};\n\tuniform highp sampler2D batchingTexture;\n\tmat4 getBatchingMatrix( const in float i ) {\n\n\t\tint size = textureSize( batchingTexture, 0 ).x;\n\t\tint j = int( i ) * 4;\n\t\tint x = j % size;\n\t\tint y = j / size;\n\t\tvec4 v1 = texelFetch( batchingTexture, ivec2( x, y ), 0 );\n\t\tvec4 v2 = texelFetch( batchingTexture, ivec2( x + 1, y ), 0 );\n\t\tvec4 v3 = texelFetch( batchingTexture, ivec2( x + 2, y ), 0 );\n\t\tvec4 v4 = texelFetch( batchingTexture, ivec2( x + 3, y ), 0 );\n\t\treturn mat4( v1, v2, v3, v4 );\n\n\t}\n#endif\n`\n\nconst batchingbaseVertex = /* glsl */ `\n#ifdef BATCHING\n\tmat4 batchingMatrix = getBatchingMatrix( ${ID_ATTR_NAME} );\n#endif\n`\n\nconst batchingnormalVertex = /* glsl */ `\n#ifdef BATCHING\n\tobjectNormal = vec4( batchingMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( batchingMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif\n`\n\nconst batchingVertex = /* glsl */ `\n#ifdef BATCHING\n\ttransformed = ( batchingMatrix * vec4( transformed, 1.0 ) ).xyz;\n#endif\n`\n\n// @TODO: SkinnedMesh support?\n// @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.\n\n// copies data from attribute \"src\" into \"target\" starting at \"targetOffset\"\nfunction copyAttributeData(\n src: BufferAttribute | InterleavedBufferAttribute,\n target: BufferAttribute | InterleavedBufferAttribute,\n targetOffset = 0,\n): void {\n const itemSize = target.itemSize\n if (\n (src as InterleavedBufferAttribute).isInterleavedBufferAttribute ||\n src.array.constructor !== target.array.constructor\n ) {\n // use the component getters and setters if the array data cannot\n // be copied directly\n const vertexCount = src.count\n for (let i = 0; i < vertexCount; i++) {\n for (let c = 0; c < itemSize; c++) {\n // @ts-ignore\n target.setComponent(i + targetOffset, c, src.getComponent(i, c))\n }\n }\n } else {\n // faster copy approach using typed array set function\n // @ts-ignore\n target.array.set(src.array, targetOffset * itemSize)\n }\n\n target.needsUpdate = true\n}\n\nclass BatchedMesh extends Mesh<BufferGeometry, Material> {\n _vertexStarts: number[]\n _vertexCounts: number[]\n _indexStarts: number[]\n _indexCounts: number[]\n _reservedRanges: { vertexStart: number; vertexCount: number; indexStart: number; indexCount: number }[]\n _visible: boolean[]\n _active: boolean[]\n _maxGeometryCount: number\n _maxVertexCount: number\n _maxIndexCount: number\n _geometryInitialized: boolean\n _geometryCount: number\n _matrices: Matrix4[]\n _matricesTexture: DataTexture | null\n _customUniforms: Record<string, IUniform>\n\n constructor(\n maxGeometryCount: number,\n maxVertexCount: number,\n maxIndexCount = maxVertexCount * 2,\n material?: Material,\n ) {\n super(new BufferGeometry(), material)\n\n this._vertexStarts = []\n this._vertexCounts = []\n this._indexStarts = []\n this._indexCounts = []\n this._reservedRanges = []\n\n this._visible = []\n this._active = []\n\n this._maxGeometryCount = maxGeometryCount\n this._maxVertexCount = maxVertexCount\n this._maxIndexCount = maxIndexCount\n\n this._geometryInitialized = false\n this._geometryCount = 0\n\n // Local matrix per geometry by using data texture\n // @TODO: Support uniform parameter per geometry\n\n this._matrices = []\n this._matricesTexture = null!\n\n // @TODO: Calculate the entire binding box and make frustumCulled true\n this.frustumCulled = false\n\n this._customUniforms = {\n batchingTexture: { value: null },\n }\n\n this._initMatricesTexture()\n this._initShader()\n\n this.onBeforeRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = true\n }\n\n // @TODO: Implement frustum culling for each geometry\n }\n\n this.onAfterRender = function () {\n if (this.material.defines) {\n this.material.defines.BATCHING = false\n }\n }\n }\n\n _initMatricesTexture(): void {\n // layout (1 matrix = 4 pixels)\n // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)\n // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)\n // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)\n // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)\n // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)\n\n let size = Math.sqrt(this._maxGeometryCount * 4) // 4 pixels needed for 1 matrix\n size = MathUtils.ceilPowerOfTwo(size)\n size = Math.max(size, 4)\n\n const matricesArray = new Float32Array(size * size * 4) // 4 floats per RGBA pixel\n const matricesTexture = new DataTexture(matricesArray, size, size, RGBAFormat, FloatType)\n\n this._matricesTexture = matricesTexture\n this._customUniforms.batchingTexture.value = this._matricesTexture\n }\n\n _initShader(): void {\n const material = this.material\n const currentOnBeforeCompile = material.onBeforeCompile\n const customUniforms = this._customUniforms\n\n material.onBeforeCompile = function onBeforeCompile(parameters, renderer) {\n // Is this replacement stable across any materials?\n parameters.vertexShader = parameters.vertexShader\n .replace('#include <skinning_pars_vertex>', '#include <skinning_pars_vertex>\\n' + batchingParsVertex)\n .replace('#include <uv_vertex>', '#include <uv_vertex>\\n' + batchingbaseVertex)\n .replace('#include <skinnormal_vertex>', '#include <skinnormal_vertex>\\n' + batchingnormalVertex)\n .replace('#include <skinning_vertex>', '#include <skinning_vertex>\\n' + batchingVertex)\n\n for (const uniformName in customUniforms) {\n parameters.uniforms[uniformName] = customUniforms[uniformName]\n }\n\n currentOnBeforeCompile.call(this, parameters, renderer)\n }\n\n material.defines = material.defines || {}\n material.defines.BATCHING = false\n }\n\n _initializeGeometry(reference: BufferGeometry): void {\n // @TODO: geometry.groups support?\n // @TODO: geometry.drawRange support?\n // @TODO: geometry.morphAttributes support?\n\n const geometry = this.geometry\n const maxVertexCount = this._maxVertexCount\n const maxGeometryCount = this._maxGeometryCount\n const maxIndexCount = this._maxIndexCount\n if (this._geometryInitialized === false) {\n for (const attributeName in reference.attributes) {\n const srcAttribute = reference.getAttribute(attributeName)\n const { array, itemSize, normalized } = srcAttribute\n\n const dstArray = new (array.constructor as Float32ArrayConstructor)(maxVertexCount * itemSize)\n const dstAttribute = new (srcAttribute.constructor as any)(dstArray, itemSize, normalized)\n\n // TODO: add usage in @types/three\n // @ts-ignore\n dstAttribute.setUsage(srcAttribute.usage)\n\n geometry.setAttribute(attributeName, dstAttribute)\n }\n\n if (reference.getIndex() !== null) {\n const indexArray = maxVertexCount > 65536 ? new Uint32Array(maxIndexCount) : new Uint16Array(maxIndexCount)\n\n geometry.setIndex(new BufferAttribute(indexArray, 1))\n }\n\n const idArray = maxGeometryCount > 65536 ? new Uint32Array(maxVertexCount) : new Uint16Array(maxVertexCount)\n geometry.setAttribute(ID_ATTR_NAME, new BufferAttribute(idArray, 1))\n\n this._geometryInitialized = true\n }\n }\n\n // Make sure the geometry is compatible with the existing combined geometry atributes\n _validateGeometry(geometry: BufferGeometry): void {\n // check that the geometry doesn't have a version of our reserved id attribute\n if (geometry.getAttribute(ID_ATTR_NAME)) {\n throw new Error(`BatchedMesh: Geometry cannot use attribute \"${ID_ATTR_NAME}\"`)\n }\n\n // check to ensure the geometries are using consistent attributes and indices\n const batchGeometry = this.geometry\n if (Boolean(geometry.getIndex()) !== Boolean(batchGeometry.getIndex())) {\n throw new Error('BatchedMesh: All geometries must consistently have \"index\".')\n }\n\n for (const attributeName in batchGeometry.attributes) {\n if (attributeName === ID_ATTR_NAME) {\n continue\n }\n\n if (!geometry.hasAttribute(attributeName)) {\n throw new Error(\n `BatchedMesh: Added geometry missing \"${attributeName}\". All geometries must have consistent attributes.`,\n )\n }\n\n const srcAttribute = geometry.getAttribute(attributeName)\n const dstAttribute = batchGeometry.getAttribute(attributeName)\n if (srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized) {\n throw new Error('BatchedMesh: All attributes must have a consistent itemSize and normalized value.')\n }\n }\n }\n\n getGeometryCount(): number {\n return this._geometryCount\n }\n\n getVertexCount(): number {\n const reservedRanges = this._reservedRanges\n if (reservedRanges.length === 0) {\n return 0\n } else {\n const finalRange = reservedRanges[reservedRanges.length - 1]\n return finalRange.vertexStart + finalRange.vertexCount\n }\n }\n\n getIndexCount(): number {\n const reservedRanges = this._reservedRanges\n const geometry = this.geometry\n if (geometry.getIndex() === null || reservedRanges.length === 0) {\n return 0\n } else {\n const finalRange = reservedRanges[reservedRanges.length - 1]\n return finalRange.indexStart + finalRange.indexCount\n }\n }\n\n addGeometry(geometry: BufferGeometry, vertexCount = -1, indexCount = -1): number {\n this._initializeGeometry(geometry)\n\n this._validateGeometry(geometry)\n\n // ensure we're not over geometry\n if (this._geometryCount >= this._maxGeometryCount) {\n throw new Error('BatchedMesh: Maximum geometry count reached.')\n }\n\n // get the necessary range fo the geometry\n const range = {\n vertexStart: -1,\n vertexCount: -1,\n indexStart: -1,\n indexCount: -1,\n }\n\n let lastRange = null\n const reservedRanges = this._reservedRanges\n if (this._geometryCount !== 0) {\n lastRange = reservedRanges[reservedRanges.length - 1]\n }\n\n if (vertexCount === -1) {\n range.vertexCount = geometry.getAttribute('position').count\n } else {\n range.vertexCount = vertexCount\n }\n\n if (lastRange === null) {\n range.vertexStart = 0\n } else {\n range.vertexStart = lastRange.vertexStart + lastRange.vertexCount\n }\n\n if (geometry.getIndex() !== null) {\n if (indexCount === -1) {\n range.indexCount = geometry.getIndex()!.count\n } else {\n range.indexCount = indexCount\n }\n\n if (lastRange === null) {\n range.indexStart = 0\n } else {\n range.indexStart = lastRange.indexStart + lastRange.indexCount\n }\n }\n\n if (\n (range.indexStart !== -1 && range.indexStart + range.indexCount > this._maxIndexCount) ||\n range.vertexStart + range.vertexCount > this._maxVertexCount\n ) {\n throw new Error('BatchedMesh: Reserved space request exceeds the maximum buffer size.')\n }\n\n const indexCounts = this._indexCounts\n const indexStarts = this._indexStarts\n const vertexCounts = this._vertexCounts\n const vertexStarts = this._vertexStarts\n\n const visible = this._visible\n const active = this._active\n const matricesTexture = this._matricesTexture\n const matrices = this._matrices\n const matricesArray = this._matricesTexture!.image.data\n\n // push new visibility states\n visible.push(true)\n active.push(true)\n\n // update id\n const geometryId = this._geometryCount\n this._geometryCount++\n\n // initialize matrix information\n matrices.push(new Matrix4())\n _identityMatrix.toArray(matricesArray, geometryId * 16)\n matricesTexture!.needsUpdate = true\n\n // add the reserved range\n reservedRanges.push(range)\n\n // push new geometry data range\n vertexStarts.push(range.vertexStart)\n vertexCounts.push(range.vertexCount)\n\n if (geometry.getIndex() !== null) {\n // push new index range\n indexStarts.push(range.indexCount)\n indexCounts.push(range.indexCount)\n }\n\n // set the id for the geometry\n const idAttribute = this.geometry.getAttribute(ID_ATTR_NAME)\n for (let i = 0; i < range.vertexCount; i++) {\n idAttribute.setX(range.vertexStart + i, geometryId)\n }\n\n idAttribute.needsUpdate = true\n\n // update the geometry\n this.setGeometryAt(geometryId, geometry)\n\n return geometryId\n }\n\n /**\n * @deprecated use `addGeometry` instead.\n */\n applyGeometry(geometry: BufferGeometry): number {\n return this.addGeometry(geometry)\n }\n\n setGeometryAt(id: number, geometry: BufferGeometry): number {\n if (id >= this._geometryCount) {\n throw new Error('BatchedMesh: Maximum geometry count reached.')\n }\n\n this._validateGeometry(geometry)\n\n const range = this._reservedRanges[id]\n if (\n (geometry.getIndex() !== null && geometry.getIndex()!.count > range.indexCount) ||\n geometry.attributes.position.count > range.vertexCount\n ) {\n throw new Error('BatchedMesh: Reserved space not large enough for provided geometry.')\n }\n\n // copy geometry over\n const batchGeometry = this.geometry\n const srcPositionAttribute = geometry.getAttribute('position')\n const hasIndex = batchGeometry.getIndex() !== null\n const dstIndex = batchGeometry.getIndex()!\n const srcIndex = geometry.getIndex()!\n\n // copy attribute data over\n const vertexStart = range.vertexStart\n const vertexCount = range.vertexCount\n for (const attributeName in batchGeometry.attributes) {\n if (attributeName === ID_ATTR_NAME) {\n continue\n }\n\n const srcAttribute = geometry.getAttribute(attributeName)\n const dstAttribute = batchGeometry.getAttribute(attributeName)\n copyAttributeData(srcAttribute, dstAttribute, vertexStart)\n\n // fill the rest in with zeroes\n const itemSize = srcAttribute.itemSize\n for (let i = srcAttribute.count, l = vertexCount; i < l; i++) {\n const index = vertexStart + i\n for (let c = 0; c < itemSize; c++) {\n // @ts-ignore\n dstAttribute.setComponent(index, c, 0)\n }\n }\n\n dstAttribute.needsUpdate = true\n }\n\n this._vertexCounts[id] = srcPositionAttribute.count\n\n if (hasIndex) {\n // fill the rest in with zeroes\n const indexStart = range.indexStart\n\n // copy index data over\n for (let i = 0; i < srcIndex.count; i++) {\n dstIndex.setX(indexStart + i, vertexStart + srcIndex.getX(i))\n }\n\n // fill the rest in with zeroes\n for (let i = srcIndex.count, l = range.indexCount; i < l; i++) {\n dstIndex.setX(indexStart + i, vertexStart)\n }\n\n dstIndex.needsUpdate = true\n this._indexCounts[id] = srcIndex.count\n }\n\n return id\n }\n\n deleteGeometry(geometryId: number): this {\n // Note: User needs to call optimize() afterward to pack the data.\n\n const active = this._active\n const matricesTexture = this._matricesTexture!\n const matricesArray = matricesTexture.image.data\n if (geometryId >= active.length || active[geometryId] === false) {\n return this\n }\n\n active[geometryId] = false\n _zeroScaleMatrix.toArray(matricesArray, geometryId * 16)\n matricesTexture!.needsUpdate = true\n\n return this\n }\n\n optimize(): never {\n throw new Error('BatchedMesh: Optimize function not implemented.')\n }\n\n setMatrixAt(geometryId: number, matrix: Matrix4): this {\n // @TODO: Map geometryId to index of the arrays because\n // optimize() can make geometryId mismatch the index\n\n const visible = this._visible\n const active = this._active\n const matricesTexture = this._matricesTexture!\n const matrices = this._matrices\n const matricesArray = matricesTexture.image.data\n if (geometryId >= matrices.length || active[geometryId] === false) {\n return this\n }\n\n if (visible[geometryId] === true) {\n matrix.toArray(matricesArray, geometryId * 16)\n matricesTexture.needsUpdate = true\n }\n\n matrices[geometryId].copy(matrix)\n\n return this\n }\n\n getMatrixAt(geometryId: number, matrix: Matrix4): Matrix4 {\n const matrices = this._matrices\n const active = this._active\n if (geometryId >= matrices.length || active[geometryId] === false) {\n return matrix\n }\n\n return matrix.copy(matrices[geometryId])\n }\n\n setVisibleAt(geometryId: number, value: boolean): this {\n const visible = this._visible\n const active = this._active\n const matricesTexture = this._matricesTexture!\n const matrices = this._matrices\n const matricesArray = matricesTexture.image.data\n\n // if the geometry is out of range, not active, or visibility state\n // does not change then return early\n if (geometryId >= visible.length || active[geometryId] === false || visible[geometryId] === value) {\n return this\n }\n\n // scale the matrix to zero if it's hidden\n if (value === true) {\n matrices[geometryId].toArray(matricesArray, geometryId * 16)\n } else {\n _zeroScaleMatrix.toArray(matricesArray, geometryId * 16)\n }\n\n matricesTexture.needsUpdate = true\n visible[geometryId] = value\n\n return this\n }\n\n getVisibleAt(geometryId: number): boolean {\n const visible = this._visible\n const active = this._active\n\n // return early if the geometry is out of range or not active\n if (geometryId >= visible.length || active[geometryId] === false) {\n return false\n }\n\n return visible[geometryId]\n }\n\n raycast(): void {\n console.warn('BatchedMesh: Raycast function not implemented.')\n }\n\n copy(): never {\n // super.copy( source );\n\n throw new Error('BatchedMesh: Copy function not implemented.')\n }\n\n toJSON(): never {\n throw new Error('BatchedMesh: toJSON function not implemented.')\n }\n\n dispose(): this {\n // Assuming the geometry is not shared with other meshes\n this.geometry.dispose()\n\n this._matricesTexture!.dispose()\n this._matricesTexture = null!\n\n return this\n }\n}\n\nexport { BatchedMesh }\n"],"names":[],"mappings":";;;;;;;AAcA,MAAM,eAAe;AACrB,MAAM,kBAAkB,IAAI;AAC5B,MAAM,mBAAmB,IAAI,QAAQ,EAAE,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAGzF,MAAM;AAAA;AAAA,EAAgC;AAAA;AAAA,mBAEnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBnB,MAAM;AAAA;AAAA,EAAgC;AAAA;AAAA,4CAEM;AAAA;AAAA;AAAA;AAI5C,MAAM;AAAA;AAAA,EAAkC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASxC,MAAM;AAAA;AAAA,EAA4B;AAAA;AAAA;AAAA;AAAA;AAAA;AAUlC,SAAS,kBACP,KACA,QACA,eAAe,GACT;AACN,QAAM,WAAW,OAAO;AACxB,MACG,IAAmC,gCACpC,IAAI,MAAM,gBAAgB,OAAO,MAAM,aACvC;AAGA,UAAM,cAAc,IAAI;AACxB,aAAS,IAAI,GAAG,IAAI,aAAa,KAAK;AACpC,eAAS,IAAI,GAAG,IAAI,UAAU,KAAK;AAE1B,eAAA,aAAa,IAAI,cAAc,GAAG,IAAI,aAAa,GAAG,CAAC,CAAC;A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Ef,WAAA;AAAA,EACT;AAAA,EAEA,aAAa,YAA6B;AACxC,UAAM,UAAU,KAAK;AACrB,UAAM,SAAS,KAAK;AAGpB,QAAI,cAAc,QAAQ,UAAU,OAAO,UAAU,MAAM,OAAO;AACzD,aAAA;AAAA,IACT;AAEA,WAAO,QAAQ,UAAU;AAAA,EAC3B;AAAA,EAEA,UAAgB;AACd,YAAQ,KAAK,gDAAgD;AAAA,EAC/D;AAAA,EAEA,OAAc;AAGN,UAAA,IAAI,MAAM,6CAA6C;AAAA,EAC/D;AAAA,EAEA,SAAgB;AACR,UAAA,IAAI,MAAM,+CAA+C;AAAA,EACjE;AAAA,EAEA,UAAgB;AAEd,SAAK,SAAS;AAEd,SAAK,iBAAkB;AACvB,SAAK,mBAAmB;AAEjB,WAAA;AAAA,EACT;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-stdlib",
3
- "version": "2.28.2",
3
+ "version": "2.28.4",
4
4
  "description": "stand-alone library of threejs examples",
5
5
  "keywords": [
6
6
  "three",