Three.js立方体不显示对角线
2016-05-18 金城 2077
同问:Three.js中创建的立方体只显示它的12条边
描述:用 THREE.BoxGeometry 创建的立方体有对角线,无法去除。
解决方法:其他方法重建模型
// 画直线
functiondraw_line(v1,v2,color){var geometry=new THREE.Geometry();geometry.vertices.push(v1,v2);return new THREE.Line(geometry,new THREE.MeshPhongMaterial({emissive:color,color:color}));}
// 画立方体线
functiondraw_cube(width,height,depth,color){
varhw=(width||200)/2,
hh=(height||200)/2,
hd=depth/2,
c=color||0xff0000;
varcube=newTHREE.Object3D();
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,-hd),newTHREE.Vector3(hw,-hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,hh,-hd),newTHREE.Vector3(hw,hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,hd),newTHREE.Vector3(hw,-hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,hh,hd),newTHREE.Vector3(hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,-hd),newTHREE.Vector3(-hw,-hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,hh,-hd),newTHREE.Vector3(-hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,-hh,-hd),newTHREE.Vector3(hw,-hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,hh,-hd),newTHREE.Vector3(hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,-hd),newTHREE.Vector3(-hw,hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,-hh,-hd),newTHREE.Vector3(hw,hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,hd),newTHREE.Vector3(-hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,-hh,hd),newTHREE.Vector3(hw,hh,hd),c));
return cube;
}
//var box=draw_cube(100,100,100,0x008000);
//scene.add(box);
functiondraw_line(v1,v2,color){var geometry=new THREE.Geometry();geometry.vertices.push(v1,v2);return new THREE.Line(geometry,new THREE.MeshPhongMaterial({emissive:color,color:color}));}
// 画立方体线
functiondraw_cube(width,height,depth,color){
varhw=(width||200)/2,
hh=(height||200)/2,
hd=depth/2,
c=color||0xff0000;
varcube=newTHREE.Object3D();
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,-hd),newTHREE.Vector3(hw,-hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,hh,-hd),newTHREE.Vector3(hw,hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,hd),newTHREE.Vector3(hw,-hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,hh,hd),newTHREE.Vector3(hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,-hd),newTHREE.Vector3(-hw,-hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,hh,-hd),newTHREE.Vector3(-hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,-hh,-hd),newTHREE.Vector3(hw,-hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,hh,-hd),newTHREE.Vector3(hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,-hd),newTHREE.Vector3(-hw,hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,-hh,-hd),newTHREE.Vector3(hw,hh,-hd),c));
cube.add(newdraw_line(newTHREE.Vector3(-hw,-hh,hd),newTHREE.Vector3(-hw,hh,hd),c));
cube.add(newdraw_line(newTHREE.Vector3(hw,-hh,hd),newTHREE.Vector3(hw,hh,hd),c));
return cube;
}
//var box=draw_cube(100,100,100,0x008000);
//scene.add(box);
例如:http://softsrc.cc(请用Firefox,Chrome,Opera,Edge浏览器查看)