@babylonjs/core 7.32.3 → 7.32.5

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 (171) hide show
  1. package/Decorators/nodeDecorator.d.ts +4 -0
  2. package/Decorators/nodeDecorator.js.map +1 -1
  3. package/Engines/abstractEngine.js +2 -2
  4. package/Engines/abstractEngine.js.map +1 -1
  5. package/Materials/Node/Blocks/Dual/sceneDepthBlock.js +1 -0
  6. package/Materials/Node/Blocks/Dual/sceneDepthBlock.js.map +1 -1
  7. package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js +4 -4
  8. package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js.map +1 -1
  9. package/Materials/Node/Blocks/PBR/clearCoatBlock.js +1 -1
  10. package/Materials/Node/Blocks/PBR/clearCoatBlock.js.map +1 -1
  11. package/Materials/Node/Blocks/PBR/reflectionBlock.js +2 -2
  12. package/Materials/Node/Blocks/PBR/reflectionBlock.js.map +1 -1
  13. package/Materials/Node/Blocks/PBR/refractionBlock.js +3 -3
  14. package/Materials/Node/Blocks/PBR/refractionBlock.js.map +1 -1
  15. package/Materials/Node/Blocks/PBR/sheenBlock.js +2 -2
  16. package/Materials/Node/Blocks/PBR/sheenBlock.js.map +1 -1
  17. package/Materials/Node/Blocks/clampBlock.js +2 -2
  18. package/Materials/Node/Blocks/clampBlock.js.map +1 -1
  19. package/Materials/Node/Blocks/cloudBlock.js +1 -1
  20. package/Materials/Node/Blocks/cloudBlock.js.map +1 -1
  21. package/Materials/Node/Blocks/conditionalBlock.js +19 -0
  22. package/Materials/Node/Blocks/conditionalBlock.js.map +1 -1
  23. package/Materials/Node/Blocks/curveBlock.js +37 -0
  24. package/Materials/Node/Blocks/curveBlock.js.map +1 -1
  25. package/Materials/Node/Blocks/loopBlock.js +1 -1
  26. package/Materials/Node/Blocks/loopBlock.js.map +1 -1
  27. package/Materials/Node/Blocks/meshAttributeExistsBlock.js +1 -0
  28. package/Materials/Node/Blocks/meshAttributeExistsBlock.js.map +1 -1
  29. package/Materials/Node/Blocks/transformBlock.d.ts +6 -0
  30. package/Materials/Node/Blocks/transformBlock.js +16 -1
  31. package/Materials/Node/Blocks/transformBlock.js.map +1 -1
  32. package/Materials/Node/Blocks/triPlanarBlock.js +1 -1
  33. package/Materials/Node/Blocks/triPlanarBlock.js.map +1 -1
  34. package/Materials/Node/Blocks/trigonometryBlock.js +29 -0
  35. package/Materials/Node/Blocks/trigonometryBlock.js.map +1 -1
  36. package/Materials/Node/Blocks/waveBlock.js +13 -0
  37. package/Materials/Node/Blocks/waveBlock.js.map +1 -1
  38. package/Materials/Node/Blocks/worleyNoise3DBlock.js +1 -1
  39. package/Materials/Node/Blocks/worleyNoise3DBlock.js.map +1 -1
  40. package/Meshes/Node/Blocks/Instances/instantiateBaseBlock.js +1 -1
  41. package/Meshes/Node/Blocks/Instances/instantiateBaseBlock.js.map +1 -1
  42. package/Meshes/Node/Blocks/Set/aggregatorBlock.js +1 -0
  43. package/Meshes/Node/Blocks/Set/aggregatorBlock.js.map +1 -1
  44. package/Meshes/Node/Blocks/Set/latticeBlock.js +4 -4
  45. package/Meshes/Node/Blocks/Set/latticeBlock.js.map +1 -1
  46. package/Meshes/Node/Blocks/Set/setColorsBlock.js +1 -1
  47. package/Meshes/Node/Blocks/Set/setColorsBlock.js.map +1 -1
  48. package/Meshes/Node/Blocks/Set/setMaterialIDBlock.js +1 -1
  49. package/Meshes/Node/Blocks/Set/setMaterialIDBlock.js.map +1 -1
  50. package/Meshes/Node/Blocks/Set/setNormalsBlock.js +1 -1
  51. package/Meshes/Node/Blocks/Set/setNormalsBlock.js.map +1 -1
  52. package/Meshes/Node/Blocks/Set/setPositionsBlock.js +1 -1
  53. package/Meshes/Node/Blocks/Set/setPositionsBlock.js.map +1 -1
  54. package/Meshes/Node/Blocks/Set/setTangentsBlock.js +1 -1
  55. package/Meshes/Node/Blocks/Set/setTangentsBlock.js.map +1 -1
  56. package/Meshes/Node/Blocks/Set/setUVsBlock.js +2 -1
  57. package/Meshes/Node/Blocks/Set/setUVsBlock.js.map +1 -1
  58. package/Meshes/Node/Blocks/Sources/boxBlock.js +1 -1
  59. package/Meshes/Node/Blocks/Sources/boxBlock.js.map +1 -1
  60. package/Meshes/Node/Blocks/Sources/capsuleBlock.js +1 -1
  61. package/Meshes/Node/Blocks/Sources/capsuleBlock.js.map +1 -1
  62. package/Meshes/Node/Blocks/Sources/cylinderBlock.js +1 -1
  63. package/Meshes/Node/Blocks/Sources/cylinderBlock.js.map +1 -1
  64. package/Meshes/Node/Blocks/Sources/discBlock.js +1 -1
  65. package/Meshes/Node/Blocks/Sources/discBlock.js.map +1 -1
  66. package/Meshes/Node/Blocks/Sources/gridBlock.js +1 -1
  67. package/Meshes/Node/Blocks/Sources/gridBlock.js.map +1 -1
  68. package/Meshes/Node/Blocks/Sources/icoSphereBlock.js +1 -1
  69. package/Meshes/Node/Blocks/Sources/icoSphereBlock.js.map +1 -1
  70. package/Meshes/Node/Blocks/Sources/meshBlock.js +1 -1
  71. package/Meshes/Node/Blocks/Sources/meshBlock.js.map +1 -1
  72. package/Meshes/Node/Blocks/Sources/planeBlock.js +1 -1
  73. package/Meshes/Node/Blocks/Sources/planeBlock.js.map +1 -1
  74. package/Meshes/Node/Blocks/Sources/sphereBlock.js +1 -1
  75. package/Meshes/Node/Blocks/Sources/sphereBlock.js.map +1 -1
  76. package/Meshes/Node/Blocks/Sources/torusBlock.js +1 -1
  77. package/Meshes/Node/Blocks/Sources/torusBlock.js.map +1 -1
  78. package/Meshes/Node/Blocks/Textures/geometryTextureBlock.js +1 -1
  79. package/Meshes/Node/Blocks/Textures/geometryTextureBlock.js.map +1 -1
  80. package/Meshes/Node/Blocks/Textures/geometryTextureFetchBlock.js +1 -1
  81. package/Meshes/Node/Blocks/Textures/geometryTextureFetchBlock.js.map +1 -1
  82. package/Meshes/Node/Blocks/booleanGeometryBlock.d.ts +5 -0
  83. package/Meshes/Node/Blocks/booleanGeometryBlock.js +43 -13
  84. package/Meshes/Node/Blocks/booleanGeometryBlock.js.map +1 -1
  85. package/Meshes/Node/Blocks/cleanGeometryBlock.js +1 -1
  86. package/Meshes/Node/Blocks/cleanGeometryBlock.js.map +1 -1
  87. package/Meshes/Node/Blocks/conditionBlock.js +1 -0
  88. package/Meshes/Node/Blocks/conditionBlock.js.map +1 -1
  89. package/Meshes/Node/Blocks/geometryClampBlock.js +2 -2
  90. package/Meshes/Node/Blocks/geometryClampBlock.js.map +1 -1
  91. package/Meshes/Node/Blocks/geometryCollectionBlock.js +1 -1
  92. package/Meshes/Node/Blocks/geometryCollectionBlock.js.map +1 -1
  93. package/Meshes/Node/Blocks/geometryCurveBlock.js +1 -0
  94. package/Meshes/Node/Blocks/geometryCurveBlock.js.map +1 -1
  95. package/Meshes/Node/Blocks/geometryOptimizeBlock.js +3 -3
  96. package/Meshes/Node/Blocks/geometryOptimizeBlock.js.map +1 -1
  97. package/Meshes/Node/Blocks/geometryTransformBlock.js +1 -1
  98. package/Meshes/Node/Blocks/geometryTransformBlock.js.map +1 -1
  99. package/Meshes/Node/Blocks/geometryTrigonometryBlock.js +1 -0
  100. package/Meshes/Node/Blocks/geometryTrigonometryBlock.js.map +1 -1
  101. package/Meshes/Node/Blocks/mappingBlock.js +1 -0
  102. package/Meshes/Node/Blocks/mappingBlock.js.map +1 -1
  103. package/Meshes/Node/Blocks/mathBlock.js +1 -0
  104. package/Meshes/Node/Blocks/mathBlock.js.map +1 -1
  105. package/Meshes/Node/Blocks/mergeGeometryBlock.js +1 -1
  106. package/Meshes/Node/Blocks/mergeGeometryBlock.js.map +1 -1
  107. package/Meshes/Node/Blocks/randomBlock.js +1 -0
  108. package/Meshes/Node/Blocks/randomBlock.js.map +1 -1
  109. package/Misc/webRequest.d.ts +4 -0
  110. package/Misc/webRequest.js +6 -0
  111. package/Misc/webRequest.js.map +1 -1
  112. package/Rendering/IBLShadows/iblShadowsAccumulationPass.d.ts +35 -11
  113. package/Rendering/IBLShadows/iblShadowsAccumulationPass.js +131 -117
  114. package/Rendering/IBLShadows/iblShadowsAccumulationPass.js.map +1 -1
  115. package/Rendering/IBLShadows/iblShadowsImportanceSamplingRenderer.js +0 -1
  116. package/Rendering/IBLShadows/iblShadowsImportanceSamplingRenderer.js.map +1 -1
  117. package/Rendering/IBLShadows/iblShadowsPluginMaterial.d.ts +60 -0
  118. package/Rendering/IBLShadows/iblShadowsPluginMaterial.js +166 -0
  119. package/Rendering/IBLShadows/iblShadowsPluginMaterial.js.map +1 -0
  120. package/Rendering/IBLShadows/iblShadowsRenderPipeline.d.ts +97 -56
  121. package/Rendering/IBLShadows/iblShadowsRenderPipeline.js +356 -308
  122. package/Rendering/IBLShadows/iblShadowsRenderPipeline.js.map +1 -1
  123. package/Rendering/IBLShadows/iblShadowsSpatialBlurPass.d.ts +20 -6
  124. package/Rendering/IBLShadows/iblShadowsSpatialBlurPass.js +67 -35
  125. package/Rendering/IBLShadows/iblShadowsSpatialBlurPass.js.map +1 -1
  126. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.d.ts +9 -4
  127. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.js +84 -66
  128. package/Rendering/IBLShadows/iblShadowsVoxelRenderer.js.map +1 -1
  129. package/Rendering/IBLShadows/iblShadowsVoxelTracingPass.d.ts +31 -6
  130. package/Rendering/IBLShadows/iblShadowsVoxelTracingPass.js +108 -60
  131. package/Rendering/IBLShadows/iblShadowsVoxelTracingPass.js.map +1 -1
  132. package/Rendering/geometryBufferRenderer.d.ts +15 -0
  133. package/Rendering/geometryBufferRenderer.js +48 -5
  134. package/Rendering/geometryBufferRenderer.js.map +1 -1
  135. package/Shaders/ShadersInclude/instancesDeclaration.js +2 -4
  136. package/Shaders/ShadersInclude/instancesDeclaration.js.map +1 -1
  137. package/Shaders/ShadersInclude/instancesVertex.js +3 -6
  138. package/Shaders/ShadersInclude/instancesVertex.js.map +1 -1
  139. package/Shaders/geometry.fragment.js +5 -1
  140. package/Shaders/geometry.fragment.js.map +1 -1
  141. package/Shaders/geometry.vertex.js +3 -3
  142. package/Shaders/geometry.vertex.js.map +1 -1
  143. package/Shaders/iblShadowAccumulation.fragment.js +7 -11
  144. package/Shaders/iblShadowAccumulation.fragment.js.map +1 -1
  145. package/Shaders/iblShadowGBufferDebug.fragment.js +2 -3
  146. package/Shaders/iblShadowGBufferDebug.fragment.js.map +1 -1
  147. package/Shaders/iblShadowSpatialBlur.fragment.js +5 -5
  148. package/Shaders/iblShadowSpatialBlur.fragment.js.map +1 -1
  149. package/Shaders/iblShadowVoxelTracing.fragment.js +31 -32
  150. package/Shaders/iblShadowVoxelTracing.fragment.js.map +1 -1
  151. package/Shaders/iblShadowsCombine.fragment.js +2 -2
  152. package/Shaders/iblShadowsCombine.fragment.js.map +1 -1
  153. package/ShadersWGSL/ShadersInclude/instancesDeclaration.js +2 -4
  154. package/ShadersWGSL/ShadersInclude/instancesDeclaration.js.map +1 -1
  155. package/ShadersWGSL/ShadersInclude/instancesVertex.js +3 -6
  156. package/ShadersWGSL/ShadersInclude/instancesVertex.js.map +1 -1
  157. package/ShadersWGSL/geometry.fragment.js +7 -1
  158. package/ShadersWGSL/geometry.fragment.js.map +1 -1
  159. package/ShadersWGSL/geometry.vertex.js +3 -3
  160. package/ShadersWGSL/geometry.vertex.js.map +1 -1
  161. package/ShadersWGSL/iblShadowAccumulation.fragment.js +5 -10
  162. package/ShadersWGSL/iblShadowAccumulation.fragment.js.map +1 -1
  163. package/ShadersWGSL/iblShadowGBufferDebug.fragment.js +2 -3
  164. package/ShadersWGSL/iblShadowGBufferDebug.fragment.js.map +1 -1
  165. package/ShadersWGSL/iblShadowSpatialBlur.fragment.js +5 -5
  166. package/ShadersWGSL/iblShadowSpatialBlur.fragment.js.map +1 -1
  167. package/ShadersWGSL/iblShadowVoxelTracing.fragment.js +45 -33
  168. package/ShadersWGSL/iblShadowVoxelTracing.fragment.js.map +1 -1
  169. package/ShadersWGSL/iblShadowsCombine.fragment.js +6 -2
  170. package/ShadersWGSL/iblShadowsCombine.fragment.js.map +1 -1
  171. package/package.json +1 -1
