- Home /
Two Color Lighting Shader Not Working
I'm writing a shader that is supposed to check if there is any light. If there is then it gets drawn as highlight color otherwise it gets drawn as shadow color. The two colors aren't supposed to interact with each other but for a reason I can't seem to find in my code they are.
Here's the code:
Shader "Custom/Shader 1" {
Properties {
_ShadowColor("Shadow Color", Color) = (0.0, 0.0, 0.0, 0.0)
_HighlightColor("Highlight Color", Color) = (1.0, 1.0, 1.0, 0.0)
}
SubShader {
Tags {
"Queue" = "Geometry"
"RenderType" = "Opaque"
}
CGPROGRAM
#pragma surface surf DuelColor
#include "UnityCG.cginc"
half4 _ShadowColor;
half4 _HighlightColor;
half4 LightingDuelColor(SurfaceOutput s, half3 lightDir, half atten) {
half4 c;
half NdotL = dot(s.Normal, lightDir);
if (Luminance(s.Albedo * _LightColor0.rgb * (NdotL * atten)) > 0) {
c.rgb = _HighlightColor.rgb;
}
else {
c.rgb = _ShadowColor.rgb;
}
c.a = 0.0;
return c;
}
struct Input {
float4 Color : COLOR;
};
void surf(Input IN, inout SurfaceOutput o) {
o.Albedo = fixed3(1.0, 1.0, 1.0);
}
ENDCG
}
}
Here's what's happening:
As you can see it should be showing up as green and red but instead it's showing up as red and yellow which is a mix of green and red. If someone knows what's happening please tell me. Thank you in advance
Answer by FortisVenaliter · Jun 08, 2016 at 06:37 PM
Okay, so I tried it myself, and was perplexed because it came out perfectly:
Then, I tried adding a new directional light, and I got this:
So, the issue is definitely related to the light setup. From what I can tell, it's running the shader as a separate pass for each light, and adding them together (you're using the deferred rendering path, right?). This is because you are using a surface shader, so there's still other code going on under the hood.
Try basing your shader off the non-surface examples here. That way Unity won't add lighting to what you write, but you lose the GI lighting stuff as well.
Thank you, I had a feeling it had to do with the fact that I was using a surface shader ins$$anonymous$$d of a vertex fragment shader. Thank you very much ^_^
Answer by Thibault-Potier · Nov 07, 2018 at 06:24 PM
Hi Did you managed to achieve what you wanted ? I'm trying to do something similar but i'm a total beginner with shader