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
}
}
}