lecodes-sdk 0.20.0 → 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 (114) hide show
  1. package/dist/global.d.ts +48 -5
  2. package/dist/inject.js +361 -260
  3. package/dist/types/audio/Bus.d.ts +45 -0
  4. package/dist/types/audio/Sound.d.ts +28 -0
  5. package/dist/types/audio/Voice.d.ts +27 -0
  6. package/dist/types/audio/audio.d.ts +83 -0
  7. package/dist/types/audio/support.d.ts +1 -0
  8. package/dist/types/canvas/Canvas.d.ts +2 -0
  9. package/dist/types/gl/DecalSet.d.ts +148 -0
  10. package/dist/types/gl/Geometry.d.ts +17 -0
  11. package/dist/types/gl/Light.d.ts +7 -0
  12. package/dist/types/gl/Lightmap.d.ts +9 -0
  13. package/dist/types/gl/Material.d.ts +90 -2
  14. package/dist/types/gl/Mesh.d.ts +18 -1
  15. package/dist/types/gl/Model.d.ts +34 -0
  16. package/dist/types/gl/Particles.d.ts +13 -0
  17. package/dist/types/gl/Scene.d.ts +23 -0
  18. package/dist/types/gl/Texture.d.ts +29 -1
  19. package/dist/types/gl/animation/AnimationClip.d.ts +25 -12
  20. package/dist/types/gl/animation/DynamicBone.d.ts +173 -0
  21. package/dist/types/gl/{IK.d.ts → animation/IK.d.ts} +4 -4
  22. package/dist/types/gl/{Locomotion.d.ts → animation/Locomotion.d.ts} +6 -6
  23. package/dist/types/gl/animation/core.d.ts +15 -10
  24. package/dist/types/gl/audio/AudioSource.d.ts +60 -0
  25. package/dist/types/gl/audio/AudioZone.d.ts +32 -0
  26. package/dist/types/gl/audio/SceneAudio.d.ts +11 -0
  27. package/dist/types/gl/{NavAgent.d.ts → nav/NavAgent.d.ts} +4 -4
  28. package/dist/types/gl/{NavMesh.d.ts → nav/NavMesh.d.ts} +4 -4
  29. package/dist/types/gl/{CharacterController.d.ts → physics/CharacterController.d.ts} +5 -5
  30. package/dist/types/gl/{Physics.d.ts → physics/Physics.d.ts} +6 -5
  31. package/dist/types/gl/{Ragdoll.d.ts → physics/Ragdoll.d.ts} +4 -4
  32. package/dist/types/gl/{Shape.d.ts → physics/Shape.d.ts} +3 -3
  33. package/dist/types/gl/{Trigger.d.ts → physics/Trigger.d.ts} +2 -2
  34. package/dist/types/gl/state.d.ts +0 -1
  35. package/dist/types/gl/{Terrain.d.ts → terrain/Terrain.d.ts} +6 -6
  36. package/dist/types/gl/{terrainMesh.d.ts → terrain/terrainMesh.d.ts} +1 -1
  37. package/dist/types/gl/vehicle/Vehicle.d.ts +300 -0
  38. package/dist/types/gl/vehicle/Wheel.d.ts +147 -0
  39. package/dist/types/inject.d.ts +33 -21
  40. package/dist/types/runtime/files.d.ts +24 -1
  41. package/dist/types/runtime/input.d.ts +11 -0
  42. package/dist/types/scene/defineScene.d.ts +10 -3
  43. package/dist/types/ui/UIImage.d.ts +15 -5
  44. package/dist/types.json +1 -1
  45. package/package.json +1 -1
  46. package/prompts/README.md +142 -142
  47. package/prompts/dist/2d-game.md +408 -197
  48. package/prompts/dist/3d-app.md +491 -166
  49. package/prompts/dist/ar-app.md +373 -163
  50. package/prompts/dist/design.md +83 -87
  51. package/prompts/dist/ui-app.md +325 -136
  52. package/src/audio/Bus.ts +102 -0
  53. package/src/audio/Sound.ts +96 -0
  54. package/src/audio/Voice.ts +102 -0
  55. package/src/audio/audio.ts +161 -0
  56. package/src/audio/support.ts +6 -0
  57. package/src/bridges.d.ts +279 -32
  58. package/src/canvas/Canvas.ts +21 -0
  59. package/src/compile/__tests__/compile.test.ts +11 -0
  60. package/src/compile/compileProject.ts +30 -15
  61. package/src/compile/header.ts +6 -3
  62. package/src/compile/index.ts +4 -0
  63. package/src/compile/sceneEditor.ts +42 -1
  64. package/src/core/Aspect.ts +33 -8
  65. package/src/g2/Scene2D.ts +7 -0
  66. package/src/gl/CameraPlace.ts +52 -52
  67. package/src/gl/DecalSet.ts +360 -0
  68. package/src/gl/Geometry.ts +348 -279
  69. package/src/gl/Light.ts +16 -0
  70. package/src/gl/Lightmap.ts +35 -7
  71. package/src/gl/Material.ts +173 -4
  72. package/src/gl/Mesh.ts +120 -83
  73. package/src/gl/Model.ts +33 -1
  74. package/src/gl/Node.ts +1 -1
  75. package/src/gl/Particles.ts +21 -3
  76. package/src/gl/Scene.ts +41 -7
  77. package/src/gl/Texture.ts +43 -3
  78. package/src/gl/animation/AnimationClip.ts +43 -20
  79. package/src/gl/animation/Animator.ts +4 -3
  80. package/src/gl/animation/DynamicBone.ts +459 -0
  81. package/src/gl/{IK.ts → animation/IK.ts} +4 -4
  82. package/src/gl/{Locomotion.ts → animation/Locomotion.ts} +7 -7
  83. package/src/gl/animation/core.ts +20 -15
  84. package/src/gl/audio/AudioSource.ts +113 -0
  85. package/src/gl/audio/AudioZone.ts +75 -0
  86. package/src/gl/audio/SceneAudio.ts +26 -0
  87. package/src/gl/{NavAgent.ts → nav/NavAgent.ts} +5 -5
  88. package/src/gl/{NavMesh.ts → nav/NavMesh.ts} +8 -8
  89. package/src/gl/{CharacterController.ts → physics/CharacterController.ts} +5 -5
  90. package/src/gl/{Physics.ts → physics/Physics.ts} +12 -5
  91. package/src/gl/{Ragdoll.ts → physics/Ragdoll.ts} +272 -270
  92. package/src/gl/{Shape.ts → physics/Shape.ts} +3 -3
  93. package/src/gl/{Trigger.ts → physics/Trigger.ts} +2 -2
  94. package/src/gl/{physicsEvents.ts → physics/physicsEvents.ts} +1 -1
  95. package/src/gl/scenarios.ts +291 -291
  96. package/src/gl/state.ts +1 -1
  97. package/src/gl/{Terrain.ts → terrain/Terrain.ts} +10 -10
  98. package/src/gl/{terrainMesh.ts → terrain/terrainMesh.ts} +1 -1
  99. package/src/gl/vehicle/Vehicle.ts +666 -0
  100. package/src/gl/vehicle/Wheel.ts +290 -0
  101. package/src/inject.ts +226 -212
  102. package/src/runtime/files.ts +32 -2
  103. package/src/runtime/input.ts +6 -1
  104. package/src/scene/defineScene.ts +26 -10
  105. package/src/scene/gizmos.ts +148 -148
  106. package/src/scene/level.ts +2 -2
  107. package/src/ui/UIImage.ts +21 -7
  108. package/dist/types/gl/Gearbox.d.ts +0 -86
  109. package/dist/types/gl/Vehicle.d.ts +0 -191
  110. package/dist/types/gl/Wheel.d.ts +0 -95
  111. package/src/gl/Gearbox.ts +0 -212
  112. package/src/gl/Vehicle.ts +0 -473
  113. package/src/gl/Wheel.ts +0 -240
  114. /package/dist/types/gl/{physicsEvents.d.ts → physics/physicsEvents.d.ts} +0 -0
