lecodes-sdk 0.20.2 → 1.0.0

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.
Files changed (77) hide show
  1. package/dist/global.d.ts +17 -5
  2. package/dist/types/canvas/Canvas.d.ts +2 -0
  3. package/dist/types/gl/DecalSet.d.ts +48 -3
  4. package/dist/types/gl/Geometry.d.ts +12 -0
  5. package/dist/types/gl/Lightmap.d.ts +9 -0
  6. package/dist/types/gl/Material.d.ts +4 -0
  7. package/dist/types/gl/Mesh.d.ts +7 -1
  8. package/dist/types/gl/Model.d.ts +34 -0
  9. package/dist/types/gl/Particles.d.ts +13 -0
  10. package/dist/types/gl/Scene.d.ts +1 -1
  11. package/dist/types/gl/animation/DynamicBone.d.ts +173 -0
  12. package/dist/types/gl/{IK.d.ts → animation/IK.d.ts} +4 -4
  13. package/dist/types/gl/{Locomotion.d.ts → animation/Locomotion.d.ts} +6 -6
  14. package/dist/types/gl/{AudioSource.d.ts → audio/AudioSource.d.ts} +6 -6
  15. package/dist/types/gl/{AudioZone.d.ts → audio/AudioZone.d.ts} +4 -4
  16. package/dist/types/gl/{SceneAudio.d.ts → audio/SceneAudio.d.ts} +1 -1
  17. package/dist/types/gl/{NavAgent.d.ts → nav/NavAgent.d.ts} +4 -4
  18. package/dist/types/gl/{NavMesh.d.ts → nav/NavMesh.d.ts} +4 -4
  19. package/dist/types/gl/{CharacterController.d.ts → physics/CharacterController.d.ts} +5 -5
  20. package/dist/types/gl/{Physics.d.ts → physics/Physics.d.ts} +6 -5
  21. package/dist/types/gl/{Ragdoll.d.ts → physics/Ragdoll.d.ts} +4 -4
  22. package/dist/types/gl/{Shape.d.ts → physics/Shape.d.ts} +3 -3
  23. package/dist/types/gl/{Trigger.d.ts → physics/Trigger.d.ts} +2 -2
  24. package/dist/types/gl/{Terrain.d.ts → terrain/Terrain.d.ts} +6 -6
  25. package/dist/types/gl/{terrainMesh.d.ts → terrain/terrainMesh.d.ts} +1 -1
  26. package/dist/types/gl/vehicle/Vehicle.d.ts +300 -0
  27. package/dist/types/gl/vehicle/Wheel.d.ts +147 -0
  28. package/dist/types/inject.d.ts +28 -26
  29. package/dist/types/runtime/files.d.ts +24 -1
  30. package/dist/types/scene/defineScene.d.ts +10 -3
  31. package/dist/types.json +1 -1
  32. package/package.json +1 -1
  33. package/prompts/README.md +142 -142
  34. package/src/bridges.d.ts +1599 -1481
  35. package/src/canvas/Canvas.ts +21 -0
  36. package/src/compile/__tests__/compile.test.ts +11 -0
  37. package/src/compile/header.ts +6 -3
  38. package/src/compile/index.ts +1 -0
  39. package/src/compile/sceneEditor.ts +42 -1
  40. package/src/gl/DecalSet.ts +132 -5
  41. package/src/gl/Geometry.ts +348 -284
  42. package/src/gl/Lightmap.ts +32 -5
  43. package/src/gl/Material.ts +21 -0
  44. package/src/gl/Mesh.ts +120 -102
  45. package/src/gl/Model.ts +33 -1
  46. package/src/gl/Node.ts +1 -1
  47. package/src/gl/Particles.ts +21 -3
  48. package/src/gl/Scene.ts +1 -1
  49. package/src/gl/animation/DynamicBone.ts +459 -0
  50. package/src/gl/{IK.ts → animation/IK.ts} +4 -4
  51. package/src/gl/{Locomotion.ts → animation/Locomotion.ts} +7 -7
  52. package/src/gl/{AudioSource.ts → audio/AudioSource.ts} +7 -7
  53. package/src/gl/{AudioZone.ts → audio/AudioZone.ts} +75 -75
  54. package/src/gl/{SceneAudio.ts → audio/SceneAudio.ts} +2 -2
  55. package/src/gl/{NavAgent.ts → nav/NavAgent.ts} +5 -5
  56. package/src/gl/{NavMesh.ts → nav/NavMesh.ts} +8 -8
  57. package/src/gl/{CharacterController.ts → physics/CharacterController.ts} +5 -5
  58. package/src/gl/{Physics.ts → physics/Physics.ts} +12 -5
  59. package/src/gl/{Ragdoll.ts → physics/Ragdoll.ts} +8 -6
  60. package/src/gl/{Shape.ts → physics/Shape.ts} +3 -3
  61. package/src/gl/{Trigger.ts → physics/Trigger.ts} +45 -45
  62. package/src/gl/{physicsEvents.ts → physics/physicsEvents.ts} +1 -1
  63. package/src/gl/{Terrain.ts → terrain/Terrain.ts} +10 -10
  64. package/src/gl/{terrainMesh.ts → terrain/terrainMesh.ts} +1 -1
  65. package/src/gl/vehicle/Vehicle.ts +666 -0
  66. package/src/gl/vehicle/Wheel.ts +290 -0
  67. package/src/inject.ts +226 -224
  68. package/src/runtime/files.ts +32 -2
  69. package/src/scene/defineScene.ts +26 -10
  70. package/src/scene/level.ts +2 -2
  71. package/dist/types/gl/Gearbox.d.ts +0 -86
  72. package/dist/types/gl/Vehicle.d.ts +0 -191
  73. package/dist/types/gl/Wheel.d.ts +0 -95
  74. package/src/gl/Gearbox.ts +0 -212
  75. package/src/gl/Vehicle.ts +0 -473
  76. package/src/gl/Wheel.ts +0 -240
  77. /package/dist/types/gl/{physicsEvents.d.ts → physics/physicsEvents.d.ts} +0 -0
