- Home /
Transparent shader disappears with multiple objects
I'm trying to write a transparent billboard shader that mimics a plane always aiming at the camera. The shader works on one object, but when two objects with the same shader are both in view they disappear at most angles.
VertexOutput vert(VertexInput i)
{
VertexOutput o;
float3 center = mul(_Object2World, float4(0,0,0,1));
float3 eye = _WorldSpaceCameraPos - center;
float3 right = normalize(cross(eye, float3(0, 1, 0)));
float3 up = normalize(cross(right, eye));
o.pos = float4(center, 1);
o.pos += i.pos.x + float4(right, 0);
o.pos += i.pos.y + float4(up, 0);
o.pos = mul(UNITY_MATRIX_VP, o.pos);
o.col = i.col;
o.tex = i.tex;
return o;
}
After trying to debug for a while I think the part that's causing this issue is the part that updates the world position with the right and up vectors:
o.pos += i.pos.x + float4(right, 0);
o.pos += i.pos.y + float4(up, 0);
I would appreciate any help with this.
I$$anonymous$$HO, shaders don't talk between each other. There's no way a line of shader code can "carry over" to the next object using that shader, and mess it up.
Are you hand-setting _Object2World, or some other uniform shader var in Unity? I'd guess with two objects, the order is messed-up. For fun, try moving the objects near and apart.
I had this on a scene of $$anonymous$$e where two transparent objects were really close and one was a bit higher. They would glitch out. I fixed that by making a small script that changed the material's render Queue.
I'm not setting any uniform variables for this shader.
The proximity of the objects doesn't seem to be causing the issues, its when they're both on the screen at the same time that they disappear under certain angles.
I've tried changing the render queue and indeed this solves the problem. But correct me if I'm wrong, but this requires a different shader for every object. This is unfortunately not possible in my case as I have many objects with this shader in my scene.
Thanks for the input!
Hey did you find an answer? If so, I would appreciate if you can explain. I have exactly the same problem with billboard shader on transparent textures. Although I am not sure if it is related to their being transparent or not. My shader doesn't work if I use the same material for multiple objects with the same texture.
Any help greatly appreciated.
Answer by Glenn-Korver · Jul 04, 2021 at 10:11 PM
You might be running into issues due to "Dynamic Batching" being enabled (which is the default). Try disabling it. https://docs.unity3d.com/Manual/SL-SubShaderTags.html
Shader "ExampleShader" {
SubShader {
Tags { "DisableBatching" = "false" }
Pass {
…
}
}
}
For reference, your example is backwards. "DisableBatching" *IS* false by default, so you would want to make it true instead.
Tags { /* etc. */ "DisableBatching" = "false" }
Your answer
Follow this Question
Related Questions
How to use the Unity 5 Transparent Shader? 1 Answer
Adding transparency to vertex shader? 0 Answers
Unity 5 Global fog with transparency issues 2 Answers
How do I add transparency to a cg vertex lit shader 2 Answers
How to compare colors in Shaders? 2 Answers