@kerf-js/keel 0.0.0-stage → 0.1.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 (192) hide show
  1. package/LICENSE +16 -0
  2. package/README.md +306 -2
  3. package/package.json +59 -3
  4. package/src/blend-inset.ts +79 -0
  5. package/src/blend.ts +120 -0
  6. package/src/client.d.ts +9 -0
  7. package/src/generate/bust.ts +24 -0
  8. package/src/generate/bust.worker.ts +4 -0
  9. package/src/generate/pack.ts +69 -0
  10. package/src/generate/plate.ts +51 -0
  11. package/src/generate/plate.worker.ts +4 -0
  12. package/src/generate/serve.ts +56 -0
  13. package/src/generate/surface.ts +16 -0
  14. package/src/generate/surface.worker.ts +4 -0
  15. package/src/generate.ts +133 -0
  16. package/src/governor.ts +87 -0
  17. package/src/index.d.ts +15 -0
  18. package/src/index.mjs +63 -0
  19. package/src/lit.d.ts +384 -0
  20. package/src/paint-live.ts +60 -0
  21. package/src/paint-recipe.d.mts +29 -0
  22. package/src/paint-recipe.mjs +29 -0
  23. package/src/paint-worker.mjs +20 -0
  24. package/src/paint.d.mts +37 -0
  25. package/src/paint.mjs +180 -0
  26. package/src/runtime.ts +35 -0
  27. package/src/scene.ts +289 -0
  28. package/src/stage.ts +192 -0
  29. package/vendor/LICENSE +21 -0
  30. package/vendor/LICENSE-modules +21 -0
  31. package/vendor/NOTICE +24 -0
  32. package/vendor/README.md +13 -0
  33. package/vendor/lit.d.mts +1 -0
  34. package/vendor/lit.mjs +2 -0
  35. package/vendor/lock.json +177 -0
  36. package/vendor/modules/band/events.d.ts +20 -0
  37. package/vendor/modules/band/index.d.ts +29 -0
  38. package/vendor/modules/band/player.d.ts +43 -0
  39. package/vendor/modules/band/roll.d.ts +14 -0
  40. package/vendor/modules/band/score.d.ts +45 -0
  41. package/vendor/modules/band/styles.data.d.ts +37 -0
  42. package/vendor/modules/band/voices.d.ts +33 -0
  43. package/vendor/modules/band.d.mts +1 -0
  44. package/vendor/modules/band.mjs +2 -0
  45. package/vendor/modules/butterflies/butterflies.glsl.d.ts +2 -0
  46. package/vendor/modules/butterflies/flight.d.ts +68 -0
  47. package/vendor/modules/butterflies/index.d.ts +79 -0
  48. package/vendor/modules/butterflies/wing.d.ts +38 -0
  49. package/vendor/modules/butterflies.d.mts +1 -0
  50. package/vendor/modules/butterflies.inputs.json +73 -0
  51. package/vendor/modules/butterflies.mjs +39 -0
  52. package/vendor/modules/camera-rail/index.d.ts +113 -0
  53. package/vendor/modules/camera-rail.d.mts +1 -0
  54. package/vendor/modules/camera-rail.mjs +2 -0
  55. package/vendor/modules/device-profile/index.d.ts +47 -0
  56. package/vendor/modules/device-profile.d.mts +1 -0
  57. package/vendor/modules/device-profile.mjs +2 -0
  58. package/vendor/modules/lit-finish/dress.d.ts +34 -0
  59. package/vendor/modules/lit-finish/finish.glsl.d.ts +15 -0
  60. package/vendor/modules/lit-finish/index.d.ts +33 -0
  61. package/vendor/modules/lit-finish/lit.d.ts +52 -0
  62. package/vendor/modules/lit-finish/pass.d.ts +35 -0
  63. package/vendor/modules/lit-finish/settings.d.ts +88 -0
  64. package/vendor/modules/lit-finish.d.mts +1 -0
  65. package/vendor/modules/lit-finish.inputs.json +200 -0
  66. package/vendor/modules/lit-finish.mjs +267 -0
  67. package/vendor/modules/look/index.d.ts +159 -0
  68. package/vendor/modules/look.d.mts +1 -0
  69. package/vendor/modules/look.inputs.json +23 -0
  70. package/vendor/modules/look.mjs +2 -0
  71. package/vendor/modules/motes/index.d.ts +83 -0
  72. package/vendor/modules/motes/motes.glsl.d.ts +9 -0
  73. package/vendor/modules/motes/place.d.ts +59 -0
  74. package/vendor/modules/motes.d.mts +1 -0
  75. package/vendor/modules/motes.inputs.json +116 -0
  76. package/vendor/modules/motes.mjs +68 -0
  77. package/vendor/modules/plate-painter/dab.d.ts +44 -0
  78. package/vendor/modules/plate-painter/exact.d.ts +19 -0
  79. package/vendor/modules/plate-painter/foliage.d.ts +4 -0
  80. package/vendor/modules/plate-painter/gpu.d.ts +65 -0
  81. package/vendor/modules/plate-painter/index.d.ts +14 -0
  82. package/vendor/modules/plate-painter/paint.glsl.d.ts +14 -0
  83. package/vendor/modules/plate-painter/plan.d.ts +42 -0
  84. package/vendor/modules/plate-painter/recipe.d.ts +136 -0
  85. package/vendor/modules/plate-painter/scene.d.ts +4 -0
  86. package/vendor/modules/plate-painter.d.mts +1 -0
  87. package/vendor/modules/plate-painter.inputs.json +50 -0
  88. package/vendor/modules/plate-painter.mjs +384 -0
  89. package/vendor/modules/portrait/body.d.ts +25 -0
  90. package/vendor/modules/portrait/bust.d.ts +67 -0
  91. package/vendor/modules/portrait/cap.d.ts +12 -0
  92. package/vendor/modules/portrait/compile.d.ts +8 -0
  93. package/vendor/modules/portrait/exact.d.ts +17 -0
  94. package/vendor/modules/portrait/hair.d.ts +26 -0
  95. package/vendor/modules/portrait/hand.d.ts +30 -0
  96. package/vendor/modules/portrait/head.d.ts +18 -0
  97. package/vendor/modules/portrait/index.d.ts +12 -0
  98. package/vendor/modules/portrait/mesh.d.ts +24 -0
  99. package/vendor/modules/portrait/parts.d.ts +33 -0
  100. package/vendor/modules/portrait/portrait.glsl.d.ts +4 -0
  101. package/vendor/modules/portrait/render.d.ts +125 -0
  102. package/vendor/modules/portrait/sdf.d.ts +75 -0
  103. package/vendor/modules/portrait/settings.d.ts +27 -0
  104. package/vendor/modules/portrait.d.mts +1 -0
  105. package/vendor/modules/portrait.inputs.json +111 -0
  106. package/vendor/modules/portrait.mjs +2 -0
  107. package/vendor/modules/quality/index.d.ts +130 -0
  108. package/vendor/modules/quality.d.mts +1 -0
  109. package/vendor/modules/quality.inputs.json +34 -0
  110. package/vendor/modules/quality.mjs +2 -0
  111. package/vendor/modules/scene-blend/blend.d.ts +54 -0
  112. package/vendor/modules/scene-blend/blend.glsl.d.ts +8 -0
  113. package/vendor/modules/scene-blend/index.d.ts +25 -0
  114. package/vendor/modules/scene-blend/lit.d.ts +61 -0
  115. package/vendor/modules/scene-blend/noise.d.ts +13 -0
  116. package/vendor/modules/scene-blend/settings.d.ts +63 -0
  117. package/vendor/modules/scene-blend.d.mts +1 -0
  118. package/vendor/modules/scene-blend.inputs.json +133 -0
  119. package/vendor/modules/scene-blend.mjs +2 -0
  120. package/vendor/modules/section-post/index.d.ts +23 -0
  121. package/vendor/modules/section-post/lit.d.ts +47 -0
  122. package/vendor/modules/section-post/noise.d.ts +14 -0
  123. package/vendor/modules/section-post/post.d.ts +31 -0
  124. package/vendor/modules/section-post/post.glsl.d.ts +8 -0
  125. package/vendor/modules/section-post/settings.d.ts +84 -0
  126. package/vendor/modules/section-post.d.mts +1 -0
  127. package/vendor/modules/section-post.inputs.json +107 -0
  128. package/vendor/modules/section-post.mjs +53 -0
  129. package/vendor/modules/surfaces/exact.d.ts +8 -0
  130. package/vendor/modules/surfaces/field.d.ts +7 -0
  131. package/vendor/modules/surfaces/gpu.d.ts +19 -0
  132. package/vendor/modules/surfaces/index.d.ts +17 -0
  133. package/vendor/modules/surfaces/paint.d.ts +28 -0
  134. package/vendor/modules/surfaces/plan.d.ts +39 -0
  135. package/vendor/modules/surfaces/print.d.ts +55 -0
  136. package/vendor/modules/surfaces/recipe.d.ts +143 -0
  137. package/vendor/modules/surfaces/surface.glsl.d.ts +4 -0
  138. package/vendor/modules/surfaces.d.mts +1 -0
  139. package/vendor/modules/surfaces.inputs.json +54 -0
  140. package/vendor/modules/surfaces.mjs +172 -0
  141. package/vendor/modules/tracks/index.d.ts +66 -0
  142. package/vendor/modules/tracks.d.mts +1 -0
  143. package/vendor/modules/tracks.mjs +2 -0
  144. package/vendor/modules/tree-growth/bend.d.ts +28 -0
  145. package/vendor/modules/tree-growth/frame.d.ts +60 -0
  146. package/vendor/modules/tree-growth/index.d.ts +23 -0
  147. package/vendor/modules/tree-growth/light.d.ts +13 -0
  148. package/vendor/modules/tree-growth/math.d.ts +19 -0
  149. package/vendor/modules/tree-growth/model.d.ts +100 -0
  150. package/vendor/modules/tree-growth/sectors.d.ts +10 -0
  151. package/vendor/modules/tree-growth/simulation.d.ts +58 -0
  152. package/vendor/modules/tree-growth/species.d.ts +117 -0
  153. package/vendor/modules/tree-growth/species.data.d.ts +2 -0
  154. package/vendor/modules/tree-growth/year.d.ts +38 -0
  155. package/vendor/modules/tree-growth.d.mts +1 -0
  156. package/vendor/modules/tree-growth.inputs.json +75 -0
  157. package/vendor/modules/tree-growth.mjs +2 -0
  158. package/vendor/modules/tree-render/features.d.ts +9 -0
  159. package/vendor/modules/tree-render/forest/bake.d.ts +41 -0
  160. package/vendor/modules/tree-render/forest/cards.d.ts +28 -0
  161. package/vendor/modules/tree-render/forest/forest.d.ts +156 -0
  162. package/vendor/modules/tree-render/index.d.ts +52 -0
  163. package/vendor/modules/tree-render/inputs.d.ts +260 -0
  164. package/vendor/modules/tree-render/instances.d.ts +66 -0
  165. package/vendor/modules/tree-render/math.d.ts +8 -0
  166. package/vendor/modules/tree-render/tree/bark.d.ts +20 -0
  167. package/vendor/modules/tree-render/tree/leaves.d.ts +29 -0
  168. package/vendor/modules/tree-render/tree/litter.d.ts +21 -0
  169. package/vendor/modules/tree-render/tree/ornaments.d.ts +36 -0
  170. package/vendor/modules/tree-render/tree/roots.d.ts +75 -0
  171. package/vendor/modules/tree-render/tree/shapes.d.ts +49 -0
  172. package/vendor/modules/tree-render/tree/tree.d.ts +239 -0
  173. package/vendor/modules/tree-render/tree/wood.d.ts +27 -0
  174. package/vendor/modules/tree-render/wind.d.ts +51 -0
  175. package/vendor/modules/tree-render.d.mts +1 -0
  176. package/vendor/modules/tree-render.inputs.json +45 -0
  177. package/vendor/modules/tree-render.mjs +2 -0
  178. package/vendor/modules/voxel-dither/dither.glsl.d.ts +9 -0
  179. package/vendor/modules/voxel-dither/index.d.ts +35 -0
  180. package/vendor/modules/voxel-dither/light.d.ts +33 -0
  181. package/vendor/modules/voxel-dither/lit.d.ts +86 -0
  182. package/vendor/modules/voxel-dither/pass.d.ts +46 -0
  183. package/vendor/modules/voxel-dither/screens.d.ts +21 -0
  184. package/vendor/modules/voxel-dither/settings.d.ts +307 -0
  185. package/vendor/modules/voxel-dither/signals.d.ts +35 -0
  186. package/vendor/modules/voxel-dither.d.mts +1 -0
  187. package/vendor/modules/voxel-dither.inputs.json +295 -0
  188. package/vendor/modules/voxel-dither.mjs +452 -0
  189. package/vendor/modules/world-light/index.d.ts +133 -0
  190. package/vendor/modules/world-light.d.mts +1 -0
  191. package/vendor/modules/world-light.inputs.json +68 -0
  192. package/vendor/modules/world-light.mjs +2 -0