@@ -1,284 +1,348 @@
1
- // Mesh geometry: vertex / normal / index / uv buffers, plus the primitive builders. `Geometry.box()`
2
- // etc. return a Geometry you can hand to Mesh.from(); Mesh.box() is sugar over them. Ported from
3
- // worker/src/primitives/*.
4
-
5
- import { cx, cy, cz, type Vec3Like } from "../math/vec"
6
-
7
- export type MeshKind = "triangles" | "vertices" | "edges"
8
-
9
- // local tuple helpers — geometry building works on raw Float32Array buffers, so it stays out of the
10
- // Vec3 wrapper (no per-vertex allocation).
11
- type Tup3 = [number, number, number]
12
- const cross = (a: Tup3, b: Tup3): Tup3 => [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ]
13
- const normalize = (a: Tup3): Tup3 => {
14
- const l = Math.hypot(a[0], a[1], a[2])
15
- return l === 0 ? [ 0, 0, 0 ] : [ a[0] / l, a[1] / l, a[2] / l ]
16
- }
17
-
18
- export class Geometry {
19
- vertices: Float32Array
20
- normals: Float32Array
21
- indices: Uint16Array
22
- uv: Float32Array
23
- /** Lightmap UV set (2 floats per vertex, non-overlapping, inside the unit square) — the
24
- * `Lightmap` atlas samples a Mesh through it. The box and cylinder builders fill it (their `uv`
25
- * tiles per face, which would fold every face onto the same texels); absent = the host reuses
26
- * `uv`, which is right for a plane. */
27
- uv1?: Float32Array
28
- /** Per-vertex COLOURS: 4 bytes (r, g, b, a) per vertex, 0..255. A material that declares
29
- * `requires: [color]` reads them through `getColor()` — one mesh, many colours, no material per
30
- * shade (a debug drawer, a gradient along a curve). Absent = every vertex white, which is what
31
- * the built-in materials expect. */
32
- colors?: Uint8Array
33
- /** @internal native mesh-type enum (0 triangles / 1 edges / 2 vertices). */
34
- _meshKind = 0
35
-
36
- constructor(vertices: Float32Array, normals: Float32Array, indices: Uint16Array, uv: Float32Array) {
37
- this.vertices = vertices
38
- this.normals = normals
39
- this.indices = indices
40
- this.uv = uv
41
- }
42
-
43
- translate(x: number, y: number, z: number): this {
44
- for (let i = 0; i < this.vertices.length; i += 3) {
45
- this.vertices[i] += x; this.vertices[i + 1] += y; this.vertices[i + 2] += z
46
- }
47
- return this
48
- }
49
- scale(x: number, y: number, z: number): this {
50
- for (let i = 0; i < this.vertices.length; i += 3) {
51
- this.vertices[i] *= x; this.vertices[i + 1] *= y; this.vertices[i + 2] *= z
52
- }
53
- return this
54
- }
55
- scaleUV(x: number, y: number): this {
56
- for (let i = 0; i < this.uv.length; i += 2) { this.uv[i] *= x; this.uv[i + 1] *= y }
57
- return this
58
- }
59
- set kind(kind: MeshKind) {
60
- this._meshKind = kind === "edges" ? 1 : kind === "vertices" ? 2 : 0
61
- }
62
-
63
- /** A box; `size` (default 1) scales the vertices AND lays the lightmap chart out in proportion to
64
- * the faces' areas (a floor's top face gets the texels, not its 1 m side strips). */
65
- static box(size: Vec3Like | number = 1): Geometry { return createCube(size) }
66
- static sphere(options?: SphereOptions): Geometry { return createSphere(options) }
67
- static cylinder(options?: CylinderOptions): Geometry { return createCylinder(options) }
68
- static plane(options?: PlaneOptions): Geometry { return createPlane(options) }
69
- }
70
-
71
- // Gutter between lightmap charts (fraction of the chart square): the bake dilates each rect by a
72
- // few texels and the shader samples bilinearly, so neighbouring faces must not touch in uv1.
73
- const LM_GUTTER = 0.03
74
-
75
- type Rect = { x: number, y: number, w: number, h: number }
76
-
77
- /** Shelf-pack rectangles (metres) into the unit square at the largest uniform scale — every face
78
- * gets texels in proportion to its area, so a 64×1×64 floor's top face isn't squeezed into the
79
- * same cell as its 1 m side strips. Returns rects in input order. */
80
- const packCharts = (dims: [number, number][]): Rect[] => {
81
- const order = dims.map((_, i) => i).sort((a, b) => dims[b][1] - dims[a][1])
82
- const attempt = (scale: number): Rect[] | null => {
83
- const out: Rect[] = new Array(dims.length)
84
- let x = 0, y = 0, rowH = 0
85
- for (const i of order) {
86
- const w = dims[i][0] * scale, h = dims[i][1] * scale
87
- if (x > 0 && x + w > 1) { y += rowH + LM_GUTTER; x = 0; rowH = 0 }
88
- if (w > 1 || y + h > 1) return null
89
- out[i] = { x, y, w, h }
90
- x += w + LM_GUTTER
91
- rowH = Math.max(rowH, h)
92
- }
93
- return out
94
- }
95
- let lo = 0, hi = 1 / Math.max(...dims.map((d) => Math.max(d[0], d[1])))
96
- let best = attempt(hi)
97
- if (!best) {
98
- for (let it = 0; it < 40; it++) {
99
- const mid = (lo + hi) / 2
100
- const r = attempt(mid)
101
- if (r) { best = r; lo = mid } else hi = mid
102
- }
103
- }
104
- return best ?? dims.map(() => ({ x: 0, y: 0, w: 1, h: 1 }))
105
- }
106
-
107
- // --- cube ---
108
- const createCube = (size: Vec3Like | number = 1): Geometry => {
109
- const sx = typeof size === "number" ? size : cx(size)
110
- const sy = typeof size === "number" ? size : cy(size)
111
- const sz = typeof size === "number" ? size : cz(size)
112
- const vertices = new Float32Array(6 * 4 * 3)
113
- const normals = new Float32Array(6 * 4 * 3)
114
- const uv = new Float32Array(6 * 4 * 2)
115
- const uv1 = new Float32Array(6 * 4 * 2)
116
- const indices = new Uint16Array(6 * 6)
117
- let cnt = 0, indicesCnt = 0
118
- const sides = [ -0.5, 0.5 ]
119
- // face extents along the (a, b) parameters below: ±x faces span (z, y), ±y faces (x, z), ±z faces (x, y)
120
- const charts = packCharts([ [ sz, sy ], [ sz, sy ], [ sx, sz ], [ sx, sz ], [ sx, sy ], [ sx, sy ] ])
121
- for (let i = 0; i < 6; i++) {
122
- const s = cnt
123
- const side = i % 2 === 0 ? -0.5 : 0.5
124
- const c = charts[i]
125
- for (const a of sides) {
126
- for (const b of sides) {
127
- uv.set([ a + 0.5, b + 0.5 ], cnt * 2)
128
- uv1.set([ c.x + (a + 0.5) * c.w, c.y + (b + 0.5) * c.h ], cnt * 2)
129
- if (i === 0 || i === 1) { vertices.set([ side * sx, b * sy, -a * sz ], cnt * 3); normals.set([ side * 2, 0, 0 ], cnt * 3) }
130
- if (i === 2 || i === 3) { vertices.set([ a * sx, side * sy, -b * sz ], cnt * 3); normals.set([ 0, side * 2, 0 ], cnt * 3) }
131
- if (i === 4 || i === 5) { vertices.set([ a * sx, b * sy, side * sz ], cnt * 3); normals.set([ 0, 0, side * 2 ], cnt * 3) }
132
- cnt++
133
- }
134
- }
135
- const s1 = s + 1, s2 = s + 2, s3 = s + 3
136
- if (i % 2 === 0) indices.set([ s, s1, s2, s3, s2, s1 ], indicesCnt)
137
- else indices.set([ s2, s1, s, s1, s2, s3 ], indicesCnt)
138
- indicesCnt += 6
139
- }
140
- const g = new Geometry(vertices, normals, indices, uv)
141
- g.uv1 = uv1
142
- return g
143
- }
144
-
145
- // --- sphere ---
146
- export type SphereOptions = { radius?: number, widthSegments?: number, heightSegments?: number }
147
- const createSphere = (options: SphereOptions = {}): Geometry => {
148
- const { radius = 0.5, widthSegments = 32, heightSegments = 32 } = options
149
- const verticesCount = (widthSegments + 1) * (heightSegments - 1) + widthSegments * 2
150
- const vertices = new Float32Array(verticesCount * 3)
151
- const normals = new Float32Array(verticesCount * 3)
152
- const uvs = new Float32Array(verticesCount * 2)
153
- const indices = new Uint16Array(widthSegments * (heightSegments - 2) * 6 + widthSegments * 3 * 2)
154
- const phiLength = Math.PI * 2, thetaLength = Math.PI
155
- let vertexCnt = 0, indicesCnt = 0
156
- for (let iy = 1; iy < heightSegments; iy++) {
157
- const v = iy / heightSegments
158
- for (let ix = 0; ix <= widthSegments; ix++) {
159
- const u = ix / widthSegments
160
- const point: [number, number, number] = [
161
- -Math.cos(u * phiLength) * Math.sin(v * thetaLength),
162
- Math.cos(v * thetaLength),
163
- Math.sin(u * phiLength) * Math.sin(v * thetaLength),
164
- ]
165
- vertices.set([ point[0] * radius, point[1] * radius, point[2] * radius ], vertexCnt * 3)
166
- normals.set(point, vertexCnt * 3)
167
- uvs.set([ u, 1 - v ], vertexCnt * 2)
168
- if (iy > 1 && ix > 0) {
169
- const s = vertexCnt - 1
170
- const s1 = vertexCnt - (widthSegments + 1) - 1
171
- indices.set([ s + 1, s1, s, s1, s + 1, s1 + 1 ], indicesCnt)
172
- indicesCnt += 6
173
- }
174
- vertexCnt++
175
- }
176
- }
177
- const sides = [ -radius, radius ]
178
- const circleStart2 = vertexCnt - (widthSegments + 1)
179
- for (const side of sides) {
180
- const circleStart = side > 0 ? 0 : circleStart2
181
- for (let ix = 0; ix < widthSegments; ix++) {
182
- const u = (ix + 0.5) / widthSegments
183
- vertices.set([ 0, side, 0 ], vertexCnt * 3)
184
- normals.set([ 0, side < 0 ? -1 : 1, 0 ], vertexCnt * 3)
185
- uvs.set([ u, side < 0 ? 0 : 1 ], vertexCnt * 2)
186
- const s = circleStart + ix
187
- if (side > 0) indices.set([ s, s + 1, vertexCnt ], indicesCnt)
188
- else indices.set([ vertexCnt, s + 1, s ], indicesCnt)
189
- indicesCnt += 3
190
- vertexCnt++
191
- }
192
- }
193
- return new Geometry(vertices, normals, indices, uvs)
194
- }
195
-
196
- // --- cylinder ---
197
- export type CylinderOptions = { radius?: number, radiusTop?: number, radiusBottom?: number, edges?: number, smooth?: boolean }
198
- const createCylinder = (options: CylinderOptions = {}): Geometry => {
199
- const { edges = 32, smooth = true, radius = 0.5, radiusTop = radius, radiusBottom = radius } = options
200
- const verticesCount = edges * 4 + (edges + 1) * 2
201
- const vertices = new Float32Array(verticesCount * 3)
202
- const normals = new Float32Array(verticesCount * 3)
203
- const uv = new Float32Array(verticesCount * 2)
204
- // lightmap chart: the side strip across the upper half, the two caps as discs in the lower half
205
- const uv1 = new Float32Array(verticesCount * 2)
206
- const indices = new Uint16Array(edges * 6 + edges * 6)
207
- const sides = [ -0.5, 0.5 ]
208
- let cnt = 0, indicesCnt = 0
209
- for (let i = 0; i < edges; i++) {
210
- const s = cnt
211
- const normalX = Math.cos((i + 0.5) / edges * Math.PI * 2)
212
- const normalY = Math.sin((i + 0.5) / edges * Math.PI * 2)
213
- for (let j = 0; j < 2; j++) {
214
- const x = Math.cos((i + j) / edges * Math.PI * 2)
215
- const y = Math.sin((i + j) / edges * Math.PI * 2)
216
- for (const side of sides) {
217
- if (side > 0) vertices.set([ x * radiusTop, side, y * radiusTop ], cnt * 3)
218
- else vertices.set([ x * radiusBottom, side, y * radiusBottom ], cnt * 3)
219
- if (smooth) normals.set([ x, 0, y ], cnt * 3)
220
- else normals.set([ normalX, 0, normalY ], cnt * 3)
221
- uv.set([ (i + j) / edges, side + 0.5 ], cnt * 2)
222
- uv1.set([ (i + j) / edges, 0.55 + (side + 0.5) * 0.43 ], cnt * 2)
223
- cnt++
224
- }
225
- }
226
- const s1 = s + 1, s2 = (s + 2) % (edges * 4), s3 = (s + 3) % (edges * 4)
227
- indices.set([ s, s1, s2, s3, s2, s1 ], indicesCnt)
228
- indicesCnt += 6
229
- }
230
- for (const side of sides) {
231
- const center = cnt
232
- const capU = side > 0 ? 0.75 : 0.25 // lightmap disc centre per cap
233
- vertices.set([ 0, side, 0 ], cnt * 3); normals.set([ 0, side * 2, 0 ], cnt * 3)
234
- uv1.set([ capU, 0.25 ], cnt * 2); cnt++
235
- for (let i = 0; i < edges; i++) {
236
- const x = Math.cos(i / edges * Math.PI * 2), y = Math.sin(i / edges * Math.PI * 2)
237
- if (side > 0) vertices.set([ x * radiusTop, side, y * radiusTop ], cnt * 3)
238
- else vertices.set([ x * radiusBottom, side, y * radiusBottom ], cnt * 3)
239
- normals.set([ 0, side * 2, 0 ], cnt * 3)
240
- uv1.set([ capU + x * 0.22, 0.25 + y * 0.22 ], cnt * 2)
241
- if (side < 0) indices.set([ center, center + i + 1, center + 1 + ((i + 1) % edges) ], indicesCnt)
242
- else indices.set([ center + 1 + ((i + 1) % edges), center + i + 1, center ], indicesCnt)
243
- indicesCnt += 3
244
- cnt++
245
- }
246
- }
247
- const g = new Geometry(vertices, normals, indices, uv)
248
- g.uv1 = uv1
249
- return g
250
- }
251
-
252
- // --- plane ---
253
- export type PlaneOptions = { normal?: Vec3Like }
254
- const createPlane = (options: PlaneOptions = {}): Geometry => {
255
- const normal: Tup3 = options.normal ? [ cx(options.normal), cy(options.normal), cz(options.normal) ] : [ 0, 0, 1 ]
256
- const vertices = new Float32Array(4 * 3)
257
- const normals = new Float32Array(4 * 3)
258
- const uvs = new Float32Array(4 * 2)
259
- const sides = [ -0.5, 0.5 ]
260
- let cnt = 0
261
- let upVector: Tup3, rightVec: Tup3
262
- if (normal[1] === 0 && normal[2] === 0) { upVector = [ 0, 1, 0 ]; rightVec = [ 0, 0, -normal[0] ] }
263
- else if (normal[0] === 0 && normal[2] === 0) { upVector = [ 0, 0, 1 ]; rightVec = [ -normal[1], 0, 0 ] }
264
- else if (normal[0] === 0 && normal[1] === 0) { upVector = [ 0, 1, 0 ]; rightVec = [ normal[2], 0, 0 ] } // тут было верно
265
- else {
266
- const temp: Tup3 = Math.abs(normal[0]) < 0.5 ? [ 1, 0, 0 ] : [ 0, 0, 1 ]
267
- upVector = normalize(cross(temp, normal))
268
- rightVec = normalize(cross(upVector, normal))
269
- }
270
- for (const a of sides) {
271
- for (const b of sides) {
272
- normals.set(normal, cnt * 3)
273
- uvs.set([ a + 0.5, b + 0.5 ], cnt * 2)
274
- vertices.set([
275
- rightVec[0] * a + upVector[0] * b,
276
- rightVec[1] * a + upVector[1] * b,
277
- rightVec[2] * a + upVector[2] * b,
278
- ], cnt * 3)
279
- cnt++
280
- }
281
- }
282
- const indices = new Uint16Array([ 2, 1, 0, 1, 2, 3 ])
283
- return new Geometry(vertices, normals, indices, uvs)
284
- }
1
+ // Mesh geometry: vertex / normal / index / uv buffers, plus the primitive builders. `Geometry.box()`
2
+ // etc. return a Geometry you can hand to Mesh.from(); Mesh.box() is sugar over them. Ported from
3
+ // worker/src/primitives/*.
4
+
5
+ import { cx, cy, cz, type Vec3Like } from "../math/vec"
6
+
7
+ export type MeshKind = "triangles" | "vertices" | "edges"
8
+
9
+ // local tuple helpers — geometry building works on raw Float32Array buffers, so it stays out of the
10
+ // Vec3 wrapper (no per-vertex allocation).
11
+ type Tup3 = [number, number, number]
12
+ const cross = (a: Tup3, b: Tup3): Tup3 => [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ]
13
+ const normalize = (a: Tup3): Tup3 => {
14
+ const l = Math.hypot(a[0], a[1], a[2])
15
+ return l === 0 ? [ 0, 0, 0 ] : [ a[0] / l, a[1] / l, a[2] / l ]
16
+ }
17
+
18
+ export class Geometry {
19
+ vertices: Float32Array
20
+ normals: Float32Array
21
+ indices: Uint16Array
22
+ uv: Float32Array
23
+ /** Lightmap UV set (2 floats per vertex, non-overlapping, inside the unit square) — the
24
+ * `Lightmap` atlas samples a Mesh through it. The box and cylinder builders fill it (their `uv`
25
+ * tiles per face, which would fold every face onto the same texels); absent = the host reuses
26
+ * `uv`, which is right for a plane. */
27
+ uv1?: Float32Array
28
+ /** Per-vertex COLOURS: 4 bytes (r, g, b, a) per vertex, 0..255. A material that declares
29
+ * `requires: [color]` reads them through `getColor()` — one mesh, many colours, no material per
30
+ * shade (a debug drawer, a gradient along a curve). Absent = every vertex white, which is what
31
+ * the built-in materials expect. */
32
+ colors?: Uint8Array
33
+ /** @internal native mesh-type enum (0 triangles / 1 edges / 2 vertices). */
34
+ _meshKind = 0
35
+
36
+ constructor(vertices: Float32Array, normals: Float32Array, indices: Uint16Array, uv: Float32Array) {
37
+ this.vertices = vertices
38
+ this.normals = normals
39
+ this.indices = indices
40
+ this.uv = uv
41
+ }
42
+
43
+ translate(x: number, y: number, z: number): this {
44
+ for (let i = 0; i < this.vertices.length; i += 3) {
45
+ this.vertices[i] += x; this.vertices[i + 1] += y; this.vertices[i + 2] += z
46
+ }
47
+ return this
48
+ }
49
+ scale(x: number, y: number, z: number): this {
50
+ for (let i = 0; i < this.vertices.length; i += 3) {
51
+ this.vertices[i] *= x; this.vertices[i + 1] *= y; this.vertices[i + 2] *= z
52
+ }
53
+ return this
54
+ }
55
+ scaleUV(x: number, y: number): this {
56
+ for (let i = 0; i < this.uv.length; i += 2) { this.uv[i] *= x; this.uv[i + 1] *= y }
57
+ return this
58
+ }
59
+ set kind(kind: MeshKind) {
60
+ this._meshKind = kind === "edges" ? 1 : kind === "vertices" ? 2 : 0
61
+ }
62
+
63
+ /** A box; `size` (default 1) scales the vertices AND lays the lightmap chart out in proportion to
64
+ * the faces' areas (a floor's top face gets the texels, not its 1 m side strips). */
65
+ static box(size: Vec3Like | number = 1): Geometry { return createCube(size) }
66
+ static sphere(options?: SphereOptions): Geometry { return createSphere(options) }
67
+ static cylinder(options?: CylinderOptions): Geometry { return createCylinder(options) }
68
+ /** A capsule along Y, centred at the origin: a cylinder `length` long (between the two cap centres)
69
+ * with a hemisphere of `radius` on each end — one closed surface, smooth across the seams. */
70
+ static capsule(options?: CapsuleOptions): Geometry { return createCapsule(options) }
71
+ static plane(options?: PlaneOptions): Geometry { return createPlane(options) }
72
+ }
73
+
74
+ // Gutter between lightmap charts (fraction of the chart square): the bake dilates each rect by a
75
+ // few texels and the shader samples bilinearly, so neighbouring faces must not touch in uv1.
76
+ const LM_GUTTER = 0.03
77
+
78
+ type Rect = { x: number, y: number, w: number, h: number }
79
+
80
+ /** Shelf-pack rectangles (metres) into the unit square at the largest uniform scale — every face
81
+ * gets texels in proportion to its area, so a 64×1×64 floor's top face isn't squeezed into the
82
+ * same cell as its 1 m side strips. Returns rects in input order. */
83
+ const packCharts = (dims: [number, number][]): Rect[] => {
84
+ const order = dims.map((_, i) => i).sort((a, b) => dims[b][1] - dims[a][1])
85
+ const attempt = (scale: number): Rect[] | null => {
86
+ const out: Rect[] = new Array(dims.length)
87
+ let x = 0, y = 0, rowH = 0
88
+ for (const i of order) {
89
+ const w = dims[i][0] * scale, h = dims[i][1] * scale
90
+ if (x > 0 && x + w > 1) { y += rowH + LM_GUTTER; x = 0; rowH = 0 }
91
+ if (w > 1 || y + h > 1) return null
92
+ out[i] = { x, y, w, h }
93
+ x += w + LM_GUTTER
94
+ rowH = Math.max(rowH, h)
95
+ }
96
+ return out
97
+ }
98
+ let lo = 0, hi = 1 / Math.max(...dims.map((d) => Math.max(d[0], d[1])))
99
+ let best = attempt(hi)
100
+ if (!best) {
101
+ for (let it = 0; it < 40; it++) {
102
+ const mid = (lo + hi) / 2
103
+ const r = attempt(mid)
104
+ if (r) { best = r; lo = mid } else hi = mid
105
+ }
106
+ }
107
+ return best ?? dims.map(() => ({ x: 0, y: 0, w: 1, h: 1 }))
108
+ }
109
+
110
+ // --- cube ---
111
+ const createCube = (size: Vec3Like | number = 1): Geometry => {
112
+ const sx = typeof size === "number" ? size : cx(size)
113
+ const sy = typeof size === "number" ? size : cy(size)
114
+ const sz = typeof size === "number" ? size : cz(size)
115
+ const vertices = new Float32Array(6 * 4 * 3)
116
+ const normals = new Float32Array(6 * 4 * 3)
117
+ const uv = new Float32Array(6 * 4 * 2)
118
+ const uv1 = new Float32Array(6 * 4 * 2)
119
+ const indices = new Uint16Array(6 * 6)
120
+ let cnt = 0, indicesCnt = 0
121
+ const sides = [ -0.5, 0.5 ]
122
+ // face extents along the (a, b) parameters below: ±x faces span (z, y), ±y faces (x, z), ±z faces (x, y)
123
+ const charts = packCharts([ [ sz, sy ], [ sz, sy ], [ sx, sz ], [ sx, sz ], [ sx, sy ], [ sx, sy ] ])
124
+ for (let i = 0; i < 6; i++) {
125
+ const s = cnt
126
+ const side = i % 2 === 0 ? -0.5 : 0.5
127
+ const c = charts[i]
128
+ for (const a of sides) {
129
+ for (const b of sides) {
130
+ uv.set([ a + 0.5, b + 0.5 ], cnt * 2)
131
+ uv1.set([ c.x + (a + 0.5) * c.w, c.y + (b + 0.5) * c.h ], cnt * 2)
132
+ if (i === 0 || i === 1) { vertices.set([ side * sx, b * sy, -a * sz ], cnt * 3); normals.set([ side * 2, 0, 0 ], cnt * 3) }
133
+ if (i === 2 || i === 3) { vertices.set([ a * sx, side * sy, -b * sz ], cnt * 3); normals.set([ 0, side * 2, 0 ], cnt * 3) }
134
+ if (i === 4 || i === 5) { vertices.set([ a * sx, b * sy, side * sz ], cnt * 3); normals.set([ 0, 0, side * 2 ], cnt * 3) }
135
+ cnt++
136
+ }
137
+ }
138
+ const s1 = s + 1, s2 = s + 2, s3 = s + 3
139
+ if (i % 2 === 0) indices.set([ s, s1, s2, s3, s2, s1 ], indicesCnt)
140
+ else indices.set([ s2, s1, s, s1, s2, s3 ], indicesCnt)
141
+ indicesCnt += 6
142
+ }
143
+ const g = new Geometry(vertices, normals, indices, uv)
144
+ g.uv1 = uv1
145
+ return g
146
+ }
147
+
148
+ // --- sphere ---
149
+ export type SphereOptions = { radius?: number, widthSegments?: number, heightSegments?: number }
150
+ const createSphere = (options: SphereOptions = {}): Geometry => {
151
+ const { radius = 0.5, widthSegments = 32, heightSegments = 32 } = options
152
+ const verticesCount = (widthSegments + 1) * (heightSegments - 1) + widthSegments * 2
153
+ const vertices = new Float32Array(verticesCount * 3)
154
+ const normals = new Float32Array(verticesCount * 3)
155
+ const uvs = new Float32Array(verticesCount * 2)
156
+ const indices = new Uint16Array(widthSegments * (heightSegments - 2) * 6 + widthSegments * 3 * 2)
157
+ const phiLength = Math.PI * 2, thetaLength = Math.PI
158
+ let vertexCnt = 0, indicesCnt = 0
159
+ for (let iy = 1; iy < heightSegments; iy++) {
160
+ const v = iy / heightSegments
161
+ for (let ix = 0; ix <= widthSegments; ix++) {
162
+ const u = ix / widthSegments
163
+ const point: [number, number, number] = [
164
+ -Math.cos(u * phiLength) * Math.sin(v * thetaLength),
165
+ Math.cos(v * thetaLength),
166
+ Math.sin(u * phiLength) * Math.sin(v * thetaLength),
167
+ ]
168
+ vertices.set([ point[0] * radius, point[1] * radius, point[2] * radius ], vertexCnt * 3)
169
+ normals.set(point, vertexCnt * 3)
170
+ uvs.set([ u, 1 - v ], vertexCnt * 2)
171
+ if (iy > 1 && ix > 0) {
172
+ const s = vertexCnt - 1
173
+ const s1 = vertexCnt - (widthSegments + 1) - 1
174
+ indices.set([ s + 1, s1, s, s1, s + 1, s1 + 1 ], indicesCnt)
175
+ indicesCnt += 6
176
+ }
177
+ vertexCnt++
178
+ }
179
+ }
180
+ const sides = [ -radius, radius ]
181
+ const circleStart2 = vertexCnt - (widthSegments + 1)
182
+ for (const side of sides) {
183
+ const circleStart = side > 0 ? 0 : circleStart2
184
+ for (let ix = 0; ix < widthSegments; ix++) {
185
+ const u = (ix + 0.5) / widthSegments
186
+ vertices.set([ 0, side, 0 ], vertexCnt * 3)
187
+ normals.set([ 0, side < 0 ? -1 : 1, 0 ], vertexCnt * 3)
188
+ uvs.set([ u, side < 0 ? 0 : 1 ], vertexCnt * 2)
189
+ const s = circleStart + ix
190
+ if (side > 0) indices.set([ s, s + 1, vertexCnt ], indicesCnt)
191
+ else indices.set([ vertexCnt, s + 1, s ], indicesCnt)
192
+ indicesCnt += 3
193
+ vertexCnt++
194
+ }
195
+ }
196
+ return new Geometry(vertices, normals, indices, uvs)
197
+ }
198
+
199
+ // --- capsule ---
200
+ export type CapsuleOptions = {
201
+ radius?: number
202
+ /** Distance between the two cap centres (the straight part); 0 = a sphere. Default 1. */
203
+ length?: number
204
+ /** Segments around the axis. */
205
+ widthSegments?: number
206
+ /** Rings per hemisphere (pole to equator). */
207
+ capSegments?: number
208
+ }
209
+ const createCapsule = (options: CapsuleOptions = {}): Geometry => {
210
+ const { radius = 0.5, length = 1, widthSegments = 32, capSegments = 8 } = options
211
+ const W = widthSegments, half = length / 2
212
+ // rings from the top cap's first ring down to the bottom cap's last: the equator appears twice (once per
213
+ // cap, `length` apart) — the band between those two rings is the cylinder; the poles are fans, like the sphere
214
+ const R = capSegments * 2
215
+ const verticesCount = (W + 1) * R + W * 2
216
+ const vertices = new Float32Array(verticesCount * 3)
217
+ const normals = new Float32Array(verticesCount * 3)
218
+ const uvs = new Float32Array(verticesCount * 2)
219
+ const indices = new Uint16Array(W * (R - 1) * 6 + W * 3 * 2)
220
+ const total = Math.PI * radius + length // arc from pole to pole, for v
221
+ let vertexCnt = 0, indicesCnt = 0
222
+ for (let j = 0; j < R; j++) {
223
+ const top = j < capSegments
224
+ const theta = (top ? j + 1 : j) / capSegments * (Math.PI / 2) // 0 = the top pole, π = the bottom
225
+ const yOff = top ? half : -half
226
+ const sinT = Math.sin(theta), cosT = Math.cos(theta)
227
+ const v = ((Math.PI - theta) * radius + (top ? length : 0)) / total
228
+ for (let ix = 0; ix <= W; ix++) {
229
+ const u = ix / W
230
+ const nx = -Math.cos(u * Math.PI * 2) * sinT, nz = Math.sin(u * Math.PI * 2) * sinT
231
+ vertices.set([ nx * radius, cosT * radius + yOff, nz * radius ], vertexCnt * 3)
232
+ normals.set([ nx, cosT, nz ], vertexCnt * 3)
233
+ uvs.set([ u, v ], vertexCnt * 2)
234
+ if (j > 0 && ix > 0) {
235
+ const s = vertexCnt - 1, s1 = vertexCnt - (W + 1) - 1
236
+ indices.set([ s + 1, s1, s, s1, s + 1, s1 + 1 ], indicesCnt)
237
+ indicesCnt += 6
238
+ }
239
+ vertexCnt++
240
+ }
241
+ }
242
+ const lastRing = vertexCnt - (W + 1)
243
+ for (const side of [ 1, -1 ]) {
244
+ const circleStart = side > 0 ? 0 : lastRing
245
+ for (let ix = 0; ix < W; ix++) {
246
+ const u = (ix + 0.5) / W
247
+ vertices.set([ 0, side * (radius + half), 0 ], vertexCnt * 3)
248
+ normals.set([ 0, side, 0 ], vertexCnt * 3)
249
+ uvs.set([ u, side > 0 ? 1 : 0 ], vertexCnt * 2)
250
+ const s = circleStart + ix
251
+ if (side > 0) indices.set([ s, s + 1, vertexCnt ], indicesCnt)
252
+ else indices.set([ vertexCnt, s + 1, s ], indicesCnt)
253
+ indicesCnt += 3
254
+ vertexCnt++
255
+ }
256
+ }
257
+ return new Geometry(vertices, normals, indices, uvs)
258
+ }
259
+
260
+ // --- cylinder ---
261
+ export type CylinderOptions = { radius?: number, radiusTop?: number, radiusBottom?: number, edges?: number, smooth?: boolean }
262
+ const createCylinder = (options: CylinderOptions = {}): Geometry => {
263
+ const { edges = 32, smooth = true, radius = 0.5, radiusTop = radius, radiusBottom = radius } = options
264
+ const verticesCount = edges * 4 + (edges + 1) * 2
265
+ const vertices = new Float32Array(verticesCount * 3)
266
+ const normals = new Float32Array(verticesCount * 3)
267
+ const uv = new Float32Array(verticesCount * 2)
268
+ // lightmap chart: the side strip across the upper half, the two caps as discs in the lower half
269
+ const uv1 = new Float32Array(verticesCount * 2)
270
+ const indices = new Uint16Array(edges * 6 + edges * 6)
271
+ const sides = [ -0.5, 0.5 ]
272
+ let cnt = 0, indicesCnt = 0
273
+ for (let i = 0; i < edges; i++) {
274
+ const s = cnt
275
+ const normalX = Math.cos((i + 0.5) / edges * Math.PI * 2)
276
+ const normalY = Math.sin((i + 0.5) / edges * Math.PI * 2)
277
+ for (let j = 0; j < 2; j++) {
278
+ const x = Math.cos((i + j) / edges * Math.PI * 2)
279
+ const y = Math.sin((i + j) / edges * Math.PI * 2)
280
+ for (const side of sides) {
281
+ if (side > 0) vertices.set([ x * radiusTop, side, y * radiusTop ], cnt * 3)
282
+ else vertices.set([ x * radiusBottom, side, y * radiusBottom ], cnt * 3)
283
+ if (smooth) normals.set([ x, 0, y ], cnt * 3)
284
+ else normals.set([ normalX, 0, normalY ], cnt * 3)
285
+ uv.set([ (i + j) / edges, side + 0.5 ], cnt * 2)
286
+ uv1.set([ (i + j) / edges, 0.55 + (side + 0.5) * 0.43 ], cnt * 2)
287
+ cnt++
288
+ }
289
+ }
290
+ const s1 = s + 1, s2 = (s + 2) % (edges * 4), s3 = (s + 3) % (edges * 4)
291
+ indices.set([ s, s1, s2, s3, s2, s1 ], indicesCnt)
292
+ indicesCnt += 6
293
+ }
294
+ for (const side of sides) {
295
+ const center = cnt
296
+ const capU = side > 0 ? 0.75 : 0.25 // lightmap disc centre per cap
297
+ vertices.set([ 0, side, 0 ], cnt * 3); normals.set([ 0, side * 2, 0 ], cnt * 3)
298
+ uv1.set([ capU, 0.25 ], cnt * 2); cnt++
299
+ for (let i = 0; i < edges; i++) {
300
+ const x = Math.cos(i / edges * Math.PI * 2), y = Math.sin(i / edges * Math.PI * 2)
301
+ if (side > 0) vertices.set([ x * radiusTop, side, y * radiusTop ], cnt * 3)
302
+ else vertices.set([ x * radiusBottom, side, y * radiusBottom ], cnt * 3)
303
+ normals.set([ 0, side * 2, 0 ], cnt * 3)
304
+ uv1.set([ capU + x * 0.22, 0.25 + y * 0.22 ], cnt * 2)
305
+ if (side < 0) indices.set([ center, center + i + 1, center + 1 + ((i + 1) % edges) ], indicesCnt)
306
+ else indices.set([ center + 1 + ((i + 1) % edges), center + i + 1, center ], indicesCnt)
307
+ indicesCnt += 3
308
+ cnt++
309
+ }
310
+ }
311
+ const g = new Geometry(vertices, normals, indices, uv)
312
+ g.uv1 = uv1
313
+ return g
314
+ }
315
+
316
+ // --- plane ---
317
+ export type PlaneOptions = { normal?: Vec3Like }
318
+ const createPlane = (options: PlaneOptions = {}): Geometry => {
319
+ const normal: Tup3 = options.normal ? [ cx(options.normal), cy(options.normal), cz(options.normal) ] : [ 0, 0, 1 ]
320
+ const vertices = new Float32Array(4 * 3)
321
+ const normals = new Float32Array(4 * 3)
322
+ const uvs = new Float32Array(4 * 2)
323
+ const sides = [ -0.5, 0.5 ]
324
+ let cnt = 0
325
+ let upVector: Tup3, rightVec: Tup3
326
+ if (normal[1] === 0 && normal[2] === 0) { upVector = [ 0, 1, 0 ]; rightVec = [ 0, 0, -normal[0] ] }
327
+ else if (normal[0] === 0 && normal[2] === 0) { upVector = [ 0, 0, 1 ]; rightVec = [ -normal[1], 0, 0 ] }
328
+ else if (normal[0] === 0 && normal[1] === 0) { upVector = [ 0, 1, 0 ]; rightVec = [ normal[2], 0, 0 ] } // тут было верно
329
+ else {
330
+ const temp: Tup3 = Math.abs(normal[0]) < 0.5 ? [ 1, 0, 0 ] : [ 0, 0, 1 ]
331
+ upVector = normalize(cross(temp, normal))
332
+ rightVec = normalize(cross(upVector, normal))
333
+ }
334
+ for (const a of sides) {
335
+ for (const b of sides) {
336
+ normals.set(normal, cnt * 3)
337
+ uvs.set([ a + 0.5, b + 0.5 ], cnt * 2)
338
+ vertices.set([
339
+ rightVec[0] * a + upVector[0] * b,
340
+ rightVec[1] * a + upVector[1] * b,
341
+ rightVec[2] * a + upVector[2] * b,
342
+ ], cnt * 3)
343
+ cnt++
344
+ }
345
+ }
346
+ const indices = new Uint16Array([ 2, 1, 0, 1, 2, 3 ])
347
+ return new Geometry(vertices, normals, indices, uvs)
348
+ }