A special version of Mesh with multi draw batch rendering support. Use BatchedMesh if you have to render a large number of objects with the same material but with different world transformations. The usage of BatchedMesh will help you to reduce the number of draw calls and thus improve the overall rendering performance in your application.

If the WEBGL_multi_draw extension is not supported then a less performant fallback is used.

const box = new THREE.BoxGeometry( 1, 1, 1 );
const sphere = new THREE.SphereGeometry( 1, 12, 12 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );

// initialize and add geometries into the batched mesh
const batchedMesh = new BatchedMesh( 10, 5000, 10000, material );
const boxGeometryId = batchedMesh.addGeometry( box );
const sphereGeometryId = batchedMesh.addGeometry( sphere );

// create instances of those geometries
const boxInstancedId1 = batchedMesh.addInstance( boxGeometryId );
const boxInstancedId2 = batchedMesh.addInstance( boxGeometryId );

const sphereInstancedId1 = batchedMesh.addInstance( sphereGeometryId );
const sphereInstancedId2 = batchedMesh.addInstance( sphereGeometryId );

// position the geometries
batchedMesh.setMatrixAt( boxInstancedId1, boxMatrix1 );
batchedMesh.setMatrixAt( boxInstancedId2, boxMatrix2 );

batchedMesh.setMatrixAt( sphereInstancedId1, sphereMatrix1 );
batchedMesh.setMatrixAt( sphereInstancedId2, sphereMatrix2 );

scene.add( batchedMesh );

Example: WebGL / mesh / batch

Hierarchy (view full)

Constructors

Properties

Accessors

Methods

Constructors

  • Parameters

    • maxInstanceCount: number

      the max number of individual geometries planned to be added.

    • maxVertexCount: number

      the max number of vertices to be used by all geometries.

    • OptionalmaxIndexCount: number

      the max number of indices to be used by all geometries.

    • Optionalmaterial: Material

      an instance of Material. Default is a new MeshBasicMaterial.

    Returns BatchedMesh

Properties

animations: AnimationClip[]

Array with object's animation clips.

[]

boundingBox: null | Box3

This bounding box encloses all instances of the BatchedMesh. Can be calculated with .computeBoundingBox().

null
boundingSphere: null | Sphere

This bounding sphere encloses all instances of the BatchedMesh. Can be calculated with .computeBoundingSphere().

null
castShadow: boolean

Whether the object gets rendered into shadow map.

false

Array with object's children.

THREE.Object3DGroup | Group for info on manually grouping objects.

[]

customDepthMaterial?: Material

Custom depth material to be used when rendering to the depth map.

Can only be used in context of meshes. When shadow-casting with a THREE.DirectionalLight | DirectionalLight or THREE.SpotLight | SpotLight, if you are modifying vertex positions in the vertex shader you must specify a customDepthMaterial for proper shadows.

undefined

customDistanceMaterial?: Material

Same as customDepthMaterial, but used with THREE.Object3DPointLight | PointLight.

undefined

customSort: null | ((this: this, list: {
    count: number;
    start: number;
    z: number;
}[], camera: Camera) => void)
frustumCulled: boolean

When this is set, it checks every frame if the object is in the frustum of the camera before rendering the object. If set to false the object gets rendered every frame even if it is not in the frustum of the camera.

true

An instance of THREE.BufferGeometry | BufferGeometry (or derived classes), defining the object's structure.

THREE.BufferGeometry | new THREE.BufferGeometry().

id: number

Unique number for this Object3D instance.

Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object. Expects a Integer

isBatchedMesh

Read-only flag to check if a given object is of type BatchedMesh.

isMesh

Read-only flag to check if a given object is of type Mesh.

This is a constant value

true

isObject3D

Flag to check if a given object is of type Object3D.

This is a constant value

true

layers: Layers

The layer membership of the object.

The object is only visible if it has at least one layer in common with the THREE.Object3DCamera | Camera in use. This property can also be used to filter out unwanted objects in ray-intersection tests when using THREE.Raycaster | Raycaster.

