Skip to content

Feature request: Make the '_indirectTexture' property of the instance 'THREE.BatchedMesh' public, or obtain the 'instance ID' using TSL in the 'colorNode' of 'THREE.BatchedMesh'. #33477

@John-D-a-i

Description

@John-D-a-i

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    TSLThree.js Shading Language

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions