2012-09-11 40 views
0

我想AABB colision所以我做一個相同大小的立方體MESH 我試試這個。 但它工作不好。WEBGL Three.js如何創建網格大小相同的BOX?

zmesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( { /*map: THREE.ImageUtils.loadTexture(filepath)*/ } )); 
zmesh.position.set(x, y, z); 
zmesh.scale.set(s, s, s); 
zmesh.rotation.x = - Math.PI/2; 
scene.add(zmesh); 
Object.push(zmesh); 

var BOX = new THREE.Mesh(new THREE.CubeGeometry(10, 10, 10), new THREE.MeshLambertMaterial({ color : 0xFFF0FF })); 
BOX.position.set(x, y, z); 
scene.add(BOX); 
COLLISION.push(BOX); 

如何創建網格框大小相同?????

+0

我還有一個問題。我如何設置邊界框的使用幾何? – user1658740

回答

0

這是我使用的editor的代碼:

[歷史參考文獻(R50)]

https://github.com/mrdoob/three.js/blob/r50/gui/js/ui/Viewport.js#L34-38

var selectionBox = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true })); 
selectionBox.geometry.dynamic = true; 
selectionBox.matrixAutoUpdate = false; 
selectionBox.visible = false; 
sceneHelpers.add(selectionBox); 

https://github.com/mrdoob/three.js/blob/r50/gui/js/ui/Viewport.js#L141-183

var geometry = object.geometry; 

if (geometry.boundingBox === null) { 

    geometry.computeBoundingBox(); 

} 

selectionBox.geometry.vertices[ 0 ].x = geometry.boundingBox.max.x; 
selectionBox.geometry.vertices[ 0 ].y = geometry.boundingBox.max.y; 
selectionBox.geometry.vertices[ 0 ].z = geometry.boundingBox.max.z; 

selectionBox.geometry.vertices[ 1 ].x = geometry.boundingBox.max.x; 
selectionBox.geometry.vertices[ 1 ].y = geometry.boundingBox.max.y; 
selectionBox.geometry.vertices[ 1 ].z = geometry.boundingBox.min.z; 

selectionBox.geometry.vertices[ 2 ].x = geometry.boundingBox.max.x; 
selectionBox.geometry.vertices[ 2 ].y = geometry.boundingBox.min.y; 
selectionBox.geometry.vertices[ 2 ].z = geometry.boundingBox.max.z; 

selectionBox.geometry.vertices[ 3 ].x = geometry.boundingBox.max.x; 
selectionBox.geometry.vertices[ 3 ].y = geometry.boundingBox.min.y; 
selectionBox.geometry.vertices[ 3 ].z = geometry.boundingBox.min.z; 

selectionBox.geometry.vertices[ 4 ].x = geometry.boundingBox.min.x; 
selectionBox.geometry.vertices[ 4 ].y = geometry.boundingBox.max.y; 
selectionBox.geometry.vertices[ 4 ].z = geometry.boundingBox.min.z; 

selectionBox.geometry.vertices[ 5 ].x = geometry.boundingBox.min.x; 
selectionBox.geometry.vertices[ 5 ].y = geometry.boundingBox.max.y; 
selectionBox.geometry.vertices[ 5 ].z = geometry.boundingBox.max.z; 

selectionBox.geometry.vertices[ 6 ].x = geometry.boundingBox.min.x; 
selectionBox.geometry.vertices[ 6 ].y = geometry.boundingBox.min.y; 
selectionBox.geometry.vertices[ 6 ].z = geometry.boundingBox.min.z; 

selectionBox.geometry.vertices[ 7 ].x = geometry.boundingBox.min.x; 
selectionBox.geometry.vertices[ 7 ].y = geometry.boundingBox.min.y; 
selectionBox.geometry.vertices[ 7 ].z = geometry.boundingBox.max.z; 

selectionBox.geometry.computeBoundingSphere(); 

selectionBox.geometry.verticesNeedUpdate = true; 

selectionBox.matrixWorld.copy(object.matrixWorld); 

selectionBox.visible = true; 

注:Viewport.js的當前版本(2015-4-30)在the /editor/js directory中找到。

+0

斷開的鏈接... –

相關問題