   Water / Terrain - Use the stencil buffer?
  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 :)   
Quote:Or maybe a fancy Z comparison check?You could try to play with the depth bias.;  Hm, I guess I could give that a try. But I thought depthbias was quite driver dependent on it's use/function.   ;  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.;  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: After: 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: 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.   ;Quote:Original post by ajolingHm, 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.;Quote:Original post by jameswI'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: After: 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: 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. :)   ;  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 :)   ;  ajoling, could you post some screens of your 3d water?  I'd like to see what you're working on, especially the shorelines.   ;  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.   