- Home /
Flipping normals with shader - zeroing out
I'm trying to create a properly lit back-face shader. My plan is to use this with cloth objects, so I can apply two materials, one for the front face, and one for the back. I've set it up so the texture displays correctly, I just have one problem - lighting. If I don't change anything, the lighting appears the same as the front face, obviously, which looks odd, so I'm trying to flip the normals on the second material, which should allow the lighting to be processed correctly. Now, I'll admit, I'm not exactly a professional at writing shaders, so I may well be missing something extremely obvious, but this is what I have at the moment:
Shader "Inverted Diffuse" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Front (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Cull Front
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Normal = -o.Normal;
fixed4 c = tex2D(_MainTex, float2(1-IN.uv_MainTex.x,IN.uv_MainTex.y)) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "VertexLit"
}
My issue is that the normals seem to be zeroing out, rather than facing the other direction. I tested this by rendering the normals as colour, and without any modification, they displayed a dull purple, while when multiplied by -1, they displayed black. Can anyone help me to work out how to flip the normals? Obviously, I can't just do it on the mesh, as I need to render the front face, too.
Answer by Hoeloe · Sep 07, 2013 at 10:22 PM
Fixed it! I suspected I might need to do it in a vertex shader (as lighting generally uses the vertex normals for the calculations, unless bump maps are present), but every time I tried, I'd get some warnings and the material wouldn't show. It turns out I just needed to make sure I had set the correct UV coordinates in the vertex shader, for them to be read out in the surface shader. It works perfectly now.
Could you show your completed shader im also stuck on this one?
Your answer
Follow this Question
Related Questions
duplicate and flip mesh in code 5 Answers
Problem of color in custom vertex shader 0 Answers
Shader: Normal Angle 1 Answer
Unity shader vertex displace doesn't work 0 Answers
Fixing normals on projected texture. 0 Answers