Skip to content

Instantly share code, notes, and snippets.

@RenaudRohlinger
Last active March 28, 2020 10:03
Show Gist options
  • Save RenaudRohlinger/67475b125bc5a1e225833bcbd209ab71 to your computer and use it in GitHub Desktop.
Save RenaudRohlinger/67475b125bc5a1e225833bcbd209ab71 to your computer and use it in GitHub Desktop.
var mesh = null;
var dummy = new THREE.Object3D();
var sectionWidth = 200;
function addInstancedMesh() {
// An InstancedMesh of 4 cubes
mesh = new THREE.InstancedMesh(new THREE.BoxBufferGeometry(50,50,50), new THREE.MeshNormalMaterial(), 4);
scene.add(mesh);
setInstancedMeshPositions(mesh);
}
function setInstancedMeshPositions(mesh) {
for ( var i = 0; i < mesh.count; i ++ ) {
// we add 200 units of distance (the width of the section) between each.
var xStaticPosition = -sectionWidth * (i - 1)
dummy.position.set(xStaticPosition, 0, 0);
dummy.updateMatrix();
mesh.setMatrixAt( i, dummy.matrix );
}
mesh.instanceMatrix.needsUpdate = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment