Horde3D
http://horde3d.org/forums/

Idea for a much faster and accurate PickNode
http://horde3d.org/forums/viewtopic.php?f=8&t=1241
Page 1 of 1

Author:  DiThi [ 06.09.2010, 10:06 ]
Post subject:  Idea for a much faster and accurate PickNode

Draw the scene, without any antialias, light or anything, just each node having a different solid color (the color can be just the node number). Then pick the color of the pixel beneath the mouse cursor. This way you can also pick nodes underneath other objects with alpha maps, just clamp the alpha to be 1 or 0.

Then you can get the 3D position of the pick by drawing the object alone with the RGB colors mapped to XYZ position.

Of course, this can be optimized by rendering only the pixel beneath the cursor. Also each node should have a "pickable" flag.

Is it easy to implement?

Author:  DarkAngel [ 06.09.2010, 12:29 ]
Post subject:  Re: Idea for a much faster and accurate PickNode

Yep this is pretty easy to implement. You can access the current node ID in the pixel shader and encode that number to a 32bit render-target. Then you can read-back that render-target to the CPU and decode the 'color'.

This kind of picking has been used a bit for some tools I know of (where you only need one picking operation every few seconds), but it's too slow to use in a game. Rendering the scene incurs quite a large CPU-side cost, and reading back data from the GPU often stalls both the CPU and the GPU.

Another common practice is to use a dedicated collision detection library / physics engine for ray-casting.

Author:  swiftcoder [ 06.09.2010, 13:59 ]
Post subject:  Re: Idea for a much faster and accurate PickNode

DiThi wrote:
Is it easy to implement?
It can still be quite tricky to get right. In particular, you need to be very careful about precision and colour space conversions - passing your colour buffer through an sRGB conversion, or an 8-bit colour buffer, may heavily change the colours, to the point that they do not match the colours you specified.

Author:  DiThi [ 06.09.2010, 15:34 ]
Post subject:  Re: Idea for a much faster and accurate PickNode

The scene is rendered 10-60 times per second anyway. What if the shader just colors the pixel underneath the cursor instead of rendering the scene again? Then you read the pixel before drawing the cursor and flipping. This way you could have a pick every frame.

edit: Is it possible to have the x/y coordinates of the screen in the fragment shader?
edit2: oh, gl_FragCoord

Author:  swiftcoder [ 06.09.2010, 15:58 ]
Post subject:  Re: Idea for a much faster and accurate PickNode

DiThi wrote:
The scene is rendered 10-60 times per second anyway. What if the shader just colors the pixel underneath the cursor instead of rendering the scene again?
Doesn't work like that - to determine the colour to make the pixel under the cursor, you have to render all pickable geometry.

This technique works fine for simple demos, but or anything serious I would recommend integrating a collision/physics library to handle this sort of thing...

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/