dewdrop-sdk 0.10.0 → 0.12.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.
package/README.md CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  The Vite plugin that builds a three.js game for Dewdrop, and the name a game
4
4
  imports model calls, multiplayer, saves, sessions and voice from. Dewdrop
5
- supplies those where the game runs; this package carries only the build.
5
+ supplies those where the game runs; this package carries the build, which
6
+ also compiles the WGSL a game moves its vertices with.
6
7
 
7
8
  How to set up a project and everything a game can do is in the docs:
8
9
  https://alpha.dewdrop.world/docs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dewdrop-sdk",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "The Vite plugin that builds a three.js game for Dewdrop.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -12,6 +12,9 @@
12
12
  "./vite": {
13
13
  "types": "./src/vite.d.ts",
14
14
  "default": "./src/vite.js"
15
+ },
16
+ "./three": {
17
+ "types": "./src/three.d.ts"
15
18
  }
16
19
  },
17
20
  "files": [
package/src/three.d.ts ADDED
@@ -0,0 +1,98 @@
1
+ // What Dewdrop reads from three.js beyond three.js's own API, and the material it adds, for a project that
2
+ // types three.js with @types/three. Add `/// <reference types="dewdrop-sdk/three" />` to a file, or
3
+ // "dewdrop-sdk/three" to the "types" of its tsconfig.json or jsconfig.json.
4
+ /// <reference path="./wgsl.d.ts" />
5
+ import type { Color, ColorRepresentation, Material, MaterialParameters } from 'three';
6
+
7
+ /** One point of `ExposureSettings.compensationCurve`: a scene's brightness in EV100, and how many stops
8
+ * brighter (positive) or darker (negative) than middle grey an adapting exposure shows it. */
9
+ export type CompensationPoint = readonly [ev100: number, stops: number];
10
+
11
+ /** How Dewdrop exposes the lit scene. Settings left out keep their defaults. */
12
+ export interface ExposureSettings {
13
+ /** A fixed exposure in EV100, where each step up halves the brightness; `null` (the default) adapts to the
14
+ * scene. */
15
+ ev?: number | null;
16
+ /** How much brighter or darker than middle grey an adapting exposure keeps a scene, by how bright the scene
17
+ * is: 1 to 8 points, straight lines between them. Default `[[-2, -2.5], [7, 0]]`. */
18
+ compensationCurve?: readonly CompensationPoint[];
19
+ /** How fast the exposure brightens (default 4). */
20
+ up?: number;
21
+ /** How fast the exposure darkens (default 10). */
22
+ down?: number;
23
+ /** Contrast below middle grey, `1` the tone curve's own (default 0.85). */
24
+ shadowContrast?: number;
25
+ /** Contrast above middle grey, `1` the tone curve's own (default 0.85). */
26
+ highlightContrast?: number;
27
+ /** How far, in EV from 0 to 6, a dark area may be lifted (default 2). */
28
+ localShadowsEv?: number;
29
+ /** How far, in EV from 0 to 6, a bright area may be brought down (default 5). */
30
+ localHighlightsEv?: number;
31
+ /** Not a setting since dewdrop-sdk 0.10.1: use `localShadowsEv`. */
32
+ localShadows?: never;
33
+ /** Not a setting since dewdrop-sdk 0.10.1: use `localHighlightsEv`. */
34
+ localHighlights?: never;
35
+ /** Not a setting since dewdrop-sdk 0.10.1: an adapting exposure is shaped by `compensationCurve`. */
36
+ strength?: never;
37
+ /** Not a setting since dewdrop-sdk 0.10.1: an adapting exposure is shaped by `compensationCurve`. */
38
+ range?: never;
39
+ }
40
+
41
+ /** The exposure as reading `renderer.exposure` gives it: every setting filled in. */
42
+ export interface ResolvedExposureSettings {
43
+ ev: number | null;
44
+ compensationCurve: readonly CompensationPoint[];
45
+ up: number;
46
+ down: number;
47
+ shadowContrast: number;
48
+ highlightContrast: number;
49
+ localShadowsEv: number;
50
+ localHighlightsEv: number;
51
+ }
52
+
53
+ declare module 'three' {
54
+ interface WebGLRenderer {
55
+ /** The style of the whole game, `style: <style phrase>. <scene sentence>`; at most 4 KiB. */
56
+ prompt: string;
57
+ /** What the sky is, every pixel no mesh covers, in a few words; at most 1 KiB. Give `scene.background` a
58
+ * matching `Color`. */
59
+ backgroundDescription: string;
60
+ /** How the lit scene is exposed: `null` for Dewdrop's own, adapting exposure. Reading it back gives every
61
+ * setting filled in. */
62
+ get exposure(): ResolvedExposureSettings | null;
63
+ set exposure(settings: ExposureSettings | null);
64
+ /** The time in seconds a `DewMaterial`'s program reads as `time()`; it stays as set, so set it from the
65
+ * game's clock every frame. */
66
+ programTime: number;
67
+ }
68
+
69
+ interface DewMaterialParameters extends MaterialParameters {
70
+ /** The vertex program, from `import sway from './sway.wgsl'`. */
71
+ module?: Uint32Array | null;
72
+ /** The program's numbers: `param(0)`, `param(1)` and on. */
73
+ params?: Float32Array | null;
74
+ /** How far, in object units, the program may move any vertex; a vertex is held within it. 0 unless set. */
75
+ maxDisplacement?: number;
76
+ /** What the mesh is drawn in. */
77
+ color?: ColorRepresentation;
78
+ /** Whether what it draws casts shadows; true unless set. */
79
+ castShadow?: boolean;
80
+ }
81
+
82
+ /**
83
+ * A material whose vertices a WGSL program moves every frame, drawn in its `color` and lit as a
84
+ * `MeshLambertMaterial` is. A change to `module`, `maxDisplacement` or `color` shows once `needsUpdate` is
85
+ * set; a change to `params`, as often as every frame, once `paramsNeedUpdate` is.
86
+ */
87
+ class DewMaterial extends Material {
88
+ constructor(parameters?: DewMaterialParameters);
89
+ readonly isDewMaterial: true;
90
+ module: Uint32Array | null;
91
+ params: Float32Array | null;
92
+ maxDisplacement: number;
93
+ color: Color;
94
+ castShadow: boolean;
95
+ /** Set true after writing `params`, to send them. */
96
+ set paramsNeedUpdate(value: boolean);
97
+ }
98
+ }
package/src/vite.js CHANGED
@@ -13,6 +13,11 @@ import { existsSync } from 'node:fs';
13
13
  import { resolve } from 'node:path';
14
14
  import { ASSETS_DIR, BACKEND_FILE, BUILD_RECORD_FILE, ENTRY_FILE, PAGE_FILE, buildRecord, detectViteMajor, openPage, outputOptions } from './build.js';
15
15
  import { ON_DEWDROP } from './message.js';
16
+ import { compileVertexProgram, programModule } from './wgsl.js';
17
+
18
+ // A WGSL file imported as it is, with no query asking Vite for its text or
19
+ // URL instead.
20
+ const WGSL = /\.wgsl$/;
16
21
 
17
22
  const BACKEND_SOURCES = ['backend.js', 'backend.ts', 'src/backend.js', 'src/backend.ts'];
18
23
 
@@ -100,6 +105,18 @@ export default function dew() {
100
105
  };
101
106
  return config;
102
107
  },
