-
Notifications
You must be signed in to change notification settings - Fork 46
/
sphere-geometry.ts
87 lines (74 loc) · 2.14 KB
/
sphere-geometry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Vec3 } from "../../math/vec3"
import { MeshGeometry3D } from "./mesh-geometry"
export interface SphereGeometryOptions {
/** The radius of the sphere. */
radius?: number
/** The number of segments of the sphere. */
segments?: number
/** The number of rings of the sphere. */
rings?: number
}
export namespace SphereGeometry {
export function create(options: SphereGeometryOptions = {}) {
const { radius = 1, segments = 32, rings = 16 } = options
// Based on https://github.com/mrdoob/three.js/blob/master/src/geometries/SphereGeometry.js
const grid = []
const indices = []
const positions = []
const uvs = []
const normals = []
let index = 0
for (let iy = 0; iy <= rings; iy++) {
const vertices = []
const v = iy / rings
let uOffset = 0
if (iy == 0) {
uOffset = 0.5 / segments
} else if (iy == rings) {
uOffset = - 0.5 / segments
}
for (let ix = 0; ix <= segments; ix++) {
const u = ix / segments
let x = - radius * Math.cos(u * Math.PI * 2) *
Math.sin(v * Math.PI)
let y = radius * Math.cos(v * Math.PI)
let z = radius * Math.sin(u * Math.PI * 2) *
Math.sin(v * Math.PI)
let pos = Vec3.fromValues(x, y, z)
positions.push(x, y, z)
normals.push(...Vec3.normalize(pos))
uvs.push(u + uOffset, 1 - v)
vertices.push(index++)
}
grid.push(vertices)
}
for (let iy = 0; iy < rings; iy++) {
for (let ix = 0; ix < segments; ix++) {
const a = grid[iy][ix + 1]
const b = grid[iy][ix]
const c = grid[iy + 1][ix]
const d = grid[iy + 1][ix + 1]
if (iy !== 0) {
indices.push(a, b, d)
}
if (iy !== rings - 1) {
indices.push(b, c, d)
}
}
}
return Object.assign(new MeshGeometry3D(), {
normals: {
buffer: new Float32Array(normals)
},
uvs: [{
buffer: new Float32Array(uvs)
}],
indices: {
buffer: new Uint16Array(indices)
},
positions: {
buffer: new Float32Array(positions)
}
})
}
}