- Home /
Double sided pass?
Hi everyone,
I have this shader but - mainly due to my limited knowledge of shader programming - don't know why it is not working as "double sided".
Currently, whenever I flip the sprite object, the reverse side is unlit. To my knowledge Cull Off should do it, but it is not working. Is is possible at all with the current code?
Shader "Sprites/Diffuse"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.5
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend One OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf Lambert alpha addshadow vertex:vert
#pragma multi_compile DUMMY PIXELSNAP_ON
sampler2D _MainTex;
fixed4 _Color;
struct Input
{
float2 uv_MainTex;
fixed4 color;
};
void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
v.vertex = UnityPixelSnap (v.vertex);
#endif
v.normal = float3(0,0,-1);
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color * _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb * c.a;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/Cutout/VertexLit"
}
Argh... Culling is working, I just need to light the back side of the sprite - it is currently unlit!
Hi, so did you successfully make the back lit? I also have the same problem, where I want my sprite to be double sided and both can lit by light and have shadows, would you $$anonymous$$d to share your shader here?
Answer by Bunny83 · Oct 21, 2014 at 03:34 PM
When you disable culling it will render both sides, however the vertex attributes exits only once, so your vertex normals can only point in one direction. That means they are wrong for the other side (usually the back-side)
A common way to write a double sided shader is to implement it as two-pass shader and do back face culling in one pass and front-face culling in the second pass. The second pass would of course use the inverted normal for lighting.
Another way is to use the camera-to-object vector and the objects normal vector to figure out which side is visible and invert the normal if it's pointing in the same direction as your cam-to-obj vector.
Usually it's way easier and more versatile to simply add the back face in the mesh. So if it's a quad you would use 2 quads one that faces the front and one that faces the back each with correct normals. That's why "cull off" shaders are usually unlit shaders like in particle systems.
Since I posted this I have been learning a lot about shaders and you basically validate everything I learned (yey). So now my question is: in terms of performance, is it better to do the 2 quads or do it dynamically in the shader through cam-to-obj?