x


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

    }
}

}

more ▼

asked Apr 20 '12 at 08:50 AM

imapler gravatar image

imapler
51 2 2 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 20 '12 at 10:01 AM

Kryptos gravatar image

Kryptos
7.2k 5 33

updated the question

Apr 23 '12 at 04:14 PM imapler
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1738
x371
x111
x36

asked: Apr 20 '12 at 08:50 AM

Seen: 1049 times

Last Updated: Apr 29 '12 at 07:53 AM