- Home /
Weird lighting with cel shader
I have started to make a basic Cel shader as one of my first materials, so I am new to writing shaders. My problem is that when ever I move thy camera thy lighting seem to change its direction, flipping, I can't figure out thy relation between thy lighting and thy movement of thy camera which would cause it to change. I have looked all over thy web for a solution or a explanation to my problem.
Here is a GIF showing my problem with thy lighting Also here is my code:
Shader "Unlit/Cel" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_Threshold ("Cell lighting threshold", Range(1.0, 20.0)) = 5.0
_Anbient ("Anbient Brightness", Range(0.05, 0.5)) = 0.2
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 normal : NORMAL;
};
float _Threshold;
float _Anbient;
float4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.normal = mul(float4(v.normal, 0.0), unity_ObjectToWorld).xyz;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.uv);
float3 normalDirection = normalize(i.normal);
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuse = _LightColor0 * max(0.0, dot(normalDirection, -lightDirection));
half4 cel = half4(floor(diffuse*_Threshold) / (_Threshold - 0.5), 1);
return col * saturate(cel + _Anbient) * _Color;
}
ENDCG
}
}
}
Also I am using Unity 2017.3.0f1 Personal 64bit. If any one can figure what my problem is and point me in thy right direction or even help me fix I would be very much appreciative of your help, thanks.
Answer by IrregularWombat · Apr 18, 2018 at 12:01 AM
I ended up finding help on [/r/Unity3D](www.reddit/r/unity3d) I replaced
o.normal = mul(float4(v.normal, 0.0), unity_ObjectToWorld).xyz;
with
o.normal = UnityObjectToWorldNormal(v.normal);
and adding the tag
"LightMode" = "ForwardBase"
And now it all works.