UNPKG

whs

Version:

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

136 lines (126 loc) 2.99 kB
import { Mesh, RingGeometry, RingBufferGeometry } from 'three'; import {MeshComponent} from '../../core/MeshComponent'; /** * @class Ring * @category components/meshes * @description Ring class creates a circle or just 2D Torus. Does not support physics. * @classDesc * <iframe src="https://threejs.org/docs/scenes/geometry-browser.html#RingGeometry"></iframe> * @param {Object} [params] - The params. * @extends module:core.MeshComponent * @memberof module:components/meshes * @example <caption>Creating a Ring, and adding to app</caption> * new Ring({ * geometry: { * innerRadius: 5, * outerRadius: 2 * }, * * material: new THREE.MeshLambertMaterial({ * color: 0xffffff, * side THREE.DoubleSide * }), * * position: [0, 8, 0], * * rotation: { * x: Math.PI/4 * } * }).addTo(app); */ class Ring extends MeshComponent { /** * Default values for parameters * @member {Object} module:components/meshes.Ring#defaults * @static * @default <pre> * { * geometry: { * innerRadius: 0, * outerRadius: 50, * thetaSegments: 8, * phiSegments: 8, * thetaStart: 0, * thetaLength: Math.PI * 2 * } * } * </pre> */ static defaults = { ...MeshComponent.defaults, geometry: { innerRadius: 0, outerRadius: 50, thetaSegments: 8, phiSegments: 8, thetaStart: 0, thetaLength: Math.PI * 2 } }; /** * Instructions * @member {Object} module:components/meshes.Ring#instructions * @static * @default <pre> * { * geometry: [ * 'innerRadius', * 'outerRadius', * 'thetaSegments', * 'phiSegments', * 'thetaStart', * 'thetaLength' * ] * } * </pre> */ static instructions = { ...MeshComponent.defaults, geometry: [ 'innerRadius', 'outerRadius', 'thetaSegments', 'phiSegments', 'thetaStart', 'thetaLength' ] }; constructor(params = {}) { super(params, Ring.defaults, Ring.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.Ring */ 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 ? RingBufferGeometry : RingGeometry)( params.geometry.innerRadius, params.geometry.outerRadius, params.geometry.thetaSegments, params.geometry.phiSegments, params.geometry.thetaStart, params.geometry.thetaLength ); } } export { Ring };