Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access the camera in an editor generator page #7510

Closed
CarbonMan opened this issue Nov 2, 2015 · 2 comments
Closed

Access the camera in an editor generator page #7510

CarbonMan opened this issue Nov 2, 2015 · 2 comments

Comments

@CarbonMan
Copy link

I wish to detect when an object has been clicked. I am generating a page from the three.js editor. It is a brilliant environment.

Following the advice from this post (https://mandemeskel.wordpress.com/2013/08/19/mouse-events-raycasting-with-three-js/) I ended up with the following code. I have placed a comment where I am having a problem.

function mousedown( event ) {
    var scene = this.parent;
    var projector = new THREE.Projector();
    var mouse_vector = new THREE.Vector3(),
    mouse = { x: 0, y: 0, z: 1 },
    ray = new THREE.Raycaster( new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0) ),
    intersects = []; 
    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
    mouse_vector.set( mouse.x, mouse.y, mouse.z );

    // How do I get the camera?
    projector.unprojectVector( mouse_vector, camera );

    var direction = mouse_vector.sub( camera.position ).normalize();

    ray.set( camera.position, direction );

    var object =  scene.getObjectByProperty( 'uuid', this.uuid, true );

    intersects = ray.intersectObject( object );

    if( intersects.length ) {
            alert( "hit" );
            event.preventDefault();  
    }
 }

I have temporarily fixed the problem by modifying app.js in the published page. By moving the camera variable to be global I got around it, but it is obviously a fudge. Is there any way to access the camera generated by the editor without modifying app.js?

var camera;
var APP = {
Player: function () {

....

@mrdoob
Copy link
Owner

mrdoob commented Nov 2, 2015

Glad you like the environment! I just added a getCamera(). You should now be able to do:

var camera = player.getCamera();

Thanks!

@mrdoob mrdoob closed this as completed Nov 2, 2015
@mrdoob
Copy link
Owner

mrdoob commented Nov 18, 2015

Just so you know, I've removed player.getCamera() as I'm now passing camera to the script directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants