whs
Version:
Super-fast 3D framework for Web Applications & Games. Based on Three.js
142 lines (130 loc) • 3.34 kB
JavaScript
import {
Mesh,
PolyhedronBufferGeometry,
PolyhedronGeometry
} from 'three';
import {MeshComponent} from '../../core/MeshComponent';
const [verticesOfCube, indicesOfFaces] = [
[
-1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1,
-1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1
],
[
2, 1, 0, 0, 3, 2,
0, 4, 7, 7, 3, 0,
0, 1, 5, 5, 4, 0,
1, 2, 6, 6, 5, 1,
2, 3, 7, 7, 6, 2,
4, 5, 6, 6, 7, 4
]
];
/**
* @class Polyhedron
* @category components/meshes
* @description In elementary geometry, a polyhedron is a solid in three dimensions with flat polygonal faces, straight edges and sharp corners or vertices.
* <br/><br/>
* `Polyhedron` creates a Polyhedron by its `radius` and `detail`.
* <br/><br/>
* @param {Object} [params] - The params.
* @extends module:core.MeshComponent
* @memberof module:components/meshes
* @example <caption>Creating an Polyhedron, and adding to app</caption>
* new Polyhedron({
* geometry: {
* radius: 2,
* detail: 1
* },
*
* material: new THREE.MeshBasicMaterial({
* color: 0xffffff
* }),
*
* position: [0, 100, 0]
* }).addTo(app);
*/
class Polyhedron extends MeshComponent {
static verticesOfCube = verticesOfCube;
static indicesOfFaces = indicesOfFaces;
/**
* Default values for parameters
* @member {Object} module:components/meshes.Polyhedron#defaults
* @static
* @default <pre>
* {
* geometry: {
* verticesOfCube: [
* -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1,
* -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1
* ],
*
* indicesOfFaces: [
* 2, 1, 0, 0, 3, 2,
* 0, 4, 7, 7, 3, 0,
* 0, 1, 5, 5, 4, 0,
* 1, 2, 6, 6, 5, 1,
* 2, 3, 7, 7, 6, 2,
* 4, 5, 6, 6, 7, 4
* ],
*
* radius: 6,
* detail: 2
* }
* }
* </pre>
*/
static defaults = {
...MeshComponent.defaults,
geometry: {
verticesOfCube,
indicesOfFaces,
radius: 6,
detail: 2
}
};
/**
* Instructions
* @member {Object} module:components/meshes.Polyhedron#instructions
* @static
* @default <pre>
* {
* geometry: ['verticesOfCube', 'indicesOfFaces', 'radius', 'detail']
* }
* </pre>
*/
static instructions = {
...MeshComponent.instructions,
geometry: ['verticesOfCube', 'indicesOfFaces', 'radius', 'detail']
};
constructor(params = {}) {
super(params, Polyhedron.defaults, Polyhedron.instructions);
if (params.build) {
this.build(params);
super.wrap();
}
}
/**
* @method build
* @description Build lifecycle creates a mesh using input params.
* @param {Object} params Component parameters.
* @return {THREE.Mesh} Built mesh
* @memberof module:components/meshes.Polyhedron
*/
build(params = this.params) {
const {geometry, material} = this.applyBridge({
geometry: this.buildGeometry(params),
material: params.material
});
return this.applyBridge({mesh: new Mesh(geometry, material)}).mesh;
}
buildGeometry(params = {}) {
return new (params.buffer ? PolyhedronBufferGeometry : PolyhedronGeometry)(
params.geometry.verticesOfCube,
params.geometry.indicesOfFaces,
params.geometry.radius,
params.geometry.detail
);
}
}
export {
Polyhedron
};