UNPKG

whs

Version:

Super-fast 3D framework for Web Applications & Games. Based on Three.js

133 lines (122 loc) 3.95 kB
import { Mesh, BufferGeometry, ExtrudeGeometry } from 'three'; import {MeshComponent} from '../../core/MeshComponent'; /** * @class Extrude * @category components/meshes * @description Extrude geometry means that you can create a 3D mesh from any 2D shape using three.js geometry based on <a href='https://threejs.org/docs/#api/math/Vector2'>THREE.Vector2.</a> <br/> * Such implementation will help you to make volumed shapes that have their own depth and can be seen from all angels.<br/><br/> * You can also find some interesting examples made using <a href='threejs.org'>three.js</a> which is a core of whs.js, such as: * - <a href='http://threejs.org/examples/webgl_geometry_extrude_shapes.html'>Webgl geometry extrude</a> * - <a href='http://threejs.org/examples/webgl_geometry_extrude_shapes2.html'>Extrude shapes from geodata</a> * - <a href='http://threejs.org/examples/webgl_geometry_extrude_splines.html'>Extrude splines</a> * * Such examples can be easily implemented using whitestorm.js or it's plugins. Use `Extrude` class with <a href='https://threejs.org/docs/#api/extras/core/Shape'>THREE.Shape</a> to get extrude effect of shape defined by 2D vectors. * This class is similar to <a href='https://threejs.org/docs/#api/geometries/ExtrudeGeometry'>THREE.ExtrudeGeometry</a>, * but it also contains all properties, applied by `Shape`, such as material, mass and vectors like position (pos) and rotation (rot). * @classDesc * <iframe src="https://threejs.org/docs/scenes/geometry-browser.html#ExtrudeGeometry"></iframe> * @param {Object} [params] - The params. * @extends module:core.MeshComponent * @memberof module:components/meshes * @example <caption>Creating a shape, then an Extrude from it</caption> * const shape = new THREE.Shape([ * new THREE.Vector2(-4,-4), * new THREE.Vector2(-2,0), * new THREE.Vector2(-4,4), * new THREE.Vector2(0,2), * new THREE.Vector2(4,4), * new THREE.Vector2(2,0), * new THREE.Vector2(4,-4), * new THREE.Vector2(0,-2) * ]); * * const extrude = new Extrude({ * geometry: { * shapes: shape, * options: { * bevelEnabled: false, * bevelSize: 0, * amount: 2 * } * }, * * material: new THREE.MeshBasicMaterial({ * color: 0xffffff * }), * * position: [0, 100, 0] * }); * * extrude.addTo(app); */ class Extrude extends MeshComponent { /** * Default values for parameters * @member {Object} module:components/meshes.Extrude#defaults * @static * @default <pre> * { * geometry: { * shapes: [], * options: {} * } * } * </pre> */ static defaults = { ...MeshComponent.defaults, geometry: { shapes: [], options: {} } }; /** * Instructions * @member {Object} module:components/meshes.Extrude#instructions * @static * @default <pre> * { * geometry: ['shapes', 'options'] * } * </pre> */ static instructions = { ...MeshComponent.instructions, geometry: ['shapes', 'options'] }; constructor(params = {}) { super(params, Extrude.defaults, Extrude.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.Extrude */ 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 ExtrudeGeometry( params.geometry.shapes, params.geometry.options ); return params.buffer ? new BufferGeometry().fromGeometry(geometry) : geometry; } } export { Extrude };