【问题标题】:three.js ray intersection r58three.js 射线相交 r58
【发布时间】:2013-04-21 18:56:14
【问题描述】:

我想在 collada 对象中使用射线检测相交,three.js r58 但是

var intersects = target.intersectObjects(ai);

不为我工作任何人解决这个问题?是 Ray 不支持 r58 中的 intersectObjects() 方法还是代码中有任何错误?那么替代Raycasting的替代方法是什么?

    var sphereMaterial = new t.MeshBasicMaterial({color: 0x333333});
    var sphereGeo = new t.SphereGeometry(2, 6, 6);
    var target;

    function createBullet(obj) {
        if (obj === undefined) {
            obj = cam;
        }
        var sphere = new t.Mesh(sphereGeo, sphereMaterial);
        sphere.position.set(obj.position.x, obj.position.y * 0.8, obj.position.z);

        if (obj instanceof t.Camera) {
            var vector = new t.Vector3(mouse.x, mouse.y, 1);
            projector.unprojectVector(vector, obj);
            target=new t.Ray(obj.position, vector.sub(obj.position).normalize());
        sphere.ray= target;
        }
        else {
            var vector = cam.position.clone();
        target=new t.Ray(obj.position, vector.sub( obj.position ).normalize());
sphere.ray= target;     
}



    sphere.owner = obj;

    bullets.push(sphere);
    scene.add(sphere);

    return sphere;
}

【问题讨论】:

  • 使用THREE.Raycaster。有关升级到当前版本的帮助,请参阅 Migration Wiki。不要依赖网络上的旧教程。参考官方的three.js示例,适用于当前版本,r.58
  • @WestLangley 但是没有官方的collada对象碰撞检测示例
  • 仅过了 3 分钟,您才做出回应。你至少需要尝试一下。首先让它与一个简单的立方体一起工作。然后使用具有子多维数据集的多维数据集。然后是科拉达。按此顺序。
  • @WestLangley 我已经尝试过一切正常,但现在问题是我的 collada 模型看起来完全是黑色的
  • 这与光线投射无关。您需要在这里搜索帖子并研究three.js 示例以查看是否缺少某些内容。如果您仍然遇到问题,请打开一个新问题。

标签: three.js webgl raycasting


【解决方案1】:
var sphereMaterialc = new t.MeshBasicMaterial({color: 0x0099FF});
var sphereGeoc = new t.SphereGeometry(5, 5, 5);
function createBullet3(obj) {
    var obj = cam;
    var spherec = new t.Mesh(sphereGeoc, sphereMaterialc);
    spherec.position.set(obj.position.x, obj.position.y, obj.position.z);

   
     
        var vector = new t.Vector3(mouse.x, mouse.y, 1);
        projector.unprojectVector(vector, obj);

        spherec.ray = new t.Ray(
                obj.position,
                vector.subSelf(obj.position).normalize()
        );
    
    

    
    spherec.id = 'player';
    spherec.owner = 'player';
    spherec.health = 100;
    spherec.lastfive = Date.now();


    bullets.push(spherec);
    scene.add(spherec);
    
    return spherec;

}

【讨论】:

  • 请在问题中说明代码背后的逻辑,正确格式化并最好添加代码 cmets。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 2012-10-21
  • 2016-05-11
  • 2012-02-14
相关资源
最近更新 更多