2013-04-23 87 views
0

我在three.js中的邊界框有一些奇怪的行爲。 我使用STLLoader和一些模型一切工作正常,但對於他們中的一些框被轉移。 例如:three.js中的邊界框的錯誤座標

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所所以任何幫助讚賞

回答

1

在你drawBoundingBox日常需要

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

你的網添加到場景前。您的魔方創建在0,0,0附近。

+0

謝謝,gaitat!那正是我想要的。只需一個小的補充,可以與縮放配合使用:boundingBoxMesh.translateX(scaleX * bboxCenter.x); – cmd 2013-04-23 13:26:08

+0

哦,還有一個小問題:我如何加載對象,使其中心符合座標系統點(0,0,0)?在我的問題截圖中,您可以看到對象的中心有時從(0,0,0) – cmd 2013-04-23 13:34:01

+0

移位,而不是mesh.position.x = 0,例如mesh.position.x = -bboxCenter.x,並且不要忘記更新您的邊界框位置。 – gaitat 2013-04-23 13:37:30