Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

raycasting so far #2

Closed
peterqliu opened this issue Jan 30, 2017 · 0 comments
Closed

raycasting so far #2

peterqliu opened this issue Jan 30, 2017 · 0 comments

Comments

@peterqliu
Copy link
Owner

Looking for object aircraft whenever mouse moves on the map object. If intersection exists, returns a JS object with the object, and point of intersection

            map.on('mousemove', function(e){

                // scale mouse pixel position to a percentage of the screen's width and height
                mouse.x = ( e.point.x / threebox.map.transform.width ) * 2 - 1;
                mouse.y = - ( e.point.y / threebox.map.transform.height ) * 2 + 1;

                // update the picking ray with the camera and mouse position

                raycaster.ray.origin.setFromMatrixPosition( threebox.camera.projectionMatrix );
                raycaster.ray.direction
                    .set( mouse.x, mouse.y, 0.5 )
                    .unproject( threebox.camera )
                    .sub( raycaster.ray.origin )
                    .normalize();

                // calculate objects intersecting the picking ray
                var intersects = raycaster.intersectObject(aircraft)[0];
                if (!intersects) return;
                var t = map.transform;
                var coords = {};
                ['x','y','z'].forEach(function(d){
                    var mapTransform = t[d] ? t[d] : 0;
                    var adjust = t[d] ? 256 : 0;
                    coords[d] = (intersects.point[d] + mapTransform) * Math.pow(0.5, t.zoom) - adjust    
                })
                
                console.log({object: intersects, intersection: threebox.unprojectFromWorld(coords)})
            })
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant