@driftengine/core 4.5.0 → 4.6.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/CHANGELOG.json +51 -0
- package/README.md +1 -1
- package/dist/geometry/boxCsg.d.ts +28 -0
- package/dist/geometry/boxCsg.js +139 -0
- package/dist/geometry/csg.d.ts +36 -0
- package/dist/geometry/csg.js +329 -0
- package/dist/geometry/meshBuilder.js +13 -0
- package/dist/geometry/meshConcat.js +1 -0
- package/dist/geometry/pixelFont.js +2 -0
- package/dist/geometry/solid.d.ts +61 -0
- package/dist/geometry/solid.js +152 -0
- package/dist/geometry/solidFlat.d.ts +35 -0
- package/dist/geometry/solidFlat.js +428 -0
- package/dist/geometry/solidHarness.d.ts +19 -0
- package/dist/geometry/solidHarness.js +66 -0
- package/dist/geometry/solidNormals.d.ts +17 -0
- package/dist/geometry/solidNormals.js +86 -0
- package/dist/geometry/solidRound.d.ts +41 -0
- package/dist/geometry/solidRound.js +323 -0
- package/dist/geometry/solidWriter.d.ts +16 -0
- package/dist/geometry/solidWriter.js +42 -0
- package/dist/index.d.ts +23 -2
- package/dist/index.js +13 -1
- package/dist/render/backend/api.d.ts +17 -3
- package/dist/render/backend/drawFog.d.ts +26 -0
- package/dist/render/backend/drawFog.js +30 -0
- package/dist/render/backend/materialChanges.d.ts +2 -1
- package/dist/render/backend/materialChanges.js +6 -1
- package/dist/render/backend/webgl2/instanced.d.ts +24 -5
- package/dist/render/backend/webgl2/instanced.js +72 -15
- package/dist/render/backend/webgl2/renderer.d.ts +122 -8
- package/dist/render/backend/webgl2/renderer.js +238 -121
- package/dist/render/backend/webgpu/buffers.d.ts +10 -0
- package/dist/render/backend/webgpu/buffers.js +34 -3
- package/dist/render/backend/webgpu/depthPass.js +5 -1
- package/dist/render/backend/webgpu/drawCommand.d.ts +5 -0
- package/dist/render/backend/webgpu/drawCommand.js +1 -0
- package/dist/render/backend/webgpu/flatPass.d.ts +4 -1
- package/dist/render/backend/webgpu/flatPass.js +38 -9
- package/dist/render/backend/webgpu/glassTintPass.js +2 -1
- package/dist/render/backend/webgpu/instanceCullPass.d.ts +34 -0
- package/dist/render/backend/webgpu/instanceCullPass.js +144 -0
- package/dist/render/backend/webgpu/instanced.d.ts +11 -1
- package/dist/render/backend/webgpu/instanced.js +21 -3
- package/dist/render/backend/webgpu/latePass.d.ts +6 -2
- package/dist/render/backend/webgpu/latePass.js +11 -4
- package/dist/render/backend/webgpu/postPass.d.ts +1 -1
- package/dist/render/backend/webgpu/renderer.d.ts +100 -10
- package/dist/render/backend/webgpu/renderer.js +462 -201
- package/dist/render/backend/webgpu/sdfTextPass.js +6 -1
- package/dist/render/backend/webgpu/shaders/instanceCull.wgsl.d.ts +18 -0
- package/dist/render/backend/webgpu/shaders/instanceCull.wgsl.js +59 -0
- package/dist/render/backend/webgpu/surfaceTexturePass.d.ts +17 -3
- package/dist/render/backend/webgpu/surfaceTexturePass.js +77 -38
- package/dist/render/driftLight/bake.d.ts +7 -0
- package/dist/render/driftLight/bake.js +61 -56
- package/dist/render/driftLight/denseField.d.ts +46 -0
- package/dist/render/driftLight/denseField.js +109 -0
- package/dist/render/driftLight/lightField.d.ts +10 -5
- package/dist/render/driftLight/lightField.js +25 -19
- package/dist/render/driftLight/presence.d.ts +50 -0
- package/dist/render/driftLight/presence.js +43 -0
- package/dist/render/driftLight/uniforms.d.ts +3 -3
- package/dist/render/driftLight/uniforms.js +5 -6
- package/dist/render/driftLight/worldLightField.d.ts +41 -0
- package/dist/render/driftLight/worldLightField.js +66 -0
- package/dist/render/fixtureAtlas.d.ts +20 -0
- package/dist/render/fixtureAtlas.js +49 -0
- package/dist/render/instanceCull.d.ts +63 -0
- package/dist/render/instanceCull.js +160 -0
- package/dist/render/instances.d.ts +15 -0
- package/dist/render/lightBudget.d.ts +22 -33
- package/dist/render/lightBudget.js +24 -35
- package/dist/render/lightGrid.d.ts +42 -0
- package/dist/render/lightGrid.js +87 -0
- package/dist/render/mesh.d.ts +18 -24
- package/dist/render/mesh.js +51 -68
- package/dist/render/occlusion.d.ts +10 -0
- package/dist/render/occlusion.js +36 -7
- package/dist/render/pointLightSelection.d.ts +7 -0
- package/dist/render/pointLightSelection.js +29 -4
- package/dist/render/rainField.d.ts +13 -0
- package/dist/render/rainField.js +27 -5
- package/dist/render/recon/lateDepth.d.ts +11 -0
- package/dist/render/recon/lateDepth.js +70 -0
- package/dist/render/recon/resolve.d.ts +34 -4
- package/dist/render/recon/resolve.js +161 -20
- package/dist/render/rendererHarness.js +4 -0
- package/dist/render/sceneTarget.d.ts +2 -0
- package/dist/render/sceneTarget.js +2 -0
- package/dist/render/shaders/depth.js +12 -9
- package/dist/render/shaders/flat/driftLight.d.ts +1 -1
- package/dist/render/shaders/flat/driftLight.js +32 -7
- package/dist/render/shaders/flat/index.js +5 -3
- package/dist/render/shaders/flat/main.d.ts +1 -1
- package/dist/render/shaders/flat/main.js +75 -23
- package/dist/render/shaders/flat/preamble.js +75 -20
- package/dist/render/shaders/flat/surfaceEffects.d.ts +29 -0
- package/dist/render/shaders/flat/surfaceEffects.js +152 -0
- package/dist/render/shaders/generated/depth.wgsl.d.ts +8 -8
- package/dist/render/shaders/generated/depth.wgsl.js +8 -8
- package/dist/render/shaders/generated/flat.wgsl.d.ts +1320 -1320
- package/dist/render/shaders/generated/flat.wgsl.js +1359 -1345
- package/dist/render/shaders/generated/lightVolume.wgsl.js +18 -4
- package/dist/render/shaders/generated/rush.wgsl.d.ts +21 -16
- package/dist/render/shaders/generated/rush.wgsl.js +21 -16
- package/dist/render/shaders/generated/sdfText.wgsl.d.ts +2 -2
- package/dist/render/shaders/generated/sdfText.wgsl.js +2 -2
- package/dist/render/shaders/generated/text.wgsl.d.ts +1 -1
- package/dist/render/shaders/generated/text.wgsl.js +1 -1
- package/dist/render/shaders/gi/driftLight.wgsl.d.ts +1 -1
- package/dist/render/shaders/gi/driftLight.wgsl.js +27 -4
- package/dist/render/shaders/recon/lateDepth.wgsl.js +38 -13
- package/dist/render/shaders/recon/resolve.wgsl.d.ts +8 -7
- package/dist/render/shaders/recon/resolve.wgsl.js +161 -27
- package/dist/render/shaders/rush.js +11 -1
- package/dist/render/shaders/sdfText.d.ts +1 -1
- package/dist/render/shaders/sdfText.js +3 -2
- package/dist/render/shaders/text.d.ts +1 -1
- package/dist/render/shaders/text.js +18 -9
- package/dist/render/surfaceEffects.d.ts +83 -0
- package/dist/render/surfaceEffects.js +75 -0
- package/dist/render/surfaceScene.d.ts +8 -0
- package/dist/render/surfaceScene.js +8 -0
- package/dist/render/surfaceTexture.d.ts +18 -2
- package/dist/render/surfaceTexture.js +68 -15
- package/dist/render/temporalAa.d.ts +25 -6
- package/dist/render/temporalAa.js +45 -15
- package/dist/render/textureSource.d.ts +30 -0
- package/dist/render/textureSource.js +53 -0
- package/dist/render/vertexDefaults.js +2 -0
- package/dist/world/hlod.d.ts +104 -0
- package/dist/world/hlod.js +214 -0
- package/package.json +6 -6
- package/src/geometry/boxCsg.ts +186 -0
- package/src/geometry/csg.ts +384 -0
- package/src/geometry/meshBuilder.ts +15 -0
- package/src/geometry/meshConcat.ts +1 -0
- package/src/geometry/pixelFont.ts +2 -0
- package/src/geometry/solid.ts +189 -0
- package/src/geometry/solidFlat.ts +522 -0
- package/src/geometry/solidHarness.ts +83 -0
- package/src/geometry/solidNormals.ts +103 -0
- package/src/geometry/solidRound.ts +443 -0
- package/src/geometry/solidWriter.ts +58 -0
- package/src/index.ts +51 -1
- package/src/render/backend/api.ts +21 -2
- package/src/render/backend/drawFog.ts +31 -0
- package/src/render/backend/materialChanges.ts +9 -2
- package/src/render/backend/webgl2/instanced.ts +80 -14
- package/src/render/backend/webgl2/renderer.ts +300 -143
- package/src/render/backend/webgpu/buffers.ts +42 -3
- package/src/render/backend/webgpu/depthPass.ts +5 -1
- package/src/render/backend/webgpu/drawCommand.ts +6 -0
- package/src/render/backend/webgpu/flatPass.ts +41 -11
- package/src/render/backend/webgpu/glassTintPass.ts +2 -1
- package/src/render/backend/webgpu/instanceCullPass.ts +165 -0
- package/src/render/backend/webgpu/instanced.ts +21 -3
- package/src/render/backend/webgpu/latePass.ts +12 -3
- package/src/render/backend/webgpu/renderer.ts +540 -226
- package/src/render/backend/webgpu/sdfTextPass.ts +6 -1
- package/src/render/backend/webgpu/shaders/instanceCull.wgsl.ts +61 -0
- package/src/render/backend/webgpu/surfaceTexturePass.ts +90 -50
- package/src/render/driftLight/bake.ts +85 -62
- package/src/render/driftLight/denseField.ts +149 -0
- package/src/render/driftLight/lightField.ts +33 -18
- package/src/render/driftLight/presence.ts +69 -0
- package/src/render/driftLight/uniforms.ts +8 -9
- package/src/render/driftLight/worldLightField.ts +83 -0
- package/src/render/fixtureAtlas.ts +63 -0
- package/src/render/instanceCull.ts +205 -0
- package/src/render/instances.ts +16 -0
- package/src/render/lightBudget.ts +25 -36
- package/src/render/lightGrid.ts +130 -0
- package/src/render/mesh.ts +59 -72
- package/src/render/occlusion.ts +51 -7
- package/src/render/pointLightSelection.ts +72 -4
- package/src/render/rainField.ts +39 -5
- package/src/render/recon/lateDepth.ts +83 -0
- package/src/render/recon/resolve.ts +206 -20
- package/src/render/rendererHarness.ts +4 -0
- package/src/render/sceneTarget.ts +4 -0
- package/src/render/shaders/depth.ts +12 -9
- package/src/render/shaders/flat/driftLight.ts +32 -7
- package/src/render/shaders/flat/index.ts +5 -3
- package/src/render/shaders/flat/main.ts +75 -23
- package/src/render/shaders/flat/preamble.ts +75 -20
- package/src/render/shaders/flat/surfaceEffects.ts +152 -0
- package/src/render/shaders/generated/depth.wgsl.ts +8 -8
- package/src/render/shaders/generated/flat.wgsl.ts +1361 -1345
- package/src/render/shaders/generated/lightVolume.wgsl.ts +20 -4
- package/src/render/shaders/generated/rush.wgsl.ts +21 -16
- package/src/render/shaders/generated/sdfText.wgsl.ts +2 -2
- package/src/render/shaders/generated/text.wgsl.ts +1 -1
- package/src/render/shaders/gi/driftLight.wgsl.ts +27 -4
- package/src/render/shaders/recon/lateDepth.wgsl.ts +38 -13
- package/src/render/shaders/recon/resolve.wgsl.ts +161 -27
- package/src/render/shaders/rush.ts +11 -1
- package/src/render/shaders/sdfText.ts +3 -2
- package/src/render/shaders/text.ts +18 -9
- package/src/render/surfaceEffects.ts +132 -0
- package/src/render/surfaceScene.ts +16 -0
- package/src/render/surfaceTexture.ts +110 -19
- package/src/render/temporalAa.ts +78 -14
- package/src/render/textureSource.ts +72 -0
- package/src/render/vertexDefaults.ts +2 -0
- package/src/world/hlod.ts +290 -0
|
@@ -13,6 +13,7 @@ import { SKINNING_GLSL } from '../skinning.ts';
|
|
|
13
13
|
import { SURFACE_GLSL } from './surface.ts';
|
|
14
14
|
import { MAIN_GLSL } from './main.ts';
|
|
15
15
|
import { DRIFT_LIGHT_GLSL } from './driftLight.ts';
|
|
16
|
+
import { SURFACE_EFFECTS_GLSL } from './surfaceEffects.ts';
|
|
16
17
|
import { CHANNEL_ATTRIBUTE, CHANNEL_BEND } from '../vertexChannel.ts';
|
|
17
18
|
import { FULL_LIGHT_BUDGET, type LightBudget } from '../../uniformVectorBudget.ts';
|
|
18
19
|
|
|
@@ -28,7 +29,7 @@ layout(location = 1) in vec3 aNormal;
|
|
|
28
29
|
layout(location = 2) in vec3 aColor;
|
|
29
30
|
layout(location = 3) in float aEmissive;
|
|
30
31
|
layout(location = 4) in float aSpecular;
|
|
31
|
-
layout(location = 5) in
|
|
32
|
+
layout(location = 5) in vec3 aUv;
|
|
32
33
|
layout(location = 6) in vec3 aEmissiveColor;
|
|
33
34
|
layout(location = 7) in float aRoughness;
|
|
34
35
|
layout(location = 8) in float aGrain;
|
|
@@ -146,7 +147,7 @@ out vec3 vWorldPos;
|
|
|
146
147
|
out float vEmissive;
|
|
147
148
|
out float vSpecular;
|
|
148
149
|
out vec4 vLightPos;
|
|
149
|
-
out
|
|
150
|
+
out vec3 vUv;
|
|
150
151
|
out vec3 vEmissiveColor;
|
|
151
152
|
out float vRoughness;
|
|
152
153
|
out float vGrain;
|
|
@@ -243,7 +244,7 @@ void main() {
|
|
|
243
244
|
vColor = aColor * tint;
|
|
244
245
|
vEmissive = aEmissive;
|
|
245
246
|
vSpecular = aSpecular;
|
|
246
|
-
vUv = aUv * uUvScale;
|
|
247
|
+
vUv = vec3(aUv.xy * uUvScale, aUv.z);
|
|
247
248
|
vEmissiveColor = aEmissiveColor;
|
|
248
249
|
vRoughness = aRoughness;
|
|
249
250
|
vGrain = aGrain;
|
|
@@ -511,6 +512,7 @@ export function flatFrag(options: FlatShaderOptions): string {
|
|
|
511
512
|
/* Unconditional for the same reason, and called only where a cutoff was asked for. */
|
|
512
513
|
CUTOUT_COVERAGE_GLSL,
|
|
513
514
|
DRIFT_LIGHT_GLSL,
|
|
515
|
+
SURFACE_EFFECTS_GLSL,
|
|
514
516
|
MAIN_GLSL,
|
|
515
517
|
].join('\n'),
|
|
516
518
|
{
|
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
/** The fragment entry point, which assembles everything above into a colour. */
|
|
2
2
|
|
|
3
3
|
export const MAIN_GLSL = `void main() {
|
|
4
|
-
|
|
4
|
+
/*
|
|
5
|
+
* Where every material map is read: the texture coordinate, and the layer of the array the face
|
|
6
|
+
* wears, rounded here so an interpolated 2.9999 lands on layer 3 however the generated WGSL
|
|
7
|
+
* turns a float into an array index. A plain texture is an array of one, so layer 0.
|
|
8
|
+
*/
|
|
9
|
+
vec3 surfaceAt = vec3(vUv.xy, floor(vUv.z + 0.5));
|
|
10
|
+
if (dot(vec4(vWorldPos, 1.0), uClipPlane) < 0.0) discard;
|
|
11
|
+
/*
|
|
12
|
+
* The layer's surface effects, zeros where the albedo array carries no table — see
|
|
13
|
+
* surfaceEffects.ts. Scroll and flipbook move surfaceAt before any map reads it, so every map of
|
|
14
|
+
* the material moves together. The gradients are taken here, outside every branch, for the room
|
|
15
|
+
* a window samples through textureGrad further down.
|
|
16
|
+
*/
|
|
17
|
+
fxLoad(int(surfaceAt.z));
|
|
18
|
+
surfaceAt = fxAnimate(surfaceAt);
|
|
19
|
+
vec2 fxDx = dFdx(surfaceAt.xy);
|
|
20
|
+
vec2 fxDy = dFdy(surfaceAt.xy);
|
|
21
|
+
if (uWriteMode.y != 0.0 && !ditherKeeps(uWriteMode.y)) discard;
|
|
5
22
|
|
|
6
23
|
/*
|
|
7
24
|
* Surface colour and its coverage, resolved before anything else — including before it is
|
|
@@ -35,13 +52,13 @@ export const MAIN_GLSL = `void main() {
|
|
|
35
52
|
*/
|
|
36
53
|
float coverage = 1.0;
|
|
37
54
|
if (uAlbedoEnabled != 0) {
|
|
38
|
-
vec4 texel = texture(uAlbedo,
|
|
55
|
+
vec4 texel = texture(uAlbedo, surfaceAt);
|
|
39
56
|
// Discarded before anything else is computed: a thrown-away fragment should not pay
|
|
40
57
|
// for the lighting it will never contribute to. The test credits alpha for the mip level
|
|
41
58
|
// it samples, so a needle averaged thin down the chain is not lost; see cutoutCoverage.ts.
|
|
42
59
|
// Behind a branch on a uniform, so a material with no cutoff pays nothing for it.
|
|
43
60
|
if (uAlbedoCutout > 0.0) {
|
|
44
|
-
float tested = cutoutAlpha(texel.a, vUv * vec2(textureSize(uAlbedo, 0)));
|
|
61
|
+
float tested = cutoutAlpha(texel.a, vUv.xy * vec2(textureSize(uAlbedo, 0).xy));
|
|
45
62
|
if (tested < uAlbedoCutout) discard;
|
|
46
63
|
}
|
|
47
64
|
albedo *= texel.rgb;
|
|
@@ -70,7 +87,7 @@ export const MAIN_GLSL = `void main() {
|
|
|
70
87
|
float ormRoughness = vRoughness;
|
|
71
88
|
float metal = 0.0;
|
|
72
89
|
if (uOrmEnabled != 0) {
|
|
73
|
-
vec3 t = texture(uOrmMap,
|
|
90
|
+
vec3 t = texture(uOrmMap, surfaceAt).rgb;
|
|
74
91
|
ormOcclusion = mix(1.0, t.r, uOrmScale.r);
|
|
75
92
|
ormRoughness = clamp(t.g * uOrmScale.g, 0.0, 1.0);
|
|
76
93
|
metal = clamp(t.b * uOrmScale.b, 0.0, 1.0);
|
|
@@ -82,6 +99,22 @@ export const MAIN_GLSL = `void main() {
|
|
|
82
99
|
* \`uLightingEnabled\` at 0 keeps to the end, and the value every lit draw starts from before
|
|
83
100
|
* the branch below builds a shaded result on top of it.
|
|
84
101
|
*/
|
|
102
|
+
/*
|
|
103
|
+
* Wear, rain and windows, on the colour and roughness everything below shades. Each is exactly
|
|
104
|
+
* nothing where its numbers are zero, which is every surface of a scene that names no effect.
|
|
105
|
+
*/
|
|
106
|
+
fxNormal = normalize(vNormal);
|
|
107
|
+
fxDistance = distance(vWorldPos, uCameraPos);
|
|
108
|
+
vec4 weathered = fxWeather(vWorldPos, albedo, ormRoughness);
|
|
109
|
+
albedo = weathered.rgb;
|
|
110
|
+
ormRoughness = weathered.w;
|
|
111
|
+
vec3 windowGlow = vec3(0.0);
|
|
112
|
+
if (fx0.x > 0.0) {
|
|
113
|
+
vec4 window = fxWindows(surfaceAt, fxDx, fxDy, coverage);
|
|
114
|
+
albedo = mix(albedo, fxRoom * 0.35, window.w);
|
|
115
|
+
windowGlow = window.rgb;
|
|
116
|
+
}
|
|
117
|
+
|
|
85
118
|
vec3 lit = albedo;
|
|
86
119
|
/* The light arriving at a glass pane from behind it, gathered in each light loop below and added
|
|
87
120
|
over the see-through at the end. Stays zero for anything that is not glass. See glass.ts. */
|
|
@@ -123,8 +156,8 @@ export const MAIN_GLSL = `void main() {
|
|
|
123
156
|
* below. The rule is about non-uniform branches, and this branch is on a uniform.
|
|
124
157
|
*/
|
|
125
158
|
if (uNormalStrength > 0.0) {
|
|
126
|
-
vec3 mapped = texture(uNormalMap,
|
|
127
|
-
mat3 tbn = tangentFrame(n, vWorldPos, vUv, vTangent, vHasTangents);
|
|
159
|
+
vec3 mapped = texture(uNormalMap, surfaceAt).xyz * 2.0 - 1.0;
|
|
160
|
+
mat3 tbn = tangentFrame(n, vWorldPos, vUv.xy, vTangent, vHasTangents);
|
|
128
161
|
n = normalize(mix(n, normalize(tbn * mapped), uNormalStrength));
|
|
129
162
|
}
|
|
130
163
|
|
|
@@ -244,14 +277,14 @@ export const MAIN_GLSL = `void main() {
|
|
|
244
277
|
* antisymmetric, the cancellation holds, and it is the more accurate estimate besides: a
|
|
245
278
|
* forward difference reports the slope half a step away from the point it is used at.
|
|
246
279
|
*/
|
|
247
|
-
vec2 duvdx = dFdx(vUv);
|
|
248
|
-
vec2 duvdy = dFdy(vUv);
|
|
280
|
+
vec2 duvdx = dFdx(vUv.xy);
|
|
281
|
+
vec2 duvdy = dFdy(vUv.xy);
|
|
249
282
|
/* Half, because a centred difference spans two steps and three.js's number means one. */
|
|
250
283
|
float reliefPerStep = uTextureRelief * 0.5;
|
|
251
|
-
float dHdx = (surfaceHeight(texture(uAlbedo, vUv + duvdx).rgb)
|
|
252
|
-
- surfaceHeight(texture(uAlbedo, vUv - duvdx).rgb)) * reliefPerStep;
|
|
253
|
-
float dHdy = (surfaceHeight(texture(uAlbedo, vUv + duvdy).rgb)
|
|
254
|
-
- surfaceHeight(texture(uAlbedo, vUv - duvdy).rgb)) * reliefPerStep;
|
|
284
|
+
float dHdx = (surfaceHeight(texture(uAlbedo, vec3(vUv.xy + duvdx, surfaceAt.z)).rgb)
|
|
285
|
+
- surfaceHeight(texture(uAlbedo, vec3(vUv.xy - duvdx, surfaceAt.z)).rgb)) * reliefPerStep;
|
|
286
|
+
float dHdy = (surfaceHeight(texture(uAlbedo, vec3(vUv.xy + duvdy, surfaceAt.z)).rgb)
|
|
287
|
+
- surfaceHeight(texture(uAlbedo, vec3(vUv.xy - duvdy, surfaceAt.z)).rgb)) * reliefPerStep;
|
|
255
288
|
|
|
256
289
|
vec3 dpdx = dFdx(vWorldPos);
|
|
257
290
|
vec3 dpdy = dFdy(vWorldPos);
|
|
@@ -908,6 +941,9 @@ export const MAIN_GLSL = `void main() {
|
|
|
908
941
|
* CLUSTER_TEXELS;
|
|
909
942
|
int clusterLights = uClustered == 1 ? int(clusterTexel(clusterBaseTexel).x) : 0;
|
|
910
943
|
|
|
944
|
+
/* The fixture atlas's size, asked once: its cookie band and its profile rows are found from it. */
|
|
945
|
+
vec2 fixtureSize = vec2(textureSize(uFixtureAtlas, 0));
|
|
946
|
+
|
|
911
947
|
// Point lights. Inverse-square-ish falloff clipped to a finite radius, so a
|
|
912
948
|
// light can be culled without a visible seam.
|
|
913
949
|
for (int slot = 0; slot < LIGHT_LOOP_MAX; slot++) {
|
|
@@ -1202,7 +1238,9 @@ export const MAIN_GLSL = `void main() {
|
|
|
1202
1238
|
vec3 iesRay = -toLight / max(dist, 1e-4);
|
|
1203
1239
|
/* The angle from the fixture's axis, as a fraction of the 0-to-180 arc every row spans. */
|
|
1204
1240
|
float iesAngle = acos(clamp(dot(iesRay, lightDir), -1.0, 1.0));
|
|
1205
|
-
|
|
1241
|
+
/* Clamped to the row's first and last texel centres, which is what the edge clamp of a
|
|
1242
|
+
texture exactly one row wide gave; the atlas can be wider now, with cookies beside it. */
|
|
1243
|
+
float iesU = clamp(iesAngle / PI_IES * IES_WIDTH, 0.5, IES_WIDTH - 0.5) / fixtureSize.x;
|
|
1206
1244
|
/*
|
|
1207
1245
|
* **The azimuth about the fixture's own axis**, behind a branch on a uniform so a scene whose
|
|
1208
1246
|
* profiles are all axially symmetric pays none of this arithmetic. A \`uniform float\` is
|
|
@@ -1256,13 +1294,14 @@ export const MAIN_GLSL = `void main() {
|
|
|
1256
1294
|
*/
|
|
1257
1295
|
float cookieTile = max(cookie, 0.0);
|
|
1258
1296
|
vec2 cookieAt = vec2(
|
|
1259
|
-
(cookieTile + clamp(cookieUv.x, COOKIE_INSET, 1.0 - COOKIE_INSET))
|
|
1260
|
-
|
|
1297
|
+
(cookieTile + clamp(cookieUv.x, COOKIE_INSET, 1.0 - COOKIE_INSET)) * COOKIE_SPAN
|
|
1298
|
+
/ fixtureSize.x,
|
|
1299
|
+
clamp(cookieUv.y, COOKIE_INSET, 1.0 - COOKIE_INSET) * COOKIE_SPAN / fixtureSize.y);
|
|
1261
1300
|
/* Unconditional and \`textureLod\`, and selected with a \`mix\` afterwards: the light
|
|
1262
1301
|
loop is not provably uniform control flow — the clustered arm reads its index from a
|
|
1263
1302
|
texture — so a branch on the *light's* cookie index around a sample is what the
|
|
1264
1303
|
2026-08-07 rule forbids. */
|
|
1265
|
-
vec3 cookieSample = textureLod(
|
|
1304
|
+
vec3 cookieSample = textureLod(uFixtureAtlas, cookieAt, 0.0).rgb;
|
|
1266
1305
|
cookieTint = mix(vec3(1.0), cookieSample, step(0.0, cookie));
|
|
1267
1306
|
}
|
|
1268
1307
|
}
|
|
@@ -1277,10 +1316,12 @@ export const MAIN_GLSL = `void main() {
|
|
|
1277
1316
|
* bytes on \`core-only\`** across the sixteen fragment permutations.
|
|
1278
1317
|
*/
|
|
1279
1318
|
float iesBase = iesRow * (uIesPlaneCount + 1.0);
|
|
1319
|
+
/* The profiles are the atlas's bottom rows, below any cookies. */
|
|
1320
|
+
float iesTop = fixtureSize.y - uIesAtlasRows;
|
|
1280
1321
|
float iesV = uIesAtlasRows <= 1.0
|
|
1281
|
-
? 0.5
|
|
1282
|
-
: (iesBase + iesTurns * uIesPlaneCount + 0.5) /
|
|
1283
|
-
float iesGain = textureLod(
|
|
1322
|
+
? (iesTop + 0.5) / fixtureSize.y
|
|
1323
|
+
: (iesTop + iesBase + iesTurns * uIesPlaneCount + 0.5) / fixtureSize.y;
|
|
1324
|
+
float iesGain = textureLod(uFixtureAtlas, vec2(iesU, iesV), 0.0).r;
|
|
1284
1325
|
float photometric = mix(1.0, iesGain, step(0.0, iesProfile));
|
|
1285
1326
|
|
|
1286
1327
|
float shape = (uLightFalloff == 1 ? falloff : falloff * falloff) * coneFalloff * photometric;
|
|
@@ -1697,10 +1738,14 @@ export const MAIN_GLSL = `void main() {
|
|
|
1697
1738
|
*/
|
|
1698
1739
|
vec3 emissiveMapped = vec3(1.0);
|
|
1699
1740
|
if (uEmissiveMapEnabled != 0) {
|
|
1700
|
-
emissiveMapped = texture(uEmissiveMap,
|
|
1741
|
+
emissiveMapped = texture(uEmissiveMap, surfaceAt).rgb * uEmissiveScale;
|
|
1701
1742
|
}
|
|
1743
|
+
/* Pulse, flicker and fade from the layer's effects; exactly 1 where it names none. */
|
|
1744
|
+
float fxGlow = fx4.z + fx5.x + fx5.w > 0.0 ? fxEmission(surfaceAt.z) : 1.0;
|
|
1702
1745
|
lit += emissiveTint * emissiveMapped * uEmissiveGain * vEmissive * uNightFactor
|
|
1703
|
-
* mix(1.0, min(lightShade, sunShade), EMISSIVE_SHADOW_SHARE);
|
|
1746
|
+
* mix(1.0, min(lightShade, sunShade), EMISSIVE_SHADOW_SHARE) * fxGlow;
|
|
1747
|
+
/* And a lit window's glow, which the scene's lit share already times: not gated on night. */
|
|
1748
|
+
lit += windowGlow * uEmissiveGain;
|
|
1704
1749
|
|
|
1705
1750
|
#if NIGHT_EMISSIVE
|
|
1706
1751
|
/*
|
|
@@ -1774,7 +1819,14 @@ export const MAIN_GLSL = `void main() {
|
|
|
1774
1819
|
fog = mediumFog(distance(vWorldPos, uCameraPos), vWorldPos.y);
|
|
1775
1820
|
}
|
|
1776
1821
|
|
|
1777
|
-
|
|
1822
|
+
/*
|
|
1823
|
+
* A surface recedes into the medium; light added to the frame fades in it (\`uFogEnabled\` at 2,
|
|
1824
|
+
* \`drawFog.ts\`). What is behind an additive draw is already fogged, so mixing the draw toward
|
|
1825
|
+
* the medium's colour and adding it puts the haze into the frame twice — a glow by day drew as
|
|
1826
|
+
* a grey shape of itself, greyer the thicker the air.
|
|
1827
|
+
*/
|
|
1828
|
+
vec3 shaded = applyOutputTransform(
|
|
1829
|
+
uFogEnabled == 2 ? lit * (1.0 - fog) : mix(lit, mediumColor(), fog));
|
|
1778
1830
|
|
|
1779
1831
|
/*
|
|
1780
1832
|
* **Refraction: the scene behind this surface, bent and absorbed.**
|
|
@@ -1871,7 +1923,7 @@ export const MAIN_GLSL = `void main() {
|
|
|
1871
1923
|
* up holding the weighted sum of colour and the weighted sum of alpha. The revealage pass draws
|
|
1872
1924
|
* the same geometry with this at 0, where the branch below is not taken at all.
|
|
1873
1925
|
*/
|
|
1874
|
-
if (
|
|
1926
|
+
if (uWriteMode.x != 0.0) {
|
|
1875
1927
|
float z = distance(vWorldPos, uCameraPos) / 200.0;
|
|
1876
1928
|
float falloff = 0.03 / (1e-5 + z * z * z * z);
|
|
1877
1929
|
float w = alpha * clamp(falloff, 1e-2, 3e3);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/** Version, inputs, uniforms and the defines the rest of the fragment shader is written against. */
|
|
2
2
|
import { COOKIE_TILE } from '../../lightBudget.ts';
|
|
3
|
+
import { IES_ATLAS_WIDTH } from '../../iesProfile.ts';
|
|
3
4
|
import {
|
|
4
5
|
CLUSTER_TEXELS,
|
|
5
6
|
CLUSTER_X,
|
|
@@ -51,7 +52,7 @@ in vec3 vWorldPos;
|
|
|
51
52
|
in float vEmissive;
|
|
52
53
|
in float vSpecular;
|
|
53
54
|
in vec4 vLightPos;
|
|
54
|
-
in
|
|
55
|
+
in vec3 vUv;
|
|
55
56
|
in vec3 vEmissiveColor;
|
|
56
57
|
in float vRoughness;
|
|
57
58
|
/*
|
|
@@ -136,7 +137,7 @@ flat in int vHasTangents;
|
|
|
136
137
|
* biome recolours a whole world without a second texture, and a mesh whose texture is
|
|
137
138
|
* white shades identically to one with no texture at all.
|
|
138
139
|
*/
|
|
139
|
-
uniform
|
|
140
|
+
uniform mediump sampler2DArray uAlbedo;
|
|
140
141
|
/** 0 leaves every textured path unevaluated, for the majority of geometry with no image. */
|
|
141
142
|
uniform int uAlbedoEnabled;
|
|
142
143
|
/**
|
|
@@ -152,6 +153,12 @@ uniform int uAlbedoEnabled;
|
|
|
152
153
|
* else changes.
|
|
153
154
|
*/
|
|
154
155
|
uniform float uAlbedoCutout;
|
|
156
|
+
/**
|
|
157
|
+
* The albedo array's per-layer effects table: six texels a layer, read with \`texelFetch\` at the
|
|
158
|
+
* layer a vertex carries. A 1×1 stand-in where the array has none, which the size test below reads
|
|
159
|
+
* as no effects at all. See \`surfaceEffects.ts\` for the layout.
|
|
160
|
+
*/
|
|
161
|
+
uniform mediump sampler2D uSurfaceEffects;
|
|
155
162
|
|
|
156
163
|
/**
|
|
157
164
|
* 1 for a surface seen from both faces: glTF's doubleSided, a curtain or a leaf card. Its draw culls
|
|
@@ -169,7 +176,7 @@ uniform int uDoubleSided;
|
|
|
169
176
|
* display values bends every normal toward the surface and reads as a green tint over everything.
|
|
170
177
|
* SurfaceTextureOptions.colorSpace already defaults to linear and says why.
|
|
171
178
|
*/
|
|
172
|
-
uniform highp
|
|
179
|
+
uniform highp sampler2DArray uNormalMap;
|
|
173
180
|
/**
|
|
174
181
|
* How hard the map turns the shading normal. 0 is no map at all, and gates the whole block.
|
|
175
182
|
*
|
|
@@ -191,7 +198,7 @@ uniform float uNormalStrength;
|
|
|
191
198
|
* Sampled linear and never sRGB. These three numbers *are* the data, not a picture of it, and
|
|
192
199
|
* decoding them as display values would bend every one of them toward its floor.
|
|
193
200
|
*/
|
|
194
|
-
uniform highp
|
|
201
|
+
uniform highp sampler2DArray uOrmMap;
|
|
195
202
|
/**
|
|
196
203
|
* 1 when a map is bound. Gates the whole block, following uAlbedoEnabled.
|
|
197
204
|
*
|
|
@@ -237,7 +244,7 @@ uniform float uEnvironmentPrefiltered;
|
|
|
237
244
|
* above, and now with a number behind them: a fifth flag was built and measured at 49% of the
|
|
238
245
|
* bundle. See \`ARCHITECTURE.md\` §1.
|
|
239
246
|
*/
|
|
240
|
-
uniform highp
|
|
247
|
+
uniform highp sampler2DArray uEmissiveMap;
|
|
241
248
|
/** 1 when a map is bound. Gates the sample, following uOrmEnabled. */
|
|
242
249
|
uniform int uEmissiveMapEnabled;
|
|
243
250
|
/**
|
|
@@ -313,9 +320,21 @@ uniform vec3 uAmbient;
|
|
|
313
320
|
uniform vec3 uAmbientGround;
|
|
314
321
|
${FOG_GLSL}
|
|
315
322
|
uniform vec3 uCameraPos;
|
|
316
|
-
/**
|
|
323
|
+
/**
|
|
324
|
+
* Optional world plane; reflection passes retain only the viewer's side.
|
|
325
|
+
*
|
|
326
|
+
* **Zero is off, and there is no flag beside it.** A zero plane gives \`dot(p, 0) = 0\`, which is
|
|
327
|
+
* never below zero, so it discards nothing — the \`uClipEnabled\` int that stood here said nothing
|
|
328
|
+
* the plane could not, and a scalar is a whole uniform row on WebGL2. Its row paid for
|
|
329
|
+
* \`uSurfaceScene\`. Other programs keep their own flags; this is the lit stage's alone.
|
|
330
|
+
*/
|
|
317
331
|
uniform vec4 uClipPlane;
|
|
318
|
-
|
|
332
|
+
/**
|
|
333
|
+
* The scene's surface clock and weather, from the environment: x seconds on the caller's clock, y
|
|
334
|
+
* how wet the world is, z the share of facade windows lit, w the share that stays lit however late.
|
|
335
|
+
* One row for all four, paid for by the flag the clip plane no longer needs.
|
|
336
|
+
*/
|
|
337
|
+
uniform vec4 uSurfaceScene;
|
|
319
338
|
uniform float uEmissiveGain;
|
|
320
339
|
uniform float uNightFactor;
|
|
321
340
|
#if NIGHT_EMISSIVE
|
|
@@ -345,22 +364,47 @@ uniform float uNightEmissive;
|
|
|
345
364
|
uniform float uOpacity;
|
|
346
365
|
|
|
347
366
|
/**
|
|
348
|
-
*
|
|
349
|
-
*
|
|
367
|
+
* Two per-draw switches on how a fragment is *written* rather than what colour it is, sharing one
|
|
368
|
+
* row: on WebGL2 every scalar uniform takes a row of its own, and the eight-light rung stands at 255
|
|
369
|
+
* of the 256 an Adreno 740 offers (\`uniformVectorBudget.test.ts\`), so a second scalar would have
|
|
370
|
+
* spent the last row and the next uniform anybody added would drop that part to four lights.
|
|
371
|
+
*
|
|
372
|
+
* **\`.x\` — whether this draw is accumulating into the order-independent transparency buffers**,
|
|
373
|
+
* and therefore weights its own output. 0 is every draw that is not, which is every draw on every
|
|
350
374
|
* published scene.
|
|
351
375
|
*
|
|
352
|
-
*
|
|
376
|
+
* A uniform rather than a permutation, and the cost is why. Writing the accumulation and the
|
|
353
377
|
* revealage from one pass needs a second fragment output, which is another \`flatFrag\` axis —
|
|
354
378
|
* measured in \`scripts/wgsl.ts\` at about 247 KB gzipped, doubling the sixteen fragment variants,
|
|
355
379
|
* and paid by every consumer whether or not they ever switch this on. A branch on a uniform costs
|
|
356
380
|
* a compare, and the geometry is submitted twice instead. See \`orderIndependent.ts\`.
|
|
357
381
|
*
|
|
358
|
-
*
|
|
382
|
+
* The revealage pass does not read it. It draws with it at 0 and a blend of
|
|
359
383
|
* \`(ZERO, ONE_MINUS_SRC_ALPHA)\`, so the colour it computes is multiplied away and only its alpha
|
|
360
384
|
* reaches the target — which is exactly the point: both passes run the same code to decide what
|
|
361
385
|
* alpha this fragment has, so they cannot disagree about it.
|
|
386
|
+
*
|
|
387
|
+
* **\`.y\` — how much of this draw survives a screen-door dither**, for a crossfade between two
|
|
388
|
+
* levels of detail of one thing. 0 is off, and every draw that never asked for it. Positive keeps
|
|
389
|
+
* that share of an 8×8 Bayer pattern's cells; negative keeps the complement — the cells a draw at
|
|
390
|
+
* the positive amount leaves out — so two draws at \`t\` and \`-t\` cover every pixel exactly once,
|
|
391
|
+
* whatever \`t\` is. Keyed on the framebuffer's own pixel, whose y runs the other way on WebGPU, so
|
|
392
|
+
* the grain is mirrored between backends and nothing else about it differs. What it gives up: a
|
|
393
|
+
* crossfade reads as grain for as long as it lasts, which is why it should be short; and a blended
|
|
394
|
+
* draw that asks for it is dithered too, since the test is a discard.
|
|
362
395
|
*/
|
|
363
|
-
uniform
|
|
396
|
+
uniform vec2 uWriteMode;
|
|
397
|
+
|
|
398
|
+
/** Whether this fragment is one \`uWriteMode.y\` keeps. Integer arithmetic, per the device rule. */
|
|
399
|
+
bool ditherKeeps(float amount) {
|
|
400
|
+
uvec2 p = uvec2(gl_FragCoord.xy) & 7u;
|
|
401
|
+
uint a = p.x ^ p.y;
|
|
402
|
+
uint b = p.y;
|
|
403
|
+
uint cell = ((a & 1u) << 5) | ((b & 1u) << 4) | ((a & 2u) << 2) | ((b & 2u) << 1) |
|
|
404
|
+
((a & 4u) >> 1) | ((b & 4u) >> 2);
|
|
405
|
+
float threshold = (float(cell) + 0.5) / 64.0;
|
|
406
|
+
return amount > 0.0 ? threshold < amount : threshold >= -amount;
|
|
407
|
+
}
|
|
364
408
|
|
|
365
409
|
/**
|
|
366
410
|
* Whether the fragment stage runs any lighting: ambient, the sun, point lights, specular,
|
|
@@ -384,10 +428,11 @@ uniform float uOitWeighted;
|
|
|
384
428
|
*/
|
|
385
429
|
uniform int uLightingEnabled;
|
|
386
430
|
/**
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
431
|
+
* How this draw meets the medium: the atmospheric haze and the underwater tint both, folded
|
|
432
|
+
* together because both are the *camera's* medium rather than a property of the surface — see
|
|
433
|
+
* \`mediumFog\`/\`mediumColor\` in \`fog.ts\`. 1 recedes into it, matching every draw before this
|
|
434
|
+
* existed; 2 fades in it without taking its colour, which is what an additive draw is given; 0
|
|
435
|
+
* keeps out of it. The rule is \`fogModeOf\` in \`backend/drawFog.ts\`.
|
|
391
436
|
*
|
|
392
437
|
* **Independent of \`uLightingEnabled\`, and that is the point of it being a second uniform
|
|
393
438
|
* rather than the same one.** "Unlit but still fading into the haze" is a real thing to
|
|
@@ -737,15 +782,15 @@ uniform vec3 uLightIesAxis[MAX_LIGHTS];
|
|
|
737
782
|
*/
|
|
738
783
|
uniform float uLightCookie[MAX_LIGHTS];
|
|
739
784
|
/**
|
|
740
|
-
* Photometric profiles: one row per fixture, a fixture's intensity by vertical angle across it
|
|
785
|
+
* Photometric profiles: one row per fixture, a fixture's intensity by vertical angle across it, in
|
|
786
|
+
* the bottom band of the fixture atlas below.
|
|
741
787
|
*
|
|
742
788
|
* **Every row spans the same 0 to 180 degree arc**, whatever the file measured, which is what lets
|
|
743
789
|
* a light carry only a row index — see \`iesProfile.ts\`. With no profile loaded this holds a
|
|
744
790
|
* single row of ones, the multiplicative identity, because a declared sampler needs a complete
|
|
745
791
|
* texture whether or not the branch reads it and a row of zeros would switch off every light.
|
|
746
792
|
*/
|
|
747
|
-
|
|
748
|
-
/** Rows in the atlas, so a row index becomes a coordinate. At least 1. */
|
|
793
|
+
/** Rows of profile in the fixture atlas, below its cookies. At least 1. */
|
|
749
794
|
uniform float uIesAtlasRows;
|
|
750
795
|
/**
|
|
751
796
|
* Horizontal planes each profile occupies, so a row is \`profile * planes + plane\`.
|
|
@@ -765,8 +810,14 @@ uniform float uIesPlaneCount;
|
|
|
765
810
|
* identity, because a declared sampler needs a complete texture whether or not the branch reads it
|
|
766
811
|
* and a black stand-in would switch off every light that reached it.
|
|
767
812
|
*/
|
|
768
|
-
uniform sampler2D uCookieAtlas;
|
|
769
813
|
uniform float uCookieTiles;
|
|
814
|
+
/**
|
|
815
|
+
* Both tables in one texture: the cookies' row of tiles in the top band, the profiles' rows below.
|
|
816
|
+
* One sampler for the two since 2026-09-30, because the lit stage bound fifteen of the sixteen
|
|
817
|
+
* WebGL2 guarantees; see \`fixtureAtlas.ts\`. The bands are found from \`textureSize\` and
|
|
818
|
+
* \`uIesAtlasRows\`, so the fold cost no uniform.
|
|
819
|
+
*/
|
|
820
|
+
uniform sampler2D uFixtureAtlas;
|
|
770
821
|
/**
|
|
771
822
|
* Half a texel of a tile, so a cookie's edge cannot reach its neighbour in the atlas.
|
|
772
823
|
*
|
|
@@ -775,6 +826,10 @@ uniform float uCookieTiles;
|
|
|
775
826
|
* cookie. Derived from \`COOKIE_TILE\` rather than written twice.
|
|
776
827
|
*/
|
|
777
828
|
#define COOKIE_INSET (0.5 / ${COOKIE_TILE}.0)
|
|
829
|
+
/** Texels across a profile's row, where a vertical angle of 0 to 180 degrees is laid. */
|
|
830
|
+
#define IES_WIDTH ${IES_ATLAS_WIDTH}.0
|
|
831
|
+
/** Texels a cookie's tile spans, on both axes. */
|
|
832
|
+
#define COOKIE_SPAN ${COOKIE_TILE}.0
|
|
778
833
|
/**
|
|
779
834
|
* Rectangular area lights, and how many of them are live.
|
|
780
835
|
*
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface effects in the lit shader: what a layer does beyond its picture, read from the albedo
|
|
3
|
+
* array's effects table at the layer a vertex carries (`surfaceEffects.ts` has the layout).
|
|
4
|
+
*
|
|
5
|
+
* **Nothing here changes a surface that asks for nothing.** With no table the fetch never runs and
|
|
6
|
+
* every value is zero; each effect is then `mix(x, y, 0)` or a multiply by exactly 1, so a scene
|
|
7
|
+
* that never names an effect draws bit for bit what it drew before. Rain is the one effect that
|
|
8
|
+
* needs no table — it is the scene's (`Environment.wetness`) — and at zero it is the same no-op.
|
|
9
|
+
*
|
|
10
|
+
* **Windows and rooms.** A layer's windows are a grid of cells over one repeat. Each window lights
|
|
11
|
+
* by a hash of its cell against the scene's lit share, quantised to tenths, so a rising share lights
|
|
12
|
+
* more windows rather than brightening the lit ones; a share of them stays lit in the small hours.
|
|
13
|
+
* A room behind a window is **interior mapping from one image**: a one-point perspective picture of
|
|
14
|
+
* a room, back wall in its centre half, is sampled where the view ray meets the room's box. A point
|
|
15
|
+
* on any wall at depth s maps into that picture at `0.5 + (xy - 0.5) / (1 + s)` — the back wall at
|
|
16
|
+
* s = 1 lands in the centre square, and a side wall runs from the picture's edge in to it — so one
|
|
17
|
+
* formula serves every wall and the picture carries its own shading.
|
|
18
|
+
*
|
|
19
|
+
* **Wear** is procedural: dust settling on what faces up, grime rising from the ground and in
|
|
20
|
+
* blotches, streaks running down walls, all fading out with distance where a table says to.
|
|
21
|
+
*
|
|
22
|
+
* Every sample inside these branches is `textureGrad` with gradients taken outside them, or
|
|
23
|
+
* `texelFetch` (the 2026-08-07 rule). Hashes are integer arithmetic (the 2026-09-20 rule).
|
|
24
|
+
*
|
|
25
|
+
* What it gives up: the room picture is one per layer, so every room of a facade is the same room
|
|
26
|
+
* lit or dark; and a room's box assumes a window as wide as it is tall, so a very tall cell
|
|
27
|
+
* stretches its room.
|
|
28
|
+
*/
|
|
29
|
+
export const SURFACE_EFFECTS_GLSL = /* glsl */ `
|
|
30
|
+
/*
|
|
31
|
+
* The layer's six rows, and what every effect reads beside them, filled once at the top of main.
|
|
32
|
+
* Globals rather than arguments, because naga passes every argument through a pointer and a copy,
|
|
33
|
+
* and in sixteen permutations that traffic was most of what the effects cost in bytes.
|
|
34
|
+
*/
|
|
35
|
+
vec4 fx0;
|
|
36
|
+
vec4 fx1;
|
|
37
|
+
vec4 fx2;
|
|
38
|
+
vec4 fx3;
|
|
39
|
+
vec4 fx4;
|
|
40
|
+
vec4 fx5;
|
|
41
|
+
vec3 fxNormal;
|
|
42
|
+
float fxDistance;
|
|
43
|
+
|
|
44
|
+
/** Fill the rows for this layer, or leave them zero where the array carries no table. */
|
|
45
|
+
void fxLoad(int layer) {
|
|
46
|
+
fx0 = vec4(0.0);
|
|
47
|
+
fx1 = vec4(0.0);
|
|
48
|
+
fx2 = vec4(0.0);
|
|
49
|
+
fx3 = vec4(0.0);
|
|
50
|
+
fx4 = vec4(0.0);
|
|
51
|
+
fx5 = vec4(0.0);
|
|
52
|
+
ivec2 size = textureSize(uSurfaceEffects, 0);
|
|
53
|
+
if (size.x < 6 || layer < 0 || layer >= size.y) return;
|
|
54
|
+
fx0 = texelFetch(uSurfaceEffects, ivec2(0, layer), 0);
|
|
55
|
+
fx1 = texelFetch(uSurfaceEffects, ivec2(1, layer), 0);
|
|
56
|
+
fx2 = texelFetch(uSurfaceEffects, ivec2(2, layer), 0);
|
|
57
|
+
fx3 = texelFetch(uSurfaceEffects, ivec2(3, layer), 0);
|
|
58
|
+
fx4 = texelFetch(uSurfaceEffects, ivec2(4, layer), 0);
|
|
59
|
+
fx5 = texelFetch(uSurfaceEffects, ivec2(5, layer), 0);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A cell and a seed to 0..1, the same on every device. */
|
|
63
|
+
float fxHash(vec2 cell, float seed) {
|
|
64
|
+
uvec2 q = uvec2(ivec2(cell)) + uvec2(uint(seed) * 7919u, uint(seed) * 104729u);
|
|
65
|
+
uint h = (q.x * 1597334677u) ^ (q.y * 3812015801u);
|
|
66
|
+
h = (h ^ (h >> 16)) * 2246822519u;
|
|
67
|
+
h ^= h >> 13;
|
|
68
|
+
return float(h & 16777215u) / 16777216.0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
float fxNoise(vec2 p) {
|
|
72
|
+
vec2 i = floor(p);
|
|
73
|
+
vec2 u = smoothstep(0.0, 1.0, fract(p));
|
|
74
|
+
return mix(
|
|
75
|
+
mix(fxHash(i, 0.0), fxHash(i + vec2(1.0, 0.0), 0.0), u.x),
|
|
76
|
+
mix(fxHash(i + vec2(0.0, 1.0), 0.0), fxHash(i + vec2(1.0, 1.0), 0.0), u.x),
|
|
77
|
+
u.y);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Scroll and flipbook, on the caller's clock. \`fract\` keeps a long clock from eating precision. */
|
|
81
|
+
vec3 fxAnimate(vec3 at) {
|
|
82
|
+
float time = uSurfaceScene.x;
|
|
83
|
+
at.xy += fract(fx4.xy * time);
|
|
84
|
+
if (fx5.y > 0.0) at.z += mod(floor(time * fx5.z), fx5.y);
|
|
85
|
+
return at;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Pulse, flicker and fade with distance, as a factor on the emission. */
|
|
89
|
+
float fxEmission(float layer) {
|
|
90
|
+
float time = uSurfaceScene.x;
|
|
91
|
+
float e = 1.0 - fx4.w * (0.5 + 0.5 * sin(6.2831853 * fract(fx4.z * time)));
|
|
92
|
+
e *= 1.0 - fx5.x * step(0.7, fxHash(vec2(floor(time * 14.0), layer), 3.0));
|
|
93
|
+
if (fx5.w > 0.0) e *= 1.0 - smoothstep(fx5.w * 0.7, fx5.w, fxDistance);
|
|
94
|
+
return e;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Dust, grime and streaks, then the scene's rain: the colour in xyz, the roughness in w. */
|
|
98
|
+
vec4 fxWeather(vec3 world, vec3 albedo, float roughness) {
|
|
99
|
+
float fade = fx3.w > 0.0 ? 1.0 - smoothstep(fx3.w * 0.5, fx3.w, fxDistance) : 1.0;
|
|
100
|
+
if (fx3.x + fx3.y + fx3.z > 0.0) {
|
|
101
|
+
float blotch = fxNoise(world.xz * 0.35 + world.y * 0.2);
|
|
102
|
+
float dust = fx3.x * smoothstep(0.35, 0.9, fxNormal.y) * (0.5 + 0.5 * blotch) * fade;
|
|
103
|
+
float grime = fx3.y * (0.55 - 0.55 * smoothstep(0.0, 2.5, world.y) + 0.45 * blotch) * fade;
|
|
104
|
+
float streak = fx3.z * (1.0 - abs(fxNormal.y))
|
|
105
|
+
* fxNoise(vec2((world.x + world.z) * 2.5, world.y * 0.15)) * fade;
|
|
106
|
+
albedo = mix(albedo, vec3(0.56, 0.53, 0.48), dust * 0.6) * (1.0 - 0.5 * grime - 0.35 * streak);
|
|
107
|
+
roughness = mix(roughness, 1.0, clamp(0.5 * (dust + grime), 0.0, 1.0));
|
|
108
|
+
}
|
|
109
|
+
float wet = uSurfaceScene.y * (1.0 - fx2.w) * mix(0.35, 1.0, smoothstep(0.2, 0.8, fxNormal.y));
|
|
110
|
+
return vec4(albedo * (1.0 - 0.45 * wet), mix(roughness, 0.08, wet));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Windows and the rooms behind them: the glow a lit window adds to the emission in xyz, and in w
|
|
115
|
+
* how much of the surface is glass, which the caller mixes toward the room's dim colour. The room
|
|
116
|
+
* itself is returned through \`fxRoom\`. \`mask\` is the albedo's alpha, and a window is a hole in the
|
|
117
|
+
* wall's picture: glass where it is 0. The other way round would lose the wall's colour, which does
|
|
118
|
+
* not survive an upload at alpha 0.
|
|
119
|
+
*
|
|
120
|
+
* **A building's own light, where its vertices name one.** The table is per layer, and every
|
|
121
|
+
* facade of one style shares a layer, so a light that differs from building to building — its
|
|
122
|
+
* colour and its strength — cannot live there. A window layer's vertex emissive is otherwise unused
|
|
123
|
+
* (its windows glow here, not through the emissive term), so the vertex's emissive colour, strength
|
|
124
|
+
* folded in, is that light: the window's colour where there is no room, a tint on the room where
|
|
125
|
+
* there is. An absent colour reads negative and leaves both exactly as the table says.
|
|
126
|
+
*/
|
|
127
|
+
vec3 fxRoom;
|
|
128
|
+
vec4 fxWindows(vec3 at, vec2 dx, vec2 dy, float mask) {
|
|
129
|
+
float glass = fx0.z > 0.0 ? 1.0 - mask : 1.0;
|
|
130
|
+
vec2 g = at.xy * fx0.xy;
|
|
131
|
+
vec2 local = fract(g);
|
|
132
|
+
float share = floor(max(uSurfaceScene.z, uSurfaceScene.w) * 10.0 + 0.5) / 10.0;
|
|
133
|
+
float lit = fxHash(floor(g), fx0.w) < share * (fx1.x > 0.0 ? fx1.z : 1.0) ? 1.0 : 0.0;
|
|
134
|
+
fxRoom = fx2.rgb;
|
|
135
|
+
bool own = vEmissiveColor.r >= 0.0;
|
|
136
|
+
vec3 glow = own ? vEmissiveColor : fx2.rgb;
|
|
137
|
+
float tangentLength = length(vTangent.xyz);
|
|
138
|
+
if (fx1.x > 0.0 && tangentLength > 1e-4) {
|
|
139
|
+
vec3 t = vTangent.xyz / tangentLength;
|
|
140
|
+
vec3 v = normalize(vWorldPos - uCameraPos);
|
|
141
|
+
vec3 d = vec3(dot(v, t), dot(v, cross(fxNormal, t)) * sign(vTangent.w + 0.5),
|
|
142
|
+
min(dot(v, fxNormal), -1e-3) / max(fx1.y, 0.05));
|
|
143
|
+
vec2 across = (step(0.0, d.xy) - local) / (sign(d.xy + 1e-9) * max(abs(d.xy), 1e-5));
|
|
144
|
+
float travel = min(min(across.x, across.y), -1.0 / d.z);
|
|
145
|
+
float depth = min(-d.z * travel, 1.0);
|
|
146
|
+
vec2 picture = 0.5 + (local + d.xy * travel - 0.5) / (1.0 + depth);
|
|
147
|
+
fxRoom = textureGrad(uAlbedo, vec3(picture, fx1.x - 1.0), dx * fx0.xy * 0.5, dy * fx0.xy * 0.5).rgb;
|
|
148
|
+
glow = own ? fxRoom * vEmissiveColor : fxRoom;
|
|
149
|
+
}
|
|
150
|
+
return vec4(glow * (lit * fx1.w * glass), glass);
|
|
151
|
+
}
|
|
152
|
+
`;
|