javascript - Three.js invisible plane not working with raycaster.intersectObject - Stack Overflow

I am trying to make draggable objects, as seen in this example: .htmlThe objects which should be dragga

I am trying to make draggable objects, as seen in this example: .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 );
}

I am trying to make draggable objects, as seen in this example: https://www.script-tutorials./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 );
}
Share Improve this question asked Jan 20, 2016 at 19:21 MrGarrettoMrGarretto 2825 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744383397a4571541.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信