@@ -1,9 +1,7 @@
1
1
 
2
2
  import { EngineStore } from "../../Engines/engineStore.js";
3
3
  import { Matrix, Vector3, Vector4, Quaternion } from "../../Maths/math.vector.js";
4
- import { Mesh } from "../../Meshes/mesh.js";
5
4
  import { Texture } from "../../Materials/Textures/texture.js";
6
- import { PrePassRenderer } from "../prePassRenderer.js";
7
5
  import { Logger } from "../../Misc/logger.js";
8
6
  import { _IblShadowsVoxelRenderer } from "./iblShadowsVoxelRenderer.js";
9
7
  import { _IblShadowsVoxelTracingPass } from "./iblShadowsVoxelTracingPass.js";
@@ -11,61 +9,56 @@ import { PostProcess } from "../../PostProcesses/postProcess.js";
11
9
  import { _IblShadowsImportanceSamplingRenderer } from "./iblShadowsImportanceSamplingRenderer.js";
12
10
  import { _IblShadowsSpatialBlurPass } from "./iblShadowsSpatialBlurPass.js";
13
11
  import { _IblShadowsAccumulationPass } from "./iblShadowsAccumulationPass.js";
14
- import { ArcRotateCamera } from "../../Cameras/arcRotateCamera.js";
15
- import { FreeCamera } from "../../Cameras/freeCamera.js";
16
12
  import { PostProcessRenderPipeline } from "../../PostProcesses/RenderPipeline/postProcessRenderPipeline.js";
