@@ -16,21 +16,87 @@ const _identity = /*@__PURE__*/ new Matrix4();
16
16
const _mesh = /*@__PURE__ */ new Mesh ( ) ;
17
17
const _sphere = /*@__PURE__ */ new Sphere ( ) ;
18
18
19
+ /**
20
+ * A special version of a mesh with instanced rendering support. Use
21
+ * this class if you have to render a large number of objects with the same
22
+ * geometry and material(s) but with different world transformations. The usage
23
+ * of 'InstancedMesh' will help you to reduce the number of draw calls and thus
24
+ * improve the overall rendering performance in your application.
25
+ *
26
+ * @augments Mesh
27
+ */
19
28
class InstancedMesh extends Mesh {
20
29
30
+ /**
31
+ * Constructs a new instanced mesh.
32
+ *
33
+ * @param {BufferGeometry } [geometry] - The mesh geometry.
34
+ * @param {Material|Array<Material> } [material] - The mesh material.
35
+ * @param {number } count - The number of instances.
36
+ */
21
37
constructor ( geometry , material , count ) {
22
38
23
39
super ( geometry , material ) ;
24
40
41
+ /**
42
+ * This flag can be used for type testing.
43
+ *
44
+ * @type {boolean }
45
+ * @readonly
46
+ * @default true
47
+ */
25
48
this . isInstancedMesh = true ;
26
49
50
+ /**
51
+ * Represents the local transformation of all instances. You have to set its
52
+ * {@link BufferAttribute#needsUpdate} flag to true if you modify instanced data
53
+ * via {@link InstancedMesh#setMatrixAt}.
54
+ *
55
+ * @type {InstancedBufferAttribute }
56
+ */
27
57
this . instanceMatrix = new InstancedBufferAttribute ( new Float32Array ( count * 16 ) , 16 ) ;
58
+
59
+ /**
60
+ * Represents the color of all instances. You have to set its
61
+ * {@link BufferAttribute#needsUpdate} flag to true if you modify instanced data
62
+ * via {@link InstancedMesh#setColorAt}.
63
+ *
64
+ * @type {?InstancedBufferAttribute }
65
+ * @default null
66
+ */
28
67
this . instanceColor = null ;
68
+
69
+ /**
70
+ * Represents the morph target weights of all instances. You have to set its
71
+ * {@link Texture#needsUpdate} flag to true if you modify instanced data
72
+ * via {@link InstancedMesh#setMorphAt}.
73
+ *
74
+ * @type {?InstancedBufferAttribute }
75
+ * @default null
76
+ */
29
77
this . morphTexture = null ;
30
78
79
+ /**
80
+ * The number of instances.
81
+ *
82
+ * @type {number }
83
+ */
31
84
this . count = count ;
32
85
86
+ /**
87
+ * The bounding box of the instanced mesh. Can be computed via {@link InstancedMesh#computeBoundingBox}.
88
+ *
89
+ * @type {?Box3 }
90
+ * @default null
91
+ */
33
92
this . boundingBox = null ;
93
+
94
+ /**
95
+ * The bounding sphere of the instanced mesh. Can be computed via {@link InstancedMesh#computeBoundingSphere}.
96
+ *
97
+ * @type {?Sphere }
98
+ * @default null
99
+ */
34
100
this . boundingSphere = null ;
35
101
36
102
for ( let i = 0 ; i < count ; i ++ ) {
@@ -41,6 +107,11 @@ class InstancedMesh extends Mesh {
41
107
42
108
}
43
109
110
+ /**
111
+ * Computes the bounding box of the instanced mesh, and updates {@link InstancedMesh#boundingBox}.
112
+ * The bounding box is not automatically computed by the engine; this method must be called by your app.
113
+ * You may need to recompute the bounding box if an instance is transformed via {@link InstancedMesh#setMatrixAt}.
114
+ */
44
115
computeBoundingBox ( ) {
45
116
46
117
const geometry = this . geometry ;
@@ -72,6 +143,11 @@ class InstancedMesh extends Mesh {
72
143
73
144
}
74
145
146
+ /**
147
+ * Computes the bounding sphere of the instanced mesh, and updates {@link InstancedMesh#boundingSphere}
148
+ * The engine automatically computes the bounding sphere when it is needed, e.g., for ray casting or view frustum culling.
149
+ * You may need to recompute the bounding sphere if an instance is transformed via {@link InstancedMesh#setMatrixAt}.
150
+ */
75
151
computeBoundingSphere ( ) {
76
152
77
153
const geometry = this . geometry ;
@@ -121,18 +197,36 @@ class InstancedMesh extends Mesh {
121
197
122
198
}
123
199
200
+ /**
201
+ * Gets the color of the defined instance.
202
+ *
203
+ * @param {number } index - The instance index.
204
+ * @param {Color } color - The target object that is used to store the method's result.
205
+ */
124
206
getColorAt ( index , color ) {
125
207
126
208
color . fromArray ( this . instanceColor . array , index * 3 ) ;
127
209
128
210
}
129
211
212
+ /**
213
+ * Gets the local transformation matrix of the defined instance.
214
+ *
215
+ * @param {number } index - The instance index.
216
+ * @param {Matrix4 } matrix - The target object that is used to store the method's result.
217
+ */
130
218
getMatrixAt ( index , matrix ) {
131
219
132
220
matrix . fromArray ( this . instanceMatrix . array , index * 16 ) ;
133
221
134
222
}
135
223
224
+ /**
225
+ * Gets the morph target weights of the defined instance.
226
+ *
227
+ * @param {number } index - The instance index.
228
+ * @param {Mesh } object - The target object that is used to store the method's result.
229
+ */
136
230
getMorphAt ( index , object ) {
137
231
138
232
const objectInfluences = object . morphTargetInfluences ;
@@ -203,6 +297,13 @@ class InstancedMesh extends Mesh {
203
297
204
298
}
205
299
300
+ /**
301
+ * Sets the given color to the defined instance. Make sure you set the `needsUpdate` flag of
302
+ * {@link InstancedMesh#instanceColor} to `true` after updating all the colors.
303
+ *
304
+ * @param {number } index - The instance index.
305
+ * @param {Color } color - The instance color.
306
+ */
206
307
setColorAt ( index , color ) {
207
308
208
309
if ( this . instanceColor === null ) {
@@ -215,12 +316,27 @@ class InstancedMesh extends Mesh {
215
316
216
317
}
217
318
319
+ /**
320
+ * Sets the given local transformation matrix to the defined instance. Make sure you set the `needsUpdate` flag of
321
+ * {@link InstancedMesh#instanceMatrix} to `true` after updating all the colors.
322
+ *
323
+ * @param {number } index - The instance index.
324
+ * @param {Matrix4 } matrix - The the local transformation.
325
+ */
218
326
setMatrixAt ( index , matrix ) {
219
327
220
328
matrix . toArray ( this . instanceMatrix . array , index * 16 ) ;
221
329
222
330
}
223
331
332
+ /**
333
+ * Sets the morph target weights to the defined instance. Make sure you set the `needsUpdate` flag of
334
+ * {@link InstancedMesh#morphTexture} to `true` after updating all the influences.
335
+ *
336
+ * @param {number } index - The instance index.
337
+ * @param {Mesh } object - A mesh which `morphTargetInfluences` property containing the morph target weights
338
+ * of a single instance.
339
+ */
224
340
setMorphAt ( index , object ) {
225
341
226
342
const objectInfluences = object . morphTargetInfluences ;
@@ -257,6 +373,10 @@ class InstancedMesh extends Mesh {
257
373
258
374
}
259
375
376
+ /**
377
+ * Frees the GPU-related resources allocated by this instance. Call this
378
+ * method whenever this instance is no longer used in your app.
379
+ */
260
380
dispose ( ) {
261
381
262
382
this . dispatchEvent ( { type : 'dispose' } ) ;
@@ -268,8 +388,6 @@ class InstancedMesh extends Mesh {
268
388
269
389
}
270
390
271
- return this ;
272
-
273
391
}
274
392
275
393
}
0 commit comments