Hello, I have created multiple instances using InstancedMesh, and I want to change the color of some vertices in one instance without affecting other instances, I tried the method, but it didn’t work
const geometry = new THREE.BufferGeometry();
const vertices = []; // vertex array
const colors = []; // color array
for (let i = 0; i < 10000; i++) {
const x = Math.random() * 2 - 1;
const y = Math.random() * 2 - 1;
const z = Math.random() * 2 - 1;
vertices.push(x, y, z);
colors.push(Math.random(), Math.random(), Math.random(), 1);
}
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(vertices), 3)
);
geometry.setAttribute(
"color",
new THREE.BufferAttribute(new Float32Array(colors), 4)
);
const material = new THREE.MeshBasicMaterial({
vertexColors: true,
transparent: true,
});
const instanceCount = 3;
const instancedMesh = new THREE.InstancedMesh(
geometry,
material,
instanceCount
);
scene.add(instancedMesh);
// test, change the color
setTimeout(() => {
// the second instance
let instanceID = 1
const attr = geometry.getAttribute("color")
const count = attr.count
// Change the color of the second vertex of the second instance
attr.setXYZW(instanceID * count + 1, 1, 0, 0, 1)
attr.needsUpdate = true // did not work, how to do?????
}, 3000);