- Home /
Distance shader returns strage distance when not at 0,0,0
i have this shader that is suposed to make objects transparent if they are to close to the camera and it works perfectly when the object is at 0,0,0 but for example when the object is at 0,0,40 i get full fade at around 0,0,20 (for this example the variables FadeStartDistance = 0 and FadeDistance = 10)
i also have a problem that the range of the fade is half, ea if i have FadeDistance = 10 then the actual lenght of the fade is 5 meters. i currectly get around this problem by multiplaying the distance by 0.5 but that doesnt seem like the right way and im guessing that the problems are related
EDIT
changed to using the correct matrix to get the worldspace and now it works for normal mashes but the problem now is that when i use it for a particle shader (im using the new 3.5 version) it again only works when its at 0,0,0 if i move it to 0,0,20 it doesnt work at all and if its at 0,0,10 i can see some particles no matter where the camera is
EDIT2
i have preformed some more testing and it seems like the Object2World matrix always is the identity matrix for particle system is there some other matrix i can/should use instead?
Shader "Custom/DistanceFade" { Properties { _MainTex ("Main Tex", 2D) = "white" {} _FadeStartDistance ("Fade Start Distance", Float) = 0 _FadeDistance ("Fade Distance", Float) = 0 _MinFade ("Minimum Fade", Float) = 0 } SubShader { Pass { //Blend SrcAlpha One //Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _FadeStartDistance;
float _FadeDistance;
float _MinFade;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float newAlpha : TEXCOORD1;
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
float3 worldSpaceObjectPos = mul(v.vertex, _Object2World).xyz;
float dist = distance(worldSpaceObjectPos.xyz, _WorldSpaceCameraPos.xyz);
float fade = (dist - _FadeStartDistance) / _FadeDistance;
fade *= fade;
o.newAlpha = clamp(fade, _MinFade, 1.0f);
return o;
}
half4 frag (v2f i) : COLOR
{
fixed4 c = tex2D(_MainTex, i.uv);
//c.w = i.newAlpha;
if(i.newAlpha <= _MinFade)
c = fixed4(1,0,0,1);
else if(i.newAlpha >= 0.99)
c = fixed4(0,1,0,1);
else
c = fixed4(i.newAlpha, i.newAlpha, i.newAlpha, 1.0f);
return c;
}
ENDCG
}
}
}
Answer by Kryptos · Apr 20, 2012 at 10:01 AM
I'm not an expert but are you certain to use the right matrix conversion?
If I'm not mistaking, UNITY_MATRIX_MVP transform the coordinates into screen position, whereas you are checking against world camera position. Can't this work better with UNITY_MATRIX_MV?
Of course at the end, you will need to transform it again to screen position, with:
o.pos = mul(UNITY_MATRIX_P, o.pos);
See here for a definition of the different matrices.
Answer by mrlinds · May 14, 2014 at 07:01 PM
length(mul (UNITY_MATRIX_MV,v.vertex)) is distance of vertex from camera.
To calculate the distance of a vertex to the camera you can use this as well:
length(ObjSpaceViewDir(v.vertex))
Your answer
Follow this Question
Related Questions
Shader distance between WorldSpaceCameraPos and Vertex 1 Answer
shader distance returns larger value further from 0,0,0 0 Answers
Particle system fine in Editor but Wrong in android build 0 Answers
Classic Game Distance masking darkness/fog help 1 Answer
How do I set up custom shader options without killing batching? 0 Answers