试图获得光线投射器的交点 [英] Trying to get the point of intersection of a raycaster

查看:26
本文介绍了试图获得光线投射器的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我将 frontObject 加载到场景中.我得到了所述物体的中心.

I load frontObject onto the scene. I get the centre of said object.

然后我通过设置一个新点移动到对象之外,该点重用除 Z 轴之外的所有对象的中心坐标(z 坐标变为 100),这样我就可以从角色外部(frontObject)向并获取角色背面的交点,以便我可以在该点上放置盾牌.

Then I move beyond the object by setting a new point that reuses all the object's centre coordinates except the Z axis (the z coordinate becomes 100), so that I can trace a raycaster from the outside of the character (frontObject) towards it and get the intersection point on the back of the character so that I may place a shield on that point.

遗憾的是,我得到以下输出:

Sadly, I get the following output:

代码:

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;

}

}

https://jsfiddle.net/Username100/y54kpe1h/66/>

推荐答案

您的代码有些错误.您需要阅读并解决您发布的警告.

Your code is somewhat buggy. You'll want to read and address the warnings that you posted.

对于 var intersects = raycaster.intersectObjects(frontObject);frontObject 需要是一个类似 [frontObject] 的数组,或者你需要使用单数 intersectObject.

For var intersects = raycaster.intersectObjects(frontObject); frontObject needs to be an Array like [frontObject] or you need to use the singular intersectObject.

我还建议使用 chrome 内置的调试器来设置断点,而不是使用 console.log('POINT ,然后看看你从光线投射中得到了什么.

I also recommend using a debugger like the one built in to chrome to put a break point, instead of console.log('POINT , and then seeing what you're getting back from the raycast.

让我知道这是否有帮助...

Let me know if this helps...

这篇关于试图获得光线投射器的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