webgpu-stage 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.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/lib/atmosphere.d.ts +100 -0
- package/lib/atmosphere.d.ts.map +1 -0
- package/lib/atmosphere.js +367 -0
- package/lib/atmosphere.js.map +1 -0
- package/lib/boot.d.ts +20 -0
- package/lib/boot.d.ts.map +1 -0
- package/lib/boot.js +85 -0
- package/lib/boot.js.map +1 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +8 -0
- package/lib/index.js.map +1 -0
- package/lib/look.d.ts +154 -0
- package/lib/look.d.ts.map +1 -0
- package/lib/look.js +420 -0
- package/lib/look.js.map +1 -0
- package/lib/pipeline.d.ts +36 -0
- package/lib/pipeline.d.ts.map +1 -0
- package/lib/pipeline.js +285 -0
- package/lib/pipeline.js.map +1 -0
- package/lib/pipelines.d.ts +10 -0
- package/lib/pipelines.d.ts.map +1 -0
- package/lib/pipelines.js +141 -0
- package/lib/pipelines.js.map +1 -0
- package/lib/sunshadow.d.ts +52 -0
- package/lib/sunshadow.d.ts.map +1 -0
- package/lib/sunshadow.js +150 -0
- package/lib/sunshadow.js.map +1 -0
- package/lib/tsl.d.ts +20 -0
- package/lib/tsl.d.ts.map +1 -0
- package/lib/tsl.js +98 -0
- package/lib/tsl.js.map +1 -0
- package/package.json +58 -3
package/lib/sunshadow.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Near-field sun shadow pass, texel-snapped orthographic shadow camera placement,
|
|
2
|
+
// and exception-safe visibility guard shared across stage passes.
|
|
3
|
+
import * as THREE from "three/webgpu";
|
|
4
|
+
const _snapSun = new THREE.Vector3();
|
|
5
|
+
const _snapFocus = new THREE.Vector3();
|
|
6
|
+
const _snapLightPos = new THREE.Vector3();
|
|
7
|
+
const _snapRight = new THREE.Vector3();
|
|
8
|
+
const _snapUp = new THREE.Vector3();
|
|
9
|
+
const _snapFwd = new THREE.Vector3();
|
|
10
|
+
/**
|
|
11
|
+
* Position an orthographic sun shadow camera around `focus`, snapping its light-space XY
|
|
12
|
+
* to exact texel increments to prevent shadow swimming as the camera moves.
|
|
13
|
+
*/
|
|
14
|
+
export function snapOrthoShadowCamera(opts) {
|
|
15
|
+
const { camera, sunDir, focus, halfExtent, resolution, distance, reversedDepth, outViewProj, outSnappedFocus } = opts;
|
|
16
|
+
_snapSun.copy(sunDir).normalize();
|
|
17
|
+
_snapFocus.copy(focus);
|
|
18
|
+
_snapLightPos.copy(_snapFocus).addScaledVector(_snapSun, distance);
|
|
19
|
+
camera.position.copy(_snapLightPos);
|
|
20
|
+
camera.up.set(0, 1, 0);
|
|
21
|
+
if (Math.abs(_snapSun.y) > 0.96)
|
|
22
|
+
camera.up.set(0, 0, -1);
|
|
23
|
+
camera.lookAt(_snapFocus);
|
|
24
|
+
camera.updateMatrixWorld(true);
|
|
25
|
+
camera.matrixWorld.extractBasis(_snapRight, _snapUp, _snapFwd);
|
|
26
|
+
const worldPerTexel = (halfExtent * 2) / resolution;
|
|
27
|
+
const projX = _snapFocus.dot(_snapRight);
|
|
28
|
+
const projY = _snapFocus.dot(_snapUp);
|
|
29
|
+
const snapX = Math.round(projX / worldPerTexel) * worldPerTexel - projX;
|
|
30
|
+
const snapY = Math.round(projY / worldPerTexel) * worldPerTexel - projY;
|
|
31
|
+
_snapFocus.addScaledVector(_snapRight, snapX).addScaledVector(_snapUp, snapY);
|
|
32
|
+
_snapLightPos.copy(_snapFocus).addScaledVector(_snapSun, distance);
|
|
33
|
+
camera.position.copy(_snapLightPos);
|
|
34
|
+
camera.lookAt(_snapFocus);
|
|
35
|
+
camera.coordinateSystem = THREE.WebGPUCoordinateSystem;
|
|
36
|
+
if (reversedDepth !== undefined) {
|
|
37
|
+
camera._reversedDepth = reversedDepth;
|
|
38
|
+
}
|
|
39
|
+
camera.updateProjectionMatrix();
|
|
40
|
+
camera.updateMatrixWorld(true);
|
|
41
|
+
outViewProj?.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
|
|
42
|
+
outSnappedFocus?.copy(_snapFocus);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Reusable visibility scope for auxiliary passes (shadow maps, planar reflections):
|
|
46
|
+
* hides currently visible objects and restores them unconditionally in `finally`.
|
|
47
|
+
*/
|
|
48
|
+
export class VisibilityGuard {
|
|
49
|
+
hidden = [];
|
|
50
|
+
hide(obj) {
|
|
51
|
+
if (obj && obj.visible) {
|
|
52
|
+
this.hidden.push(obj);
|
|
53
|
+
obj.visible = false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
hideAll(objs) {
|
|
57
|
+
for (const obj of objs)
|
|
58
|
+
this.hide(obj);
|
|
59
|
+
}
|
|
60
|
+
restore() {
|
|
61
|
+
for (let i = 0; i < this.hidden.length; i++) {
|
|
62
|
+
this.hidden[i].visible = true;
|
|
63
|
+
}
|
|
64
|
+
this.hidden.length = 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export class SunShadowPass {
|
|
68
|
+
visGuard = new VisibilityGuard();
|
|
69
|
+
opaqued = [];
|
|
70
|
+
wasTransparent = [];
|
|
71
|
+
wasDepthWrite = [];
|
|
72
|
+
renderer;
|
|
73
|
+
scene;
|
|
74
|
+
atmo;
|
|
75
|
+
constructor(renderer, scene, atmo) {
|
|
76
|
+
this.renderer = renderer;
|
|
77
|
+
this.scene = scene;
|
|
78
|
+
this.atmo = atmo;
|
|
79
|
+
}
|
|
80
|
+
render(cam, fwd, hLen, focusPos, heightAt, actors, skip, focusExact = false) {
|
|
81
|
+
if (focusExact) {
|
|
82
|
+
this.atmo.updateSunShadowCamera(focusPos.x, focusPos.y, focusPos.z);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const fx = cam.position.x + (fwd.x / hLen) * 14;
|
|
86
|
+
const fz = cam.position.z + (fwd.z / hLen) * 14;
|
|
87
|
+
this.atmo.updateSunShadowCamera(fx, heightAt(fx, fz), fz);
|
|
88
|
+
}
|
|
89
|
+
this.visGuard.hideAll(skip);
|
|
90
|
+
for (const child of this.scene.children) {
|
|
91
|
+
if (child.isLine2 || child.isLineSegments2 || child.isPoints || child.isLine) {
|
|
92
|
+
this.visGuard.hide(child);
|
|
93
|
+
}
|
|
94
|
+
else if (child.isMesh &&
|
|
95
|
+
child.material &&
|
|
96
|
+
(child.material.transparent || child.material.depthWrite === false)) {
|
|
97
|
+
this.visGuard.hide(child);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (const actor of actors) {
|
|
101
|
+
if (actor.hiddenShadows) {
|
|
102
|
+
this.visGuard.hideAll(actor.hiddenShadows);
|
|
103
|
+
}
|
|
104
|
+
const opacity = actor.opacity ?? 1;
|
|
105
|
+
if (opacity <= 0.6) {
|
|
106
|
+
this.visGuard.hide(actor.mesh);
|
|
107
|
+
if (actor.auxMeshes) {
|
|
108
|
+
this.visGuard.hideAll(actor.auxMeshes);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
this.makeOpaqueOf(actor.mesh);
|
|
113
|
+
if (actor.auxMeshes) {
|
|
114
|
+
for (const aux of actor.auxMeshes)
|
|
115
|
+
this.makeOpaqueOf(aux);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const prevTarget = this.renderer.getRenderTarget();
|
|
120
|
+
const prevAutoClear = this.renderer.autoClear;
|
|
121
|
+
this.renderer.autoClear = true;
|
|
122
|
+
this.renderer.setRenderTarget(this.atmo.sunShadowRT);
|
|
123
|
+
try {
|
|
124
|
+
this.renderer.render(this.scene, this.atmo.sunShadowCam);
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
this.renderer.setRenderTarget(prevTarget);
|
|
128
|
+
this.renderer.autoClear = prevAutoClear;
|
|
129
|
+
for (let i = 0; i < this.opaqued.length; i++) {
|
|
130
|
+
this.opaqued[i].transparent = this.wasTransparent[i];
|
|
131
|
+
this.opaqued[i].depthWrite = this.wasDepthWrite[i];
|
|
132
|
+
}
|
|
133
|
+
this.opaqued.length = this.wasTransparent.length = this.wasDepthWrite.length = 0;
|
|
134
|
+
this.visGuard.restore();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
makeOpaqueOf(obj) {
|
|
138
|
+
const mat = obj.material;
|
|
139
|
+
if (mat)
|
|
140
|
+
this.makeOpaque(mat);
|
|
141
|
+
}
|
|
142
|
+
makeOpaque(m) {
|
|
143
|
+
this.opaqued.push(m);
|
|
144
|
+
this.wasTransparent.push(m.transparent);
|
|
145
|
+
this.wasDepthWrite.push(m.depthWrite);
|
|
146
|
+
m.transparent = false;
|
|
147
|
+
m.depthWrite = true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=sunshadow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sunshadow.js","sourceRoot":"","sources":["../src/sunshadow.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,kEAAkE;AAElE,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AA2BtC,MAAM,QAAQ,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;AACrC,MAAM,UAAU,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;AACvC,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;AAC1C,MAAM,UAAU,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;AACvC,MAAM,OAAO,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;AACpC,MAAM,QAAQ,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;AAErC;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAkC;IACtE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,QAA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,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,UAAU,CAAC,CAAiB;QAClC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACrB,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;QACxC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;QACtC,CAAC,CAAC,WAAW,GAAG,KAAK,CAAC;QACtB,CAAC,CAAC,UAAU,GAAG,IAAI,CAAC;IACtB,CAAC;CACF","sourcesContent":["// Near-field sun shadow pass, texel-snapped orthographic shadow camera placement,\n// and exception-safe visibility guard shared across stage passes.\n\nimport * as THREE from \"three/webgpu\";\n\nexport interface ShadowCasterActor {\n mesh: THREE.Object3D;\n auxMeshes?: THREE.Object3D[];\n hiddenShadows?: (THREE.Object3D | null | undefined)[];\n opacity?: number;\n}\n\nexport interface SunShadowAtmosphereTarget {\n sunShadowRT: THREE.RenderTarget;\n sunShadowCam: THREE.OrthographicCamera;\n updateSunShadowCamera: (focusX: number, focusY: number, focusZ: number) => void;\n}\n\nexport interface SnapOrthoShadowCameraOptions {\n camera: THREE.OrthographicCamera;\n sunDir: THREE.Vector3;\n focus: THREE.Vector3;\n halfExtent: number;\n resolution: number;\n distance: number;\n reversedDepth?: boolean;\n outViewProj?: THREE.Matrix4;\n outSnappedFocus?: THREE.Vector3;\n}\n\nconst _snapSun = new THREE.Vector3();\nconst _snapFocus = new THREE.Vector3();\nconst _snapLightPos = new THREE.Vector3();\nconst _snapRight = new THREE.Vector3();\nconst _snapUp = new THREE.Vector3();\nconst _snapFwd = new THREE.Vector3();\n\n/**\n * Position an orthographic sun shadow camera around `focus`, snapping its light-space XY\n * to exact texel increments to prevent shadow swimming as the camera moves.\n */\nexport function snapOrthoShadowCamera(opts: SnapOrthoShadowCameraOptions): void {\n const { camera, sunDir, focus, halfExtent, resolution, distance, reversedDepth, outViewProj, outSnappedFocus } = opts;\n _snapSun.copy(sunDir).normalize();\n _snapFocus.copy(focus);\n _snapLightPos.copy(_snapFocus).addScaledVector(_snapSun, distance);\n\n camera.position.copy(_snapLightPos);\n camera.up.set(0, 1, 0);\n if (Math.abs(_snapSun.y) > 0.96) camera.up.set(0, 0, -1);\n camera.lookAt(_snapFocus);\n camera.updateMatrixWorld(true);\n camera.matrixWorld.extractBasis(_snapRight, _snapUp, _snapFwd);\n\n const worldPerTexel = (halfExtent * 2) / resolution;\n const projX = _snapFocus.dot(_snapRight);\n const projY = _snapFocus.dot(_snapUp);\n const snapX = Math.round(projX / worldPerTexel) * worldPerTexel - projX;\n const snapY = Math.round(projY / worldPerTexel) * worldPerTexel - projY;\n _snapFocus.addScaledVector(_snapRight, snapX).addScaledVector(_snapUp, snapY);\n _snapLightPos.copy(_snapFocus).addScaledVector(_snapSun, distance);\n\n camera.position.copy(_snapLightPos);\n camera.lookAt(_snapFocus);\n camera.coordinateSystem = THREE.WebGPUCoordinateSystem;\n if (reversedDepth !== undefined) {\n (camera as unknown as { _reversedDepth: boolean })._reversedDepth = reversedDepth;\n }\n camera.updateProjectionMatrix();\n camera.updateMatrixWorld(true);\n outViewProj?.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);\n outSnappedFocus?.copy(_snapFocus);\n}\n\n/**\n * Reusable visibility scope for auxiliary passes (shadow maps, planar reflections):\n * hides currently visible objects and restores them unconditionally in `finally`.\n */\nexport class VisibilityGuard {\n private readonly hidden: THREE.Object3D[] = [];\n\n hide(obj: THREE.Object3D | null | undefined): void {\n if (obj && obj.visible) {\n this.hidden.push(obj);\n obj.visible = false;\n }\n }\n\n hideAll(objs: Iterable<THREE.Object3D | null | undefined>): void {\n for (const obj of objs) this.hide(obj);\n }\n\n restore(): void {\n for (let i = 0; i < this.hidden.length; i++) {\n this.hidden[i].visible = true;\n }\n this.hidden.length = 0;\n }\n}\n\ntype SceneChild = THREE.Object3D & {\n isLine2?: boolean;\n isLineSegments2?: boolean;\n isPoints?: boolean;\n isLine?: boolean;\n isMesh?: boolean;\n material?: THREE.Material;\n};\n\nexport class SunShadowPass {\n private readonly visGuard = new VisibilityGuard();\n private readonly opaqued: THREE.Material[] = [];\n private readonly wasTransparent: boolean[] = [];\n private readonly wasDepthWrite: boolean[] = [];\n\n private readonly renderer: THREE.WebGPURenderer;\n private readonly scene: THREE.Scene;\n private readonly atmo: SunShadowAtmosphereTarget;\n\n constructor(\n renderer: THREE.WebGPURenderer,\n scene: THREE.Scene,\n atmo: SunShadowAtmosphereTarget,\n ) {\n this.renderer = renderer;\n this.scene = scene;\n this.atmo = atmo;\n }\n\n render(\n cam: THREE.PerspectiveCamera,\n fwd: THREE.Vector3,\n hLen: number,\n focusPos: THREE.Vector3,\n heightAt: (x: number, z: number) => number,\n actors: readonly ShadowCasterActor[],\n skip: readonly (THREE.Object3D | null | undefined)[],\n focusExact = false,\n ): void {\n if (focusExact) {\n this.atmo.updateSunShadowCamera(focusPos.x, focusPos.y, focusPos.z);\n } else {\n const fx = cam.position.x + (fwd.x / hLen) * 14;\n const fz = cam.position.z + (fwd.z / hLen) * 14;\n this.atmo.updateSunShadowCamera(fx, heightAt(fx, fz), fz);\n }\n\n this.visGuard.hideAll(skip);\n for (const child of this.scene.children as SceneChild[]) {\n if (child.isLine2 || child.isLineSegments2 || child.isPoints || child.isLine) {\n this.visGuard.hide(child);\n } else if (\n child.isMesh &&\n child.material &&\n (child.material.transparent || child.material.depthWrite === false)\n ) {\n this.visGuard.hide(child);\n }\n }\n\n for (const actor of actors) {\n if (actor.hiddenShadows) {\n this.visGuard.hideAll(actor.hiddenShadows);\n }\n const opacity = actor.opacity ?? 1;\n if (opacity <= 0.6) {\n this.visGuard.hide(actor.mesh);\n if (actor.auxMeshes) {\n this.visGuard.hideAll(actor.auxMeshes);\n }\n } else {\n this.makeOpaqueOf(actor.mesh);\n if (actor.auxMeshes) {\n for (const aux of actor.auxMeshes) this.makeOpaqueOf(aux);\n }\n }\n }\n\n const prevTarget = this.renderer.getRenderTarget();\n const prevAutoClear = this.renderer.autoClear;\n this.renderer.autoClear = true;\n this.renderer.setRenderTarget(this.atmo.sunShadowRT);\n try {\n this.renderer.render(this.scene, this.atmo.sunShadowCam);\n } finally {\n this.renderer.setRenderTarget(prevTarget);\n this.renderer.autoClear = prevAutoClear;\n for (let i = 0; i < this.opaqued.length; i++) {\n this.opaqued[i].transparent = this.wasTransparent[i];\n this.opaqued[i].depthWrite = this.wasDepthWrite[i];\n }\n this.opaqued.length = this.wasTransparent.length = this.wasDepthWrite.length = 0;\n this.visGuard.restore();\n }\n }\n\n private makeOpaqueOf(obj: THREE.Object3D): void {\n const mat = (obj as SceneChild).material;\n if (mat) this.makeOpaque(mat);\n }\n\n private makeOpaque(m: THREE.Material): void {\n this.opaqued.push(m);\n this.wasTransparent.push(m.transparent);\n this.wasDepthWrite.push(m.depthWrite);\n m.transparent = false;\n m.depthWrite = true;\n }\n}\n"]}
|
package/lib/tsl.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as THREE from "three/webgpu";
|
|
2
|
+
export declare const BENCH: boolean;
|
|
3
|
+
export declare const uBenchEager: THREE.UniformNode<"float", number>;
|
|
4
|
+
export declare const benchGate: (cond: THREE.Node<"bool">) => THREE.Node<"bool">;
|
|
5
|
+
export declare const typedWgslFn: <R extends "float" | "vec2" | "vec3" | "vec4">(code: string) => (...args: unknown[]) => THREE.Node<R>;
|
|
6
|
+
export declare const hash21Tsl: THREE.TSL.FnNode<[THREE.Vector2 | THREE.Node<"vec2">], THREE.Node<"float">>;
|
|
7
|
+
/** Exact 4-bilinear-tap cubic B-spline filter (safe on non-mipmapped SDFs). */
|
|
8
|
+
export declare function smoothFieldTsl(tex: THREE.DataTexture, uvNode: THREE.Node<"vec2">, uSdfSize: THREE.Node<"vec2">): THREE.Node<"vec4">;
|
|
9
|
+
export declare function makeDummyRgbaTex(): THREE.DataTexture;
|
|
10
|
+
/** Fine mineral/sand/gravel grain procedural noise. */
|
|
11
|
+
export declare const wgslSandGrainFn: (...args: unknown[]) => THREE.Node<"vec2">;
|
|
12
|
+
export declare const SAND_GRAIN = 0.3;
|
|
13
|
+
export declare const SAND_SPECK = 0.16;
|
|
14
|
+
export interface SwappableTexture {
|
|
15
|
+
at(uv: THREE.Node<"vec2">): THREE.TextureNode;
|
|
16
|
+
set(next: THREE.Texture): void;
|
|
17
|
+
}
|
|
18
|
+
export declare function swappableTexture(first: THREE.Texture): SwappableTexture;
|
|
19
|
+
export declare const textureKind: (t: THREE.Texture | null) => string;
|
|
20
|
+
//# sourceMappingURL=tsl.d.ts.map
|
package/lib/tsl.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tsl.d.ts","sourceRoot":"","sources":["../src/tsl.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAKtC,eAAO,MAAM,KAAK,SAAiG,CAAC;AACpH,eAAO,MAAM,WAAW,oCAAa,CAAC;AACtC,eAAO,MAAM,SAAS,SAAU,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAChB,CAAC;AAEvD,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,QAAQ,MAAM,KACzD,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAEnE,eAAO,MAAM,SAAS,6EAIpB,CAAC;AAEH,+EAA+E;AAC/E,wBAAgB,cAAc,CAC5B,GAAG,EAAE,KAAK,CAAC,WAAW,EACtB,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAC1B,QAAQ,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,sBA2B7B;AAED,wBAAgB,gBAAgB,IAAI,KAAK,CAAC,WAAW,CAOpD;AAED,uDAAuD;AACvD,eAAO,MAAM,eAAe,YAnDW,OAAO,EAAE,uBAqF9C,CAAC;AAEH,eAAO,MAAM,UAAU,MAAM,CAAC;AAC9B,eAAO,MAAM,UAAU,OAAO,CAAC;AAE/B,MAAM,WAAW,gBAAgB;IAC/B,EAAE,CAAC,EAAE,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC;IAC9C,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;CAChC;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,GAAG,gBAAgB,CAQvE;AAED,eAAO,MAAM,WAAW,MAAO,KAAK,CAAC,OAAO,GAAG,IAAI,KAAG,MAC4F,CAAC"}
|
package/lib/tsl.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// TSL helpers shared by WebGPU renderers.
|
|
2
|
+
import * as THREE from "three/webgpu";
|
|
3
|
+
import { Fn, dot, float, fract, mix, texture, uniform, vec2, vec4, wgslFn } from "three/tsl";
|
|
4
|
+
const IS_DEV = Boolean(import.meta.env?.DEV);
|
|
5
|
+
export const BENCH = IS_DEV && typeof location !== "undefined" && new URLSearchParams(location.search).has("bench");
|
|
6
|
+
export const uBenchEager = uniform(0);
|
|
7
|
+
export const benchGate = (cond) => BENCH ? cond.or(uBenchEager.greaterThan(0.5)) : cond;
|
|
8
|
+
export const typedWgslFn = (code) => wgslFn(code);
|
|
9
|
+
export const hash21Tsl = Fn(([pIn]) => {
|
|
10
|
+
const p = fract(pIn.mul(vec2(123.34, 456.21))).toVar();
|
|
11
|
+
p.addAssign(dot(p, p.add(45.32)));
|
|
12
|
+
return fract(p.x.mul(p.y));
|
|
13
|
+
});
|
|
14
|
+
/** Exact 4-bilinear-tap cubic B-spline filter (safe on non-mipmapped SDFs). */
|
|
15
|
+
export function smoothFieldTsl(tex, uvNode, uSdfSize) {
|
|
16
|
+
const cubic = (v) => {
|
|
17
|
+
const n = vec4(1, 2, 3, 4).sub(v).toVar();
|
|
18
|
+
const s = n.mul(n).mul(n).toVar();
|
|
19
|
+
const x = s.x;
|
|
20
|
+
const y = s.y.sub(s.x.mul(4));
|
|
21
|
+
const z = s.z.sub(s.y.mul(4)).add(s.x.mul(6));
|
|
22
|
+
const w = float(6).sub(x).sub(y).sub(z);
|
|
23
|
+
return vec4(x, y, z, w).div(6);
|
|
24
|
+
};
|
|
25
|
+
const st = uvNode.mul(uSdfSize).sub(0.5).toVar();
|
|
26
|
+
const f = fract(st).toVar();
|
|
27
|
+
const base = st.sub(f).toVar();
|
|
28
|
+
const xc = cubic(f.x).toVar();
|
|
29
|
+
const yc = cubic(f.y).toVar();
|
|
30
|
+
const c = vec4(base.x, base.x, base.y, base.y).add(vec4(-0.5, 1.5, -0.5, 1.5)).toVar();
|
|
31
|
+
const sum = vec4(xc.x.add(xc.y), xc.z.add(xc.w), yc.x.add(yc.y), yc.z.add(yc.w)).toVar();
|
|
32
|
+
const off = vec4(xc.y, xc.w, yc.y, yc.w).div(sum);
|
|
33
|
+
const at = c.add(off).div(vec4(uSdfSize.x, uSdfSize.x, uSdfSize.y, uSdfSize.y)).toVar();
|
|
34
|
+
const a = texture(tex, vec2(at.x, at.z)).level(float(0));
|
|
35
|
+
const b = texture(tex, vec2(at.y, at.z)).level(float(0));
|
|
36
|
+
const cc = texture(tex, vec2(at.x, at.w)).level(float(0));
|
|
37
|
+
const d = texture(tex, vec2(at.y, at.w)).level(float(0));
|
|
38
|
+
const sx = sum.x.div(sum.x.add(sum.y));
|
|
39
|
+
const sy = sum.z.div(sum.z.add(sum.w));
|
|
40
|
+
return mix(mix(d, cc, sx), mix(b, a, sx), sy);
|
|
41
|
+
}
|
|
42
|
+
export function makeDummyRgbaTex() {
|
|
43
|
+
const tex = new THREE.DataTexture(new Uint8Array([0, 0, 0, 0]), 1, 1, THREE.RGBAFormat, THREE.UnsignedByteType);
|
|
44
|
+
tex.magFilter = tex.minFilter = THREE.LinearFilter;
|
|
45
|
+
tex.colorSpace = THREE.NoColorSpace;
|
|
46
|
+
tex.generateMipmaps = false;
|
|
47
|
+
tex.needsUpdate = true;
|
|
48
|
+
return tex;
|
|
49
|
+
}
|
|
50
|
+
/** Fine mineral/sand/gravel grain procedural noise. */
|
|
51
|
+
export const wgslSandGrainFn = typedWgslFn(`
|
|
52
|
+
fn wgsl_sand_grain(p: vec2f) -> vec2f {
|
|
53
|
+
var acc: f32 = 0.0;
|
|
54
|
+
for (var o: i32 = 0; o < 2; o++) {
|
|
55
|
+
let q = p * select(250.0, 700.0, o == 1);
|
|
56
|
+
let c = floor(q);
|
|
57
|
+
let f = q - c;
|
|
58
|
+
let u = f * f * (3.0 - 2.0 * f);
|
|
59
|
+
let ci = vec2i(c);
|
|
60
|
+
var v: f32 = 0.0;
|
|
61
|
+
for (var k: i32 = 0; k < 4; k++) {
|
|
62
|
+
let ox = k & 1;
|
|
63
|
+
let oy = k >> 1u;
|
|
64
|
+
var h: u32 = ((u32(ci.x + ox) * 0x9E3779B1u) ^ (u32(ci.y + oy) * 0x85EBCA77u)) ^ ((u32(o) + 3u) * 0xC2B2AE3Du);
|
|
65
|
+
h = (h ^ (h >> 15u)) * 0x2C1B3C6Du;
|
|
66
|
+
h = (h ^ (h >> 12u)) * 0x297A2D39u;
|
|
67
|
+
h = h ^ (h >> 15u);
|
|
68
|
+
let wx = select(1.0 - u.x, u.x, ox == 1);
|
|
69
|
+
let wy = select(1.0 - u.y, u.y, oy == 1);
|
|
70
|
+
v += (f32(h >> 8u) / 16777216.0) * wx * wy;
|
|
71
|
+
}
|
|
72
|
+
acc += (v - 0.5) * select(1.3, 0.7, o == 1);
|
|
73
|
+
}
|
|
74
|
+
let q2 = p * 420.0;
|
|
75
|
+
let c2 = floor(q2);
|
|
76
|
+
let ci2 = vec2i(c2);
|
|
77
|
+
var h2: u32 = (u32(ci2.x) * 0x85EBCA77u) ^ (u32(ci2.y) * 0x9E3779B1u);
|
|
78
|
+
h2 = (h2 ^ (h2 >> 15u)) * 0x2C1B3C6Du;
|
|
79
|
+
h2 = (h2 ^ (h2 >> 12u)) * 0x297A2D39u;
|
|
80
|
+
h2 = h2 ^ (h2 >> 15u);
|
|
81
|
+
let rnd = f32(h2 >> 8u) / 16777216.0;
|
|
82
|
+
let speck = smoothstep(0.955, 0.995, rnd) * (1.0 - smoothstep(0.18, 0.46, length(q2 - c2 - 0.5)));
|
|
83
|
+
return vec2f(acc, speck);
|
|
84
|
+
}
|
|
85
|
+
`);
|
|
86
|
+
export const SAND_GRAIN = 0.3;
|
|
87
|
+
export const SAND_SPECK = 0.16;
|
|
88
|
+
export function swappableTexture(first) {
|
|
89
|
+
const base = texture(first);
|
|
90
|
+
return {
|
|
91
|
+
at: (uv) => texture(base, uv),
|
|
92
|
+
set(next) {
|
|
93
|
+
base.value = next;
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export const textureKind = (t) => t ? [t.isDataArrayTexture ? "array" : "2d", t.format, t.type, t.minFilter, t.magFilter].join(":") : "none";
|
|
98
|
+
//# sourceMappingURL=tsl.js.map
|
package/lib/tsl.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tsl.js","sourceRoot":"","sources":["../src/tsl.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAE1C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAE7F,MAAM,MAAM,GAAG,OAAO,CAAE,OAAO,IAAoC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAE9E,MAAM,CAAC,MAAM,KAAK,GAAG,MAAM,IAAI,OAAO,QAAQ,KAAK,WAAW,IAAI,IAAI,eAAe,CAAC,QAAQ,CAAC,MA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G,WAAW,CAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkClD,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,CAAC;AAC9B,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,CAAC;AAO/B,MAAM,UAAU,gBAAgB,CAAC,KAAoB;IACnD,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAC5B,OAAO;QACL,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;QAC7B,GAAG,CAAC,IAAI;YACN,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QACpB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAuB,EAAU,EAAE,CAC7D,CAAC,CAAC,CAAC,CAAC,CAAE,CAAsC,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC","sourcesContent":["// TSL helpers shared by WebGPU renderers.\n\nimport * as THREE from \"three/webgpu\";\nimport { Fn, dot, float, fract, mix, texture, uniform, vec2, vec4, wgslFn } from \"three/tsl\";\n\nconst IS_DEV = Boolean((import.meta as { env?: { DEV?: boolean } }).env?.DEV);\n\nexport const BENCH = IS_DEV && typeof location !== \"undefined\" && new URLSearchParams(location.search).has(\"bench\");\nexport const uBenchEager = uniform(0);\nexport const benchGate = (cond: THREE.Node<\"bool\">): THREE.Node<\"bool\"> =>\n BENCH ? cond.or(uBenchEager.greaterThan(0.5)) : cond;\n\nexport const typedWgslFn = <R extends \"float\" | \"vec2\" | \"vec3\" | \"vec4\">(code: string) =>\n wgslFn(code) as unknown as (...args: unknown[]) => THREE.Node<R>;\n\nexport const hash21Tsl = Fn(([pIn]: [THREE.Node<\"vec2\">]) => {\n const p = fract(pIn.mul(vec2(123.34, 456.21))).toVar();\n p.addAssign(dot(p, p.add(45.32)));\n return fract(p.x.mul(p.y));\n});\n\n/** Exact 4-bilinear-tap cubic B-spline filter (safe on non-mipmapped SDFs). */\nexport function smoothFieldTsl(\n tex: THREE.DataTexture,\n uvNode: THREE.Node<\"vec2\">,\n uSdfSize: THREE.Node<\"vec2\">,\n) {\n const cubic = (v: THREE.Node<\"float\">) => {\n const n = vec4(1, 2, 3, 4).sub(v).toVar();\n const s = n.mul(n).mul(n).toVar();\n const x = s.x;\n const y = s.y.sub(s.x.mul(4));\n const z = s.z.sub(s.y.mul(4)).add(s.x.mul(6));\n const w = float(6).sub(x).sub(y).sub(z);\n return vec4(x, y, z, w).div(6);\n };\n const st = uvNode.mul(uSdfSize).sub(0.5).toVar();\n const f = fract(st).toVar();\n const base = st.sub(f).toVar();\n const xc = cubic(f.x).toVar();\n const yc = cubic(f.y).toVar();\n const c = vec4(base.x, base.x, base.y, base.y).add(vec4(-0.5, 1.5, -0.5, 1.5)).toVar();\n const sum = vec4(xc.x.add(xc.y), xc.z.add(xc.w), yc.x.add(yc.y), yc.z.add(yc.w)).toVar();\n const off = vec4(xc.y, xc.w, yc.y, yc.w).div(sum);\n const at = c.add(off).div(vec4(uSdfSize.x, uSdfSize.x, uSdfSize.y, uSdfSize.y)).toVar();\n const a = texture(tex, vec2(at.x, at.z)).level(float(0));\n const b = texture(tex, vec2(at.y, at.z)).level(float(0));\n const cc = texture(tex, vec2(at.x, at.w)).level(float(0));\n const d = texture(tex, vec2(at.y, at.w)).level(float(0));\n const sx = sum.x.div(sum.x.add(sum.y));\n const sy = sum.z.div(sum.z.add(sum.w));\n return mix(mix(d, cc, sx), mix(b, a, sx), sy);\n}\n\nexport function makeDummyRgbaTex(): THREE.DataTexture {\n const tex = new THREE.DataTexture(new Uint8Array([0, 0, 0, 0]), 1, 1, THREE.RGBAFormat, THREE.UnsignedByteType);\n tex.magFilter = tex.minFilter = THREE.LinearFilter;\n tex.colorSpace = THREE.NoColorSpace;\n tex.generateMipmaps = false;\n tex.needsUpdate = true;\n return tex;\n}\n\n/** Fine mineral/sand/gravel grain procedural noise. */\nexport const wgslSandGrainFn = typedWgslFn<\"vec2\">(`\n fn wgsl_sand_grain(p: vec2f) -> vec2f {\n var acc: f32 = 0.0;\n for (var o: i32 = 0; o < 2; o++) {\n let q = p * select(250.0, 700.0, o == 1);\n let c = floor(q);\n let f = q - c;\n let u = f * f * (3.0 - 2.0 * f);\n let ci = vec2i(c);\n var v: f32 = 0.0;\n for (var k: i32 = 0; k < 4; k++) {\n let ox = k & 1;\n let oy = k >> 1u;\n var h: u32 = ((u32(ci.x + ox) * 0x9E3779B1u) ^ (u32(ci.y + oy) * 0x85EBCA77u)) ^ ((u32(o) + 3u) * 0xC2B2AE3Du);\n h = (h ^ (h >> 15u)) * 0x2C1B3C6Du;\n h = (h ^ (h >> 12u)) * 0x297A2D39u;\n h = h ^ (h >> 15u);\n let wx = select(1.0 - u.x, u.x, ox == 1);\n let wy = select(1.0 - u.y, u.y, oy == 1);\n v += (f32(h >> 8u) / 16777216.0) * wx * wy;\n }\n acc += (v - 0.5) * select(1.3, 0.7, o == 1);\n }\n let q2 = p * 420.0;\n let c2 = floor(q2);\n let ci2 = vec2i(c2);\n var h2: u32 = (u32(ci2.x) * 0x85EBCA77u) ^ (u32(ci2.y) * 0x9E3779B1u);\n h2 = (h2 ^ (h2 >> 15u)) * 0x2C1B3C6Du;\n h2 = (h2 ^ (h2 >> 12u)) * 0x297A2D39u;\n h2 = h2 ^ (h2 >> 15u);\n let rnd = f32(h2 >> 8u) / 16777216.0;\n let speck = smoothstep(0.955, 0.995, rnd) * (1.0 - smoothstep(0.18, 0.46, length(q2 - c2 - 0.5)));\n return vec2f(acc, speck);\n }\n`);\n\nexport const SAND_GRAIN = 0.3;\nexport const SAND_SPECK = 0.16;\n\nexport interface SwappableTexture {\n at(uv: THREE.Node<\"vec2\">): THREE.TextureNode;\n set(next: THREE.Texture): void;\n}\n\nexport function swappableTexture(first: THREE.Texture): SwappableTexture {\n const base = texture(first);\n return {\n at: (uv) => texture(base, uv),\n set(next) {\n base.value = next;\n },\n };\n}\n\nexport const textureKind = (t: THREE.Texture | null): string =>\n t ? [(t as { isDataArrayTexture?: boolean }).isDataArrayTexture ? \"array\" : \"2d\", t.format, t.type, t.minFilter, t.magFilter].join(\":\") : \"none\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webgpu-stage",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared Three.js WebGPU stage kernel: renderer boot, async pipelines, TSL helpers, sun shadows, atmosphere/sky dome, post-processing pipeline, and procedural look textures.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/jakobholmelund/webgpu-biome.git",
|
|
9
|
+
"directory": "packages/stage"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/jakobholmelund/webgpu-biome/tree/main/packages/stage#readme",
|
|
12
|
+
"bugs": "https://github.com/jakobholmelund/webgpu-biome/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"three",
|
|
15
|
+
"threejs",
|
|
16
|
+
"webgpu",
|
|
17
|
+
"tsl",
|
|
18
|
+
"stage",
|
|
19
|
+
"postprocessing",
|
|
20
|
+
"atmosphere",
|
|
21
|
+
"shadows"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./lib/index.js",
|
|
25
|
+
"types": "./lib/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./lib/index.d.ts",
|
|
29
|
+
"import": "./lib/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"lib",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/three": "^0.187.0",
|
|
43
|
+
"three": "^0.187.0",
|
|
44
|
+
"typescript": "^7.0.2"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"three": ">=0.183.0",
|
|
48
|
+
"@types/three": ">=0.183.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependenciesMeta": {
|
|
51
|
+
"@types/three": {
|
|
52
|
+
"optional": true
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "pnpm run build:lib",
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"test": "node --test test/*.test.mjs test/*.test.ts",
|
|
59
|
+
"build:lib": "rm -rf lib && tsc -p tsconfig.lib.json && node ../../tooling/rewrite-dts-extensions.mjs lib"
|
|
60
|
+
}
|
|
6
61
|
}
|