package/src/lit.d.ts ADDED
@@ -0,0 +1,384 @@
1
+ // keel/lit's surface, loosely typed: the parts a scene touches. The engine's own types aren't shipped yet.
2
+
3
+ /** An RGB colour with Three-style setters. */
4
+ export interface LitColor {
5
+ r: number
6
+ g: number
7
+ b: number
8
+ set(value: string | number | LitColor): LitColor
9
+ clone(): LitColor
10
+ lerp(to: LitColor, amount: number): LitColor
11
+ add(other: LitColor): LitColor
12
+ multiplyScalar(amount: number): LitColor
13
+ toArray(): [number, number, number]
14
+ }
15
+
16
+ /** A point or direction. */
17
+ export interface LitVector3 {
18
+ x: number
19
+ y: number
20
+ z: number
21
+ set(x: number, y: number, z: number): LitVector3
22
+ copy(from: LitVector3): LitVector3
23
+ clone(): LitVector3
24
+ lerp(to: LitVector3, amount: number): LitVector3
25
+ /** Into the camera's normalised device coordinates: x and y in -1..1 across the view. */
26
+ project(camera: LitCamera): LitVector3
27
+ /** Moves the point by a matrix: `new lit.Vector3(0, 0.13, 0.07).applyMatrix4(group.matrixWorld)`. */
28
+ applyMatrix4(matrix: LitMatrix4): LitVector3
29
+ toArray(): [number, number, number]
30
+ }
31
+
32
+ /** Anything in the scene graph. */
33
+ export interface LitNode {
34
+ position: LitVector3
35
+ rotation: { x: number; y: number; z: number; set(x: number, y: number, z: number): void }
36
+ scale: LitVector3
37
+ visible: boolean
38
+ name: string
39
+ readonly children: LitNode[]
40
+ userData: Record<string, unknown>
41
+ add(...children: unknown[]): LitNode
42
+ remove(...children: unknown[]): LitNode
43
+ lookAt(x: number | LitVector3, y?: number, z?: number): void
44
+ rotateZ(angle: number): LitNode
45
+ /** False draws it even when its bounds fall outside the camera's view: a full-frame backdrop, say. */
46
+ frustumCulled: boolean
47
+ /** Draw order among the scene's objects, lowest first; 0 by default. */
48
+ renderOrder: number
49
+ /** Where it is in the scene, after its parents': current once `updateMatrixWorld()` has run. */
50
+ readonly matrixWorld: LitMatrix4
51
+ updateMatrixWorld(force?: boolean): void
52
+ /** Bakes a matrix into its position, rotation and scale. */
53
+ applyMatrix4(matrix: LitMatrix4): LitNode
54
+ /** Calls `fn` on this node and every node under it. */
55
+ traverse(fn: (node: LitNode) => void): void
56
+ }
57
+
58
+ /** The root of a scene: `fog` and `background` live here. */
59
+ export interface LitStage extends LitNode {
60
+ fog: { color: LitColor; near: number; far: number } | null
61
+ background: LitColor | null
62
+ /** Light from the room around the scene: an `Environment`. A metal reflects it; a rough surface takes its diffuse. */
63
+ environment: LitEnvironment | null
64
+ environmentIntensity: number
65
+ /** Turns the room: `environmentRotation.y = Math.PI / 6`. */
66
+ environmentRotation: {
67
+ x: number
68
+ y: number
69
+ z: number
70
+ set(x: number, y: number, z: number): void
71
+ }
72
+ }
73
+
74
+ /** One soft box of a studio: where it hangs, how big, its linear colour and strength. */
75
+ export interface SoftBox {
76
+ /** A direction from the subject. */
77
+ readonly dir: readonly [number, number, number]
78
+ /** Half its width and height, as tangents of the angle it spans. */
79
+ readonly size: readonly [number, number]
80
+ readonly colour: readonly [number, number, number]
81
+ readonly intensity: number
82
+ /** Edge softness, 0 crisp to 0.5 very soft. */
83
+ readonly soft?: number
84
+ /** Turn about its own axis, radians. */
85
+ readonly roll?: number
86
+ }
87
+
88
+ /** A studio as data: a sky, horizon and ground gradient (linear) and its soft boxes. */
89
+ export interface Studio {
90
+ readonly sky: readonly [number, number, number]
91
+ readonly horizon: readonly [number, number, number]
92
+ readonly ground: readonly [number, number, number]
93
+ readonly boxes: readonly SoftBox[]
94
+ }
95
+
96
+ /** Options for `Environment`. */
97
+ export interface EnvironmentOptions {
98
+ /** A face's size at the sharpest level: a power of two, 16 or more. Default 128. */
99
+ size?: number
100
+ /** GGX samples per texel at the first blurred level. Default 384. */
101
+ samples?: number
102
+ }
103
+
104
+ /** A studio prefiltered for lit shading on the GPU the first frame that reads it. No file. */
105
+ export interface LitEnvironment extends LitTexture {
106
+ studio: Studio
107
+ readonly size: number
108
+ }
109
+
110
+ /** A perspective camera. Call `updateProjectionMatrix()` after changing `fov` or `aspect`. */
111
+ export interface LitCamera extends LitNode {
112
+ fov: number
113
+ aspect: number
114
+ near: number
115
+ far: number
116
+ updateProjectionMatrix(): void
117
+ }
118
+
119
+ /** A light. `SunLight` adds `target`, `castShadow` and `shadow`; `SkyLight` adds `groundColor`. */
120
+ export interface LitLight extends LitNode {
121
+ color: LitColor
122
+ groundColor: LitColor
123
+ intensity: number
124
+ castShadow: boolean
125
+ target: LitNode
126
+ shadow: {
127
+ bias: number
128
+ mapSize: { set(width: number, height: number): void }
129
+ camera: {
130
+ left: number
131
+ right: number
132
+ top: number
133
+ bottom: number
134
+ near: number
135
+ far: number
136
+ updateProjectionMatrix(): void
137
+ }
138
+ }
139
+ }
140
+
141
+ /** Vertices and their attributes. `dispose()` frees its GPU buffers. */
142
+ export interface LitGeometry {
143
+ dispose(): void
144
+ }
145
+
146
+ /** What every material shares. `dispose()` frees its programs. */
147
+ export interface LitMaterialNode {
148
+ transparent: boolean
149
+ opacity: number
150
+ depthWrite: boolean
151
+ needsUpdate: boolean
152
+ dispose(): void
153
+ }
154
+
155
+ /** The physically based material's own fields. */
156
+ export interface LitSurfaceMaterial extends LitMaterialNode {
157
+ color: LitColor
158
+ roughness: number
159
+ metalness: number
160
+ /** Its share of the stage's environment light. Default 1. */
161
+ envMapIntensity: number
162
+ }
163
+
164
+ /** A uniform the shader reads: set `value` and the next frame uploads it. */
165
+ export interface LitUniform<T = unknown> {
166
+ value: T
167
+ }
168
+
169
+ /** Options for `CustomMaterial`. */
170
+ export interface CustomMaterialOptions {
171
+ uniforms?: Record<string, LitUniform>
172
+ vertexShader: string
173
+ fragmentShader: string
174
+ transparent?: boolean
175
+ depthWrite?: boolean
176
+ depthTest?: boolean
177
+ }
178
+
179
+ /** A material from GLSL. */
180
+ export interface LitCustomMaterial extends LitMaterialNode {
181
+ uniforms: Record<string, LitUniform>
182
+ vertexShader: string
183
+ fragmentShader: string
184
+ }
185
+
186
+ /** A mesh: geometry and material, and whether it throws and takes shadows. */
187
+ export interface LitMesh extends LitNode {
188
+ geometry: LitGeometry
189
+ material: LitMaterialNode | LitMaterialNode[]
190
+ castShadow: boolean
191
+ receiveShadow: boolean
192
+ }
193
+
194
+ /** A 4×4 transform. */
195
+ export interface LitMatrix4 {
196
+ identity(): LitMatrix4
197
+ makeTranslation(x: number, y: number, z: number): LitMatrix4
198
+ compose(position: LitVector3, rotation: LitQuaternion, scale: LitVector3): LitMatrix4
199
+ }
200
+
201
+ /** A rotation. */
202
+ export interface LitQuaternion {
203
+ setFromAxisAngle(axis: LitVector3, angle: number): LitQuaternion
204
+ }
205
+
206
+ /** A mesh drawn many times. Set `instanceMatrix.needsUpdate` after `setMatrixAt`. */
207
+ export interface LitInstances extends LitMesh {
208
+ count: number
209
+ instanceMatrix: { needsUpdate: boolean }
210
+ setMatrixAt(index: number, matrix: LitMatrix4): void
211
+ setColorAt(index: number, color: LitColor): void
212
+ }
213
+
214
+ /** An image the GPU samples. */
215
+ export interface LitTexture {
216
+ needsUpdate: boolean
217
+ colorSpace: string
218
+ wrapS: string
219
+ wrapT: string
220
+ magFilter: string
221
+ minFilter: string
222
+ dispose(): void
223
+ }
224
+
225
+ /** Options for `RenderTarget`. */
226
+ export interface LitRenderTargetOptions {
227
+ /** Multisampling: 0 is off. */
228
+ samples?: number
229
+ depthBuffer?: boolean
230
+ /** The texel type, such as `half` for half floats. */
231
+ type?: string
232
+ colorSpace?: string
233
+ }
234
+
235
+ /** An offscreen image to draw into. */
236
+ export interface LitRenderTarget {
237
+ readonly width: number
238
+ readonly height: number
239
+ readonly texture: LitTexture
240
+ setSize(width: number, height: number): void
241
+ dispose(): void
242
+ }
243
+
244
+ /** A camera without perspective. */
245
+ export interface LitOrthoCamera extends LitNode {
246
+ left: number
247
+ right: number
248
+ top: number
249
+ bottom: number
250
+ near: number
251
+ far: number
252
+ updateProjectionMatrix(): void
253
+ }
254
+
255
+ /** The renderer bound to a canvas. */
256
+ export interface LitRenderer {
257
+ domElement: HTMLCanvasElement
258
+ outputColorSpace: string
259
+ toneMapping: string
260
+ toneMappingExposure: number
261
+ autoClear: boolean
262
+ shadowMap: { enabled: boolean; type: string }
263
+ info: { render: { calls: number; triangles: number } }
264
+ getPixelRatio(): number
265
+ setPixelRatio(value: number): void
266
+ setSize(width: number, height: number, updateStyle?: boolean): void
267
+ getContext(): WebGL2RenderingContext
268
+ /** Where the next `render` draws: a target, or null for the canvas. */
269
+ setRenderTarget(target: LitRenderTarget | null): void
270
+ getRenderTarget(): LitRenderTarget | null
271
+ /** Limits drawing to a rectangle, CSS px from the bottom left, while `setScissorTest(true)`. */
272
+ setScissor(x: number, y: number, width: number, height: number): void
273
+ setScissorTest(on: boolean): void
274
+ setClearColor(color: string | number | LitColor, alpha?: number): void
275
+ clear(color?: boolean, depth?: boolean, stencil?: boolean): void
276
+ render(scene: unknown, camera: unknown): void
277
+ dispose(): void
278
+ forceContextLoss(): void
279
+ }
280
+
281
+ /** Options for `createLitRenderer`. */
282
+ export interface LitRendererOptions {
283
+ canvas?: HTMLCanvasElement
284
+ antialias?: boolean
285
+ alpha?: boolean
286
+ powerPreference?: 'default' | 'high-performance' | 'low-power'
287
+ preserveDrawingBuffer?: boolean
288
+ }
289
+
290
+ /** Makes a renderer. Throws where WebGL2 is missing. */
291
+ export declare function createLitRenderer(options?: LitRendererOptions): LitRenderer
292
+ /** The root of a scene. */
293
+ export declare const Stage: new () => LitStage
294
+ /** A node that only holds others. */
295
+ export declare const Group: new () => LitNode
296
+ /** A camera with perspective: field of view in degrees, aspect, near and far. */
297
+ export declare const PerspectiveCamera: new (
298
+ fov: number,
299
+ aspect: number,
300
+ near: number,
301
+ far: number,
302
+ ) => LitCamera
303
+ /** A directional light that can throw shadows: the sun or the moon. */
304
+ export declare const SunLight: new (color: string, intensity: number) => LitLight
305
+ /** Light from the whole sky above and the ground below. */
306
+ export declare const SkyLight: new (sky: string, ground: string, intensity: number) => LitLight
307
+ /** A colour from a CSS hex string or a number. */
308
+ export declare const Color: new (value?: string | number) => LitColor
309
+ /** A point or direction. */
310
+ export declare const Vector3: new (x?: number, y?: number, z?: number) => LitVector3
311
+ /** Linear fog between two distances. */
312
+ export declare const Fog: new (
313
+ color: string,
314
+ near: number,
315
+ far: number,
316
+ ) => NonNullable<LitStage['fog']>
317
+ /** The house studio: a big key upper left, two strips for the edges, a soft top, a low fill. */
318
+ export declare const STUDIO: Studio
319
+ /** A generated environment for `stage.environment`: `new Environment(STUDIO, { size: 128 })`. */
320
+ export declare const Environment: new (
321
+ studio?: Studio,
322
+ options?: EnvironmentOptions,
323
+ ) => LitEnvironment
324
+ /** A geometry drawn with a material. */
325
+ export declare const Mesh: new (geometry: unknown, material: unknown) => LitMesh
326
+ /** The physically based material: `color`, `roughness`, `metalness`, `envMapIntensity`, maps. */
327
+ export declare const LitMaterial: new (options?: Record<string, unknown>) => LitSurfaceMaterial
328
+ /** Unlit colour or texture. */
329
+ export declare const FlatMaterial: new (options?: Record<string, unknown>) => LitMaterialNode
330
+ /** A material from GLSL: `uniforms`, `vertexShader`, `fragmentShader`, `transparent`, `depthWrite`. */
331
+ export declare const CustomMaterial: new (options: CustomMaterialOptions) => LitCustomMaterial
332
+ /** One geometry drawn `count` times, each with its own matrix and colour. */
333
+ export declare const Instances: new (
334
+ geometry: unknown,
335
+ material: unknown,
336
+ count: number,
337
+ ) => LitInstances
338
+ /** An offscreen image to draw into: `renderer.setRenderTarget(target)`, then sample `target.texture`. */
339
+ export declare const RenderTarget: new (
340
+ width: number,
341
+ height: number,
342
+ options?: LitRenderTargetOptions,
343
+ ) => LitRenderTarget
344
+ /** A camera without perspective: left, right, top, bottom, near and far. */
345
+ export declare const OrthoCamera: new (
346
+ left?: number,
347
+ right?: number,
348
+ top?: number,
349
+ bottom?: number,
350
+ near?: number,
351
+ far?: number,
352
+ ) => LitOrthoCamera
353
+ /** A texture from raw texels, such as a seeded noise field. */
354
+ export declare const DataTexture: new (
355
+ data: ArrayBufferView,
356
+ width: number,
357
+ height: number,
358
+ ) => LitTexture
359
+ /** A texture drawn with a 2D canvas. Set `needsUpdate` after drawing again. */
360
+ export declare const CanvasTexture: new (canvas: HTMLCanvasElement | OffscreenCanvas) => LitTexture
361
+ /** A 4×4 transform. */
362
+ export declare const Matrix4: new () => LitMatrix4
363
+ /** A rotation. */
364
+ export declare const Quaternion: new () => LitQuaternion
365
+ /** A box: width, height and depth. */
366
+ export declare function box(width?: number, height?: number, depth?: number): LitGeometry
367
+ /** A sphere: radius, then segments round and down. */
368
+ export declare function sphere(radius?: number, across?: number, down?: number): LitGeometry
369
+ /** A torus: radius, tube radius, then segments round the tube and along it. */
370
+ export declare function torus(
371
+ radius?: number,
372
+ tube?: number,
373
+ radial?: number,
374
+ tubular?: number,
375
+ ): LitGeometry
376
+ /** An icosahedron: radius and subdivisions. */
377
+ export declare function icosahedron(radius?: number, detail?: number): LitGeometry
378
+ /** A flat rectangle in the XY plane. */
379
+ export declare function plane(
380
+ width: number,
381
+ height: number,
382
+ across?: number,
383
+ down?: number,
384
+ ): LitGeometry
@@ -0,0 +1,60 @@
1
+ // The run-time path for a surface that can't be painted at build: a themed colour, a visitor's seed. Opt in with
2
+ // `paintLive(recipe)`: `generate` paints it in a worker (on a WebGL2 `OffscreenCanvas`, or on the CPU without one),
3
+ // and this thread encodes the albedo to a blob and gives a media record whose `src` is its blob URL. Same recipe,
4
+ // same picture as `?paint`.
5
+ import type { MediaAsset } from '@kerf-js/core/media'
6
+ import type { Surface } from '../vendor/modules/surfaces.mjs'
7
+ import { forget, generate, keyOf } from './generate'
8
+ import type { PaintRecipe } from './paint-recipe.mjs'
9
+
10
+ export type { PaintRecipe }
11
+
12
+ /** A record painted in this page: `<Media src={asset}>`; `revoke()` frees its blob once nothing shows it. */
13
+ export type LiveAsset = MediaAsset & { readonly digest: string; revoke(): void }
14
+
15
+ const painted = new Map<string, Promise<LiveAsset>>()
16
+
17
+ async function encode(surface: Surface, request: PaintRecipe, key: string): Promise<LiveAsset> {
18
+ const { width, height, digest } = surface
19
+ const canvas = new OffscreenCanvas(width, height)
20
+ const pixels = new Uint8ClampedArray(surface.buffers[0])
21
+ canvas.getContext('2d')!.putImageData(new ImageData(pixels, width, height), 0, 0)
22
+ const blob = await canvas.convertToBlob({ type: 'image/webp', quality: 0.9 })
23
+ const src = URL.createObjectURL(blob)
24
+ return {
25
+ kind: 'graphic',
26
+ src,
27
+ width,
28
+ height,
29
+ type: blob.type,
30
+ sources: [],
31
+ name: `${request.surface ?? request.recipe?.kind ?? 'surface'}-${request.seed ?? 0}`,
32
+ quality: 'original',
33
+ digest,
34
+ revoke() {
35
+ URL.revokeObjectURL(src)
36
+ painted.delete(key)
37
+ forget('surface', request)
38
+ },
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Paints a surface off this thread, once per request while it is kept:
44
+ * `<Media src={await paintLive(recipe)} alt="" decorative />`. Needs `OffscreenCanvas` to encode.
45
+ */
46
+ export function paintLive(request: PaintRecipe): Promise<LiveAsset> {
47
+ const key = keyOf('surface', request)
48
+ let asset = painted.get(key)
49
+ if (!asset) {
50
+ painted.set(key, (asset = generate('surface', request).then((s) => encode(s, request, key))))
51
+ }
52
+ return asset
53
+ }
54
+
55
+ /** A record as a CSS image: an `image-set` of each format's widest file, for a tile under `background-image`. */
56
+ export function cssImage(asset: MediaAsset): string {
57
+ const widest = (srcset: string) => srcset.split(',').at(-1)!.trim().split(/\s+/)[0]!
58
+ const sets = asset.sources.map((s) => `url("${widest(s.srcset)}") type("${s.type}")`)
59
+ return sets.length ? `image-set(${sets.join(', ')})` : `url("${asset.src}")`
60
+ }
@@ -0,0 +1,29 @@
1
+ import type { SurfaceRecipe, SurfaceSettings } from '../vendor/modules/surfaces.mjs'
2
+ import type * as Surfaces from '../vendor/modules/surfaces.mjs'
3
+
4
+ /** A painted surface by request: a preset or a whole recipe, a seed and a size in pixels. */
5
+ export interface PaintRecipe {
6
+ /** One of the surfaces module's recipes: `paper` and `knit` tile, `painted-*` are card grounds. */
7
+ readonly surface?: keyof typeof PRESETS
8
+ /** Laid over the preset: `{ "ground": "#2f4f5a" }`. */
9
+ readonly with?: Readonly<Record<string, unknown>>
10
+ /** A whole recipe instead of a preset. */
11
+ readonly recipe?: SurfaceRecipe
12
+ /** A whole number from 0. Default 0. */
13
+ readonly seed?: number
14
+ /** Pixels, 1..4096. Default 1024; the height defaults to the width. */
15
+ readonly width?: number
16
+ readonly height?: number
17
+ }
18
+
19
+ export declare const PRESETS: {
20
+ readonly paper: 'PAPER'
21
+ readonly knit: 'KNIT'
22
+ readonly 'painted-floral': 'PAINTED_FLORAL'
23
+ readonly 'painted-cloth': 'PAINTED_CLOTH'
24
+ }
25
+
26
+ export declare function recipeOf(
27
+ request: PaintRecipe,
28
+ surfaces: typeof Surfaces,
29
+ ): { recipe: SurfaceRecipe; settings: SurfaceSettings }
@@ -0,0 +1,29 @@
1
+ // A paint recipe as a page writes it, turned into the surfaces module's recipe and settings. Shared by the build step
2
+ // (`./paint`) and the run-time path (`./paint-live`), so both paint the same thing from the same request.
3
+
4
+ /** The surfaces module's ready recipes, by the name a request gives. */
5
+ export const PRESETS = {
6
+ paper: 'PAPER',
7
+ knit: 'KNIT',
8
+ 'painted-floral': 'PAINTED_FLORAL',
9
+ 'painted-cloth': 'PAINTED_CLOTH',
10
+ }
11
+
12
+ /**
13
+ * The recipe and settings a request paints: a preset by name (with `with` laid over it) or a whole `recipe`, at
14
+ * `seed`, `width` and `height`.
15
+ */
16
+ export function recipeOf(request, surfaces) {
17
+ if (!request || typeof request !== 'object') throw new Error('keel paint: a recipe is an object')
18
+ const named = request.surface && PRESETS[request.surface]
19
+ if (request.surface && !named)
20
+ throw new Error(
21
+ `keel paint: no surface \`${request.surface}\`; one of ${Object.keys(PRESETS).join(', ')}`,
22
+ )
23
+ const base = named ? surfaces[named] : request.recipe
24
+ if (!base) throw new Error('keel paint: give `surface` (a preset) or `recipe`')
25
+ const recipe = request.with ? { ...base, ...request.with } : base
26
+ const settings = { seed: request.seed ?? 0, width: request.width ?? 1024 }
27
+ if (request.height !== undefined) settings.height = request.height
28
+ return { recipe, settings }
29
+ }
@@ -0,0 +1,20 @@
1
+ // A thread of `keelPaint`'s pool: plans and paints one recipe on the CPU and answers with its PNG's bytes.
2
+ import { parentPort } from 'node:worker_threads'
3
+ import { recipeOf } from './paint-recipe.mjs'
4
+ import { png } from './paint.mjs'
5
+
6
+ const surfaces = await import('../vendor/modules/surfaces.mjs')
7
+
8
+ parentPort?.on('message', (request) => {
9
+ try {
10
+ const { recipe, settings } = recipeOf(request, surfaces)
11
+ const surface = surfaces.paintPlan(surfaces.planSurface(recipe, settings))
12
+ // A copy that owns its whole buffer: a Node Buffer may be a slice of a shared pool, which cannot be moved.
13
+ const bytes = new Uint8Array(
14
+ png(new Uint8Array(surface.buffers[0]), surface.width, surface.height),
15
+ )
16
+ parentPort?.postMessage({ bytes, width: surface.width, height: surface.height }, [bytes.buffer])
17
+ } catch (error) {
18
+ parentPort?.postMessage({ error: error?.message ?? String(error) })
19
+ }
20
+ })
@@ -0,0 +1,37 @@
1
+ import type { Plugin } from 'vite'
2
+
3
+ import type { PaintRecipe } from './paint-recipe.mjs'
4
+
5
+ export type { PaintRecipe }
6
+
7
+ /** Options for `keelPaint`. */
8
+ export interface KeelPaintOptions {
9
+ /** Where painted PNGs are kept, keyed by the plan's digest. Default `<root>/.kerf/cache/paint`. */
10
+ readonly cacheDir?: string
11
+ /** How many recipes paint at once, each on a thread of its own. Default one fewer than the cores, up to 4; 0 paints
12
+ * on Vite's thread. */
13
+ readonly threads?: number
14
+ }
15
+
16
+ /** Threads that paint recipes side by side. */
17
+ export interface PaintPool {
18
+ paint(request: PaintRecipe): Promise<{ bytes: Uint8Array; width: number; height: number }>
19
+ close(): Promise<void>
20
+ }
21
+
22
+ /** A pool of up to `size` painting threads, started as they are needed. */
23
+ export declare function createPaintPool(size?: number): PaintPool
24
+
25
+ /** The Vite plugin for `import ground from './card.paint.json?paint'`: a media record of a surface painted at build. */
26
+ export declare function keelPaint(options?: KeelPaintOptions): Plugin
27
+
28
+ /** The painted PNG for a recipe file, from the cache or painted now. */
29
+ export declare function paintFile(
30
+ file: string,
31
+ cacheDir: string,
32
+ log?: (line: string) => void,
33
+ pool?: PaintPool | null,
34
+ ): Promise<{ file: string; painted: boolean }>
35
+
36
+ /** RGBA rows as an unfiltered RGB PNG (the alpha left out). */
37
+ export declare function png(rgba: Uint8Array, width: number, height: number): Buffer