Hi, new to 3 and 3D, so just need some basic pointers. I was able to follow along the examples and get a basic scene with some box items displayed as below:
this.geometry = new THREE.BoxGeometry( 1, 1, 1 );
this.geometry.translate( 0, 0.5, 0 );
this.material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
for ( let i = 0; i < 500; i ++ ) {
const mesh = new THREE.Mesh( this.geometry, this.material );
mesh.position.x = Math.random() * 1600 - 800;
mesh.position.y = 0;
mesh.position.z = Math.random() * 1600 - 800;
mesh.scale.x = 20;
mesh.scale.y = Math.random() * 80 + 10;
mesh.scale.z = 20;
mesh.updateMatrix();
mesh.matrixAutoUpdate = false;
this.scene.add( mesh );
}
But, I was thinking instance mesh might be smarter/better performance since all of my items will just be boxes. They’ll need to have different sizes and colors however, so maybe that’s not a good fit for instance mesh? So, I tried the following and I just get one box displayed on screen. My render function isn’t doing anything other than a call to render the scene right now. So, I’m def doing something wrong when I’m trying to use an instance mesh and iterate over the instances and change their shape and position… Thanks for any help!
this.geometry = new THREE.BoxGeometry( 1, 1, 1 );
this.geometry.translate( 0, 0.5, 0 );
this.material = new THREE.MeshPhongMaterial( { color: 0x0020ee, flatShading: true, shininess: 150 } );
this.mesh = new THREE.InstancedMesh( this.geometry, this.material, 500 );
var matrix = new THREE.Matrix4();
this.mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
console.log("created instance mesh");
this.scene.add( this.mesh );
for ( let i = 0; i < 500; i ++ )
{
//var matrix = new THREE.Matrix4();
//this.mesh.getMatrixAt(i, matrix);
var x = Math.random() * 1600 - 800;
var y = 0;
var z = Math.random() * 1600 - 800;
matrix.setPosition(x,y,z);
this.mesh.setMatrixAt(i, matrix);
x = 20;
y = Math.random() * 80 + 10;
z = 20;
matrix.makeScale(x, y, z);
this.mesh.setMatrixAt(i, matrix);
//itemMesh.updateMatrix();
//mesh.matrixAutoUpdate = false;
}
this.mesh.instanceMatrix.needsUpdate = true;