108
+ // A `.wgsl` import is its vertex program; one that does not build stops
109
+ // the build at the line and column of the file the error is at.
110
+ transform(code, id) {
111
+ if (!WGSL.test(id)) return null;
112
+ const result = compileVertexProgram(code);
113
+ if (!result.ok) {
114
+ const error = { message: result.message, id };
115
+ if (result.line > 0) error.loc = { file: id, line: result.line, column: result.column - 1 };
116
+ this.error(error);
117
+ }
118
+ return { code: programModule(result.code), map: { mappings: '' } };
119
+ },
103
120
  // The built page, its URLs rewritten and its stylesheets linked as Vite
104
121
  // made it for a browser, is opened at the top of the entry, and the
105
122
  // build's record is written beside it. Last of the hooks, since Vite
package/src/wgsl.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ // The type of a `.wgsl` import in a game built with `dewdrop-sdk/vite`.
2
+
3
+ declare module '*.wgsl' {
4
+ /**
5
+ * The compiled vertex program the file holds, for the `module` of a
6
+ * `DewMaterial`. The file defines `fn vertex(v: Vertex) -> Moved`, which
7
+ * runs for every vertex every frame and may use:
8
+ *
9
+ * - `struct Vertex { position: vec3f, normal: vec3f, uv: vec2f, color: vec4f, index: u32 }`,
10
+ * the vertex in object space, after skinning and morphs;
11
+ * - `struct Moved { position: vec3f, normal: vec3f }`, where it goes and its
12
+ * normal there, in object space;
13
+ * - `time() -> f32`, `renderer.programTime` in seconds;
14
+ * - `param(i: u32) -> f32`, the material's number `i` (0 past the last);
15
+ * - `camera() -> vec3f`, the camera's position in the world;
16
+ * - `world(p: vec3f) -> vec3f`, object-space `p` in the world, each
17
+ * instance's own for an `InstancedMesh` (`world(vec3f(0.0))` is where
18
+ * the instance stands).
19
+ *
20
+ * Programs use 32-bit types, declare no bindings or entry points, and leave
21
+ * out a few built-in functions (`asinh`, `acosh`, `atanh`, `modf`, `frexp`,
22
+ * `ldexp`, the `pack` and `unpack` families, `firstLeadingBit`,
23
+ * `firstTrailingBit`, `countLeadingZeros`, `countTrailingZeros`,
24
+ * `extractBits`, `insertBits`, `reverseBits`, `quantizeToF16`,
25
+ * `determinant`, `dot4I8Packed`, `dot4U8Packed`). A file is at most 64 KiB;
26
+ * its brackets nest at most 32 deep, a statement holds at most 512
27
+ * operators, and an `if` chains at most 32 `else if`. A program's variables
28
+ * hold at most 1,024 values in all, and its size and complexity are held
29
+ * to limits far past a sway, a wave or a swell. A vertex runs for a
30
+ * limited time each frame: a loop with a constant count that would run
31
+ * past it is refused, and one whose count the game sets (`param(i)`) is
32
+ * the program's own to keep to a few thousand passes, since a vertex that
33
+ * runs past it is dropped with its triangles. `vite build` says where a
34
+ * file goes outside any of these.
35
+ */
36
+ const module: Uint32Array;
37
+ export default module;
38
+ }
package/src/wgsl.js ADDED
@@ -0,0 +1,49 @@
1
+ // Compiles a vertex program from its WGSL file with `wgsl.wasm`: the
2
+ // program's code, or an error at a 1-based line and column of the file (0
3
+ // and 0 for none) with its message.
4
+ import { readFileSync } from 'node:fs';
5
+
6
+ let compiled = null;
7
+ let instance = null;
8
+
9
+ // The compiler, made once and made again after a failure inside it.
10
+ function compiler() {
11
+ compiled ??= new WebAssembly.Module(readFileSync(new URL('./wgsl.wasm', import.meta.url)));
12
+ instance ??= new WebAssembly.Instance(compiled, {}).exports;
13
+ return instance;
14
+ }
15
+
16
+ export function compileVertexProgram(source) {
17
+ const bytes = new TextEncoder().encode(source);
18
+ const c = compiler();
19
+ let status;
20
+ let output;
21
+ try {
22
+ // Null for a file past the most it reads, which it refuses unread;
23
+ // memory read after the call, which may grow it.
24
+ const at = c.dewdrop_input(bytes.length);
25
+ if (at !== 0) new Uint8Array(c.memory.buffer, at, bytes.length).set(bytes);
26
+ status = c.dewdrop_compile();
27
+ output = new Uint8Array(c.memory.buffer, c.dewdrop_output(), c.dewdrop_output_len()).slice();
28
+ } catch (error) {
29
+ instance = null;
30
+ return { ok: false, line: 0, column: 0, message: `the compiler failed on this file (${error.message})` };
31
+ }
32
+ const view = new DataView(output.buffer);
33
+ if (status === 0) {
34
+ // Little-endian words, whatever the machine building the game is.
35
+ return { ok: true, code: Uint32Array.from({ length: output.length / 4 }, (_, i) => view.getUint32(i * 4, true)) };
36
+ }
37
+ return {
38
+ ok: false,
39
+ line: view.getUint32(0, true),
40
+ column: view.getUint32(4, true),
41
+ message: new TextDecoder().decode(output.subarray(8)),
42
+ };
43
+ }
44
+
45
+ // The module a `.wgsl` import becomes: its program's code, the `module` of
46
+ // a `DewMaterial`.
47
+ export function programModule(code) {
48
+ return `export default new Uint32Array([${code.join(',')}]);\n`;
49
+ }
package/src/wgsl.wasm ADDED
Binary file