@babylonjs/loaders 9.32.0 → 9.33.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 (37) hide show
  1. package/FBX/fbxFileLoader.pure.js +7 -3
  2. package/FBX/fbxFileLoader.pure.js.map +1 -1
  3. package/SPLAT/gaussianSplattingWorkBuffer.js +1 -1
  4. package/SPLAT/gaussianSplattingWorkBuffer.js.map +1 -1
  5. package/SPLAT/sog.pure.js +2 -2
  6. package/SPLAT/sog.pure.js.map +1 -1
  7. package/glTF/2.0/Extensions/KHR_materials_scatter.d.ts +6 -0
  8. package/glTF/2.0/Extensions/KHR_materials_scatter.js +9 -0
  9. package/glTF/2.0/Extensions/KHR_materials_scatter.js.map +1 -0
  10. package/glTF/2.0/Extensions/KHR_materials_scatter.pure.d.ts +40 -0
  11. package/glTF/2.0/Extensions/KHR_materials_scatter.pure.js +105 -0
  12. package/glTF/2.0/Extensions/KHR_materials_scatter.pure.js.map +1 -0
  13. package/glTF/2.0/Extensions/KHR_materials_scatter.types.d.ts +9 -0
  14. package/glTF/2.0/Extensions/KHR_materials_scatter.types.js +2 -0
  15. package/glTF/2.0/Extensions/KHR_materials_scatter.types.js.map +1 -0
  16. package/glTF/2.0/Extensions/KHR_materials_volume_scatter.pure.js +2 -0
  17. package/glTF/2.0/Extensions/KHR_materials_volume_scatter.pure.js.map +1 -1
  18. package/glTF/2.0/Extensions/dynamic.js +5 -0
  19. package/glTF/2.0/Extensions/dynamic.js.map +1 -1
  20. package/glTF/2.0/Extensions/index.d.ts +1 -0
  21. package/glTF/2.0/Extensions/index.js +1 -0
  22. package/glTF/2.0/Extensions/index.js.map +1 -1
  23. package/glTF/2.0/Extensions/pure.d.ts +1 -0
  24. package/glTF/2.0/Extensions/pure.js +1 -0
  25. package/glTF/2.0/Extensions/pure.js.map +1 -1
  26. package/glTF/2.0/glTFLoader.pure.d.ts +7 -0
  27. package/glTF/2.0/glTFLoader.pure.js +29 -3
  28. package/glTF/2.0/glTFLoader.pure.js.map +1 -1
  29. package/glTF/2.0/materialLoadingAdapter.d.ts +23 -0
  30. package/glTF/2.0/materialLoadingAdapter.js.map +1 -1
  31. package/glTF/2.0/openpbrMaterialLoadingAdapter.d.ts +30 -4
  32. package/glTF/2.0/openpbrMaterialLoadingAdapter.js +172 -14
  33. package/glTF/2.0/openpbrMaterialLoadingAdapter.js.map +1 -1
  34. package/glTF/2.0/pbrMaterialLoadingAdapter.d.ts +89 -7
  35. package/glTF/2.0/pbrMaterialLoadingAdapter.js +278 -25
  36. package/glTF/2.0/pbrMaterialLoadingAdapter.js.map +1 -1
  37. package/package.json +4 -4
@@ -1,6 +1,22 @@
1
- import { Color3 } from "@babylonjs/core/Maths/math.color.pure.js";
1
+ import { Color3, Color4 } from "@babylonjs/core/Maths/math.color.pure.js";
2
2
  import { Constants } from "@babylonjs/core/Engines/constants.js";
3
- import { Vector3 } from "@babylonjs/core/Maths/math.vector.pure.js";
3
+ import { CreateFactorOperand, CreateTextureWithFactorOperand, ExtractMaxChannelAsync, LerpTexturesAsync, TextureChannel, TextureColorSpace, ThinWalledScatterWeightsAsync, } from "@babylonjs/core/Materials/Textures/textureProcessor.js";
4
+ /**
5
+ * Render-target options for intermediate texture processor passes: filterable floating point when the
6
+ * engine can render to it, so that only the final textures are quantized to 8 bits.
7
+ * @param scene the scene whose engine renders the passes
8
+ * @returns output options for an intermediate pass
9
+ */
10
+ function IntermediateOutputOptions(scene) {
11
+ const caps = scene.getEngine().getCaps();
12
+ if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
13
+ return { textureType: Constants.TEXTURETYPE_FLOAT };
14
+ }
15
+ if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
16
+ return { textureType: Constants.TEXTURETYPE_HALF_FLOAT };
17
+ }
18
+ return { textureType: Constants.TEXTURETYPE_UNSIGNED_BYTE };
19
+ }
4
20
  /**
5
21
  * Material Loading Adapter for PBR materials that provides a unified OpenPBR-like interface.
6
22
  */
