Fog shader camera
Asked Answered
W

1

0

I have some difficulties with my vertex-fragment fog shader in Unity. I have a good visual result but the problem is that the gradient is based on the camera's position, it moves as the camera moves. I don't know how to fix it.

Here is the shader code.

struct v2f {
    float4 pos : SV_POSITION;
    float4 grabUV : TEXCOORD0;
    float2 uv_depth : TEXCOORD1;
    float4 interpolatedRay : TEXCOORD2;
    float4 screenPos : TEXCOORD3;
};

v2f vert(appdata_base v) {
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    o.uv_depth = v.texcoord.xy;
    o.grabUV = ComputeGrabScreenPos(o.pos);
    half index = v.vertex.z;
    o.screenPos = ComputeScreenPos(o.pos); 
    o.interpolatedRay = mul(UNITY_MATRIX_MV, v.vertex);
    return o;
}

sampler2D _GrabTexture;

float4 frag(v2f IN) : COLOR {
    float3 uv = UNITY_PROJ_COORD(IN.grabUV);
    float dpth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, uv));
    dpth = LinearEyeDepth(dpth);
    float4 wsPos = (IN.screenPos + dpth * IN.interpolatedRay); // Here is the problem but how to fix it
    float fogVert = max(0.0, (wsPos.y - _Depth) * (_DepthScale * 0.1f));
    fogVert *= fogVert; 
    fogVert = (exp (-fogVert));
    return fogVert;
}
Wardmote answered 14/6, 2013 at 12:13 Comment(4)
Isn't that how fog is suposed to work?Cheroot
No the actual behaviour is that if I look at the top of my objects, the fog is no more horizontal in world space, it's always based on the camera position.Wardmote
Ah, you want real volumetric fog?Cheroot
Yes, this is what I have but the only issue is the camera problem.Wardmote
W
0

It seems that it's a Matrix problem

o.interpolatedRay = mul(UNITY_MATRIX_MV, v.vertex);
Wardmote answered 14/6, 2013 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.