whs
Version:
Super-fast 3D framework for Web Applications & Games. Based on Three.js
111 lines (101 loc) • 2.69 kB
JavaScript
import {
Mesh,
ShapeBufferGeometry,
ShapeGeometry
} from 'three';
import {MeshComponent} from '../../core/MeshComponent';
/**
* @class Shape
* @category components/meshes
* @description Shape is a universal class. It allows you to create different 2D shapes in 3D scene.<br/>
* Unfortunately, not all of them support physics, an alternative is to make a similar 3D object and scale its width down to near zero.
* <br/><br/>
* `Shape` consists of shapes that are in its shapes parameter.
* @classDesc
* <iframe src="https://threejs.org/docs/scenes/geometry-browser.html#ShapeGeometry"></iframe>
* @param {Object} [params] - The params.
* @extends module:core.MeshComponent
* @memberof module:components/meshes
* @example <caption>Creating a plane looking Shape from a THREE.Shape, and adding it to app</caption>
* const rectWidth = 10,
* rectLength = 5;
*
* const rectShape = new THREE.Shape();
* rectShape.moveTo(0,0);
* rectShape.lineTo(0, rectWidth);
* rectShape.lineTo(rectLength, rectWidth);
* rectShape.lineTo(rectLength, 0);
* rectShape.lineTo(0, 0);
*
* const plane = new Shape({
* geometry: {
* shape: rectShape
* },
*
* material: new THREE.MeshBasicMaterial({
* color: 0xffffff
* })
* }).addTo(app);
*/
class Shape extends MeshComponent {
/**
* Default values for parameters
* @member {Object} module:components/meshes.Shape#defaults
* @static
* @default <pre>
* {
* geometry: {
* shapes: []
* }
* </pre>
*/
static defaults = {
...MeshComponent.defaults,
geometry: {
shapes: []
}
};
/**
* Instructions
* @member {Object} module:components/meshes.Shape#instructions
* @static
* @default <pre>
* {
* geometry: ['shapes']
* }
* </pre>
*/
static instructions = {
...MeshComponent.instructions,
geometry: ['shapes']
};
constructor(params = {}) {
super(params, Shape.defaults, Shape.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.Shape
*/
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 ? ShapeBufferGeometry : ShapeGeometry)(
params.geometry.shapes
);
}
}
export {
Shape
};