Hello I spend many hours by trying to get to work simple picking in WebGL. I used before draw every object:
if(Window.stencilEnabled)
gl.stencilFunc(gl.ALWAYS, mesh.pickingID, -1);
And finally:
mouse_x = this.lastPickingCoords.x;
mouse_y = this.lastPickingCoords.y;
var pixels = new Uint8Array(4);
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
gl.readPixels(mouse_x, mouse_y, 1, 1, gl.STENCIL_INDEX, gl.UNSIGNED_BYTE, pixels);
}else{
console.log("Framebuffer error.");
}
oID = -1 + pixels[0];
console.log("Picking object: " + oID + " at " + mouse_x + ":" + mouse_y);
if(this.mapArray[oID] != undefined){
console.log("Object exists!");
console.log(this.mapArray[oID]);
}else{
console.log("Object not found");
}
this.lastPickingCoords.x = 0;
this.lastPickingCoords.y = 0;
But it seems from error "WebGL: INVALID_ENUM: readPixels: invalid format" that reading stencil buffer is not allowed... Is there any workaround, extension? How to do it other way without writing picking shaders? Thank you.