#132 open
lindsay.kay (at xeolabs)

Find better color-to-object mapping for GLSL picking

Reported by lindsay.kay (at xeolabs) | July 28th, 2010 @ 05:12 AM

At the moment the Picking Module computes pick shader colour for each object (node) thus:

  color.g = parseFloat(Math.round((nodeIndex + 1) / 256) / 256);
  color.r = parseFloat((nodeIndex - color.g * 256 + 1) / 256);
  color.b = 1.0;

For each object (node) rendered, that colour is loaded into a singleton picking fragment shader:

uniform vec3 uPickColor;
    void main(void) {
            gl_FragColor = vec4(uPickColor.rgb, 1.0);
    }

Once frame is rendered, the colour is read from the framebuffer:

 function readPickBuffer() {
        var context = boundPickBuf.canvas.context;
        var pix = context.readPixels(pickX, boundPickBuf.canvas.canvas.height - pickY, 1, 1, context.RGBA, context.UNSIGNED_BYTE);
        if (!pix) {  //  http://asalga.wordpress.com/2010/07/14/compensating-for-webgl-readpixels-spec-changes/
            pix = new WebGLUnsignedByteArray(4);
            context.readPixels(pickX, boundPickBuf.canvas.height - pickY, 1, 1, context.RGBA, context.UNSIGNED_BYTE, pix);
        }
        if (debugCfg.logTrace) {
            SceneJS._loggingModule.info("Reading pick buffer - picked pixel(" + pickX + ", " + pickY + ") = {r:" + pix[0] + ", g:" + pix[1] + ", b:" + pix[2] + "}");
        }
        pickedNodeIndex = (pix[0] + pix[1] * 256) - 1;
    }

However the pickedNodeIndex does not always map to the original nodeIndex, probably due to inaccuracies in numeric translation between JS and GLSL.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

SceneJS provides easy access to WebGL through a simple and declarative JavaScript API. The SceneJS API is functional, which enables its scene definitions to be really compact and expressive, while hooking into other JavaScript code just that little bit more smoothly.

Shared Ticket Bins

People watching this ticket

Pages