mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
99.32k stars 35.12k forks source link

Point in 3d place #1024

Closed BetweenGoodAndEvil closed 12 years ago

BetweenGoodAndEvil commented 12 years ago

Hi, im new to three.js. Im trying to get the mouse z coordinate trough raycasting the with a plane on the scene ( which i took from the examples) and i get an intersection. However 1 - the intersection is only halfway the plane (which is weird) 2 - i cant get the z position by "mouseZ = INTERSECTED.point.z ;" this stayes 0; The parts of the code that affect this are included. Could you please help me out?

regards Teo

var projector = new THREE.Projector();
var INTERSECTED;

function findZ(x,y)
    {
        var intersected;
        var v = new THREE.Vector3(x,y, 0.5);
        projector.unprojectVector(v, camera);
        var ray = new THREE.Ray(camera.position, v.subSelf(camera.position).normalize());
        var intersects = ray.intersectObjects( objects );
        if (intersects.length > 0) 
            {
                mouseIntersect = true;
                INTERSECTED = intersects[ 0 ].object;
                INTERSECTED.material.color.setHex( 0xff0000 );
                // show the intersected object more clearly by changing color
                mouseZ = INTERSECTED.point.z ;
            }
        else    
            {
                mouseIntersect = false;
                mouseZ="broken";
            }
    }   

var objects = [];
var material;       
var planeW = 90;
var planeH = 90;
var testplane;

function createandaddplane()
    {   
        testplane = new THREE.Mesh( new THREE.PlaneGeometry( planeW, planeH, 3, 3), new THREE.MeshBasicMaterial( { color: 0x555555, wireframe: true } ) );
        testplane.rotation.x = - 90 * Math.PI / 180;
        testplane.name = test;
        scene.add(testplane);
        testplane.visible = false;
        objects.push(testplane);
    }

function onDocumentMouseMove( event ) 
    {
        mouseX = event.clientX;
        mouseY = event.clientY;
        var newx = (mouseX/window.innerWidth)*2-1;
        var newy = -(mouseY/window.innerHeight)*2+1;
        findZ(newx,newy);
        mousebox.innerHTML = newx+ ' ' + newy +' '+ mousedown + mouseZ + ' '+ mouseIntersect;
    }
mrdoob commented 12 years ago

Do you have a live link so we can understand what you're trying to do?