forge中自己加入的元素怎么通过鼠标选中

forge中自己加入的元素怎么通过鼠标选中

阅读 3.7k
3 个回答

利用Raycaster的方法,但是这个跟three.js的方法不太一样,需要做一些设置,从源码得出这个方法,需要对透视相机和正交相机做出区别:

    
function intersectObjects(pointer,camera,obj) {
    var domElement = viewer.impl.canvas;
    var pointerVector = new THREE.Vector3();
    var pointerDir = new THREE.Vector3();
    var ray = new THREE.Raycaster();
    var rect = domElement.getBoundingClientRect();
    var x = ( ( pointer.clientX - rect.left ) / rect.width ) * 2 - 1;
    var y = - ( ( pointer.clientY - rect.top ) / rect.height ) * 2 + 1;

    if (camera.isPerspective) {
        pointerVector.set(x, y, 0.5);
        pointerVector.unproject(camera);
        ray.set(camera.position, pointerVector.sub(camera.position).normalize());
    } else {
        pointerVector.set(x, y, -1);
        pointerVector.unproject(camera);
        pointerDir.set(0, 0, -1);
        ray.set(pointerVector, pointerDir.transformDirection(camera.matrixWorld));
    }
    
    var intersections = ray.intersectObjects( obj, true );
    return intersections[0] ? intersections[0] : false;
}

新手上路,请多包涵
       window.onclick = () => {
        var camera = NOP_VIEWER.getCamera();
        var pointerVector = new THREE.Vector3();
        var pointerDir = new THREE.Vector3();
        var x = (window.event.clientX / window.innerWidth) * 2 - 1;
        var y = - (window.event.clientY / window.innerHeight) * 2 + 1;
        if (camera.isPerspective) { //判断透视相机和正交相机
            pointerVector.set(x, y, 0.5);
            pointerVector.unproject(camera);
            var raycaster = new THREE.Raycaster(camera.position, pointerVector.sub(camera.position).normalize());
        } else {
            pointerVector.set(x, y, -1);
            pointerVector.unproject(camera);
            pointerDir.set(0, 0, -1);
            var raycaster = new THREE.Raycaster(pointerVector, pointerDir.transformDirection(camera.matrixWorld));
        }
        // 获取raycaster直线和所有模型相交的数组集合
        var objs = NOP_VIEWER.impl.scene.children;
        var intersects = raycaster.intersectObjects(objs, true);
        console.log(intersects);
        if (intersects.length > 0) {
            intersects[0].object.material.color.set(0xff0000);
        }
    }
    
    您好,按照这样写后,点击物体会返回空数组。但是点击某个角度后,会把所有objs中的物体都上色。返回的是
    

clipboard.png

新手上路,请多包涵

你好,请问是如何实现在forge中加入自己的元素的呢?我根据https://forge.autodesk.com/bl...,在模型内并没有生成对应的图形,但是程序也并没有报错,该生成的图形对象也都有。
请问您是怎么实现的在forge中添加元素的呢?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进