How do I fix this diffuse shading flip
I have a frag shader and after a bit of rotation the diffuse light shift from one side to another. My guess is that some value is switching a value from positive to negative in one octrant (I think that is what its called) . I have tried to normalize a number of things but as I am newish to this I a missing something. Perhaps a key search term or a bit of knowledge about a function. I keep researching and learning but so far am unable to figure this out.
I made the shader a bit more extreme to show the difference in the seeming flip of the light direction. Any help or pointers would be greatly appreciated. Thanks
Shader "Unlit/diffuseShader"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_Gloss("Gloss", Range(0,100)) = 1
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag Standard alpha:fade
#pragma target 3.0
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
// Mesh data: vertex position, vertex normal, UVs, tangents, vertex colors
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv0 : TEXCOORD0;
};
struct VertexOutput {
float4 clipSpacePos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float3 normal : TEXCOOD1;
float3 worldPos : TEXCOORDS2;
};
float4 _Color;
float _Gloss;
uniform sampler2D _MainTex;
// Vertex Shader
VertexOutput vert(VertexInput v) {
VertexOutput o;
o.uv0 = v.uv0;
o.normal = v.normal;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.clipSpacePos = UnityObjectToClipPos(v.vertex);
return o;
}
float4 frag(VertexOutput o) : SV_Target{
//return _Gloss;
float2 uv = o.uv0;
float3 normal = normalize(o.normal);//Interpolated
//direct Diffuse light
float3 lightDir = _WorldSpaceLightPos0.xyz;
float3 lightColor = _LightColor0.rgb;
float lightFalloff = max(0, dot(lightDir, normal)) ;
float directDiffuseLight = lightColor * lightFalloff;
// Composite
//float3 diffuseLight = ambientLight + directDiffuseLight;
float3 diffuseLight = directDiffuseLight;
float3 finalSurfaceColor = normalize(diffuseLight * _Color.rgb );
return float4(finalSurfaceColor, 1);
}
ENDCG
}
}
}
seems to be something with this
return float4(_WorldSpaceLightPos0.xyz,1); There is an area that cause it to flipfloat3 lightDir = _WorldSpaceLightPos0.xyz;
Answer by adredengaming · Sep 20, 2019 at 12:52 AM
so adding an abs() can work
float3 lightDir = abs(_WorldSpaceLightPos0.xyz);
Your answer
![](https://koobas.hobune.stream/wayback/20220612212332im_/https://answers.unity.com/themes/thub/images/avi.jpg)