UNPKG

playcanvas

Version:

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

52 lines (51 loc) 1.82 kB
import { math } from "../../core/math/math.js"; import { calculateTangents } from "./geometry-utils.js"; import { Geometry } from "./geometry.js"; class TorusGeometry extends Geometry { constructor(opts = {}) { super(); const rc = opts.tubeRadius ?? 0.2; const rt = opts.ringRadius ?? 0.3; const sectorAngle = (opts.sectorAngle ?? 360) * math.DEG_TO_RAD; const segments = opts.segments ?? 30; const sides = opts.sides ?? 20; const positions = []; const normals = []; const uvs = []; const indices = []; for (let i = 0; i <= sides; i++) { for (let j = 0; j <= segments; j++) { const x = Math.cos(sectorAngle * j / segments) * (rt + rc * Math.cos(2 * Math.PI * i / sides)); const y = Math.sin(2 * Math.PI * i / sides) * rc; const z = Math.sin(sectorAngle * j / segments) * (rt + rc * Math.cos(2 * Math.PI * i / sides)); const nx = Math.cos(sectorAngle * j / segments) * Math.cos(2 * Math.PI * i / sides); const ny = Math.sin(2 * Math.PI * i / sides); const nz = Math.sin(sectorAngle * j / segments) * Math.cos(2 * Math.PI * i / sides); const u = i / sides; const v = 1 - j / segments; positions.push(x, y, z); normals.push(nx, ny, nz); uvs.push(u, 1 - v); if (i < sides && j < segments) { const first = i * (segments + 1) + j; const second = (i + 1) * (segments + 1) + j; const third = i * (segments + 1) + (j + 1); const fourth = (i + 1) * (segments + 1) + (j + 1); indices.push(first, second, third); indices.push(second, fourth, third); } } } this.positions = positions; this.normals = normals; this.uvs = uvs; this.uvs1 = uvs; this.indices = indices; if (opts.calculateTangents) { this.tangents = calculateTangents(positions, normals, uvs, indices); } } } export { TorusGeometry };