Jump to content
  • Advertisement
Sign in to follow this  
ajoling

Water / Terrain - Use the stencil buffer?

This topic is 4525 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone :) I just turned my water in something nicer, allowing me to go beyond a single water plane. I use a pixel shader to get the nice water blending at the sides. For this to work, I turn the z-buffer checks off, but the z-write is still enabled. If I turn the Z-buffer checks on, I get very straight corners because of the low-res terrain polygons (the terrain is a few thousand poly's) Now, the problem is that, when the player is at one side of a hill/mountain, and there is water on the other side, the water will shine right through the terrain. I was wondering if it would be possible to use the stencil buffer to solve this, somehow. I haven't had real use for stencil buffers yet in my game developing time. Or maybe a fancy Z comparison check? The Idea is that: The water poly's are not "cut off" by the terrain poly's. But the water should not shine through the terrain hills, when looking from the other side. If this is not clear, I can provide a screenshot :) Many thanks for any suggestions :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Or maybe a fancy Z comparison check?

You could try to play with the depth bias.

Share this post


Link to post
Share on other sites
Hm, I guess I could give that a try. But I thought depthbias was quite driver dependent on it's use/function.

Share this post


Link to post
Share on other sites
I really don't think the bias will do anything in this case, but if you somehow find a way please do let us know.

I am pretty certain you can use the stencil for this, but performance might be questionable. I think you'd have to render all the terrain which is above sea level to the stencil, then again for display, so it's a little expensive. Then use the stencil to filter out where not to render the water. I haven't done it, but its what the Stencil is used for.

You could also use this technique to cut a cave into the side of the terrain for example.

Share this post


Link to post
Share on other sites
I'm not sure if this would work for a 3d water surface, but for a water plane you can blend the reflection out near the edges.

Before:
no fadeout

After:
fadeout

This is done using the heightmap as input to the water pixel shader, and then scaling and clamping it into the range you want, to eventually look like this:

edge factor

I'm still working on getting higher precision, because with a 16 bit heightmap I get a lot of banding. The edge factor is useful for scaling down the reflection and refraction distortion too.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajoling
Hm, I guess I could give that a try. But I thought depthbias was quite driver dependent on it's use/function.

No, that was Z bias, and older version of this functionality. Depth bias is well defined.

Not saying it's the perfect answer, but if you want to say that something close to the real Z shouldn't be culled but something far should, it seems like a good candidate.

Share this post


Link to post
Share on other sites
Quote:
Original post by jamesw
I'm not sure if this would work for a 3d water surface, but for a water plane you can blend the reflection out near the edges.

Before:
no fadeout

After:
fadeout

This is done using the heightmap as input to the water pixel shader, and then scaling and clamping it into the range you want, to eventually look like this:

edge factor

I'm still working on getting higher precision, because with a 16 bit heightmap I get a lot of banding. The edge factor is useful for scaling down the reflection and refraction distortion too.


Hi James,

That's exactly what I am doing too. It creates a very nice effect, but , because I turn off Z bufering, the water shines through. I could ofcourse make the water less "wide" than the terrain/river "gap". but I prefer a different approach.

ET3D: Going to try the depth bias. :)

Share this post


Link to post
Share on other sites
Okay, the depthbias function is weird :) I was using 'big' values like 1-3.0f and it looked really odd. Some obejcts would suddenly be "behind" the water for example.

but. I tweaked it down to 0.0005, and the water is rendered okay. Z-buffer is enabled (no more shine through), and my objects are still fine.

Conclusion: I'm happy :)

Share this post


Link to post
Share on other sites
ajoling, could you post some screens of your 3d water? I'd like to see what you're working on, especially the shorelines.

Share this post


Link to post
Share on other sites
Wow, jamesw, would you be willing to point me in the right direction on how to make an effect like that? I'm pretty clueless on pixel shaders, I'd love to know how that's actually done.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!