dewdrop-sdk 0.11.0 → 0.13.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 +2 -2
- package/package.json +1 -5
- package/src/three.d.ts +36 -5
- package/src/vite.js +6 -15
- package/src/wgsl.d.ts +34 -3
- package/src/wgsl.js +19 -25
- package/src/wgsl.wasm +0 -0
- package/src/vertex.d.ts +0 -97
- package/src/vertex.js +0 -5
package/README.md
CHANGED
|
@@ -2,8 +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 the build,
|
|
6
|
-
|
|
5
|
+
supplies those where the game runs; this package carries the build, which
|
|
6
|
+
also compiles the WGSL a game moves its vertices with.
|
|
7
7
|
|
|
8
8
|
How to set up a project and everything a game can do is in the docs:
|
|
9
9
|
https://alpha.dewdrop.world/docs
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dewdrop-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "The Vite plugin that builds a three.js game for Dewdrop.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -15,10 +15,6 @@
|
|
|
15
15
|
},
|
|
16
16
|
"./three": {
|
|
17
17
|
"types": "./src/three.d.ts"
|
|
18
|
-
},
|
|
19
|
-
"./vertex": {
|
|
20
|
-
"types": "./src/vertex.d.ts",
|
|
21
|
-
"default": "./src/vertex.js"
|
|
22
18
|
}
|
|
23
19
|
},
|
|
24
20
|
"files": [
|
package/src/three.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// What Dewdrop reads from three.js beyond three.js's own API, for a project that
|
|
2
|
-
// @types/three. Add `/// <reference types="dewdrop-sdk/three" />` to a file, or
|
|
3
|
-
// "types" of its tsconfig.json or jsconfig.json.
|
|
4
|
-
|
|
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';
|
|
5
6
|
|
|
6
7
|
/** One point of `ExposureSettings.compensationCurve`: a scene's brightness in EV100, and how many stops
|
|
7
8
|
* brighter (positive) or darker (negative) than middle grey an adapting exposure shows it. */
|
|
@@ -60,8 +61,38 @@ declare module 'three' {
|
|
|
60
61
|
* setting filled in. */
|
|
61
62
|
get exposure(): ResolvedExposureSettings | null;
|
|
62
63
|
set exposure(settings: ExposureSettings | null);
|
|
63
|
-
/** The time in seconds a `
|
|
64
|
+
/** The time in seconds a `DewMaterial`'s program reads as `time()`; it stays as set, so set it from the
|
|
64
65
|
* game's clock every frame. */
|
|
65
66
|
programTime: number;
|
|
66
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
|
+
}
|
|
67
98
|
}
|
package/src/vite.js
CHANGED
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
// says where. The plugin imports nothing from Vite itself, so it works
|
|
11
11
|
// linked from a checkout as well as installed, on Vite 3 through 8.
|
|
12
12
|
import { existsSync } from 'node:fs';
|
|
13
|
-
import {
|
|
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 {
|
|
16
|
+
import { compileVertexProgram, programModule } from './wgsl.js';
|
|
17
17
|
|
|
18
18
|
// A WGSL file imported as it is, with no query asking Vite for its text or
|
|
19
19
|
// URL instead.
|
|
@@ -56,13 +56,8 @@ export function backendEntry(root) {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export default function dew() {
|
|
59
|
-
// The project's root, which a `.wgsl` file's path is given from.
|
|
60
|
-
let projectRoot = process.cwd();
|
|
61
59
|
return {
|
|
62
60
|
name: 'dew',
|
|
63
|
-
configResolved(config) {
|
|
64
|
-
projectRoot = config.root;
|
|
65
|
-
},
|
|
66
61
|
config(userConfig, env) {
|
|
67
62
|
// Said in the terminal, where the developer typed `vite`, rather than
|
|
68
63
|
// left to the page's console.
|
|
@@ -110,21 +105,17 @@ export default function dew() {
|
|
|
110
105
|
};
|
|
111
106
|
return config;
|
|
112
107
|
},
|
|
113
|
-
// A `.wgsl` import is its vertex program; one
|
|
114
|
-
//
|
|
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.
|
|
115
110
|
transform(code, id) {
|
|
116
111
|
if (!WGSL.test(id)) return null;
|
|
117
|
-
const result =
|
|
112
|
+
const result = compileVertexProgram(code);
|
|
118
113
|
if (!result.ok) {
|
|
119
114
|
const error = { message: result.message, id };
|
|
120
115
|
if (result.line > 0) error.loc = { file: id, line: result.line, column: result.column - 1 };
|
|
121
116
|
this.error(error);
|
|
122
117
|
}
|
|
123
|
-
|
|
124
|
-
const path = relative(projectRoot, id);
|
|
125
|
-
const outside = isAbsolute(path) || path === '..' || path.startsWith(`..${sep}`);
|
|
126
|
-
const file = outside ? basename(id) : path.split(sep).join('/');
|
|
127
|
-
return { code: programModule(code, file), map: { mappings: '' } };
|
|
118
|
+
return { code: programModule(result.code), map: { mappings: '' } };
|
|
128
119
|
},
|
|
129
120
|
// The built page, its URLs rewritten and its stylesheets linked as Vite
|
|
130
121
|
// made it for a browser, is opened at the top of the entry, and the
|
package/src/wgsl.d.ts
CHANGED
|
@@ -1,7 +1,38 @@
|
|
|
1
1
|
// The type of a `.wgsl` import in a game built with `dewdrop-sdk/vite`.
|
|
2
2
|
|
|
3
3
|
declare module '*.wgsl' {
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
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;
|
|
7
38
|
}
|
package/src/wgsl.js
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// and
|
|
4
|
-
// passes is taken into the game as its source, which Dewdrop builds.
|
|
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.
|
|
5
4
|
import { readFileSync } from 'node:fs';
|
|
6
5
|
|
|
7
6
|
let compiled = null;
|
|
8
7
|
let instance = null;
|
|
9
8
|
|
|
10
|
-
// The
|
|
11
|
-
function
|
|
9
|
+
// The compiler, made once and made again after a failure inside it.
|
|
10
|
+
function compiler() {
|
|
12
11
|
compiled ??= new WebAssembly.Module(readFileSync(new URL('./wgsl.wasm', import.meta.url)));
|
|
13
12
|
instance ??= new WebAssembly.Instance(compiled, {}).exports;
|
|
14
13
|
return instance;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
export function
|
|
16
|
+
export function compileVertexProgram(source) {
|
|
18
17
|
const bytes = new TextEncoder().encode(source);
|
|
19
|
-
const c =
|
|
18
|
+
const c = compiler();
|
|
20
19
|
let status;
|
|
21
20
|
let output;
|
|
22
21
|
try {
|
|
23
|
-
// Null for a file past the most it reads, which
|
|
24
|
-
//
|
|
22
|
+
// Null for a file past the most it reads, which it refuses unread;
|
|
23
|
+
// memory read after the call, which may grow it.
|
|
25
24
|
const at = c.dewdrop_input(bytes.length);
|
|
26
25
|
if (at !== 0) new Uint8Array(c.memory.buffer, at, bytes.length).set(bytes);
|
|
27
|
-
status = c.
|
|
26
|
+
status = c.dewdrop_compile();
|
|
28
27
|
output = new Uint8Array(c.memory.buffer, c.dewdrop_output(), c.dewdrop_output_len()).slice();
|
|
29
28
|
} catch (error) {
|
|
30
29
|
instance = null;
|
|
31
|
-
return { ok: false, line: 0, column: 0, message: `the
|
|
30
|
+
return { ok: false, line: 0, column: 0, message: `the compiler failed on this file (${error.message})` };
|
|
32
31
|
}
|
|
33
|
-
if (status === 0) return { ok: true };
|
|
34
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
|
+
}
|
|
35
37
|
return {
|
|
36
38
|
ok: false,
|
|
37
39
|
line: view.getUint32(0, true),
|
|
@@ -40,16 +42,8 @@ export function checkVertexProgram(source) {
|
|
|
40
42
|
};
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
// The module a `.wgsl` import becomes: its program,
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
export function programModule(source, file) {
|
|
48
|
-
return `const text = (hex) => new TextDecoder('utf-8', { ignoreBOM: true }).decode(Uint8Array.from(hex.match(/../g) ?? [], (h) => parseInt(h, 16)));
|
|
49
|
-
export default Object.freeze({ isVertexProgram: true, file: text("${hex(file)}"), source: text("${hex(source)}") });
|
|
50
|
-
`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function hex(text) {
|
|
54
|
-
return Buffer.from(text, 'utf8').toString('hex');
|
|
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`;
|
|
55
49
|
}
|
package/src/wgsl.wasm
CHANGED
|
Binary file
|
package/src/vertex.d.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// The types of `dewdrop-sdk/vertex`: a material whose vertices the game's
|
|
2
|
-
// own WGSL moves every frame, which Dewdrop's `three` provides.
|
|
3
|
-
/// <reference path="./wgsl.d.ts" />
|
|
4
|
-
import type { Color, ColorRepresentation, Material, MaterialParameters, Texture } from 'three';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A vertex program: what importing a `.wgsl` file gives in a game built
|
|
8
|
-
* with `dewdrop-sdk/vite`. The file defines
|
|
9
|
-
* `fn vertex(v: Vertex) -> Moved`, which runs for every vertex every frame
|
|
10
|
-
* and may use:
|
|
11
|
-
*
|
|
12
|
-
* - `struct Vertex { position: vec3f, normal: vec3f, uv: vec2f, color: vec4f, index: u32 }`,
|
|
13
|
-
* the vertex in object space, after skinning and morphs;
|
|
14
|
-
* - `struct Moved { position: vec3f, normal: vec3f }`, where it goes and its
|
|
15
|
-
* normal there, in object space;
|
|
16
|
-
* - `time() -> f32`, `renderer.programTime` in seconds;
|
|
17
|
-
* - `param(i: u32) -> f32`, the material's number `i` (0 past the last);
|
|
18
|
-
* - `camera() -> vec3f`, the camera's position in the world;
|
|
19
|
-
* - `world(p: vec3f) -> vec3f`, object-space `p` in the world, each
|
|
20
|
-
* instance's own for an `InstancedMesh` (`world(vec3f(0.0))` is where
|
|
21
|
-
* the instance stands).
|
|
22
|
-
*
|
|
23
|
-
* Programs use 32-bit types, declare no bindings or entry points, and leave
|
|
24
|
-
* out a few built-in functions (`asinh`, `acosh`, `atanh`, `modf`, `frexp`,
|
|
25
|
-
* `ldexp`, the `pack` and `unpack` families, `firstLeadingBit`,
|
|
26
|
-
* `firstTrailingBit`, `countLeadingZeros`, `countTrailingZeros`,
|
|
27
|
-
* `extractBits`, `insertBits`, `reverseBits`, `quantizeToF16`,
|
|
28
|
-
* `determinant`, `dot4I8Packed`, `dot4U8Packed`). A file is at most 64 KiB;
|
|
29
|
-
* its brackets nest at most 32 deep, a statement holds at most 512
|
|
30
|
-
* operators, and an `if` chains at most 32 `else if`. A program's variables
|
|
31
|
-
* hold at most 1,024 values in all, and its size and complexity are held
|
|
32
|
-
* to limits far past a sway, a wave or a swell. A vertex runs for a
|
|
33
|
-
* limited time each frame: a loop with a constant count that would run
|
|
34
|
-
* past it is refused, and one whose count the game sets (`param(i)`) is
|
|
35
|
-
* the program's own to keep to a few thousand passes, since a vertex that
|
|
36
|
-
* runs past it is dropped with its triangles. `vite build` says where a
|
|
37
|
-
* file goes outside any of these. A material drawn with a program for the
|
|
38
|
-
* first time may draw its mesh unmoved for a frame or so.
|
|
39
|
-
*/
|
|
40
|
-
export interface VertexProgram {
|
|
41
|
-
readonly isVertexProgram: true;
|
|
42
|
-
/** The file's path in the project, which an error building it names. */
|
|
43
|
-
readonly file: string;
|
|
44
|
-
/** The file's WGSL. */
|
|
45
|
-
readonly source: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface VertexMaterialParameters extends MaterialParameters {
|
|
49
|
-
/** The program, from `import sway from './sway.wgsl'`. */
|
|
50
|
-
program?: VertexProgram | null;
|
|
51
|
-
/** The program's numbers: `param(0)`, `param(1)` and on, at most 1,000. */
|
|
52
|
-
params?: ArrayLike<number> | null;
|
|
53
|
-
/** How far, in object units, the program may move any vertex; a vertex is held within it. 1 unless set. */
|
|
54
|
-
maxDisplacement?: number;
|
|
55
|
-
color?: ColorRepresentation;
|
|
56
|
-
/** Multiplies `color`: a PNG, JPEG or WebP texture, or a DataTexture of RGBA bytes; sRGB when its `colorSpace` is. */
|
|
57
|
-
map?: Texture | null;
|
|
58
|
-
roughness?: number;
|
|
59
|
-
metalness?: number;
|
|
60
|
-
emissive?: ColorRepresentation;
|
|
61
|
-
emissiveIntensity?: number;
|
|
62
|
-
/** Whether the geometry's `color` attribute multiplies `color`. */
|
|
63
|
-
vertexColors?: boolean;
|
|
64
|
-
/** Whether what it draws casts shadows; true unless set. */
|
|
65
|
-
castShadow?: boolean;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* A material whose vertices a WGSL program moves every frame, shaded as a
|
|
70
|
-
* `MeshStandardMaterial` is: `color` × `map` (× vertex colors), `roughness`,
|
|
71
|
-
* `metalness`, and `emissive` × `emissiveIntensity`. A change to any of
|
|
72
|
-
* those shows on the next frame without `needsUpdate`; a game sets
|
|
73
|
-
* `renderer.programTime` from its clock each frame for `time()`.
|
|
74
|
-
*/
|
|
75
|
-
export class VertexMaterial extends Material {
|
|
76
|
-
constructor(parameters?: VertexMaterialParameters);
|
|
77
|
-
readonly isVertexMaterial: true;
|
|
78
|
-
program: VertexProgram | null;
|
|
79
|
-
maxDisplacement: number;
|
|
80
|
-
color: Color;
|
|
81
|
-
map: Texture | null;
|
|
82
|
-
roughness: number;
|
|
83
|
-
metalness: number;
|
|
84
|
-
emissive: Color;
|
|
85
|
-
emissiveIntensity: number;
|
|
86
|
-
vertexColors: boolean;
|
|
87
|
-
castShadow: boolean;
|
|
88
|
-
/** The program's numbers: `param(0)`, `param(1)` and on, at most 1,000; a number edited in place shows the next frame. */
|
|
89
|
-
get params(): Float32Array;
|
|
90
|
-
set params(values: ArrayLike<number>);
|
|
91
|
-
/** The program's number `i`, 0 past the last. */
|
|
92
|
-
getParam(i: number): number;
|
|
93
|
-
/** Sets the program's number `i`, from 0 to 999, from the next frame on, as often as every frame. */
|
|
94
|
-
setParam(i: number, value: number): void;
|
|
95
|
-
copy(source: VertexMaterial): this;
|
|
96
|
-
clone(): this;
|
|
97
|
-
}
|
package/src/vertex.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
// `VertexMaterial`: a mesh material whose vertices a game's own WGSL
|
|
2
|
-
// function moves every frame, shaded as a standard material is. Dewdrop's
|
|
3
|
-
// `three` provides it, and builds the program a `.wgsl` import gives
|
|
4
|
-
// (`vite.js`).
|
|
5
|
-
export { VertexMaterial } from 'three';
|