The solution provided is effective for pyramids with equal sides at the base. However, if you require a pyramid with a rectangular base, you can follow these steps:
var geometry = new THREE.Geometry();
geometry.vertices = [
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 0, 1, 0 ),
new THREE.Vector3( 1, 1, 0 ),
new THREE.Vector3( 1, 0, 0 ),
new THREE.Vector3( 0.5, 0.5, 1 )
];
geometry.faces = [
new THREE.Face3( 0, 1, 2 ),
new THREE.Face3( 0, 2, 3 ),
new THREE.Face3( 1, 0, 4 ),
new THREE.Face3( 2, 1, 4 ),
new THREE.Face3( 3, 2, 4 ),
new THREE.Face3( 0, 3, 4 )
];
You now have a pyramid geometry with a base of 1 x 1
and a height of 1
. To adjust the dimensions to your desired width
/length
/height
combination, apply a scaling matrix like this:
var transformation = new THREE.Matrix4().makeScale( width, length, height );
geometry.applyMatrix( transformation );
To simplify usage, encapsulate this process in a custom Pyramid
geometry class as shown below:
new THREE.Pyramid( width, length, height );