Hello, Im looking for the TSL equiv of WebGL’s gl_DrawID
For context I’m trying to sample a custom batching texture just like how the batching matrices data texture is sampled internally.
Internally drawIndex/instanceIndex is used for this purpose here, but when I use the same code, those index variables are always the same value (null) it looks like. https://github.com/mrdoob/three.js/blob/2eefcd6db11dba2d55dd49a51e7c529b6c20ef28/src/nodes/accessors/BatchNode.js#L90
Here is a minimal working WebGL example I’d like to recreate in WebGPU
https://codesandbox.io/p/devbox/thirsty-bush-f7z3c3?workspaceId=ws_5NvLnrKyk5Nd2YftNHw3Fy
Here is my WebGPU attempt so far. I am aware of Multi draw not being supported in WebGPU so far but even when using forceWebGL flag on the renderer, this example does not work.
https://codesandbox.io/p/devbox/gracious-bassi-yhm3xg?workspaceId=ws_5NvLnrKyk5Nd2YftNHw3Fy
Any help would be very much appriciated. Thanks!
Fixed. Stupid mistake Needed to move material definition before BatchedMesh initialization and change colorNode to the following.
material.colorNode = TSL.Fn(() => {
return TSL.color(TSL.float(GetIndirectIndexNode(TSL.drawIndex)).div(n));
})();
Important to cast GetIndirectIndexNode result to float before division, else, the division result will be an int.
Fixed demo:
https://codesandbox.io/p/devbox/inspiring-brook-w6jjgw?workspaceId=ws_5NvLnrKyk5Nd2YftNHw3Fy
Mugen87
November 7, 2025, 10:02am
3
I’m glad you fixed it, I was actually looking into your code yesterday which lead to BatchNode: Optimize generated shader code. by Mugen87 · Pull Request #32203 · mrdoob/three.js · GitHub .
To clarify the title: The TSL object drawIndex only works with WebGL 2 right now and only if the draw call was triggered by a multidraw command. Otherwise it is always 0. In WebGPU, drawIndex is currently not supported and triggers an error. When at some point multi-draw-indirect is supported, we can revisit this issue.
That’s great. Yes sounds good on multi draw support. What is the fallback on WebGPU? Is it regular instanced drawing?
if so, will instanceIndex work as intended?
Mugen87
November 7, 2025, 11:28am
5
instanceIndex is used many times in the WebGPU examples and should work as expected.
Just tested. Works as intended. Thanks!