whs
Version:
Super-fast 3D framework for Web Applications & Games. Based on Three.js
111 lines (100 loc) • 2.49 kB
JavaScript
import {
Mesh,
PlaneBufferGeometry,
PlaneGeometry
} from 'three';
import {MeshComponent} from '../../core/MeshComponent';
/**
* @class Plane
* @category components/meshes
* @description `Plane` is used for creating planes given some `width` and `height`.
* @classDesc
* <iframe src="https://threejs.org/docs/scenes/geometry-browser.html#PlaneGeometry"></iframe>
* @param {Object} [params] - The params.
* @extends module:core.MeshComponent
* @memberof module:components/meshes
* @example <caption>Creating a Plane, and adding to app</caption>
* new Plane({
* geometry: {
* width: 20,
* height: 30
* },
*
* material: new THREE.MeshBasicMaterial({
* color: 0xffffff
* })
* }).addTo(app);
*/
class Plane extends MeshComponent {
/**
* Default values for parameters
* @member {Object} module:components/meshes.Plane#defaults
* @static
* @default <pre>
* {
* geometry: {
* width: 10,
* height: 10,
* wSegments: 1,
* hSegments: 1
* }
* }
* </pre>
*/
static defaults = {
...MeshComponent.defaults,
geometry: {
width: 10,
height: 10,
wSegments: 1,
hSegments: 1
}
};
/**
* Instructions
* @member {Object} module:components/meshes.Plane#instructions
* @static
* @default <pre>
* {
* geometry: ['width', 'height', 'wSegments', 'hSegments']
* }
* </pre>
*/
static instructions = {
...MeshComponent.instructions,
geometry: ['width', 'height', 'wSegments', 'hSegments']
};
constructor(params = {}) {
super(params, Plane.defaults, Plane.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.Plane
*/
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 = {}) {
const geometry = new (params.buffer ? PlaneBufferGeometry : PlaneGeometry)(
params.geometry.width,
params.geometry.height,
params.geometry.wSegments,
params.geometry.hSegments
);
return geometry;
}
}
export {
Plane
};