@luma.gl/engine 10.0.0-alpha.10 → 10.0.0-alpha.12

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 (226) hide show
  1. package/dist/animation/node-animation-binding.cjs +10 -0
  2. package/dist/animation/node-animation-binding.d.ts +1 -1
  3. package/dist/animation/node-animation-binding.d.ts.map +1 -1
  4. package/dist/animation/node-animation-binding.js +10 -0
  5. package/dist/animation/node-animation-binding.js.map +1 -1
  6. package/dist/dist.dev.js +457 -206
  7. package/dist/dist.min.js +58 -58
  8. package/dist/gltf/gltf/animations/animations.d.ts +1 -1
  9. package/dist/gltf/gltf/animations/animations.d.ts.map +1 -1
  10. package/dist/gltf/gltf/create-scenegraph-from-gltf.cjs +80 -12
  11. package/dist/gltf/gltf/create-scenegraph-from-gltf.d.ts +8 -1
  12. package/dist/gltf/gltf/create-scenegraph-from-gltf.d.ts.map +1 -1
  13. package/dist/gltf/gltf/create-scenegraph-from-gltf.js +82 -14
  14. package/dist/gltf/gltf/create-scenegraph-from-gltf.js.map +1 -1
  15. package/dist/gltf/gltf/gltf-billboard.cjs +6 -1
  16. package/dist/gltf/gltf/gltf-billboard.d.ts.map +1 -1
  17. package/dist/gltf/gltf/gltf-billboard.js +6 -1
  18. package/dist/gltf/gltf/gltf-billboard.js.map +1 -1
  19. package/dist/gltf/gltf/gltf-extension-support.cjs +2 -2
  20. package/dist/gltf/gltf/gltf-extension-support.js +2 -2
  21. package/dist/gltf/gltf/gltf-extension-support.js.map +1 -1
  22. package/dist/gltf/index.cjs +2 -0
  23. package/dist/gltf/index.cjs.map +3 -3
  24. package/dist/gltf/index.d.ts +1 -1
  25. package/dist/gltf/index.d.ts.map +1 -1
  26. package/dist/gltf/index.js +1 -1
  27. package/dist/gltf/index.js.map +1 -1
  28. package/dist/gltf/parsers/parse-gltf-animations.cjs +8 -5
  29. package/dist/gltf/parsers/parse-gltf-animations.js +20 -5
  30. package/dist/gltf/parsers/parse-gltf-animations.js.map +1 -1
  31. package/dist/gltf/parsers/parse-gltf-lights.cjs +36 -72
  32. package/dist/gltf/parsers/parse-gltf-lights.d.ts +6 -2
  33. package/dist/gltf/parsers/parse-gltf-lights.d.ts.map +1 -1
  34. package/dist/gltf/parsers/parse-gltf-lights.js +34 -91
  35. package/dist/gltf/parsers/parse-gltf-lights.js.map +1 -1
  36. package/dist/gltf/parsers/parse-gltf-metadata.cjs +60 -0
  37. package/dist/gltf/parsers/parse-gltf-metadata.d.ts +5 -0
  38. package/dist/gltf/parsers/parse-gltf-metadata.d.ts.map +1 -0
  39. package/dist/gltf/parsers/parse-gltf-metadata.js +39 -0
  40. package/dist/gltf/parsers/parse-gltf-metadata.js.map +1 -0
  41. package/dist/gltf/parsers/parse-gltf.cjs +4 -1
  42. package/dist/gltf/parsers/parse-gltf.d.ts +4 -0
  43. package/dist/gltf/parsers/parse-gltf.d.ts.map +1 -1
  44. package/dist/gltf/parsers/parse-gltf.js +3 -0
  45. package/dist/gltf/parsers/parse-gltf.js.map +1 -1
  46. package/dist/gltf/pbr/pbr-environment.cjs +21 -3
  47. package/dist/gltf/pbr/pbr-environment.d.ts +3 -0
  48. package/dist/gltf/pbr/pbr-environment.d.ts.map +1 -1
  49. package/dist/gltf/pbr/pbr-environment.js +19 -1
  50. package/dist/gltf/pbr/pbr-environment.js.map +1 -1
  51. package/dist/graph/models/index.cjs.map +1 -1
  52. package/dist/graph/models/path/path-attribute-model.cjs +1 -1
  53. package/dist/graph/models/path/path-attribute-model.js +1 -1
  54. package/dist/graph/models/path/path-attribute-model.js.map +1 -1
  55. package/dist/graph/models/path/path-storage-model.cjs +1 -1
  56. package/dist/graph/models/path/path-storage-model.js +1 -1
  57. package/dist/graph/models/path/path-storage-model.js.map +1 -1
  58. package/dist/graph/physics/index.cjs.map +1 -1
  59. package/dist/graph/tables/index.cjs.map +1 -1
  60. package/dist/index.cjs +13 -0
  61. package/dist/index.cjs.map +3 -3
  62. package/dist/index.d.ts +4 -0
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +3 -0
  65. package/dist/index.js.map +1 -1
  66. package/dist/lighting/ambient-occlusion-pass.cjs +126 -0
  67. package/dist/lighting/ambient-occlusion-pass.d.ts +31 -0
  68. package/dist/lighting/ambient-occlusion-pass.d.ts.map +1 -0
  69. package/dist/lighting/ambient-occlusion-pass.js +108 -0
  70. package/dist/lighting/ambient-occlusion-pass.js.map +1 -0
  71. package/dist/lighting/clustered-light-grid.cjs +378 -0
  72. package/dist/lighting/clustered-light-grid.d.ts +94 -0
  73. package/dist/lighting/clustered-light-grid.d.ts.map +1 -0
  74. package/dist/lighting/clustered-light-grid.js +370 -0
  75. package/dist/lighting/clustered-light-grid.js.map +1 -0
  76. package/dist/lighting/contact-shadow-composite.cjs +80 -0
  77. package/dist/lighting/contact-shadow-composite.d.ts +6 -0
  78. package/dist/lighting/contact-shadow-composite.d.ts.map +1 -0
  79. package/dist/lighting/contact-shadow-composite.js +59 -0
  80. package/dist/lighting/contact-shadow-composite.js.map +1 -0
  81. package/dist/lighting/contact-shadow-pass.cjs +102 -0
  82. package/dist/lighting/contact-shadow-pass.d.ts +23 -0
  83. package/dist/lighting/contact-shadow-pass.d.ts.map +1 -0
  84. package/dist/lighting/contact-shadow-pass.js +85 -0
  85. package/dist/lighting/contact-shadow-pass.js.map +1 -0
  86. package/dist/lighting/index.cjs +118 -0
  87. package/dist/lighting/index.cjs.map +6 -0
  88. package/dist/lighting/index.d.ts +31 -0
  89. package/dist/lighting/index.d.ts.map +1 -0
  90. package/dist/lighting/index.js +20 -0
  91. package/dist/lighting/index.js.map +1 -0
  92. package/dist/lighting/lightmap-baker.cjs +238 -0
  93. package/dist/lighting/lightmap-baker.d.ts +62 -0
  94. package/dist/lighting/lightmap-baker.d.ts.map +1 -0
  95. package/dist/lighting/lightmap-baker.js +236 -0
  96. package/dist/lighting/lightmap-baker.js.map +1 -0
  97. package/dist/lighting/lightmap-data.cjs +117 -0
  98. package/dist/lighting/lightmap-data.d.ts +20 -0
  99. package/dist/lighting/lightmap-data.d.ts.map +1 -0
  100. package/dist/lighting/lightmap-data.js +110 -0
  101. package/dist/lighting/lightmap-data.js.map +1 -0
  102. package/dist/lighting/lightmap-environment.cjs +61 -0
  103. package/dist/lighting/lightmap-environment.d.ts +18 -0
  104. package/dist/lighting/lightmap-environment.d.ts.map +1 -0
  105. package/dist/lighting/lightmap-environment.js +46 -0
  106. package/dist/lighting/lightmap-environment.js.map +1 -0
  107. package/dist/lighting/pbr-environment.cjs +45 -0
  108. package/dist/lighting/pbr-environment.d.ts +25 -0
  109. package/dist/lighting/pbr-environment.d.ts.map +1 -0
  110. package/dist/lighting/pbr-environment.js +30 -0
  111. package/dist/lighting/pbr-environment.js.map +1 -0
  112. package/dist/lighting/pbr-material.cjs +75 -0
  113. package/dist/lighting/pbr-material.d.ts +33 -0
  114. package/dist/lighting/pbr-material.d.ts.map +1 -0
  115. package/dist/lighting/pbr-material.js +52 -0
  116. package/dist/lighting/pbr-material.js.map +1 -0
  117. package/dist/lighting/pbr-model.cjs +579 -0
  118. package/dist/lighting/pbr-model.d.ts +45 -0
  119. package/dist/lighting/pbr-model.d.ts.map +1 -0
  120. package/dist/lighting/pbr-model.js +545 -0
  121. package/dist/lighting/pbr-model.js.map +1 -0
  122. package/dist/lighting/pbr-surface-pass.cjs +116 -0
  123. package/dist/lighting/pbr-surface-pass.d.ts +35 -0
  124. package/dist/lighting/pbr-surface-pass.d.ts.map +1 -0
  125. package/dist/lighting/pbr-surface-pass.js +98 -0
  126. package/dist/lighting/pbr-surface-pass.js.map +1 -0
  127. package/dist/lighting/prefiltered-environment.cjs +275 -0
  128. package/dist/lighting/prefiltered-environment.d.ts +56 -0
  129. package/dist/lighting/prefiltered-environment.d.ts.map +1 -0
  130. package/dist/lighting/prefiltered-environment.js +264 -0
  131. package/dist/lighting/prefiltered-environment.js.map +1 -0
  132. package/dist/lighting/shadow-map-renderer.cjs +631 -0
  133. package/dist/lighting/shadow-map-renderer.d.ts +92 -0
  134. package/dist/lighting/shadow-map-renderer.d.ts.map +1 -0
  135. package/dist/lighting/shadow-map-renderer.js +622 -0
  136. package/dist/lighting/shadow-map-renderer.js.map +1 -0
  137. package/dist/lighting/shadow-quality.cjs +58 -0
  138. package/dist/lighting/shadow-quality.d.ts +13 -0
  139. package/dist/lighting/shadow-quality.d.ts.map +1 -0
  140. package/dist/lighting/shadow-quality.js +37 -0
  141. package/dist/lighting/shadow-quality.js.map +1 -0
  142. package/dist/material/material.cjs +3 -0
  143. package/dist/material/material.d.ts +2 -0
  144. package/dist/material/material.d.ts.map +1 -1
  145. package/dist/material/material.js +3 -0
  146. package/dist/material/material.js.map +1 -1
  147. package/dist/model/gpu-vector-model.cjs +11 -1
  148. package/dist/model/gpu-vector-model.d.ts +4 -2
  149. package/dist/model/gpu-vector-model.d.ts.map +1 -1
  150. package/dist/model/gpu-vector-model.js +12 -1
  151. package/dist/model/gpu-vector-model.js.map +1 -1
  152. package/dist/scenegraph/controllers/punctual-light-controller.cjs +121 -0
  153. package/dist/scenegraph/controllers/punctual-light-controller.d.ts +31 -0
  154. package/dist/scenegraph/controllers/punctual-light-controller.d.ts.map +1 -0
  155. package/dist/scenegraph/controllers/punctual-light-controller.js +117 -0
  156. package/dist/scenegraph/controllers/punctual-light-controller.js.map +1 -0
  157. package/dist/scenegraph/scene-metadata.cjs +43 -0
  158. package/dist/scenegraph/scene-metadata.d.ts +9 -0
  159. package/dist/scenegraph/scene-metadata.d.ts.map +1 -0
  160. package/dist/scenegraph/scene-metadata.js +21 -0
  161. package/dist/scenegraph/scene-metadata.js.map +1 -0
  162. package/dist/scenegraph/scenegraph-interaction.cjs +99 -0
  163. package/dist/scenegraph/scenegraph-interaction.d.ts +47 -0
  164. package/dist/scenegraph/scenegraph-interaction.d.ts.map +1 -0
  165. package/dist/scenegraph/scenegraph-interaction.js +79 -0
  166. package/dist/scenegraph/scenegraph-interaction.js.map +1 -0
  167. package/dist/scenegraph/scenegraph-node.cjs +10 -0
  168. package/dist/scenegraph/scenegraph-node.d.ts +10 -0
  169. package/dist/scenegraph/scenegraph-node.d.ts.map +1 -1
  170. package/dist/scenegraph/scenegraph-node.js +10 -0
  171. package/dist/scenegraph/scenegraph-node.js.map +1 -1
  172. package/dist/text/experimental.cjs.map +1 -1
  173. package/dist/text/fonts/atlas/text-utils.cjs +1 -1
  174. package/dist/text/fonts/atlas/text-utils.js +1 -1
  175. package/dist/text/fonts/atlas/text-utils.js.map +1 -1
  176. package/dist/text/fonts/index.cjs.map +1 -1
  177. package/dist/text/index.cjs.map +1 -1
  178. package/dist/text/text-2d/models/text-attribute-model.cjs +1 -1
  179. package/dist/text/text-2d/models/text-attribute-model.js +1 -1
  180. package/dist/text/text-2d/models/text-attribute-model.js.map +1 -1
  181. package/dist/text/text-2d/models/text-dictionary-model.cjs +1 -1
  182. package/dist/text/text-2d/models/text-dictionary-model.js +1 -1
  183. package/dist/text/text-2d/models/text-dictionary-model.js.map +1 -1
  184. package/dist/text/text-2d/models/text-storage-model.cjs +1 -1
  185. package/dist/text/text-2d/models/text-storage-model.js +1 -1
  186. package/dist/text/text-2d/models/text-storage-model.js.map +1 -1
  187. package/package.json +13 -8
  188. package/src/animation/node-animation-binding.ts +18 -1
  189. package/src/gltf/gltf/animations/animations.ts +8 -1
  190. package/src/gltf/gltf/create-scenegraph-from-gltf.ts +98 -17
  191. package/src/gltf/gltf/gltf-billboard.ts +6 -1
  192. package/src/gltf/gltf/gltf-extension-support.ts +2 -2
  193. package/src/gltf/index.ts +1 -0
  194. package/src/gltf/parsers/parse-gltf-animations.ts +22 -8
  195. package/src/gltf/parsers/parse-gltf-lights.ts +42 -130
  196. package/src/gltf/parsers/parse-gltf-metadata.ts +40 -0
  197. package/src/gltf/parsers/parse-gltf.ts +5 -0
  198. package/src/gltf/pbr/pbr-environment.ts +27 -1
  199. package/src/graph/models/path/path-attribute-model.ts +1 -1
  200. package/src/graph/models/path/path-storage-model.ts +1 -1
  201. package/src/index.ts +16 -0
  202. package/src/lighting/ambient-occlusion-pass.ts +130 -0
  203. package/src/lighting/clustered-light-grid.ts +481 -0
  204. package/src/lighting/contact-shadow-composite.ts +67 -0
  205. package/src/lighting/contact-shadow-pass.ts +107 -0
  206. package/src/lighting/index.ts +107 -0
  207. package/src/lighting/lightmap-baker.ts +306 -0
  208. package/src/lighting/lightmap-data.ts +138 -0
  209. package/src/lighting/lightmap-environment.ts +70 -0
  210. package/src/lighting/pbr-environment.ts +55 -0
  211. package/src/lighting/pbr-material.ts +85 -0
  212. package/src/lighting/pbr-model.ts +608 -0
  213. package/src/lighting/pbr-surface-pass.ts +122 -0
  214. package/src/lighting/prefiltered-environment.ts +354 -0
  215. package/src/lighting/shadow-map-renderer.ts +918 -0
  216. package/src/lighting/shadow-quality.ts +50 -0
  217. package/src/material/material.ts +3 -0
  218. package/src/model/gpu-vector-model.ts +18 -3
  219. package/src/scenegraph/controllers/punctual-light-controller.ts +172 -0
  220. package/src/scenegraph/scene-metadata.ts +23 -0
  221. package/src/scenegraph/scenegraph-interaction.ts +131 -0
  222. package/src/scenegraph/scenegraph-node.ts +12 -0
  223. package/src/text/fonts/atlas/text-utils.ts +1 -1
  224. package/src/text/text-2d/models/text-attribute-model.ts +1 -1
  225. package/src/text/text-2d/models/text-dictionary-model.ts +1 -1
  226. package/src/text/text-2d/models/text-storage-model.ts +1 -1
