试图获得光线投射器的交点
Trying to get the point of intersection of a raycaster
问题:
我将 frontObject 加载到场景中。我得到了所述对象的中心。
然后我通过设置一个新点移动到对象之外,该点重用除 Z 轴之外的所有对象的中心坐标(z 坐标变为 100),这样我就可以从角色外部跟踪光线投射器(frontObject ) 朝向它并获得角色背面的交点,以便我可以在该点上放置一个盾牌。
遗憾的是,我得到以下输出:
代码:
let box = new THREE.Box3().setFromObject(frontObject);
let sphere = box.getBoundingSphere();
let centerPoint = sphere.center;
backObject.position.set(0,132,-15);
console.log("CENTER POINT X: "+centerPoint.x);
console.log("CENTER POINT Y: "+centerPoint.y);
console.log("CENTER POINT Z: "+centerPoint.z);
centerPoint.z = 100;
var newCoordinate = shootRay(centerPoint, frontObject);
console.log("NEW POINT X: "+newCoordinate.x);
console.log("NEW POINT Y: "+newCoordinate.y);
console.log("NEW POINT Z: "+newCoordinate.z);
function shootRay(center, frontObject) {
var raycaster = new THREE.Raycaster();
var direction = new THREE.Vector3( 0, 0, 1 );
raycaster.ray.direction.copy( direction );
raycaster.ray.origin.copy( center);
var intersects = raycaster.intersectObject(frontObject);
console.log("GO");
if (intersects) {
var point = intersects.point;
console.log("POINT:"+point);
return point;
}
}
编辑:
您的代码有些错误。您需要阅读并解决您发布的警告。
对于 var intersects = raycaster.intersectObjects(frontObject); frontObject 需要是像 [frontObject] 这样的数组,或者您需要使用单数 intersectObject。
我还建议使用 chrome 中内置的调试器来设置断点,而不是 console.log('POINT ,然后查看您从光线投射。
如果有帮助请告诉我...
问题:
我将 frontObject 加载到场景中。我得到了所述对象的中心。
然后我通过设置一个新点移动到对象之外,该点重用除 Z 轴之外的所有对象的中心坐标(z 坐标变为 100),这样我就可以从角色外部跟踪光线投射器(frontObject ) 朝向它并获得角色背面的交点,以便我可以在该点上放置一个盾牌。
遗憾的是,我得到以下输出:
代码:
let box = new THREE.Box3().setFromObject(frontObject);
let sphere = box.getBoundingSphere();
let centerPoint = sphere.center;
backObject.position.set(0,132,-15);
console.log("CENTER POINT X: "+centerPoint.x);
console.log("CENTER POINT Y: "+centerPoint.y);
console.log("CENTER POINT Z: "+centerPoint.z);
centerPoint.z = 100;
var newCoordinate = shootRay(centerPoint, frontObject);
console.log("NEW POINT X: "+newCoordinate.x);
console.log("NEW POINT Y: "+newCoordinate.y);
console.log("NEW POINT Z: "+newCoordinate.z);
function shootRay(center, frontObject) {
var raycaster = new THREE.Raycaster();
var direction = new THREE.Vector3( 0, 0, 1 );
raycaster.ray.direction.copy( direction );
raycaster.ray.origin.copy( center);
var intersects = raycaster.intersectObject(frontObject);
console.log("GO");
if (intersects) {
var point = intersects.point;
console.log("POINT:"+point);
return point;
}
}
编辑:
您的代码有些错误。您需要阅读并解决您发布的警告。
对于 var intersects = raycaster.intersectObjects(frontObject); frontObject 需要是像 [frontObject] 这样的数组,或者您需要使用单数 intersectObject。
我还建议使用 chrome 中内置的调试器来设置断点,而不是 console.log('POINT ,然后查看您从光线投射。
如果有帮助请告诉我...