Sign in to follow this  
skyemaidstone

Soft Particles Vanishing in the Distance

Recommended Posts

    Hi Guys,

I'm having a problem with my soft particles.

They look great and nice and "soft" and blend into the floor/geometry but since they use the depthbuffer to determine how soft/faded they are then they vanish if you go too far away and are more faint the further you are away. 

This looks a bit weird if you look at a misty graveyard from the distance its "spooky mist" free and when you get closer to it is covered in the stuff. 

Hopefully someone has a fix for my shader.

    PixelShaderOutput output;
    output.Color = tex2D(texSampler, input.UV);

    //sample depth from depth map
    //map the clip space coords (input.Position) to texture space 
    float2 depthTex = input.Pos.xy / input.Pos.w;
    depthTex.y = -depthTex.y;
    depthTex.xy = depthTex.xy / 2  + 0.5;

    float depthFromMap = tex2D(DepthMapSampler, depthTex).r;

    float depthOfPixel = input.Depth.x; //input.Depth.y;

	// this makes the particle soft or not
	float scale = 20.0f;
	float4 fade=saturate(((depthFromMap-depthOfPixel))*scale);


	output.Color.rbga *= fade;

	return output;
}

Thanks

Share this post


Link to post
Share on other sites

If your depth buffer isn't linear, then the depth distance between things at a unit distance from eachother in the world, will become shorter as you get further away from the camera. ( https://developer.nvidia.com/content/depth-precision-visualized the article isn't particularily about this, but the figures illustrate well what happens here. ) So effectively your particles get "closer" to other stuff, that would trigger the softening the furter away from the camera they are. The fastest fix in this case would be linearizing the two depths before you compare them.

Share this post


Link to post
Share on other sites
Thanks. That makes sense. The article maths got a bit confusing for me. Right now im storing depth by passing a float2 z and w to the pixel shader and then dividing z/w. This is a log depth maybe? Not sure of the exact name.

So whats the easiest way of getting a linear depth? Ive read few article but they seems little conflicting. Also is it too late to convert my non linear depth to linear once its stored in the depth map texture?

Thanks for your help so far. This problem has been bugging me for days.

Share this post


Link to post
Share on other sites

You can convert the depth from depth map like that :

float DepthToViewZ(in float DepthValue)
{
  return Projection[3][2] / (DepthValue - Projection[2][2]);
}

You simply have to use the same projection matrix used to render the depth map.
Another solution is to simply store the view-space depth in a render target, but since it's already stored in the depth map, better to use it.

Edited by Alundra

Share this post


Link to post
Share on other sites

You take the depth from the depth map then you put this value in the "DepthValue" param of this function and that converts the value.

float depthFromMap = DepthToViewZ( tex2D( DepthMapSampler, depthTex ).r );

To render in the depth map you can avoid the pixel shader, simply use the vertex shader.

Edited by Alundra

Share this post


Link to post
Share on other sites

I can't seem to get that to work.

Just as a test i switched my terrain shader and soft particle shader to both calculate/store the depth using

output.Depth.x = output.Position.z * output.Position.w / 20000.0f;

(20000 being a hardcoded farclip for now) and bingo it worked great. Mist appears nicely in the distance and close up.

But I don't really want to switch everything to using linear depth ideally.

I switched it to just my soft particle shader calculating depth as the line of code above then the Passing the depth into the function above but it didn't work. I'm not sure what's wrong at all though. My depth buffer depth is just a z/w. I'm missing something. 

Share this post


Link to post
Share on other sites
Ok i got it. Multiplying the value from my depthmap back by w then doing the line to use linear depth from my post above worked perfectly.

Now i must resist the urge to endlessly fidding with the size, brightness, scale of my ground mist and actually get on with something that someone besides me will care about!

Thanks guys

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this