来自THREEjs.org/docs提供的示例:
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3( -10, 10, 0 ),
new THREE.Vector3( -10, -10, 0 ),
new THREE.Vector3( 10, -10, 0 )
);
geometry.faces.push( new THREE.Face3( 0, 1, 2 ) );
geometry.computeBoundingSphere();
通过指定三角形的顶点可以非常直接地创建三角形。如果我想继续使用这种方法制作一个四面体,我发现自己重复这个代码三次,一次为四面体的每个面,每次改变一个向量来定义构成四面体的三角形之一...
有没有一种简单的方法,如果我定义四面体的第四个向量,创建一个四面体而不必单独定义每个面?
类似的东西:
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3( -10, 10, 0 ),
new THREE.Vector3( -10, -10, 0 ),
new THREE.Vector3( 10, -10, 0 ),
new THREE.Vector3( 10, 10, 10 ),
);
geometry.faces.push( new THREE.Face3( 0, 1, 2 ) );
geometry.computeBoundingSphere();
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh )
当然这段代码不起作用,最后一个顶点被忽略了。
答案 0 :(得分:3)
在three.js的代码中查看src / extras / geometry / TetrahedronGeometry.js
复制此处的代码以供参考:
THREE.TetrahedronGeometry = function ( radius, detail ) {
var vertices = [ 1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1];
var indices = [ 2, 1, 0, 0, 3, 2, 1, 3, 0, 2, 3, 1];
THREE.PolyhedronGeometry.call( this, vertices, indices, radius, detail );
};
THREE.TetrahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );