Hello,
Please forgive my noobness, i am somewhat new to all this…
Using latest r111 InstancedMesh, referencing the example webgl_instancing_raycast
in the example, i have simply replaced the below:
var geometry = new THREE.BoxBufferGeometry( .5, .5, .5, 1, 1, 1 );
var material = new THREE.MeshStandardMaterial( { map: new THREE.TextureLoader().load( 'textures/square-outline-textured.png' ) } );
which now draws cubes with the given texture…
my dilemma is: how to control color / texture of individual instances?! i am hoping this new InstancedMesh class makes this easy somehow?
Instancing is a more advanced approach recently being easier to start with with InstancedMesh, to add any more individual attributes you need to add a InstancedBufferAttribute to the geometry which instead controlling the attributes of a vertex, will be the attribute per instance, for example a individual color per instance.
For textures this requires more work depending on your case, if you have many different textures you would have to pack them into a texture atlas or texture array, replacing the line in the code sampling from the texture.
Anyway for more individual control you have to patch the material with a onBeforeCompile callback or you might take a look at this plugin i made to make patching easier.
@Fyrestar
Can I use instanced buffer attributes with THREE.InstancedMesh()?
I thought it relates to THREE.InstancedBufferGeometry().
But if so, would be cool to see any example with instanced attributes and instanced mesh.
@Hayawen here’s an example coloring each sphere individually using THREE.InstancedMesh and the plugin to patch the material to use the new attribute. (scroll to the bottom of the code)
It works without being based on THREE.InstancedBufferGeometry
Since you want to have 6 different textures on every instance, you have 6 values you store the texture index for each side, but in the shader you need to know which side it is in, which of those 6 values you will use to pick the texture from the texture atlas or array.
So you need to have an id attribute storing a side index for every 2 faces vertices of a side. You might have thought it is similar to a regular cube, using a different material for every side, but for instancing you need to use one shader. Since you said you’re new to all this it might be better to study more examples first as this is a little too complicated for beginners.
Hmm, ok, how about this… if InstancedMesh is given an array of 6 materials instead of just 1 material, the cube will display each texture on 1 face accordingly…
i am wondering can this be used somehow to know which face is being painted in the call? is it possible to know maybe which material it’s using and override based on that?
Ok this gives me an idea after having posted this… i can probably do this giving each material a special onBeforeCompile! ^^ gonna try that!
ok, here’s what i’ve managed to do so far…
the codepen below should display 6 different textures on each side of the cubes… but only for ONE side i am overriding the onBeforeUpdate function…
However, i am getting a very weird problem where only the 1st texture i replace works ok, 2nd and 3rd seem to always overlap with original?!