@babylonjs/addons 9.14.0 → 9.15.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.
@@ -6,8 +6,6 @@ import { MaterialPluginBase } from "@babylonjs/core/Materials/materialPluginBase
6
6
  import { type Nullable } from "@babylonjs/core/types.js";
7
7
  import { type UniformBuffer } from "@babylonjs/core/Materials/uniformBuffer.js";
8
8
  import { ShaderLanguage } from "@babylonjs/core/Materials/shaderLanguage.js";
9
- import "./ShadersWGSL/ShadersInclude/atmosphereFunctions.js";
10
- import "./ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js";
11
9
  declare class AtmospherePBRMaterialDefines extends MaterialDefines {
12
10
  USE_CUSTOM_REFLECTION: boolean;
13
11
  USE_AERIAL_PERSPECTIVE_LUT: boolean;
@@ -27,6 +25,10 @@ declare class AtmospherePBRMaterialDefines extends MaterialDefines {
27
25
  export declare class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
28
26
  private readonly _atmosphere;
29
27
  private readonly _isAerialPerspectiveEnabled;
28
+ private static readonly _ShaderIncludesRetryDelayMs;
29
+ private _shaderIncludesLoaded;
30
+ private _shaderIncludesLoadPromise;
31
+ private _shaderIncludesNextRetryTime;
30
32
  /**
31
33
  * Constructs the {@link AtmospherePBRMaterialPlugin}.
32
34
  * @param material - The material to apply the plugin to.
@@ -58,6 +60,12 @@ export declare class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
58
60
  * @override
59
61
  */
60
62
  isReadyForSubMesh(): boolean;
63
+ /**
64
+ * Lazily loads (and thereby registers into the ShaderStore) the shader includes referenced by
65
+ * this plugin's injected custom code. Loading the correct variant for the host material's shader
66
+ * language keeps this module free of top-level shader side effects so it can be tree-shaken.
67
+ */
68
+ private _loadShaderIncludesAsync;
61
69
  /**
62
70
  * @override
63
71
  */
@@ -2,9 +2,8 @@
2
2
  // Licensed under the MIT License.
3
3
  import { MaterialDefines } from "@babylonjs/core/Materials/materialDefines.js";
4
4
  import { MaterialPluginBase } from "@babylonjs/core/Materials/materialPluginBase.pure.js";
5
+ import { Logger } from "@babylonjs/core/Misc/logger.js";
5
6
  import { Vector3FromFloatsToRef, Vector3ScaleToRef } from "@babylonjs/core/Maths/math.vector.functions.js";
6
- import "./ShadersWGSL/ShadersInclude/atmosphereFunctions.js";
7
- import "./ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js";
8
7
  class AtmospherePBRMaterialDefines extends MaterialDefines {
9
8
  /**
10
9
  * Constructs the {@link AtmospherePBRMaterialDefines}.
@@ -65,6 +64,9 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
65
64
  );
66
65
  this._atmosphere = _atmosphere;
67
66
  this._isAerialPerspectiveEnabled = _isAerialPerspectiveEnabled;
67
+ this._shaderIncludesLoaded = false;
68
+ this._shaderIncludesLoadPromise = null;
69
+ this._shaderIncludesNextRetryTime = 0;
68
70
  this.doNotSerialize = true;
69
71
  // This calls `getCode` so we need to do this after having initialized the class fields.
70
72
  this._pluginManager._addPlugin(this);
@@ -95,6 +97,16 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
95
97
  * @override
96
98
  */
97
99
  isReadyForSubMesh() {
100
+ // The shader includes used by this plugin's custom code (injected via getCustomCode) are
101
+ // registered into the ShaderStore lazily so this module stays free of top-level side effects.
102
+ // Gate readiness until they are loaded, otherwise the host material's effect would fail to
103
+ // resolve `#include<atmosphereFunctions>` and friends at compile time.
104
+ if (!this._shaderIncludesLoaded) {
105
+ if (!this._shaderIncludesLoadPromise && Date.now() >= this._shaderIncludesNextRetryTime) {
106
+ this._shaderIncludesLoadPromise = this._loadShaderIncludesAsync();
107
+ }
108
+ return false;
109
+ }
98
110
  const atmosphere = this._atmosphere;
99
111
  if (!atmosphere.transmittanceLut?.hasLutData || (atmosphere.diffuseSkyIrradianceLut && !atmosphere.diffuseSkyIrradianceLut.hasLutData)) {
100
112
  return false;
@@ -107,6 +119,35 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
107
119
  }
108
120
  return true;
109
121
  }
122
+ /**
123
+ * Lazily loads (and thereby registers into the ShaderStore) the shader includes referenced by
124
+ * this plugin's injected custom code. Loading the correct variant for the host material's shader
125
+ * language keeps this module free of top-level shader side effects so it can be tree-shaken.
126
+ */
127
+ async _loadShaderIncludesAsync() {
128
+ try {
129
+ if (this._material.shaderLanguage === 1 /* ShaderLanguage.WGSL */) {
130
+ await Promise.all([import("./ShadersWGSL/ShadersInclude/atmosphereFunctions.js"), import("./ShadersWGSL/ShadersInclude/atmosphereUboDeclaration.js")]);
131
+ }
132
+ else {
133
+ await Promise.all([
134
+ import("./Shaders/ShadersInclude/atmosphereFunctions.js"),
135
+ import("./Shaders/ShadersInclude/atmosphereUboDeclaration.js"),
136
+ import("./Shaders/ShadersInclude/atmosphereFragmentDeclaration.js"),
137
+ ]);
138
+ }
139
+ this._shaderIncludesLoaded = true;
140
+ }
141
+ catch (error) {
142
+ // Surface the failure (a missing shader-include registration would otherwise silently stall
143
+ // rendering forever) and back off before retrying so we don't hammer the loader every frame.
144
+ // A retry still allows recovery from transient failures such as a dropped network request.
145
+ Logger.Error(`AtmospherePBRMaterialPlugin: Failed to load atmosphere shader includes; the atmosphere material will not render until they load. Retrying in ${AtmospherePBRMaterialPlugin._ShaderIncludesRetryDelayMs / 1000}s. ${error}`);
146
+ this._shaderIncludesNextRetryTime = Date.now() + AtmospherePBRMaterialPlugin._ShaderIncludesRetryDelayMs;
147
+ // Clear the in-flight promise so a later readiness check can retry the load.
148
+ this._shaderIncludesLoadPromise = null;
149
+ }
150
+ }
110
151
  /**
111
152
  * @override
112
153
  */
@@ -339,4 +380,5 @@ export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
339
380
  }
340
381
  }
341
382
  }