17
13
  import { PostProcessRenderEffect } from "../../PostProcesses/RenderPipeline/postProcessRenderEffect.js";
18
- class IblShadowsPrepassConfiguration {
19
- constructor() {
20
- /**
21
- * Is this effect enabled
22
- */
23
- this.enabled = true;
24
- /**
25
- * Name of the configuration
26
- */
27
- this.name = "iblShadows";
28
- /**
29
- * Textures that should be present in the MRT for this effect to work
30
- */
31
- this.texturesRequired = [
32
- 5,
33
- 10,
34
- 8,
35
- // 6, // TODO - don't need this for IBL shadows
36
- 11,
37
- // Local positions used for shadow accumulation pass
38
- 1,
39
- 9,
40
- ];
41
- }
42
- }
14
+ import { GeometryBufferRenderer } from "../geometryBufferRenderer.js";
15
+ import { RawTexture } from "../../Materials/Textures/rawTexture.js";
16
+ import { RawTexture3D } from "../../Materials/Textures/rawTexture3D.js";
17
+ import { Engine } from "../../Engines/engine.js";
18
+ import { IBLShadowsPluginMaterial } from "./iblShadowsPluginMaterial.js";
19
+ import { PBRBaseMaterial } from "../../Materials/PBR/pbrBaseMaterial.js";
20
+ import { StandardMaterial } from "../../Materials/standardMaterial.js";
21
+ import { Observable } from "../../Misc/observable.js";
43
22
  /**
44
23
  * Voxel-based shadow rendering for IBL's.
45
24
  * This should not be instanciated directly, as it is part of a scene component
46
25
  */
47
26
  export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
48
27
  /**
49
- * How dark the shadows appear. 1.0 is full opacity, 0.0 is no shadows.
28
+ * Reset the shadow accumulation.
50
29
  */
