ThreeJS BufferGeometry

A mesh, line, or point geometry representation. Included within buffers are vertex positions, face indices, normals, colours, UVs, and custom attributes, lowering the cost of passing all of this data to the GPU.

Code Example

const geometry = new THREE.BufferGeometry();
// create a simple square shape. We duplicate the top left and bottom right
// vertices because each vertex needs to appear once per triangle.
const vertices = new Float32Array( [
	-1.0, -1.0,  1.0,
	 1.0, -1.0,  1.0,
	 1.0,  1.0,  1.0,


	 1.0,  1.0,  1.0,
	-1.0,  1.0,  1.0,
	-1.0, -1.0,  1.0
] );


// itemSize = 3 because there are 3 values (components) per vertex
geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
const material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
const mesh = new THREE.Mesh( geometry, material );

Constructor

BufferGeometry()

This creates a new BufferGeometry. It also sets several properties to a default value.

Properties

.attributes : Object

This hashmap has the name of the attribute to be set as the id and the buffer to set it to as the value. Instead of directly accessing this property, use. To access this geometry's attributes, use setAttribute and getAttribute.

.boundingBox : Box3

Bounding box for the bufferGeometry, which can be calculated with .computeBoundingBox(). Default is null.

.boundingSphere : Sphere

Bounding sphere for the bufferGeometry, which can be calculated with .computeBoundingSphere(). Default is null.

.drawRange : Object

Determines the part of the geometry to render. This should not be set directly, instead use .setDrawRange. Default is

{ start: 0, count: Infinity }

Count is the number of vertices to render for non-indexed BufferGeometry. Count is the number of indices to render for indexed BufferGeometry.

.groups : Array

Split the geometry into groups, each of which will be rendered in a separate WebGL draw call. This allows an array of materials to be used with the bufferGeometry.

Each group is an object of the form:

{ start: Integer, count: Integer, materialIndex: Integer }

where start specifies the first element in this draw call – the first vertex for non-indexed geometry, or the first triangle index otherwise. The number of vertices (or indices) included is specified by count, and the material array index is specified by materialIndex.

Rather than modifying this array directly, use .addGroup to add groups.

.id : Integer

Unique number for this bufferGeometry instance.

.index : BufferAttribute

Allows vertices to be reused across multiple triangles, a technique known as "indexed triangles." Each triangle is associated with three vertices' indices. As a result, this attribute stores the index of each vertex for each triangular face. If this attribute is not specified, the renderer assumes that each three adjacent positions represent a single triangle. The default value is null.

.morphAttributes : Object

Hashmap of BufferAttributes holding details of the geometry's morph targets.

.morphTargetsRelative : Boolean

Used to control the morph target behavior; when set to true, the morph target data is treated as relative offsets, rather than as absolute positions/normals. Default is false.

.name : String

Optional name for this bufferGeometry instance. Default is an empty string.

.userData : Object

An object that can be used to store custom data about the BufferGeometry. 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.

Methods

EventDispatcher methods are available on this class.

.setAttribute ( name : String, attribute : BufferAttribute ) : this

Sets an attribute to this geometry. Use this rather than the attributes property, because an internal hashmap of .attributes is maintained to speed up iterating over attributes.

.addGroup ( start : Integer, count : Integer, materialIndex : Integer ) : undefined

Adds a group to this geometry; see the groups property for details.

.applyMatrix4 ( matrix : Matrix4 ) : this

Applies the matrix transform to the geometry.

.applyQuaternion ( quaternion : Quaternion ) : this

Applies the rotation represented by the quaternion to the geometry.

.center () : this

Center the geometry based on the bounding box.

.clone () : BufferGeometry

Creates a clone of this BufferGeometry.

.copy ( bufferGeometry : BufferGeometry ) : this

Copies another BufferGeometry to this BufferGeometry.

.clearGroups ( ) : undefined

Clears all groups.

.computeBoundingBox () : undefined

Computes bounding box of the geometry, updating .boundingBox attribute.

Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are null.

.computeBoundingSphere () : undefined

Computes bounding sphere of the geometry, updating .boundingSphere attribute.

Bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are null.

.computeTangents () : undefined

Calculates and adds a tangent attribute to this geometry.

The computation is only supported for indexed geometries and if position, normal, and uv attributes are defined.

.computeVertexNormals () : undefined

Computes vertex normals by averaging face normals.

.dispose () : undefined

Disposes the object from memory.

You need to call this when you want the BufferGeometry removed while the application is running.

.getAttribute ( name : String ) : BufferAttribute

Returns the attribute with the specified name.

.getIndex () : BufferAttribute

Return the .index buffer.

.hasAttribute ( name : String ) : Boolean

Returns true if the attribute with the specified name exists.

.lookAt ( vector : Vector3 ) : this

vector - A world vector to look at.

Rotates the geometry to face a point in space. This is typically done as a one time operation, and not during a loop. Use Object3D.lookAt for typical real-time mesh usage.

.merge ( bufferGeometry : BufferGeometry, offset : Integer ) : this

Merge in another BufferGeometry with an optional offset of where to start merging in.

.normalizeNormals () : undefined

Every normal vector in a geometry will have a magnitude of 1. This will correct lighting on the geometry surfaces.

.deleteAttribute ( name : String ) : BufferAttribute

Deletes the attribute with the specified name.

.rotateX ( radians : Float ) : this

Rotate the geometry about the X axis. This is typically done as a one time operation, and not during a loop. Use Object3D.rotation for typical real-time mesh rotation.

.rotateY ( radians : Float ) : this

Rotate the geometry about the Y axis. This is typically done as a one time operation, and not during a loop. Use Object3D.rotation for typical real-time mesh rotation.

