Soft Particles Vanishing in the Distance

Started by
6 comments, last by skyemaidstone 6 years, 11 months ago

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

Advertisement

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.

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.

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.

Hmm thats an interesting function thanks.

But how do i get linear depth? Currently i get depth by doing z/w in the pixel shader

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.

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.

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

This topic is closed to new replies.

Advertisement