@@ -0,0 +1,122 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
3
+
4
+ import {assert, Texture, type Device, type Framebuffer, type RenderPass} from '@luma.gl/core';
5
+
6
+ export type PBRSurfacePassProps = {width: number; height: number};
7
+
8
+ export type PBRSurfaceRenderView = PBRSurfacePassProps & {
9
+ framebuffer: Framebuffer;
10
+ renderPass: RenderPass;
11
+ };
12
+
13
+ export type PBRSurfaceRenderOptions = {
14
+ /** Create or upload per-object uniform storage before the render pass begins. */
15
+ prepareSurfaces?: (view: Omit<PBRSurfaceRenderView, 'renderPass'>) => void;
16
+ /** Record caller-selected surface model draws. The pass is ended by the owner. */
17
+ drawSurfaces: (view: PBRSurfaceRenderView) => void;
18
+ };
19
+
20
+ type SurfaceResources = PBRSurfacePassProps & {
21
+ normalTexture: Texture;
22
+ depthTexture: Texture;
23
+ framebuffer: Framebuffer;
24
+ };
25
+
26
+ /** Owns a single-sample camera depth/normal target, without scene traversal or submission. */
27
+ export class PBRSurfacePass {
28
+ readonly device: Device;
29
+ private resources: SurfaceResources;
30
+ private destroyed = false;
31
+
32
+ constructor(device: Device, props: PBRSurfacePassProps) {
33
+ this.device = device;
34
+ this.resources = createSurfaceResources(device, props);
35
+ }
36
+
37
+ get width(): number {
38
+ return this.resources.width;
39
+ }
40
+ get height(): number {
41
+ return this.resources.height;
42
+ }
43
+ /** Borrowed rgba8unorm encoded view normals; alpha zero denotes background. */
44
+ get normalTexture(): Texture {
45
+ return this.resources.normalTexture;
46
+ }
47
+ /** Borrowed depth32float hardware depth in [0,1], cleared to one. */
48
+ get depthTexture(): Texture {
49
+ return this.resources.depthTexture;
50
+ }
51
+ get framebuffer(): Framebuffer {
52
+ return this.resources.framebuffer;
53
+ }
54
+
55
+ /** Replaces and destroys old attachments only when dimensions change. Rebind consumers afterward. */
56
+ resize(props: PBRSurfacePassProps): void {
57
+ // A destroyed surface pass cannot be reused.
58
+ assert(!this.destroyed);
59
+ if (props.width === this.width && props.height === this.height) return;
60
+ const resources = createSurfaceResources(this.device, props);
61
+ destroySurfaceResources(this.resources);
62
+ this.resources = resources;
63
+ }
64
+
65
+ /** Records camera-surface draws and ends the pass even if a callback throws. Never submits. */
66
+ render(options: PBRSurfaceRenderOptions): void {
67
+ // A destroyed surface pass cannot be reused.
68
+ assert(!this.destroyed);
69
+ const view = {width: this.width, height: this.height, framebuffer: this.framebuffer};
70
+ options.prepareSurfaces?.(view);
71
+ const renderPass = this.device.beginRenderPass({
72
+ framebuffer: this.framebuffer,
73
+ clearColor: [0.5, 0.5, 1, 0],
74
+ clearDepth: 1
75
+ });
76
+ try {
77
+ renderPass.setParameters({viewport: [0, 0, this.width, this.height]});
78
+ options.drawSurfaces({...view, renderPass});
79
+ } finally {
80
+ renderPass.end();
81
+ }
82
+ }
83
+
84
+ destroy(): void {
85
+ if (this.destroyed) return;
86
+ this.destroyed = true;
87
+ destroySurfaceResources(this.resources);
88
+ }
89
+ }
90
+
91
+ function createSurfaceResources(device: Device, props: PBRSurfacePassProps): SurfaceResources {
92
+ const {width, height} = props;
93
+ // Surface targets require positive integer dimensions; the device validates its own limits.
94
+ assert(Number.isInteger(width) && width > 0 && Number.isInteger(height) && height > 0);
95
+ let normalTexture: Texture | undefined;
96
+ let depthTexture: Texture | undefined;
97
+ let framebuffer: Framebuffer | undefined;
98
+ try {
99
+ const usage = Texture.RENDER_ATTACHMENT | Texture.SAMPLE | Texture.COPY_SRC;
100
+ const sampler = {minFilter: 'nearest', magFilter: 'nearest'} as const;
101
+ normalTexture = device.createTexture({width, height, format: 'rgba8unorm', usage, sampler});
102
+ depthTexture = device.createTexture({width, height, format: 'depth32float', usage, sampler});
103
+ framebuffer = device.createFramebuffer({
104
+ width,
105
+ height,
106
+ colorAttachments: [normalTexture],
107
+ depthStencilAttachment: depthTexture
108
+ });
109
+ return {width, height, normalTexture, depthTexture, framebuffer};
110
+ } catch (error) {
111
+ framebuffer?.destroy();
112
+ depthTexture?.destroy();
113
+ normalTexture?.destroy();
114
+ throw error;
115
+ }
116
+ }
117
+
118
+ function destroySurfaceResources(resources: SurfaceResources): void {
119
+ resources.framebuffer.destroy();
120
+ resources.depthTexture.destroy();
121
+ resources.normalTexture.destroy();
122
+ }
@@ -0,0 +1,354 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ assert,
7
+ type Device,
8
+ type Framebuffer,
9
+ Texture,
10
+ type TextureFormatColor,
11
+ type TextureView
12
+ } from '@luma.gl/core';
13
+ import {getPBREnvironmentBindings} from './pbr-environment';
14
+ import {ClipSpace} from '../models/clip-space';
15
+ import {ShaderInputs} from '../shader-inputs';
16
+ import {
17
+ PBR_ENVIRONMENT_FRAGMENT_GLSL,
18
+ PBR_ENVIRONMENT_FRAGMENT_WGSL,
19
+ pbrEnvironmentFilter
20
+ } from '@luma.gl/shaders';
21
+
22
+ /** Source and quality controls for preparing a portable physically based lighting environment. */
23
+ export type PrefilteredEnvironmentOptions = {
24
+ /** Caller-owned linear HDR or sRGB equirectangular source texture. */
25
+ source: Texture;
26
+ /** Source color encoding. HDR and floating-point sources are linear by default. */
27
+ sourceEncoding?: 'linear' | 'srgb';
28
+ /** Base dimension of the GGX-prefiltered specular cubemap. Defaults to 64 pixels. */
29
+ size?: number;
30
+ /** Dimension of the cosine-weighted diffuse irradiance cubemap. Defaults to 16 pixels. */
31
+ irradianceSize?: number;
32
+ /** Dimension of the integrated split-sum BRDF lookup texture. Defaults to 128 pixels. */
33
+ brdfLUTSize?: number;
34
+ /** Number of importance samples for each destination texel. Defaults to 64. */
35
+ sampleCount?: number;
36
+ /** Renderable and filterable output format. Defaults to rgba16float; LDR fallback is explicit. */
37
+ format?: TextureFormatColor;
38
+ /** Borrow a compatible split-sum lookup. Its lifetime remains caller-owned. */
39
+ brdfLUTTexture?: Texture;
40
+ };
41
+
42
+ /** Generated linear-light textures. Diffuse stores irradiance divided by pi. */
43
+ export type PrefilteredEnvironmentTextures = {
44
+ diffuseTexture: Texture;
45
+ specularTexture: Texture;
46
+ brdfLUTTexture: Texture;
47
+ };
48
+
49
+ /** Owns generated resources independently of the generator and source texture. */
50
+ export class PrefilteredEnvironment {
51
+ readonly textures: PrefilteredEnvironmentTextures;
52
+ readonly ownsBrdfLUTTexture: boolean;
53
+ private destroyed = false;
54
+
55
+ constructor(textures: PrefilteredEnvironmentTextures, ownsBrdfLUTTexture = true) {
56
+ this.textures = textures;
57
+ this.ownsBrdfLUTTexture = ownsBrdfLUTTexture;
58
+ }
59
+
60
+ /** Returns the texture bindings declared by the IBL shader module. */
61
+ getBindings() {
62
+ return getPBREnvironmentBindings(this.textures);
63
+ }
64
+
65
+ destroy(): void {
66
+ if (this.destroyed) return;
67
+ this.destroyed = true;
68
+ this.textures.diffuseTexture.destroy();
69
+ this.textures.specularTexture.destroy();
70
+ if (this.ownsBrdfLUTTexture) this.textures.brdfLUTTexture.destroy();
71
+ }
72
+ }
73
+
74
+ /** Reusable WebGL/WebGPU equirectangular-to-IBL importance-sampling pipeline. */
75
+ export class PrefilteredEnvironmentGenerator {
76
+ readonly device: Device;
77
+
78
+ private model: ClipSpace | undefined;
79
+ private modelFormat: TextureFormatColor | undefined;
80
+
81
+ constructor(device: Device) {
82
+ this.device = device;
83
+ }
84
+
85
+ /** Integrates all six cube faces, every roughness mip, diffuse irradiance, and the BRDF LUT. */
86
+ prepare(options: PrefilteredEnvironmentOptions): PrefilteredEnvironment {
87
+ // Equirectangular environment sources require ordinary two-dimensional texture coordinates.
88
+ assert(
89
+ options.source.dimension === '2d' &&
90
+ options.source.device === this.device &&
91
+ !options.source.destroyed
92
+ );
93
+ const size = Math.max(1, Math.floor(options.size ?? 64));
94
+ const irradianceSize = Math.max(1, Math.floor(options.irradianceSize ?? 16));
95
+ const brdfLUTSize = Math.max(1, Math.floor(options.brdfLUTSize ?? 128));
96
+ const sampleCount = Math.max(1, Math.min(Math.floor(options.sampleCount ?? 64), 1024));
97
+ const format = options.format ?? 'rgba16float';
98
+ const capabilities = this.device.getTextureFormatCapabilities(format);
99
+ // Never silently clip HDR radiance to an unorm format.
100
+ assert(capabilities.render && capabilities.filter && !format.endsWith('-srgb'));
101
+ if (options.brdfLUTTexture) {
102
+ // Borrowed LUTs must use the same device and the documented split-sum convention.
103
+ assert(
104
+ options.brdfLUTTexture.device === this.device &&
105
+ !options.brdfLUTTexture.destroyed &&
106
+ options.brdfLUTTexture.dimension === '2d'
107
+ );
108
+ }
109
+ const mipLevels = this.device.getMipLevelCount(size, size);
110
+
111
+ const allocatedTextures: Texture[] = [];
112
+ const framebuffers: Framebuffer[] = [];
113
+ const views: TextureView[] = [];
114
+ const allocateTexture = (properties: Parameters<typeof createEnvironmentTexture>[1]) => {
115
+ const texture = createEnvironmentTexture(this.device, properties);
116
+ allocatedTextures.push(texture);
117
+ return texture;
118
+ };
119
+ try {
120
+ const specularTexture = allocateTexture({
121
+ id: 'pbr-environment-specular',
122
+ dimension: 'cube',
123
+ size,
124
+ mipLevels,
125
+ format
126
+ });
127
+ const diffuseTexture = allocateTexture({
128
+ id: 'pbr-environment-diffuse',
129
+ dimension: 'cube',
130
+ size: irradianceSize,
131
+ mipLevels: 1,
132
+ format
133
+ });
134
+ const brdfLUTTexture =
135
+ options.brdfLUTTexture ??
136
+ allocateTexture({
137
+ id: 'pbr-environment-brdf-lut',
138
+ dimension: '2d',
139
+ size: brdfLUTSize,
140
+ mipLevels: 1,
141
+ format
142
+ });
143
+
144
+ const model = this.getFilterModel(format);
145
+ const sourceEncoding =
146
+ options.sourceEncoding === 'srgb' && !options.source.format.endsWith('-srgb') ? 1 : 0;
147
+
148
+ for (let mipLevel = 0; mipLevel < mipLevels; mipLevel++) {
149
+ const mipSize = Math.max(1, size >> mipLevel);
150
+ const roughness = mipLevels > 1 ? mipLevel / (mipLevels - 1) : 0;
151
+ for (let face = 0; face < 6; face++) {
152
+ this.drawEnvironmentView({
153
+ texture: specularTexture,
154
+ size: mipSize,
155
+ mipLevel,
156
+ face,
157
+ roughness,
158
+ mode: 0,
159
+ source: options.source,
160
+ sourceEncoding,
161
+ sampleCount,
162
+ model,
163
+ framebuffers,
164
+ views
165
+ });
166
+ }
167
+ }
168
+
169
+ for (let face = 0; face < 6; face++) {
170
+ this.drawEnvironmentView({
171
+ texture: diffuseTexture,
172
+ size: irradianceSize,
173
+ mipLevel: 0,
174
+ face,
175
+ roughness: 1,
176
+ mode: 1,
177
+ source: options.source,
178
+ sourceEncoding,
179
+ sampleCount,
180
+ model,
181
+ framebuffers,
182
+ views
183
+ });
184
+ }
185
+
186
+ if (!options.brdfLUTTexture)
187
+ this.drawEnvironmentView({
188
+ texture: brdfLUTTexture,
189
+ size: brdfLUTSize,
190
+ mipLevel: 0,
191
+ face: 0,
192
+ roughness: 0,
193
+ mode: 2,
194
+ source: options.source,
195
+ sourceEncoding,
196
+ sampleCount,
197
+ model,
198
+ framebuffers,
199
+ views
200
+ });
201
+
202
+ // Queue order makes subsequent sampling safe; this is not a GPU completion wait.
203
+ this.device.submit();
204
+
205
+ return new PrefilteredEnvironment(
206
+ {
207
+ diffuseTexture,
208
+ specularTexture,
209
+ brdfLUTTexture
210
+ },
211
+ !options.brdfLUTTexture
212
+ );
213
+ } catch (error) {
214
+ for (const texture of allocatedTextures) texture.destroy();
215
+ throw error;
216
+ } finally {
217
+ for (const framebuffer of framebuffers) {
218
+ framebuffer.destroy();
219
+ }
220
+ for (const view of views) {
221
+ view.destroy();
222
+ }
223
+ }
224
+ }
225
+
226
+ /** Releases the reusable integration pipeline without destroying generated environments. */
227
+ destroy(): void {
228
+ this.model?.destroy();
229
+ this.model = undefined;
230
+ this.modelFormat = undefined;
231
+ }
232
+
233
+ private getFilterModel(format: TextureFormatColor): ClipSpace {
234
+ if (this.model && this.modelFormat !== format) {
235
+ this.model.destroy();
236
+ this.model = undefined;
237
+ }
238
+
239
+ if (!this.model) {
240
+ this.model = new ClipSpace(this.device, {
241
+ id: 'pbr-environment-integration-model',
242
+ fs: PBR_ENVIRONMENT_FRAGMENT_GLSL,
243
+ source: PBR_ENVIRONMENT_FRAGMENT_WGSL,
244
+ modules: [pbrEnvironmentFilter],
245
+ shaderInputs: new ShaderInputs({pbrEnvironmentFilter}),
246
+ colorAttachmentFormats: [format],
247
+ parameters: {depthWriteEnabled: false}
248
+ });
249
+ this.modelFormat = format;
250
+ }
251
+
252
+ return this.model;
253
+ }
254
+
255
+ private drawEnvironmentView(options: {
256
+ texture: Texture;
257
+ size: number;
258
+ mipLevel: number;
259
+ face: number;
260
+ roughness: number;
261
+ mode: number;
262
+ source: Texture;
263
+ sourceEncoding: number;
264
+ sampleCount: number;
265
+ model: ClipSpace;
266
+ framebuffers: Framebuffer[];
267
+ views: TextureView[];
268
+ }): void {
269
+ const view = options.texture.createView({
270
+ id: `${options.texture.id}-${options.face}-${options.mipLevel}`,
271
+ dimension: '2d',
272
+ baseMipLevel: options.mipLevel,
273
+ mipLevelCount: 1,
274
+ baseArrayLayer: options.face,
275
+ arrayLayerCount: 1
276
+ });
277
+ options.views.push(view);
278
+
279
+ const framebuffer = this.device.createFramebuffer({
280
+ id: `${view.id}-framebuffer`,
281
+ width: options.size,
282
+ height: options.size,
283
+ colorAttachments: [view],
284
+ depthStencilAttachment: null
285
+ });
286
+ options.framebuffers.push(framebuffer);
287
+
288
+ options.model.shaderInputs.setProps({
289
+ pbrEnvironmentFilter: {
290
+ face: options.face,
291
+ roughness: options.roughness,
292
+ mode: options.mode,
293
+ sampleCount: options.sampleCount,
294
+ sourceEncoding: options.sourceEncoding,
295
+ pbrEnvironmentSource: options.source
296
+ }
297
+ });
298
+ options.model.predraw(this.device.commandEncoder);
299
+
300
+ const renderPass = this.device.beginRenderPass({
301
+ id: `${view.id}-integration`,
302
+ framebuffer,
303
+ clearColor: [0, 0, 0, 1],
304
+ clearDepth: false
305
+ });
306
+ try {
307
+ options.model.draw(renderPass);
308
+ } finally {
309
+ renderPass.end();
310
+ }
311
+ }
312
+ }
313
+
314
+ /** Prepares one caller-owned lighting environment without retaining the integration pipeline. */
315
+ export function createPrefilteredEnvironment(
316
+ device: Device,
317
+ options: PrefilteredEnvironmentOptions
318
+ ): PrefilteredEnvironment {
319
+ const generator = new PrefilteredEnvironmentGenerator(device);
320
+ try {
321
+ return generator.prepare(options);
322
+ } finally {
323
+ generator.destroy();
324
+ }
325
+ }
326
+
327
+ function createEnvironmentTexture(
328
+ device: Device,
329
+ options: {
330
+ id: string;
331
+ dimension: '2d' | 'cube';
332
+ size: number;
333
+ mipLevels: number;
334
+ format: TextureFormatColor;
335
+ }
336
+ ): Texture {
337
+ return device.createTexture({
338
+ id: options.id,
339
+ dimension: options.dimension,
340
+ width: options.size,
341
+ height: options.size,
342
+ depth: options.dimension === 'cube' ? 6 : 1,
343
+ mipLevels: options.mipLevels,
344
+ format: options.format,
345
+ usage: Texture.SAMPLE | Texture.RENDER | Texture.COPY_SRC,
346
+ sampler: {
347
+ minFilter: 'linear',
348
+ magFilter: 'linear',
349
+ mipmapFilter: 'linear',
350
+ addressModeU: 'clamp-to-edge',
351
+ addressModeV: 'clamp-to-edge'
352
+ }
353
+ });
354
+ }