Three.js: Get 3d coordinates from 2d point

Created on 1 Jul 2011  ·  6Comments  ·  Source: mrdoob/three.js

Hi guys

I would like to know how can i get the 3d coordinates from a collision point mouse 2d
from this code

function onDocumentMouseDown( event ) {
   event.preventDefault();
   mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
   mouse2d.y = - ( event.clientY  / window.innerHeight ) * 2 + 1;
   mouse2d.z = 1;

   var r = new THREE.Ray();
   r.origin.copy( mouse2d );
   var matrix = camera.matrixWorld.clone();
   matrix.multiplySelf( THREE.Matrix4.makeInvert( camera.projectionMatrix ) );
   matrix.multiplyVector3( r.origin );
   r.direction = r.origin.clone().subSelf( camera.position );
   var c = THREE.Collisions.rayCastNearest( r );
   if( c ) {
//////////////////HERE GET THE 3D POINT COORDINATES////////////////////////
      c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
   } else {
      plane.materials[0].color.setHex( 0xF9EFD7 );
   }
};

The collision object is a plane which i use as ground and i want to know the collision point 3d coords in order to set an object's position.
Thanks in advance

Question

All 6 comments

Never mind found it

How did you end up doing it?

I'm having this problem myself. I'm not even get a hit from the collider. How did you solve it?

@inear: did you try with the Ray class?

Thanks! That works like a charm. The point-property is just what I needed.

var vector = mouse2D;
projector.unprojectVector( vector, camera );
var r = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize());
var c = r.intersectObject(wave)
if( c.length > 0 ) {
   sphere.position = c[0].point;
}
var getZvalue = function (e)
{
    var rect = e.target.getBoundingClientRect();
    // 2D
    var mouseX = e.clientX - rect.left;
    var mouseY = e.clientY - rect.top;

    // 3D
    SCREEN_WIDTH = window.innerWidth;
    SCREEN_HEIGHT= window.innerHeight;
    var mouse = new THREE.Vector2();
    mouse.x = (mouseX/SCREEN_WIDTH) *2 - 1;
    mouse.y = -(mouseY/SCREEN_HEIGHT) *2 + 1;

    // Raycaster
    if (TerraDroneViewer.raycaster == null)
    {
        TerraDroneViewer.raycaster = new THREE.Raycaster();
    }

    TerraDroneViewer.raycaster.setFromCamera( mouse, TerraDroneViewer.camera );

    var intersects = TerraDroneViewer.raycaster.intersectObjects( TerraDroneViewer.maps.children );
    console.log("raycast=" + intersects.length);

    if (intersects.length != 0)
    {
        var x = mouse.x * 10;
        var y = mouse.y * 10;
        console.log("x = " + intersects[0].point.x + " y = " + intersects[0].point.y);
        var z = parseFloat(intersects[0].point.z) * 10;

        return {
                     x: x,
                     y: y,
                     z: z
                }
    }

        return null;
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MohmadAyman picture MohmadAyman  ·  3Comments

xgqfrms picture xgqfrms  ·  4Comments

savalin picture savalin  ·  5Comments

hevali picture hevali  ·  3Comments

gogorikidze picture gogorikidze  ·  3Comments