@@ -16,21 +16,87 @@ const _identity = /*@__PURE__*/ new Matrix4();
1616const _mesh = /*@__PURE__ */ new Mesh ( ) ;
1717const _sphere = /*@__PURE__ */ new Sphere ( ) ;
1818
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+ */
1928class InstancedMesh extends Mesh {
2029
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+ */
2137 constructor ( geometry , material , count ) {
2238
2339 super ( geometry , material ) ;
2440
41+ /**
42+ * This flag can be used for type testing.
43+ *
44+ * @type {boolean }
45+ * @readonly
46+ * @default true
47+ */
2548 this . isInstancedMesh = true ;
2649
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+ */
2757 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+ */
2867 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+ */
2977 this . morphTexture = null ;
3078
79+ /**
80+ * The number of instances.
81+ *
82+ * @type {number }
83+ */
3184 this . count = count ;
3285
86+ /**
87+ * The bounding box of the instanced mesh. Can be computed via {@link InstancedMesh#computeBoundingBox}.
88+ *
89+ * @type {?Box3 }
90+ * @default null
91+ */
3392 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+ */
34100 this . boundingSphere = null ;
35101
36102 for ( let i = 0 ; i < count ; i ++ ) {
@@ -41,6 +107,11 @@ class InstancedMesh extends Mesh {
41107
42108 }
43109
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+ */
44115 computeBoundingBox ( ) {
45116
46117 const geometry = this . geometry ;
@@ -72,6 +143,11 @@ class InstancedMesh extends Mesh {
72143
73144 }
74145
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+ */
75151 computeBoundingSphere ( ) {
76152
77153 const geometry = this . geometry ;
@@ -121,18 +197,36 @@ class InstancedMesh extends Mesh {
121197
122198 }
123199
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+ */
124206 getColorAt ( index , color ) {
125207
126208 color . fromArray ( this . instanceColor . array , index * 3 ) ;
127209
128210 }
129211
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+ */
130218 getMatrixAt ( index , matrix ) {
131219
132220 matrix . fromArray ( this . instanceMatrix . array , index * 16 ) ;
133221
134222 }
135223
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+ */
136230 getMorphAt ( index , object ) {
137231
138232 const objectInfluences = object . morphTargetInfluences ;
@@ -203,6 +297,13 @@ class InstancedMesh extends Mesh {
203297
204298 }
205299
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+ */
206307 setColorAt ( index , color ) {
207308
208309 if ( this . instanceColor === null ) {
@@ -215,12 +316,27 @@ class InstancedMesh extends Mesh {
215316
216317 }
217318
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+ */
218326 setMatrixAt ( index , matrix ) {
219327
220328 matrix . toArray ( this . instanceMatrix . array , index * 16 ) ;
221329
222330 }
223331
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+ */
224340 setMorphAt ( index , object ) {
225341
226342 const objectInfluences = object . morphTargetInfluences ;
@@ -257,6 +373,10 @@ class InstancedMesh extends Mesh {
257373
258374 }
259375
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+ */
260380 dispose ( ) {
261381
262382 this . dispatchEvent ( { type : 'dispose' } ) ;
@@ -268,8 +388,6 @@ class InstancedMesh extends Mesh {
268388
269389 }
270390
271- return this ;
272-
273391 }
274392
275393}
0 commit comments