mouse ray intersect
Members - Reputation: 114
Posted 10 July 2012 - 04:57 AM
I've got a terrain mesh(es) pre-calculated from a heightmap, and want to change it to generate the height on the fly from the heightmap in a vertex shader.
As its currently pre-calced, i can use standard mouse-ray picking to get my mouse world position, but once the shader is providing the height, how can I still get the mouse world position?
Crossbones+ - Reputation: 2441
Posted 10 July 2012 - 05:18 AM
Crossbones+ - Reputation: 4972
Posted 10 July 2012 - 05:27 AM
Though this is a valid way of doing it (I do it myself to pick objects), it bears the danger of flushing the command queue of the GPU which could result in bad performance. First off restrict the readback only to a very small section (1 pixel) and try to move the read back as far as possible away from the render calls, i.e. just before clearing or refilling the depth buffer again in the next frame.
You could read back the depth buffer value of the pixel under the cursor to get Z, and use that to unproject the point and get the world position.
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
Members - Reputation: 416
Posted 10 July 2012 - 06:37 PM
If it's by texture, then I'd say you could read back that value from the texture rather than from the depth buffer. If it's by noise functions, then you'd probably have to do it via a sampling of the depth buffer.
OR, if using a texture for the heightmap, you could set up a post-processing pipeline and include an "overlay" texture which represents any user interaction. This way you could adjust the size of the "brush" that offsets the terrain. You'd do an RTT that combines the current heightmap with the overlay texture (which varies depending on if the user clicked, brush selection, etc.) to render to another heightmap. Then you ping pong back and forth between the heightmaps so that you can update the values seen by the vertex shader.