gpt4 book ai didi

javascript - Three.js 不可见平面不适用于 raycaster.intersectObject

转载 作者:搜寻专家 更新时间:2023-11-01 05:10:20 25 4
gpt4 key购买 nike

我正在尝试制作可拖动的对象,如本例所示:https://www.script-tutorials.com/demos/467/index.html

应该可拖动的对象在数组 objectMoverLines 中。

我使用以下代码在我的场景中添加了一架飞机:

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

问题出现在onDocumentMouseDown函数下。出于某种原因,如果平面可见性设置为 false (plane.visible = false),那么在某个点,将不会填充 intersectsobjmovers。但是,如果飞机的可见性设置为 true,它将正常工作(但显然,这会导致巨大的飞机挡住一切):

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

此外,这是我目前在 onDocumentMouseMove 函数下的内容:

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

最佳答案

试试这个:

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

scene.add(plane);

关于javascript - Three.js 不可见平面不适用于 raycaster.intersectObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34908664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com