@geastack/gea3d 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +82 -2
- package/THIRD_PARTY_NOTICES.md +26 -0
- package/package.json +35 -4
- package/src/index.ts +6 -0
- package/src/native/api.ts +194 -0
- package/src/native/api.web.ts +968 -0
- package/src/native/constants.ts +53 -0
- package/src/native/gea3d_math.h +185 -0
- package/src/native/gea3d_native.cpp +2261 -0
- package/src/native/gea3d_native.h +196 -0
- package/src/native/gea3d_raster.h +1186 -0
- package/src/raster/pipeline.ts +520 -0
- package/src/raster/presenter.ts +25 -0
- package/src/renderer/GeaRenderer.ts +128 -0
- package/src/three/cache.ts +43 -0
- package/src/three/camera.ts +120 -0
- package/src/three/core.ts +1186 -0
- package/src/three/fog.ts +40 -0
- package/src/three/geometries.ts +528 -0
- package/src/three/index.ts +57 -0
- package/src/three/lights.ts +2 -0
- package/src/three/materials.ts +315 -0
- package/src/three/math.ts +1228 -0
- package/src/three/mesh.ts +2 -0
- package/src/three/raycaster.ts +101 -0
package/src/three/fog.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-only
|
|
2
|
+
// Distance fog: geometry blends toward `color` with distance from the camera.
|
|
3
|
+
|
|
4
|
+
import { Color } from './math'
|
|
5
|
+
|
|
6
|
+
export const FOG_MODE_NONE = 0
|
|
7
|
+
export const FOG_MODE_LINEAR = 1
|
|
8
|
+
export const FOG_MODE_EXP2 = 2
|
|
9
|
+
|
|
10
|
+
// Linear fog: none at `near`, full at `far` (off when far <= near).
|
|
11
|
+
export class Fog {
|
|
12
|
+
readonly color: Color
|
|
13
|
+
near: number
|
|
14
|
+
far: number
|
|
15
|
+
// FogExp2 density (0 for linear fog).
|
|
16
|
+
density: number
|
|
17
|
+
isFogExp2: boolean
|
|
18
|
+
|
|
19
|
+
constructor(color: number, near: number = 1, far: number = 1000) {
|
|
20
|
+
this.color = new Color(color)
|
|
21
|
+
this.near = near
|
|
22
|
+
this.far = far
|
|
23
|
+
this.density = 0
|
|
24
|
+
this.isFogExp2 = false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
mode(): number {
|
|
28
|
+
if (this.isFogExp2) return this.density > 0 ? FOG_MODE_EXP2 : FOG_MODE_NONE
|
|
29
|
+
return this.far > this.near ? FOG_MODE_LINEAR : FOG_MODE_NONE
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Exponential squared fog: 1 - exp(-(density * distance)^2).
|
|
34
|
+
export class FogExp2 extends Fog {
|
|
35
|
+
constructor(color: number, density: number = 0.00025) {
|
|
36
|
+
super(color, 0, 0)
|
|
37
|
+
this.density = density
|
|
38
|
+
this.isFogExp2 = true
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-only
|
|
2
|
+
// Typed ports of the three.js geometry generators (same parameterization,
|
|
3
|
+
// same vertex layouts). Dynamic component indexing from upstream buildPlane
|
|
4
|
+
// is replaced with explicit axis dispatch.
|
|
5
|
+
|
|
6
|
+
import { BufferGeometry, Float32BufferAttribute } from './core'
|
|
7
|
+
|
|
8
|
+
class PlaneBuilder {
|
|
9
|
+
readonly positions: number[]
|
|
10
|
+
readonly normals: number[]
|
|
11
|
+
readonly uvs: number[]
|
|
12
|
+
readonly indices: number[]
|
|
13
|
+
vertexCount: number
|
|
14
|
+
|
|
15
|
+
constructor() {
|
|
16
|
+
this.positions = []
|
|
17
|
+
this.normals = []
|
|
18
|
+
this.uvs = []
|
|
19
|
+
this.indices = []
|
|
20
|
+
this.vertexCount = 0
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// u, v, w: axis indices (0=x, 1=y, 2=z) receiving the plane's U, V and
|
|
24
|
+
// normal components — the typed replacement for three's vector['x'] trick.
|
|
25
|
+
buildPlane(
|
|
26
|
+
u: number,
|
|
27
|
+
v: number,
|
|
28
|
+
w: number,
|
|
29
|
+
udir: number,
|
|
30
|
+
vdir: number,
|
|
31
|
+
width: number,
|
|
32
|
+
height: number,
|
|
33
|
+
depth: number,
|
|
34
|
+
gridX: number,
|
|
35
|
+
gridY: number,
|
|
36
|
+
): void {
|
|
37
|
+
const segmentWidth = width / gridX
|
|
38
|
+
const segmentHeight = height / gridY
|
|
39
|
+
const widthHalf = width / 2
|
|
40
|
+
const heightHalf = height / 2
|
|
41
|
+
const depthHalf = depth / 2
|
|
42
|
+
const gridX1 = gridX + 1
|
|
43
|
+
const gridY1 = gridY + 1
|
|
44
|
+
const vertexStart = this.vertexCount
|
|
45
|
+
|
|
46
|
+
for (let iy = 0; iy < gridY1; iy++) {
|
|
47
|
+
const y = iy * segmentHeight - heightHalf
|
|
48
|
+
for (let ix = 0; ix < gridX1; ix++) {
|
|
49
|
+
const x = ix * segmentWidth - widthHalf
|
|
50
|
+
const cu = x * udir
|
|
51
|
+
const cv = y * vdir
|
|
52
|
+
const cw = depthHalf
|
|
53
|
+
// component vector: value into axis slot
|
|
54
|
+
let px = 0
|
|
55
|
+
let py = 0
|
|
56
|
+
let pz = 0
|
|
57
|
+
if (u === 0) px = cu
|
|
58
|
+
else if (u === 1) py = cu
|
|
59
|
+
else pz = cu
|
|
60
|
+
if (v === 0) px = cv
|
|
61
|
+
else if (v === 1) py = cv
|
|
62
|
+
else pz = cv
|
|
63
|
+
if (w === 0) px = cw
|
|
64
|
+
else if (w === 1) py = cw
|
|
65
|
+
else pz = cw
|
|
66
|
+
this.positions.push(px, py, pz)
|
|
67
|
+
|
|
68
|
+
let nx = 0
|
|
69
|
+
let ny = 0
|
|
70
|
+
let nz = 0
|
|
71
|
+
const nw = depth > 0 ? 1 : -1
|
|
72
|
+
if (w === 0) nx = nw
|
|
73
|
+
else if (w === 1) ny = nw
|
|
74
|
+
else nz = nw
|
|
75
|
+
this.normals.push(nx, ny, nz)
|
|
76
|
+
|
|
77
|
+
this.uvs.push(ix / gridX, 1 - iy / gridY)
|
|
78
|
+
this.vertexCount++
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
for (let iy = 0; iy < gridY; iy++) {
|
|
83
|
+
for (let ix = 0; ix < gridX; ix++) {
|
|
84
|
+
const a = vertexStart + ix + gridX1 * iy
|
|
85
|
+
const b = vertexStart + ix + gridX1 * (iy + 1)
|
|
86
|
+
const c = vertexStart + (ix + 1) + gridX1 * (iy + 1)
|
|
87
|
+
const d = vertexStart + (ix + 1) + gridX1 * iy
|
|
88
|
+
this.indices.push(a, b, d)
|
|
89
|
+
this.indices.push(b, c, d)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class BoxGeometry extends BufferGeometry {
|
|
96
|
+
constructor(
|
|
97
|
+
width: number = 1,
|
|
98
|
+
height: number = 1,
|
|
99
|
+
depth: number = 1,
|
|
100
|
+
widthSegments: number = 1,
|
|
101
|
+
heightSegments: number = 1,
|
|
102
|
+
depthSegments: number = 1,
|
|
103
|
+
) {
|
|
104
|
+
super()
|
|
105
|
+
const ws = Math.floor(widthSegments)
|
|
106
|
+
const hs = Math.floor(heightSegments)
|
|
107
|
+
const ds = Math.floor(depthSegments)
|
|
108
|
+
const b = new PlaneBuilder()
|
|
109
|
+
// axis mapping mirrors three.js BoxGeometry: (u, v, w, udir, vdir, w, h, d)
|
|
110
|
+
b.buildPlane(2, 1, 0, -1, -1, depth, height, width, ds, hs) // px
|
|
111
|
+
b.buildPlane(2, 1, 0, 1, -1, depth, height, -width, ds, hs) // nx
|
|
112
|
+
b.buildPlane(0, 2, 1, 1, 1, width, depth, height, ws, ds) // py
|
|
113
|
+
b.buildPlane(0, 2, 1, 1, -1, width, depth, -height, ws, ds) // ny
|
|
114
|
+
b.buildPlane(0, 1, 2, 1, -1, width, height, depth, ws, hs) // pz
|
|
115
|
+
b.buildPlane(0, 1, 2, -1, -1, width, height, -depth, ws, hs) // nz
|
|
116
|
+
this.setIndex(b.indices)
|
|
117
|
+
this.setAttribute('position', new Float32BufferAttribute(b.positions, 3))
|
|
118
|
+
this.setAttribute('normal', new Float32BufferAttribute(b.normals, 3))
|
|
119
|
+
this.setAttribute('uv', new Float32BufferAttribute(b.uvs, 2))
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export class PlaneGeometry extends BufferGeometry {
|
|
124
|
+
constructor(width: number = 1, height: number = 1, widthSegments: number = 1, heightSegments: number = 1) {
|
|
125
|
+
super()
|
|
126
|
+
const widthHalf = width / 2
|
|
127
|
+
const heightHalf = height / 2
|
|
128
|
+
const gridX = Math.floor(widthSegments)
|
|
129
|
+
const gridY = Math.floor(heightSegments)
|
|
130
|
+
const gridX1 = gridX + 1
|
|
131
|
+
const gridY1 = gridY + 1
|
|
132
|
+
const segmentWidth = width / gridX
|
|
133
|
+
const segmentHeight = height / gridY
|
|
134
|
+
|
|
135
|
+
const indices: number[] = []
|
|
136
|
+
const positions: number[] = []
|
|
137
|
+
const normals: number[] = []
|
|
138
|
+
const uvs: number[] = []
|
|
139
|
+
|
|
140
|
+
for (let iy = 0; iy < gridY1; iy++) {
|
|
141
|
+
const y = iy * segmentHeight - heightHalf
|
|
142
|
+
for (let ix = 0; ix < gridX1; ix++) {
|
|
143
|
+
const x = ix * segmentWidth - widthHalf
|
|
144
|
+
positions.push(x, -y, 0)
|
|
145
|
+
normals.push(0, 0, 1)
|
|
146
|
+
uvs.push(ix / gridX, 1 - iy / gridY)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
for (let iy = 0; iy < gridY; iy++) {
|
|
150
|
+
for (let ix = 0; ix < gridX; ix++) {
|
|
151
|
+
const a = ix + gridX1 * iy
|
|
152
|
+
const b = ix + gridX1 * (iy + 1)
|
|
153
|
+
const c = ix + 1 + gridX1 * (iy + 1)
|
|
154
|
+
const d = ix + 1 + gridX1 * iy
|
|
155
|
+
indices.push(a, b, d)
|
|
156
|
+
indices.push(b, c, d)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
this.setIndex(indices)
|
|
160
|
+
this.setAttribute('position', new Float32BufferAttribute(positions, 3))
|
|
161
|
+
this.setAttribute('normal', new Float32BufferAttribute(normals, 3))
|
|
162
|
+
this.setAttribute('uv', new Float32BufferAttribute(uvs, 2))
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export class SphereGeometry extends BufferGeometry {
|
|
167
|
+
constructor(
|
|
168
|
+
radius: number = 1,
|
|
169
|
+
widthSegments: number = 32,
|
|
170
|
+
heightSegments: number = 16,
|
|
171
|
+
phiStart: number = 0,
|
|
172
|
+
phiLength: number = Math.PI * 2,
|
|
173
|
+
thetaStart: number = 0,
|
|
174
|
+
thetaLength: number = Math.PI,
|
|
175
|
+
) {
|
|
176
|
+
super()
|
|
177
|
+
const ws = Math.max(3, Math.floor(widthSegments))
|
|
178
|
+
const hs = Math.max(2, Math.floor(heightSegments))
|
|
179
|
+
const thetaEnd = Math.min(thetaStart + thetaLength, Math.PI)
|
|
180
|
+
|
|
181
|
+
const indices: number[] = []
|
|
182
|
+
const positions: number[] = []
|
|
183
|
+
const normals: number[] = []
|
|
184
|
+
const uvs: number[] = []
|
|
185
|
+
const grid: number[][] = []
|
|
186
|
+
let index = 0
|
|
187
|
+
|
|
188
|
+
for (let iy = 0; iy <= hs; iy++) {
|
|
189
|
+
const verticesRow: number[] = []
|
|
190
|
+
const v = iy / hs
|
|
191
|
+
let uOffset = 0
|
|
192
|
+
if (iy === 0 && thetaStart === 0) uOffset = 0.5 / ws
|
|
193
|
+
else if (iy === hs && thetaEnd === Math.PI) uOffset = -0.5 / ws
|
|
194
|
+
for (let ix = 0; ix <= ws; ix++) {
|
|
195
|
+
const u = ix / ws
|
|
196
|
+
const px = -radius * Math.cos(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength)
|
|
197
|
+
const py = radius * Math.cos(thetaStart + v * thetaLength)
|
|
198
|
+
const pz = radius * Math.sin(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength)
|
|
199
|
+
positions.push(px, py, pz)
|
|
200
|
+
const len = Math.sqrt(px * px + py * py + pz * pz)
|
|
201
|
+
const inv = len > 0 ? 1 / len : 0
|
|
202
|
+
normals.push(px * inv, py * inv, pz * inv)
|
|
203
|
+
uvs.push(u + uOffset, 1 - v)
|
|
204
|
+
verticesRow.push(index)
|
|
205
|
+
index++
|
|
206
|
+
}
|
|
207
|
+
grid.push(verticesRow)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
for (let iy = 0; iy < hs; iy++) {
|
|
211
|
+
for (let ix = 0; ix < ws; ix++) {
|
|
212
|
+
const a = grid[iy][ix + 1]
|
|
213
|
+
const b = grid[iy][ix]
|
|
214
|
+
const c = grid[iy + 1][ix]
|
|
215
|
+
const d = grid[iy + 1][ix + 1]
|
|
216
|
+
if (iy !== 0 || thetaStart > 0) indices.push(a, b, d)
|
|
217
|
+
if (iy !== hs - 1 || thetaEnd < Math.PI) indices.push(b, c, d)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
this.setIndex(indices)
|
|
222
|
+
this.setAttribute('position', new Float32BufferAttribute(positions, 3))
|
|
223
|
+
this.setAttribute('normal', new Float32BufferAttribute(normals, 3))
|
|
224
|
+
this.setAttribute('uv', new Float32BufferAttribute(uvs, 2))
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export class TorusGeometry extends BufferGeometry {
|
|
229
|
+
constructor(
|
|
230
|
+
radius: number = 1,
|
|
231
|
+
tube: number = 0.4,
|
|
232
|
+
radialSegments: number = 12,
|
|
233
|
+
tubularSegments: number = 48,
|
|
234
|
+
arc: number = Math.PI * 2,
|
|
235
|
+
) {
|
|
236
|
+
super()
|
|
237
|
+
const rs = Math.floor(radialSegments)
|
|
238
|
+
const ts = Math.floor(tubularSegments)
|
|
239
|
+
|
|
240
|
+
const indices: number[] = []
|
|
241
|
+
const positions: number[] = []
|
|
242
|
+
const normals: number[] = []
|
|
243
|
+
const uvs: number[] = []
|
|
244
|
+
|
|
245
|
+
for (let j = 0; j <= rs; j++) {
|
|
246
|
+
for (let i = 0; i <= ts; i++) {
|
|
247
|
+
const u = (i / ts) * arc
|
|
248
|
+
const v = (j / rs) * Math.PI * 2
|
|
249
|
+
const px = (radius + tube * Math.cos(v)) * Math.cos(u)
|
|
250
|
+
const py = (radius + tube * Math.cos(v)) * Math.sin(u)
|
|
251
|
+
const pz = tube * Math.sin(v)
|
|
252
|
+
positions.push(px, py, pz)
|
|
253
|
+
const cx = radius * Math.cos(u)
|
|
254
|
+
const cy = radius * Math.sin(u)
|
|
255
|
+
let nx = px - cx
|
|
256
|
+
let ny = py - cy
|
|
257
|
+
let nz = pz
|
|
258
|
+
const len = Math.sqrt(nx * nx + ny * ny + nz * nz)
|
|
259
|
+
const inv = len > 0 ? 1 / len : 0
|
|
260
|
+
nx *= inv
|
|
261
|
+
ny *= inv
|
|
262
|
+
nz *= inv
|
|
263
|
+
normals.push(nx, ny, nz)
|
|
264
|
+
uvs.push(i / ts, j / rs)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
for (let j = 1; j <= rs; j++) {
|
|
269
|
+
for (let i = 1; i <= ts; i++) {
|
|
270
|
+
const a = (ts + 1) * j + i - 1
|
|
271
|
+
const b = (ts + 1) * (j - 1) + i - 1
|
|
272
|
+
const c = (ts + 1) * (j - 1) + i
|
|
273
|
+
const d = (ts + 1) * j + i
|
|
274
|
+
indices.push(a, b, d)
|
|
275
|
+
indices.push(b, c, d)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this.setIndex(indices)
|
|
280
|
+
this.setAttribute('position', new Float32BufferAttribute(positions, 3))
|
|
281
|
+
this.setAttribute('normal', new Float32BufferAttribute(normals, 3))
|
|
282
|
+
this.setAttribute('uv', new Float32BufferAttribute(uvs, 2))
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export class TorusKnotGeometry extends BufferGeometry {
|
|
287
|
+
constructor(
|
|
288
|
+
radius: number = 1,
|
|
289
|
+
tube: number = 0.4,
|
|
290
|
+
tubularSegments: number = 64,
|
|
291
|
+
radialSegments: number = 8,
|
|
292
|
+
p: number = 2,
|
|
293
|
+
q: number = 3,
|
|
294
|
+
) {
|
|
295
|
+
super()
|
|
296
|
+
const ts = Math.floor(tubularSegments)
|
|
297
|
+
const rs = Math.floor(radialSegments)
|
|
298
|
+
|
|
299
|
+
const indices: number[] = []
|
|
300
|
+
const positions: number[] = []
|
|
301
|
+
const normals: number[] = []
|
|
302
|
+
const uvs: number[] = []
|
|
303
|
+
|
|
304
|
+
for (let i = 0; i <= ts; i++) {
|
|
305
|
+
const u = (i / ts) * p * Math.PI * 2
|
|
306
|
+
// curve point and a slightly advanced point for the frame
|
|
307
|
+
const p1x = knotX(u, p, q, radius)
|
|
308
|
+
const p1y = knotY(u, p, q, radius)
|
|
309
|
+
const p1z = knotZ(u, p, q, radius)
|
|
310
|
+
const u2 = u + 0.01
|
|
311
|
+
const p2x = knotX(u2, p, q, radius)
|
|
312
|
+
const p2y = knotY(u2, p, q, radius)
|
|
313
|
+
const p2z = knotZ(u2, p, q, radius)
|
|
314
|
+
|
|
315
|
+
// Frenet-ish frame
|
|
316
|
+
let tx = p2x - p1x
|
|
317
|
+
let ty = p2y - p1y
|
|
318
|
+
let tz = p2z - p1z
|
|
319
|
+
let nx = p2x + p1x
|
|
320
|
+
let ny = p2y + p1y
|
|
321
|
+
let nz = p2z + p1z
|
|
322
|
+
// B = T x N
|
|
323
|
+
let bx = ty * nz - tz * ny
|
|
324
|
+
let by = tz * nx - tx * nz
|
|
325
|
+
let bz = tx * ny - ty * nx
|
|
326
|
+
// N = B x T
|
|
327
|
+
nx = by * tz - bz * ty
|
|
328
|
+
ny = bz * tx - bx * tz
|
|
329
|
+
nz = bx * ty - by * tx
|
|
330
|
+
let len = Math.sqrt(bx * bx + by * by + bz * bz)
|
|
331
|
+
let inv = len > 0 ? 1 / len : 0
|
|
332
|
+
bx *= inv
|
|
333
|
+
by *= inv
|
|
334
|
+
bz *= inv
|
|
335
|
+
len = Math.sqrt(nx * nx + ny * ny + nz * nz)
|
|
336
|
+
inv = len > 0 ? 1 / len : 0
|
|
337
|
+
nx *= inv
|
|
338
|
+
ny *= inv
|
|
339
|
+
nz *= inv
|
|
340
|
+
|
|
341
|
+
for (let j = 0; j <= rs; j++) {
|
|
342
|
+
const v = (j / rs) * Math.PI * 2
|
|
343
|
+
const cx = -tube * Math.cos(v)
|
|
344
|
+
const cy = tube * Math.sin(v)
|
|
345
|
+
const px = p1x + (cx * nx + cy * bx)
|
|
346
|
+
const py = p1y + (cx * ny + cy * by)
|
|
347
|
+
const pz = p1z + (cx * nz + cy * bz)
|
|
348
|
+
positions.push(px, py, pz)
|
|
349
|
+
let vnx = px - p1x
|
|
350
|
+
let vny = py - p1y
|
|
351
|
+
let vnz = pz - p1z
|
|
352
|
+
const vlen = Math.sqrt(vnx * vnx + vny * vny + vnz * vnz)
|
|
353
|
+
const vinv = vlen > 0 ? 1 / vlen : 0
|
|
354
|
+
normals.push(vnx * vinv, vny * vinv, vnz * vinv)
|
|
355
|
+
uvs.push(i / ts, j / rs)
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
for (let j = 1; j <= ts; j++) {
|
|
360
|
+
for (let i = 1; i <= rs; i++) {
|
|
361
|
+
const a = (rs + 1) * (j - 1) + (i - 1)
|
|
362
|
+
const b = (rs + 1) * j + (i - 1)
|
|
363
|
+
const c = (rs + 1) * j + i
|
|
364
|
+
const d = (rs + 1) * (j - 1) + i
|
|
365
|
+
indices.push(a, b, d)
|
|
366
|
+
indices.push(b, c, d)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
this.setIndex(indices)
|
|
371
|
+
this.setAttribute('position', new Float32BufferAttribute(positions, 3))
|
|
372
|
+
this.setAttribute('normal', new Float32BufferAttribute(normals, 3))
|
|
373
|
+
this.setAttribute('uv', new Float32BufferAttribute(uvs, 2))
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function knotX(u: number, p: number, q: number, radius: number): number {
|
|
378
|
+
const cu = Math.cos(u)
|
|
379
|
+
const quOverP = (q / p) * u
|
|
380
|
+
const cs = Math.cos(quOverP)
|
|
381
|
+
return radius * (2 + cs) * 0.5 * cu
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function knotY(u: number, p: number, q: number, radius: number): number {
|
|
385
|
+
const su = Math.sin(u)
|
|
386
|
+
const quOverP = (q / p) * u
|
|
387
|
+
const cs = Math.cos(quOverP)
|
|
388
|
+
return radius * (2 + cs) * su * 0.5
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function knotZ(u: number, p: number, q: number, radius: number): number {
|
|
392
|
+
return radius * Math.sin((q / p) * u) * 0.5
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export class CylinderGeometry extends BufferGeometry {
|
|
396
|
+
constructor(
|
|
397
|
+
radiusTop: number = 1,
|
|
398
|
+
radiusBottom: number = 1,
|
|
399
|
+
height: number = 1,
|
|
400
|
+
radialSegments: number = 32,
|
|
401
|
+
heightSegments: number = 1,
|
|
402
|
+
openEnded: boolean = false,
|
|
403
|
+
thetaStart: number = 0,
|
|
404
|
+
thetaLength: number = Math.PI * 2,
|
|
405
|
+
) {
|
|
406
|
+
super()
|
|
407
|
+
const rs = Math.floor(radialSegments)
|
|
408
|
+
const hs = Math.floor(heightSegments)
|
|
409
|
+
|
|
410
|
+
const indices: number[] = []
|
|
411
|
+
const positions: number[] = []
|
|
412
|
+
const normals: number[] = []
|
|
413
|
+
const uvs: number[] = []
|
|
414
|
+
let index = 0
|
|
415
|
+
const halfHeight = height / 2
|
|
416
|
+
|
|
417
|
+
// torso
|
|
418
|
+
const indexRows: number[][] = []
|
|
419
|
+
const slope = (radiusBottom - radiusTop) / height
|
|
420
|
+
for (let y = 0; y <= hs; y++) {
|
|
421
|
+
const indexRow: number[] = []
|
|
422
|
+
const v = y / hs
|
|
423
|
+
const radius = v * (radiusBottom - radiusTop) + radiusTop
|
|
424
|
+
for (let x = 0; x <= rs; x++) {
|
|
425
|
+
const u = x / rs
|
|
426
|
+
const theta = u * thetaLength + thetaStart
|
|
427
|
+
const sinTheta = Math.sin(theta)
|
|
428
|
+
const cosTheta = Math.cos(theta)
|
|
429
|
+
positions.push(radius * sinTheta, -v * height + halfHeight, radius * cosTheta)
|
|
430
|
+
let nx = sinTheta
|
|
431
|
+
let ny = slope
|
|
432
|
+
let nz = cosTheta
|
|
433
|
+
const len = Math.sqrt(nx * nx + ny * ny + nz * nz)
|
|
434
|
+
const inv = len > 0 ? 1 / len : 0
|
|
435
|
+
normals.push(nx * inv, ny * inv, nz * inv)
|
|
436
|
+
uvs.push(u, 1 - v)
|
|
437
|
+
indexRow.push(index)
|
|
438
|
+
index++
|
|
439
|
+
}
|
|
440
|
+
indexRows.push(indexRow)
|
|
441
|
+
}
|
|
442
|
+
for (let x = 0; x < rs; x++) {
|
|
443
|
+
for (let y = 0; y < hs; y++) {
|
|
444
|
+
const a = indexRows[y][x]
|
|
445
|
+
const b = indexRows[y + 1][x]
|
|
446
|
+
const c = indexRows[y + 1][x + 1]
|
|
447
|
+
const d = indexRows[y][x + 1]
|
|
448
|
+
// skip only the degenerate apex rows (three.js semantics)
|
|
449
|
+
if (radiusTop > 0 || y !== 0) indices.push(a, b, d)
|
|
450
|
+
if (radiusBottom > 0 || y !== hs - 1) indices.push(b, c, d)
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// caps
|
|
455
|
+
if (!openEnded) {
|
|
456
|
+
if (radiusTop > 0) {
|
|
457
|
+
index = generateCap(true, radiusTop, halfHeight, rs, thetaStart, thetaLength, positions, normals, uvs, indices, index)
|
|
458
|
+
}
|
|
459
|
+
if (radiusBottom > 0) {
|
|
460
|
+
index = generateCap(false, radiusBottom, halfHeight, rs, thetaStart, thetaLength, positions, normals, uvs, indices, index)
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
this.setIndex(indices)
|
|
465
|
+
this.setAttribute('position', new Float32BufferAttribute(positions, 3))
|
|
466
|
+
this.setAttribute('normal', new Float32BufferAttribute(normals, 3))
|
|
467
|
+
this.setAttribute('uv', new Float32BufferAttribute(uvs, 2))
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function generateCap(
|
|
472
|
+
top: boolean,
|
|
473
|
+
radius: number,
|
|
474
|
+
halfHeight: number,
|
|
475
|
+
radialSegments: number,
|
|
476
|
+
thetaStart: number,
|
|
477
|
+
thetaLength: number,
|
|
478
|
+
positions: number[],
|
|
479
|
+
normals: number[],
|
|
480
|
+
uvs: number[],
|
|
481
|
+
indices: number[],
|
|
482
|
+
startIndex: number,
|
|
483
|
+
): number {
|
|
484
|
+
let index = startIndex
|
|
485
|
+
const sign = top ? 1 : -1
|
|
486
|
+
const centerIndexStart = index
|
|
487
|
+
for (let x = 1; x <= radialSegments; x++) {
|
|
488
|
+
positions.push(0, halfHeight * sign, 0)
|
|
489
|
+
normals.push(0, sign, 0)
|
|
490
|
+
uvs.push(0.5, 0.5)
|
|
491
|
+
index++
|
|
492
|
+
}
|
|
493
|
+
const centerIndexEnd = index
|
|
494
|
+
for (let x = 0; x <= radialSegments; x++) {
|
|
495
|
+
const u = x / radialSegments
|
|
496
|
+
const theta = u * thetaLength + thetaStart
|
|
497
|
+
const cosTheta = Math.cos(theta)
|
|
498
|
+
const sinTheta = Math.sin(theta)
|
|
499
|
+
positions.push(radius * sinTheta, halfHeight * sign, radius * cosTheta)
|
|
500
|
+
normals.push(0, sign, 0)
|
|
501
|
+
uvs.push(cosTheta * 0.5 + 0.5, sinTheta * 0.5 * sign + 0.5)
|
|
502
|
+
index++
|
|
503
|
+
}
|
|
504
|
+
for (let x = 0; x < radialSegments; x++) {
|
|
505
|
+
const c = centerIndexStart + x
|
|
506
|
+
const i = centerIndexEnd + x
|
|
507
|
+
if (top) {
|
|
508
|
+
indices.push(i, i + 1, c)
|
|
509
|
+
} else {
|
|
510
|
+
indices.push(i + 1, i, c)
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return index
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
export class ConeGeometry extends CylinderGeometry {
|
|
517
|
+
constructor(
|
|
518
|
+
radius: number = 1,
|
|
519
|
+
height: number = 1,
|
|
520
|
+
radialSegments: number = 32,
|
|
521
|
+
heightSegments: number = 1,
|
|
522
|
+
openEnded: boolean = false,
|
|
523
|
+
thetaStart: number = 0,
|
|
524
|
+
thetaLength: number = Math.PI * 2,
|
|
525
|
+
) {
|
|
526
|
+
super(0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength)
|
|
527
|
+
}
|
|
528
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-only
|
|
2
|
+
// The 'three'-compatible export surface. App code written against this module
|
|
3
|
+
// should read like idiomatic three.js.
|
|
4
|
+
|
|
5
|
+
export { clamp, DEG2RAD, Color, Euler, Matrix4, Quaternion, Vector2, Vector3, Vector4 } from './math'
|
|
6
|
+
export {
|
|
7
|
+
BufferAttribute,
|
|
8
|
+
BufferGeometry,
|
|
9
|
+
Drawable,
|
|
10
|
+
Float32BufferAttribute,
|
|
11
|
+
GeometryAttributes,
|
|
12
|
+
Group,
|
|
13
|
+
InstancedBufferAttribute,
|
|
14
|
+
Intersection,
|
|
15
|
+
Object3D,
|
|
16
|
+
Scene,
|
|
17
|
+
SceneCollector,
|
|
18
|
+
Sphere,
|
|
19
|
+
} from './core'
|
|
20
|
+
export { Camera, OrthographicCamera, PerspectiveCamera } from './camera'
|
|
21
|
+
export {
|
|
22
|
+
AdditiveBlending,
|
|
23
|
+
BackSide,
|
|
24
|
+
DoubleSide,
|
|
25
|
+
FrontSide,
|
|
26
|
+
LineBasicMaterial,
|
|
27
|
+
Material,
|
|
28
|
+
MeshBasicMaterial,
|
|
29
|
+
MeshLambertMaterial,
|
|
30
|
+
MeshNormalMaterial,
|
|
31
|
+
MeshPhongMaterial,
|
|
32
|
+
MeshStandardMaterial,
|
|
33
|
+
NoBlending,
|
|
34
|
+
NormalBlending,
|
|
35
|
+
PointsMaterial,
|
|
36
|
+
SpriteMaterial,
|
|
37
|
+
} from './materials'
|
|
38
|
+
export type {
|
|
39
|
+
LineBasicMaterialParameters,
|
|
40
|
+
MaterialParameters,
|
|
41
|
+
MeshMaterialParameters,
|
|
42
|
+
PointsMaterialParameters,
|
|
43
|
+
SpriteMaterialParameters,
|
|
44
|
+
} from './materials'
|
|
45
|
+
export { Fog, FogExp2 } from './fog'
|
|
46
|
+
export { AmbientLight, DirectionalLight, HemisphereLight, Light, PointLight } from './lights'
|
|
47
|
+
export { InstancedMesh, Line, LineSegments, Mesh, Points, Sprite } from './mesh'
|
|
48
|
+
export { Ray, Raycaster } from './raycaster'
|
|
49
|
+
export {
|
|
50
|
+
BoxGeometry,
|
|
51
|
+
ConeGeometry,
|
|
52
|
+
CylinderGeometry,
|
|
53
|
+
PlaneGeometry,
|
|
54
|
+
SphereGeometry,
|
|
55
|
+
TorusGeometry,
|
|
56
|
+
TorusKnotGeometry,
|
|
57
|
+
} from './geometries'
|