UNPKG

whs

Version:

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

100 lines (92 loc) 2.91 kB
import { Mesh, ParametricBufferGeometry, ParametricGeometry, Vector3 } from 'three'; import {MeshComponent} from '../../core/MeshComponent'; /** * @class Parametric * @category components/meshes * @description `Parametric` generates a geometry representing a <a href='https://en.wikipedia.org/wiki/Parametric_surface'>Parametric surface</a> * <br/><br/> * It is usually used to develop different kinds of highfields or visualize a <a href='https://stemkoski.github.io/Three.js/Graphulus-Function.html'>math function</a>. * <br/> * - <a href='http://math.hws.edu/graphicsbook/source/threejs/curves-and-surfaces.html'>Parametric surface</a> * - <a href='https://stemkoski.github.io/Three.js/Graphulus-Surface.html'>"Graphulus"</a> * <br/><br/> * @classDesc * <iframe src="https://threejs.org/docs/scenes/geometry-browser.html#ParametricGeometry"></iframe> * @param {Object} [params] - The params. * @extends module:core.MeshComponent * @memberof module:components/meshes * @example <caption>Example creating an heightfield-like geometry. `u` and `v` are like `x` and `y` in shape, but their values are always from `0` to `1`. * We use them in `THREE.Vector3` like `x` and `z` and `Math.random() * 5` for `y`.</caption> * const createParametric = (u, v) => { * return new THREE.Vector3(u * 30, Math.random() * 5, v * 30); * } * * new Parametric({ * geometry: { * func: createParametric * }, * * material: new THREE.MeshLambertMaterial({ * color: 0xffffff, * side: THREE.DoubleSide * }), * * position: [0, 100, -100] * }).addTo(app); */ class Parametric extends MeshComponent { /** * Default values for parameters * @member {Object} module:components/meshes.Parametric#defaults * @static * @default <pre> * { * geometry: { * func: (u, v) => new Vector3(u, v, 0), * slices: 10, * tacks: 10 * } * } * </pre> */ static defaults = { ...MeshComponent.defaults, geometry: { func: (u, v) => new Vector3(u, v, 0), slices: 10, stacks: 10 } } constructor(params = {}) { super(params, Parametric.defaults, Parametric.instructions); } /** * @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.Parametric */ 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 ? ParametricBufferGeometry : ParametricGeometry)( params.geometry.func, params.geometry.slices, params.geometry.stacks ); } } export { Parametric };