- Home /
How to set shader vertex normals to face up
I am having a lot of trouble with this and haven't been able stumble upon the answer. Here's what I've tried:
void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON)
v.vertex = UnityPixelSnap (v.vertex);
#endif
fixed3 normal = mul(unity_ObjectToWorld, v.normal);
v.normal += abs(normal - fixed3(0,1,0));
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color * _Color * _RendererColor;
}
void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON)
v.vertex = UnityPixelSnap (v.vertex);
#endif
v.normal = fixed3(0,1,0);
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color * _Color * _RendererColor;
}
And others.
I'm trying to do this to get my billboarding sprite to be lit from behind. Other questions asked on this topic are unresolved:
https://answers.unity.com/questions/1468837/how-to-light-a-plane-from-behind.html https://forum.unity.com/threads/lighting-sprites-uniformly-from-behind.686731/
Answer by Namey5 · Dec 30, 2019 at 08:48 AM
Assuming you're using a surface shader, you can just use a custom lighting function that ignores normals altogether and just samples the light's attenuation;
#pragma surface surf Uniform ...
//Then, later in the shader
half4 LightingUniform (SurfaceOutput s, half3 lightDir, fixed atten)
{
half4 c;
c.rgb = (s.Albedo * atten) * _LightColor0.rgb;
c.a = s.Alpha;
return c;
}
This is wonderful, thank you, where did you read about this surface function?
The manual has a few pages about custom lighting models in surface shaders. This one is just a simplified Lambert.
https://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaderLighting.html
https://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaderLightingExamples.html
Yeah, I've read those but there's not anything referenced that follows the lighting model that you used as far as the parameters called or a reference to the Uniform surf directive, which I'm really interested in. Its behavior seems identical to SimpleLambert and I'm also wondering what the difference is between the two. Why are you required (and how did you know) to pass the half3 parameter lightDir?