javascript - Determining the camera's visible x,y of an object in the scene -
i trying determine location (x, y, z coordinates ) of object (a boxgeometry) in world respect screen (frustum) , not world. frustum.intersectsobject(…) method returns boolean, want determine quadrant ([-x,y],[x,y],[x,-y],[-x,-y]) box in relative camera (the red lines in screenshot below follow camera , not scene) . right now, works long camera not rotated (room.children[i].position.x
) because camera , world have same coordinates moment rotate camera, not work.
i think things showing in if (frustum.intersectsobject(room.children[i])) {
shouldn't be. (see image below)
i'm new three.js, there easy way have not happened across. if reason there not, easiest way implement such thing? thanks!!
function ondocumentkeydown(event) { event.preventdefault(); var frustum = new three.frustum(); frustum.setfrommatrix(new three.matrix4().multiply(camera.projectionmatrix)); (var = 0; < room.children.length; i++) { if (frustum.intersectsobject(room.children[i])) { var xyz=new three.vector4(); xyz.copy(room.children[i].position).applymatrix4(camera.matrixworldinverse); console.log(i, 'left', frustum.intersectsobject(room.children[i]), xyz.x,xyz.y,xyz.z,xyz.w, room.children[i].material.emissive.gethex()); if (xyz.x < 0 && xyz.y > 0 && (event.keycode === 103 || event.keycode === 13) ) { if (room.children[i].material.emissive.gethex() !== 255) { $('#score').text(score++); room.children[i].material.emissive.sethex(0x00ff00); console.log(frustum); console.log(i, 'what bounding box coordinates of box????'); } } } } }
the vector3.project( camera ) method seems looking for. give position of object in normalized device coordinates, should range -1 1 in both x , y directions.
vector.set( x, y, z ) // or vector.copy( ...) vector.project( camera ) if ( vector.x <= 0 ) { ... }
should true if object in left half of view, though haven't tested it.
Comments
Post a Comment