383
+ AtmospherePBRMaterialPlugin._ShaderIncludesRetryDelayMs = 5000;
342
384
  //# sourceMappingURL=atmospherePBRMaterialPlugin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"atmospherePBRMater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ACY,CAAC;QACN,CAAC;aAAM,CAAC;YACJ,OAAO;YACP,OAAO;gBACH,2BAA2B,EACvB,IAAI,CAAC,2BAA2B,IAAI,IAAI,CAAC,WAAW,CAAC,6BAA6B;oBAC9E,CAAC,CAAC,uLAAuL,uBAAuB,mCAAmC;oBACnP,CAAC,CAAC,sFAAsF,uBAAuB,mCAAmC;gBAE1J,wEAAwE;gBACxE,mBAAmB,EAAE;;oFAE+C,uBAAuB;;;yCAGlE,uBAAuB;;;;CAI/D;gBAEe,iEAAiE;gBACjE,sFAAsF;gBACtF,+FAA+F;gBAC/F,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.pure\";\r\nimport { type Material } from \"core/Materials/material.pure\";\r\nimport { MaterialDefines } from \"core/Materials/materialDefines\";\r\nimport { MaterialPluginBase } from \"core/Materials/materialPluginBase.pure\";\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_CUSTOM_REFLECTION = false;\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 is computed dynamically in prepareDefines because it\r\n // depends on whether the material's currently-active reflection setup actually\r\n // declares `irradianceSampler` (i.e. has USEIRRADIANCEMAP set). Setting it\r\n // unconditionally here would inject shader code that references an undeclared\r\n // sampler when the material renders against a cube env. See forum 63276.\r\n USE_CUSTOM_REFLECTION: false,\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 const atmosphere = this._atmosphere;\r\n\r\n if (!atmosphere.transmittanceLut?.hasLutData || (atmosphere.diffuseSkyIrradianceLut && !atmosphere.diffuseSkyIrradianceLut.hasLutData)) {\r\n return false;\r\n }\r\n\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n if (!aerialPerspectiveLutRenderTarget?.isReady()) {\r\n return false;\r\n }\r\n }\r\n\r\n return true;\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 lastUseCustomReflection = defines.USE_CUSTOM_REFLECTION;\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 // Only override the PBR reflection block when the surrounding shader actually declares\r\n // `irradianceSampler` (USEIRRADIANCEMAP). When the material renders against a non-irradiance-map\r\n // env (e.g. a cube env using spherical harmonics), the standard PBR reflection block must run\r\n // instead, otherwise the injected `sampleReflection(irradianceSampler, ...)` would reference an\r\n // undeclared identifier. See forum 63276.\r\n defines.USE_CUSTOM_REFLECTION = this._atmosphere.diffuseSkyIrradianceLut !== null && !!defines.USEIRRADIANCEMAP;\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 lastUseCustomReflection !== defines.USE_CUSTOM_REFLECTION ||\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\r\n // Provides the direct light contribution, accounting for transmittance.\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\r\n // Approximates the environment contribution from the atmosphere.\r\n // Note there are some tuned constants used below to modify the environment intensity.\r\n // A more physically accurate approach could be considered, and/or uniforms added to customize.\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 = getLuminanceUnclamped(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\r\n // Provides the direct light contribution, accounting for transmittance.\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\r\n // Approximates the environment contribution from the atmosphere.\r\n // Note there are some tuned constants used below to modify the environment intensity.\r\n // A more physically accurate approach could be considered, and/or uniforms added to customize.\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 = getLuminanceUnclamped(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"]}
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,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,wCAAwC,CAAC;AAE5E,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,OAAO,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAG7F,MAAM,4BAA6B,SAAQ,eAAe;IAQtD;;;OAGG;IACH,YAAY,uBAAgC;QACxC,KAAK,EAAE,CAAC;QAZL,0BAAqB,GAAG,KAAK,CAAC;QAE9B,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,EAA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CAAC,2BAA2B,IAAI,IAAI,CAAC,WAAW,CAAC,6BAA6B;oBAC9E,CAAC,CAAC,6HAA6H,uBAAuB,mCAAmC;oBACzL,CAAC,CAAC,0CAA0C,uBAAuB,mCAAmC;gBAE9G,wEAAwE;gBACxE,mBAAmB,EAAE;;6DAEwB,uBAAuB;;;0CAG1C,uBAAuB;;;;CAIhE;gBAEe,iEAAiE;gBACjE,sFAAsF;gBACtF,+FAA+F;gBAC/F,iBAAiB,EAAE;;8DAE2B,uBAAuB;;;;0CAI3C,uBAAuB;;;;;;;;;;;;;;;;;;;;;;CAsBhE;gBACe,6EAA6E;gBAC7E,0BAA0B,EAAE;;;;;;4CAMA,8BAA8B;;;;;;;;;;;CAWzE;aACY,CAAC;QACN,CAAC;aAAM,CAAC;YACJ,OAAO;YACP,OAAO;gBACH,2BAA2B,EACvB,IAAI,CAAC,2BAA2B,IAAI,IAAI,CAAC,WAAW,CAAC,6BAA6B;oBAC9E,CAAC,CAAC,uLAAuL,uBAAuB,mCAAmC;oBACnP,CAAC,CAAC,sFAAsF,uBAAuB,mCAAmC;gBAE1J,wEAAwE;gBACxE,mBAAmB,EAAE;;oFAE+C,uBAAuB;;;yCAGlE,uBAAuB;;;;CAI/D;gBAEe,iEAAiE;gBACjE,sFAAsF;gBACtF,+FAA+F;gBAC/F,iBAAiB,EAAE;;qFAEkD,uBAAuB;;;;yCAInE,uBAAuB;;;;;;;;;;;;;;;;;;;;;;CAsB/D;gBACe,6EAA6E;gBAC7E,0BAA0B,EAAE;;;;;;gEAMoB,8BAA8B;;;;;;;;;;;CAW7F;aACY,CAAC;QACN,CAAC;IACL,CAAC;;AAjYuB,uDAA2B,GAAG,IAAI,AAAP,CAAQ","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.pure\";\r\nimport { type Material } from \"core/Materials/material.pure\";\r\nimport { MaterialDefines } from \"core/Materials/materialDefines\";\r\nimport { MaterialPluginBase } from \"core/Materials/materialPluginBase.pure\";\r\nimport { type Nullable } from \"core/types\";\r\nimport { Logger } from \"core/Misc/logger\";\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\n\r\nclass AtmospherePBRMaterialDefines extends MaterialDefines {\r\n public USE_CUSTOM_REFLECTION = false;\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 private static readonly _ShaderIncludesRetryDelayMs = 5000;\r\n\r\n private _shaderIncludesLoaded = false;\r\n private _shaderIncludesLoadPromise: Nullable<Promise<void>> = null;\r\n private _shaderIncludesNextRetryTime = 0;\r\n\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 is computed dynamically in prepareDefines because it\r\n // depends on whether the material's currently-active reflection setup actually\r\n // declares `irradianceSampler` (i.e. has USEIRRADIANCEMAP set). Setting it\r\n // unconditionally here would inject shader code that references an undeclared\r\n // sampler when the material renders against a cube env. See forum 63276.\r\n USE_CUSTOM_REFLECTION: false,\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 // The shader includes used by this plugin's custom code (injected via getCustomCode) are\r\n // registered into the ShaderStore lazily so this module stays free of top-level side effects.\r\n // Gate readiness until they are loaded, otherwise the host material's effect would fail to\r\n // resolve `#include<atmosphereFunctions>` and friends at compile time.\r\n if (!this._shaderIncludesLoaded) {\r\n if (!this._shaderIncludesLoadPromise && Date.now() >= this._shaderIncludesNextRetryTime) {\r\n this._shaderIncludesLoadPromise = this._loadShaderIncludesAsync();\r\n }\r\n return false;\r\n }\r\n\r\n const atmosphere = this._atmosphere;\r\n\r\n if (!atmosphere.transmittanceLut?.hasLutData || (atmosphere.diffuseSkyIrradianceLut && !atmosphere.diffuseSkyIrradianceLut.hasLutData)) {\r\n return false;\r\n }\r\n\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n if (!aerialPerspectiveLutRenderTarget?.isReady()) {\r\n return false;\r\n }\r\n }\r\n\r\n return true;\r\n }\r\n\r\n /**\r\n * Lazily loads (and thereby registers into the ShaderStore) the shader includes referenced by\r\n * this plugin's injected custom code. Loading the correct variant for the host material's shader\r\n * language keeps this module free of top-level shader side effects so it can be tree-shaken.\r\n */\r\n private async _loadShaderIncludesAsync(): Promise<void> {\r\n try {\r\n if (this._material.shaderLanguage === ShaderLanguage.WGSL) {\r\n await Promise.all([import(\"./ShadersWGSL/ShadersInclude/atmosphereFunctions\"), import(\"./ShadersWGSL/ShadersInclude/atmosphereUboDeclaration\")]);\r\n } else {\r\n await Promise.all([\r\n import(\"./Shaders/ShadersInclude/atmosphereFunctions\"),\r\n import(\"./Shaders/ShadersInclude/atmosphereUboDeclaration\"),\r\n import(\"./Shaders/ShadersInclude/atmosphereFragmentDeclaration\"),\r\n ]);\r\n }\r\n this._shaderIncludesLoaded = true;\r\n } catch (error) {\r\n // Surface the failure (a missing shader-include registration would otherwise silently stall\r\n // rendering forever) and back off before retrying so we don't hammer the loader every frame.\r\n // A retry still allows recovery from transient failures such as a dropped network request.\r\n Logger.Error(\r\n `AtmospherePBRMaterialPlugin: Failed to load atmosphere shader includes; the atmosphere material will not render until they load. Retrying in ${\r\n AtmospherePBRMaterialPlugin._ShaderIncludesRetryDelayMs / 1000\r\n }s. ${error}`\r\n );\r\n this._shaderIncludesNextRetryTime = Date.now() + AtmospherePBRMaterialPlugin._ShaderIncludesRetryDelayMs;\r\n // Clear the in-flight promise so a later readiness check can retry the load.\r\n this._shaderIncludesLoadPromise = null;\r\n }\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 lastUseCustomReflection = defines.USE_CUSTOM_REFLECTION;\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 // Only override the PBR reflection block when the surrounding shader actually declares\r\n // `irradianceSampler` (USEIRRADIANCEMAP). When the material renders against a non-irradiance-map\r\n // env (e.g. a cube env using spherical harmonics), the standard PBR reflection block must run\r\n // instead, otherwise the injected `sampleReflection(irradianceSampler, ...)` would reference an\r\n // undeclared identifier. See forum 63276.\r\n defines.USE_CUSTOM_REFLECTION = this._atmosphere.diffuseSkyIrradianceLut !== null && !!defines.USEIRRADIANCEMAP;\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 lastUseCustomReflection !== defines.USE_CUSTOM_REFLECTION ||\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\r\n // Provides the direct light contribution, accounting for transmittance.\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\r\n // Approximates the environment contribution from the atmosphere.\r\n // Note there are some tuned constants used below to modify the environment intensity.\r\n // A more physically accurate approach could be considered, and/or uniforms added to customize.\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 = getLuminanceUnclamped(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\r\n // Provides the direct light contribution, accounting for transmittance.\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\r\n // Approximates the environment contribution from the atmosphere.\r\n // Note there are some tuned constants used below to modify the environment intensity.\r\n // A more physically accurate approach could be considered, and/or uniforms added to customize.\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 = getLuminanceUnclamped(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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babylonjs/addons",
3
- "version": "9.14.0",
3
+ "version": "9.15.0",
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": "9.14.0",
22
+ "@babylonjs/core": "9.15.0",
23
23
  "@dev/addons": "^1.0.0",
24
24
  "@dev/build-tools": "^1.0.0"
25
25
  },