@tinypoly/gpu-renderer 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/ARCHITECTURE.md +81 -10
  2. package/README.md +53 -5
  3. package/dist/atmosphere.d.ts +21 -0
  4. package/dist/atmosphere.d.ts.map +1 -0
  5. package/dist/atmosphere.js +72 -0
  6. package/dist/atmosphere.js.map +1 -0
  7. package/dist/bvh.d.ts +11 -6
  8. package/dist/bvh.d.ts.map +1 -1
  9. package/dist/bvh.js +64 -11
  10. package/dist/bvh.js.map +1 -1
  11. package/dist/denoise.d.ts +17 -2
  12. package/dist/denoise.d.ts.map +1 -1
  13. package/dist/denoise.js +95 -27
  14. package/dist/denoise.js.map +1 -1
  15. package/dist/dispatchScheduler.d.ts +11 -4
  16. package/dist/dispatchScheduler.d.ts.map +1 -1
  17. package/dist/dispatchScheduler.js +14 -7
  18. package/dist/dispatchScheduler.js.map +1 -1
  19. package/dist/frameLayout.d.ts.map +1 -1
  20. package/dist/frameLayout.js +18 -0
  21. package/dist/frameLayout.js.map +1 -1
  22. package/dist/geometry.d.ts.map +1 -1
  23. package/dist/geometry.js +6 -3
  24. package/dist/geometry.js.map +1 -1
  25. package/dist/index.d.ts +4 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +3 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/lensPass.d.ts +60 -0
  30. package/dist/lensPass.d.ts.map +1 -0
  31. package/dist/lensPass.js +235 -0
  32. package/dist/lensPass.js.map +1 -0
  33. package/dist/lightTree.d.ts +43 -0
  34. package/dist/lightTree.d.ts.map +1 -0
  35. package/dist/lightTree.js +107 -0
  36. package/dist/lightTree.js.map +1 -0
  37. package/dist/lights.d.ts +19 -1
  38. package/dist/lights.d.ts.map +1 -1
  39. package/dist/lights.js +63 -1
  40. package/dist/lights.js.map +1 -1
  41. package/dist/materialLayout.d.ts +24 -0
  42. package/dist/materialLayout.d.ts.map +1 -1
  43. package/dist/materialLayout.js +36 -1
  44. package/dist/materialLayout.js.map +1 -1
  45. package/dist/pathTracePass.d.ts +6 -3
  46. package/dist/pathTracePass.d.ts.map +1 -1
  47. package/dist/pathTracePass.js +16 -10
  48. package/dist/pathTracePass.js.map +1 -1
  49. package/dist/renderer.d.ts +19 -7
  50. package/dist/renderer.d.ts.map +1 -1
  51. package/dist/renderer.js +198 -36
  52. package/dist/renderer.js.map +1 -1
  53. package/dist/sceneUpload.d.ts +19 -1
  54. package/dist/sceneUpload.d.ts.map +1 -1
  55. package/dist/sceneUpload.js +25 -3
  56. package/dist/sceneUpload.js.map +1 -1
  57. package/dist/settings.d.ts +136 -0
  58. package/dist/settings.d.ts.map +1 -0
  59. package/dist/settings.js +100 -0
  60. package/dist/settings.js.map +1 -0
  61. package/dist/subsurface.d.ts +76 -0
  62. package/dist/subsurface.d.ts.map +1 -0
  63. package/dist/subsurface.js +70 -0
  64. package/dist/subsurface.js.map +1 -0
  65. package/dist/texturePack.d.ts +16 -4
  66. package/dist/texturePack.d.ts.map +1 -1
  67. package/dist/texturePack.js +61 -8
  68. package/dist/texturePack.js.map +1 -1
  69. package/dist/volumeContainment.d.ts +17 -0
  70. package/dist/volumeContainment.d.ts.map +1 -0
  71. package/dist/volumeContainment.js +61 -0
  72. package/dist/volumeContainment.js.map +1 -0
  73. package/dist/wgsl/bsdf.wgsl.d.ts +1 -1
  74. package/dist/wgsl/bsdf.wgsl.d.ts.map +1 -1
  75. package/dist/wgsl/bsdf.wgsl.js +35 -3
  76. package/dist/wgsl/bsdf.wgsl.js.map +1 -1
  77. package/dist/wgsl/bvh.wgsl.d.ts +8 -2
  78. package/dist/wgsl/bvh.wgsl.d.ts.map +1 -1
  79. package/dist/wgsl/bvh.wgsl.js +167 -96
  80. package/dist/wgsl/bvh.wgsl.js.map +1 -1
  81. package/dist/wgsl/common.wgsl.d.ts.map +1 -1
  82. package/dist/wgsl/common.wgsl.js +24 -7
  83. package/dist/wgsl/common.wgsl.js.map +1 -1
  84. package/dist/wgsl/lens.wgsl.d.ts +17 -0
  85. package/dist/wgsl/lens.wgsl.d.ts.map +1 -0
  86. package/dist/wgsl/lens.wgsl.js +315 -0
  87. package/dist/wgsl/lens.wgsl.js.map +1 -0
  88. package/dist/wgsl/lights.wgsl.d.ts +1 -1
  89. package/dist/wgsl/lights.wgsl.d.ts.map +1 -1
  90. package/dist/wgsl/lights.wgsl.js +206 -33
  91. package/dist/wgsl/lights.wgsl.js.map +1 -1
  92. package/dist/wgsl/pathTrace.wgsl.d.ts +1 -1
  93. package/dist/wgsl/pathTrace.wgsl.d.ts.map +1 -1
  94. package/dist/wgsl/pathTrace.wgsl.js +410 -25
  95. package/dist/wgsl/pathTrace.wgsl.js.map +1 -1
  96. package/dist/wgsl/random.wgsl.d.ts +1 -1
  97. package/dist/wgsl/random.wgsl.d.ts.map +1 -1
  98. package/dist/wgsl/random.wgsl.js +9 -17
  99. package/dist/wgsl/random.wgsl.js.map +1 -1
  100. package/dist/wgsl/sampling.wgsl.d.ts +1 -1
  101. package/dist/wgsl/sampling.wgsl.d.ts.map +1 -1
  102. package/dist/wgsl/sampling.wgsl.js +25 -0
  103. package/dist/wgsl/sampling.wgsl.js.map +1 -1
  104. package/dist/wgsl/surface.wgsl.d.ts +1 -1
  105. package/dist/wgsl/surface.wgsl.d.ts.map +1 -1
  106. package/dist/wgsl/surface.wgsl.js +47 -4
  107. package/dist/wgsl/surface.wgsl.js.map +1 -1
  108. package/dist/wgsl/texture.wgsl.d.ts +1 -1
  109. package/dist/wgsl/texture.wgsl.d.ts.map +1 -1
  110. package/dist/wgsl/texture.wgsl.js +10 -11
  111. package/dist/wgsl/texture.wgsl.js.map +1 -1
  112. package/package.json +1 -1
  113. package/src/atmosphere.ts +95 -0
  114. package/src/bvh.ts +81 -12
  115. package/src/denoise.ts +110 -27
  116. package/src/dispatchScheduler.ts +16 -7
  117. package/src/frameLayout.ts +18 -0
  118. package/src/geometry.ts +6 -2
  119. package/src/index.ts +13 -0
  120. package/src/lensPass.ts +299 -0
  121. package/src/lightTree.ts +172 -0
  122. package/src/lights.ts +90 -2
  123. package/src/materialLayout.ts +52 -1
  124. package/src/pathTracePass.ts +19 -10
  125. package/src/renderer.ts +258 -43
  126. package/src/sceneUpload.ts +41 -4
  127. package/src/settings.ts +234 -0
  128. package/src/subsurface.ts +153 -0
  129. package/src/texturePack.ts +80 -13
  130. package/src/volumeContainment.ts +81 -0
  131. package/src/wgsl/bsdf.wgsl.ts +35 -3
  132. package/src/wgsl/bvh.wgsl.ts +167 -96
  133. package/src/wgsl/common.wgsl.ts +24 -7
  134. package/src/wgsl/lens.wgsl.ts +314 -0
  135. package/src/wgsl/lights.wgsl.ts +206 -33
  136. package/src/wgsl/pathTrace.wgsl.ts +410 -25
  137. package/src/wgsl/random.wgsl.ts +9 -17
  138. package/src/wgsl/sampling.wgsl.ts +25 -0
  139. package/src/wgsl/surface.wgsl.ts +47 -4
  140. package/src/wgsl/texture.wgsl.ts +10 -11
