UNPKG

whs

Version:

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

116 lines (106 loc) 3.19 kB
import { Mesh, LatheBufferGeometry, LatheGeometry } from 'three'; import {MeshComponent} from '../../core/MeshComponent'; /** * @class Lathe * @category components/meshes * @description A `LatheGeometry` allows you to create shapes from a smooth curve. * This curve is defined by a number of points (also called knots) and is most often called a spline. This spline is rotated around a fixed point and results in vase- and bell-like shapes.<br/><br/> * In 3D computer graphics, a lathed object is a 3D model whose vertex geometry is produced by rotating the points of a spline or other point set around a fixed axis. * The lathing may be partial; the amount of rotation is not necessarily a full 360 degrees. * The point set providing the initial source data can be thought of as a cross section through the object along a plane containing its axis of radial symmetry. <br/><br/> * The <a href='http://threejs.org/docs/scenes/geometry-browser.html#LatheGeometry'>following example</a> shows a geometry which can be generated using `Lathe` class. * @classDesc * <iframe src="https://threejs.org/docs/scenes/geometry-browser.html#LatheGeometry"></iframe> * @param {Object} [params] - The params. * @extends module:core.MeshComponent * @memberof module:components/meshes * @example <caption>Creating a Lath, and adding to app</caption> * const points = []; * * for (let i = 0; i < 10; i++) { * points.push( * new THREE.Vector2( * (Math.sin(i * 0.7) * 15 + 50) / 10, * (i - 5) * 0.2 * ) * ); * } * * const lathe = new Lathe({ * geometry: { * points: points * }, * * material: new THREE.MeshBasicMaterial({ * color: 0xffffff * }), * * position: [0, 50, 10] * }).addTo(app); */ class Lathe extends MeshComponent { /** * Default values for parameters * @member {Object} module:components/meshes.Lathe#defaults * @static * @default <pre> * { * geometry: { * points: [] * } * } * </pre> */ static defaults = { ...MeshComponent.defaults, geometry: { points: [] } }; /** * Instructions * @member {Object} module:components/meshes.Lathe#instructions * @static * @default <pre>{ * geometry: ['points'] * } * </pre> */ static instructions = { ...MeshComponent.instructions, geometry: ['points'] }; constructor(params = {}) { super(params, Lathe.defaults, Lathe.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.Lathe */ 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 ? LatheBufferGeometry : LatheGeometry)( params.geometry.points ); } } export { Lathe };