UNPKG

11.9 kBTypeScriptView Raw
1import { Camera } from "../cameras/Camera.js";
2import { BufferGeometry } from "../core/BufferGeometry.js";
3import { Material } from "../materials/Material.js";
4import { Box3 } from "../math/Box3.js";
5import { Color } from "../math/Color.js";
6import { Matrix4 } from "../math/Matrix4.js";
7import { Sphere } from "../math/Sphere.js";
8import { Mesh } from "./Mesh.js";
9
10export interface BatchedMeshGeometryRange {
11 vertexStart: number;
12 vertexCount: number;
13 reservedVertexCount: number;
14 indexStart: number;
15 indexCount: number;
16 reservedIndexCount: number;
17 start: number;
18 count: number;
19}
20
21/**
22 * A special version of {@link Mesh} with multi draw batch rendering support. Use BatchedMesh if you have to render a
23 * large number of objects with the same material but with different geometries or world transformations. The usage of
24 * BatchedMesh will help you to reduce the number of draw calls and thus improve the overall rendering performance in
25 * your application.
26 *
27 * If the {@link https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_multi_draw WEBGL_multi_draw extension} is not
28 * supported then a less performant fallback is used.
29 *
30 * @example
31 * const box = new THREE.BoxGeometry( 1, 1, 1 );
32 * const sphere = new THREE.SphereGeometry( 1, 12, 12 );
33 * const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
34 *
35 * // initialize and add geometries into the batched mesh
36 * const batchedMesh = new BatchedMesh( 10, 5000, 10000, material );
37 * const boxGeometryId = batchedMesh.addGeometry( box );
38 * const sphereGeometryId = batchedMesh.addGeometry( sphere );
39 *
40 * // create instances of those geometries
41 * const boxInstancedId1 = batchedMesh.addInstance( boxGeometryId );
42 * const boxInstancedId2 = batchedMesh.addInstance( boxGeometryId );
43 *
44 * const sphereInstancedId1 = batchedMesh.addInstance( sphereGeometryId );
45 * const sphereInstancedId2 = batchedMesh.addInstance( sphereGeometryId );
46 *
47 * // position the geometries
48 * batchedMesh.setMatrixAt( boxInstancedId1, boxMatrix1 );
49 * batchedMesh.setMatrixAt( boxInstancedId2, boxMatrix2 );
50 *
51 * batchedMesh.setMatrixAt( sphereInstancedId1, sphereMatrix1 );
52 * batchedMesh.setMatrixAt( sphereInstancedId2, sphereMatrix2 );
53 *
54 * scene.add( batchedMesh );
55 *
56 * @also Example: {@link https://threejs.org/examples/#webgl_mesh_batch WebGL / mesh / batch}
57 */
58declare class BatchedMesh extends Mesh<BufferGeometry, Material> {
59 /**
60 * This bounding box encloses all instances of the {@link BatchedMesh}. Can be calculated with
61 * {@link .computeBoundingBox()}.
62 * @default null
63 */
64 boundingBox: Box3 | null;
65
66 /**
67 * This bounding sphere encloses all instances of the {@link BatchedMesh}. Can be calculated with
68 * {@link .computeBoundingSphere()}.
69 * @default null
70 */
71 boundingSphere: Sphere | null;
72
73 customSort: ((this: this, list: Array<{ start: number; count: number; z: number }>, camera: Camera) => void) | null;
74
75 /**
76 * If true then the individual objects within the {@link BatchedMesh} are frustum culled.
77 * @default true
78 */
79 perObjectFrustumCulled: boolean;
80
81 /**
82 * If true then the individual objects within the {@link BatchedMesh} are sorted to improve overdraw-related
83 * artifacts. If the material is marked as "transparent" objects are rendered back to front and if not then they are
84 * rendered front to back.
85 * @default true
86 */
87 sortObjects: boolean;
88
89 /**
90 * The maximum number of individual geometries that can be stored in the {@link BatchedMesh}. Read only.
91 */
92 get maxInstanceCount(): number;
93
94 get instanceCount(): number;
95
96 get unusedVertexCount(): number;
97
98 get unusedIndexCount(): number;
99
100 /**
101 * Read-only flag to check if a given object is of type {@link BatchedMesh}.
102 */
103 readonly isBatchedMesh: true;
104
105 /**
106 * @param maxInstanceCount the max number of individual geometries planned to be added.
107 * @param maxVertexCount the max number of vertices to be used by all geometries.
108 * @param maxIndexCount the max number of indices to be used by all geometries.
109 * @param material an instance of {@link Material}. Default is a new {@link MeshBasicMaterial}.
110 */
111 constructor(maxInstanceCount: number, maxVertexCount: number, maxIndexCount?: number, material?: Material);
112
113 /**
114 * Computes the bounding box, updating {@link .boundingBox} attribute.
115 * Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`.
116 */
117 computeBoundingBox(): void;
118
119 /**
120 * Computes the bounding sphere, updating {@link .boundingSphere} attribute.
121 * Bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`.
122 */
123 computeBoundingSphere(): void;
124
125 /**
126 * Frees the GPU-related resources allocated by this instance. Call this method whenever this instance is no longer
127 * used in your app.
128 */
129 dispose(): this;
130
131 /**
132 * Takes a sort a function that is run before render. The function takes a list of instances to sort and a camera.
133 * The objects in the list include a "z" field to perform a depth-ordered sort with.
134 */
135 setCustomSort(
136 sortFunction:
137 | ((this: this, list: Array<{ start: number; count: number; z: number }>, camera: Camera) => void)
138 | null,
139 ): this;
140
141 /**
142 * Get the color of the defined geometry.
143 * @param instanceId The id of an instance to get the color of.
144 * @param target The target object to copy the color in to.
145 */
146 getColorAt(instanceId: number, target: Color): void;
147
148 /**
149 * Get the local transformation matrix of the defined instance.
150 * @param instanceId The id of an instance to get the matrix of.
151 * @param target This 4x4 matrix will be set to the local transformation matrix of the defined instance.
152 */
153 getMatrixAt(instanceId: number, target: Matrix4): Matrix4;
154
155 /**
156 * Get whether the given instance is marked as "visible" or not.
157 * @param instanceId The id of an instance to get the visibility state of.
158 */
159 getVisibleAt(instanceId: number): boolean;
160
161 /**
162 * Get the range representing the subset of triangles related to the attached geometry, indicating the starting
163 * offset and count, or `null` if invalid.
164 *
165 * Return an object of the form: { start: Integer, count: Integer }
166 * @param geometryId The id of the geometry to get the range of.
167 * @param target Optional target object to copy the range in to.
168 */
169 getGeometryRangeAt(
170 geometryId: number,
171 target?: BatchedMeshGeometryRange,
172 ): BatchedMeshGeometryRange | null;
173
174 /**
175 * Get the geometryIndex of the defined instance.
176 * @param instanceId The id of an instance to get the geometryIndex of.
177 */
178 getGeometryIdAt(instanceId: number): number;
179
180 /**
181 * Sets the given color to the defined geometry instance.
182 * @param instanceId The id of the instance to set the color of.
183 * @param color The color to set the instance to.
184 */
185 setColorAt(instanceId: number, color: Color): void;
186
187 /**
188 * Sets the given local transformation matrix to the defined instance.
189 * @param instanceId The id of an instance to set the matrix of.
190 * @param matrix A 4x4 matrix representing the local transformation of a single instance.
191 */
192 setMatrixAt(instanceId: number, matrix: Matrix4): this;
193
194 /**
195 * Sets the visibility of the instance at the given index.
196 * @param instanceId The id of the instance to set the visibility of.
197 * @param visible A boolean value indicating the visibility state.
198 */
199 setVisibleAt(instanceId: number, visible: boolean): this;
200
201 /**
202 * Sets the geometryIndex of the instance at the given index.
203 * @param instanceId The id of the instance to set the geometryIndex of.
204 * @param geometryId The geometryIndex to be use by the instance.
205 */
206 setGeometryIdAt(instanceId: number, geometryId: number): this;
207
208 /**
209 * Adds the given geometry to the {@link BatchedMesh} and returns the associated index referring to it.
210 * @param geometry The geometry to add into the {@link BatchedMesh}.
211 * @param reservedVertexRange Optional parameter specifying the amount of vertex buffer space to reserve for the
212 * added geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is
213 * larger than the original geometry. Defaults to the length of the given geometry vertex buffer.
214 * @param reservedIndexRange Optional parameter specifying the amount of index buffer space to reserve for the added
215 * geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is larger
216 * than the original geometry. Defaults to the length of the given geometry index buffer.
217 */
218 addGeometry(geometry: BufferGeometry, reservedVertexRange?: number, reservedIndexRange?: number): number;
219
220 /**
221 * Adds a new instance to the {@link BatchedMesh} using the geometry of the given geometryId and returns a new id
222 * referring to the new instance to be used by other functions.
223 * @param geometryId The id of a previously added geometry via "addGeometry" to add into the {@link BatchedMesh} to
224 * render.
225 */
226 addInstance(geometryId: number): number;
227
228 /**
229 * @param geometryId The id of a geometry to remove from the [name] that was previously added via "addGeometry". Any
230 * instances referencing this geometry will also be removed as a side effect.
231 */
232 deleteGeometry(geometryId: number): this;
233
234 /**
235 * Removes an existing instance from the BatchedMesh using the given instanceId.
236 * @param instanceId The id of an instance to remove from the BatchedMesh that was previously added via
237 * "addInstance".
238 */
239 deleteInstance(instanceId: number): this;
240
241 /**
242 * Replaces the geometry at `geometryId` with the provided geometry. Throws an error if there is not enough space
243 * reserved for geometry. Calling this will change all instances that are rendering that geometry.
244 * @param geometryId Which geometry id to replace with this geometry.
245 * @param geometry The geometry to substitute at the given geometry id.
246 */
247 setGeometryAt(geometryId: number, geometry: BufferGeometry): number;
248
249 /**
250 * Repacks the sub geometries in [name] to remove any unused space remaining from previously deleted geometry,
251 * freeing up space to add new geometry.
252 */
253 optimize(): this;
254
255 /**
256 * Resizes the available space in BatchedMesh's vertex and index buffer attributes to the provided sizes. If the
257 * provided arguments shrink the geometry buffers but there is not enough unused space at the end of the geometry
258 * attributes then an error is thrown.
259 * @param maxVertexCount the max number of vertices to be used by all unique geometries to resize to.
260 * @param maxIndexCount the max number of indices to be used by all unique geometries to resize to.
261 */
262 setGeometrySize(maxVertexCount: number, maxIndexCount: number): void;
263
264 /**
265 * Resizes the necessary buffers to support the provided number of instances. If the provided arguments shrink the
266 * number of instances but there are not enough unused ids at the end of the list then an error is thrown.
267 * @param maxInstanceCount the max number of individual instances that can be added and rendered by the BatchedMesh.
268 */
269 setInstanceCount(maxInstanceCount: number): void;
270
271 getBoundingBoxAt(geometryId: number, target: Box3): Box3 | null;
272 getBoundingSphereAt(geometryId: number, target: Sphere): Sphere | null;
273}
274
275export { BatchedMesh };
276
\No newline at end of file