BatchedMesh Opacity + Wireframe (r183)

Performance-focused Three.js `BatchedMesh` demo showing per-instance opacity animation via `setColorAt(Vector4)` and live wireframe mode toggling.

Key topics: three.js batchedmesh, per-instance opacity, wireframe, r183, rendering performance

Overview

BatchedMesh is Three.js's answer to a problem InstancedMesh cannot solve: drawing many objects with different geometries in a single draw call. This demo packs boxes, spheres and cones into one batched container that shares a material, then animates each instance's opacity independently and lets you toggle wireframe mode, two features that recent releases made work reliably with batching.

How it works

A BatchedMesh is created with a maximum instance count and a vertex and index budget. You add geometries to it once with addGeometry, which copies them into shared vertex and index buffers and returns a geometry ID. Then addInstance(geometryId) creates an instance that uses that geometry, and setMatrixAt(instanceId, matrix) positions it. Under the hood Three.js uses the WebGL multi-draw extension where available, so all instances of all geometries are submitted with one call.

Per-instance colour is stored in a texture. Passing a Vector4 to setColorAt sets the alpha channel too, which the material uses for transparency when transparent is enabled. That is how each object here fades in and out on its own schedule, even though they all share one material. Toggling material.wireframe applies to the whole batch, which is useful for inspecting the packed geometry.

Exploring the demo

Watch individual objects pulse in transparency at different rates while the scene still costs a single draw call. Toggle wireframe to see the mix of geometries packed into the batch. Orbit around and notice that transparent instances still need sensible sorting; BatchedMesh can sort its instances (sortObjects) to reduce blending artefacts.

When to use it

Choose InstancedMesh when every copy uses the same geometry (trees of one species, bullets, grass blades), and BatchedMesh when a scene has many different static or semi-static props that share a material, such as city furniture or modular level pieces. Benchmark the two in instancing vs BatchedMesh, learn the instancing basics in the instanced grid, and see how draw calls dominate CPU time in instancing performance.

What this code does

- BatchedMesh Aggregation: combines many object instances into one renderable for lower CPU submission overhead.
- Per-Instance Opacity: uses `batchedMesh.setColorAt(instanceId, Vector4)` where the alpha channel controls transparency.
- Wireframe Compatibility: toggling material wireframe demonstrates r183-era support improvements for batched rendering paths.
- Mixed Geometries: boxes, spheres, and cones are packed into one batched container with shared material state.
- Dynamic Opacity Animation: alpha pulsing runs per instance to stress-test color/opacity updates each frame.
- Practical Use: useful when you need many repeated props with varying transparency and optional debugging wireframe views.

Continue Learning

JavaScript Implementation

ES6+
import * as THREE from 'three'

const material = new THREE.MeshStandardMaterial({
  vertexColors: true,
  transparent: true
})
const batched = new THREE.BatchedMesh(200, 25000, 60000, material)

const boxId = batched.addGeometry(new THREE.BoxGeometry(1, 1, 1))
const sphereId = batched.addGeometry(new THREE.SphereGeometry(0.6, 16, 16))

const matrix = new THREE.Matrix4()
for (let i = 0; i < 200; i++) {
  const gId = i % 2 === 0 ? boxId : sphereId
  const instanceId = batched.addInstance(gId)
  matrix.makeTranslation((i % 20) - 10, 1, Math.floor(i / 20) - 5)
  batched.setMatrixAt(instanceId, matrix)
  batched.setColorAt(instanceId, new THREE.Vector4(0.2, 0.7, 1.0, 0.6))
}

batched.material.wireframe = false
scene.add(batched)

Related demos

Trending this week

Most popular