- Home /
Getting the index of a triangle in shader?
Hello.
I'm kinda new to shader programming and I'm writing a shader where i would like to send custom data (1 byte) accompanying each triangle in the mesh.
Basically I want to have an array of data that is the same size as the number of triangles in the mesh, and then read that data in the shader based on which triangle is currently being drawn.
The problem here is that I need to know the index of a particular triangle in the shader, is that possible at all?
thx
Answer by BastianUrbach · Sep 06, 2019 at 09:07 AM
You can use the SV_PrimitiveID semantic. This worked in a quick test but I'm not 100% certain if it does on all platforms. I also don't know if there is a way to do this in a surface shader (since you don't write the fragment shader directly in that case but only a small part of it).
float4 frag (v2f i, uint triangleID: SV_PrimitiveID) : SV_Target {
// fragment shader code
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612211703im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Advanced Shader Help 0 Answers
How to set shader vertex normals to face up 1 Answer
EnableKeyword("_EMISSION") not working 2019.3.13f1 0 Answers
Only render stencil mask when behind maskable object 0 Answers
Silhouette overlay shader 0 Answers