- Home /
shader distance returns larger value further from 0,0,0
So this is the origin, 0,0,0 , and the shader I have makes stuff in the background slightly blue:
Shader "Unlit/Diffuse" {
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTexture ("Main Texture", 2D) = "white" {}
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
float4 _LightColor0;
//float4x4 _Object2World;
fixed4 _Color;
sampler2D _MainTexture;
float dist;
v2f vert (appdata IN)
{
v2f OUT;
OUT.pos = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.normal = mul(float4(IN.normal, 0.0), _Object2World).xyz;
OUT.texcoord = IN.texcoord;
return OUT;
}
fixed4 frag (v2f IN) : COLOR
{
float3 normalDirection = normalize(IN.normal);
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuse = _LightColor0.rgb * max(0.0, dot(lightDirection, normalDirection) * -1);
dist = distance(_WorldSpaceCameraPos, mul(_Object2World, IN.pos).xyz);
fixed4 texColor = tex2D(_MainTexture, IN.texcoord);
return lerp(texColor,fixed4(0,0.7,1,1), (dist/10000000));
}
ENDCG
}
}
}
The problem is when I walk about 3000 units in any direction the blue becomes very intense:
So why is the distance not working like it should? Also the few chunks around the origin (0,0,0) are green no matter how far they are from the camera, shouldn't the shader make distant pixels blue?
I'm lost here and don't know what is making this happpen.
screenshot-7-4.png
(227.2 kB)
screenshot-9-3.png
(225.6 kB)
Comment