new THREE.Layers()

material: Material

An instance of material derived from the THREE.Material | Material base class or an array of materials, defining the object's appearance.

THREE.MeshBasicMaterial | new THREE.MeshBasicMaterial().

matrix: Matrix4

The local transform matrix.

new THREE.Matrix4()

matrixAutoUpdate: boolean

When this is set, it calculates the matrix of position, (rotation or quaternion) and scale every frame and also recalculates the matrixWorld property.

DEFAULT_MATRIX_AUTO_UPDATE - that is (true).

matrixWorld: Matrix4

The global transform of the object.

If the Object3D has no parent, then it's identical to the local transform THREE.Object3D.matrix | .matrix.

new THREE.Matrix4()

matrixWorldAutoUpdate: boolean

If set, then the renderer checks every frame if the object and its children need matrix updates. When it isn't, then you have to maintain all matrices in the object and its children yourself.

DEFAULT_MATRIX_WORLD_AUTO_UPDATE - that is (true).

matrixWorldNeedsUpdate: boolean

When this is set, it calculates the matrixWorld in that frame and resets this property to false.

false

modelViewMatrix: Matrix4

new THREE.Matrix4()

morphTargetDictionary?: {
    [key: string]: number;
}

A dictionary of morphTargets based on the morphTarget.name property.

undefined, but rebuilt by .updateMorphTargets().

morphTargetInfluences?: number[]

An array of weights typically from 0-1 that specify how much of the morph is applied.

undefined, but reset to a blank array by .updateMorphTargets().

name: string

Optional name of the object