@@ -11,6 +27,12 @@ export class PBRMaterialLoadingAdapter {
11
27
  */
12
28
  constructor(material) {
13
29
  this._specWorkflow = false;
30
+ this._volumetricScatterStrengthFactor = null;
31
+ this._volumetricScatterStrengthTexture = null;
32
+ this._thinWalledScatterStrengthFactor = null;
33
+ this._thinWalledScatterStrengthTexture = null;
34
+ this._transmissionScatterTexture = null;
35
+ this._subsurfaceColorTexture = null;
14
36
  this._material = material;
15
37
  this._material.enableSpecularAntiAliasing = true;
16
38
  }
@@ -21,10 +43,177 @@ export class PBRMaterialLoadingAdapter {
21
43
  return this._material;
22
44
  }
23
45
  /**
24
- * No-op: PBRMaterial has no deferred finalization.
25
- * @param _loader Unused.
46
+ * Finalizes material properties after all loading is complete.
47
+ * @param loader The glTF loader that created the material.
48
+ */
49
+ async finalizeAsync(loader) {
50
+ // Thin-walled KHR_materials_scatter stages its scatter strength S; convert it together with the
51
+ // transmission T only now that every texture has loaded.
52
+ const thinScatterStrength = this._thinWalledScatterStrengthFactor;
53
+ const thinScatterTex = this._thinWalledScatterStrengthTexture;
54
+ this._thinWalledScatterStrengthFactor = null;
55
+ this._thinWalledScatterStrengthTexture = null;
56
+ if (thinScatterStrength !== null && (thinScatterStrength > 0 || thinScatterTex)) {
57
+ await this._applyThinWalledScatterAsync(loader, thinScatterStrength, thinScatterTex);
58
+ if (loader._disposed) {
59
+ return;
60
+ }
61
+ }
62
+ else {
63
+ this._disposeTextureIfUnused(loader, thinScatterTex);
64
+ }
65
+ // Volumetric KHR_materials_scatter maps to PBR translucency, applied only to the transmitted fraction
66
+ // of each texel (see _applyVolumetricScatterAsync). The staged textures are applied here, after every
67
+ // texture has loaded, so they deterministically replace any KHR_materials_diffuse_transmission
68
+ // textures, whose async loads can otherwise resolve after these.
69
+ // The subsurface color texture (thin-walled KHR_materials_scatter, KHR_materials_volume_scatter)
70
+ // tints translucency through translucencyColorTexture and is applied here for the same reason.
71
+ // Volumetric scatter only acts on transmitted light, so without transmission it has no visible
72
+ // effect and must leave translucency (e.g. from KHR_materials_diffuse_transmission) untouched.
73
+ const strengthTex = this._volumetricScatterStrengthTexture;
74
+ const transmissionScatterTex = this._transmissionScatterTexture;
75
+ const subsurfaceColorTex = this._subsurfaceColorTexture;
76
+ const scatterStrength = this._volumetricScatterStrengthFactor;
77
+ this._volumetricScatterStrengthFactor = null;
78
+ this._volumetricScatterStrengthTexture = null;
79
+ this._transmissionScatterTexture = null;
80
+ this._subsurfaceColorTexture = null;
81
+ const applyScatter = scatterStrength !== null && this.transmissionWeight > 0;
82
+ const colorTex = (applyScatter ? transmissionScatterTex : null) ?? subsurfaceColorTex;
83
+ const subSurface = this._material.subSurface;
84
+ if (applyScatter) {
85
+ await this._applyVolumetricScatterAsync(loader, scatterStrength, strengthTex);
86
+ if (loader._disposed) {
87
+ return;
88
+ }
89
+ }
90
+ if (colorTex && subSurface.translucencyColorTexture !== colorTex) {
91
+ const oldColorTexture = subSurface.translucencyColorTexture;
92
+ subSurface.translucencyColorTexture = colorTex;
93
+ this._disposeTextureIfUnused(loader, oldColorTexture);
94
+ }
95
+ // Dispose staged textures that weren't applied. The strength texture is baked into the
96
+ // translucency intensity, so it's never applied directly.
97
+ for (const texture of [strengthTex, transmissionScatterTex, subsurfaceColorTex]) {
98
+ if (texture !== colorTex) {
99
+ this._disposeTextureIfUnused(loader, texture);
100
+ }
101
+ }
102
+ }
103
+ /**
104
+ * Converts volumetric scatter strength S into the PBR translucency intensity. Volumetric scatter only
105
+ * acts on the transmitted fraction T of the surface, so per texel the intensity blends from any
106
+ * independently configured diffuse transmission D (where T = 0) to S (where T = 1): D + T * (S - D).
107
+ * The staged strength texture isn't disposed here.
108
+ * @param loader The glTF loader that created the material
109
+ * @param scatterStrength The scatter strength factor
110
+ * @param strengthTex The scatter strength texture (alpha channel) or null
111
+ */
112
+ async _applyVolumetricScatterAsync(loader, scatterStrength, strengthTex) {
113
+ const subSurface = this._material.subSurface;
114
+ const scene = this._material.getScene();
115
+ const name = this._material.name;
116
+ const transmissionFactor = this.transmissionWeight;
117
+ const transmissionTex = subSurface.refractionIntensityTexture;
118
+ const oldIntensityTexture = subSurface.translucencyIntensityTexture;
119
+ const diffuseFactor = this.subsurfaceWeight;
120
+ const diffuseTex = diffuseFactor > 0 ? oldIntensityTexture : null;
121
+ // The translucency intensity is read from the alpha channel, but T is in the red channel of the
122
+ // transmission texture and the channel swizzles keep the source alpha. So T is first broadcast to all
123
+ // four channels (zeroing the source alpha before the max), in a floating point target when possible.
124
+ let transmission;
125
+ if (transmissionTex) {
126
+ transmission = await ExtractMaxChannelAsync(`${name}_scatterTransmission`, CreateTextureWithFactorOperand(transmissionTex, new Color4(transmissionFactor, transmissionFactor, transmissionFactor, 0), TextureChannel.R), scene, true, TextureColorSpace.Linear, undefined, IntermediateOutputOptions(scene));
127
+ if (loader._disposed) {
128
+ transmission.dispose?.();
129
+ return;
130
+ }
131
+ }
132
+ else {
133
+ transmission = CreateFactorOperand(new Color4(transmissionFactor, transmissionFactor, transmissionFactor, transmissionFactor));
134
+ }
135
+ const intensity = await LerpTexturesAsync(`${name}_scatterTranslucency`, CreateTextureWithFactorOperand(diffuseTex, new Color4(diffuseFactor, diffuseFactor, diffuseFactor, diffuseFactor), TextureChannel.A), CreateTextureWithFactorOperand(strengthTex, new Color4(scatterStrength, scatterStrength, scatterStrength, scatterStrength), TextureChannel.A), transmission, scene);
136
+ if (loader._disposed) {
137
+ intensity.dispose?.();
138
+ return;
139
+ }
140
+ if (intensity.texture) {
141
+ subSurface.isTranslucencyEnabled = true;
142
+ subSurface.translucencyIntensity = 1.0;
143
+ subSurface.translucencyIntensityTexture = intensity.texture;
144
+ subSurface.useGltfStyleTextures = true;
145
+ }
146
+ else {
147
+ const value = intensity.factor?.a ?? 0;
148
+ subSurface.isTranslucencyEnabled = value > 0;
149
+ subSurface.translucencyIntensity = value;
150
+ subSurface.translucencyIntensityTexture = null;
151
+ }
152
+ this._disposeTextureIfUnused(loader, oldIntensityTexture);
153
+ }
154
+ /**
155
+ * Converts thin-walled scatter strength S into PBR refraction and translucency intensities.
156
+ * S turns a fraction of the transmission T into diffuse transmission, so per texel the coherent
157
+ * transmission is T * (1 - S). PBR applies translucency to the surface left over by refraction, so
158
+ * the translucency intensity is T * S / (1 - T * (1 - S)), which keeps the diffusely transmitted
159
+ * light at T * S and the reflected diffuse at 1 - T.
160
+ * @param loader The glTF loader that created the material
161
+ * @param scatterStrength The scatter strength factor
162
+ * @param scatterTex The scatter strength texture (alpha channel) or null
163
+ */
164
+ async _applyThinWalledScatterAsync(loader, scatterStrength, scatterTex) {
165
+ const subSurface = this._material.subSurface;
166
+ const transmissionFactor = this.transmissionWeight;
167
+ if (transmissionFactor <= 0) {
168
+ // Nothing to convert; keep any independently configured diffuse transmission.
169
+ this._disposeTextureIfUnused(loader, scatterTex);
170
+ return;
171
+ }
172
+ const transmissionTex = subSurface.refractionIntensityTexture;
173
+ const scene = this._material.getScene();
174
+ const name = this._material.name;
175
+ // The weights are computed from the red channel of T and S and broadcast to all four channels, so
176
+ // they serve both PBR's refraction intensity (red) and translucency intensity (alpha). T is passed
177
+ // with its factor unbaked so that it isn't quantized before the weights are computed.
178
+ const weights = await ThinWalledScatterWeightsAsync(name, CreateTextureWithFactorOperand(transmissionTex, new Color4(transmissionFactor, transmissionFactor, transmissionFactor, transmissionFactor), TextureChannel.R), CreateTextureWithFactorOperand(scatterTex, new Color4(scatterStrength, scatterStrength, scatterStrength, scatterStrength), TextureChannel.A), scene);
179
+ if (loader._disposed) {
180
+ weights.transmission.dispose?.();
181
+ weights.subsurface.dispose?.();
182
+ return;
183
+ }
184
+ if (weights.transmission.texture) {
185
+ this.transmissionWeight = 1.0;
186
+ subSurface.refractionIntensityTexture = weights.transmission.texture;
187
+ subSurface.useGltfStyleTextures = true;
188
+ this._disposeTextureIfUnused(loader, transmissionTex);
189
+ }
190
+ else {
191
+ this.transmissionWeight = weights.transmission.factor?.r ?? 0;
192
+ }
193
+ if (weights.subsurface.texture) {
194
+ const oldTranslucencyTexture = subSurface.translucencyIntensityTexture;
195
+ this.subsurfaceWeight = 1.0;
196
+ subSurface.translucencyIntensityTexture = weights.subsurface.texture;
197
+ subSurface.useGltfStyleTextures = true;
198
+ this._disposeTextureIfUnused(loader, oldTranslucencyTexture);
199
+ }
200
+ else {
201
+ this.subsurfaceWeight = weights.subsurface.factor?.r ?? 0;
202
+ }
203
+ this._disposeTextureIfUnused(loader, scatterTex);
204
+ }
205
+ /**
206
+ * Disposes a texture that has been replaced on this material, unless another slot on this
207
+ * material (or on any other material created by the loader) still references it.
208
+ * Must be called after the texture has been removed from the slot being replaced.
209
+ * @param loader The glTF loader that created the materials
210
+ * @param texture The replaced texture
26
211
  */
27
- async finalizeAsync(_loader) { }
212
+ _disposeTextureIfUnused(loader, texture) {
213
+ if (texture && !loader._isTextureUsedByMaterials(texture)) {
214
+ texture.dispose();
215
+ }
216
+ }
28
217
  /**
29
218
  * Whether the material should be treated as unlit
30
219
  */
@@ -705,22 +894,87 @@ export class PBRMaterialLoadingAdapter {
705
894
  return new Color3(0, 0, 0);
706
895
  }
707
896
  /**
708
- * Sets the transmission scatter coefficient.
709
- * @param value The scatter coefficient as a Color3
897
+ * Sets the transmission scatter coefficient, scaled by transmissionDepth.
898
+ * @param value The scatter coefficient * transmissionDepth as a Color3
710
899
  */
711
900
  set transmissionScatter(value) {
712
- // TODO convert from scatter coefficient to diffusion distance
713
- this._material.subSurface.diffusionDistance = value;
901
+ this._material.subSurface.translucencyColor = value;
714
902
  }
715
903
  /**
716
- * Sets the transmission scatter coefficient.
717
- * @returns The scatter coefficient as a Color3
904
+ * Gets the transmission scatter coefficient, scaled by transmissionDepth.
905
+ * @returns The scatter coefficient * transmissionDepth as a Color3
718
906
  */
719
907
  get transmissionScatter() {
720
- // TODO convert from diffusion distance to scatter coefficient
721
- return this._material.subSurface.diffusionDistance;
908
+ // Callers may mutate the result in place, so never return the attenuation tint itself. Until a scatter
909
+ // color is set, the renderer falls back to the tint, so start from a copy of it to keep the same look.
910
+ if (!this._material.subSurface.translucencyColor) {
911
+ this._material.subSurface.translucencyColor = this._material.subSurface.tintColor.clone();
912
+ }
913
+ return this._material.subSurface.translucencyColor;
914
+ }
915
+ /**
916
+ * Sets the transmission scatter texture.
917
+ * Staged and applied to subSurface.translucencyColorTexture in finalizeAsync.
918
+ * @param value The multiscatter color texture or null
919
+ */
920
+ set transmissionScatterTexture(value) {
921
+ this._transmissionScatterTexture = value;
922
+ }
923
+ /**
924
+ * Gets the staged transmission scatter texture.
925
+ * @returns The staged multiscatter color texture or null
926
+ */
927
+ get transmissionScatterTexture() {
928
+ return this._transmissionScatterTexture;
929
+ }
930
+ /** @internal */
931
+ set volumetricScatterStrengthFactor(value) {
932
+ // Staged and applied in finalizeAsync, once the transmission weight is known.
933
+ this._volumetricScatterStrengthFactor = value;
934
+ }
935
+ /** @internal */
936
+ get volumetricScatterStrengthFactor() {
937
+ return this._volumetricScatterStrengthFactor;
938
+ }
939
+ /**
940
+ * Sets the volumetric scatter strength texture.
941
+ * Staged and applied to subSurface.translucencyIntensityTexture in finalizeAsync.
942
+ * @param value The scatter strength texture (alpha channel) or null
943
+ */
944
+ set volumetricScatterStrengthTexture(value) {
945
+ this._volumetricScatterStrengthTexture = value;
946
+ }
947
+ /**
948
+ * Gets the staged volumetric scatter strength texture.
949
+ * @returns The staged scatter strength texture or null
950
+ */
951
+ get volumetricScatterStrengthTexture() {
952
+ return this._volumetricScatterStrengthTexture;
953
+ }
954
+ /**
955
+ * Sets the thin-walled scatter strength.
956
+ * Staged and converted, together with the transmission weight, in finalizeAsync.
957
+ * @internal
958
+ */
959
+ set thinWalledScatterStrengthFactor(value) {
960
+ this._thinWalledScatterStrengthFactor = value;
961
+ }
962
+ /** @internal */
963
+ get thinWalledScatterStrengthFactor() {
964
+ return this._thinWalledScatterStrengthFactor;
965
+ }
966
+ /**
967
+ * Sets the thin-walled scatter strength texture (alpha channel).
968
+ * Staged and converted, together with the transmission weight, in finalizeAsync.
969
+ * @internal
970
+ */
971
+ set thinWalledScatterStrengthTexture(value) {
972
+ this._thinWalledScatterStrengthTexture = value;
973
+ }
974
+ /** @internal */
975
+ get thinWalledScatterStrengthTexture() {
976
+ return this._thinWalledScatterStrengthTexture;
722
977
  }
723
- set transmissionScatterTexture(value) { }
724
978
  /**
725
979
  * Sets the transmission scattering anisotropy.
726
980
  * @param value The anisotropy intensity value (-1 to 1)
@@ -855,23 +1109,22 @@ export class PBRMaterialLoadingAdapter {
855
1109
  * @param value The subsurface tint color as a Color3
856
1110
  */
857
1111
  set subsurfaceColor(value) {
858
- // PBRMaterial does not have a direct equivalent for subsurface color,
859
- // We could set the base color to this value, wherever subsurfaceWeight > 0
860
- // When scatterAnisotropy is 1, I believe we can approximate the subsurface effect quite well with
861
- // Translucency and a diffusion distance
862
- const extinctionCoefficient = new Vector3(-Math.log(this.transmissionColor.r), -Math.log(this.transmissionColor.g), -Math.log(this.transmissionColor.b));
863
- extinctionCoefficient.scaleInPlace(1 / Math.max(this.transmissionDepth, 0.001));
864
- const absorptionCoeff = extinctionCoefficient;
865
- const maxChannel = Math.max(absorptionCoeff.x, Math.max(absorptionCoeff.y, absorptionCoeff.z));
866
- const attenuationDistance = maxChannel > 0 ? 1.0 / maxChannel : 1;
867
- this._material.subSurface.diffusionDistance = new Color3(Math.exp(-absorptionCoeff.x * attenuationDistance), Math.exp(-absorptionCoeff.y * attenuationDistance), Math.exp(-absorptionCoeff.z * attenuationDistance));
1112
+ this._material.subSurface.translucencyColor = value;
868
1113
  }
869
1114
  /**
870
1115
  * Sets the subsurface color texture.
1116
+ * Staged and applied to subSurface.translucencyColorTexture in finalizeAsync.
871
1117
  * @param value The subsurface tint texture or null
872
1118
  */
873
1119
  set subsurfaceColorTexture(value) {
874
- // PBRMaterial does not have a direct equivalent for subsurface color texture,
1120
+ this._subsurfaceColorTexture = value;
1121
+ }
1122
+ /**
1123
+ * Gets the staged subsurface color texture.
1124
+ * @returns The staged subsurface tint texture or null
1125
+ */
1126
+ get subsurfaceColorTexture() {
1127
+ return this._subsurfaceColorTexture;
875
1128
  }
876
1129
  /**
877
1130
  * Sets the surface tint of the material (when using subsurface scattering)