Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js invisible plane not working with raycaster.intersectObject

I am trying to make draggable objects, as seen in this example: https://www.script-tutorials.com/demos/467/index.html

The objects which should be draggable are in the array objectMoverLines.

I have added a plane to my scene with the following code:

plane = new THREE.Mesh(new THREE.PlaneBufferGeometry(500, 500, 8, 8), new THREE.MeshBasicMaterial({color: 0x248f24, alphaTest: 0}));
plane.visible = false;
scene.add(plane);

The problem occurs under the onDocumentMouseDown function. For some reason, if the planes visibility is set to false (plane.visible = false), then at a certain point, intersectsobjmovers will not be populated. If the plane's visibility is set to true, however, it will work fine (but obviously, that causes a huge plane to be in the way of everything):

function onDocumentMouseDown(event) {
    // Object position movers
    var vector = new THREE.Vector3(mouse.x, mouse.y, 1);
    vector.unproject(camera);

    raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
    var intersectsobjmovers = raycaster.intersectObjects(objectMoverLines);
    if (intersectsobjmovers.length > 0) {
        console.log('clicking an object mover');
        // Disable the controls
        controls.enabled = false;
        // Set the selection - first intersected object
        objmoverselection = intersectsobjmovers[0].object;
        // Calculate the offset
        var intersectsobjmovers = raycaster.intersectObject(plane);

        // At this point, intersectsobjmovers does not include any items, even though
        // it should (but it does work when plane.visible is set to true...)

        offset.copy(intersectsobjmovers[0].point).sub(plane.position);
    } else {
        controls.enabled = true;
    }
}

Also, this is what I currently have under the onDocumentMouseMove function:

function onDocumentMouseMove(event) {
    event.preventDefault();

    mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;

    // Get 3D vector from 3D mouse position using 'unproject' function
    var vector = new THREE.Vector3(mouse.x, mouse.y, 1);
    vector.unproject(camera);

    // Set the raycaster position
    raycaster.set( camera.position, vector.sub( camera.position ).normalize() );

    if (objmoverselection) {
        // Check the position where the plane is intersected
        var intersectsobjmovers = raycaster.intersectObject(plane);
        // Reposition the object based on the intersection point with the plane
        objmoverselection.position.copy(intersectsobjmovers[0].point.sub(offset));
    } else {
        // Update position of the plane if need
        var intersectsobjmovers = raycaster.intersectObjects(objectMoverLines);
        if (intersectsobjmovers.length > 0) {
            // var lookAtVector = new THREE.Vector3(0,0, -1);
            // lookAtVector.applyQuaternion(camera.quaternion);
            plane.position.copy(intersectsobjmovers[0].object.position);
            plane.lookAt(camera.position);
        }
    }

    requestAnimationFrame( render );
}
like image 814
MrGarretto Avatar asked Feb 07 '23 23:02

MrGarretto


1 Answers

try this:

plane = new THREE.Mesh(new THREE.PlaneBufferGeometry(500, 500, 8, 8), 
   new THREE.MeshBasicMaterial( {
       color: 0x248f24, alphaTest: 0, visible: false
}));

scene.add(plane);
like image 75
Almaz Vildanov Avatar answered Feb 11 '23 01:02

Almaz Vildanov