function tryToPick (pointer, viewport, fullData) { // warning !!! posible issues with camera-unproject , itself used in other modules https://github.com/Jam3/camera-unproject/issues/1 const viewportWidth = viewport[2] const viewportHeight = viewport[3] // your camera matrices const projection = mat4.perspective([], Math.PI / 4, viewportWidth / viewportHeight, 0.01, 1000) var view = fullData.camera.view var projView = mat4.multiply([], projection, view) var invProjView = mat4.invert([], projView) let ray = { // this data will get mutated to contain data ro: [0, 0, 0], rd: [0, 0, 0] } // store result in ray (origin, direction) pick(ray.ro, ray.rd, pointer.position, viewport, invProjView) return fullData.entities .filter(e => e.meta.pickable) .map(function (entity, index) { return intersect(ray, entity, index) }) .filter(h => h !== null) .concat([]) }
picking({ clientX: x, clientY: y }) { const { gl, verts, } = this.state; const { view, projection: proj } = this.getCamera(); const { width, height } = gl.canvas; const { top, left } = gl.canvas.getBoundingClientRect(); const projView = mat4.multiply(mat4.create(), proj, view); const invProjView = mat4.invert(mat4.create(), projView); const ray = new Ray(); const viewport = [0, 0, width, height]; pick(ray.origin, ray.direction, [x - left, y - top], viewport, invProjView); if (ray.intersectsBox( [ [verts[0], verts[1], verts[2]], [verts[9], verts[10], verts[11]], ] )) console.log('pick'); }