0

我在 three.js 中的边界框有一些奇怪的行为。我使用 STLLoader,对于某些模型,一切正常,但对于其中一些模型,框已移动。例如:

http://oi37.tinypic.com/35a1y4l.jpghttp://oi34.tinypic.com/4hf4tl.jpg

边界框大小合适,位置为(0,0,0)。相同位置已加载 STL 模型。

这是我的代码:

function stlLoader() {
         var redPhongMaterial = new THREE.MeshPhongMaterial({ color: 0xFFEA32, side: THREE.DoubleSide, ambient:0x000000}); // yellow
            var stlLoader = new THREE.STLLoader();
            stlLoader.addEventListener('load', function (event) {
                    var stlGeometry = event.content;
                    var mesh = new THREE.Mesh(stlGeometry, redPhongMaterial);
                    mesh.scale.set(2, 2, 2);
                    mesh.castShadow = true;
                    mesh.receiveShadow = true;
                    stlGeometry.computeBoundingBox();
                    var boundingBox = mesh.geometry.boundingBox.clone();
                    drawBoundingBox(boundingBox, mesh.scale.x, mesh.scale.y, mesh.scale.z);
                    mesh.position.y = 0;
                    mesh.position.x = 0;
                    mesh.position.z = 0;
        scene.add( mesh );
                    loadComplete();
                } );
            stlLoader.load( ptsfilestoload );
    }

    function drawBoundingBox(box, scaleX, scaleY, scaleZ)
    {
        var length = scaleX * (box.max.x - box.min.x);
        var height = scaleY * (box.max.y - box.min.y);
        var depth =  scaleZ * (box.max.z - box.min.z);
        var boundingBoxGeometry = new THREE.CubeGeometry( length, height, depth );
        for ( var i = 0; i < boundingBoxGeometry.faces.length; i ++ ) 
        {
             boundingBoxGeometry.faces[i].color.setHex( Math.random() * 0xffffff );
        }
        var boundingBoxMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors, transparent: true, opacity: 0.7 } );
        var boundingBoxMesh = new THREE.Mesh( boundingBoxGeometry, boundingBoxMaterial);
        scene.add( boundingBoxMesh );
    }

或者这可能是 STLLoader 的问题?我对 webgl 和 three.js 真的很陌生,所以任何帮助表示赞赏

4

1 回答 1

1

在您的 drawBoundingBox 例程中,您需要

var bboxCenter = box.center ();
boundingBoxMesh .translateX (bboxCenter.x);
boundingBoxMesh .translateY (bboxCenter.y);
boundingBoxMesh .translateZ (bboxCenter.z);

就在您将网格添加到场景之前。您的 Cube 是在 0,0,0 左右创建的。

于 2013-04-23T13:19:21.573 回答