UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

99 lines (98 loc) 2.69 kB
import { TorusGeometry } from "../../../scene/geometry/torus-geometry.js"; import { Mesh } from "../../../scene/mesh.js"; import { TriData } from "../tri-data.js"; import { Shape } from "./shape.js"; const TORUS_RENDER_SEGMENTS = 80; const TORUS_INTERSECT_SEGMENTS = 20; class ArcShape extends Shape { _tubeRadius = 0.01; _ringRadius = 0.5; _sectorAngle = 360; _tolerance = 0.05; _triDataCache; constructor(device, args = {}) { super(device, "disk", args); this._tubeRadius = args.tubeRadius ?? this._tubeRadius; this._ringRadius = args.ringRadius ?? this._ringRadius; this._sectorAngle = args.sectorAngle ?? this._sectorAngle; this._triDataCache = [ new TriData(this._createTorusGeometry(this._sectorAngle)), new TriData(this._createTorusGeometry(360)) ]; this.triData = [this._triDataCache[0]]; this._createRenderComponent(this.entity, [ this._createTorusMesh(this._sectorAngle), this._createTorusMesh(360) ]); this.show("sector"); this._update(); } _createTorusGeometry(sectorAngle) { return new TorusGeometry({ tubeRadius: this._tubeRadius + this._tolerance, ringRadius: this._ringRadius, sectorAngle, segments: TORUS_INTERSECT_SEGMENTS }); } _createTorusMesh(sectorAngle) { const geom = new TorusGeometry({ tubeRadius: this._tubeRadius, ringRadius: this._ringRadius, sectorAngle, segments: TORUS_RENDER_SEGMENTS }); return Mesh.fromGeometry(this.device, geom); } set tubeRadius(value) { this._tubeRadius = value ?? this._tubeRadius; this._update(); } get tubeRadius() { return this._tubeRadius; } set ringRadius(value) { this._ringRadius = value ?? this._ringRadius; this._update(); } get ringRadius() { return this._ringRadius; } set tolerance(value) { this._tolerance = value ?? this._tolerance; this._update(); } get tolerance() { return this._tolerance; } _update() { this._triDataCache[0].fromGeometry(this._createTorusGeometry(this._sectorAngle)); this._triDataCache[1].fromGeometry(this._createTorusGeometry(360)); this.meshInstances[0].mesh = this._createTorusMesh(this._sectorAngle); this.meshInstances[1].mesh = this._createTorusMesh(360); } show(state) { switch (state) { case "sector": { this.triData[0] = this._triDataCache[0]; this.meshInstances[0].visible = true; this.meshInstances[1].visible = false; break; } case "ring": { this.triData[0] = this._triDataCache[1]; this.meshInstances[0].visible = false; this.meshInstances[1].visible = true; break; } case "none": { this.meshInstances[0].visible = false; this.meshInstances[1].visible = false; break; } } } } export { ArcShape };