@@ -1,279 +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
- /** @internal native mesh-type enum (0 triangles / 1 edges / 2 vertices). */
29
- _meshKind = 0
30
-
31
- constructor(vertices: Float32Array, normals: Float32Array, indices: Uint16Array, uv: Float32Array) {
32
- this.vertices = vertices
33
- this.normals = normals
34
- this.indices = indices
35
- this.uv = uv
36
- }
37
-
38
- translate(x: number, y: number, z: number): this {
39
- for (let i = 0; i < this.vertices.length; i += 3) {
40
- this.vertices[i] += x; this.vertices[i + 1] += y; this.vertices[i + 2] += z
41
- }
42
- return this
43
- }
44
- scale(x: number, y: number, z: number): this {
45
- for (let i = 0; i < this.vertices.length; i += 3) {
46
- this.vertices[i] *= x; this.vertices[i + 1] *= y; this.vertices[i + 2] *= z
47
- }
48
- return this
49
- }
50
- scaleUV(x: number, y: number): this {
51
- for (let i = 0; i < this.uv.length; i += 2) { this.uv[i] *= x; this.uv[i + 1] *= y }
52
- return this
53
- }
54
- set kind(kind: MeshKind) {
55
- this._meshKind = kind === "edges" ? 1 : kind === "vertices" ? 2 : 0
56
- }
57
-
58
- /** A box; `size` (default 1) scales the vertices AND lays the lightmap chart out in proportion to
59
- * the faces' areas (a floor's top face gets the texels, not its 1 m side strips). */
60
- static box(size: Vec3Like | number = 1): Geometry { return createCube(size) }
61
- static sphere(options?: SphereOptions): Geometry { return createSphere(options) }
62
- static cylinder(options?: CylinderOptions): Geometry { return createCylinder(options) }
63
- static plane(options?: PlaneOptions): Geometry { return createPlane(options) }
64
- }
65
-
66
- // Gutter between lightmap charts (fraction of the chart square): the bake dilates each rect by a
67
- // few texels and the shader samples bilinearly, so neighbouring faces must not touch in uv1.
68
- const LM_GUTTER = 0.03
69
-
70
- type Rect = { x: number, y: number, w: number, h: number }
71
-
72
- /** Shelf-pack rectangles (metres) into the unit square at the largest uniform scale — every face
73
- * gets texels in proportion to its area, so a 64×1×64 floor's top face isn't squeezed into the
74
- * same cell as its 1 m side strips. Returns rects in input order. */
75
- const packCharts = (dims: [number, number][]): Rect[] => {
76
- const order = dims.map((_, i) => i).sort((a, b) => dims[b][1] - dims[a][1])
77
- const attempt = (scale: number): Rect[] | null => {
78
- const out: Rect[] = new Array(dims.length)
79
- let x = 0, y = 0, rowH = 0
80
- for (const i of order) {
81
- const w = dims[i][0] * scale, h = dims[i][1] * scale
82
- if (x > 0 && x + w > 1) { y += rowH + LM_GUTTER; x = 0; rowH = 0 }
83
- if (w > 1 || y + h > 1) return null
84
- out[i] = { x, y, w, h }
85
- x += w + LM_GUTTER
86
- rowH = Math.max(rowH, h)
87
- }
88
- return out
89
- }
90
- let lo = 0, hi = 1 / Math.max(...dims.map((d) => Math.max(d[0], d[1])))
91
- let best = attempt(hi)
92
- if (!best) {
93
- for (let it = 0; it < 40; it++) {
94
- const mid = (lo + hi) / 2
95
- const r = attempt(mid)
96
- if (r) { best = r; lo = mid } else hi = mid
97
- }
98
- }
99
- return best ?? dims.map(() => ({ x: 0, y: 0, w: 1, h: 1 }))
100
- }
101
-
102
- // --- cube ---
103
- const createCube = (size: Vec3Like | number = 1): Geometry => {
104
- const sx = typeof size === "number" ? size : cx(size)
105
- const sy = typeof size === "number" ? size : cy(size)
106
- const sz = typeof size === "number" ? size : cz(size)
107
- const vertices = new Float32Array(6 * 4 * 3)
108
- const normals = new Float32Array(6 * 4 * 3)
109
- const uv = new Float32Array(6 * 4 * 2)
110
- const uv1 = new Float32Array(6 * 4 * 2)
111
- const indices = new Uint16Array(6 * 6)
112
- let cnt = 0, indicesCnt = 0
113
- const sides = [ -0.5, 0.5 ]
114
- // face extents along the (a, b) parameters below: ±x faces span (z, y), ±y faces (x, z), ±z faces (x, y)
115
- const charts = packCharts([ [ sz, sy ], [ sz, sy ], [ sx, sz ], [ sx, sz ], [ sx, sy ], [ sx, sy ] ])
116
- for (let i = 0; i < 6; i++) {
117
- const s = cnt
118
- const side = i % 2 === 0 ? -0.5 : 0.5
119
- const c = charts[i]
120
- for (const a of sides) {
121
- for (const b of sides) {
122
- uv.set([ a + 0.5, b + 0.5 ], cnt * 2)
123
- uv1.set([ c.x + (a + 0.5) * c.w, c.y + (b + 0.5) * c.h ], cnt * 2)
124
- if (i === 0 || i === 1) { vertices.set([ side * sx, b * sy, -a * sz ], cnt * 3); normals.set([ side * 2, 0, 0 ], cnt * 3) }
125
- if (i === 2 || i === 3) { vertices.set([ a * sx, side * sy, -b * sz ], cnt * 3); normals.set([ 0, side * 2, 0 ], cnt * 3) }
126
- if (i === 4 || i === 5) { vertices.set([ a * sx, b * sy, side * sz ], cnt * 3); normals.set([ 0, 0, side * 2 ], cnt * 3) }
127
- cnt++
128
- }
129
- }
130
- const s1 = s + 1, s2 = s + 2, s3 = s + 3
131
- if (i % 2 === 0) indices.set([ s, s1, s2, s3, s2, s1 ], indicesCnt)
132
- else indices.set([ s2, s1, s, s1, s2, s3 ], indicesCnt)
133
- indicesCnt += 6
134
- }
135
- const g = new Geometry(vertices, normals, indices, uv)
136
- g.uv1 = uv1
137
- return g
138
- }
139
-
140
- // --- sphere ---
141
- export type SphereOptions = { radius?: number, widthSegments?: number, heightSegments?: number }
142
- const createSphere = (options: SphereOptions = {}): Geometry => {
143
- const { radius = 0.5, widthSegments = 32, heightSegments = 32 } = options
144
- const verticesCount = (widthSegments + 1) * (heightSegments - 1) + widthSegments * 2
145
- const vertices = new Float32Array(verticesCount * 3)
146
- const normals = new Float32Array(verticesCount * 3)
147
- const uvs = new Float32Array(verticesCount * 2)
148
- const indices = new Uint16Array(widthSegments * (heightSegments - 2) * 6 + widthSegments * 3 * 2)
149
- const phiLength = Math.PI * 2, thetaLength = Math.PI
150
- let vertexCnt = 0, indicesCnt = 0
151
- for (let iy = 1; iy < heightSegments; iy++) {
152
- const v = iy / heightSegments
153
- for (let ix = 0; ix <= widthSegments; ix++) {
154
- const u = ix / widthSegments
155
- const point: [number, number, number] = [
156
- -Math.cos(u * phiLength) * Math.sin(v * thetaLength),
157
- Math.cos(v * thetaLength),
158
- Math.sin(u * phiLength) * Math.sin(v * thetaLength),
159
- ]
160
- vertices.set([ point[0] * radius, point[1] * radius, point[2] * radius ], vertexCnt * 3)
161
- normals.set(point, vertexCnt * 3)
162
- uvs.set([ u, 1 - v ], vertexCnt * 2)
163
- if (iy > 1 && ix > 0) {
164
- const s = vertexCnt - 1
165
- const s1 = vertexCnt - (widthSegments + 1) - 1
166
- indices.set([ s + 1, s1, s, s1, s + 1, s1 + 1 ], indicesCnt)
167
- indicesCnt += 6
168
- }
169
- vertexCnt++
170
- }
171
- }
172
- const sides = [ -radius, radius ]
173
- const circleStart2 = vertexCnt - (widthSegments + 1)
174
- for (const side of sides) {
175
- const circleStart = side > 0 ? 0 : circleStart2
176
- for (let ix = 0; ix < widthSegments; ix++) {
177
- const u = (ix + 0.5) / widthSegments
178
- vertices.set([ 0, side, 0 ], vertexCnt * 3)
179
- normals.set([ 0, side < 0 ? -1 : 1, 0 ], vertexCnt * 3)
180
- uvs.set([ u, side < 0 ? 0 : 1 ], vertexCnt * 2)
181
- const s = circleStart + ix
182
- if (side > 0) indices.set([ s, s + 1, vertexCnt ], indicesCnt)
183
- else indices.set([ vertexCnt, s + 1, s ], indicesCnt)
184
- indicesCnt += 3
185
- vertexCnt++
186
- }
187
- }
188
- return new Geometry(vertices, normals, indices, uvs)
189
- }
190
-
191
- // --- cylinder ---
192
- export type CylinderOptions = { radius?: number, radiusTop?: number, radiusBottom?: number, edges?: number, smooth?: boolean }
193
- const createCylinder = (options: CylinderOptions = {}): Geometry => {
194
- const { edges = 32, smooth = true, radius = 0.5, radiusTop = radius, radiusBottom = radius } = options
195
- const verticesCount = edges * 4 + (edges + 1) * 2
196
- const vertices = new Float32Array(verticesCount * 3)
197
- const normals = new Float32Array(verticesCount * 3)
198
- const uv = new Float32Array(verticesCount * 2)
199
- // lightmap chart: the side strip across the upper half, the two caps as discs in the lower half
200
- const uv1 = new Float32Array(verticesCount * 2)
201
- const indices = new Uint16Array(edges * 6 + edges * 6)
202
- const sides = [ -0.5, 0.5 ]
203
- let cnt = 0, indicesCnt = 0
204
- for (let i = 0; i < edges; i++) {
205
- const s = cnt
206
- const normalX = Math.cos((i + 0.5) / edges * Math.PI * 2)
207
- const normalY = Math.sin((i + 0.5) / edges * Math.PI * 2)
208
- for (let j = 0; j < 2; j++) {
209
- const x = Math.cos((i + j) / edges * Math.PI * 2)
210
- const y = Math.sin((i + j) / edges * Math.PI * 2)
211
- for (const side of sides) {
212
- if (side > 0) vertices.set([ x * radiusTop, side, y * radiusTop ], cnt * 3)
213
- else vertices.set([ x * radiusBottom, side, y * radiusBottom ], cnt * 3)
214
- if (smooth) normals.set([ x, 0, y ], cnt * 3)
215
- else normals.set([ normalX, 0, normalY ], cnt * 3)
216
- uv.set([ (i + j) / edges, side + 0.5 ], cnt * 2)
217
- uv1.set([ (i + j) / edges, 0.55 + (side + 0.5) * 0.43 ], cnt * 2)
218
- cnt++
219
- }
220
- }
221
- const s1 = s + 1, s2 = (s + 2) % (edges * 4), s3 = (s + 3) % (edges * 4)
222
- indices.set([ s, s1, s2, s3, s2, s1 ], indicesCnt)
223
- indicesCnt += 6
224
- }
225
- for (const side of sides) {
226
- const center = cnt
227
- const capU = side > 0 ? 0.75 : 0.25 // lightmap disc centre per cap
228
- vertices.set([ 0, side, 0 ], cnt * 3); normals.set([ 0, side * 2, 0 ], cnt * 3)
229
- uv1.set([ capU, 0.25 ], cnt * 2); cnt++
230
- for (let i = 0; i < edges; i++) {
231
- const x = Math.cos(i / edges * Math.PI * 2), y = Math.sin(i / edges * Math.PI * 2)
232
- if (side > 0) vertices.set([ x * radiusTop, side, y * radiusTop ], cnt * 3)
233
- else vertices.set([ x * radiusBottom, side, y * radiusBottom ], cnt * 3)
234
- normals.set([ 0, side * 2, 0 ], cnt * 3)
235
- uv1.set([ capU + x * 0.22, 0.25 + y * 0.22 ], cnt * 2)
236
- if (side < 0) indices.set([ center, center + i + 1, center + 1 + ((i + 1) % edges) ], indicesCnt)
237
- else indices.set([ center + 1 + ((i + 1) % edges), center + i + 1, center ], indicesCnt)
238
- indicesCnt += 3
239
- cnt++
240
- }
241
- }
242
- const g = new Geometry(vertices, normals, indices, uv)
243
- g.uv1 = uv1
244
- return g
245
- }
246
-
247
- // --- plane ---
248
- export type PlaneOptions = { normal?: Vec3Like }
249
- const createPlane = (options: PlaneOptions = {}): Geometry => {
250
- const normal: Tup3 = options.normal ? [ cx(options.normal), cy(options.normal), cz(options.normal) ] : [ 0, 0, 1 ]
251
- const vertices = new Float32Array(4 * 3)
252
- const normals = new Float32Array(4 * 3)
253
- const uvs = new Float32Array(4 * 2)
254
- const sides = [ -0.5, 0.5 ]
255
- let cnt = 0
256
- let upVector: Tup3, rightVec: Tup3
257
- if (normal[1] === 0 && normal[2] === 0) { upVector = [ 0, 1, 0 ]; rightVec = [ 0, 0, -normal[0] ] }
258
- else if (normal[0] === 0 && normal[2] === 0) { upVector = [ 0, 0, 1 ]; rightVec = [ -normal[1], 0, 0 ] }
259
- else if (normal[0] === 0 && normal[1] === 0) { upVector = [ 0, 1, 0 ]; rightVec = [ normal[2], 0, 0 ] } // тут было верно
260
- else {
261
- const temp: Tup3 = Math.abs(normal[0]) < 0.5 ? [ 1, 0, 0 ] : [ 0, 0, 1 ]
262
- upVector = normalize(cross(temp, normal))
263
- rightVec = normalize(cross(upVector, normal))
264
- }
265
- for (const a of sides) {
266
- for (const b of sides) {
267
- normals.set(normal, cnt * 3)
268
- uvs.set([ a + 0.5, b + 0.5 ], cnt * 2)
269
- vertices.set([
270
- rightVec[0] * a + upVector[0] * b,
271
- rightVec[1] * a + upVector[1] * b,
272
- rightVec[2] * a + upVector[2] * b,
273
- ], cnt * 3)
274
- cnt++
275
- }
276
- }
277
- const indices = new Uint16Array([ 2, 1, 0, 1, 2, 3 ])
278
- return new Geometry(vertices, normals, indices, uvs)
279
- }
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
+ }