UNPKG

awv3

Version:
86 lines (65 loc) 2.7 kB
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import * as THREE from 'three'; //note: number of vertices is constant in order to use the same vertex buffer var RingGeometry = /*#__PURE__*/ function (_THREE$BufferGeometry) { _inheritsLoose(RingGeometry, _THREE$BufferGeometry); function RingGeometry(segments) { var _this; _this = _THREE$BufferGeometry.call(this) || this; _this.parameters = { thetaSegments: segments, innerRadius: 1.0, outerRadius: 2.0, thetaStart: 0.0, thetaLength: 2 * Math.PI }; var vertsCount = (segments + 1) * 2; var facesCount = segments * 2; _this.setIndex(new THREE.BufferAttribute(new Uint32Array(facesCount * 3), 1)); _this.addAttribute('position', new THREE.BufferAttribute(new Float32Array(vertsCount * 3), 3)); _this.addAttribute('normal', new THREE.BufferAttribute(new Float32Array(vertsCount * 3), 3)); _this.attributes.position.dynamic = true; var f = 0; for (var i = 0; i < segments; i++) { var _arr = [2 * i, 2 * i + 2, 2 * i + 3, 2 * i + 3, 2 * i + 1, 2 * i]; for (var _i = 0; _i < _arr.length; _i++) { var idx = _arr[_i]; _this.index.array[f++] = idx; } } var n = 0; for (var _i2 = 0; _i2 < vertsCount; _i2++) { var _arr2 = [0, 0, 1]; for (var _i3 = 0; _i3 < _arr2.length; _i3++) { var crd = _arr2[_i3]; _this.attributes.normal.array[n++] = crd; } } _this.updateParameters(_this.parameters); return _this; } var _proto = RingGeometry.prototype; _proto.updateParameters = function updateParameters(newParams) { Object.assign(this.parameters, newParams); var segments = this.parameters.thetaSegments; var vertexBuffer = this.attributes.position.array; for (var i = 0; i <= segments; i++) { var angle = this.parameters.thetaStart + this.parameters.thetaLength * i / segments; var cosA = Math.cos(angle), sinA = Math.sin(angle); var innerPos = new THREE.Vector3(cosA, sinA, 0).multiplyScalar(this.parameters.innerRadius); var outerPos = new THREE.Vector3(cosA, sinA, 0).multiplyScalar(this.parameters.outerRadius); vertexBuffer[6 * i + 0] = innerPos.x; vertexBuffer[6 * i + 1] = innerPos.y; vertexBuffer[6 * i + 2] = innerPos.z; vertexBuffer[6 * i + 3] = outerPos.x; vertexBuffer[6 * i + 4] = outerPos.y; vertexBuffer[6 * i + 5] = outerPos.z; } this.attributes.position.needsUpdate = true; //Note: this update may be done faster: this.computeBoundingSphere(); }; return RingGeometry; }(THREE.BufferGeometry); export { RingGeometry as default };