加载Obj文件和测量工具

时间:2016-03-08 01:45:12

标签: three.js

我成功加载了一个obj文件,但我试图通过用两个点标记3d模型并在它们之间画一条直线来添加一个测量工具。像Potree这样的东西(不幸的是,它不支持.obj文件)。怎么能这样做因为我使用three.js。

1 个答案:

答案 0 :(得分:5)

  1. 添加mousedown监听器
  2. camera到鼠标位置的光线投射
  3. 将交叉点A,B保存为Vector3
  4. 计算A,B
  5. 之间的距离

    缩短代码:

    function onDocumentMouseDown( event ) {
        var vector = new THREE.Vector2();
        vector.set(
            ( event.clientX / window.innerWidth ) * 2 - 1,
            - ( event.clientY / window.innerHeight ) * 2 + 1 );
    
        var raycaster = new THREE.Raycaster();
        raycaster.setFromCamera( vector, camera );  
    
        var intersects = raycaster.intersectObjects( model.children );
    
        if ( intersects.length > 0 ) {
    
            pointA = intersects[ 0 ].point;
            // some logic to save pointB
            // on the second click...
            //pointB = intersects[ 0 ].point;
    
            var distance = pointA.distanceTo( pointB );
    
            // draw Line
            var geometry = new THREE.Geometry();
            geometry.vertices.push( pointA );
            geometry.vertices.push( pointB );
            var material = new THREE.LineBasicMaterial();
            line = new THREE.Line( geometry, material );
            scene.add(line);
    
        }
    }
    

    工作示例,距离输出在控制台中:http://jsfiddle.net/ev3tuLuc/95/