Description
'BatchedMesh' of 'THREE' has the 'setColorAt' method which accomplishes the color setting task. However, I need a more flexible way to handle 'material.colorNode = Fn(() => { .... })'. Within the function, I want to handle the TSL effect and implement different effect logic based on 'instance ID'. Therefore, I hope to obtain the 'instance ID' of 'BatchedMesh' within this function.
Solution
There are two solutions. You only need to implement one of them.
Method 1: Change the private attribute _indirectTexture of batchedMesh to an public attribute.
Method 2.
TSL adds a function for obtaining the instance ID
const GetIndirectIndexNode = Fn((arr) => {
const [id] = arr
const textureSizeNode = textureSize(textureLoad(batchedMesh._indirectTexture), int(0))
const size = int(textureSizeNode.x)
const x = int(id).mod(size)
const y = int(id).div(size)
return textureLoad(batchedMesh._indirectTexture, ivec2(x, y)).x
})
Alternatives
It has been verified that a method for perfectly obtaining the 'instance ID' is currently available.
const GetIndirectIndexNode = Fn((arr) => {
const [id] = arr
const textureSizeNode = textureSize(textureLoad(batchedMesh._indirectTexture), int(0))
const size = int(textureSizeNode.x)
const x = int(id).mod(size)
const y = int(id).div(size)
return textureLoad(batchedMesh._indirectTexture, ivec2(x, y)).x
})
WebGL mode
const index = float(GetIndirectIndexNode(drawIndex)) //batchedMesh instance ID
WebGPU mode
const index = float(GetIndirectIndexNode(instanceIndex)) //batchedMesh instance ID
Additional context
const renderer = new THREE.WebGPURenderer({forceWebGL:true}) // WebGL mode
const renderer = new THREE.WebGPURenderer({forceWebGL:false}) // WebGPU mode
Description
'BatchedMesh' of 'THREE' has the 'setColorAt' method which accomplishes the color setting task. However, I need a more flexible way to handle 'material.colorNode = Fn(() => { .... })'. Within the function, I want to handle the TSL effect and implement different effect logic based on 'instance ID'. Therefore, I hope to obtain the 'instance ID' of 'BatchedMesh' within this function.
Solution
There are two solutions. You only need to implement one of them.
Method 1: Change the private attribute _indirectTexture of batchedMesh to an public attribute.
Method 2.
TSL adds a function for obtaining the instance ID
Alternatives
It has been verified that a method for perfectly obtaining the 'instance ID' is currently available.
WebGL mode
const index = float(GetIndirectIndexNode(drawIndex)) //batchedMesh instance IDWebGPU mode
const index = float(GetIndirectIndexNode(instanceIndex)) //batchedMesh instance IDAdditional context
const renderer = new THREE.WebGPURenderer({forceWebGL:true}) // WebGL mode
const renderer = new THREE.WebGPURenderer({forceWebGL:false}) // WebGPU mode