(doesn't need to be unique).

""

normalMatrix: Matrix3

new THREE.Matrix3()

parent: null | Object3D<Object3DEventMap>

Object's parent in the scene graph.

An object can have at most one parent.

null

perObjectFrustumCulled: boolean

If true then the individual objects within the BatchedMesh are frustum culled.

true
position: Vector3

Object's local position.

new THREE.Vector3() - that is (0, 0, 0).

quaternion: Quaternion

Object's local rotation as a THREE.Quaternion | Quaternion.

new THREE.Quaternion() - that is (0, 0, 0, 1).

receiveShadow: boolean

Whether the material receives shadows.

false

renderOrder: number

This value allows the default rendering order of scene graph objects to be overridden although opaque and transparent objects remain sorted independently.

When this property is set for an instance of Group, all descendants objects will be sorted and rendered together. Sorting is from lowest to highest renderOrder.

0

rotation: Euler

Object's local rotation (Euler angles), in radians.

new THREE.Euler() - that is (0, 0, 0, Euler.DEFAULT_ORDER).

scale: Vector3

The object's local scale.

new THREE.Vector3( 1, 1, 1 )

sortObjects: boolean

If true then the individual objects within the BatchedMesh are sorted to improve overdraw-related artifacts. If the material is marked as "transparent" objects are rendered back to front and if not then they are rendered front to back.

true
type: string

Mesh

This is used by the lookAt method, for example, to determine the orientation of the result.

Object3D.DEFAULT_UP - that is (0, 1, 0).

userData: Record<string, any>

An object that can be used to store custom data about the Object3D.

It should not hold references to functions as these will not be cloned.

{}

uuid: string

UUID of this object instance.

This gets automatically assigned and shouldn't be edited.

visible: boolean

Object gets rendered if true.

true

DEFAULT_MATRIX_AUTO_UPDATE: boolean

The default setting for matrixAutoUpdate for newly created Object3Ds.

true

DEFAULT_MATRIX_WORLD_AUTO_UPDATE: boolean

The default setting for matrixWorldAutoUpdate for newly created Object3Ds.

true

DEFAULT_UP: Vector3

The default up direction for objects, also used as the default position for THREE.DirectionalLight | DirectionalLight, THREE.HemisphereLight | HemisphereLight and THREE.Spotlight | Spotlight (which creates lights shining from the top down).

new THREE.Vector3( 0, 1, 0)

Accessors

  • get instanceCount(): number
  • Returns number

  • get maxInstanceCount(): number
  • The maximum number of individual geometries that can be stored in the BatchedMesh. Read only.

    Returns number

  • get unusedIndexCount(): number
  • Returns number

  • get unusedVertexCount(): number
  • Returns number

Methods

  • Adds another Object3D as child of this Object3D.

    Parameters

    Returns this

    An arbitrary number of objects may be added Any current parent on an object passed in here will be removed, since an Object3D can have at most one parent.

    • attach
    • THREE.Group | Group for info on manually grouping objects.
  • Adds a listener to an event type.

    Type Parameters

    Parameters

    Returns void

  • Adds the given geometry to the BatchedMesh and returns the associated index referring to it.

    Parameters

    • geometry: BufferGeometry<NormalBufferAttributes>

      The geometry to add into the BatchedMesh.

    • OptionalreservedVertexRange: number

      Optional parameter specifying the amount of vertex buffer space to reserve for the added geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is larger than the original geometry. Defaults to the length of the given geometry vertex buffer.

    • OptionalreservedIndexRange: number

      Optional parameter specifying the amount of index buffer space to reserve for the added geometry. This is necessary if it is planned to set a new geometry at this index at a later time that is larger than the original geometry. Defaults to the length of the given geometry index buffer.

    Returns number

  • Adds a new instance to the BatchedMesh using the geometry of the given geometryId and returns a new id referring to the new instance to be used by other functions.

    Parameters

    • geometryId: number

      The id of a previously added geometry via "addGeometry" to add into the BatchedMesh to render.

    Returns number

  • Applies the matrix transform to the object and updates the object's position, rotation and scale.

    Parameters

    Returns void

  • Applies the rotation represented by the quaternion to the object.

    Parameters

    Returns this

  • Adds a Object3D as a child of this, while maintaining the object's world transform.

    Parameters

    Returns this

    Note: This method does not support scene graphs having non-uniformly-scaled nodes(s).

    add

  • Removes all child objects.

    Returns this

  • Returns a clone of this object and optionally all descendants.

    Parameters

    • Optionalrecursive: boolean

      If true, descendants of the object are also cloned. Default true

    Returns this

  • Computes the bounding box, updating .boundingBox attribute. Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are null.

    Returns void

  • Computes the bounding sphere, updating .boundingSphere attribute. Bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are null.

    Returns void

  • Copies the given object into this object.

    Parameters

    • object: Object3D<Object3DEventMap>
    • Optionalrecursive: boolean

      If set to true, descendants of the object are copied next to the existing ones. If set to false, descendants are left unchanged. Default is true.

    Returns this

    Event listeners and user-defined callbacks (.onAfterRender and .onBeforeRender) are not copied.

  • Parameters

    • geometryId: number

      The id of a geometry to remove from the [name] that was previously added via "addGeometry". Any instances referencing this geometry will also be removed as a side effect.

    Returns this

  • Removes an existing instance from the BatchedMesh using the given instanceId.

    Parameters

    • instanceId: number

      The id of an instance to remove from the BatchedMesh that was previously added via "addInstance".

    Returns this

  • Frees the GPU-related resources allocated by this instance. Call this method whenever this instance is no longer used in your app.

    Returns this

  • Parameters

    • geometryId: number
    • target: Box3

    Returns null | Box3

  • Parameters

    • geometryId: number
    • target: Sphere

    Returns null | Sphere

  • Get the color of the defined geometry.

    Parameters

    • instanceId: number

      The id of an instance to get the color of.

    • target: Color

      The target object to copy the color in to.

    Returns void

  • Get the geometryIndex of the defined instance.

    Parameters

    • instanceId: number

      The id of an instance to get the geometryIndex of.

    Returns number

  • Get the range representing the subset of triangles related to the attached geometry, indicating the starting offset and count, or null if invalid.

    Return an object of the form: { start: Integer, count: Integer }

    Parameters

    • geometryId: number

      The id of the geometry to get the range of.

    • Optionaltarget: BatchedMeshGeometryRange

      Optional target object to copy the range in to.

    Returns null | BatchedMeshGeometryRange

  • Get the local transformation matrix of the defined instance.

    Parameters

    • instanceId: number

      The id of an instance to get the matrix of.

    • target: Matrix4

      This 4x4 matrix will be set to the local transformation matrix of the defined instance.

    Returns Matrix4

  • Searches through an object and its children, starting with the object itself, and returns the first with a matching id.

    Parameters

    • id: number

      Unique number of the object instance. Expects a Integer

    Returns undefined | Object3D<Object3DEventMap>

    Note that ids are assigned in chronological order: 1, 2, 3, ..., incrementing by one for each new object.

    id

  • Searches through an object and its children, starting with the object itself, and returns the first with a matching name.

    Parameters

    • name: string

      String to match to the children's Object3D.name property.

    Returns undefined | Object3D<Object3DEventMap>

    Note that for most objects the name is an empty string by default You will have to set it manually to make use of this method.

  • Searches through an object and its children, starting with the object itself, and returns the first with a property that matches the value given.

    Parameters

    • name: string

      the property name to search for.

    • value: any

      value of the given property.

    Returns undefined | Object3D<Object3DEventMap>

  • Searches through an object and its children, starting with the object itself, and returns the first with a property that matches the value given.

    Parameters

    • name: string

      The property name to search for.

    • value: any

      Value of the given property.

    • OptionaloptionalTarget: Object3D<Object3DEventMap>[]

      target to set the result. Otherwise a new Array is instantiated. If set, you must clear this array prior to each call (i.e., array.length = 0;).

    Returns Object3D<Object3DEventMap>[]

  • Get the local-space position of the vertex at the given index, taking into account the current animation state of both morph targets and skinning.

    Parameters

    • index: number

      Expects a Integer

    • target: Vector3

    Returns Vector3

  • Get whether the given instance is marked as "visible" or not.

    Parameters

    • instanceId: number

      The id of an instance to get the visibility state of.

    Returns boolean

  • Returns a vector representing the direction of object's positive z-axis in world space.

    Parameters

    • target: Vector3

      The result will be copied into this Vector3.

    Returns Vector3

  • Returns a vector representing the position of the object in world space.

    Parameters

    • target: Vector3

      The result will be copied into this Vector3.

    Returns Vector3

  • Returns a quaternion representing the rotation of the object in world space.

    Parameters

    • target: Quaternion

      The result will be copied into this Quaternion.

    Returns Quaternion

  • Returns a vector of the scaling factors applied to the object for each axis in world space.

    Parameters

    • target: Vector3

      The result will be copied into this Vector3.

    Returns Vector3

  • Checks if listener is added to an event type.

    Type Parameters

    Parameters

    Returns boolean

  • Converts the vector from this object's local space to world space.

    Parameters

    • vector: Vector3

      A vector representing a position in this object's local space.

    Returns Vector3

  • Rotates the object to face a point in world space.

    Parameters

    • vector: Vector3

      A vector representing a position in world space to look at.

    Returns void

    This method does not support objects having non-uniformly-scaled parent(s).

  • Rotates the object to face a point in world space.

    Parameters

    • x: number

      Expects a Float

    • y: number

      Expects a Float

    • z: number

      Expects a Float

    Returns void

    This method does not support objects having non-uniformly-scaled parent(s).

  • An optional callback that is executed immediately after a 3D object is rendered.

    Parameters

    Returns void

    This function is called with the following parameters: renderer, scene, camera, geometry, material, group. Please notice that this callback is only executed for renderable 3D objects. Meaning 3D objects which define their visual appearance with geometries and materials like instances of Mesh, Line, Points or Sprite. Instances of Object3D, Group or Bone are not renderable and thus this callback is not executed for such objects.

  • An optional callback that is executed immediately after a 3D object is rendered to a shadow map.

    Parameters

    Returns void

    This function is called with the following parameters: renderer, scene, camera, shadowCamera, geometry, depthMaterial, group. Please notice that this callback is only executed for renderable 3D objects. Meaning 3D objects which define their visual appearance with geometries and materials like instances of Mesh, Line, Points or Sprite. Instances of Object3D, Group or Bone are not renderable and thus this callback is not executed for such objects.

  • An optional callback that is executed immediately before a 3D object is rendered.

    Parameters

    Returns void

    This function is called with the following parameters: renderer, scene, camera, geometry, material, group. Please notice that this callback is only executed for renderable 3D objects. Meaning 3D objects which define their visual appearance with geometries and materials like instances of Mesh, Line, Points or Sprite. Instances of Object3D, Group or Bone are not renderable and thus this callback is not executed for such objects.

  • An optional callback that is executed immediately before a 3D object is rendered to a shadow map.

    Parameters

    Returns void

    This function is called with the following parameters: renderer, scene, camera, shadowCamera, geometry, depthMaterial, group. Please notice that this callback is only executed for renderable 3D objects. Meaning 3D objects which define their visual appearance with geometries and materials like instances of Mesh, Line, Points or Sprite. Instances of Object3D, Group or Bone are not renderable and thus this callback is not executed for such objects.

  • Repacks the sub geometries in [name] to remove any unused space remaining from previously deleted geometry, freeing up space to add new geometry.

    Returns this

  • Abstract (empty) method to get intersections between a casted ray and this object

    Parameters

    Returns void

    Subclasses such as THREE.Mesh | Mesh, THREE.Line | Line, and THREE.Points | Points implement this method in order to use raycasting.

    THREE.Raycaster | Raycaster

    () => {}

  • Removes a Object3D as child of this Object3D.

    Parameters

    Returns this

    An arbitrary number of objects may be removed.

    THREE.Group | Group for info on manually grouping objects.

  • Removes a listener from an event type.

    Type Parameters

    Parameters

    Returns void

  • Removes this object from its current parent.

    Returns this

  • Rotate an object along an axis in object space.

    Parameters

    • axis: Vector3

      A normalized vector in object space.

    • angle: number

      The angle in radians. Expects a Float

    Returns this

    The axis is assumed to be normalized.

  • Rotate an object along an axis in world space.

    Parameters

    • axis: Vector3

      A normalized vector in world space.

    • angle: number

      The angle in radians. Expects a Float

    Returns this

    The axis is assumed to be normalized Method Assumes no rotated parent.

  • Rotates the object around x axis in local space.

    Parameters

    • angle: number

    Returns this

  • Rotates the object around y axis in local space.

    Parameters

    • angle: number

    Returns this

  • Rotates the object around z axis in local space.

    Parameters

    • angle: number

    Returns this

  • Sets the given color to the defined geometry instance.

    Parameters

    • instanceId: number

      The id of the instance to set the color of.

    • color: Color

      The color to set the instance to.

    Returns void

  • Takes a sort a function that is run before render. The function takes a list of instances to sort and a camera. The objects in the list include a "z" field to perform a depth-ordered sort with.

    Parameters

    • sortFunction: null | ((this: this, list: {
          count: number;
          start: number;
          z: number;
      }[], camera: Camera) => void)

    Returns this

  • Replaces the geometry at geometryId with the provided geometry. Throws an error if there is not enough space reserved for geometry. Calling this will change all instances that are rendering that geometry.

    Parameters

    Returns number

  • Sets the geometryIndex of the instance at the given index.

    Parameters

    • instanceId: number

      The id of the instance to set the geometryIndex of.

    • geometryId: number

      The geometryIndex to be use by the instance.

    Returns this

  • Resizes the available space in BatchedMesh's vertex and index buffer attributes to the provided sizes. If the provided arguments shrink the geometry buffers but there is not enough unused space at the end of the geometry attributes then an error is thrown.

    Parameters

    • maxVertexCount: number

      the max number of vertices to be used by all unique geometries to resize to.

    • maxIndexCount: number

      the max number of indices to be used by all unique geometries to resize to.

    Returns void

  • Resizes the necessary buffers to support the provided number of instances. If the provided arguments shrink the number of instances but there are not enough unused ids at the end of the list then an error is thrown.

    Parameters

    • maxInstanceCount: number

      the max number of individual instances that can be added and rendered by the BatchedMesh.

    Returns void

  • Sets the given local transformation matrix to the defined instance.

    Parameters

    • instanceId: number

      The id of an instance to set the matrix of.

    • matrix: Matrix4

      A 4x4 matrix representing the local transformation of a single instance.

    Returns this

  • Calls THREE.Quaternion.setFromAxisAngle | setFromAxisAngle(axis, angle) on the .quaternion.

    Parameters

    • axis: Vector3

      A normalized vector in object space.

    • angle: number

      Angle in radians. Expects a Float

    Returns void

  • Calls THREE.Quaternion.setFromEuler | setFromEuler(euler) on the .quaternion.

    Parameters

    • euler: Euler

      Euler angle specifying rotation amount.

    Returns void

  • Calls THREE.Quaternion.setFromRotationMatrix | setFromRotationMatrix(m) on the .quaternion.

    Parameters

    • m: Matrix4

      Rotate the quaternion by the rotation component of the matrix.

    Returns void

    Note that this assumes that the upper 3x3 of m is a pure rotation matrix (i.e, unscaled).

  • Copy the given THREE.Quaternion | Quaternion into .quaternion.

    Parameters

    Returns void

  • Sets the visibility of the instance at the given index.

    Parameters

    • instanceId: number

      The id of the instance to set the visibility of.

    • visible: boolean

      A boolean value indicating the visibility state.

    Returns this

  • Convert the object to three.js JSON Object/Scene format.

    Parameters

    • Optionalmeta: JSONMeta

      Object containing metadata such as materials, textures or images for the object.

    Returns MeshJSON

  • Translate an object by distance along an axis in object space

    Parameters

    • axis: Vector3

      A normalized vector in object space.

    • distance: number

      The distance to translate. Expects a Float

    Returns this

    The axis is assumed to be normalized.

  • Translates object along x axis in object space by distance units.

    Parameters

    • distance: number

      Expects a Float

    Returns this

  • Translates object along y axis in object space by distance units.

    Parameters

    • distance: number

      Expects a Float

    Returns this

  • Translates object along z axis in object space by distance units.

    Parameters

    • distance: number

      Expects a Float

    Returns this

  • Executes the callback on this object and all descendants.

    Parameters

    Returns void

    Note: Modifying the scene graph inside the callback is discouraged.

  • Executes the callback on all ancestors.

    Parameters

    Returns void

    Note: Modifying the scene graph inside the callback is discouraged.

  • Like traverse, but the callback will only be executed for visible objects

    Parameters

    Returns void

    Descendants of invisible objects are not traversed. Note: Modifying the scene graph inside the callback is discouraged.

  • Updates local transform.

    Returns void

  • Updates the global transform of the object. And will update the object descendants if .matrixWorldNeedsUpdate is set to true or if the force parameter is set to true.

    Parameters

    • Optionalforce: boolean

      A boolean that can be used to bypass .matrixWorldAutoUpdate, to recalculate the world matrix of the object and descendants on the current frame. Useful if you cannot wait for the renderer to update it on the next frame, assuming .matrixWorldAutoUpdate set to true.

    Returns void

  • Updates the global transform of the object.

    Parameters

    • updateParents: boolean

      Recursively updates global transform of ancestors.

    • updateChildren: boolean

      Recursively updates global transform of descendants.

    Returns void

  • Converts the vector from world space to this object's local space.

    Parameters

    • vector: Vector3

      A vector representing a position in world space.

    Returns Vector3