@@ -0,0 +1,76 @@
1
+ import type { SerializedMaterial } from "./upstream.js";
2
+ /**
3
+ * How a material scatters light inside itself, from `userData.cpuRenderer.subsurface`. `true` takes every default.
4
+ *
5
+ * The material's own colour (with its map) is what the medium reflects in all once light has wandered through it, as
6
+ * in Blender's Principled BSDF, so a textured skin keeps its texture; `radius` says how far each colour gets before it
7
+ * scatters, which is what makes red bleed through an ear and blue stay at the surface.
8
+ */
9
+ export type SubsurfaceInput = boolean | {
10
+ /** Share of the diffuse layer that scatters inside rather than at the surface, `0` to `1`. Defaults to `1`. */
11
+ weight?: number;
12
+ /**
13
+ * Mean free path of red, green and blue, as multiples of `scale`; one number for all three. Defaults to
14
+ * `[1, 0.2, 0.1]`, Blender's, which suits skin.
15
+ */
16
+ radius?: number | readonly number[];
17
+ /** World units `radius` is measured in. Defaults to `0.05`: five centimetres in a scene modelled in metres. */
18
+ scale?: number;
19
+ /** Henyey-Greenstein anisotropy of each scattering, `-1` (back) to `1` (forward). Defaults to `0`. */
20
+ anisotropy?: number;
21
+ };
22
+ /** A medium resolved to what the kernel reads. */
23
+ export interface SubsurfaceMedium {
24
+ weight: number;
25
+ /** Mean free path per channel, in world units. */
26
+ radius: [number, number, number];
27
+ anisotropy: number;
28
+ }
29
+ /** A snapshot material, with the medium `serializeLive` attaches; a snapshot built elsewhere can carry one too. */
30
+ export type SubsurfaceMaterial = SerializedMaterial & {
31
+ subsurface?: SubsurfaceInput;
32
+ };
33
+ /** The medium an input describes, with its defaults, or `null` when it describes none. */
34
+ export declare function subsurfaceMedium(input: unknown): SubsurfaceMedium | null;
35
+ /** The medium a snapshot material carries, if any. */
36
+ export declare function subsurfaceOf(material: SerializedMaterial | undefined): SubsurfaceMedium | null;
37
+ /**
38
+ * A mesh filled with a medium, from `userData.cpuRenderer.volume`: its surface is only the boundary, invisible, and
39
+ * light scatters and fades in the space it encloses. The mesh has to be closed, with its normals pointing out.
40
+ */
41
+ export type VolumeInput = {
42
+ /** How much of the light the medium takes per scene unit: one over the mean distance a ray gets. Defaults to `1`. */
43
+ density?: number;
44
+ /** What it keeps at each scattering, `#rrggbb` or linear rgb: white for fog and clouds, dark for soot. */
45
+ color?: string | readonly number[];
46
+ /** Henyey-Greenstein anisotropy, `-1` (back) to `1` (forward). Defaults to `0`; haze and dust are forward, ~0.7. */
47
+ anisotropy?: number;
48
+ };
49
+ export interface VolumeMedium {
50
+ density: number;
51
+ /** Single scattering albedo, linear rgb. */
52
+ albedo: [number, number, number];
53
+ anisotropy: number;
54
+ }
55
+ /** A snapshot material that may carry a volume, as `serializeLive` attaches it. */
56
+ export type VolumeMaterial = SerializedMaterial & {
57
+ volume?: VolumeInput;
58
+ };
59
+ /** The medium an input describes, with its defaults, or `null` when it describes none. */
60
+ export declare function volumeMedium(input: unknown): VolumeMedium | null;
61
+ /**
62
+ * Rounded edges at shading time, from `userData.cpuRenderer.bevel`: the radius, in scene units, over which the
63
+ * shading normal turns from one face to the next, as Cycles' bevel node rounds them. A number, or `{ radius }`.
64
+ */
65
+ export type BevelInput = number | {
66
+ radius?: number;
67
+ };
68
+ /** A snapshot material that may carry a bevel, as `serializeLive` attaches it. */
69
+ export type BevelMaterial = SerializedMaterial & {
70
+ bevel?: BevelInput;
71
+ };
72
+ /** The bevel radius an input asks for, or zero. */
73
+ export declare function bevelRadius(input: unknown): number;
74
+ export declare function bevelOf(material: SerializedMaterial | undefined): number;
75
+ export declare function volumeOf(material: SerializedMaterial | undefined): VolumeMedium | null;
76
+ //# sourceMappingURL=subsurface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subsurface.d.ts","sourceRoot":"","sources":["../src/subsurface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAExD;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG;IACtC,+GAA+G;IAC/G,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACpC,+GAA+G;IAC/G,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sGAAsG;IACtG,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,kDAAkD;AAClD,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,kDAAkD;IAClD,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,mHAAmH;AACnH,MAAM,MAAM,kBAAkB,GAAG,kBAAkB,GAAG;IAAE,UAAU,CAAC,EAAE,eAAe,CAAA;CAAE,CAAC;AAQvF,0FAA0F;AAC1F,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,gBAAgB,GAAG,IAAI,CAyBxE;AAED,sDAAsD;AACtD,wBAAgB,YAAY,CAAC,QAAQ,EAAE,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Number.isFinite(value) ? value : fallback;
4
+ /** The medium an input describes, with its defaults, or `null` when it describes none. */
5
+ export function subsurfaceMedium(input) {
6
+ if (input === true)
7
+ input = {};
8
+ if (!input || typeof input !== "object")
9
+ return null;
10
+ const value = input;
11
+ const weight = Math.min(1, Math.max(0, finite(value.weight, 1)));
12
+ if (weight <= 0)
13
+ return null;
14
+ const scale = Math.max(0, finite(value.scale, DEFAULT_SCALE));
15
+ const radius = typeof value.radius === "number"
16
+ ? [value.radius, value.radius, value.radius]
17
+ : Array.isArray(value.radius) && value.radius.length >= 3
18
+ ? value.radius.slice(0, 3)
19
+ : DEFAULT_RADIUS;
20
+ // A path that never travels would divide by zero; a hair of distance keeps it a very dense medium instead.
21
+ const paths = radius.map((channel, k) => Math.max(1e-5, finite(channel, DEFAULT_RADIUS[k]) * scale));
22
+ return {
23
+ weight,
24
+ radius: [paths[0], paths[1], paths[2]],
25
+ anisotropy: Math.min(0.95, Math.max(-0.95, finite(value.anisotropy, 0))),
26
+ };
27
+ }
28
+ /** The medium a snapshot material carries, if any. */
29
+ export function subsurfaceOf(material) {
30
+ return subsurfaceMedium(material?.subsurface);
31
+ }
32
+ const srgbToLinear = (c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
33
+ function albedoOf(color) {
34
+ if (Array.isArray(color) && color.length >= 3) {
35
+ return [0, 1, 2].map(k => Math.min(1, Math.max(0, finite(color[k], 1))));
36
+ }
37
+ const match = typeof color === "string" ? /^#?([0-9a-f]{6})$/i.exec(color) : null;
38
+ if (!match)
39
+ return [1, 1, 1];
40
+ const value = parseInt(match[1], 16);
41
+ return [(value >> 16) & 255, (value >> 8) & 255, value & 255].map(c => srgbToLinear(c / 255));
42
+ }
43
+ /** The medium an input describes, with its defaults, or `null` when it describes none. */
44
+ export function volumeMedium(input) {
45
+ if (input === true)
46
+ input = {};
47
+ if (!input || typeof input !== "object")
48
+ return null;
49
+ const value = input;
50
+ const density = Math.max(0, finite(value.density, 1));
51
+ if (density <= 0)
52
+ return null;
53
+ return {
54
+ density,
55
+ albedo: albedoOf(value.color),
56
+ anisotropy: Math.min(0.95, Math.max(-0.95, finite(value.anisotropy, 0))),
57
+ };
58
+ }
59
+ /** The bevel radius an input asks for, or zero. */
60
+ export function bevelRadius(input) {
61
+ const radius = typeof input === "number" ? input : input?.radius;
62
+ return typeof radius === "number" && Number.isFinite(radius) && radius > 0 ? radius : 0;
63
+ }
64
+ export function bevelOf(material) {
65
+ return bevelRadius(material?.bevel);
66
+ }
67
+ export function volumeOf(material) {
68
+ return volumeMedium(material?.volume);
69
+ }
70
+ //# sourceMappingURL=subsurface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subsurface.js","sourceRoot":"","sources":["../src/subsurface.ts"],"names":[],"mappings":"AAkCA,MAAM,cAAc,GAA6B,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAC/D,MAAM,aAAa,GAAG,IAAI,CAAC;AAE3B,MAAM,MAAM,GAAG,CAAC,KAAc,EAAE,QAAgB,EAAE,EAAE,CAClD,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC;AAEzE,0FAA0F;AAC1F,MAAM,UAAU,gBAAgB,CAAC,KAAc;IAC7C,IAAI,KAAK,KAAK,IAAI;QAAE,KAAK,GAAG,EAAE,CAAC;IAC/B,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAErD,MAAM,KAAK,GAAG,KAA0C,CAAC;IACzD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAEjE,IAAI,MAAM,IAAI,CAAC;QAAE,OAAO,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AK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAErD,MAAM,KAAK,GAAG,KAAoB,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;IAEtD,IAAI,OAAO,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAE9B,OAAO;QACL,OAAO;QACP,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC;QAC7B,UAAU,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC;KACzE,CAAC;AACJ,CAAC;AAWD,mDAAmD;AACnD,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,MAAM,MAAM,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,KAAqC,EAAE,MAAM,CAAC;IAElG,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1F,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,QAAwC;IAC9D,OAAO,WAAW,CAAE,QAAsC,EAAE,KAAK,CAAC,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,QAAwC;IAC/D,OAAO,YAAY,CAAE,QAAuC,EAAE,MAAM,CAAC,CAAC;AACxE,CAAC"}
@@ -9,10 +9,10 @@ import type { SerializedScene, SerializedTexture } from "./upstream.js";
9
9
  */
10
10
  export interface PackedTextures {
11
11
  /**
12
- * One buffer for all of it: `TEXTURE_WORDS` words per texture first, as `TextureInfo` in `wgsl/common.wgsl.ts`, then
13
- * the texels each entry points at. A byte texture takes one word per texel, RGBA in the low to high bytes; a float
14
- * texture takes four, each the bits of one linear channel. Keeping the two in one binding is what holds the shader
15
- * inside the eight storage buffers a WebGPU device has to offer.
12
+ * One buffer for all of it: the lookup tables first (see `TEXTURE_TABLE`), then `TEXTURE_WORDS` words per texture, as
13
+ * `TextureInfo` in `wgsl/common.wgsl.ts`, then the texels each entry points at. A byte texture takes one word per
14
+ * texel, RGBA in the low to high bytes; a float texture takes four, each the bits of one linear channel. Keeping them
15
+ * in one binding is what holds the shader inside the eight storage buffers a WebGPU device has to offer.
16
16
  */
17
17
  data: Uint32Array;
18
18
  count: number;
@@ -28,6 +28,17 @@ export interface TextureExtras {
28
28
  }
29
29
  /** Sixteen words per entry: see `TextureInfo` in `wgsl/common.wgsl.ts`. */
30
30
  export declare const TEXTURE_WORDS = 16;
31
+ /**
32
+ * Lookup tables that lead the buffer, at offsets the shader knows as constants: the second Sobol dimension one byte of
33
+ * the index at a time (four tables of 256), then the linear value of each of the 256 sRGB codes. Both replace work the
34
+ * kernel did on every read with the very numbers it computed, so nothing they feed changes.
35
+ */
36
+ export declare const SOBOL_TABLE = 0;
37
+ export declare const SRGB_TABLE: number;
38
+ /** Where the texture table starts, after the lookup tables. */
39
+ export declare const TEXTURE_TABLE: number;
40
+ /** Where a texture's entry starts in the buffer. */
41
+ export declare const textureEntry: (index: number) => number;
31
42
  /** What a material's map index means when the material has no such map. */
32
43
  export declare const NO_TEXTURE = 4294967295;
33
44
  /** Three's own wrap constants, which the shader compares against. */
@@ -42,6 +53,7 @@ export declare const UV_CHANNELS = 2;
42
53
  export interface TexturePlan {
43
54
  textures: SerializedTexture[];
44
55
  indexOf: Map<string, number>;
56
+ /** The lookup tables and the texture table together: where the texels begin. */
45
57
  tableWords: number;
46
58
  /** Where the words appended after the texels begin. */
47
59
  extraOffset: number;
@@ -1 +1 @@
1
- {"version":3,"file":"texturePack.d.ts","sourceRoot":"","sources":["../src/texturePack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AAM,CAAC;CACrB;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,eAAe,EAAE,aAAa,GAAE,SAAS,iBAAiB,EAAO,GAAG,WAAW,CAalH;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,eAAe,EAAE,MAAM,GAAE,aAAkB,GAAG,cAAc,CAE/F;AAED,oFAAoF;AACpF,wBAAgB,aAAa,CAAC,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,GAAG,cAAc,CA+DxF;AAED,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,CAAC"}
@@ -1,5 +1,49 @@
1
1
  /** Sixteen words per entry: see `TextureInfo` in `wgsl/common.wgsl.ts`. */
2
2
  export const TEXTURE_WORDS = 16;
3
+ /**
4
+ * Lookup tables that lead the buffer, at offsets the shader knows as constants: the second Sobol dimension one byte of
5
+ * the index at a time (four tables of 256), then the linear value of each of the 256 sRGB codes. Both replace work the
6
+ * kernel did on every read with the very numbers it computed, so nothing they feed changes.
7
+ */
8
+ export const SOBOL_TABLE = 0;
9
+ export const SRGB_TABLE = SOBOL_TABLE + 4 * 256;
10
+ /** Where the texture table starts, after the lookup tables. */
11
+ export const TEXTURE_TABLE = SRGB_TABLE + 256;
12
+ /** The four byte tables of the Sobol sequence's second dimension, whose generator matrix is Pascal's triangle mod 2. */
13
+ function sobolTables() {
14
+ const directions = [];
15
+ let direction = 0x80000000;
16
+ for (let bit = 0; bit < 32; bit++) {
17
+ directions.push(direction >>> 0);
18
+ direction = (direction ^ (direction >>> 1)) >>> 0;
19
+ }
20
+ const tables = new Uint32Array(4 * 256);
21
+ for (let byte = 0; byte < 4; byte++) {
22
+ for (let value = 0; value < 256; value++) {
23
+ let xor = 0;
24
+ for (let bit = 0; bit < 8; bit++)
25
+ if (value & (1 << bit))
26
+ xor ^= directions[byte * 8 + bit];
27
+ tables[byte * 256 + value] = xor >>> 0;
28
+ }
29
+ }
30
+ return tables;
31
+ }
32
+ /** The linear value of every sRGB code, worked out in double precision and rounded once. */
33
+ function srgbTable() {
34
+ return Float32Array.from({ length: 256 }, (_, code) => {
35
+ const value = Math.fround(code / 255);
36
+ return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
37
+ });
38
+ }
39
+ const LOOKUP = (() => {
40
+ const words = new Uint32Array(TEXTURE_TABLE);
41
+ words.set(sobolTables(), SOBOL_TABLE);
42
+ words.set(new Uint32Array(srgbTable().buffer), SRGB_TABLE);
43
+ return words;
44
+ })();
45
+ /** Where a texture's entry starts in the buffer. */
46
+ export const textureEntry = (index) => TEXTURE_TABLE + index * TEXTURE_WORDS;
3
47
  /** What a material's map index means when the material has no such map. */
4
48
  export const NO_TEXTURE = 0xffffffff;
5
49
  const TEXTURE_FLOAT = 1;
@@ -13,6 +57,8 @@ const REPEAT = 1000;
13
57
  const MIRRORED_REPEAT = 1002;
14
58
  /** How many UV sets the geometry carries, and so the highest channel a texture may ask for. */
15
59
  export const UV_CHANNELS = 2;
60
+ /** Whether a word's first byte is its lowest, as it is on the device: then RGBA bytes are already RGBA words. */
61
+ const LITTLE_ENDIAN = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1;
16
62
  const texelCount = (texture) => Math.max(0, texture.width) * Math.max(0, texture.height);
17
63
  const isFloat = (texture) => texture.data instanceof Float32Array;
18
64
  /**
@@ -27,7 +73,7 @@ export function planTextures(scene, extraTextures = []) {
27
73
  .filter(texture => texelCount(texture) > 0 && texture.data.length >= texelCount(texture) * 4)
28
74
  .filter(texture => !seen.has(texture.id) && !!seen.add(texture.id));
29
75
  const indexOf = new Map(textures.map((texture, index) => [texture.id, index]));
30
- const tableWords = Math.max(1, textures.length) * TEXTURE_WORDS;
76
+ const tableWords = TEXTURE_TABLE + Math.max(1, textures.length) * TEXTURE_WORDS;
31
77
  const texelWords = textures.reduce((sum, texture) => sum + texelCount(texture) * (isFloat(texture) ? 4 : 1), 0);
32
78
  return { textures, indexOf, tableWords, extraOffset: tableWords + texelWords };
33
79
  }
@@ -42,8 +88,9 @@ export function writeTextures(plan, extraWords) {
42
88
  const words = data;
43
89
  const floats = new Float32Array(data.buffer);
44
90
  let texelAt = tableWords;
91
+ data.set(LOOKUP, 0);
45
92
  textures.forEach((texture, index) => {
46
- const at = index * TEXTURE_WORDS;
93
+ const at = textureEntry(index);
47
94
  const texels = texelCount(texture);
48
95
  const float = isFloat(texture);
49
96
  words[at] = texture.width;
@@ -69,12 +116,18 @@ export function writeTextures(plan, extraWords) {
69
116
  }
70
117
  else {
71
118
  const source = texture.data;
72
- for (let texel = 0; texel < texels; texel++) {
73
- const offset = texel * 4;
74
- words[texelAt + texel] = source[offset]
75
- | (source[offset + 1] << 8)
76
- | (source[offset + 2] << 16)
77
- | (source[offset + 3] << 24);
119
+ if (LITTLE_ENDIAN) {
120
+ // The bytes already sit in the order the words want them: copied whole, as the GPU reads them.
121
+ new Uint8Array(data.buffer, texelAt * 4, texels * 4).set(source.subarray(0, texels * 4));
122
+ }
123
+ else {
124
+ for (let texel = 0; texel < texels; texel++) {
125
+ const offset = texel * 4;
126
+ words[texelAt + texel] = source[offset]
127
+ | (source[offset + 1] << 8)
128
+ | (source[offset + 2] << 16)
129
+ | (source[offset + 3] << 24);
130
+ }
78
131
  }
79
132
  texelAt += texels;
80
133
  }
@@ -1 +1 @@
1
- {"version":3,"file":"texturePack.js","sourceRoot":"","sources":["../src/texturePack.ts"],"names":[],"mappings":"AA+BA,2EAA2E;AAC3E,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC,2EAA2E;AAC3E,MAAM,CAAC,MAAM,UAAU,GAAG,UAAU,CAAC;AAErC,MAAM,aAAa,GAAG,CAAC,CAAC;AACxB,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,cAAc,GAAG,CAAC,CAAC;AACzB,mFAAmF;AACnF,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAE9B,qEAAqE;AACrE,MAAM,MAAM,GAAG,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@ -0,0 +1,17 @@
1
+ import type { PackedGeometry } from "./geometry.js";
2
+ /** What the kernel reads as no material at all. */
3
+ export declare const NO_MATERIAL = 4294967295;
4
+ /** The triangles that bound a volume, each with its material's index. */
5
+ export interface VolumeTriangles {
6
+ /** Nine floats a triangle. */
7
+ positions: Float32Array;
8
+ materials: Uint32Array;
9
+ }
10
+ /** The boundaries of every volume in a packed scene, or `null` when it has none. */
11
+ export declare function volumeTriangles(geometry: PackedGeometry): VolumeTriangles | null;
12
+ /**
13
+ * The volume a point stands inside, by the parity of the boundary crossings of a ray from it: an odd count of one
14
+ * volume's triangles means inside it. The camera usually stands in none, so the kernel only asks this of the camera.
15
+ */
16
+ export declare function containingVolume(volumes: VolumeTriangles | null, point: readonly number[]): number;
17
+ //# sourceMappingURL=volumeContainment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"volumeContainment.d.ts","sourceRoot":"","sources":["../src/volumeContainment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAGpD,mDAAmD;AACnD,eAAO,MAAM,WAAW,aAAa,CAAC;AAEtC,yEAAyE;AACzE,MAAM,WAAW,eAAe;IAC9B,8BAA8B;IAC9B,SAAS,EAAE,YAAY,CAAC;IACxB,SAAS,EAAE,WAAW,CAAC;CACxB;AAED,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,QAAQ,EAAE,cAAc,GAAG,eAAe,GAAG,IAAI,CAmBhF;AAKD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAsClG"}
@@ -0,0 +1,61 @@
1
+ import { MATERIAL_VOLUME } from "./materialLayout.js";
2
+ /** What the kernel reads as no material at all. */
3
+ export const NO_MATERIAL = 0xffffffff;
4
+ /** The boundaries of every volume in a packed scene, or `null` when it has none. */
5
+ export function volumeTriangles(geometry) {
6
+ const volume = geometry.materialProperties.map(properties => (properties.flags & MATERIAL_VOLUME) !== 0);
7
+ if (!volume.some(Boolean))
8
+ return null;
9
+ const picked = [];
10
+ for (let triangle = 0; triangle < geometry.triangleCount; triangle++)
11
+ if (volume[geometry.materials[triangle]])
12
+ picked.push(triangle);
13
+ const positions = new Float32Array(picked.length * 9);
14
+ const materials = new Uint32Array(picked.length);
15
+ picked.forEach((triangle, k) => {
16
+ positions.set(geometry.positions.subarray(triangle * 9, triangle * 9 + 9), k * 9);
17
+ materials[k] = geometry.materials[triangle];
18
+ });
19
+ return { positions, materials };
20
+ }
21
+ /** An irrational direction, so a ray from the camera does not run along an edge of an axis aligned box. */
22
+ const PROBE = [0.4123, 0.7071, 0.5745].map((value, _, all) => value / Math.hypot(...all));
23
+ /**
24
+ * The volume a point stands inside, by the parity of the boundary crossings of a ray from it: an odd count of one
25
+ * volume's triangles means inside it. The camera usually stands in none, so the kernel only asks this of the camera.
26
+ */
27
+ export function containingVolume(volumes, point) {
28
+ if (!volumes)
29
+ return NO_MATERIAL;
30
+ const crossings = new Map();
31
+ const p = volumes.positions;
32
+ const [dx, dy, dz] = PROBE;
33
+ for (let k = 0; k < volumes.materials.length; k++) {
34
+ const at = k * 9;
35
+ const e1x = p[at + 3] - p[at], e1y = p[at + 4] - p[at + 1], e1z = p[at + 5] - p[at + 2];
36
+ const e2x = p[at + 6] - p[at], e2y = p[at + 7] - p[at + 1], e2z = p[at + 8] - p[at + 2];
37
+ // Moller and Trumbore.
38
+ const hx = dy * e2z - dz * e2y, hy = dz * e2x - dx * e2z, hz = dx * e2y - dy * e2x;
39
+ const det = e1x * hx + e1y * hy + e1z * hz;
40
+ if (Math.abs(det) < 1e-12)
41
+ continue;
42
+ const inverse = 1 / det;
43
+ const sx = point[0] - p[at], sy = point[1] - p[at + 1], sz = point[2] - p[at + 2];
44
+ const u = (sx * hx + sy * hy + sz * hz) * inverse;
45
+ if (u < 0 || u > 1)
46
+ continue;
47
+ const qx = sy * e1z - sz * e1y, qy = sz * e1x - sx * e1z, qz = sx * e1y - sy * e1x;
48
+ const v = (dx * qx + dy * qy + dz * qz) * inverse;
49
+ if (v < 0 || u + v > 1)
50
+ continue;
51
+ if ((e2x * qx + e2y * qy + e2z * qz) * inverse > 0) {
52
+ const material = volumes.materials[k];
53
+ crossings.set(material, (crossings.get(material) ?? 0) + 1);
54
+ }
55
+ }
56
+ for (const [material, count] of crossings)
57
+ if (count % 2 === 1)
58
+ return material;
59
+ return NO_MATERIAL;
60
+ }
61
+ //# sourceMappingURL=volumeContainment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"volumeContainment.js","sourceRoot":"","sources":["../src/volumeContainment.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,mDAAmD;AACnD,MAAM,CAAC,MAAM,WAAW,GAAG,UAAU,CAAC;AAStC,oFAAoF;AACpF,MAAM,UAAU,eAAe,CAAC,QAAwB;IACtD,MAAM,MAAM,GAAG,QAAQ,CAAC,kBAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,UAAU,CAAC,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC;IAEzG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;QAAE,OAAO,IAAI,CAAC;IAEvC,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,KAAK,IAAI,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC,aAAa,EAAE,QAAQ,EAAE;QAClE,IAAI,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;YAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAElE,MAAM,SAAS,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACtD,MAAM,SAAS,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,MAAM,CA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D,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;YAAE,SAAS;QAE7B,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC;QACnF,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC;QAElD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,SAAS;QAEjC,IAAI,CAAC,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC,GAAG,OAAO,GAAG,CAAC,EAAE,CAAC;YACnD,MAAM,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;YAEtC,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC;IAED,KAAK,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,SAAS;QAAE,IAAI,KAAK,GAAG,CAAC,KAAK,CAAC;YAAE,OAAO,QAAQ,CAAC;IAEhF,OAAO,WAAW,CAAC;AACrB,CAAC"}
@@ -9,5 +9,5 @@
9
9
  * for the diffuse lobe, the visible normal distribution (Heitz 2018) for GGX, anisotropic or not, and the Blinn-Phong
10
10
  * lobe for Phong, mixed by how much each lobe reflects.
11
11
  */
12
- export declare const BSDF_WGSL = "\n/** How a surface answers a direction: what it reflects, and how likely sampling was to have chosen it. */\nstruct BsdfEval {\n // The bidirectional distribution times the cosine at the surface, which is what a contribution is multiplied by.\n value: vec3<f32>,\n // Solid angle density of this direction under this renderer's own sampling, for the power heuristic.\n pdf: f32,\n};\n\nstruct BsdfSample {\n direction: vec3<f32>,\n // The distribution over its own density: what the path's throughput is multiplied by.\n weight: vec3<f32>,\n pdf: f32,\n valid: bool,\n // The diffuse lobe chose the direction, rather than a specular one: which of the denoiser's two signals it feeds.\n diffuse: bool,\n};\n\nfn luminanceOf(color: vec3<f32>) -> f32 {\n return dot(color, vec3<f32>(0.2126, 0.7152, 0.0722));\n}\n\nfn max3(color: vec3<f32>) -> f32 {\n return max(color.r, max(color.g, color.b));\n}\n\n/** Schlick's approximation, as in Three: the reflectance always reaches one at a grazing angle. */\nfn fresnelSchlick(f0: vec3<f32>, cosine: f32) -> vec3<f32> {\n return schlick3(f0, 1.0, cosine);\n}\n\n/** Reflectance at normal incidence: the dielectric's own, turned towards the base colour as the surface turns metal. */\nfn surfaceF0(surface: Surface) -> vec3<f32> {\n return mix(surface.specularColor, surface.albedo, surface.metalness);\n}\n\n/** GGX, with alpha the square of the roughness, as Three parameterises it. */\nfn ggxDistribution(alpha2: f32, nh: f32) -> f32 {\n let denominator = nh * nh * (alpha2 - 1.0) + 1.0;\n\n return alpha2 / (PI * denominator * denominator);\n}\n\n/** Smith's height correlated visibility, which already carries the 1/(4 nl nv) the lobe would otherwise need. */\nfn ggxVisibility(alpha2: f32, nl: f32, nv: f32) -> f32 {\n return 0.5 / (nl * sqrt(nv * nv * (1.0 - alpha2) + alpha2) + nv * sqrt(nl * nl * (1.0 - alpha2) + alpha2) + 1e-7);\n}\n\n/** The separable masking term, which is the one the visible normal sampling is built on. */\nfn ggxMasking(alpha2: f32, nv: f32) -> f32 {\n return 2.0 * nv / (nv + sqrt(alpha2 + (1.0 - alpha2) * nv * nv));\n}\n\n/** Karis' analytic fit of the GGX directional albedo (Three's DFGApprox): the scale and bias applied to F0 and F90. */\nfn dfgApprox(roughness: f32, nv: f32) -> vec2<f32> {\n let r = roughness * vec4<f32>(-1.0, -0.0275, -0.572, 0.022) + vec4<f32>(1.0, 0.0425, 1.04, -0.04);\n let a004 = min(r.x * r.x, exp2(-9.28 * nv)) * r.x + r.y;\n\n return vec2<f32>(-1.04, 1.04) * a004 + r.zw;\n}\n\n/** Three's IBLSheenBRDF: the Charlie sheen lobe integrated over the hemisphere. */\nfn sheenAlbedo(cosine: f32, roughness: f32) -> f32 {\n let r2 = roughness * roughness;\n let inverse = 1.0 / (roughness + 0.1);\n let a = -1.9362 + 1.0678 * roughness + 0.4573 * r2 - 0.8469 * inverse;\n let b = -0.6014 + 0.5538 * roughness - 0.4670 * r2 - 0.1255 * inverse;\n\n return clamp(exp(a * clamp(cosine, 0.0, 1.0) + b), 0.0, 1.0);\n}\n\n/** Three's D_Charlie times V_Neubelt. */\nfn sheenLobe(roughness: f32, nh: f32, nv: f32, nl: f32) -> f32 {\n let inverse = 1.0 / (roughness * roughness);\n let sin2h = max(1.0 - nh * nh, 0.0078125);\n let distribution = (2.0 + inverse) * pow(sin2h, inverse * 0.5) / (2.0 * PI);\n\n return distribution * clamp(1.0 / (4.0 * (nl + nv - nl * nv)), 0.0, 1.0);\n}\n\n/** The frame the specular lobe is defined in: Three's anisotropy direction when there is one, any frame otherwise. */\nfn specularBasis(surface: Surface) -> mat3x3<f32> {\n let normal = surface.normal;\n\n if (surface.anisotropy > 0.0) {\n let t = surface.anisotropyT - normal * dot(normal, surface.anisotropyT);\n let tangent = normalize(t);\n\n if (all(tangent == tangent) && dot(t, t) > 1e-12) {\n return mat3x3<f32>(tangent, cross(normal, tangent), normal);\n }\n }\n\n return orthonormalBasis(normal);\n}\n\n/** The GGX roughness along the frame's two axes: Three's alphaT along the anisotropy, alpha across it. */\nfn specularAlphas(surface: Surface) -> vec2<f32> {\n let alpha = surface.roughness * surface.roughness;\n\n if (surface.anisotropy > 0.0) {\n return vec2<f32>(max(surface.alphaT, 1e-4), max(alpha, 1e-4));\n }\n\n return vec2<f32>(max(alpha, 1e-4));\n}\n\n/** Anisotropic GGX in the lobe's own frame; with equal alphas it is exactly the isotropic distribution. */\nfn ggxAnisotropic(h: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let x = h.x / alphas.x;\n let y = h.y / alphas.y;\n let d = x * x + y * y + h.z * h.z;\n\n return 1.0 / (PI * alphas.x * alphas.y * d * d);\n}\n\n/** Smith's masking of one direction, in the lobe's frame. */\nfn ggxMaskingAnisotropic(v: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let projected = alphas.x * alphas.x * v.x * v.x + alphas.y * alphas.y * v.y * v.y;\n\n return 2.0 * v.z / (v.z + sqrt(v.z * v.z + projected));\n}\n\n/** Three's V_GGX_SmithCorrelated_Anisotropic, in the lobe's frame. */\nfn ggxVisibilityAnisotropic(l: vec3<f32>, v: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let gv = l.z * length(vec3<f32>(alphas.x * v.x, alphas.y * v.y, v.z));\n let gl = v.z * length(vec3<f32>(alphas.x * l.x, alphas.y * l.y, l.z));\n\n return 0.5 / max(gv + gl, 1e-7);\n}\n\n/**\n * A half vector drawn from the distribution of normals the view direction can actually see (Heitz 2018), which is what\n * keeps the specular lobe from wasting samples on microfacets facing away. The view is in the lobe's frame.\n */\nfn sampleVisibleNormal(view: vec3<f32>, alphas: vec2<f32>, u: vec2<f32>) -> vec3<f32> {\n let stretched = normalize(vec3<f32>(alphas.x * view.x, alphas.y * view.y, view.z));\n let lengthSquared = stretched.x * stretched.x + stretched.y * stretched.y;\n let t1 = select(\n vec3<f32>(1.0, 0.0, 0.0),\n vec3<f32>(-stretched.y, stretched.x, 0.0) * inverseSqrt(max(lengthSquared, 1e-20)),\n lengthSquared > 1e-20);\n let t2 = cross(stretched, t1);\n let radius = sqrt(u.x);\n let angle = 2.0 * PI * u.y;\n let px = radius * cos(angle);\n let raw = radius * sin(angle);\n let lift = 0.5 * (1.0 + stretched.z);\n let py = (1.0 - lift) * sqrt(max(0.0, 1.0 - px * px)) + lift * raw;\n let normal = t1 * px + t2 * py + stretched * sqrt(max(0.0, 1.0 - px * px - py * py));\n\n return normalize(vec3<f32>(alphas.x * normal.x, alphas.y * normal.y, max(normal.z, 1e-6)));\n}\n\n/** What the clearcoat takes back from everything under it, seen from this view: Three's (1 - clearcoat * Fcc). */\nfn coatTransmittance(surface: Surface, view: vec3<f32>) -> f32 {\n if (surface.clearcoat <= 0.0) {\n return 1.0;\n }\n\n let nv = clamp(dot(surface.clearcoatNormal, view), 0.0, 1.0);\n\n return 1.0 - surface.clearcoat * schlick3(vec3<f32>(0.04), 1.0, nv).x;\n}\n\n/** How often each lobe is chosen, as (diffuse, specular, clearcoat): by how much of the reflection each accounts for. */\nfn lobeWeights(surface: Surface, view: vec3<f32>) -> vec3<f32> {\n let nv = max(dot(surface.normal, view), 1e-4);\n\n if (surface.kind == KIND_PHONG) {\n let diffuse = luminanceOf(surface.albedo) + 0.02;\n let specular = luminanceOf(surface.specularColor) * surface.specularStrength * (surface.shininess * 0.125 + 1.0) + 0.02;\n\n return vec3<f32>(diffuse, specular, 0.0) / (diffuse + specular);\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return vec3<f32>(1.0, 0.0, 0.0);\n }\n\n let base = coatTransmittance(surface, view);\n // The floors keep every lobe reachable: the diffuse one also carries the sheen and the multiple scattering term.\n let diffuse = (luminanceOf(surface.albedo * (1.0 - surface.metalness)) + max3(surface.sheenColor)) * base + 0.02;\n let specular = luminanceOf(schlick3(surface.specularColor, surface.specularF90, nv)\n * (1.0 - surface.metalness) + surface.albedo * surface.metalness) * base + 0.05;\n let coat = select(0.0, (1.0 - base) + 0.02, surface.clearcoat > 0.0);\n let total = diffuse + specular + coat;\n\n return vec3<f32>(diffuse, specular, coat) / total;\n}\n\n/** Three's physical direct lighting for one light direction, times the cosine. */\nfn physicalValue(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> vec3<f32> {\n let normal = surface.normal;\n let nl = dot(normal, light);\n let nv = max(dot(normal, view), 1e-4);\n var value = vec3<f32>(0.0);\n\n if (nl > 0.0) {\n let basis = specularBasis(surface);\n let alphas = specularAlphas(surface);\n let half = normalize(light + view);\n let vh = clamp(dot(view, half), 0.0, 1.0);\n let f0 = surfaceF0(surface);\n var fresnel = schlick3(f0, surface.specularF90, vh);\n\n if (surface.iridescence > 0.0) {\n fresnel = mix(fresnel, surface.iridescenceFresnel, surface.iridescence);\n }\n\n let localLight = vec3<f32>(dot(light, basis[0]), dot(light, basis[1]), nl);\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), nv);\n let localHalf = vec3<f32>(dot(half, basis[0]), dot(half, basis[1]), max(dot(half, normal), 0.0));\n let single = fresnel * (ggxVisibilityAnisotropic(localLight, localView, alphas) * ggxAnisotropic(localHalf, alphas));\n\n // Three's BRDF_GGX_Multiscatter: the energy single scattering loses at high roughness, returned.\n let dfgV = dfgApprox(surface.roughness, nv);\n let dfgL = dfgApprox(surface.roughness, nl);\n let essV = f0 * dfgV.x + surface.specularF90 * dfgV.y;\n let essL = f0 * dfgL.x + surface.specularF90 * dfgL.y;\n let emsV = 1.0 - (dfgV.x + dfgV.y);\n let emsL = 1.0 - (dfgL.x + dfgL.y);\n let average = f0 + (vec3<f32>(1.0) - f0) * 0.047619;\n let multiple = essV * essL * average / (vec3<f32>(1.0) - emsV * emsL * average + 1e-7) * (emsV * emsL);\n\n var diffuse = surface.albedo * (1.0 - surface.metalness) / PI;\n var specular = single + multiple;\n\n // Three's sheen: its lobe sits on top and takes back the energy it reflects from the layers below.\n if (any(surface.sheenColor > vec3<f32>(0.0))) {\n let energy = 1.0 - max3(surface.sheenColor)\n * max(sheenAlbedo(nv, surface.sheenRoughness), sheenAlbedo(nl, surface.sheenRoughness));\n\n diffuse = diffuse * energy;\n specular = specular * energy\n + surface.sheenColor * sheenLobe(surface.sheenRoughness, clamp(dot(normal, half), 0.0, 1.0), nv, nl);\n }\n\n value = (diffuse + specular) * nl;\n }\n\n if (surface.clearcoat > 0.0) {\n let coatNormal = surface.clearcoatNormal;\n value = value * coatTransmittance(surface, view);\n\n let nlCoat = dot(coatNormal, light);\n let nvCoat = max(dot(coatNormal, view), 1e-4);\n\n if (nlCoat > 0.0) {\n let half = normalize(light + view);\n let alpha = surface.clearcoatRoughness * surface.clearcoatRoughness;\n let alpha2 = alpha * alpha;\n let fresnel = schlick3(vec3<f32>(0.04), 1.0, clamp(dot(view, half), 0.0, 1.0)).x;\n let lobe = fresnel * ggxVisibility(alpha2, nlCoat, nvCoat) * ggxDistribution(alpha2, max(dot(coatNormal, half), 0.0));\n\n value = value + vec3<f32>(surface.clearcoat * lobe * nlCoat);\n }\n }\n\n return value;\n}\n\n/** Three's D_BlinnPhong times its implicit visibility of a quarter: the Phong highlight's distribution. */\nfn blinnPhong(shininess: f32, nh: f32) -> f32 {\n return 0.25 * (shininess * 0.5 + 1.0) / PI * pow(max(nh, 0.0), shininess);\n}\n\n/** Density of the Blinn-Phong lobe's sampling, per solid angle of the light direction. */\nfn blinnPhongPdf(shininess: f32, nh: f32, vh: f32) -> f32 {\n return (shininess + 1.0) / (2.0 * PI) * pow(max(nh, 0.0), shininess) / max(4.0 * vh, 1e-6);\n}\n\n/** Density with which the GGX lobe (VNDF) would have produced this light direction, in the lobe's frame. */\nfn ggxPdf(localLight: vec3<f32>, localView: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let half = normalize(localLight + localView);\n\n if (half.z <= 0.0 || localView.z <= 0.0) {\n return 0.0;\n }\n\n return ggxMaskingAnisotropic(localView, alphas) * ggxAnisotropic(half, alphas) / (4.0 * localView.z);\n}\n\n/** The mixture density of every lobe this surface samples with. */\nfn bsdfPdf(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> f32 {\n let normal = surface.normal;\n let nl = dot(normal, light);\n let weights = lobeWeights(surface, view);\n var pdf = weights.x * max(nl, 0.0) / PI;\n\n if (surface.kind == KIND_PHONG) {\n let half = normalize(light + view);\n\n return pdf + weights.y * blinnPhongPdf(surface.shininess, dot(normal, half), max(dot(view, half), 0.0));\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return pdf;\n }\n\n let basis = specularBasis(surface);\n let alphas = specularAlphas(surface);\n\n pdf = pdf + weights.y * ggxPdf(\n vec3<f32>(dot(light, basis[0]), dot(light, basis[1]), nl),\n vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), dot(view, normal)),\n alphas);\n\n if (weights.z > 0.0) {\n let coatBasis = orthonormalBasis(surface.clearcoatNormal);\n let alpha = max(surface.clearcoatRoughness * surface.clearcoatRoughness, 1e-4);\n\n pdf = pdf + weights.z * ggxPdf(\n vec3<f32>(dot(light, coatBasis[0]), dot(light, coatBasis[1]), dot(light, surface.clearcoatNormal)),\n vec3<f32>(dot(view, coatBasis[0]), dot(view, coatBasis[1]), dot(view, surface.clearcoatNormal)),\n vec2<f32>(alpha));\n }\n\n return pdf;\n}\n\n/**\n * What the surface sends towards the view direction, and the density of that direction. A Lambert material keeps\n * Three's Lambert term alone, which is all its own shading has; a toon material reads its ramp in place of the cosine.\n */\nfn evaluateBsdf(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> BsdfEval {\n let normal = surface.normal;\n var result: BsdfEval;\n result.value = vec3<f32>(0.0);\n result.pdf = 0.0;\n\n let nl = dot(normal, light);\n let nv = dot(normal, view);\n\n if (nv <= 0.0) {\n return result;\n }\n\n if (surface.kind == KIND_TOON) {\n // Three's toon lighting reads its ramp across the whole sphere of directions, the side facing away included.\n result.value = surface.albedo / PI * toonRamp(surface.gradientMap, nl);\n result.pdf = max(nl, 0.0) / PI;\n\n return result;\n }\n\n // A clearcoat may face a light its base turns away from, so only a surface without one stops here.\n if (nl <= 0.0 && surface.clearcoat <= 0.0) {\n return result;\n }\n\n if ((surface.flags & MATERIAL_PBR) != 0u) {\n result.value = physicalValue(surface, view, light);\n } else if (surface.kind == KIND_PHONG) {\n let half = normalize(light + view);\n let fresnel = schlick3(surface.specularColor, 1.0, clamp(dot(view, half), 0.0, 1.0));\n let specular = fresnel * blinnPhong(surface.shininess, dot(normal, half)) * surface.specularStrength;\n\n result.value = (surface.albedo / PI + specular) * max(nl, 0.0);\n } else {\n result.value = surface.albedo / PI * max(nl, 0.0);\n }\n\n result.pdf = bsdfPdf(surface, view, light);\n\n return result;\n}\n\n/**\n * Reflectance of a smooth dielectric interface, from both polarisations rather than Schlick's fit: refraction needs the\n * exact cut-off past which nothing gets through. eta is the index the ray leaves over the index it enters.\n */\nfn fresnelDielectric(cosine: f32, eta: f32) -> f32 {\n let c = clamp(cosine, 0.0, 1.0);\n let sin2 = eta * eta * (1.0 - c * c);\n\n if (sin2 >= 1.0) {\n return 1.0;\n }\n\n let t = sqrt(1.0 - sin2);\n let s = (eta * c - t) / (eta * c + t);\n let p = (c - eta * t) / (c + eta * t);\n\n return 0.5 * (s * s + p * p);\n}\n\n/** Where a transmissive surface sends the path: off it, or through it. */\nstruct DielectricSample {\n direction: vec3<f32>,\n weight: vec3<f32>,\n // The path went through the surface instead of reflecting off it.\n crossed: bool,\n // It went through without bending, so the density that found the next hit is still the one before this surface.\n straight: bool,\n valid: bool,\n};\n\n/**\n * The transmissive share of a physical material. Reflection and refraction are chosen in proportion to the Fresnel\n * reflectance on a microfacet drawn from the visible normals, which leaves only the masking of the outgoing direction\n * in the weight. Light is tinted by the base colour once, on the way in, which is where Three applies it.\n *\n * A thin wall (zero thickness, Three's default) bends nothing: what is not reflected carries straight on, exactly as a\n * shadow ray crossing it does in thinCrossing, so the two estimators agree on a window. ior is the index this path\n * sees, which dispersion makes depend on the colour the path carries.\n */\nfn sampleDielectric(surface: Surface, view: vec3<f32>, ior: f32, lobe: f32, u: vec2<f32>) -> DielectricSample {\n let normal = surface.normal;\n var result: DielectricSample;\n result.valid = false;\n result.crossed = false;\n result.straight = false;\n result.weight = vec3<f32>(0.0);\n\n let nv = dot(normal, view);\n\n if (nv <= 0.0) {\n return result;\n }\n\n let alpha = max(surface.roughness * surface.roughness, 1e-4);\n let alpha2 = alpha * alpha;\n let basis = orthonormalBasis(normal);\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), nv);\n let localHalf = sampleVisibleNormal(localView, vec2<f32>(alpha), u);\n let half = basis[0] * localHalf.x + basis[1] * localHalf.y + normal * localHalf.z;\n\n if (surface.thickness <= 0.0) {\n let reflectance = fresnelDielectric(abs(dot(surface.facing, view)), 1.0 / ior);\n\n if (lobe >= reflectance) {\n result.direction = -view;\n result.weight = surface.albedo;\n result.crossed = true;\n result.straight = true;\n result.valid = true;\n\n return result;\n }\n\n result.direction = reflect(-view, half);\n } else {\n let eta = select(ior, 1.0 / ior, surface.front);\n let refracted = refract(-view, half, eta);\n // Past the critical angle refract() returns nothing, and everything reflects.\n let reflectance = select(fresnelDielectric(dot(view, half), eta), 1.0, all(refracted == vec3<f32>(0.0)));\n\n if (lobe >= reflectance) {\n result.direction = normalize(refracted);\n\n let no = -dot(normal, result.direction);\n\n if (no <= 0.0) {\n return result;\n }\n\n result.weight = vec3<f32>(ggxMasking(alpha2, no)) * select(vec3<f32>(1.0), surface.albedo, surface.front);\n result.crossed = true;\n result.valid = true;\n\n return result;\n }\n\n result.direction = reflect(-view, half);\n }\n\n let nl = dot(normal, result.direction);\n\n if (nl <= 0.0) {\n return result;\n }\n\n result.weight = vec3<f32>(ggxMasking(alpha2, nl));\n result.valid = true;\n\n return result;\n}\n\n/** A half vector around the normal distributed as cos^s, which is the Blinn-Phong lobe's own shape. */\nfn sampleBlinnPhongHalf(normal: vec3<f32>, shininess: f32, u: vec2<f32>) -> vec3<f32> {\n let cosine = pow(u.x, 1.0 / (shininess + 1.0));\n let sine = sqrt(max(0.0, 1.0 - cosine * cosine));\n let angle = 2.0 * PI * u.y;\n\n return normalize(orthonormalBasis(normal) * vec3<f32>(sine * cos(angle), sine * sin(angle), cosine));\n}\n\n/** Which way the path carries on, and what its throughput is multiplied by. */\nfn sampleBsdf(surface: Surface, view: vec3<f32>, lobe: f32, u: vec2<f32>) -> BsdfSample {\n let normal = surface.normal;\n var result: BsdfSample;\n result.valid = false;\n result.weight = vec3<f32>(0.0);\n result.pdf = 0.0;\n result.diffuse = true;\n\n if (dot(normal, view) <= 0.0) {\n return result;\n }\n\n // Lambert, and the bounce of a toon surface: Three lights a toon surface's indirect light with a plain Lambert term.\n if ((surface.flags & MATERIAL_PBR) == 0u && surface.kind != KIND_PHONG) {\n result.direction = cosineDirection(normal, u);\n // The cosine weight cancels the Lambert cosine exactly, which leaves the albedo alone.\n result.weight = surface.albedo;\n result.pdf = max(dot(normal, result.direction), 0.0) / PI;\n result.valid = result.pdf > 0.0;\n\n return result;\n }\n\n let weights = lobeWeights(surface, view);\n\n result.diffuse = lobe < weights.x;\n\n if (lobe < weights.x) {\n result.direction = cosineDirection(normal, u);\n } else if (surface.kind == KIND_PHONG) {\n result.direction = reflect(-view, sampleBlinnPhongHalf(normal, surface.shininess, u));\n } else if (lobe < weights.x + weights.y) {\n let basis = specularBasis(surface);\n // The lobe is drawn in its own frame, so the view direction goes into that frame and the result comes back out.\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), dot(view, normal));\n let localHalf = sampleVisibleNormal(localView, specularAlphas(surface), u);\n let half = basis * localHalf;\n\n result.direction = reflect(-view, half);\n } else {\n let coatNormal = surface.clearcoatNormal;\n let basis = orthonormalBasis(coatNormal);\n let alpha = max(surface.clearcoatRoughness * surface.clearcoatRoughness, 1e-4);\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), dot(view, coatNormal));\n let half = basis * sampleVisibleNormal(localView, vec2<f32>(alpha), u);\n\n result.direction = reflect(-view, half);\n }\n\n let evaluated = evaluateBsdf(surface, view, result.direction);\n\n if (evaluated.pdf <= 0.0 || all(evaluated.value <= vec3<f32>(0.0))) {\n return result;\n }\n\n result.pdf = evaluated.pdf;\n result.weight = evaluated.value / evaluated.pdf;\n result.valid = true;\n\n return result;\n}\n\n/**\n * Only the diffuse part of evaluateBsdf, times the cosine: what of the light from this direction the denoiser counts\n * as diffuse. The rest of the value is specular.\n */\nfn diffuseValue(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> vec3<f32> {\n let normal = surface.normal;\n let nl = dot(normal, light);\n let nv = max(dot(normal, view), 1e-4);\n\n if (surface.kind == KIND_TOON) {\n return surface.albedo / PI * toonRamp(surface.gradientMap, nl);\n }\n\n if (nl <= 0.0) {\n return vec3<f32>(0.0);\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return surface.albedo / PI * nl;\n }\n\n var diffuse = surface.albedo * (1.0 - surface.metalness) / PI;\n\n if (any(surface.sheenColor > vec3<f32>(0.0))) {\n diffuse = diffuse * (1.0 - max3(surface.sheenColor)\n * max(sheenAlbedo(nv, surface.sheenRoughness), sheenAlbedo(nl, surface.sheenRoughness)));\n }\n\n return diffuse * nl * coatTransmittance(surface, view);\n}\n\n/** How much of the light a surface's specular lobes reflect in all, seen from this view: what that signal is divided by. */\nfn specularAlbedo(surface: Surface, view: vec3<f32>) -> vec3<f32> {\n let nv = clamp(dot(surface.normal, view), 0.0, 1.0);\n\n if (surface.kind == KIND_PHONG) {\n return surface.specularColor * surface.specularStrength;\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return vec3<f32>(0.0);\n }\n\n let dfg = dfgApprox(surface.roughness, nv);\n var albedo = surfaceF0(surface) * dfg.x + vec3<f32>(surface.specularF90 * dfg.y);\n\n if (surface.clearcoat > 0.0) {\n let coatDfg = dfgApprox(surface.clearcoatRoughness, clamp(dot(surface.clearcoatNormal, view), 0.0, 1.0));\n albedo = albedo * coatTransmittance(surface, view) + vec3<f32>(surface.clearcoat * (0.04 * coatDfg.x + coatDfg.y));\n }\n\n // What crosses a transmissive surface is the specular signal too, and it is not filtered by any colour of the surface.\n return mix(albedo, vec3<f32>(1.0), clamp(surface.transmission, 0.0, 1.0));\n}\n";
12
+ export declare const BSDF_WGSL = "\n/** How a surface answers a direction: what it reflects, and how likely sampling was to have chosen it. */\nstruct BsdfEval {\n // The bidirectional distribution times the cosine at the surface, which is what a contribution is multiplied by.\n value: vec3<f32>,\n // Solid angle density of this direction under this renderer's own sampling, for the power heuristic.\n pdf: f32,\n};\n\nstruct BsdfSample {\n direction: vec3<f32>,\n // The distribution over its own density: what the path's throughput is multiplied by.\n weight: vec3<f32>,\n pdf: f32,\n valid: bool,\n // The diffuse lobe chose the direction, rather than a specular one: which of the denoiser's two signals it feeds.\n diffuse: bool,\n};\n\nfn luminanceOf(color: vec3<f32>) -> f32 {\n return dot(color, vec3<f32>(0.2126, 0.7152, 0.0722));\n}\n\nfn max3(color: vec3<f32>) -> f32 {\n return max(color.r, max(color.g, color.b));\n}\n\n/** Schlick's approximation, as in Three: the reflectance always reaches one at a grazing angle. */\nfn fresnelSchlick(f0: vec3<f32>, cosine: f32) -> vec3<f32> {\n return schlick3(f0, 1.0, cosine);\n}\n\n/** Reflectance at normal incidence: the dielectric's own, turned towards the base colour as the surface turns metal. */\nfn surfaceF0(surface: Surface) -> vec3<f32> {\n return mix(surface.specularColor, surface.albedo, surface.metalness);\n}\n\n/** GGX, with alpha the square of the roughness, as Three parameterises it. */\nfn ggxDistribution(alpha2: f32, nh: f32) -> f32 {\n let denominator = nh * nh * (alpha2 - 1.0) + 1.0;\n\n return alpha2 / (PI * denominator * denominator);\n}\n\n/** Smith's height correlated visibility, which already carries the 1/(4 nl nv) the lobe would otherwise need. */\nfn ggxVisibility(alpha2: f32, nl: f32, nv: f32) -> f32 {\n return 0.5 / (nl * sqrt(nv * nv * (1.0 - alpha2) + alpha2) + nv * sqrt(nl * nl * (1.0 - alpha2) + alpha2) + 1e-7);\n}\n\n/** The separable masking term, which is the one the visible normal sampling is built on. */\nfn ggxMasking(alpha2: f32, nv: f32) -> f32 {\n return 2.0 * nv / (nv + sqrt(alpha2 + (1.0 - alpha2) * nv * nv));\n}\n\n/** Karis' analytic fit of the GGX directional albedo (Three's DFGApprox): the scale and bias applied to F0 and F90. */\nfn dfgApprox(roughness: f32, nv: f32) -> vec2<f32> {\n let r = roughness * vec4<f32>(-1.0, -0.0275, -0.572, 0.022) + vec4<f32>(1.0, 0.0425, 1.04, -0.04);\n let a004 = min(r.x * r.x, exp2(-9.28 * nv)) * r.x + r.y;\n\n return vec2<f32>(-1.04, 1.04) * a004 + r.zw;\n}\n\n/** Three's IBLSheenBRDF: the Charlie sheen lobe integrated over the hemisphere. */\nfn sheenAlbedo(cosine: f32, roughness: f32) -> f32 {\n let r2 = roughness * roughness;\n let inverse = 1.0 / (roughness + 0.1);\n let a = -1.9362 + 1.0678 * roughness + 0.4573 * r2 - 0.8469 * inverse;\n let b = -0.6014 + 0.5538 * roughness - 0.4670 * r2 - 0.1255 * inverse;\n\n return clamp(exp(a * clamp(cosine, 0.0, 1.0) + b), 0.0, 1.0);\n}\n\n/** Three's D_Charlie times V_Neubelt. */\nfn sheenLobe(roughness: f32, nh: f32, nv: f32, nl: f32) -> f32 {\n let inverse = 1.0 / (roughness * roughness);\n let sin2h = max(1.0 - nh * nh, 0.0078125);\n let distribution = (2.0 + inverse) * pow(sin2h, inverse * 0.5) / (2.0 * PI);\n\n return distribution * clamp(1.0 / (4.0 * (nl + nv - nl * nv)), 0.0, 1.0);\n}\n\n/** The frame the specular lobe is defined in: Three's anisotropy direction when there is one, any frame otherwise. */\nfn specularBasis(surface: Surface) -> mat3x3<f32> {\n let normal = surface.normal;\n\n if (surface.anisotropy > 0.0) {\n let t = surface.anisotropyT - normal * dot(normal, surface.anisotropyT);\n let tangent = normalize(t);\n\n if (all(tangent == tangent) && dot(t, t) > 1e-12) {\n return mat3x3<f32>(tangent, cross(normal, tangent), normal);\n }\n }\n\n return orthonormalBasis(normal);\n}\n\n/** The GGX roughness along the frame's two axes: Three's alphaT along the anisotropy, alpha across it. */\nfn specularAlphas(surface: Surface) -> vec2<f32> {\n let alpha = surface.roughness * surface.roughness;\n\n if (surface.anisotropy > 0.0) {\n return vec2<f32>(max(surface.alphaT, 1e-4), max(alpha, 1e-4));\n }\n\n return vec2<f32>(max(alpha, 1e-4));\n}\n\n/** Anisotropic GGX in the lobe's own frame; with equal alphas it is exactly the isotropic distribution. */\nfn ggxAnisotropic(h: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let x = h.x / alphas.x;\n let y = h.y / alphas.y;\n let d = x * x + y * y + h.z * h.z;\n\n return 1.0 / (PI * alphas.x * alphas.y * d * d);\n}\n\n/** Smith's masking of one direction, in the lobe's frame. */\nfn ggxMaskingAnisotropic(v: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let projected = alphas.x * alphas.x * v.x * v.x + alphas.y * alphas.y * v.y * v.y;\n\n return 2.0 * v.z / (v.z + sqrt(v.z * v.z + projected));\n}\n\n/** Three's V_GGX_SmithCorrelated_Anisotropic, in the lobe's frame. */\nfn ggxVisibilityAnisotropic(l: vec3<f32>, v: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let gv = l.z * length(vec3<f32>(alphas.x * v.x, alphas.y * v.y, v.z));\n let gl = v.z * length(vec3<f32>(alphas.x * l.x, alphas.y * l.y, l.z));\n\n return 0.5 / max(gv + gl, 1e-7);\n}\n\n/**\n * A half vector drawn from the distribution of normals the view direction can actually see (Heitz 2018), which is what\n * keeps the specular lobe from wasting samples on microfacets facing away. The view is in the lobe's frame.\n */\nfn sampleVisibleNormal(view: vec3<f32>, alphas: vec2<f32>, u: vec2<f32>) -> vec3<f32> {\n let stretched = normalize(vec3<f32>(alphas.x * view.x, alphas.y * view.y, view.z));\n let lengthSquared = stretched.x * stretched.x + stretched.y * stretched.y;\n let t1 = select(\n vec3<f32>(1.0, 0.0, 0.0),\n vec3<f32>(-stretched.y, stretched.x, 0.0) * inverseSqrt(max(lengthSquared, 1e-20)),\n lengthSquared > 1e-20);\n let t2 = cross(stretched, t1);\n let radius = sqrt(u.x);\n let angle = 2.0 * PI * u.y;\n let px = radius * cos(angle);\n let raw = radius * sin(angle);\n let lift = 0.5 * (1.0 + stretched.z);\n let py = (1.0 - lift) * sqrt(max(0.0, 1.0 - px * px)) + lift * raw;\n let normal = t1 * px + t2 * py + stretched * sqrt(max(0.0, 1.0 - px * px - py * py));\n\n return normalize(vec3<f32>(alphas.x * normal.x, alphas.y * normal.y, max(normal.z, 1e-6)));\n}\n\n/** What the clearcoat takes back from everything under it, seen from this view: Three's (1 - clearcoat * Fcc). */\nfn coatTransmittance(surface: Surface, view: vec3<f32>) -> f32 {\n if (surface.clearcoat <= 0.0) {\n return 1.0;\n }\n\n let nv = clamp(dot(surface.clearcoatNormal, view), 0.0, 1.0);\n\n return 1.0 - surface.clearcoat * schlick3(vec3<f32>(0.04), 1.0, nv).x;\n}\n\n/**\n * A point inside a volume, where light scatters by the medium's phase function instead of off a surface. Only the\n * integrator makes one: its normal faces back along the ray, its albedo is the medium's, its anisotropy the phase's.\n */\nconst KIND_VOLUME: u32 = 100u;\n\n/** How often each lobe is chosen, as (diffuse, specular, clearcoat): by how much of the reflection each accounts for. */\nfn lobeWeights(surface: Surface, view: vec3<f32>) -> vec3<f32> {\n let nv = max(dot(surface.normal, view), 1e-4);\n\n if (surface.kind == KIND_PHONG) {\n let diffuse = luminanceOf(surface.albedo) + 0.02;\n let specular = luminanceOf(surface.specularColor) * surface.specularStrength * (surface.shininess * 0.125 + 1.0) + 0.02;\n\n return vec3<f32>(diffuse, specular, 0.0) / (diffuse + specular);\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return vec3<f32>(1.0, 0.0, 0.0);\n }\n\n let base = coatTransmittance(surface, view);\n // The floors keep every lobe reachable: the diffuse one also carries the sheen and the multiple scattering term.\n let diffuse = (luminanceOf(surface.albedo * ((1.0 - surface.metalness) * (1.0 - surface.subsurface)))\n + max3(surface.sheenColor)) * base + 0.02;\n let specular = luminanceOf(schlick3(surface.specularColor, surface.specularF90, nv)\n * (1.0 - surface.metalness) + surface.albedo * surface.metalness) * base + 0.05;\n let coat = select(0.0, (1.0 - base) + 0.02, surface.clearcoat > 0.0);\n let total = diffuse + specular + coat;\n\n return vec3<f32>(diffuse, specular, coat) / total;\n}\n\n/** Three's physical direct lighting for one light direction, times the cosine. */\nfn physicalValue(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> vec3<f32> {\n let normal = surface.normal;\n let nl = dot(normal, light);\n let nv = max(dot(normal, view), 1e-4);\n var value = vec3<f32>(0.0);\n\n if (nl > 0.0) {\n let basis = specularBasis(surface);\n let alphas = specularAlphas(surface);\n let half = normalize(light + view);\n let vh = clamp(dot(view, half), 0.0, 1.0);\n let f0 = surfaceF0(surface);\n var fresnel = schlick3(f0, surface.specularF90, vh);\n\n if (surface.iridescence > 0.0) {\n fresnel = mix(fresnel, surface.iridescenceFresnel, surface.iridescence);\n }\n\n let localLight = vec3<f32>(dot(light, basis[0]), dot(light, basis[1]), nl);\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), nv);\n let localHalf = vec3<f32>(dot(half, basis[0]), dot(half, basis[1]), max(dot(half, normal), 0.0));\n let single = fresnel * (ggxVisibilityAnisotropic(localLight, localView, alphas) * ggxAnisotropic(localHalf, alphas));\n\n // Three's BRDF_GGX_Multiscatter: the energy single scattering loses at high roughness, returned.\n let dfgV = dfgApprox(surface.roughness, nv);\n let dfgL = dfgApprox(surface.roughness, nl);\n let essV = f0 * dfgV.x + surface.specularF90 * dfgV.y;\n let essL = f0 * dfgL.x + surface.specularF90 * dfgL.y;\n let emsV = 1.0 - (dfgV.x + dfgV.y);\n let emsL = 1.0 - (dfgL.x + dfgL.y);\n let average = f0 + (vec3<f32>(1.0) - f0) * 0.047619;\n let multiple = essV * essL * average / (vec3<f32>(1.0) - emsV * emsL * average + 1e-7) * (emsV * emsL);\n\n // What scatters inside the material leaves it elsewhere, by the random walk, not from this point.\n var diffuse = surface.albedo * ((1.0 - surface.metalness) * (1.0 - surface.subsurface)) / PI;\n var specular = single + multiple;\n\n // Three's sheen: its lobe sits on top and takes back the energy it reflects from the layers below.\n if (any(surface.sheenColor > vec3<f32>(0.0))) {\n let energy = 1.0 - max3(surface.sheenColor)\n * max(sheenAlbedo(nv, surface.sheenRoughness), sheenAlbedo(nl, surface.sheenRoughness));\n\n diffuse = diffuse * energy;\n specular = specular * energy\n + surface.sheenColor * sheenLobe(surface.sheenRoughness, clamp(dot(normal, half), 0.0, 1.0), nv, nl);\n }\n\n value = (diffuse + specular) * nl;\n }\n\n if (surface.clearcoat > 0.0) {\n let coatNormal = surface.clearcoatNormal;\n value = value * coatTransmittance(surface, view);\n\n let nlCoat = dot(coatNormal, light);\n let nvCoat = max(dot(coatNormal, view), 1e-4);\n\n if (nlCoat > 0.0) {\n let half = normalize(light + view);\n let alpha = surface.clearcoatRoughness * surface.clearcoatRoughness;\n let alpha2 = alpha * alpha;\n let fresnel = schlick3(vec3<f32>(0.04), 1.0, clamp(dot(view, half), 0.0, 1.0)).x;\n let lobe = fresnel * ggxVisibility(alpha2, nlCoat, nvCoat) * ggxDistribution(alpha2, max(dot(coatNormal, half), 0.0));\n\n value = value + vec3<f32>(surface.clearcoat * lobe * nlCoat);\n }\n }\n\n return value;\n}\n\n/** Three's D_BlinnPhong times its implicit visibility of a quarter: the Phong highlight's distribution. */\nfn blinnPhong(shininess: f32, nh: f32) -> f32 {\n return 0.25 * (shininess * 0.5 + 1.0) / PI * pow(max(nh, 0.0), shininess);\n}\n\n/** Density of the Blinn-Phong lobe's sampling, per solid angle of the light direction. */\nfn blinnPhongPdf(shininess: f32, nh: f32, vh: f32) -> f32 {\n return (shininess + 1.0) / (2.0 * PI) * pow(max(nh, 0.0), shininess) / max(4.0 * vh, 1e-6);\n}\n\n/** Density with which the GGX lobe (VNDF) would have produced this light direction, in the lobe's frame. */\nfn ggxPdf(localLight: vec3<f32>, localView: vec3<f32>, alphas: vec2<f32>) -> f32 {\n let half = normalize(localLight + localView);\n\n if (half.z <= 0.0 || localView.z <= 0.0) {\n return 0.0;\n }\n\n return ggxMaskingAnisotropic(localView, alphas) * ggxAnisotropic(half, alphas) / (4.0 * localView.z);\n}\n\n/** The mixture density of every lobe this surface samples with. */\nfn bsdfPdf(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> f32 {\n let normal = surface.normal;\n let nl = dot(normal, light);\n let weights = lobeWeights(surface, view);\n var pdf = weights.x * max(nl, 0.0) / PI;\n\n if (surface.kind == KIND_PHONG) {\n let half = normalize(light + view);\n\n return pdf + weights.y * blinnPhongPdf(surface.shininess, dot(normal, half), max(dot(view, half), 0.0));\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return pdf;\n }\n\n let basis = specularBasis(surface);\n let alphas = specularAlphas(surface);\n\n pdf = pdf + weights.y * ggxPdf(\n vec3<f32>(dot(light, basis[0]), dot(light, basis[1]), nl),\n vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), dot(view, normal)),\n alphas);\n\n if (weights.z > 0.0) {\n let coatBasis = orthonormalBasis(surface.clearcoatNormal);\n let alpha = max(surface.clearcoatRoughness * surface.clearcoatRoughness, 1e-4);\n\n pdf = pdf + weights.z * ggxPdf(\n vec3<f32>(dot(light, coatBasis[0]), dot(light, coatBasis[1]), dot(light, surface.clearcoatNormal)),\n vec3<f32>(dot(view, coatBasis[0]), dot(view, coatBasis[1]), dot(view, surface.clearcoatNormal)),\n vec2<f32>(alpha));\n }\n\n return pdf;\n}\n\n/**\n * What the surface sends towards the view direction, and the density of that direction. A Lambert material keeps\n * Three's Lambert term alone, which is all its own shading has; a toon material reads its ramp in place of the cosine.\n */\nfn evaluateBsdf(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> BsdfEval {\n let normal = surface.normal;\n var result: BsdfEval;\n result.value = vec3<f32>(0.0);\n result.pdf = 0.0;\n\n let nl = dot(normal, light);\n let nv = dot(normal, view);\n\n // In a medium every direction is open: the phase function stands for the lobes, and samples itself exactly.\n if (surface.kind == KIND_VOLUME) {\n let phase = henyeyGreenstein(dot(-view, light), surface.anisotropy);\n\n result.value = surface.albedo * phase;\n result.pdf = phase;\n\n return result;\n }\n\n if (nv <= 0.0) {\n return result;\n }\n\n if (surface.kind == KIND_TOON) {\n // Three's toon lighting reads its ramp across the whole sphere of directions, the side facing away included.\n result.value = surface.albedo / PI * toonRamp(surface.gradientMap, nl);\n result.pdf = max(nl, 0.0) / PI;\n\n return result;\n }\n\n // A clearcoat may face a light its base turns away from, so only a surface without one stops here.\n if (nl <= 0.0 && surface.clearcoat <= 0.0) {\n return result;\n }\n\n if ((surface.flags & MATERIAL_PBR) != 0u) {\n result.value = physicalValue(surface, view, light);\n } else if (surface.kind == KIND_PHONG) {\n let half = normalize(light + view);\n let fresnel = schlick3(surface.specularColor, 1.0, clamp(dot(view, half), 0.0, 1.0));\n let specular = fresnel * blinnPhong(surface.shininess, dot(normal, half)) * surface.specularStrength;\n\n result.value = (surface.albedo / PI + specular) * max(nl, 0.0);\n } else {\n result.value = surface.albedo / PI * max(nl, 0.0);\n }\n\n result.pdf = bsdfPdf(surface, view, light);\n\n return result;\n}\n\n/**\n * Reflectance of a smooth dielectric interface, from both polarisations rather than Schlick's fit: refraction needs the\n * exact cut-off past which nothing gets through. eta is the index the ray leaves over the index it enters.\n */\nfn fresnelDielectric(cosine: f32, eta: f32) -> f32 {\n let c = clamp(cosine, 0.0, 1.0);\n let sin2 = eta * eta * (1.0 - c * c);\n\n if (sin2 >= 1.0) {\n return 1.0;\n }\n\n let t = sqrt(1.0 - sin2);\n let s = (eta * c - t) / (eta * c + t);\n let p = (c - eta * t) / (c + eta * t);\n\n return 0.5 * (s * s + p * p);\n}\n\n/** Where a transmissive surface sends the path: off it, or through it. */\nstruct DielectricSample {\n direction: vec3<f32>,\n weight: vec3<f32>,\n // The path went through the surface instead of reflecting off it.\n crossed: bool,\n // It went through without bending, so the density that found the next hit is still the one before this surface.\n straight: bool,\n valid: bool,\n};\n\n/**\n * The transmissive share of a physical material. Reflection and refraction are chosen in proportion to the Fresnel\n * reflectance on a microfacet drawn from the visible normals, which leaves only the masking of the outgoing direction\n * in the weight. Light is tinted by the base colour once, on the way in, which is where Three applies it.\n *\n * A thin wall (zero thickness, Three's default) bends nothing: what is not reflected carries straight on, exactly as a\n * shadow ray crossing it does in thinCrossing, so the two estimators agree on a window. ior is the index this path\n * sees, which dispersion makes depend on the colour the path carries.\n */\nfn sampleDielectric(surface: Surface, view: vec3<f32>, ior: f32, lobe: f32, u: vec2<f32>) -> DielectricSample {\n let normal = surface.normal;\n var result: DielectricSample;\n result.valid = false;\n result.crossed = false;\n result.straight = false;\n result.weight = vec3<f32>(0.0);\n\n let nv = dot(normal, view);\n\n if (nv <= 0.0) {\n return result;\n }\n\n let alpha = max(surface.roughness * surface.roughness, 1e-4);\n let alpha2 = alpha * alpha;\n let basis = orthonormalBasis(normal);\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), nv);\n let localHalf = sampleVisibleNormal(localView, vec2<f32>(alpha), u);\n let half = basis[0] * localHalf.x + basis[1] * localHalf.y + normal * localHalf.z;\n\n if (surface.thickness <= 0.0) {\n let reflectance = fresnelDielectric(abs(dot(surface.facing, view)), 1.0 / ior);\n\n if (lobe >= reflectance) {\n result.direction = -view;\n result.weight = surface.albedo;\n result.crossed = true;\n result.straight = true;\n result.valid = true;\n\n return result;\n }\n\n result.direction = reflect(-view, half);\n } else {\n let eta = select(ior, 1.0 / ior, surface.front);\n let refracted = refract(-view, half, eta);\n // Past the critical angle refract() returns nothing, and everything reflects.\n let reflectance = select(fresnelDielectric(dot(view, half), eta), 1.0, all(refracted == vec3<f32>(0.0)));\n\n if (lobe >= reflectance) {\n result.direction = normalize(refracted);\n\n let no = -dot(normal, result.direction);\n\n if (no <= 0.0) {\n return result;\n }\n\n result.weight = vec3<f32>(ggxMasking(alpha2, no)) * select(vec3<f32>(1.0), surface.albedo, surface.front);\n result.crossed = true;\n result.valid = true;\n\n return result;\n }\n\n result.direction = reflect(-view, half);\n }\n\n let nl = dot(normal, result.direction);\n\n if (nl <= 0.0) {\n return result;\n }\n\n result.weight = vec3<f32>(ggxMasking(alpha2, nl));\n result.valid = true;\n\n return result;\n}\n\n/** A half vector around the normal distributed as cos^s, which is the Blinn-Phong lobe's own shape. */\nfn sampleBlinnPhongHalf(normal: vec3<f32>, shininess: f32, u: vec2<f32>) -> vec3<f32> {\n let cosine = pow(u.x, 1.0 / (shininess + 1.0));\n let sine = sqrt(max(0.0, 1.0 - cosine * cosine));\n let angle = 2.0 * PI * u.y;\n\n return normalize(orthonormalBasis(normal) * vec3<f32>(sine * cos(angle), sine * sin(angle), cosine));\n}\n\n/** Which way the path carries on, and what its throughput is multiplied by. */\nfn sampleBsdf(surface: Surface, view: vec3<f32>, lobe: f32, u: vec2<f32>) -> BsdfSample {\n let normal = surface.normal;\n var result: BsdfSample;\n result.valid = false;\n result.weight = vec3<f32>(0.0);\n result.pdf = 0.0;\n result.diffuse = true;\n\n if (surface.kind == KIND_VOLUME) {\n result.direction = sampleHenyeyGreenstein(-view, surface.anisotropy, u);\n result.weight = surface.albedo;\n result.pdf = henyeyGreenstein(dot(-view, result.direction), surface.anisotropy);\n result.valid = true;\n\n return result;\n }\n\n if (dot(normal, view) <= 0.0) {\n return result;\n }\n\n // Lambert, and the bounce of a toon surface: Three lights a toon surface's indirect light with a plain Lambert term.\n if ((surface.flags & MATERIAL_PBR) == 0u && surface.kind != KIND_PHONG) {\n result.direction = cosineDirection(normal, u);\n // The cosine weight cancels the Lambert cosine exactly, which leaves the albedo alone.\n result.weight = surface.albedo;\n result.pdf = max(dot(normal, result.direction), 0.0) / PI;\n result.valid = result.pdf > 0.0;\n\n return result;\n }\n\n let weights = lobeWeights(surface, view);\n\n result.diffuse = lobe < weights.x;\n\n if (lobe < weights.x) {\n result.direction = cosineDirection(normal, u);\n } else if (surface.kind == KIND_PHONG) {\n result.direction = reflect(-view, sampleBlinnPhongHalf(normal, surface.shininess, u));\n } else if (lobe < weights.x + weights.y) {\n let basis = specularBasis(surface);\n // The lobe is drawn in its own frame, so the view direction goes into that frame and the result comes back out.\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), dot(view, normal));\n let localHalf = sampleVisibleNormal(localView, specularAlphas(surface), u);\n let half = basis * localHalf;\n\n result.direction = reflect(-view, half);\n } else {\n let coatNormal = surface.clearcoatNormal;\n let basis = orthonormalBasis(coatNormal);\n let alpha = max(surface.clearcoatRoughness * surface.clearcoatRoughness, 1e-4);\n let localView = vec3<f32>(dot(view, basis[0]), dot(view, basis[1]), dot(view, coatNormal));\n let half = basis * sampleVisibleNormal(localView, vec2<f32>(alpha), u);\n\n result.direction = reflect(-view, half);\n }\n\n let evaluated = evaluateBsdf(surface, view, result.direction);\n\n if (evaluated.pdf <= 0.0 || all(evaluated.value <= vec3<f32>(0.0))) {\n return result;\n }\n\n result.pdf = evaluated.pdf;\n result.weight = evaluated.value / evaluated.pdf;\n result.valid = true;\n\n return result;\n}\n\n/**\n * Only the diffuse part of evaluateBsdf, times the cosine: what of the light from this direction the denoiser counts\n * as diffuse. The rest of the value is specular.\n */\nfn diffuseValue(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> vec3<f32> {\n let normal = surface.normal;\n let nl = dot(normal, light);\n let nv = max(dot(normal, view), 1e-4);\n\n if (surface.kind == KIND_TOON) {\n return surface.albedo / PI * toonRamp(surface.gradientMap, nl);\n }\n\n // Light scattered in a medium is all diffuse, as the denoiser sees it.\n if (surface.kind == KIND_VOLUME) {\n return surface.albedo * henyeyGreenstein(dot(-view, light), surface.anisotropy);\n }\n\n if (nl <= 0.0) {\n return vec3<f32>(0.0);\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return surface.albedo / PI * nl;\n }\n\n var diffuse = surface.albedo * ((1.0 - surface.metalness) * (1.0 - surface.subsurface)) / PI;\n\n if (any(surface.sheenColor > vec3<f32>(0.0))) {\n diffuse = diffuse * (1.0 - max3(surface.sheenColor)\n * max(sheenAlbedo(nv, surface.sheenRoughness), sheenAlbedo(nl, surface.sheenRoughness)));\n }\n\n return diffuse * nl * coatTransmittance(surface, view);\n}\n\n/** How much of the light a surface's specular lobes reflect in all, seen from this view: what that signal is divided by. */\nfn specularAlbedo(surface: Surface, view: vec3<f32>) -> vec3<f32> {\n let nv = clamp(dot(surface.normal, view), 0.0, 1.0);\n\n if (surface.kind == KIND_PHONG) {\n return surface.specularColor * surface.specularStrength;\n }\n\n if ((surface.flags & MATERIAL_PBR) == 0u) {\n return vec3<f32>(0.0);\n }\n\n let dfg = dfgApprox(surface.roughness, nv);\n var albedo = surfaceF0(surface) * dfg.x + vec3<f32>(surface.specularF90 * dfg.y);\n\n if (surface.clearcoat > 0.0) {\n let coatDfg = dfgApprox(surface.clearcoatRoughness, clamp(dot(surface.clearcoatNormal, view), 0.0, 1.0));\n albedo = albedo * coatTransmittance(surface, view) + vec3<f32>(surface.clearcoat * (0.04 * coatDfg.x + coatDfg.y));\n }\n\n // What crosses a transmissive surface is the specular signal too, and it is not filtered by any colour of the surface.\n return mix(albedo, vec3<f32>(1.0), clamp(surface.transmission, 0.0, 1.0));\n}\n";
13
13
  //# sourceMappingURL=bsdf.wgsl.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bsdf.wgsl.d.ts","sourceRoot":"","sources":["../../src/wgsl/bsdf.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,s8tBA8lBrB,CAAC"}
1
+ {"version":3,"file":"bsdf.wgsl.d.ts","sourceRoot":"","sources":["../../src/wgsl/bsdf.wgsl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,+twBA8nBrB,CAAC"}