@babylonjs/addons 8.46.2 → 8.47.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/atmosphere/Shaders/ShadersInclude/atmosphereFunctions.js +7 -11
- package/atmosphere/Shaders/ShadersInclude/atmosphereFunctions.js.map +1 -1
- package/atmosphere/Shaders/compositeAerialPerspective.fragment.js +1 -2
- package/atmosphere/Shaders/compositeAerialPerspective.fragment.js.map +1 -1
- package/atmosphere/Shaders/compositeGlobeAtmosphere.fragment.js +2 -4
- package/atmosphere/Shaders/compositeGlobeAtmosphere.fragment.js.map +1 -1
- package/atmosphere/Shaders/compositeSky.fragment.js +2 -4
- package/atmosphere/Shaders/compositeSky.fragment.js.map +1 -1
- package/atmosphere/Shaders/diffuseSkyIrradiance.fragment.js +1 -2
- package/atmosphere/Shaders/diffuseSkyIrradiance.fragment.js.map +1 -1
- package/atmosphere/ShadersWGSL/ShadersInclude/atmosphereFunctions.d.ts +5 -0
- package/atmosphere/ShadersWGSL/ShadersInclude/atmosphereFunctions.js +67 -0
- package/atmosphere/ShadersWGSL/ShadersInclude/atmosphereFunctions.js.map +1 -0
- package/atmosphere/ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.d.ts +5 -0
- package/atmosphere/ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js +56 -0
- package/atmosphere/ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js.map +1 -0
- package/atmosphere/ShadersWGSL/fullscreenTriangle.vertex.d.ts +6 -0
- package/atmosphere/ShadersWGSL/fullscreenTriangle.vertex.js +31 -0
- package/atmosphere/ShadersWGSL/fullscreenTriangle.vertex.js.map +1 -0
- package/atmosphere/ShadersWGSL/transmittance.fragment.d.ts +8 -0
- package/atmosphere/ShadersWGSL/transmittance.fragment.js +21 -0
- package/atmosphere/ShadersWGSL/transmittance.fragment.js.map +1 -0
- package/atmosphere/atmosphere.d.ts +1 -6
- package/atmosphere/atmosphere.js +46 -51
- package/atmosphere/atmosphere.js.map +1 -1
- package/atmosphere/atmospherePBRMaterialPlugin.d.ts +8 -1
- package/atmosphere/atmospherePBRMaterialPlugin.js +89 -23
- package/atmosphere/atmospherePBRMaterialPlugin.js.map +1 -1
- package/atmosphere/transmittanceLut.d.ts +4 -2
- package/atmosphere/transmittanceLut.js +18 -3
- package/atmosphere/transmittanceLut.js.map +1 -1
- package/package.json +3 -3
|
@@ -5,6 +5,9 @@ import { MaterialDefines } from "@babylonjs/core/Materials/materialDefines.js";
|
|
|
5
5
|
import { MaterialPluginBase } from "@babylonjs/core/Materials/materialPluginBase.js";
|
|
6
6
|
import type { Nullable } from "@babylonjs/core/types.js";
|
|
7
7
|
import type { UniformBuffer } from "@babylonjs/core/Materials/uniformBuffer.js";
|
|
8
|
+
import { ShaderLanguage } from "@babylonjs/core/Materials/shaderLanguage.js";
|
|
9
|
+
import "./ShadersWGSL/ShadersInclude/atmosphereFunctions.js";
|
|
10
|
+
import "./ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js";
|
|
8
11
|
declare class AtmospherePBRMaterialDefines extends MaterialDefines {
|
|
9
12
|
USE_AERIAL_PERSPECTIVE_LUT: boolean;
|
|
10
13
|
APPLY_AERIAL_PERSPECTIVE_INTENSITY: boolean;
|
|
@@ -30,6 +33,10 @@ export declare class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
30
33
|
* @param _isAerialPerspectiveEnabled - Whether to apply aerial perspective.
|
|
31
34
|
*/
|
|
32
35
|
constructor(material: Material, _atmosphere: Atmosphere, _isAerialPerspectiveEnabled?: boolean);
|
|
36
|
+
/**
|
|
37
|
+
* @override
|
|
38
|
+
*/
|
|
39
|
+
isCompatible(): boolean;
|
|
33
40
|
/**
|
|
34
41
|
* @override
|
|
35
42
|
*/
|
|
@@ -69,6 +76,6 @@ export declare class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
69
76
|
/**
|
|
70
77
|
* @override
|
|
71
78
|
*/
|
|
72
|
-
getCustomCode(shaderType: string): Nullable<Record<string, string>>;
|
|
79
|
+
getCustomCode(shaderType: string, shaderLanguage: ShaderLanguage): Nullable<Record<string, string>>;
|
|
73
80
|
}
|
|
74
81
|
export {};
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
import { MaterialDefines } from "@babylonjs/core/Materials/materialDefines.js";
|
|
4
4
|
import { MaterialPluginBase } from "@babylonjs/core/Materials/materialPluginBase.js";
|
|
5
5
|
import { Vector3FromFloatsToRef, Vector3ScaleToRef } from "@babylonjs/core/Maths/math.vector.functions.js";
|
|
6
|
+
import "./ShadersWGSL/ShadersInclude/atmosphereFunctions.js";
|
|
7
|
+
import "./ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js";
|
|
6
8
|
class AtmospherePBRMaterialDefines extends MaterialDefines {
|
|
7
9
|
/**
|
|
8
10
|
* Constructs the {@link AtmospherePBRMaterialDefines}.
|
|
@@ -10,13 +12,9 @@ class AtmospherePBRMaterialDefines extends MaterialDefines {
|
|
|
10
12
|
*/
|
|
11
13
|
constructor(useAerialPerspectiveLut) {
|
|
12
14
|
super();
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14
15
|
this.APPLY_AERIAL_PERSPECTIVE_INTENSITY = false;
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
16
16
|
this.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = false;
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
18
17
|
this.SAMPLE_TRANSMITTANCE_LUT = true;
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
20
18
|
this.EXCLUDE_RAY_MARCHING_FUNCTIONS = true;
|
|
21
19
|
this.USE_AERIAL_PERSPECTIVE_LUT = useAerialPerspectiveLut;
|
|
22
20
|
}
|
|
@@ -48,19 +46,12 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
48
46
|
*/
|
|
49
47
|
constructor(material, _atmosphere, _isAerialPerspectiveEnabled = false) {
|
|
50
48
|
super(material, PluginName, PluginPriority, {
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
52
49
|
USE_CUSTOM_REFLECTION: _atmosphere.diffuseSkyIrradianceLut !== null,
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
54
50
|
CUSTOM_FRAGMENT_BEFORE_FOG: _isAerialPerspectiveEnabled,
|
|
55
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
56
51
|
USE_AERIAL_PERSPECTIVE_LUT: _isAerialPerspectiveEnabled && _atmosphere.isAerialPerspectiveLutEnabled,
|
|
57
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
58
52
|
APPLY_AERIAL_PERSPECTIVE_INTENSITY: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveIntensity !== 1.0,
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
60
53
|
APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveRadianceBias !== 0.0,
|
|
61
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
62
54
|
SAMPLE_TRANSMITTANCE_LUT: true,
|
|
63
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
64
55
|
EXCLUDE_RAY_MARCHING_FUNCTIONS: true,
|
|
65
56
|
}, false, // addPluginToList -- false because we need to control when this is added to the list
|
|
66
57
|
true, // enable
|
|
@@ -72,13 +63,20 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
72
63
|
// This calls `getCode` so we need to do this after having initialized the class fields.
|
|
73
64
|
this._pluginManager._addPlugin(this);
|
|
74
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* @override
|
|
68
|
+
*/
|
|
69
|
+
isCompatible() {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
75
72
|
/**
|
|
76
73
|
* @override
|
|
77
74
|
*/
|
|
78
75
|
getUniformBuffersNames(_ubos) {
|
|
79
76
|
const uniformBuffer = this._atmosphere.uniformBuffer;
|
|
80
77
|
if (uniformBuffer.useUbo) {
|
|
81
|
-
|
|
78
|
+
const uboName = this._material.shaderLanguage === 1 /* ShaderLanguage.WGSL */ ? "atmosphere" : uniformBuffer.name;
|
|
79
|
+
_ubos.push(uboName);
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
82
|
/**
|
|
@@ -174,7 +172,7 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
174
172
|
/**
|
|
175
173
|
* @override
|
|
176
174
|
*/
|
|
177
|
-
getCustomCode(shaderType) {
|
|
175
|
+
getCustomCode(shaderType, shaderLanguage) {
|
|
178
176
|
// Assumed inputs are light0, vPositionW, normalW.
|
|
179
177
|
// Only works for directional lights.
|
|
180
178
|
if (shaderType !== "fragment") {
|
|
@@ -182,13 +180,14 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
182
180
|
}
|
|
183
181
|
const useUbo = this._atmosphere.scene.getEngine().supportsUniformBuffers;
|
|
184
182
|
const directionToLightSnippet = useUbo ? "-light0.vLightData.xyz" : "-vLightData0.xyz";
|
|
185
|
-
const useAtmosphereUbo = this._atmosphere.uniformBuffer.useUbo;
|
|
183
|
+
const useAtmosphereUbo = shaderLanguage === 1 /* ShaderLanguage.WGSL */ || this._atmosphere.uniformBuffer.useUbo;
|
|
186
184
|
const atmosphereImportSnippet = useAtmosphereUbo ? "#include<atmosphereUboDeclaration>" : "#include<atmosphereFragmentDeclaration>";
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
185
|
+
if (shaderLanguage === 0 /* ShaderLanguage.GLSL */) {
|
|
186
|
+
return {
|
|
187
|
+
CUSTOM_FRAGMENT_DEFINITIONS: this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled
|
|
188
|
+
? `uniform sampler2D transmittanceLut;\r\nprecision highp sampler2DArray;\r\nuniform sampler2DArray aerialPerspectiveLut;\r\n${atmosphereImportSnippet}\r\n#include<atmosphereFunctions>`
|
|
189
|
+
: `uniform sampler2D transmittanceLut;\r\n${atmosphereImportSnippet}\r\n#include<atmosphereFunctions>`,
|
|
190
|
+
CUSTOM_LIGHT0_COLOR: `
|
|
192
191
|
{
|
|
193
192
|
vec3 positionGlobal = 0.001 * vPositionW + ${OriginOffsetUniformName};
|
|
194
193
|
float positionRadius = length(positionGlobal);
|
|
@@ -198,7 +197,7 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
198
197
|
diffuse0 = lightIntensity * sampleTransmittanceLut(transmittanceLut, positionRadius, cosAngleLightToZenith);
|
|
199
198
|
}
|
|
200
199
|
`,
|
|
201
|
-
|
|
200
|
+
CUSTOM_REFLECTION: `
|
|
202
201
|
{
|
|
203
202
|
vec3 positionGlobal = 0.001 * vPositionW + ${OriginOffsetUniformName};
|
|
204
203
|
float positionRadius = length(positionGlobal);
|
|
@@ -227,8 +226,8 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
227
226
|
reflectionOut.environmentRadiance.rgb = reflectionOut.environmentIrradiance;
|
|
228
227
|
}
|
|
229
228
|
`,
|
|
230
|
-
|
|
231
|
-
|
|
229
|
+
// TODO: Support full ray marching if USE_AERIAL_PERSPECTIVE_LUT is disabled.
|
|
230
|
+
CUSTOM_FRAGMENT_BEFORE_FOG: `
|
|
232
231
|
#if USE_AERIAL_PERSPECTIVE_LUT
|
|
233
232
|
{
|
|
234
233
|
float distanceFromCameraKm = 0.001 * distance(vEyePosition.xyz, vPositionW);
|
|
@@ -246,7 +245,74 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
|
246
245
|
}
|
|
247
246
|
#endif
|
|
248
247
|
`,
|
|
249
|
-
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
// WGSL
|
|
252
|
+
return {
|
|
253
|
+
CUSTOM_FRAGMENT_DEFINITIONS: this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled
|
|
254
|
+
? `var transmittanceLutSampler: sampler;\r\nvar transmittanceLut: texture_2d<f32>;\r\nvar aerialPerspectiveLutSampler: sampler;\r\nvar aerialPerspectiveLut: texture_2d_array<f32>;\r\n${atmosphereImportSnippet}\r\n#include<atmosphereFunctions>`
|
|
255
|
+
: `var transmittanceLutSampler: sampler;\r\nvar transmittanceLut: texture_2d<f32>;\r\n${atmosphereImportSnippet}\r\n#include<atmosphereFunctions>`,
|
|
256
|
+
CUSTOM_LIGHT0_COLOR: `
|
|
257
|
+
{
|
|
258
|
+
var positionGlobal = 0.001 * fragmentInputs.vPositionW + uniforms.${OriginOffsetUniformName};
|
|
259
|
+
var positionRadius = length(positionGlobal);
|
|
260
|
+
var geocentricNormal = positionGlobal / positionRadius;
|
|
261
|
+
var directionToLight = ${directionToLightSnippet};
|
|
262
|
+
var cosAngleLightToZenith = dot(directionToLight, geocentricNormal);
|
|
263
|
+
diffuse0 = atmosphere.lightIntensity * sampleTransmittanceLut(transmittanceLut, positionRadius, cosAngleLightToZenith);
|
|
264
|
+
}
|
|
265
|
+
`,
|
|
266
|
+
CUSTOM_REFLECTION: `
|
|
267
|
+
{
|
|
268
|
+
var positionGlobal = 0.001 * fragmentInputs.vPositionW + uniforms.${OriginOffsetUniformName};
|
|
269
|
+
var positionRadius = length(positionGlobal);
|
|
270
|
+
var geocentricNormal = positionGlobal / positionRadius;
|
|
271
|
+
|
|
272
|
+
var directionToLight = ${directionToLightSnippet};
|
|
273
|
+
var cosAngleLightToZenith = dot(directionToLight, geocentricNormal);
|
|
274
|
+
|
|
275
|
+
var uv = vec2f(0.5 + 0.5 * cosAngleLightToZenith, (positionRadius - atmosphere.planetRadius) / atmosphere.atmosphereThickness);
|
|
276
|
+
var irradianceScaleT = 0.5 * dot(normalW, geocentricNormal) + 0.5;
|
|
277
|
+
var irradianceScale = ((-0.6652 * irradianceScaleT) + 1.5927) * irradianceScaleT + 0.1023;
|
|
278
|
+
var environmentIrradiance = atmosphere.lightIntensity * textureSample(irradianceSampler, irradianceSamplerSampler, uv).rgb;
|
|
279
|
+
|
|
280
|
+
// Add a contribution here to estimate indirect lighting.
|
|
281
|
+
const r = 0.2;
|
|
282
|
+
var indirect = getLuminance(environmentIrradiance) / max(0.00001, 1.0 - r);
|
|
283
|
+
environmentIrradiance *= irradianceScale;
|
|
284
|
+
environmentIrradiance += indirect;
|
|
285
|
+
|
|
286
|
+
environmentIrradiance += atmosphere.additionalDiffuseSkyIrradiance;
|
|
287
|
+
|
|
288
|
+
const diffuseBrdf = 1.0 / PI;
|
|
289
|
+
environmentIrradiance *= diffuseBrdf * atmosphere.diffuseSkyIrradianceIntensity;
|
|
290
|
+
|
|
291
|
+
reflectionOut.environmentIrradiance = environmentIrradiance;
|
|
292
|
+
reflectionOut.environmentRadiance = vec4f(reflectionOut.environmentIrradiance, reflectionOut.environmentRadiance.a);
|
|
293
|
+
}
|
|
294
|
+
`,
|
|
295
|
+
// TODO: Support full ray marching if USE_AERIAL_PERSPECTIVE_LUT is disabled.
|
|
296
|
+
CUSTOM_FRAGMENT_BEFORE_FOG: `
|
|
297
|
+
#if USE_AERIAL_PERSPECTIVE_LUT
|
|
298
|
+
{
|
|
299
|
+
var distanceFromCameraKm = 0.001 * distance(scene.vEyePosition.xyz, fragmentInputs.vPositionW);
|
|
300
|
+
var aerialPerspective = vec4f(0.);
|
|
301
|
+
if (sampleAerialPerspectiveLut(
|
|
302
|
+
fragmentInputs.position.xy * uniforms.${InverseViewportSizeUniformName},
|
|
303
|
+
true,
|
|
304
|
+
distanceFromCameraKm,
|
|
305
|
+
NumAerialPerspectiveLutLayers,
|
|
306
|
+
AerialPerspectiveLutKMPerSlice,
|
|
307
|
+
AerialPerspectiveLutRangeKM,
|
|
308
|
+
&aerialPerspective)) {
|
|
309
|
+
finalColor = aerialPerspective + (1. - aerialPerspective.a) * finalColor;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
#endif
|
|
313
|
+
`,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
250
316
|
}
|
|
251
317
|
}
|
|
252
318
|
//# sourceMappingURL=atmospherePBRMaterialPlugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atmospherePBRMaterialPlugin.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/atmospherePBRMaterialPlugin.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAKlC,OAAO,EAAE,eAAe,EAAE,qDAAuC;AACjE,OAAO,EAAE,kBAAkB,EAAE,wDAA0C;AAGvE,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,uDAAyC;AAE7F,MAAM,4BAA6B,SAAQ,eAAe;IAYtD;;;OAGG;IACH,YAAY,uBAAgC;QACxC,KAAK,EAAE,CAAC;QAdZ,gEAAgE;QACzD,uCAAkC,GAAG,KAAK,CAAC;QAClD,gEAAgE;QACzD,2CAAsC,GAAG,KAAK,CAAC;QACtD,gEAAgE;QACzD,6BAAwB,GAAG,IAAI,CAAC;QACvC,gEAAgE;QACzD,mCAA8B,GAAG,IAAI,CAAC;QAQzC,IAAI,CAAC,0BAA0B,GAAG,uBAAuB,CAAC;IAC9D,CAAC;CACJ;AAED,MAAM,uBAAuB,GAAG,gBAAgB,CAAC;AACjD,MAAM,8BAA8B,GAAG,qBAAqB,CAAC;AAE7D,MAAM,QAAQ,GAAG;IACb,EAAE,IAAI,EAAE,uBAAuB,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE;IACxD,EAAE,IAAI,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE;IACpD,EAAE,IAAI,EAAE,8BAA8B,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE;CAClE,CAAC;AACF,MAAM,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AAK,UAAU,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC,sBAAsB,CAAC;QACzE,MAAM,uBAAuB,GAAG,MAAM,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,kBAAkB,CAAC;QAEvF,MAAM,gBAAgB,GAAG,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,MAAM,CAAC;QAC/D,MAAM,uBAAuB,GAAG,gBAAgB,CAAC,CAAC,CAAC,oCAAoC,CAAC,CAAC,CAAC,yCAAyC,CAAC;QAEpI,OAAO;YACH,2BAA2B,EACvB,IAAI,CAAC,2BAA2B,IAAI,IAAI,CAAC,WAAW,CAAC,6BAA6B;gBAC9E,CAAC,CAAC,6HAA6H,uBAAuB,mCAAmC;gBACzL,CAAC,CAAC,0CAA0C,uBAAuB,mCAAmC;YAC9G,mBAAmB,EAAE;;6DAE4B,uBAAuB;;;0CAG1C,uBAAuB;;;;CAIhE;YACW,iBAAiB,EAAE;;8DAE+B,uBAAuB;;;;0CAI3C,uBAAuB;;;;;;;;;;;;;;;;;;;;;;CAsBhE;YACW,6EAA6E;YAC7E,0BAA0B,EAAE;;;;;;4CAMI,8BAA8B;;;;;;;;;;;CAWzE;SACQ,CAAC;IACN,CAAC;CACJ","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT License.\r\n\r\nimport type { Atmosphere } from \"./atmosphere\";\r\nimport type { BaseTexture } from \"core/Materials/Textures/baseTexture\";\r\nimport type { Material } from \"core/Materials/material\";\r\nimport { MaterialDefines } from \"core/Materials/materialDefines\";\r\nimport { MaterialPluginBase } from \"core/Materials/materialPluginBase\";\r\nimport type { Nullable } from \"core/types\";\r\nimport type { UniformBuffer } from \"core/Materials/uniformBuffer\";\r\nimport { Vector3FromFloatsToRef, Vector3ScaleToRef } from \"core/Maths/math.vector.functions\";\r\n\r\nclass AtmospherePBRMaterialDefines extends MaterialDefines {\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public USE_AERIAL_PERSPECTIVE_LUT: boolean;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public APPLY_AERIAL_PERSPECTIVE_INTENSITY = false;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = false;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public SAMPLE_TRANSMITTANCE_LUT = true;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public EXCLUDE_RAY_MARCHING_FUNCTIONS = true;\r\n\r\n /**\r\n * Constructs the {@link AtmospherePBRMaterialDefines}.\r\n * @param useAerialPerspectiveLut - Whether to use the aerial perspective LUT.\r\n */\r\n constructor(useAerialPerspectiveLut: boolean) {\r\n super();\r\n this.USE_AERIAL_PERSPECTIVE_LUT = useAerialPerspectiveLut;\r\n }\r\n}\r\n\r\nconst OriginOffsetUniformName = \"originOffsetKm\";\r\nconst InverseViewportSizeUniformName = \"inverseViewportSize\";\r\n\r\nconst UboArray = [\r\n { name: OriginOffsetUniformName, size: 3, type: \"vec3\" },\r\n { name: \"_atmoPbrPadding1\", size: 1, type: \"float\" },\r\n { name: InverseViewportSizeUniformName, size: 2, type: \"vec2\" },\r\n];\r\nconst MakeUniforms = (atmosphere: Atmosphere) => ({\r\n ubo: UboArray,\r\n fragment: `uniform vec2 ${InverseViewportSizeUniformName};\\nuniform vec3 ${OriginOffsetUniformName};\\n`,\r\n externalUniforms: atmosphere.uniformBuffer.getUniformNames(),\r\n});\r\n\r\nconst PluginName = \"AtmospherePBRMaterialPlugin\";\r\nconst PluginPriority = 600;\r\nconst OriginOffsetKm = { x: 0, y: 0, z: 0 };\r\n\r\n/**\r\n * Adds shading logic to a PBRMaterial that provides radiance, diffuse sky irradiance, and aerial perspective from the atmosphere.\r\n */\r\nexport class AtmospherePBRMaterialPlugin extends MaterialPluginBase {\r\n /**\r\n * Constructs the {@link AtmospherePBRMaterialPlugin}.\r\n * @param material - The material to apply the plugin to.\r\n * @param _atmosphere - The atmosphere to use for shading.\r\n * @param _isAerialPerspectiveEnabled - Whether to apply aerial perspective.\r\n */\r\n constructor(\r\n material: Material,\r\n private readonly _atmosphere: Atmosphere,\r\n private readonly _isAerialPerspectiveEnabled = false\r\n ) {\r\n super(\r\n material,\r\n PluginName,\r\n PluginPriority,\r\n {\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n USE_CUSTOM_REFLECTION: _atmosphere.diffuseSkyIrradianceLut !== null,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n CUSTOM_FRAGMENT_BEFORE_FOG: _isAerialPerspectiveEnabled,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n USE_AERIAL_PERSPECTIVE_LUT: _isAerialPerspectiveEnabled && _atmosphere.isAerialPerspectiveLutEnabled,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n APPLY_AERIAL_PERSPECTIVE_INTENSITY: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveIntensity !== 1.0,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveRadianceBias !== 0.0,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n SAMPLE_TRANSMITTANCE_LUT: true,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n EXCLUDE_RAY_MARCHING_FUNCTIONS: true,\r\n },\r\n false, // addPluginToList -- false because we need to control when this is added to the list\r\n true, // enable\r\n true // resolveIncludes\r\n );\r\n\r\n this.doNotSerialize = true;\r\n\r\n // This calls `getCode` so we need to do this after having initialized the class fields.\r\n this._pluginManager._addPlugin(this);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getUniformBuffersNames(_ubos: string[]): void {\r\n const uniformBuffer = this._atmosphere.uniformBuffer;\r\n if (uniformBuffer.useUbo) {\r\n _ubos.push(uniformBuffer.name);\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getUniforms() {\r\n return MakeUniforms(this._atmosphere);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override isReadyForSubMesh(): boolean {\r\n let isReady = true;\r\n const atmosphere = this._atmosphere;\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n isReady = isReady && !!aerialPerspectiveLutRenderTarget?.isReady();\r\n }\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n isReady = isReady && !!transmittanceLutRenderTarget?.isReady();\r\n return isReady;\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getActiveTextures(_activeTextures: BaseTexture[]): void {\r\n const atmosphere = this._atmosphere;\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n if (aerialPerspectiveLutRenderTarget) {\r\n _activeTextures.push(aerialPerspectiveLutRenderTarget);\r\n }\r\n }\r\n\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n if (transmittanceLutRenderTarget) {\r\n _activeTextures.push(transmittanceLutRenderTarget);\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override bindForSubMesh(uniformBuffer: UniformBuffer): void {\r\n const atmosphere = this._atmosphere;\r\n const scene = atmosphere.scene;\r\n const engine = scene.getEngine();\r\n\r\n // Bind the atmosphere's uniform buffer to the effect.\r\n const effect = uniformBuffer.currentEffect;\r\n if (effect) {\r\n this._atmosphere.bindUniformBufferToEffect(effect);\r\n }\r\n\r\n // Need the offset to apply which will take a world space position and convert it to a global space position in the atmosphere.\r\n // If floating origin mode is enabled, that offset is the floating origin offset.\r\n // If not, it's an offset up the Y-axis by the planet radius.\r\n uniformBuffer.updateVector3(\r\n OriginOffsetUniformName,\r\n scene.floatingOriginMode\r\n ? Vector3ScaleToRef(scene.floatingOriginOffset, 0.001, OriginOffsetKm) // Convert to kilometers\r\n : Vector3FromFloatsToRef(0, atmosphere.physicalProperties.planetRadius, 0, OriginOffsetKm) // planetRadius is already in kilometers\r\n );\r\n\r\n const width = engine.getRenderWidth();\r\n const height = engine.getRenderHeight();\r\n uniformBuffer.updateFloat2(InverseViewportSizeUniformName, 1.0 / width, 1.0 / height);\r\n\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n uniformBuffer.setTexture(\"aerialPerspectiveLut\", aerialPerspectiveLutRenderTarget);\r\n }\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n uniformBuffer.setTexture(\"transmittanceLut\", transmittanceLutRenderTarget);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override prepareDefines(defines: AtmospherePBRMaterialDefines): void {\r\n const lastUseAerialPerspectiveLut = defines.USE_AERIAL_PERSPECTIVE_LUT;\r\n const lastApplyAerialPerspectiveIntensity = defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY;\r\n const lastApplyAerialPerspectiveRadianceBias = defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS;\r\n defines.USE_AERIAL_PERSPECTIVE_LUT = this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled;\r\n defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveIntensity !== 1.0;\r\n defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveRadianceBias !== 0.0;\r\n if (\r\n lastUseAerialPerspectiveLut !== defines.USE_AERIAL_PERSPECTIVE_LUT ||\r\n lastApplyAerialPerspectiveIntensity !== defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY ||\r\n lastApplyAerialPerspectiveRadianceBias !== defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS\r\n ) {\r\n defines.markAllAsDirty();\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getSamplers(samplers: string[]): void {\r\n samplers.push(\"transmittanceLut\");\r\n if (this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled) {\r\n samplers.push(\"aerialPerspectiveLut\");\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getCustomCode(shaderType: string): Nullable<Record<string, string>> {\r\n // Assumed inputs are light0, vPositionW, normalW.\r\n // Only works for directional lights.\r\n if (shaderType !== \"fragment\") {\r\n return null;\r\n }\r\n\r\n const useUbo = this._atmosphere.scene.getEngine().supportsUniformBuffers;\r\n const directionToLightSnippet = useUbo ? \"-light0.vLightData.xyz\" : \"-vLightData0.xyz\";\r\n\r\n const useAtmosphereUbo = this._atmosphere.uniformBuffer.useUbo;\r\n const atmosphereImportSnippet = useAtmosphereUbo ? \"#include<atmosphereUboDeclaration>\" : \"#include<atmosphereFragmentDeclaration>\";\r\n\r\n return {\r\n CUSTOM_FRAGMENT_DEFINITIONS:\r\n this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled\r\n ? `uniform sampler2D transmittanceLut;\\r\\nprecision highp sampler2DArray;\\r\\nuniform sampler2DArray aerialPerspectiveLut;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`\r\n : `uniform sampler2D transmittanceLut;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`,\r\n CUSTOM_LIGHT0_COLOR: `\r\n {\r\n vec3 positionGlobal = 0.001 * vPositionW + ${OriginOffsetUniformName};\r\n float positionRadius = length(positionGlobal);\r\n vec3 geocentricNormal = positionGlobal / positionRadius;\r\n vec3 directionToLight = ${directionToLightSnippet};\r\n float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n diffuse0 = lightIntensity * sampleTransmittanceLut(transmittanceLut, positionRadius, cosAngleLightToZenith);\r\n }\r\n`,\r\n CUSTOM_REFLECTION: `\r\n {\r\n vec3 positionGlobal = 0.001 * vPositionW + ${OriginOffsetUniformName};\r\n float positionRadius = length(positionGlobal);\r\n vec3 geocentricNormal = positionGlobal / positionRadius;\r\n\r\n vec3 directionToLight = ${directionToLightSnippet};\r\n float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n\r\n vec2 uv = vec2(0.5 + 0.5 * cosAngleLightToZenith, (positionRadius - planetRadius) / atmosphereThickness);\r\n float irradianceScaleT = 0.5 * dot(normalW, geocentricNormal) + 0.5;\r\n float irradianceScale = ((-0.6652 * irradianceScaleT) + 1.5927) * irradianceScaleT + 0.1023;\r\n vec3 environmentIrradiance = lightIntensity * sampleReflection(irradianceSampler, uv).rgb;\r\n\r\n // Add a contribution here to estimate indirect lighting.\r\n const float r = 0.2;\r\n float indirect = getLuminance(environmentIrradiance) / max(0.00001, 1. - r);\r\n environmentIrradiance *= irradianceScale;\r\n environmentIrradiance += indirect;\r\n\r\n environmentIrradiance += additionalDiffuseSkyIrradiance;\r\n\r\n const float diffuseBrdf = 1. / PI;\r\n environmentIrradiance *= diffuseBrdf * diffuseSkyIrradianceIntensity;\r\n\r\n reflectionOut.environmentIrradiance = environmentIrradiance;\r\n reflectionOut.environmentRadiance.rgb = reflectionOut.environmentIrradiance;\r\n }\r\n`,\r\n // TODO: Support full ray marching if USE_AERIAL_PERSPECTIVE_LUT is disabled.\r\n CUSTOM_FRAGMENT_BEFORE_FOG: `\r\n #if USE_AERIAL_PERSPECTIVE_LUT\r\n {\r\n float distanceFromCameraKm = 0.001 * distance(vEyePosition.xyz, vPositionW);\r\n vec4 aerialPerspective = vec4(0.);\r\n if (sampleAerialPerspectiveLut(\r\n gl_FragCoord.xy * ${InverseViewportSizeUniformName},\r\n true,\r\n distanceFromCameraKm,\r\n NumAerialPerspectiveLutLayers,\r\n AerialPerspectiveLutKMPerSlice,\r\n AerialPerspectiveLutRangeKM,\r\n aerialPerspective)) {\r\n finalColor = aerialPerspective + (1. - aerialPerspective.a) * finalColor;\r\n }\r\n }\r\n #endif\r\n`,\r\n };\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"atmospherePBRMaterialPlugin.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/atmospherePBRMaterialPlugin.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAKlC,OAAO,EAAE,eAAe,EAAE,qDAAuC;AACjE,OAAO,EAAE,kBAAkB,EAAE,wDAA0C;AAGvE,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,uDAAyC;AAE7F,OAAO,kDAAkD,CAAC;AAC1D,OAAO,uDAAuD,CAAC;AAE/D,MAAM,4BAA6B,SAAQ,eAAe;IAOtD;;;OAGG;IACH,YAAY,uBAAgC;QACxC,KAAK,EAAE,CAAC;QAVL,uCAAkC,GAAG,KAAK,CAAC;QAC3C,2CAAsC,GAAG,KAAK,CAAC;QAC/C,6BAAwB,GAAG,IAAI,CAAC;QAChC,mCAA8B,GAAG,IAAI,CAAC;QAQzC,IAAI,CAAC,0BAA0B,GAAG,uBAAuB,CAAC;IAC9D,CAAC;CACJ;AAED,MAAM,uBAAuB,GAAG,gBAAgB,CAAC;AACjD,MAAM,8BAA8B,GAAG,qBAAqB,CAAC;AAE7D,MAAM,QAAQ,GAAG;IACb,EAAE,IAAI,EAAE,uBAAuB,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE;IACxD,EAAE,IAAI,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE;IACpD,EAAE,IAAI,EAAE,8BAA8B,EAAE,IAAI,EAAE,CAAC,EAAE,IA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FAAsF,uBAAuB,mCAAmC;gBAC1J,mBAAmB,EAAE;;oFAE+C,uBAAuB;;;yCAGlE,uBAAuB;;;;CAI/D;gBACe,iBAAiB,EAAE;;qFAEkD,uBAAuB;;;;yCAInE,uBAAuB;;;;;;;;;;;;;;;;;;;;;;CAsB/D;gBACe,6EAA6E;gBAC7E,0BAA0B,EAAE;;;;;;gEAMoB,8BAA8B;;;;;;;;;;;CAW7F;aACY,CAAC;QACN,CAAC;IACL,CAAC;CACJ","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT License.\r\n\r\nimport type { Atmosphere } from \"./atmosphere\";\r\nimport type { BaseTexture } from \"core/Materials/Textures/baseTexture\";\r\nimport type { Material } from \"core/Materials/material\";\r\nimport { MaterialDefines } from \"core/Materials/materialDefines\";\r\nimport { MaterialPluginBase } from \"core/Materials/materialPluginBase\";\r\nimport type { Nullable } from \"core/types\";\r\nimport type { UniformBuffer } from \"core/Materials/uniformBuffer\";\r\nimport { Vector3FromFloatsToRef, Vector3ScaleToRef } from \"core/Maths/math.vector.functions\";\r\nimport { ShaderLanguage } from \"core/Materials/shaderLanguage\";\r\nimport \"./ShadersWGSL/ShadersInclude/atmosphereFunctions\";\r\nimport \"./ShadersWGSL/ShadersInclude/atmosphereUboDeclaration\";\r\n\r\nclass AtmospherePBRMaterialDefines extends MaterialDefines {\r\n public USE_AERIAL_PERSPECTIVE_LUT: boolean;\r\n public APPLY_AERIAL_PERSPECTIVE_INTENSITY = false;\r\n public APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = false;\r\n public SAMPLE_TRANSMITTANCE_LUT = true;\r\n public EXCLUDE_RAY_MARCHING_FUNCTIONS = true;\r\n\r\n /**\r\n * Constructs the {@link AtmospherePBRMaterialDefines}.\r\n * @param useAerialPerspectiveLut - Whether to use the aerial perspective LUT.\r\n */\r\n constructor(useAerialPerspectiveLut: boolean) {\r\n super();\r\n this.USE_AERIAL_PERSPECTIVE_LUT = useAerialPerspectiveLut;\r\n }\r\n}\r\n\r\nconst OriginOffsetUniformName = \"originOffsetKm\";\r\nconst InverseViewportSizeUniformName = \"inverseViewportSize\";\r\n\r\nconst UboArray = [\r\n { name: OriginOffsetUniformName, size: 3, type: \"vec3\" },\r\n { name: \"_atmoPbrPadding1\", size: 1, type: \"float\" },\r\n { name: InverseViewportSizeUniformName, size: 2, type: \"vec2\" },\r\n];\r\nconst MakeUniforms = (atmosphere: Atmosphere) => ({\r\n ubo: UboArray,\r\n fragment: `uniform vec2 ${InverseViewportSizeUniformName};\\nuniform vec3 ${OriginOffsetUniformName};\\n`,\r\n externalUniforms: atmosphere.uniformBuffer.getUniformNames(),\r\n});\r\n\r\nconst PluginName = \"AtmospherePBRMaterialPlugin\";\r\nconst PluginPriority = 600;\r\nconst OriginOffsetKm = { x: 0, y: 0, z: 0 };\r\n\r\n/**\r\n * Adds shading logic to a PBRMaterial that provides radiance, diffuse sky irradiance, and aerial perspective from the atmosphere.\r\n */\r\nexport class AtmospherePBRMaterialPlugin extends MaterialPluginBase {\r\n /**\r\n * Constructs the {@link AtmospherePBRMaterialPlugin}.\r\n * @param material - The material to apply the plugin to.\r\n * @param _atmosphere - The atmosphere to use for shading.\r\n * @param _isAerialPerspectiveEnabled - Whether to apply aerial perspective.\r\n */\r\n constructor(\r\n material: Material,\r\n private readonly _atmosphere: Atmosphere,\r\n private readonly _isAerialPerspectiveEnabled = false\r\n ) {\r\n super(\r\n material,\r\n PluginName,\r\n PluginPriority,\r\n {\r\n USE_CUSTOM_REFLECTION: _atmosphere.diffuseSkyIrradianceLut !== null,\r\n CUSTOM_FRAGMENT_BEFORE_FOG: _isAerialPerspectiveEnabled,\r\n USE_AERIAL_PERSPECTIVE_LUT: _isAerialPerspectiveEnabled && _atmosphere.isAerialPerspectiveLutEnabled,\r\n APPLY_AERIAL_PERSPECTIVE_INTENSITY: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveIntensity !== 1.0,\r\n APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveRadianceBias !== 0.0,\r\n SAMPLE_TRANSMITTANCE_LUT: true,\r\n EXCLUDE_RAY_MARCHING_FUNCTIONS: true,\r\n },\r\n false, // addPluginToList -- false because we need to control when this is added to the list\r\n true, // enable\r\n true // resolveIncludes\r\n );\r\n\r\n this.doNotSerialize = true;\r\n\r\n // This calls `getCode` so we need to do this after having initialized the class fields.\r\n this._pluginManager._addPlugin(this);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override isCompatible(): boolean {\r\n return true;\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getUniformBuffersNames(_ubos: string[]): void {\r\n const uniformBuffer = this._atmosphere.uniformBuffer;\r\n if (uniformBuffer.useUbo) {\r\n const uboName = this._material.shaderLanguage === ShaderLanguage.WGSL ? \"atmosphere\" : uniformBuffer.name;\r\n _ubos.push(uboName);\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getUniforms() {\r\n return MakeUniforms(this._atmosphere);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override isReadyForSubMesh(): boolean {\r\n let isReady = true;\r\n const atmosphere = this._atmosphere;\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n isReady = isReady && !!aerialPerspectiveLutRenderTarget?.isReady();\r\n }\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n isReady = isReady && !!transmittanceLutRenderTarget?.isReady();\r\n return isReady;\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getActiveTextures(_activeTextures: BaseTexture[]): void {\r\n const atmosphere = this._atmosphere;\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n if (aerialPerspectiveLutRenderTarget) {\r\n _activeTextures.push(aerialPerspectiveLutRenderTarget);\r\n }\r\n }\r\n\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n if (transmittanceLutRenderTarget) {\r\n _activeTextures.push(transmittanceLutRenderTarget);\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override bindForSubMesh(uniformBuffer: UniformBuffer): void {\r\n const atmosphere = this._atmosphere;\r\n const scene = atmosphere.scene;\r\n const engine = scene.getEngine();\r\n\r\n // Bind the atmosphere's uniform buffer to the effect.\r\n const effect = uniformBuffer.currentEffect;\r\n if (effect) {\r\n this._atmosphere.bindUniformBufferToEffect(effect);\r\n }\r\n\r\n // Need the offset to apply which will take a world space position and convert it to a global space position in the atmosphere.\r\n // If floating origin mode is enabled, that offset is the floating origin offset.\r\n // If not, it's an offset up the Y-axis by the planet radius.\r\n uniformBuffer.updateVector3(\r\n OriginOffsetUniformName,\r\n scene.floatingOriginMode\r\n ? Vector3ScaleToRef(scene.floatingOriginOffset, 0.001, OriginOffsetKm) // Convert to kilometers\r\n : Vector3FromFloatsToRef(0, atmosphere.physicalProperties.planetRadius, 0, OriginOffsetKm) // planetRadius is already in kilometers\r\n );\r\n\r\n const width = engine.getRenderWidth();\r\n const height = engine.getRenderHeight();\r\n uniformBuffer.updateFloat2(InverseViewportSizeUniformName, 1.0 / width, 1.0 / height);\r\n\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n uniformBuffer.setTexture(\"aerialPerspectiveLut\", aerialPerspectiveLutRenderTarget);\r\n }\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n uniformBuffer.setTexture(\"transmittanceLut\", transmittanceLutRenderTarget);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override prepareDefines(defines: AtmospherePBRMaterialDefines): void {\r\n const lastUseAerialPerspectiveLut = defines.USE_AERIAL_PERSPECTIVE_LUT;\r\n const lastApplyAerialPerspectiveIntensity = defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY;\r\n const lastApplyAerialPerspectiveRadianceBias = defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS;\r\n defines.USE_AERIAL_PERSPECTIVE_LUT = this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled;\r\n defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveIntensity !== 1.0;\r\n defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveRadianceBias !== 0.0;\r\n if (\r\n lastUseAerialPerspectiveLut !== defines.USE_AERIAL_PERSPECTIVE_LUT ||\r\n lastApplyAerialPerspectiveIntensity !== defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY ||\r\n lastApplyAerialPerspectiveRadianceBias !== defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS\r\n ) {\r\n defines.markAllAsDirty();\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getSamplers(samplers: string[]): void {\r\n samplers.push(\"transmittanceLut\");\r\n if (this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled) {\r\n samplers.push(\"aerialPerspectiveLut\");\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getCustomCode(shaderType: string, shaderLanguage: ShaderLanguage): Nullable<Record<string, string>> {\r\n // Assumed inputs are light0, vPositionW, normalW.\r\n // Only works for directional lights.\r\n if (shaderType !== \"fragment\") {\r\n return null;\r\n }\r\n\r\n const useUbo = this._atmosphere.scene.getEngine().supportsUniformBuffers;\r\n const directionToLightSnippet = useUbo ? \"-light0.vLightData.xyz\" : \"-vLightData0.xyz\";\r\n\r\n const useAtmosphereUbo = shaderLanguage === ShaderLanguage.WGSL || this._atmosphere.uniformBuffer.useUbo;\r\n const atmosphereImportSnippet = useAtmosphereUbo ? \"#include<atmosphereUboDeclaration>\" : \"#include<atmosphereFragmentDeclaration>\";\r\n\r\n if (shaderLanguage === ShaderLanguage.GLSL) {\r\n return {\r\n CUSTOM_FRAGMENT_DEFINITIONS:\r\n this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled\r\n ? `uniform sampler2D transmittanceLut;\\r\\nprecision highp sampler2DArray;\\r\\nuniform sampler2DArray aerialPerspectiveLut;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`\r\n : `uniform sampler2D transmittanceLut;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`,\r\n CUSTOM_LIGHT0_COLOR: `\r\n {\r\n vec3 positionGlobal = 0.001 * vPositionW + ${OriginOffsetUniformName};\r\n float positionRadius = length(positionGlobal);\r\n vec3 geocentricNormal = positionGlobal / positionRadius;\r\n vec3 directionToLight = ${directionToLightSnippet};\r\n float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n diffuse0 = lightIntensity * sampleTransmittanceLut(transmittanceLut, positionRadius, cosAngleLightToZenith);\r\n }\r\n`,\r\n CUSTOM_REFLECTION: `\r\n {\r\n vec3 positionGlobal = 0.001 * vPositionW + ${OriginOffsetUniformName};\r\n float positionRadius = length(positionGlobal);\r\n vec3 geocentricNormal = positionGlobal / positionRadius;\r\n\r\n vec3 directionToLight = ${directionToLightSnippet};\r\n float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n\r\n vec2 uv = vec2(0.5 + 0.5 * cosAngleLightToZenith, (positionRadius - planetRadius) / atmosphereThickness);\r\n float irradianceScaleT = 0.5 * dot(normalW, geocentricNormal) + 0.5;\r\n float irradianceScale = ((-0.6652 * irradianceScaleT) + 1.5927) * irradianceScaleT + 0.1023;\r\n vec3 environmentIrradiance = lightIntensity * sampleReflection(irradianceSampler, uv).rgb;\r\n\r\n // Add a contribution here to estimate indirect lighting.\r\n const float r = 0.2;\r\n float indirect = getLuminance(environmentIrradiance) / max(0.00001, 1. - r);\r\n environmentIrradiance *= irradianceScale;\r\n environmentIrradiance += indirect;\r\n\r\n environmentIrradiance += additionalDiffuseSkyIrradiance;\r\n\r\n const float diffuseBrdf = 1. / PI;\r\n environmentIrradiance *= diffuseBrdf * diffuseSkyIrradianceIntensity;\r\n\r\n reflectionOut.environmentIrradiance = environmentIrradiance;\r\n reflectionOut.environmentRadiance.rgb = reflectionOut.environmentIrradiance;\r\n }\r\n`,\r\n // TODO: Support full ray marching if USE_AERIAL_PERSPECTIVE_LUT is disabled.\r\n CUSTOM_FRAGMENT_BEFORE_FOG: `\r\n #if USE_AERIAL_PERSPECTIVE_LUT\r\n {\r\n float distanceFromCameraKm = 0.001 * distance(vEyePosition.xyz, vPositionW);\r\n vec4 aerialPerspective = vec4(0.);\r\n if (sampleAerialPerspectiveLut(\r\n gl_FragCoord.xy * ${InverseViewportSizeUniformName},\r\n true,\r\n distanceFromCameraKm,\r\n NumAerialPerspectiveLutLayers,\r\n AerialPerspectiveLutKMPerSlice,\r\n AerialPerspectiveLutRangeKM,\r\n aerialPerspective)) {\r\n finalColor = aerialPerspective + (1. - aerialPerspective.a) * finalColor;\r\n }\r\n }\r\n #endif\r\n`,\r\n };\r\n } else {\r\n // WGSL\r\n return {\r\n CUSTOM_FRAGMENT_DEFINITIONS:\r\n this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled\r\n ? `var transmittanceLutSampler: sampler;\\r\\nvar transmittanceLut: texture_2d<f32>;\\r\\nvar aerialPerspectiveLutSampler: sampler;\\r\\nvar aerialPerspectiveLut: texture_2d_array<f32>;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`\r\n : `var transmittanceLutSampler: sampler;\\r\\nvar transmittanceLut: texture_2d<f32>;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`,\r\n CUSTOM_LIGHT0_COLOR: `\r\n {\r\n var positionGlobal = 0.001 * fragmentInputs.vPositionW + uniforms.${OriginOffsetUniformName};\r\n var positionRadius = length(positionGlobal);\r\n var geocentricNormal = positionGlobal / positionRadius;\r\n var directionToLight = ${directionToLightSnippet};\r\n var cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n diffuse0 = atmosphere.lightIntensity * sampleTransmittanceLut(transmittanceLut, positionRadius, cosAngleLightToZenith);\r\n }\r\n`,\r\n CUSTOM_REFLECTION: `\r\n {\r\n var positionGlobal = 0.001 * fragmentInputs.vPositionW + uniforms.${OriginOffsetUniformName};\r\n var positionRadius = length(positionGlobal);\r\n var geocentricNormal = positionGlobal / positionRadius;\r\n\r\n var directionToLight = ${directionToLightSnippet};\r\n var cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n\r\n var uv = vec2f(0.5 + 0.5 * cosAngleLightToZenith, (positionRadius - atmosphere.planetRadius) / atmosphere.atmosphereThickness);\r\n var irradianceScaleT = 0.5 * dot(normalW, geocentricNormal) + 0.5;\r\n var irradianceScale = ((-0.6652 * irradianceScaleT) + 1.5927) * irradianceScaleT + 0.1023;\r\n var environmentIrradiance = atmosphere.lightIntensity * textureSample(irradianceSampler, irradianceSamplerSampler, uv).rgb;\r\n\r\n // Add a contribution here to estimate indirect lighting.\r\n const r = 0.2;\r\n var indirect = getLuminance(environmentIrradiance) / max(0.00001, 1.0 - r);\r\n environmentIrradiance *= irradianceScale;\r\n environmentIrradiance += indirect;\r\n\r\n environmentIrradiance += atmosphere.additionalDiffuseSkyIrradiance;\r\n\r\n const diffuseBrdf = 1.0 / PI;\r\n environmentIrradiance *= diffuseBrdf * atmosphere.diffuseSkyIrradianceIntensity;\r\n\r\n reflectionOut.environmentIrradiance = environmentIrradiance;\r\n reflectionOut.environmentRadiance = vec4f(reflectionOut.environmentIrradiance, reflectionOut.environmentRadiance.a);\r\n }\r\n`,\r\n // TODO: Support full ray marching if USE_AERIAL_PERSPECTIVE_LUT is disabled.\r\n CUSTOM_FRAGMENT_BEFORE_FOG: `\r\n #if USE_AERIAL_PERSPECTIVE_LUT\r\n {\r\n var distanceFromCameraKm = 0.001 * distance(scene.vEyePosition.xyz, fragmentInputs.vPositionW);\r\n var aerialPerspective = vec4f(0.);\r\n if (sampleAerialPerspectiveLut(\r\n fragmentInputs.position.xy * uniforms.${InverseViewportSizeUniformName},\r\n true,\r\n distanceFromCameraKm,\r\n NumAerialPerspectiveLutLayers,\r\n AerialPerspectiveLutKMPerSlice,\r\n AerialPerspectiveLutRangeKM,\r\n &aerialPerspective)) {\r\n finalColor = aerialPerspective + (1. - aerialPerspective.a) * finalColor;\r\n }\r\n }\r\n #endif\r\n`,\r\n };\r\n }\r\n }\r\n}\r\n"]}
|
|
@@ -3,8 +3,6 @@ import type { DirectionalLight } from "@babylonjs/core/Lights/directionalLight.j
|
|
|
3
3
|
import type { IColor3Like, IVector3Like } from "@babylonjs/core/Maths/math.like.js";
|
|
4
4
|
import { Observable } from "@babylonjs/core/Misc/observable.js";
|
|
5
5
|
import { RenderTargetTexture } from "@babylonjs/core/Materials/Textures/renderTargetTexture.js";
|
|
6
|
-
import "./Shaders/fullscreenTriangle.vertex.js";
|
|
7
|
-
import "./Shaders/transmittance.fragment.js";
|
|
8
6
|
/**
|
|
9
7
|
* The transmittance LUT can be used to get the radiance from an external light source arriving a given point, accounting for atmospheric scattering.
|
|
10
8
|
*/
|
|
@@ -30,6 +28,10 @@ export declare class TransmittanceLut {
|
|
|
30
28
|
* @throws if the LUT has been disposed.
|
|
31
29
|
*/
|
|
32
30
|
get renderTarget(): RenderTargetTexture;
|
|
31
|
+
/**
|
|
32
|
+
* True if the LUT data has been read back from the GPU.
|
|
33
|
+
*/
|
|
34
|
+
get hasLutData(): boolean;
|
|
33
35
|
/**
|
|
34
36
|
* Constructs the {@link TransmittanceLut}.
|
|
35
37
|
* @param atmosphere - The atmosphere that owns this LUT.
|
|
@@ -8,8 +8,6 @@ import { Observable } from "@babylonjs/core/Misc/observable.js";
|
|
|
8
8
|
import { RenderTargetTexture } from "@babylonjs/core/Materials/Textures/renderTargetTexture.js";
|
|
9
9
|
import { Sample2DRgbaToRef } from "./sampling.js";
|
|
10
10
|
import { Vector3Dot } from "@babylonjs/core/Maths/math.vector.functions.js";
|
|
11
|
-
import "./Shaders/fullscreenTriangle.vertex.js";
|
|
12
|
-
import "./Shaders/transmittance.fragment.js";
|
|
13
11
|
const LutWidthPx = 256;
|
|
14
12
|
const LutHeightPx = 64;
|
|
15
13
|
const EffectiveDomainInUVSpace = { x: (LutWidthPx - 1.0) / LutWidthPx, y: (LutHeightPx - 1.0) / LutHeightPx };
|
|
@@ -69,6 +67,12 @@ export class TransmittanceLut {
|
|
|
69
67
|
}
|
|
70
68
|
return this._renderTarget;
|
|
71
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* True if the LUT data has been read back from the GPU.
|
|
72
|
+
*/
|
|
73
|
+
get hasLutData() {
|
|
74
|
+
return this._lutData[0] !== undefined;
|
|
75
|
+
}
|
|
72
76
|
/**
|
|
73
77
|
* Constructs the {@link TransmittanceLut}.
|
|
74
78
|
* @param atmosphere - The atmosphere that owns this LUT.
|
|
@@ -99,6 +103,8 @@ export class TransmittanceLut {
|
|
|
99
103
|
renderTarget.skipInitialClear = true;
|
|
100
104
|
const atmosphereUbo = atmosphere.uniformBuffer;
|
|
101
105
|
const useUbo = atmosphereUbo.useUbo;
|
|
106
|
+
const useWebGPU = engine.isWebGPU && !EffectWrapper.ForceGLSL;
|
|
107
|
+
const uboName = useWebGPU ? "atmosphere" : atmosphereUbo.name;
|
|
102
108
|
this._effectWrapper = new EffectWrapper({
|
|
103
109
|
engine,
|
|
104
110
|
name,
|
|
@@ -106,9 +112,18 @@ export class TransmittanceLut {
|
|
|
106
112
|
fragmentShader: "transmittance",
|
|
107
113
|
attributeNames: ["position"],
|
|
108
114
|
uniformNames: ["depth", ...(useUbo ? [] : atmosphereUbo.getUniformNames())],
|
|
109
|
-
uniformBuffers: useUbo ? [
|
|
115
|
+
uniformBuffers: useUbo ? [uboName] : [],
|
|
110
116
|
defines: ["#define POSITION_VEC2"],
|
|
111
117
|
useShaderStore: true,
|
|
118
|
+
shaderLanguage: useWebGPU ? 1 /* ShaderLanguage.WGSL */ : 0 /* ShaderLanguage.GLSL */,
|
|
119
|
+
extraInitializations: (_, list) => {
|
|
120
|
+
if (useWebGPU) {
|
|
121
|
+
list.push(Promise.all([import("./ShadersWGSL/fullscreenTriangle.vertex.js"), import("./ShadersWGSL/transmittance.fragment.js")]));
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
list.push(Promise.all([import("./Shaders/fullscreenTriangle.vertex.js"), import("./Shaders/transmittance.fragment.js")]));
|
|
125
|
+
}
|
|
126
|
+
},
|
|
112
127
|
});
|
|
113
128
|
this._effectRenderer = new EffectRenderer(engine, {
|
|
114
129
|
// Full screen triangle.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transmittanceLut.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/transmittanceLut.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,uDAAyC;AACrE,OAAO,EAAE,SAAS,EAAE,6CAA+B;AAEnD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,oDAAsC;AAC9E,OAAO,EAAE,aAAa,EAAE,6CAA+B;AAGvD,OAAO,EAAE,UAAU,EAAE,2CAA6B;AAClD,OAAO,EAAE,mBAAmB,EAAE,kEAAoD;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,uDAAyC;AAC9D,OAAO,qCAAqC,CAAC;AAC7C,OAAO,kCAAkC,CAAC;AAE1C,MAAM,UAAU,GAAG,GAAG,CAAC;AACvB,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,wBAAwB,GAAG,EAAE,CAAC,EAAE,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AI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC5B,CAAC;CACJ","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT License.\r\n\r\nimport type { Atmosphere } from \"./atmosphere\";\r\nimport type { AtmospherePhysicalProperties } from \"./atmospherePhysicalProperties\";\r\nimport { Clamp, SmoothStep } from \"core/Maths/math.scalar.functions\";\r\nimport { Constants } from \"core/Engines/constants\";\r\nimport type { DirectionalLight } from \"core/Lights/directionalLight\";\r\nimport { EffectRenderer, EffectWrapper } from \"core/Materials/effectRenderer\";\r\nimport { FromHalfFloat } from \"core/Misc/textureTools\";\r\nimport type { IColor3Like, IColor4Like, IVector2Like, IVector3Like } from \"core/Maths/math.like\";\r\nimport type { Nullable } from \"core/types\";\r\nimport { Observable } from \"core/Misc/observable\";\r\nimport { RenderTargetTexture } from \"core/Materials/Textures/renderTargetTexture\";\r\nimport { Sample2DRgbaToRef } from \"./sampling\";\r\nimport { Vector3Dot } from \"core/Maths/math.vector.functions\";\r\nimport \"./Shaders/fullscreenTriangle.vertex\";\r\nimport \"./Shaders/transmittance.fragment\";\r\n\r\nconst LutWidthPx = 256;\r\nconst LutHeightPx = 64;\r\nconst EffectiveDomainInUVSpace = { x: (LutWidthPx - 1.0) / LutWidthPx, y: (LutHeightPx - 1.0) / LutHeightPx };\r\nconst HalfTexelSize = { x: 0.5 / LutWidthPx, y: 0.5 / LutHeightPx };\r\nconst TransmittanceHorizonRange = 2.0 * HalfTexelSize.x;\r\nconst TransmittanceMaxUnoccludedU = 1.0 - 0.5 * TransmittanceHorizonRange;\r\n\r\nconst UseHalfFloat = false;\r\n\r\n// Temporary storage.\r\nconst Uv = { x: Number.NaN, y: Number.NaN } as IVector2Like;\r\nconst LightColorTemp = { r: Number.NaN, g: Number.NaN, b: Number.NaN } as IColor3Like;\r\nconst DirectionToLightTemp = { x: Number.NaN, y: Number.NaN, z: Number.NaN } as IVector3Like;\r\nconst Color4Temp = { r: Number.NaN, g: Number.NaN, b: Number.NaN, a: Number.NaN } as IColor4Like;\r\n\r\nconst ComputeLutUVToRef = (properties: AtmospherePhysicalProperties, radius: number, cosAngleLightToZenith: number, uv: IVector2Like): void => {\r\n const radiusSquared = radius * radius;\r\n const distanceToHorizon = Math.sqrt(Math.max(0.0, radiusSquared - properties.planetRadiusSquared));\r\n\r\n const cosAngleLightToZenithSq = cosAngleLightToZenith * cosAngleLightToZenith;\r\n const discriminant = radiusSquared * (cosAngleLightToZenithSq - 1.0) + properties.atmosphereRadiusSquared;\r\n const distanceToAtmosphereEdge = Math.max(0.0, -radius * cosAngleLightToZenith + Math.sqrt(Math.max(0.0, discriminant)));\r\n\r\n const minDistanceToAtmosphereEdge = Math.max(0.0, properties.atmosphereRadius - radius);\r\n const maxDistanceToAtmosphereEdge = distanceToHorizon + properties.horizonDistanceToAtmosphereEdge;\r\n const cosAngleLightToZenithCoordinate =\r\n (distanceToAtmosphereEdge - minDistanceToAtmosphereEdge) / Math.max(0.000001, maxDistanceToAtmosphereEdge - minDistanceToAtmosphereEdge);\r\n const distanceToHorizonCoordinate = distanceToHorizon / Math.max(0.000001, properties.horizonDistanceToAtmosphereEdge);\r\n\r\n // Unit to UV.\r\n uv.x = EffectiveDomainInUVSpace.x * cosAngleLightToZenithCoordinate + HalfTexelSize.x;\r\n uv.y = EffectiveDomainInUVSpace.y * distanceToHorizonCoordinate + HalfTexelSize.y;\r\n};\r\n\r\nconst SampleLutToRef = (\r\n properties: AtmospherePhysicalProperties,\r\n lutData: Uint8Array | Uint16Array,\r\n positionDistanceToOrigin: number,\r\n cosAngleLightToZenith: number,\r\n result: IColor4Like\r\n): void => {\r\n if (positionDistanceToOrigin > properties.atmosphereRadius) {\r\n result.r = result.g = result.b = result.a = 1.0;\r\n return;\r\n }\r\n\r\n ComputeLutUVToRef(properties, positionDistanceToOrigin, cosAngleLightToZenith, Uv);\r\n Sample2DRgbaToRef(Uv.x, Uv.y, LutWidthPx, LutHeightPx, lutData, result, UseHalfFloat ? FromHalfFloat : (value) => value / 255.0);\r\n\r\n const weight = Clamp(SmoothStep(1.0, 0.0, Clamp((Uv.x - TransmittanceMaxUnoccludedU) / TransmittanceHorizonRange)));\r\n result.r *= weight;\r\n result.g *= weight;\r\n result.b *= weight;\r\n result.a *= weight;\r\n};\r\n\r\n/**\r\n * The transmittance LUT can be used to get the radiance from an external light source arriving a given point, accounting for atmospheric scattering.\r\n */\r\nexport class TransmittanceLut {\r\n /**\r\n * Listen to this observer to know when the LUT data has been updated.\r\n * This is typically infrequent (once at startup), but also happens whenever the atmosphere's properties change.\r\n */\r\n public readonly onUpdatedObservable = new Observable<void>();\r\n\r\n private readonly _atmosphere: Atmosphere;\r\n private _lutData: Uint8Array | Uint16Array = new Uint8Array(0);\r\n private _renderTarget: Nullable<RenderTargetTexture>;\r\n private _effectWrapper: Nullable<EffectWrapper>;\r\n private _effectRenderer: Nullable<EffectRenderer>;\r\n private _isDirty = true;\r\n private _isDisposed = false;\r\n\r\n /**\r\n * True if the LUT has been rendered.\r\n */\r\n public get isDirty(): boolean {\r\n return this._isDirty;\r\n }\r\n\r\n /**\r\n * The render target that contains the transmittance LUT.\r\n * @throws if the LUT has been disposed.\r\n */\r\n public get renderTarget(): RenderTargetTexture {\r\n if (this._isDisposed || this._renderTarget === null) {\r\n throw new Error();\r\n }\r\n return this._renderTarget;\r\n }\r\n\r\n /**\r\n * Constructs the {@link TransmittanceLut}.\r\n * @param atmosphere - The atmosphere that owns this LUT.\r\n */\r\n constructor(atmosphere: Atmosphere) {\r\n this._atmosphere = atmosphere;\r\n\r\n const scene = this._atmosphere.scene;\r\n const engine = scene.getEngine();\r\n const useHalfFloat = UseHalfFloat && engine.getCaps().textureHalfFloatRender;\r\n\r\n const name = \"atmo-transmittance\";\r\n const renderTarget = (this._renderTarget = new RenderTargetTexture(name, { width: LutWidthPx, height: LutHeightPx }, scene, {\r\n type: useHalfFloat ? Constants.TEXTURETYPE_HALF_FLOAT : Constants.TEXTURETYPE_UNSIGNED_BYTE,\r\n samplingMode: Constants.TEXTURE_BILINEAR_SAMPLINGMODE,\r\n generateDepthBuffer: false,\r\n gammaSpace: false,\r\n }));\r\n renderTarget.wrapU = Constants.TEXTURE_CLAMP_ADDRESSMODE;\r\n renderTarget.wrapV = Constants.TEXTURE_CLAMP_ADDRESSMODE;\r\n renderTarget.anisotropicFilteringLevel = 1;\r\n renderTarget.skipInitialClear = true;\r\n\r\n const atmosphereUbo = atmosphere.uniformBuffer;\r\n const useUbo = atmosphereUbo.useUbo;\r\n this._effectWrapper = new EffectWrapper({\r\n engine,\r\n name,\r\n vertexShader: \"fullscreenTriangle\",\r\n fragmentShader: \"transmittance\",\r\n attributeNames: [\"position\"],\r\n uniformNames: [\"depth\", ...(useUbo ? [] : atmosphereUbo.getUniformNames())],\r\n uniformBuffers: useUbo ? [atmosphereUbo.name] : [],\r\n defines: [\"#define POSITION_VEC2\"],\r\n useShaderStore: true,\r\n });\r\n\r\n this._effectRenderer = new EffectRenderer(engine, {\r\n // Full screen triangle.\r\n indices: [0, 2, 1],\r\n positions: [-1, -1, -1, 3, 3, -1],\r\n });\r\n }\r\n\r\n /**\r\n * Gets the transmittance of an external light through the atmosphere to a point specified by its distance to the planet center and its geocentric normal.\r\n * The result is always a linear space color.\r\n * @param directionToLight - The direction to the light source.\r\n * @param pointRadius - The distance from the origin to the point.\r\n * @param pointGeocentricNormal - The normal of the point.\r\n * @param result - The color to write the result to.\r\n * @returns The result color.\r\n */\r\n public getTransmittedColorToRef<T extends IColor3Like>(directionToLight: IVector3Like, pointRadius: number, pointGeocentricNormal: IVector3Like, result: T): T {\r\n if (this._lutData[0] !== undefined) {\r\n const cosAngleLightToZenith = Vector3Dot(directionToLight, pointGeocentricNormal);\r\n SampleLutToRef(this._atmosphere.physicalProperties, this._lutData, pointRadius, cosAngleLightToZenith, Color4Temp);\r\n result.r = Color4Temp.r;\r\n result.g = Color4Temp.g;\r\n result.b = Color4Temp.b;\r\n } else {\r\n // Fallback.\r\n result.r = result.g = result.b = 1.0;\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Derives light color from the transmittance at a point specified by its distance to the planet center and its geocentric normal.\r\n * @param light - The light to update.\r\n * @param pointRadius - The distance from the origin to the point.\r\n * @param pointGeocentricNormal - The normal of the point.\r\n */\r\n public updateLightParameters(light: DirectionalLight, pointRadius: number, pointGeocentricNormal: IVector3Like): void {\r\n const lightDirection = light.direction;\r\n DirectionToLightTemp.x = -lightDirection.x;\r\n DirectionToLightTemp.y = -lightDirection.y;\r\n DirectionToLightTemp.z = -lightDirection.z;\r\n this.getTransmittedColorToRef(DirectionToLightTemp, pointRadius, pointGeocentricNormal, LightColorTemp);\r\n\r\n light.diffuse.copyFromFloats(LightColorTemp.r, LightColorTemp.g, LightColorTemp.b);\r\n light.specular.copyFromFloats(LightColorTemp.r, LightColorTemp.g, LightColorTemp.b);\r\n }\r\n\r\n /**\r\n * Renders the LUT if needed.\r\n * @returns true if the LUT was rendered.\r\n */\r\n public render(): boolean {\r\n // Only need to render the LUT once.\r\n const effectWrapper = this._effectWrapper;\r\n if (!this._isDirty || !effectWrapper?.isReady() || !this._renderTarget?.isReady()) {\r\n return false;\r\n }\r\n\r\n const engine = this._atmosphere.scene.getEngine();\r\n\r\n engine.bindFramebuffer(this.renderTarget.renderTarget!, undefined, undefined, undefined, true);\r\n\r\n const effectRenderer = this._effectRenderer!;\r\n effectRenderer.applyEffectWrapper(effectWrapper);\r\n\r\n effectRenderer.saveStates();\r\n effectRenderer.setViewport();\r\n\r\n const effect = effectWrapper.effect;\r\n effectRenderer.bindBuffers(effect);\r\n\r\n this._atmosphere.bindUniformBufferToEffect(effect);\r\n\r\n effect.setFloat(\"depth\", 0);\r\n\r\n effectRenderer.draw();\r\n\r\n effectRenderer.restoreStates();\r\n engine.restoreDefaultFramebuffer();\r\n\r\n this._isDirty = false;\r\n\r\n // eslint-disable-next-line github/no-then\r\n void this.renderTarget.readPixels(0, 0, undefined, undefined, UseHalfFloat /* noDataConversion */)?.then((value: ArrayBufferView) => {\r\n if (this._isDisposed) {\r\n return;\r\n }\r\n this._lutData = value as Uint8Array | Uint16Array;\r\n this.onUpdatedObservable.notifyObservers();\r\n });\r\n\r\n return true;\r\n }\r\n\r\n /**\r\n * Marks the LUT as needing to be rendered.\r\n */\r\n public markDirty(): void {\r\n this._isDirty = true;\r\n }\r\n\r\n /**\r\n * Disposes the LUT and its resources.\r\n */\r\n public dispose(): void {\r\n this._renderTarget?.dispose();\r\n this._renderTarget = null;\r\n this._effectWrapper?.dispose();\r\n this._effectWrapper = null;\r\n this._effectRenderer?.dispose();\r\n this._effectRenderer = null;\r\n this._isDisposed = true;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"transmittanceLut.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/transmittanceLut.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,uDAAyC;AACrE,OAAO,EAAE,SAAS,EAAE,6CAA+B;AAEnD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,oDAAsC;AAC9E,OAAO,EAAE,aAAa,EAAE,6CAA+B;AAGvD,OAAO,EAAE,UAAU,EAAE,2CAA6B;AAClD,OAAO,EAAE,mBAAmB,EAAE,kEAAoD;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,uDAAyC;AAG9D,MAAM,UAAU,GAAG,GAAG,CAAC;AACvB,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,wBAAwB,GAAG,EAAE,CAAC,EAAE,CAAC,UAAU,GAAG,GAAG,CAAC,GAAG,UAAU,EAAE,CAAC,EAAE,CAAC,WAAW,GAAG,GAAG,CAAC,GAAG,WAAW,EAAE,CAAC;AAC9G,MAAM,aAAa,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,UAAU,EAAE,CAAC,EAAE,GAAG,GAAG,WAAW,EAAE,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,CAAC;YACD,IAAI,CAAC,QAAQ,GAAG,KAAiC,CAAC;YAClD,IAAI,CAAC,mBAAmB,CAAC,eAAe,EAAE,CAAC;QAC/C,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;OAEG;IACI,SAAS;QACZ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACzB,CAAC;IAED;;OAEG;IACI,OAAO;QACV,IAAI,CAAC,aAAa,EAAE,OAAO,EAAE,CAAC;QAC9B,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;QAC1B,IAAI,CAAC,cAAc,EAAE,OAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;QAC3B,IAAI,CAAC,eAAe,EAAE,OAAO,EAAE,CAAC;QAChC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QAC5B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC5B,CAAC;CACJ","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT License.\r\n\r\nimport type { Atmosphere } from \"./atmosphere\";\r\nimport type { AtmospherePhysicalProperties } from \"./atmospherePhysicalProperties\";\r\nimport { Clamp, SmoothStep } from \"core/Maths/math.scalar.functions\";\r\nimport { Constants } from \"core/Engines/constants\";\r\nimport type { DirectionalLight } from \"core/Lights/directionalLight\";\r\nimport { EffectRenderer, EffectWrapper } from \"core/Materials/effectRenderer\";\r\nimport { FromHalfFloat } from \"core/Misc/textureTools\";\r\nimport type { IColor3Like, IColor4Like, IVector2Like, IVector3Like } from \"core/Maths/math.like\";\r\nimport type { Nullable } from \"core/types\";\r\nimport { Observable } from \"core/Misc/observable\";\r\nimport { RenderTargetTexture } from \"core/Materials/Textures/renderTargetTexture\";\r\nimport { Sample2DRgbaToRef } from \"./sampling\";\r\nimport { Vector3Dot } from \"core/Maths/math.vector.functions\";\r\nimport { ShaderLanguage } from \"core/Materials/shaderLanguage\";\r\n\r\nconst LutWidthPx = 256;\r\nconst LutHeightPx = 64;\r\nconst EffectiveDomainInUVSpace = { x: (LutWidthPx - 1.0) / LutWidthPx, y: (LutHeightPx - 1.0) / LutHeightPx };\r\nconst HalfTexelSize = { x: 0.5 / LutWidthPx, y: 0.5 / LutHeightPx };\r\nconst TransmittanceHorizonRange = 2.0 * HalfTexelSize.x;\r\nconst TransmittanceMaxUnoccludedU = 1.0 - 0.5 * TransmittanceHorizonRange;\r\n\r\nconst UseHalfFloat = false;\r\n\r\n// Temporary storage.\r\nconst Uv = { x: Number.NaN, y: Number.NaN } as IVector2Like;\r\nconst LightColorTemp = { r: Number.NaN, g: Number.NaN, b: Number.NaN } as IColor3Like;\r\nconst DirectionToLightTemp = { x: Number.NaN, y: Number.NaN, z: Number.NaN } as IVector3Like;\r\nconst Color4Temp = { r: Number.NaN, g: Number.NaN, b: Number.NaN, a: Number.NaN } as IColor4Like;\r\n\r\nconst ComputeLutUVToRef = (properties: AtmospherePhysicalProperties, radius: number, cosAngleLightToZenith: number, uv: IVector2Like): void => {\r\n const radiusSquared = radius * radius;\r\n const distanceToHorizon = Math.sqrt(Math.max(0.0, radiusSquared - properties.planetRadiusSquared));\r\n\r\n const cosAngleLightToZenithSq = cosAngleLightToZenith * cosAngleLightToZenith;\r\n const discriminant = radiusSquared * (cosAngleLightToZenithSq - 1.0) + properties.atmosphereRadiusSquared;\r\n const distanceToAtmosphereEdge = Math.max(0.0, -radius * cosAngleLightToZenith + Math.sqrt(Math.max(0.0, discriminant)));\r\n\r\n const minDistanceToAtmosphereEdge = Math.max(0.0, properties.atmosphereRadius - radius);\r\n const maxDistanceToAtmosphereEdge = distanceToHorizon + properties.horizonDistanceToAtmosphereEdge;\r\n const cosAngleLightToZenithCoordinate =\r\n (distanceToAtmosphereEdge - minDistanceToAtmosphereEdge) / Math.max(0.000001, maxDistanceToAtmosphereEdge - minDistanceToAtmosphereEdge);\r\n const distanceToHorizonCoordinate = distanceToHorizon / Math.max(0.000001, properties.horizonDistanceToAtmosphereEdge);\r\n\r\n // Unit to UV.\r\n uv.x = EffectiveDomainInUVSpace.x * cosAngleLightToZenithCoordinate + HalfTexelSize.x;\r\n uv.y = EffectiveDomainInUVSpace.y * distanceToHorizonCoordinate + HalfTexelSize.y;\r\n};\r\n\r\nconst SampleLutToRef = (\r\n properties: AtmospherePhysicalProperties,\r\n lutData: Uint8Array | Uint16Array,\r\n positionDistanceToOrigin: number,\r\n cosAngleLightToZenith: number,\r\n result: IColor4Like\r\n): void => {\r\n if (positionDistanceToOrigin > properties.atmosphereRadius) {\r\n result.r = result.g = result.b = result.a = 1.0;\r\n return;\r\n }\r\n\r\n ComputeLutUVToRef(properties, positionDistanceToOrigin, cosAngleLightToZenith, Uv);\r\n Sample2DRgbaToRef(Uv.x, Uv.y, LutWidthPx, LutHeightPx, lutData, result, UseHalfFloat ? FromHalfFloat : (value) => value / 255.0);\r\n\r\n const weight = Clamp(SmoothStep(1.0, 0.0, Clamp((Uv.x - TransmittanceMaxUnoccludedU) / TransmittanceHorizonRange)));\r\n result.r *= weight;\r\n result.g *= weight;\r\n result.b *= weight;\r\n result.a *= weight;\r\n};\r\n\r\n/**\r\n * The transmittance LUT can be used to get the radiance from an external light source arriving a given point, accounting for atmospheric scattering.\r\n */\r\nexport class TransmittanceLut {\r\n /**\r\n * Listen to this observer to know when the LUT data has been updated.\r\n * This is typically infrequent (once at startup), but also happens whenever the atmosphere's properties change.\r\n */\r\n public readonly onUpdatedObservable = new Observable<void>();\r\n\r\n private readonly _atmosphere: Atmosphere;\r\n private _lutData: Uint8Array | Uint16Array = new Uint8Array(0);\r\n private _renderTarget: Nullable<RenderTargetTexture>;\r\n private _effectWrapper: Nullable<EffectWrapper>;\r\n private _effectRenderer: Nullable<EffectRenderer>;\r\n private _isDirty = true;\r\n private _isDisposed = false;\r\n\r\n /**\r\n * True if the LUT has been rendered.\r\n */\r\n public get isDirty(): boolean {\r\n return this._isDirty;\r\n }\r\n\r\n /**\r\n * The render target that contains the transmittance LUT.\r\n * @throws if the LUT has been disposed.\r\n */\r\n public get renderTarget(): RenderTargetTexture {\r\n if (this._isDisposed || this._renderTarget === null) {\r\n throw new Error();\r\n }\r\n return this._renderTarget;\r\n }\r\n\r\n /**\r\n * True if the LUT data has been read back from the GPU.\r\n */\r\n public get hasLutData(): boolean {\r\n return this._lutData[0] !== undefined;\r\n }\r\n\r\n /**\r\n * Constructs the {@link TransmittanceLut}.\r\n * @param atmosphere - The atmosphere that owns this LUT.\r\n */\r\n constructor(atmosphere: Atmosphere) {\r\n this._atmosphere = atmosphere;\r\n\r\n const scene = this._atmosphere.scene;\r\n const engine = scene.getEngine();\r\n const useHalfFloat = UseHalfFloat && engine.getCaps().textureHalfFloatRender;\r\n\r\n const name = \"atmo-transmittance\";\r\n const renderTarget = (this._renderTarget = new RenderTargetTexture(name, { width: LutWidthPx, height: LutHeightPx }, scene, {\r\n type: useHalfFloat ? Constants.TEXTURETYPE_HALF_FLOAT : Constants.TEXTURETYPE_UNSIGNED_BYTE,\r\n samplingMode: Constants.TEXTURE_BILINEAR_SAMPLINGMODE,\r\n generateDepthBuffer: false,\r\n gammaSpace: false,\r\n }));\r\n renderTarget.wrapU = Constants.TEXTURE_CLAMP_ADDRESSMODE;\r\n renderTarget.wrapV = Constants.TEXTURE_CLAMP_ADDRESSMODE;\r\n renderTarget.anisotropicFilteringLevel = 1;\r\n renderTarget.skipInitialClear = true;\r\n\r\n const atmosphereUbo = atmosphere.uniformBuffer;\r\n const useUbo = atmosphereUbo.useUbo;\r\n const useWebGPU = engine.isWebGPU && !EffectWrapper.ForceGLSL;\r\n const uboName = useWebGPU ? \"atmosphere\" : atmosphereUbo.name;\r\n this._effectWrapper = new EffectWrapper({\r\n engine,\r\n name,\r\n vertexShader: \"fullscreenTriangle\",\r\n fragmentShader: \"transmittance\",\r\n attributeNames: [\"position\"],\r\n uniformNames: [\"depth\", ...(useUbo ? [] : atmosphereUbo.getUniformNames())],\r\n uniformBuffers: useUbo ? [uboName] : [],\r\n defines: [\"#define POSITION_VEC2\"],\r\n useShaderStore: true,\r\n shaderLanguage: useWebGPU ? ShaderLanguage.WGSL : ShaderLanguage.GLSL,\r\n extraInitializations: (_, list) => {\r\n if (useWebGPU) {\r\n list.push(Promise.all([import(\"./ShadersWGSL/fullscreenTriangle.vertex\"), import(\"./ShadersWGSL/transmittance.fragment\")]));\r\n } else {\r\n list.push(Promise.all([import(\"./Shaders/fullscreenTriangle.vertex\"), import(\"./Shaders/transmittance.fragment\")]));\r\n }\r\n },\r\n });\r\n\r\n this._effectRenderer = new EffectRenderer(engine, {\r\n // Full screen triangle.\r\n indices: [0, 2, 1],\r\n positions: [-1, -1, -1, 3, 3, -1],\r\n });\r\n }\r\n\r\n /**\r\n * Gets the transmittance of an external light through the atmosphere to a point specified by its distance to the planet center and its geocentric normal.\r\n * The result is always a linear space color.\r\n * @param directionToLight - The direction to the light source.\r\n * @param pointRadius - The distance from the origin to the point.\r\n * @param pointGeocentricNormal - The normal of the point.\r\n * @param result - The color to write the result to.\r\n * @returns The result color.\r\n */\r\n public getTransmittedColorToRef<T extends IColor3Like>(directionToLight: IVector3Like, pointRadius: number, pointGeocentricNormal: IVector3Like, result: T): T {\r\n if (this._lutData[0] !== undefined) {\r\n const cosAngleLightToZenith = Vector3Dot(directionToLight, pointGeocentricNormal);\r\n SampleLutToRef(this._atmosphere.physicalProperties, this._lutData, pointRadius, cosAngleLightToZenith, Color4Temp);\r\n result.r = Color4Temp.r;\r\n result.g = Color4Temp.g;\r\n result.b = Color4Temp.b;\r\n } else {\r\n // Fallback.\r\n result.r = result.g = result.b = 1.0;\r\n }\r\n return result;\r\n }\r\n\r\n /**\r\n * Derives light color from the transmittance at a point specified by its distance to the planet center and its geocentric normal.\r\n * @param light - The light to update.\r\n * @param pointRadius - The distance from the origin to the point.\r\n * @param pointGeocentricNormal - The normal of the point.\r\n */\r\n public updateLightParameters(light: DirectionalLight, pointRadius: number, pointGeocentricNormal: IVector3Like): void {\r\n const lightDirection = light.direction;\r\n DirectionToLightTemp.x = -lightDirection.x;\r\n DirectionToLightTemp.y = -lightDirection.y;\r\n DirectionToLightTemp.z = -lightDirection.z;\r\n this.getTransmittedColorToRef(DirectionToLightTemp, pointRadius, pointGeocentricNormal, LightColorTemp);\r\n\r\n light.diffuse.copyFromFloats(LightColorTemp.r, LightColorTemp.g, LightColorTemp.b);\r\n light.specular.copyFromFloats(LightColorTemp.r, LightColorTemp.g, LightColorTemp.b);\r\n }\r\n\r\n /**\r\n * Renders the LUT if needed.\r\n * @returns true if the LUT was rendered.\r\n */\r\n public render(): boolean {\r\n // Only need to render the LUT once.\r\n const effectWrapper = this._effectWrapper;\r\n if (!this._isDirty || !effectWrapper?.isReady() || !this._renderTarget?.isReady()) {\r\n return false;\r\n }\r\n\r\n const engine = this._atmosphere.scene.getEngine();\r\n\r\n engine.bindFramebuffer(this.renderTarget.renderTarget!, undefined, undefined, undefined, true);\r\n\r\n const effectRenderer = this._effectRenderer!;\r\n effectRenderer.applyEffectWrapper(effectWrapper);\r\n\r\n effectRenderer.saveStates();\r\n effectRenderer.setViewport();\r\n\r\n const effect = effectWrapper.effect;\r\n effectRenderer.bindBuffers(effect);\r\n\r\n this._atmosphere.bindUniformBufferToEffect(effect);\r\n\r\n effect.setFloat(\"depth\", 0);\r\n\r\n effectRenderer.draw();\r\n\r\n effectRenderer.restoreStates();\r\n engine.restoreDefaultFramebuffer();\r\n\r\n this._isDirty = false;\r\n\r\n // eslint-disable-next-line github/no-then\r\n void this.renderTarget.readPixels(0, 0, undefined, undefined, UseHalfFloat /* noDataConversion */)?.then((value: ArrayBufferView) => {\r\n if (this._isDisposed) {\r\n return;\r\n }\r\n this._lutData = value as Uint8Array | Uint16Array;\r\n this.onUpdatedObservable.notifyObservers();\r\n });\r\n\r\n return true;\r\n }\r\n\r\n /**\r\n * Marks the LUT as needing to be rendered.\r\n */\r\n public markDirty(): void {\r\n this._isDirty = true;\r\n }\r\n\r\n /**\r\n * Disposes the LUT and its resources.\r\n */\r\n public dispose(): void {\r\n this._renderTarget?.dispose();\r\n this._renderTarget = null;\r\n this._effectWrapper?.dispose();\r\n this._effectWrapper = null;\r\n this._effectRenderer?.dispose();\r\n this._effectRenderer = null;\r\n this._isDisposed = true;\r\n }\r\n}\r\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@babylonjs/addons",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.47.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"module": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"postcompile": "build-tools -c add-js-to-es6"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@babylonjs/core": "
|
|
22
|
+
"@babylonjs/core": "8.47.1",
|
|
23
23
|
"@dev/addons": "^1.0.0",
|
|
24
24
|
"@dev/build-tools": "^1.0.0"
|
|
25
25
|
},
|
|
@@ -45,4 +45,4 @@
|
|
|
45
45
|
"bugs": {
|
|
46
46
|
"url": "https://github.com/BabylonJS/Babylon.js/issues"
|
|
47
47
|
}
|
|
48
|
-
}
|
|
48
|
+
}
|