UNPKG

8.62 kBTypeScriptView Raw
1import { BufferAttributeJSON } from "./../core/BufferAttribute.js";
2import { BufferGeometry } from "../core/BufferGeometry.js";
3import { InstancedBufferAttribute } from "../core/InstancedBufferAttribute.js";
4import { JSONMeta, Object3DEventMap } from "../core/Object3D.js";
5import { Material } from "../materials/Material.js";
6import { Box3 } from "../math/Box3.js";
7import { Color } from "../math/Color.js";
8import { Matrix4 } from "../math/Matrix4.js";
9import { Sphere } from "../math/Sphere.js";
10import { DataTexture } from "../textures/DataTexture.js";
11import { Mesh, MeshJSONObject } from "./Mesh.js";
12
13export interface InstancedMeshJSONObject extends MeshJSONObject {
14 count: number;
15 instanceMatrix: BufferAttributeJSON;
16 instanceColor?: BufferAttributeJSON;
17}
18
19export interface InstancedMeshJSON extends MeshJSONObject {
20 object: InstancedMeshJSONObject;
21}
22
23export interface InstancedMeshEventMap extends Object3DEventMap {
24 dispose: {};
25}
26
27/**
28 * A special version of {@link THREE.Mesh | Mesh} with instanced rendering support
29 * @remarks
30 * Use {@link InstancedMesh} if you have to render a large number of objects with the same geometry and material(s) but with different world transformations
31 * @remarks
32 * The usage of {@link InstancedMesh} will help you to reduce the number of draw calls and thus improve the overall rendering performance in your application.
33 * @see Example: {@link https://threejs.org/examples/#webgl_instancing_dynamic | WebGL / instancing / dynamic}
34 * @see Example: {@link https://threejs.org/examples/#webgl_instancing_performance | WebGL / instancing / performance}
35 * @see Example: {@link https://threejs.org/examples/#webgl_instancing_scatter | WebGL / instancing / scatter}
36 * @see Example: {@link https://threejs.org/examples/#webgl_instancing_raycast | WebGL / instancing / raycast}
37 * @see {@link https://threejs.org/docs/index.html#api/en/objects/InstancedMesh | Official Documentation}
38 * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/InstancedMesh.js | Source}
39 */
40export class InstancedMesh<
41 TGeometry extends BufferGeometry = BufferGeometry,
42 TMaterial extends Material | Material[] = Material | Material[],
43 TEventMap extends InstancedMeshEventMap = InstancedMeshEventMap,
44> extends Mesh<TGeometry, TMaterial, TEventMap> {
45 /**
46 * Create a new instance of {@link InstancedMesh}
47 * @param geometry An instance of {@link BufferGeometry}.
48 * @param material A single or an array of {@link Material}. Default is a new {@link MeshBasicMaterial}.
49 * @param count The **maximum** number of instances of this Mesh. Expects a `Integer`
50 */
51 constructor(geometry: TGeometry | undefined, material: TMaterial | undefined, count: number);
52
53 /**
54 * Read-only flag to check if a given object is of type {@link InstancedMesh}.
55 * @remarks This is a _constant_ value
56 * @defaultValue `true`
57 */
58 readonly isInstancedMesh: true;
59
60 /**
61 * This bounding box encloses all instances of the {@link InstancedMesh},, which can be calculated with {@link computeBoundingBox | .computeBoundingBox()}.
62 * @remarks Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`.
63 * @defaultValue `null`
64 */
65 boundingBox: Box3 | null;
66
67 /**
68 * This bounding sphere encloses all instances of the {@link InstancedMesh}, which can be calculated with {@link computeBoundingSphere | .computeBoundingSphere()}.
69 * @remarks bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`.
70 * @defaultValue `null`
71 */
72 boundingSphere: Sphere | null;
73
74 /**
75 * The number of instances.
76 * @remarks
77 * The `count` value passed into the {@link InstancedMesh | constructor} represents the **maximum** number of instances of this mesh.
78 * You can change the number of instances at runtime to an integer value in the range `[0, count]`.
79 * @remarks If you need more instances than the original `count` value, you have to create a new InstancedMesh.
80 * @remarks Expects a `Integer`
81 */
82 count: number;
83
84 /**
85 * Represents the colors of all instances.
86 * You have to set {@link InstancedBufferAttribute.needsUpdate | .instanceColor.needsUpdate()} flag to `true` if you modify instanced data via {@link setColorAt | .setColorAt()}.
87 * @defaultValue `null`
88 */
89 instanceColor: InstancedBufferAttribute | null;
90
91 /**
92 * Represents the local transformation of all instances.
93 * You have to set {@link InstancedBufferAttribute.needsUpdate | .instanceMatrix.needsUpdate()} flag to `true` if you modify instanced data via {@link setMatrixAt | .setMatrixAt()}.
94 */
95 instanceMatrix: InstancedBufferAttribute;
96
97 /**
98 * Represents the morph target weights of all instances. You have to set its {@link .needsUpdate} flag to true if
99 * you modify instanced data via {@link .setMorphAt}.
100 */
101 morphTexture: DataTexture | null;
102
103 /**
104 * Computes the bounding box of the instanced mesh, and updates the {@link .boundingBox} attribute. The bounding box
105 * is not computed by the engine; it must be computed by your app. You may need to recompute the bounding box if an
106 * instance is transformed via {@link .setMatrixAt()}.
107 */
108 computeBoundingBox(): void;
109
110 /**
111 * Computes the bounding sphere of the instanced mesh, and updates the {@link .boundingSphere} attribute. The engine
112 * automatically computes the bounding sphere when it is needed, e.g., for ray casting or view frustum culling. You
113 * may need to recompute the bounding sphere if an instance is transformed via [page:.setMatrixAt]().
114 */
115 computeBoundingSphere(): void;
116
117 /**
118 * Get the color of the defined instance.
119 * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer`
120 * @param color This color object will be set to the color of the defined instance.
121 */
122 getColorAt(index: number, color: Color): void;
123
124 /**
125 * Sets the given color to the defined instance
126 * @remarks
127 * Make sure you set {@link InstancedBufferAttribute.needsUpdate | .instanceColor.needsUpdate()} to `true` after updating all the colors.
128 * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer`
129 * @param color The color of a single instance.
130 */
131 setColorAt(index: number, color: Color): void;
132
133 /**
134 * Get the local transformation matrix of the defined instance.
135 * @param index The index of an instance Values have to be in the range `[0, count]`. Expects a `Integer`
136 * @param matrix This 4x4 matrix will be set to the local transformation matrix of the defined instance.
137 */
138 getMatrixAt(index: number, matrix: Matrix4): void;
139
140 /**
141 * Get the morph target weights of the defined instance.
142 * @param index The index of an instance. Values have to be in the range [0, count].
143 * @param mesh The {@link .morphTargetInfluences} property of this mesh will be filled with the morph target weights of the defined instance.
144 */
145 getMorphAt(index: number, mesh: Mesh): void;
146
147 /**
148 * Sets the given local transformation matrix to the defined instance.
149 * @remarks
150 * Make sure you set {@link InstancedBufferAttribute.needsUpdate | .instanceMatrix.needsUpdate()} flag to `true` after updating all the matrices.
151 * @param index The index of an instance. Values have to be in the range `[0, count]`. Expects a `Integer`
152 * @param matrix A 4x4 matrix representing the local transformation of a single instance.
153 */
154 setMatrixAt(index: number, matrix: Matrix4): void;
155
156 /**
157 * Sets the morph target weights to the defined instance. Make sure you set {@link .morphTexture}{@link .needsUpdate}
158 * to true after updating all the influences.
159 * @param index The index of an instance. Values have to be in the range [0, count].
160 * @param mesh A mesh with {@link .morphTargetInfluences} property containing the morph target weights of a single instance.
161 */
162 setMorphAt(index: number, mesh: Mesh): void;
163
164 /**
165 * No effect in {@link InstancedMesh}.
166 * @ignore
167 * @hidden
168 */
169 override updateMorphTargets(): void;
170
171 /**
172 * Frees the GPU-related resources allocated by this instance
173 * @remarks
174 * Call this method whenever this instance is no longer used in your app.
175 */
176 dispose(): this;
177
178 toJSON(meta?: JSONMeta): InstancedMeshJSON;
179}