.rotateZ ( radians : Float ) : this

Rotate the geometry about the Z axis. This is typically done as a one time operation, and not during a loop. Use Object3D.rotation for typical real-time mesh rotation.

.scale ( x : Float, y : Float, z : Float ) : this

Scale the geometry data. This is typically done as a one time operation, and not during a loop. Use Object3D.scale for typical real-time mesh scaling.

.setIndex ( index : BufferAttribute ) : this

Set the .index buffer.

.setDrawRange ( start : Integer, count : Integer ) : undefined

Set the .drawRange property. For non-indexed BufferGeometry, count is the number of vertices to render. For indexed BufferGeometry, count is the number of indices to render.

.setFromPoints ( points : Array ) : this

Sets the attributes for this BufferGeometry from an array of points.

.toJSON () : Object

Convert the buffer geometry to three.js JSON Object/Scene format.

.toNonIndexed () : BufferGeometry

Return a non-index version of an indexed BufferGeometry.

.translate ( x : Float, y : Float, z : Float ) : this

Translate the geometry. This is typically done as a onetime operation, and not during a loop. Use Object3D.position for typical real-time mesh translation.


Related Topics

ThreeJS AnimationAction

AnimationActions are used to schedule the playback of animations saved in AnimationClips. Note that the majority of AnimationAction's methods can be chained together. The "Animation System" page in the "Next Steps" part...

6 minutes read.

ThreeJS Layers

A Layers object assigns an Object3D to one or more of the 32 layers numbered 0 to 31 - the layers are internally stored as a bit mask, and all...

1 minute read.

ThreeJS AnimationClip

An AnimationClip is a group of keyframe tracks that may be reused to depict an animation. The "Animation System" page in the "Next Steps" part of the handbook provides an overview...

2 minutes read.

ThreeJS Creating VR Content

This article gives a quick introduction of the main components of a three.js-based web-based VR application. Workflow      First, you have to include VRButton.js into your project. import { VRButton } from...

1 minute read.

ThreeJS AnimationMixer

The AnimationMixer is a player that allows you to play animations on a specific object in the scene. When many items in a scene are animated individually, each object may...

2 minutes read.

ThreeJS Uniform

Uniforms are global GLSL variables. They are passed to shader programs. Code Example When declaring a uniform of a ShaderMaterial, it is declared by value or by object. uniforms: { time: { value: 1.0...

2 minutes read.

ThreeJS Object3D

This is the base class for most three.js objects, and it provides a set of properties and methods for manipulating objects in 3D space. It should be noted that this can...

9 minutes read.

ThreeJS Audio

It is used to create a non-positional (global ) audio object. Code Example: // create an AudioListener and add it to the camera const listener = new THREE.AudioListener(); camera.add( listener ); // create a global...

6 minutes read.

ThreeJS Loaders 2

ImageBitmapLoader A loader that converts an image to an ImageBitmap. An ImageBitmap provides an asynchronous and resource-efficient path for preparing textures for WebGL rendering. ImageBitmapLoader, unlike FileLoader, does not prevent multiple concurrent...

8 minutes read.

ThreeJS Helper 1

ArrowHelper A 3D arrow object for visualizing directions. Code Example const dir = new THREE.Vector3( 1, 2, 0 ); //normalize the direction vector (convert to vector of length 1) dir.normalize(); const origin = new THREE.Vector3( 0,...

5 minutes read.

ThreeJS Compatibiliy Check and Running Locally

Even though this is becoming less of an issue, some devices or browsers may still be incompatible with WebGL. The method below enables you to check if it is supported...

3 minutes read.

ThreeJS AnimationObjectGroup and Utils

AnimationObjectGroup A group of objects that receives a shared animation state. Usage Add items you'd normally send as 'root' to AnimationMixer's function Object() { [native code] } or clipAction method, and instead pass...

2 minutes read.

ThreeJS Property Binding and Mixer

PropertyBinding Internally, this holds a reference to real property in the scene graph. Constructor PropertyBinding( rootNode : Object3D, path, parsedPath ) -- rootNode: -- path -- parsedPath (optional) Properties The properties are as follows: .path : Number.parsedPath...

2 minutes read.

ThreeJS Animation Systems

You may animate many aspects of your models with the three.js animation system, including the bones of a skinned and rigged model, morph targets, multiple material properties (colours, opacity, booleans),...

3 minutes read.

ThreeJS Math

Box2 Represents an axis-aligned bounding box (AABB) in 2D space. Constructor Box2( min : Vector2, max : Vector2 ) min - (optional) Vector2 representing the lower (x, y) boundary of the box. Default is...

48 minutes read.

ThreeJS Keyframe Track

A KeyframeTrack is a timed series of keyframes that are made up of lists of times and related values and are used to animate an object's unique property. The "Animation System"...

4 minutes read.

ThreeJS Renderers

WebGLMultipleRenderTargets A special render target that enables a fragment shader to write to several textures. This approach is useful for advanced rendering techniques like post-processing or deferred rendering. Heads up: WebGLMultipleRenderTargets...

15 minutes read.

ThreeJS Shadows

LightShadow Serves as a base class for the other shadow classes. Constructor LightShadow( camera : Camera ) camera - the light's view of the world. Create a new LightShadow. This is not intended to be...

7 minutes read.

ThreeJS Raycaster

This class is intended to help with raycasting. Raycasting is used for a variety of purposes, including mouse picking (determining which objects in 3D space the mouse is over). Code Example const...

4 minutes read.

ThreeJS Managers

DefaultLoadingManager When no custom manager is specified, most loaders use a global instance of the LoadingManager. This will suffice for most purposes, but there may be times when you want separate loading...

4 minutes read.