Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orthographic Camera and pickingRay?

I tried to implement a function to select and move objects in a stage compound of an orthographic camera.

I'd like to get the same example as : http://threejs.org/examples/webgl_interactive_draggablecubes.html

but not with perspective camera.

I already replaced :

var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );

with :

var camera =  new THREE.OrthographicCamera(window.innerWidth / -zoom, window.innerWidth / zoom, window.innerHeight / zoom, window.innerHeight / -zoom, -1000, 1000);

and replaced this :

var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

by this :

var ray = projector.pickingRay( vector, camera );

But, it does not seem to be enough to run.

So, what's it lack ?

Is it possible to use Projector and Ray with OrthographicCamera ??

Thanks.

like image 852
Kyrion Avatar asked Jan 01 '26 08:01

Kyrion


1 Answers

Please try this code in your sample:

event.preventDefault();
    if (event.button === 0) {

        var projector = new THREE.Projector();
        var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
        // use picking ray since it's an orthographic camera
        var raycaster = projector.pickingRay(vector, camera);
        //ditermine whether any of the supplied objects are hit by this ray    
        var intersertsObj = raycaster.intersectObjects(_entities);
        // _entities : is array entities you put to scence
        // you can store this list before you entity to scence: 
        // _entities.push(entity);
        // scene.add(entity);
        if (intersertsObj.length > 0) {           
            var pickedObject = intersertsObj[0];               
        }       
    }
    event = null;

it is works fine in my project.

like image 152
NinhKu Avatar answered Jan 03 '26 20:01

NinhKu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!