51
- get shadowOpacity() {
52
- return this._shadowOpacity;
30
+ resetAccumulation() {
31
+ this._accumulationPass.reset = true;
53
32
  }
54
33
  /**
55
34
  * How dark the shadows appear. 1.0 is full opacity, 0.0 is no shadows.
56
35
  */
36
+ get shadowOpacity() {
37
+ return this._shadowOpacity;
38
+ }
57
39
  set shadowOpacity(value) {
58
40
  this._shadowOpacity = value;
41
+ this._setPluginParameters();
59
42
  }
60
43
  /**
61
- * How dark the voxel shadows appear. 1.0 is full opacity, 0.0 is no shadows.
44
+ * A multiplier for the render size of the shadows. Used for rendering lower-resolution shadows.
62
45
  */
63
- get voxelShadowOpacity() {
64
- return this._voxelTracingPass?.voxelShadowOpacity;
46
+ get shadowRenderSizeFactor() {
47
+ return this._renderSizeFactor;
48
+ }
49
+ set shadowRenderSizeFactor(value) {
50
+ this._renderSizeFactor = Math.max(Math.min(value, 1.0), 0.0);
51
+ this._voxelTracingPass.resize(value);
52
+ this._spatialBlurPass.resize(value);
53
+ this._accumulationPass.resize(value);
54
+ this._setPluginParameters();
65
55
  }
66
56
  /**
67
57
  * How dark the voxel shadows appear. 1.0 is full opacity, 0.0 is no shadows.
68
58
  */
59
+ get voxelShadowOpacity() {
60
+ return this._voxelTracingPass?.voxelShadowOpacity;
61
+ }
69
62
  set voxelShadowOpacity(value) {
70
63
  if (!this._voxelTracingPass)
71
64
  return;
@@ -77,9 +70,6 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
77
70
  get ssShadowOpacity() {
78
71
  return this._voxelTracingPass?.ssShadowOpacity;
79
72
  }
80
- /**
81
- * How dark the screen-space shadows appear. 1.0 is full opacity, 0.0 is no shadows.
82
- */
83
73
  set ssShadowOpacity(value) {
84
74
  if (!this._voxelTracingPass)
85
75
  return;
@@ -91,9 +81,6 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
91
81
  get ssShadowSamples() {
92
82
  return this._voxelTracingPass?.sssSamples;
93
83
  }
94
- /**
95
- * The number of samples used in the screen space shadow pass.
96
- */
97
84
  set ssShadowSamples(value) {
98
85
  if (!this._voxelTracingPass)
99
86
  return;
@@ -105,43 +92,31 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
105
92
  get ssShadowStride() {
106
93
  return this._voxelTracingPass?.sssStride;
107
94
  }
108
- /**
109
- * The stride of the screen-space shadow pass. This controls the distance between samples.
110
- */
111
95
  set ssShadowStride(value) {
112
96
  if (!this._voxelTracingPass)
113
97
  return;
114
98
  this._voxelTracingPass.sssStride = value;
115
99
  }
116
100
  /**
117
- * The maximum distance a shadow can be cast in screen space. This should usually be kept small
118
- * as screenspace shadows are mostly useful for small details.
101
+ * A scale for the maximum distance a shadow can be cast in screen space.
102
+ * The absolute distance for SS shadows is derived from the voxel size and this scalar.
119
103
  */
120
- get ssShadowMaxDist() {
121
- return this._voxelTracingPass?.sssMaxDist;
104
+ get ssShadowDistanceScale() {
105
+ return this._sssMaxDistScale;
122
106
  }
123
- /**
124
- * The maximum distance a shadow can be cast in screen space. This should usually be kept small
125
- * as screenspace shadows are mostly useful for small details.
126
- */
127
- set ssShadowMaxDist(value) {
128
- if (!this._voxelTracingPass)
129
- return;
130
- this._voxelTracingPass.sssMaxDist = value;
107
+ set ssShadowDistanceScale(value) {
108
+ this._sssMaxDistScale = value;
109
+ this._updateSSShadowParams();
131
110
  }
132
111
  /**
133
112
  * Screen-space shadow thickness. This value controls the perceived thickness of the SS shadows.
134
113
  */
135
- get ssShadowThickness() {
136
- return this._voxelTracingPass?.sssThickness;
114
+ get ssShadowThicknessScale() {
115
+ return this._sssThicknessScale;
137
116
  }
138
- /**
139
- * Screen-space shadow thickness. This value controls the perceived thickness of the SS shadows.
140
- */
141
- set ssShadowThickness(value) {
142
- if (!this._voxelTracingPass)
143
- return;
144
- this._voxelTracingPass.sssThickness = value;
117
+ set ssShadowThicknessScale(value) {
118
+ this._sssThicknessScale = value;
119
+ this._updateSSShadowParams();
145
120
  }
146
121
  /**
147
122
  * Set the IBL image to be used for shadowing. It can be either a cubemap
@@ -156,23 +131,86 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
156
131
  /**
157
132
  * Returns the texture containing the voxel grid data
158
133
  * @returns The texture containing the voxel grid data
134
+ * @internal
159
135
  */
160
- getVoxelGridTexture() {
161
- return this._voxelRenderer?.getVoxelGrid();
136
+ _getVoxelGridTexture() {
137
+ const tex = this._voxelRenderer?.getVoxelGrid();
138
+ if (tex && tex.isReady()) {
139
+ return tex;
140
+ }
141
+ return this._dummyTexture3d;
162
142
  }
163
143
  /**
164
144
  * Returns the texture containing the importance sampling CDF data for the IBL shadow pipeline
165
145
  * @returns The texture containing the importance sampling CDF data for the IBL shadow pipeline
146
+ * @internal
166
147
  */
167
- getIcdfyTexture() {
168
- return this._importanceSamplingRenderer.getIcdfyTexture();
148
+ _getIcdfyTexture() {
149
+ const tex = this._importanceSamplingRenderer.getIcdfyTexture();
150
+ if (tex && tex.isReady()) {
151
+ return tex;
152
+ }
153
+ return this._dummyTexture2d;
169
154
  }
170
155
  /**
171
156
  * Returns the texture containing the importance sampling CDF data for the IBL shadow pipeline
172
157
  * @returns The texture containing the importance sampling CDF data for the IBL shadow pipeline
158
+ * @internal
173
159
  */
174
- getIcdfxTexture() {
175
- return this._importanceSamplingRenderer.getIcdfxTexture();
160
+ _getIcdfxTexture() {
161
+ const tex = this._importanceSamplingRenderer.getIcdfxTexture();
162
+ if (tex && tex.isReady()) {
163
+ return tex;
164
+ }
165
+ return this._dummyTexture2d;
166
+ }
167
+ /**
168
+ * Returns the noise texture.
169
+ * @returns The noise texture.
170
+ * @internal
171
+ */
172
+ _getNoiseTexture() {
173
+ const tex = this._noiseTexture;
174
+ if (tex && tex.isReady()) {
175
+ return tex;
176
+ }
177
+ return this._dummyTexture2d;
178
+ }
179
+ /**
180
+ * Returns the voxel-tracing texture.
181
+ * @returns The voxel-tracing texture.
182
+ * @internal
183
+ */
184
+ _getVoxelTracingTexture() {
185
+ const tex = this._voxelTracingPass?.getOutputTexture();
186
+ if (tex && tex.isReady()) {
187
+ return tex;
188
+ }
189
+ return this._dummyTexture2d;
190
+ }
191
+ /**
192
+ * Returns the spatial blur texture.
193
+ * @returns The spatial blur texture.
194
+ * @internal
195
+ */
196
+ _getSpatialBlurTexture() {
197
+ const tex = this._spatialBlurPass.getOutputTexture();
198
+ if (tex && tex.isReady()) {
199
+ return tex;
200
+ }
201
+ return this._dummyTexture2d;
202
+ }
203
+ /**
204
+ * Returns the accumulated shadow texture.
205
+ * @returns The accumulated shadow texture.
206
+ * @internal
207
+ */
208
+ _getAccumulatedTexture() {
209
+ const tex = this._accumulationPass?.getOutputTexture();
210
+ if (tex && tex.isReady()) {
211
+ return tex;
212
+ }
213
+ return this._dummyTexture2d;
176
214
  }
177
215
  /**
178
216
  * Is the debug view of the G-Buffer enabled?
@@ -300,7 +338,7 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
300
338
  * Display the debug view for the spatial blur pass
301
339
  */
302
340
  get spatialBlurPassDebugEnabled() {
303
- return this._spatialBlurPass?.debugEnabled;
341
+ return this._spatialBlurPass.debugEnabled;
304
342
  }
305
343
  /**
306
344
  * Display the debug view for the spatial blur pass
@@ -349,22 +387,41 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
349
387
  }
350
388
  }
351
389
  /**
352
- * Add a mesh in the exclusion list to prevent it to be handled by the IBL shadow pipeline
353
- * @param mesh The mesh to exclude from the IBL shadow pipeline
390
+ * Add a mesh to be used for shadow casting in the IBL shadow pipeline
391
+ * @param mesh A mesh or list of meshes that you want to cast shadows
354
392
  */
355
- addExcludedMesh(mesh) {
356
- if (this._excludedMeshes.indexOf(mesh.uniqueId) === -1) {
357
- this._excludedMeshes.push(mesh.uniqueId);
393
+ addShadowCastingMesh(mesh) {
394
+ if (Array.isArray(mesh)) {
395
+ for (const m of mesh) {
396
+ if (m && this._shadowCastingMeshes.indexOf(m) === -1) {
397
+ this._shadowCastingMeshes.push(m);
398
+ }
399
+ }
400
+ }
401
+ else {
402
+ if (mesh && this._shadowCastingMeshes.indexOf(mesh) === -1) {
403
+ this._shadowCastingMeshes.push(mesh);
404
+ }
358
405
  }
359
406
  }
360
407
  /**
361
- * Remove a mesh from the exclusion list of the IBL shadow pipeline
362
- * @param mesh The mesh to remove
408
+ * Remove a mesh from the shadow-casting list.
409
+ * @param mesh The mesh or list of meshes that you don't want to cast shadows.
363
410
  */
364
- removeExcludedMesh(mesh) {
365
- const index = this._excludedMeshes.indexOf(mesh.uniqueId);
366
- if (index !== -1) {
367
- this._excludedMeshes.splice(index, 1);
411
+ removeShadowCastingMesh(mesh) {
412
+ if (Array.isArray(mesh)) {
413
+ for (const m of mesh) {
414
+ const index = this._shadowCastingMeshes.indexOf(m);
415
+ if (index !== -1) {
416
+ this._shadowCastingMeshes.splice(index, 1);
417
+ }
418
+ }
419
+ }
420
+ else {
421
+ const index = this._shadowCastingMeshes.indexOf(mesh);
422
+ if (index !== -1) {
423
+ this._shadowCastingMeshes.splice(index, 1);
424
+ }
368
425
  }
369
426
  }
370
427
  /**
@@ -388,7 +445,6 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
388
445
  return;
389
446
  }
390
447
  this._voxelRenderer.voxelResolutionExp = newResolution;
391
- this.updateVoxelization();
392
448
  this._accumulationPass.reset = true;
393
449
  }
394
450
  /**
@@ -432,6 +488,7 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
432
488
  if (!this._voxelTracingPass)
433
489
  return;
434
490
  this._voxelTracingPass.envRotation = value;
491
+ this._accumulationPass.reset = true;
435
492
  }
436
493
  /**
437
494
  * Allow debug passes to be enabled. Default is false.
@@ -478,70 +535,93 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
478
535
  */
479
536
  constructor(name, scene, options = {}, cameras) {
480
537
  super(scene.getEngine(), name);
481
- this._voxelizationDirty = true;
482
- this._boundsNeedUpdate = true;
483
538
  this._allowDebugPasses = false;
484
539
  this._debugPasses = [];
485
- this._excludedMeshes = [];
486
- this._shadowOpacity = 0.75;
540
+ this._shadowCastingMeshes = [];
541
+ this._shadowOpacity = 0.8;
487
542
  this._enabled = true;
543
+ this._materialsWithRenderPlugin = [];
544
+ /**
545
+ * Observable that triggers when the shadow renderer is ready
546
+ */
547
+ this.onShadowTextureReadyObservable = new Observable();
548
+ /**
549
+ * Observable that triggers when a new IBL is set and the importance sampling is ready
550
+ */
551
+ this.onNewIblReadyObservable = new Observable();
488
552
  /**
489
553
  * The current world-space size of that the voxel grid covers in the scene.
490
554
  */
491
555
  this.voxelGridSize = 1.0;
556
+ this._renderSizeFactor = 1.0;
492
557
  this._gbufferDebugEnabled = false;
493
558
  this._gBufferDebugSizeParams = new Vector4(0.0, 0.0, 0.0, 0.0);
494
559
  this.scene = scene;
495
- // We need a depth texture for opaque
496
- if (!scene.enablePrePassRenderer()) {
497
- Logger.Warn("IBL Shadows Render Pipeline could not enable PrePass, aborting.");
498
- return;
499
- }
500
- this.shadowOpacity = options.shadowOpacity || 0.75;
501
- this._prePassEffectConfiguration = new IblShadowsPrepassConfiguration();
560
+ this._cameras = cameras || [scene.activeCamera];
561
+ // Create the dummy textures to be used when the pipeline is not ready
562
+ const blackPixels = new Uint8Array([0, 0, 0, 255]);
563
+ this._dummyTexture2d = new RawTexture(blackPixels, 1, 1, Engine.TEXTUREFORMAT_RGBA, scene, false);
564
+ this._dummyTexture3d = new RawTexture3D(blackPixels, 1, 1, 1, Engine.TEXTUREFORMAT_RGBA, scene, false);
565
+ // Setup the geometry buffer target formats
566
+ const textureTypesAndFormats = {};
567
+ textureTypesAndFormats[GeometryBufferRenderer.SCREENSPACE_DEPTH_TEXTURE_TYPE] = {
568
+ textureFormat: 6,
569
+ textureType: 1,
570
+ };
571
+ textureTypesAndFormats[GeometryBufferRenderer.VELOCITY_LINEAR_TEXTURE_TYPE] = {
572
+ textureFormat: 7,
573
+ textureType: 2,
574
+ };
575
+ textureTypesAndFormats[GeometryBufferRenderer.POSITION_TEXTURE_TYPE] = {
576
+ textureFormat: 5,
577
+ textureType: 2,
578
+ };
579
+ textureTypesAndFormats[GeometryBufferRenderer.NORMAL_TEXTURE_TYPE] = {
580
+ textureFormat: 5,
581
+ textureType: 2,
582
+ };
583
+ const geometryBufferRenderer = scene.enableGeometryBufferRenderer(undefined, 14, textureTypesAndFormats);
584
+ if (!geometryBufferRenderer) {
585
+ Logger.Error("Geometry buffer renderer is required for IBL shadows to work.");
586
+ return;
587
+ }
588
+ this._geometryBufferRenderer = geometryBufferRenderer;
589
+ this._geometryBufferRenderer.enableScreenspaceDepth = true;
590
+ this._geometryBufferRenderer.enableVelocityLinear = true;
591
+ this._geometryBufferRenderer.enablePosition = true;
592
+ this._geometryBufferRenderer.enableNormal = true;
593
+ this._geometryBufferRenderer.generateNormalsInWorldSpace = true;
594
+ this.shadowOpacity = options.shadowOpacity || 0.8;
502
595
  this._voxelRenderer = new _IblShadowsVoxelRenderer(this.scene, this, options ? options.resolutionExp : 6, options.triPlanarVoxelization !== undefined ? options.triPlanarVoxelization : true);
503
596
  this._importanceSamplingRenderer = new _IblShadowsImportanceSamplingRenderer(this.scene);
504
597
  this._voxelTracingPass = new _IblShadowsVoxelTracingPass(this.scene, this);
598
+ this._spatialBlurPass = new _IblShadowsSpatialBlurPass(this.scene, this);
599
+ this._accumulationPass = new _IblShadowsAccumulationPass(this.scene, this);
600
+ this._accumulationPass.onReadyObservable.addOnce(() => {
601
+ this.onShadowTextureReadyObservable.notifyObservers();
602
+ });
505
603
  this.sampleDirections = options.sampleDirections || 2;
506
- this.voxelShadowOpacity = options.voxelShadowOpacity || 1.0;
604
+ this.voxelShadowOpacity = options.voxelShadowOpacity ?? 1.0;
605
+ this.shadowRenderSizeFactor = options.shadowRenderSizeFactor || 1.0;
507
606
  this.ssShadowOpacity = options.ssShadowsEnabled === undefined || options.ssShadowsEnabled ? 1.0 : 0.0;
508
- this.ssShadowMaxDist = options.ssShadowMaxDist || 0.05;
607
+ this.ssShadowDistanceScale = options.ssShadowDistanceScale || 1.25;
509
608
  this.ssShadowSamples = options.ssShadowSampleCount || 16;
510
609
  this.ssShadowStride = options.ssShadowStride || 8;
511
- this.ssShadowThickness = options.ssShadowThickness || 0.5;
512
- this._spatialBlurPass = new _IblShadowsSpatialBlurPass(this.scene);
513
- this._accumulationPass = new _IblShadowsAccumulationPass(this.scene);
514
- this.shadowRemenance = options.shadowRemenance || 0.75;
610
+ this.ssShadowThicknessScale = options.ssShadowThicknessScale || 1.0;
611
+ this.shadowRemenance = options.shadowRemenance ?? 0.75;
515
612
  this._noiseTexture = new Texture("https://assets.babylonjs.com/textures/blue_noise/blue_noise_rgb.png", this.scene, false, true, 1);
516
613
  if (this.scene.environmentTexture) {
517
614
  this._importanceSamplingRenderer.iblSource = this.scene.environmentTexture;
518
615
  }
519
- // Create post process that applies the shadows to the scene
520
- this._createShadowCombinePostProcess();
521
616
  scene.postProcessRenderPipelineManager.addPipeline(this);
522
- this.scene.onNewMeshAddedObservable.add(this.updateSceneBounds.bind(this));
523
- this.scene.onMeshRemovedObservable.add(this.updateSceneBounds.bind(this));
524
617
  this.scene.onActiveCameraChanged.add(this._listenForCameraChanges.bind(this));
525
618
  this.scene.onBeforeRenderObservable.add(this._updateBeforeRender.bind(this));
526
619
  this._listenForCameraChanges();
527
620
  this.scene.getEngine().onResizeObservable.add(this._handleResize.bind(this));
528
- // Only turn on the pipeline when the importance sampling RT's are ready
529
- this._importanceSamplingRenderer.onReadyObservable.addOnce(() => {
530
- this._createEffectPasses(cameras);
531
- const checkVoxelRendererReady = () => {
532
- if (this._voxelRenderer.isReady()) {
533
- this.toggleShadow(this._enabled);
534
- if (this._enabled) {
535
- this._voxelizationDirty = true;
536
- }
537
- }
538
- else {
539
- setTimeout(() => {
540
- checkVoxelRendererReady();
541
- }, 16);
542
- }
543
- };
544
- checkVoxelRendererReady();
621
+ // Assigning the shadow texture to the materials needs to be done after the RT's are created.
622
+ this._importanceSamplingRenderer.onReadyObservable.add(() => {
623
+ this._setPluginParameters();
624
+ this.onNewIblReadyObservable.notifyObservers();
545
625
  });
546
626
  }
547
627
  /**
@@ -550,85 +630,30 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
550
630
  */
551
631
  toggleShadow(enabled) {
552
632
  this._enabled = enabled;
553
- if (enabled) {
554
- this._enableEffect("IBLShadowVoxelTracingPass", this.cameras);
555
- this._enableEffect("IBLShadowSpatialBlurPass", this.cameras);
556
- this._enableEffect("IBLShadowAccumulationBlurPass", this.cameras);
557
- this._enableEffect("IBLShadowCompositePass", this.cameras);
558
- }
559
- else {
560
- this._disableEffect("IBLShadowVoxelTracingPass", null);
561
- this._disableEffect("IBLShadowSpatialBlurPass", null);
562
- this._disableEffect("IBLShadowAccumulationBlurPass", null);
563
- this._disableEffect("IBLShadowCompositePass", null);
564
- }
633
+ this._voxelTracingPass.enabled = enabled;
634
+ this._spatialBlurPass.enabled = enabled;
635
+ this._accumulationPass.enabled = enabled;
636
+ this._materialsWithRenderPlugin.forEach((mat) => {
637
+ if (mat.pluginManager) {
638
+ const plugin = mat.pluginManager.getPlugin(IBLShadowsPluginMaterial.Name);
639
+ plugin.isEnabled = enabled;
640
+ }
641
+ });
642
+ this._setPluginParameters();
565
643
  }
566
644
  _handleResize() {
567
645
  this._voxelRenderer.resize();
568
- this._accumulationPass?.resize();
569
- }
570
- _createShadowCombinePostProcess() {
571
- const isWebGPU = this.engine.isWebGPU;
572
- const compositeOptions = {
573
- width: this.scene.getEngine().getRenderWidth(),
574
- height: this.scene.getEngine().getRenderHeight(),
575
- uniforms: ["shadowOpacity"],
576
- samplers: ["sceneTexture"],
577
- samplingMode: 2,
578
- engine: this.scene.getEngine(),
579
- textureType: 0,
580
- reusable: false,
581
- shaderLanguage: isWebGPU ? 1 /* ShaderLanguage.WGSL */ : 0 /* ShaderLanguage.GLSL */,
582
- extraInitializations: (useWebGPU, list) => {
583
- if (useWebGPU) {
584
- list.push(import("../../ShadersWGSL/iblShadowsCombine.fragment.js"));
585
- }
586
- else {
587
- list.push(import("../../Shaders/iblShadowsCombine.fragment.js"));
588
- }
589
- },
590
- };
591
- this._shadowCompositePP = new PostProcess("iblShadowsCombine", "iblShadowsCombine", compositeOptions);
592
- this._shadowCompositePP.autoClear = false;
593
- this._shadowCompositePP.onApplyObservable.add((effect) => {
594
- // Setting the input of the tracing pass because this is the scene RT that we want to apply the shadows to.
595
- effect.setTextureFromPostProcess("sceneTexture", this._voxelTracingPass.getPassPP());
596
- effect.setFloat("shadowOpacity", this._shadowOpacity);
597
- if (this._importanceSamplingRenderer?.isReady() &&
598
- this._voxelRenderer?.isReady() &&
599
- this._voxelTracingPass?.isReady() &&
600
- this._spatialBlurPass?.isReady() &&
601
- this._accumulationPass?.isReady()) {
602
- this.update();
603
- }
604
- });
605
- this._shadowCompositePP._prePassEffectConfiguration = this._prePassEffectConfiguration;
606
- }
607
- _createEffectPasses(cameras) {
608
- this.addEffect(new PostProcessRenderEffect(this.scene.getEngine(), "IBLShadowVoxelTracingPass", () => {
609
- return this._voxelTracingPass.getPassPP();
610
- }, true));
611
- this.addEffect(new PostProcessRenderEffect(this.scene.getEngine(), "IBLShadowSpatialBlurPass", () => {
612
- return this._spatialBlurPass.getPassPP();
613
- }, true));
614
- this.addEffect(new PostProcessRenderEffect(this.scene.getEngine(), "IBLShadowAccumulationBlurPass", () => {
615
- return this._accumulationPass.getPassPP();
616
- }, true));
617
- this.addEffect(new PostProcessRenderEffect(this.scene.getEngine(), "IBLShadowCompositePass", () => {
618
- return this._shadowCompositePP;
619
- }, true));
620
- if (cameras) {
621
- this.scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this.name, cameras);
622
- }
623
- this.toggleShadow(false);
624
- this._enabled = true;
646
+ this._voxelTracingPass.resize(this.shadowRenderSizeFactor);
647
+ this._spatialBlurPass.resize(this.shadowRenderSizeFactor);
648
+ this._accumulationPass.resize(this.shadowRenderSizeFactor);
649
+ this._setPluginParameters();
625
650
  }
626
651
  _getGBufferDebugPass() {
627
652
  if (this._gbufferDebugPass) {
628
653
  return this._gbufferDebugPass;
629
654
  }
630
655
  const isWebGPU = this.engine.isWebGPU;
631
- const textureNames = this._prePassEffectConfiguration.texturesRequired.map((type) => PrePassRenderer.TextureFormats[type].name.toString());
656
+ const textureNames = ["depthSampler", "normalSampler", "positionSampler", "velocitySampler"];
632
657
  const options = {
633
658
  width: this.scene.getEngine().getRenderWidth(),
634
659
  height: this.scene.getEngine().getRenderHeight(),
@@ -652,16 +677,14 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
652
677
  this._gbufferDebugPass = new PostProcess("iblShadowGBufferDebug", "iblShadowGBufferDebug", options);
653
678
  this._gbufferDebugPass.autoClear = false;
654
679
  this._gbufferDebugPass.onApplyObservable.add((effect) => {
655
- this._prePassEffectConfiguration.texturesRequired.forEach((type) => {
656
- const prePassRenderer = this.scene.prePassRenderer;
657
- if (!prePassRenderer) {
658
- Logger.Error("Can't enable G-Buffer debug rendering since prepassRenderer doesn't exist.");
659
- return;
660
- }
661
- const index = prePassRenderer.getIndex(type);
662
- if (index >= 0)
663
- effect.setTexture(PrePassRenderer.TextureFormats[type].name, prePassRenderer.getRenderTarget().textures[index]);
664
- });
680
+ const depthIndex = this._geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.SCREENSPACE_DEPTH_TEXTURE_TYPE);
681
+ effect.setTexture("depthSampler", this._geometryBufferRenderer.getGBuffer().textures[depthIndex]);
682
+ const normalIndex = this._geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.NORMAL_TEXTURE_TYPE);
683
+ effect.setTexture("normalSampler", this._geometryBufferRenderer.getGBuffer().textures[normalIndex]);
684
+ const positionIndex = this._geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.POSITION_TEXTURE_TYPE);
685
+ effect.setTexture("positionSampler", this._geometryBufferRenderer.getGBuffer().textures[positionIndex]);
686
+ const velocityIndex = this._geometryBufferRenderer.getTextureIndex(GeometryBufferRenderer.VELOCITY_LINEAR_TEXTURE_TYPE);
687
+ effect.setTexture("velocitySampler", this._geometryBufferRenderer.getGBuffer().textures[velocityIndex]);
665
688
  effect.setVector4("sizeParams", this._gBufferDebugSizeParams);
666
689
  if (this.scene.activeCamera) {
667
690
  effect.setFloat("maxDepth", this.scene.activeCamera.maxZ);
@@ -671,11 +694,11 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
671
694
  }
672
695
  _createDebugPasses() {
673
696
  this._debugPasses = [
674
- { pass: this._importanceSamplingRenderer?.getDebugPassPP(), enabled: this.importanceSamplingDebugEnabled },
675
- { pass: this._voxelRenderer?.getDebugPassPP(), enabled: this.voxelDebugEnabled },
676
- { pass: this._voxelTracingPass?.getDebugPassPP(), enabled: this.voxelTracingDebugEnabled },
677
- { pass: this._spatialBlurPass?.getDebugPassPP(), enabled: this.spatialBlurPassDebugEnabled },
678
- { pass: this._accumulationPass?.getDebugPassPP(), enabled: this.accumulationPassDebugEnabled },
697
+ { pass: this._importanceSamplingRenderer.getDebugPassPP(), enabled: this.importanceSamplingDebugEnabled },
698
+ { pass: this._voxelRenderer.getDebugPassPP(), enabled: this.voxelDebugEnabled },
699
+ { pass: this._voxelTracingPass.getDebugPassPP(), enabled: this.voxelTracingDebugEnabled },
700
+ { pass: this._spatialBlurPass.getDebugPassPP(), enabled: this.spatialBlurPassDebugEnabled },
701
+ { pass: this._accumulationPass.getDebugPassPP(), enabled: this.accumulationPassDebugEnabled },
679
702
  { pass: this._getGBufferDebugPass(), enabled: this.gbufferDebugEnabled },
680
703
  ];
681
704
  for (let i = 0; i < this._debugPasses.length; i++) {
@@ -701,10 +724,6 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
701
724
  }
702
725
  _disposeEffectPasses() {
703
726
  this.scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this.name, this.cameras);
704
- this._disableEffect("IBLShadowVoxelTracingPass", this.cameras);
705
- this._disableEffect("IBLShadowSpatialBlurPass", this.cameras);
706
- this._disableEffect("IBLShadowAccumulationBlurPass", this.cameras);
707
- this._disableEffect("IBLShadowCompositePass", this.cameras);
708
727
  this._disposeDebugPasses();
709
728
  this._reset();
710
729
  }
@@ -736,11 +755,6 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
736
755
  let x = 0;
737
756
  let y = 0;
738
757
  if (this.gbufferDebugEnabled) {
739
- const prePassRenderer = this.scene.prePassRenderer;
740
- if (!prePassRenderer) {
741
- Logger.Error("Can't enable G-Buffer debug rendering since prepassRenderer doesn't exist.");
742
- return;
743
- }
744
758
  this._gBufferDebugSizeParams.set(x, y, cols, rows);
745
759
  x -= width;
746
760
  if (x <= -1) {
@@ -793,60 +807,136 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
793
807
  * Trigger the scene to be re-voxelized. This is useful when the scene has changed and the voxel grid needs to be updated.
794
808
  */
795
809
  updateVoxelization() {
796
- this._voxelizationDirty = true;
810
+ if (this._shadowCastingMeshes.length === 0) {
811
+ Logger.Warn("IBL Shadows: updateVoxelization called with no shadow-casting meshes to voxelize.");
812
+ return;
813
+ }
814
+ this._voxelRenderer.updateVoxelGrid(this._shadowCastingMeshes);
815
+ this._updateSSShadowParams();
797
816
  }
798
817
  /**
799
818
  * Trigger the scene bounds of shadow-casters to be updated. This is useful when the scene has changed and the bounds need
800
819
  * to be recalculated. This will also trigger a re-voxelization.
801
820
  */
802
821
  updateSceneBounds() {
803
- this._voxelizationDirty = true;
804
- this._boundsNeedUpdate = true;
822
+ const bounds = {
823
+ min: new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE),
824
+ max: new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE),
825
+ };
826
+ this._shadowCastingMeshes.forEach((mesh) => {
827
+ const localBounds = mesh.getHierarchyBoundingVectors(true);
828
+ bounds.min = Vector3.Minimize(bounds.min, localBounds.min);
829
+ bounds.max = Vector3.Maximize(bounds.max, localBounds.max);
830
+ });
831
+ const size = bounds.max.subtract(bounds.min);
832
+ this.voxelGridSize = Math.max(size.x, size.y, size.z);
833
+ if (this._shadowCastingMeshes.length === 0 || !isFinite(this.voxelGridSize) || this.voxelGridSize === 0) {
834
+ Logger.Warn("IBL Shadows: Scene size is invalid. Can't update bounds.");
835
+ this.voxelGridSize = 1.0;
836
+ return;
837
+ }
838
+ const halfSize = this.voxelGridSize / 2.0;
839
+ const centre = bounds.max.add(bounds.min).multiplyByFloats(-0.5, -0.5, -0.5);
840
+ const invWorldScaleMatrix = Matrix.Compose(new Vector3(1.0 / halfSize, 1.0 / halfSize, 1.0 / halfSize), new Quaternion(), new Vector3(0, 0, 0));
841
+ const invTranslationMatrix = Matrix.Compose(new Vector3(1.0, 1.0, 1.0), new Quaternion(), centre);
842
+ invTranslationMatrix.multiplyToRef(invWorldScaleMatrix, invWorldScaleMatrix);
843
+ this._voxelTracingPass.setWorldScaleMatrix(invWorldScaleMatrix);
844
+ this._voxelRenderer.setWorldScaleMatrix(invWorldScaleMatrix);
845
+ // Set world scale for spatial blur.
846
+ this._spatialBlurPass.setWorldScale(halfSize * 2.0);
847
+ this._updateSSShadowParams();
805
848
  }
806
- _updateBeforeRender() {
807
- this._updateDebugPasses();
849
+ /**
850
+ * Update the SS shadow max distance and thickness based on the voxel grid size and resolution.
851
+ * The max distance should be just a little larger than the world size of a single voxel.
852
+ */
853
+ _updateSSShadowParams() {
854
+ this._voxelTracingPass.sssMaxDist = (this._sssMaxDistScale * this.voxelGridSize) / (1 << this.resolutionExp);
855
+ this._voxelTracingPass.sssThickness = this._sssThicknessScale * 0.005 * this.voxelGridSize;
808
856
  }
809
- _listenForCameraChanges() {
810
- // We want to listen for camera changes and change settings while the camera is moving.
811
- if (this.scene.activeCamera instanceof ArcRotateCamera) {
812
- this.scene.onBeforeCameraRenderObservable.add((camera) => {
813
- let isMoving = false;
814
- if (camera instanceof ArcRotateCamera) {
815
- isMoving =
816
- camera.inertialAlphaOffset !== 0 ||
817
- camera.inertialBetaOffset !== 0 ||
818
- camera.inertialRadiusOffset !== 0 ||
819
- camera.inertialPanningX !== 0 ||
820
- camera.inertialPanningY !== 0;
821
- }
822
- else if (camera instanceof FreeCamera) {
823
- isMoving =
824
- camera.cameraDirection.x !== 0 ||
825
- camera.cameraDirection.y !== 0 ||
826
- camera.cameraDirection.z !== 0 ||
827
- camera.cameraRotation.x !== 0 ||
828
- camera.cameraRotation.y !== 0;
829
- }
830
- if (this._accumulationPass) {
831
- if (isMoving) {
832
- // this._accumulationPass.reset = true;
833
- // this._accumulationPass.remenance = 1.0;
834
- }
835
- else {
836
- // this._accumulationPass.reset = false;
837
- // this._accumulationPass.remenance = 0.9;
838
- }
839
- }
857
+ /**
858
+ * Apply the shadows to a material or array of materials.
859
+ * @param material Material that will be affected by the shadows. If not provided, all materials of the scene will be affected.
860
+ */
861
+ addShadowReceivingMaterial(material) {
862
+ if (material) {
863
+ if (Array.isArray(material)) {
864
+ material.forEach((m) => {
865
+ this._addShadowSupportToMaterial(m);
866
+ });
867
+ }
868
+ else {
869
+ this._addShadowSupportToMaterial(material);
870
+ }
871
+ }
872
+ else {
873
+ this.scene.materials.forEach((mat) => {
874
+ this._addShadowSupportToMaterial(mat);
840
875
  });
841
876
  }
842
877
  }
843
878
  /**
844
- * Links to the prepass renderer
845
- * @param prePassRenderer The scene PrePassRenderer
846
- * @returns true if the pre pass is setup
879
+ * Remove a material from receiving shadows
880
+ * @param material The material that will no longer receive shadows
847
881
  */
848
- setPrePassRenderer(prePassRenderer) {
849
- return !!prePassRenderer.addEffectConfiguration(this._prePassEffectConfiguration);
882
+ removeShadowReceivingMaterial(material) {
883
+ if (Array.isArray(material)) {
884
+ material.forEach((m) => {
885
+ const matIndex = this._materialsWithRenderPlugin.indexOf(m);
886
+ if (matIndex !== -1) {
887
+ this._materialsWithRenderPlugin.splice(matIndex, 1);
888
+ const plugin = m.pluginManager?.getPlugin(IBLShadowsPluginMaterial.Name);
889
+ plugin.isEnabled = false;
890
+ }
891
+ });
892
+ }
893
+ else {
894
+ const matIndex = this._materialsWithRenderPlugin.indexOf(material);
895
+ if (matIndex !== -1) {
896
+ this._materialsWithRenderPlugin.splice(matIndex, 1);
897
+ const plugin = material.pluginManager.getPlugin(IBLShadowsPluginMaterial.Name);
898
+ plugin.isEnabled = false;
899
+ }
900
+ }
901
+ }
902
+ _addShadowSupportToMaterial(material) {
903
+ if (!(material instanceof PBRBaseMaterial) && !(material instanceof StandardMaterial)) {
904
+ return;
905
+ }
906
+ let plugin = material.pluginManager?.getPlugin(IBLShadowsPluginMaterial.Name);
907
+ if (!plugin) {
908
+ plugin = new IBLShadowsPluginMaterial(material);
909
+ }
910
+ if (this._materialsWithRenderPlugin.indexOf(material) !== -1) {
911
+ return;
912
+ }
913
+ if (this._enabled) {
914
+ plugin.iblShadowsTexture = this._getAccumulatedTexture().getInternalTexture();
915
+ plugin.shadowOpacity = this.shadowOpacity;
916
+ }
917
+ plugin.isEnabled = this._enabled;
918
+ this._materialsWithRenderPlugin.push(material);
919
+ }
920
+ _setPluginParameters() {
921
+ if (!this._enabled) {
922
+ return;
923
+ }
924
+ this._materialsWithRenderPlugin.forEach((mat) => {
925
+ if (mat.pluginManager) {
926
+ const plugin = mat.pluginManager.getPlugin(IBLShadowsPluginMaterial.Name);
927
+ plugin.iblShadowsTexture = this._getAccumulatedTexture().getInternalTexture();
928
+ plugin.shadowOpacity = this.shadowOpacity;
929
+ }
930
+ });
931
+ }
932
+ _updateBeforeRender() {
933
+ this._updateDebugPasses();
934
+ }
935
+ _listenForCameraChanges() {
936
+ // We want to listen for camera changes and change settings while the camera is moving.
937
+ this.scene.activeCamera?.onViewMatrixChangedObservable.add(() => {
938
+ this._accumulationPass.isMoving = true;
939
+ });
850
940
  }
851
941
  /**
852
942
  * Checks if the IBL shadow pipeline is ready to render shadows
@@ -860,54 +950,6 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
860
950
  (!this._spatialBlurPass || this._spatialBlurPass.isReady()) &&
861
951
  (!this._accumulationPass || this._accumulationPass.isReady()));
862
952
  }
863
- /**
864
- * Renders accumulated shadows for IBL
865
- */
866
- update() {
867
- // This is called for every MRT in the customRenderTargets structure during voxelization. That doesn't make
868
- // sense. We only want this to run after voxelization so we should put in some state logic here to return
869
- // if voxelization is happening.
870
- if (this._voxelRenderer.isVoxelizationInProgress()) {
871
- return;
872
- }
873
- if (this._boundsNeedUpdate) {
874
- const bounds = this.scene.getWorldExtends((mesh) => {
875
- return mesh instanceof Mesh && this._excludedMeshes.indexOf(mesh.uniqueId) === -1;
876
- });
877
- const size = bounds.max.subtract(bounds.min);
878
- this.voxelGridSize = Math.max(size.x, Math.max(size.y, size.z));
879
- if (!isFinite(this.voxelGridSize) || this.voxelGridSize === 0) {
880
- Logger.Warn("IBL Shadows: Scene size is invalid. Can't update bounds.");
881
- this._boundsNeedUpdate = false;
882
- this.voxelGridSize = 1.0;
883
- return;
884
- }
885
- const halfSize = this.voxelGridSize / 2.0;
886
- const centre = bounds.max.add(bounds.min).multiplyByFloats(-0.5, -0.5, -0.5);
887
- const invWorldScaleMatrix = Matrix.Compose(new Vector3(1.0 / halfSize, 1.0 / halfSize, 1.0 / halfSize), new Quaternion(), new Vector3(0, 0, 0));
888
- const invTranslationMatrix = Matrix.Compose(new Vector3(1.0, 1.0, 1.0), new Quaternion(), centre);
889
- invTranslationMatrix.multiplyToRef(invWorldScaleMatrix, invWorldScaleMatrix);
890
- this._voxelTracingPass?.setWorldScaleMatrix(invWorldScaleMatrix);
891
- this._voxelRenderer.setWorldScaleMatrix(invWorldScaleMatrix);
892
- // Set world scale for spatial blur.
893
- this._spatialBlurPass?.setWorldScale(halfSize * 2.0);
894
- this._boundsNeedUpdate = false;
895
- // Logger.Log("IBL Shadows: Scene size: " + size);
896
- // Logger.Log("Half size: " + halfSize);
897
- // Logger.Log("Centre translation: " + centre);
898
- // Update the SS shadow max distance based on the voxel grid size and resolution.
899
- // The max distance should be just a little larger than the world size of a single voxel.
900
- this.ssShadowMaxDist = (1.1 * this.voxelGridSize) / (1 << this.resolutionExp);
901
- }
902
- // If update is needed, render voxels
903
- if (this._voxelizationDirty) {
904
- this._voxelRenderer.updateVoxelGrid(this._excludedMeshes);
905
- this._voxelizationDirty = false;
906
- // Update the SS shadow max distance based on the voxel grid size and resolution.
907
- // The max distance should be just a little larger than the world size of a single voxel.
908
- this.ssShadowMaxDist = (1.1 * this.voxelGridSize) / (1 << this.resolutionExp);
909
- }
910
- }
911
953
  /**
912
954
  * Get the class name
913
955
  * @returns "IBLShadowsRenderPipeline"
@@ -919,13 +961,19 @@ export class IblShadowsRenderPipeline extends PostProcessRenderPipeline {
919
961
  * Disposes the IBL shadow pipeline and associated resources
920
962
  */
921
963
  dispose() {
964
+ const materials = this._materialsWithRenderPlugin.splice(0);
965
+ materials.forEach((mat) => {
966
+ this.removeShadowReceivingMaterial(mat);
967
+ });
922
968
  this._disposeEffectPasses();
923
969
  this._noiseTexture.dispose();
924
970
  this._voxelRenderer.dispose();
925
971
  this._importanceSamplingRenderer.dispose();
926
- this._voxelTracingPass?.dispose();
927
- this._spatialBlurPass?.dispose();
928
- this._accumulationPass?.dispose();
972
+ this._voxelTracingPass.dispose();
973
+ this._spatialBlurPass.dispose();
974
+ this._accumulationPass.dispose();
975
+ this._dummyTexture2d.dispose();
976
+ this._dummyTexture3d.dispose();
929
977
  super.dispose();
930
978
  }
931
979
  }