@onerjs/addons 8.49.4 → 8.49.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -64,7 +64,7 @@ export class DiffuseSkyIrradianceLut {
64
64
  this._isDirty = true;
65
65
  this._isDisposed = false;
66
66
  this._needsReadPixels = false;
67
- this._lutData = new Uint16Array(0);
67
+ this._lutData = new Float32Array(0);
68
68
  this._atmosphere = atmosphere;
69
69
  const scene = atmosphere.scene;
70
70
  const engine = scene.getEngine();
@@ -150,7 +150,7 @@ export class DiffuseSkyIrradianceLut {
150
150
  }
151
151
  const cosAngleLightToZenith = Vector3Dot(directionToLight, cameraGeocentricNormal);
152
152
  ComputeLutUVToRef(properties, radius, cosAngleLightToZenith, UvTemp);
153
- Sample2DRgbaToRef(UvTemp.x, UvTemp.y, LutWidthPx, LutHeightPx, this._lutData, Color4Temp, FromHalfFloat);
153
+ Sample2DRgbaToRef(UvTemp.x, UvTemp.y, LutWidthPx, LutHeightPx, this._lutData, Color4Temp, null);
154
154
  const intensity = atmosphere.diffuseSkyIrradianceIntensity;
155
155
  result.r = intensity * (lightIrradiance * Color4Temp.r + additionalDiffuseSkyIrradiance.r);
156
156
  result.g = intensity * (lightIrradiance * Color4Temp.g + additionalDiffuseSkyIrradiance.g);
@@ -201,7 +201,18 @@ export class DiffuseSkyIrradianceLut {
201
201
  this._needsReadPixels = false;
202
202
  const value = await this.renderTarget.readPixels(0, 0, undefined, true, true /* noDataConversion */);
203
203
  if (value && !this._isDisposed) {
204
- this._lutData = value;
204
+ const rawLutData = value;
205
+ const lutData = (this._lutData = new Float32Array(rawLutData.length));
206
+ if (rawLutData instanceof Uint16Array) {
207
+ for (let i = 0; i < rawLutData.length; i++) {
208
+ lutData[i] = FromHalfFloat(rawLutData[i]);
209
+ }
210
+ }
211
+ else {
212
+ for (let i = 0; i < rawLutData.length; i++) {
213
+ lutData[i] = rawLutData[i] / 255.0;
214
+ }
215
+ }
205
216
  }
206
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C;QAEH,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,eAAe;QACxB,IAAI,CAAC,IAAI,CAAC,gBAAgB,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAC7C,OAAO;QACX,CAAC;QACD,IAAI,CAAC,gBAAgB,GAAG,KAAK,CAAC;QAE9B,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,sBAAsB,CAAC,CAAC;QACrG,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;YAC7B,IAAI,CAAC,QAAQ,GAAG,KAAiC,CAAC;QACtD,CAAC;IACL,CAAC;IAED;;OAEG;IACI,SAAS;QACZ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACzB,CAAC;IAED;;OAEG;IACI,OAAO;QACV,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACrB,IAAI,CAAC,aAAa,CAAC,iBAAiB,GAAG,IAAI,CAAC;YAC5C,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC;QACjC,CAAC;QACD,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 } from \"core/Maths/math.scalar.functions\";\r\nimport { Constants } from \"core/Engines/constants\";\r\nimport { EffectRenderer, EffectWrapper } from \"core/Materials/effectRenderer\";\r\nimport { FromHalfFloat } from \"core/Misc/textureTools\";\r\nimport { type IColor3Like, type IColor4Like, type IVector2Like, type IVector3Like } from \"core/Maths/math.like\";\r\nimport { type Nullable } from \"core/types\";\r\nimport { RenderTargetTexture } from \"core/Materials/Textures/renderTargetTexture\";\r\nimport { Sample2DRgbaToRef } from \"./sampling\";\r\nimport { ShaderLanguage } from \"core/Materials/shaderLanguage\";\r\nimport { ShaderStore } from \"core/Engines/shaderStore\";\r\nimport { Vector3Dot } from \"core/Maths/math.vector.functions\";\r\n\r\nconst RaySamples = 128;\r\nconst LutWidthPx = 64;\r\nconst LutHeightPx = 16;\r\nconst HalfTexelSize = { x: 0.5 / LutWidthPx, y: 0.5 / LutHeightPx };\r\nconst UnitToUVScale = { x: (LutWidthPx - 1.0) / LutWidthPx, y: (LutHeightPx - 1.0) / LutHeightPx };\r\nconst UvTemp = { x: Number.NaN, y: Number.NaN };\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, result: IVector2Like): void => {\r\n const unitX = Clamp(0.5 + 0.5 * cosAngleLightToZenith);\r\n const unitY = Clamp((radius - properties.planetRadius) / properties.atmosphereThickness);\r\n result.x = unitX * UnitToUVScale.x + HalfTexelSize.x;\r\n result.y = unitY * UnitToUVScale.y + HalfTexelSize.y;\r\n};\r\n\r\n/**\r\n * The diffuse sky irradiance LUT is used to query the diffuse irradiance at a specified position.\r\n */\r\nexport class DiffuseSkyIrradianceLut {\r\n private readonly _atmosphere: Atmosphere;\r\n private _renderTarget: Nullable<RenderTargetTexture> = null;\r\n private _effectWrapper: Nullable<EffectWrapper> = null;\r\n private _effectRenderer: Nullable<EffectRenderer> = null;\r\n private _isDirty = true;\r\n private _isDisposed = false;\r\n private _needsReadPixels = false;\r\n private _lutData: Uint8Array | Uint16Array = new Uint16Array(0);\r\n\r\n /**\r\n * True if the LUT needs to be rendered.\r\n */\r\n public get isDirty() {\r\n return this._isDirty;\r\n }\r\n\r\n /**\r\n * True if the LUT has been disposed.\r\n */\r\n public get isDisposed(): boolean {\r\n return this._isDisposed;\r\n }\r\n\r\n /**\r\n * The render target used for this 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 DiffuseSkyIrradianceLut}.\r\n * @param atmosphere - The atmosphere to use.\r\n */\r\n constructor(atmosphere: Atmosphere) {\r\n this._atmosphere = atmosphere;\r\n const scene = atmosphere.scene;\r\n const engine = scene.getEngine();\r\n\r\n const name = \"atmo-diffuseSkyIrradiance\";\r\n const caps = engine.getCaps();\r\n const textureType = caps.textureHalfFloatRender ? Constants.TEXTURETYPE_HALF_FLOAT : Constants.TEXTURETYPE_UNSIGNED_BYTE;\r\n const renderTarget = (this._renderTarget = new RenderTargetTexture(name, { width: LutWidthPx, height: LutHeightPx }, scene, {\r\n generateMipMaps: false,\r\n type: textureType,\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\r\n this._effectWrapper = new EffectWrapper({\r\n engine,\r\n name,\r\n vertexShader: \"fullscreenTriangle\",\r\n fragmentShader: \"diffuseSkyIrradiance\",\r\n attributeNames: [\"position\"],\r\n uniformNames: [\"depth\", ...(useUbo ? [] : atmosphereUbo.getUniformNames())],\r\n uniformBuffers: useUbo ? [uboName] : [],\r\n defines: [\"#define POSITION_VEC2\", `#define NUM_SAMPLES ${RaySamples}u`, \"#define CUSTOM_IRRADIANCE_FILTERING_INPUT\", \"#define CUSTOM_IRRADIANCE_FILTERING_FUNCTION\"],\r\n samplers: [\"transmittanceLut\", \"multiScatteringLut\"],\r\n useShaderStore: true,\r\n shaderLanguage: useWebGPU ? ShaderLanguage.WGSL : ShaderLanguage.GLSL,\r\n extraInitializationsAsync: async () => {\r\n await Promise.all(\r\n useWebGPU\r\n ? [import(\"./ShadersWGSL/fullscreenTriangle.vertex\"), import(\"./ShadersWGSL/diffuseSkyIrradiance.fragment\")]\r\n : [import(\"./Shaders/fullscreenTriangle.vertex\"), import(\"./Shaders/diffuseSkyIrradiance.fragment\")]\r\n );\r\n\r\n // Replace the CUSTOM_IRRADIANCE_FILTERING_INPUT and CUSTOM_IRRADIANCE_FILTERING_FUNCTION placeholders.\r\n // Note, the regex replacements look for lines that *only* contain these placeholder strings.\r\n // Since buildShaders removes leading whitespace, the placeholders are expected to start at the beginning of the line.\r\n const includeStore = useWebGPU ? ShaderStore.IncludesShadersStoreWGSL : ShaderStore.IncludesShadersStore;\r\n let patchedInclude = includeStore[\"hdrFilteringFunctions\"];\r\n patchedInclude = patchedInclude.replace(/^CUSTOM_IRRADIANCE_FILTERING_INPUT\\s*$/gm, \"\");\r\n patchedInclude = patchedInclude.replace(\r\n /^CUSTOM_IRRADIANCE_FILTERING_FUNCTION\\s*$/gm,\r\n useWebGPU ? \"var c = integrateForIrradiance(n, Ls, vec3f(0., filteringInfo.x, 0.));\" : \"vec3 c = integrateForIrradiance(n, Ls, vec3(0., filteringInfo.x, 0.));\"\r\n );\r\n\r\n // Replace the existing #include<hdrFilteringFunctions> with the patched include.\r\n const shaderStore = useWebGPU ? ShaderStore.ShadersStoreWGSL : ShaderStore.ShadersStore;\r\n let shader = shaderStore[\"diffuseSkyIrradiancePixelShader\"];\r\n shader = shader.replace(\"#include<hdrFilteringFunctions>\", patchedInclude);\r\n shaderStore[\"diffuseSkyIrradiancePixelShader\"] = shader;\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 // The sky irradiance will also be used for the environment texture.\r\n scene.environmentTexture = renderTarget;\r\n scene.environmentTexture.irradianceTexture = renderTarget;\r\n scene.environmentIntensity = 1.0;\r\n\r\n // Prevent the irradiance LUT from being rendered redundantly at the beginning of the frame.\r\n scene.environmentTexture.isRenderTarget = false;\r\n }\r\n\r\n /**\r\n * Gets the diffuse sky irradiance for a surface oriented along the geocentric normal.\r\n * Resulting color is always in linear space.\r\n * @param directionToLight - The direction to the light in world space.\r\n * @param radius - The position's distance to the planet origin.\r\n * @param cameraGeocentricNormal - The geocentric normal of the camera.\r\n * @param lightIrradiance - The irradiance of the light.\r\n * @param result - The color to store the result in.\r\n * @returns The result color.\r\n */\r\n public getDiffuseSkyIrradianceToRef<T extends IColor3Like>(\r\n directionToLight: IVector3Like,\r\n radius: number,\r\n cameraGeocentricNormal: IVector3Like,\r\n lightIrradiance: number,\r\n result: T\r\n ): T {\r\n const atmosphere = this._atmosphere;\r\n const additionalDiffuseSkyIrradiance = atmosphere.additionalDiffuseSkyIrradiance;\r\n\r\n const properties = atmosphere.physicalProperties;\r\n if (this._lutData[0] === undefined || radius > properties.atmosphereRadius) {\r\n result.r = additionalDiffuseSkyIrradiance.r;\r\n result.g = additionalDiffuseSkyIrradiance.g;\r\n result.b = additionalDiffuseSkyIrradiance.b;\r\n return result;\r\n }\r\n\r\n const cosAngleLightToZenith = Vector3Dot(directionToLight, cameraGeocentricNormal);\r\n ComputeLutUVToRef(properties, radius, cosAngleLightToZenith, UvTemp);\r\n Sample2DRgbaToRef(UvTemp.x, UvTemp.y, LutWidthPx, LutHeightPx, this._lutData, Color4Temp, FromHalfFloat);\r\n\r\n const intensity = atmosphere.diffuseSkyIrradianceIntensity;\r\n result.r = intensity * (lightIrradiance * Color4Temp.r + additionalDiffuseSkyIrradiance.r);\r\n result.g = intensity * (lightIrradiance * Color4Temp.g + additionalDiffuseSkyIrradiance.g);\r\n result.b = intensity * (lightIrradiance * Color4Temp.b + additionalDiffuseSkyIrradiance.b);\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Renders the LUT.\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 effectRenderer = this._effectRenderer!;\r\n effectRenderer.saveStates();\r\n\r\n const engine = this._atmosphere.scene.getEngine();\r\n engine.bindFramebuffer(this.renderTarget.renderTarget!, undefined, undefined, undefined, true);\r\n\r\n effectRenderer.setViewport();\r\n effectRenderer.applyEffectWrapper(effectWrapper);\r\n\r\n const effect = effectWrapper.effect;\r\n effectRenderer.bindBuffers(effect);\r\n\r\n effect.setTexture(\"transmittanceLut\", this._atmosphere.transmittanceLut!.renderTarget);\r\n effect.setTexture(\"multiScatteringLut\", this._atmosphere.multiScatteringLutRenderTarget);\r\n\r\n this._atmosphere.bindUniformBufferToEffect(effect);\r\n\r\n effect.setFloat(\"depth\", 0.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 this._needsReadPixels = true;\r\n\r\n // Defer readPixels to the end of the frame.\r\n this._atmosphere.scene.onAfterRenderObservable.addOnce(async () => {\r\n await this.readPixelsAsync();\r\n });\r\n\r\n return true;\r\n }\r\n\r\n /**\r\n * Reads back the LUT data from the GPU if a readback is pending.\r\n * @internal\r\n */\r\n public async readPixelsAsync(): Promise<void> {\r\n if (!this._needsReadPixels || this._isDisposed) {\r\n return;\r\n }\r\n this._needsReadPixels = false;\r\n\r\n const value = await this.renderTarget.readPixels(0, 0, undefined, true, true /* noDataConversion */);\r\n if (value && !this._isDisposed) {\r\n this._lutData = value as Uint8Array | Uint16Array;\r\n }\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.\r\n */\r\n public dispose() {\r\n if (this._renderTarget) {\r\n this._renderTarget.irradianceTexture = null;\r\n this._renderTarget.dispose();\r\n }\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":"diffuseSkyIrradianceLut.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/diffuseSkyIrradianceLut.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,OAAO,EAAE,KAAK,EAAE,MAAM,kCAAkC,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAGvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,6CAA6C,CAAC;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAE9D,MAAM,UAAU,GA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;gBACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;oBACzC,OAAO,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;gBAC9C,CAAC;YACL,CAAC;iBAAM,CAAC;gBACJ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;oBACzC,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;gBACvC,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC;IAED;;OAEG;IACI,SAAS;QACZ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACzB,CAAC;IAED;;OAEG;IACI,OAAO;QACV,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACrB,IAAI,CAAC,aAAa,CAAC,iBAAiB,GAAG,IAAI,CAAC;YAC5C,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC;QACjC,CAAC;QACD,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 } from \"core/Maths/math.scalar.functions\";\r\nimport { Constants } from \"core/Engines/constants\";\r\nimport { EffectRenderer, EffectWrapper } from \"core/Materials/effectRenderer\";\r\nimport { FromHalfFloat } from \"core/Misc/textureTools\";\r\nimport { type IColor3Like, type IColor4Like, type IVector2Like, type IVector3Like } from \"core/Maths/math.like\";\r\nimport { type Nullable } from \"core/types\";\r\nimport { RenderTargetTexture } from \"core/Materials/Textures/renderTargetTexture\";\r\nimport { Sample2DRgbaToRef } from \"./sampling\";\r\nimport { ShaderLanguage } from \"core/Materials/shaderLanguage\";\r\nimport { ShaderStore } from \"core/Engines/shaderStore\";\r\nimport { Vector3Dot } from \"core/Maths/math.vector.functions\";\r\n\r\nconst RaySamples = 128;\r\nconst LutWidthPx = 64;\r\nconst LutHeightPx = 16;\r\nconst HalfTexelSize = { x: 0.5 / LutWidthPx, y: 0.5 / LutHeightPx };\r\nconst UnitToUVScale = { x: (LutWidthPx - 1.0) / LutWidthPx, y: (LutHeightPx - 1.0) / LutHeightPx };\r\nconst UvTemp = { x: Number.NaN, y: Number.NaN };\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, result: IVector2Like): void => {\r\n const unitX = Clamp(0.5 + 0.5 * cosAngleLightToZenith);\r\n const unitY = Clamp((radius - properties.planetRadius) / properties.atmosphereThickness);\r\n result.x = unitX * UnitToUVScale.x + HalfTexelSize.x;\r\n result.y = unitY * UnitToUVScale.y + HalfTexelSize.y;\r\n};\r\n\r\n/**\r\n * The diffuse sky irradiance LUT is used to query the diffuse irradiance at a specified position.\r\n */\r\nexport class DiffuseSkyIrradianceLut {\r\n private readonly _atmosphere: Atmosphere;\r\n private _renderTarget: Nullable<RenderTargetTexture> = null;\r\n private _effectWrapper: Nullable<EffectWrapper> = null;\r\n private _effectRenderer: Nullable<EffectRenderer> = null;\r\n private _isDirty = true;\r\n private _isDisposed = false;\r\n private _needsReadPixels = false;\r\n private _lutData: Float32Array = new Float32Array(0);\r\n\r\n /**\r\n * True if the LUT needs to be rendered.\r\n */\r\n public get isDirty() {\r\n return this._isDirty;\r\n }\r\n\r\n /**\r\n * True if the LUT has been disposed.\r\n */\r\n public get isDisposed(): boolean {\r\n return this._isDisposed;\r\n }\r\n\r\n /**\r\n * The render target used for this 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 DiffuseSkyIrradianceLut}.\r\n * @param atmosphere - The atmosphere to use.\r\n */\r\n constructor(atmosphere: Atmosphere) {\r\n this._atmosphere = atmosphere;\r\n const scene = atmosphere.scene;\r\n const engine = scene.getEngine();\r\n\r\n const name = \"atmo-diffuseSkyIrradiance\";\r\n const caps = engine.getCaps();\r\n const textureType = caps.textureHalfFloatRender ? Constants.TEXTURETYPE_HALF_FLOAT : Constants.TEXTURETYPE_UNSIGNED_BYTE;\r\n const renderTarget = (this._renderTarget = new RenderTargetTexture(name, { width: LutWidthPx, height: LutHeightPx }, scene, {\r\n generateMipMaps: false,\r\n type: textureType,\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\r\n this._effectWrapper = new EffectWrapper({\r\n engine,\r\n name,\r\n vertexShader: \"fullscreenTriangle\",\r\n fragmentShader: \"diffuseSkyIrradiance\",\r\n attributeNames: [\"position\"],\r\n uniformNames: [\"depth\", ...(useUbo ? [] : atmosphereUbo.getUniformNames())],\r\n uniformBuffers: useUbo ? [uboName] : [],\r\n defines: [\"#define POSITION_VEC2\", `#define NUM_SAMPLES ${RaySamples}u`, \"#define CUSTOM_IRRADIANCE_FILTERING_INPUT\", \"#define CUSTOM_IRRADIANCE_FILTERING_FUNCTION\"],\r\n samplers: [\"transmittanceLut\", \"multiScatteringLut\"],\r\n useShaderStore: true,\r\n shaderLanguage: useWebGPU ? ShaderLanguage.WGSL : ShaderLanguage.GLSL,\r\n extraInitializationsAsync: async () => {\r\n await Promise.all(\r\n useWebGPU\r\n ? [import(\"./ShadersWGSL/fullscreenTriangle.vertex\"), import(\"./ShadersWGSL/diffuseSkyIrradiance.fragment\")]\r\n : [import(\"./Shaders/fullscreenTriangle.vertex\"), import(\"./Shaders/diffuseSkyIrradiance.fragment\")]\r\n );\r\n\r\n // Replace the CUSTOM_IRRADIANCE_FILTERING_INPUT and CUSTOM_IRRADIANCE_FILTERING_FUNCTION placeholders.\r\n // Note, the regex replacements look for lines that *only* contain these placeholder strings.\r\n // Since buildShaders removes leading whitespace, the placeholders are expected to start at the beginning of the line.\r\n const includeStore = useWebGPU ? ShaderStore.IncludesShadersStoreWGSL : ShaderStore.IncludesShadersStore;\r\n let patchedInclude = includeStore[\"hdrFilteringFunctions\"];\r\n patchedInclude = patchedInclude.replace(/^CUSTOM_IRRADIANCE_FILTERING_INPUT\\s*$/gm, \"\");\r\n patchedInclude = patchedInclude.replace(\r\n /^CUSTOM_IRRADIANCE_FILTERING_FUNCTION\\s*$/gm,\r\n useWebGPU ? \"var c = integrateForIrradiance(n, Ls, vec3f(0., filteringInfo.x, 0.));\" : \"vec3 c = integrateForIrradiance(n, Ls, vec3(0., filteringInfo.x, 0.));\"\r\n );\r\n\r\n // Replace the existing #include<hdrFilteringFunctions> with the patched include.\r\n const shaderStore = useWebGPU ? ShaderStore.ShadersStoreWGSL : ShaderStore.ShadersStore;\r\n let shader = shaderStore[\"diffuseSkyIrradiancePixelShader\"];\r\n shader = shader.replace(\"#include<hdrFilteringFunctions>\", patchedInclude);\r\n shaderStore[\"diffuseSkyIrradiancePixelShader\"] = shader;\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 // The sky irradiance will also be used for the environment texture.\r\n scene.environmentTexture = renderTarget;\r\n scene.environmentTexture.irradianceTexture = renderTarget;\r\n scene.environmentIntensity = 1.0;\r\n\r\n // Prevent the irradiance LUT from being rendered redundantly at the beginning of the frame.\r\n scene.environmentTexture.isRenderTarget = false;\r\n }\r\n\r\n /**\r\n * Gets the diffuse sky irradiance for a surface oriented along the geocentric normal.\r\n * Resulting color is always in linear space.\r\n * @param directionToLight - The direction to the light in world space.\r\n * @param radius - The position's distance to the planet origin.\r\n * @param cameraGeocentricNormal - The geocentric normal of the camera.\r\n * @param lightIrradiance - The irradiance of the light.\r\n * @param result - The color to store the result in.\r\n * @returns The result color.\r\n */\r\n public getDiffuseSkyIrradianceToRef<T extends IColor3Like>(\r\n directionToLight: IVector3Like,\r\n radius: number,\r\n cameraGeocentricNormal: IVector3Like,\r\n lightIrradiance: number,\r\n result: T\r\n ): T {\r\n const atmosphere = this._atmosphere;\r\n const additionalDiffuseSkyIrradiance = atmosphere.additionalDiffuseSkyIrradiance;\r\n\r\n const properties = atmosphere.physicalProperties;\r\n if (this._lutData[0] === undefined || radius > properties.atmosphereRadius) {\r\n result.r = additionalDiffuseSkyIrradiance.r;\r\n result.g = additionalDiffuseSkyIrradiance.g;\r\n result.b = additionalDiffuseSkyIrradiance.b;\r\n return result;\r\n }\r\n\r\n const cosAngleLightToZenith = Vector3Dot(directionToLight, cameraGeocentricNormal);\r\n ComputeLutUVToRef(properties, radius, cosAngleLightToZenith, UvTemp);\r\n Sample2DRgbaToRef(UvTemp.x, UvTemp.y, LutWidthPx, LutHeightPx, this._lutData, Color4Temp, null);\r\n\r\n const intensity = atmosphere.diffuseSkyIrradianceIntensity;\r\n result.r = intensity * (lightIrradiance * Color4Temp.r + additionalDiffuseSkyIrradiance.r);\r\n result.g = intensity * (lightIrradiance * Color4Temp.g + additionalDiffuseSkyIrradiance.g);\r\n result.b = intensity * (lightIrradiance * Color4Temp.b + additionalDiffuseSkyIrradiance.b);\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Renders the LUT.\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 effectRenderer = this._effectRenderer!;\r\n effectRenderer.saveStates();\r\n\r\n const engine = this._atmosphere.scene.getEngine();\r\n engine.bindFramebuffer(this.renderTarget.renderTarget!, undefined, undefined, undefined, true);\r\n\r\n effectRenderer.setViewport();\r\n effectRenderer.applyEffectWrapper(effectWrapper);\r\n\r\n const effect = effectWrapper.effect;\r\n effectRenderer.bindBuffers(effect);\r\n\r\n effect.setTexture(\"transmittanceLut\", this._atmosphere.transmittanceLut!.renderTarget);\r\n effect.setTexture(\"multiScatteringLut\", this._atmosphere.multiScatteringLutRenderTarget);\r\n\r\n this._atmosphere.bindUniformBufferToEffect(effect);\r\n\r\n effect.setFloat(\"depth\", 0.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 this._needsReadPixels = true;\r\n\r\n // Defer readPixels to the end of the frame.\r\n this._atmosphere.scene.onAfterRenderObservable.addOnce(async () => {\r\n await this.readPixelsAsync();\r\n });\r\n\r\n return true;\r\n }\r\n\r\n /**\r\n * Reads back the LUT data from the GPU if a readback is pending.\r\n * @internal\r\n */\r\n public async readPixelsAsync(): Promise<void> {\r\n if (!this._needsReadPixels || this._isDisposed) {\r\n return;\r\n }\r\n this._needsReadPixels = false;\r\n\r\n const value = await this.renderTarget.readPixels(0, 0, undefined, true, true /* noDataConversion */);\r\n if (value && !this._isDisposed) {\r\n const rawLutData = value as Uint8Array | Uint16Array;\r\n const lutData = (this._lutData = new Float32Array(rawLutData.length));\r\n if (rawLutData instanceof Uint16Array) {\r\n for (let i = 0; i < rawLutData.length; i++) {\r\n lutData[i] = FromHalfFloat(rawLutData[i]);\r\n }\r\n } else {\r\n for (let i = 0; i < rawLutData.length; i++) {\r\n lutData[i] = rawLutData[i] / 255.0;\r\n }\r\n }\r\n }\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.\r\n */\r\n public dispose() {\r\n if (this._renderTarget) {\r\n this._renderTarget.irradianceTexture = null;\r\n this._renderTarget.dispose();\r\n }\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"]}
@@ -9,7 +9,7 @@ import { type IColor4Like } from "@onerjs/core/Maths/math.like.js";
9
9
  * @param heightPx - The height of the texture in texels.
10
10
  * @param data - The texture data to sample.
11
11
  * @param result - The color to store the sample.
12
- * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255.
12
+ * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255. Pass null for no normalization.
13
13
  * @returns The result color.
14
14
  */
15
- export declare function Sample2DRgbaToRef<T extends IColor4Like>(u: number, v: number, widthPx: number, heightPx: number, data: Uint8Array | Uint16Array | Float32Array, result: T, normalizeFunc?: (value: number) => number): T;
15
+ export declare function Sample2DRgbaToRef<T extends IColor4Like>(u: number, v: number, widthPx: number, heightPx: number, data: Uint8Array | Uint16Array | Float32Array, result: T, normalizeFunc?: ((value: number) => number) | null): T;
@@ -13,6 +13,7 @@ const TmpColor1 = MakeTempColor4Like();
13
13
  const TmpColor2 = MakeTempColor4Like();
14
14
  const TmpColor3 = MakeTempColor4Like();
15
15
  const TmpColor4 = MakeTempColor4Like();
16
+ const DefaultNormalize = (value) => value / 255.0;
16
17
  /**
17
18
  * Samples the texture data at the given uv coordinate using bilinear interpolation.
18
19
  * Note this will not match GPU sampling behavior exactly.
@@ -23,10 +24,10 @@ const TmpColor4 = MakeTempColor4Like();
23
24
  * @param heightPx - The height of the texture in texels.
24
25
  * @param data - The texture data to sample.
25
26
  * @param result - The color to store the sample.
26
- * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255.
27
+ * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255. Pass null for no normalization.
27
28
  * @returns The result color.
28
29
  */
29
- export function Sample2DRgbaToRef(u, v, widthPx, heightPx, data, result, normalizeFunc = (value) => value / 255.0) {
30
+ export function Sample2DRgbaToRef(u, v, widthPx, heightPx, data, result, normalizeFunc = DefaultNormalize) {
30
31
  if (widthPx <= 0 || heightPx <= 0) {
31
32
  throw new Error("Sample2DRgbaToRef: widthPx and heightPx must be positive.");
32
33
  }
@@ -73,17 +74,25 @@ export function Sample2DRgbaToRef(u, v, widthPx, heightPx, data, result, normali
73
74
  * @param height - The height of the texture in texels.
74
75
  * @param data - The texture data to sample from.
75
76
  * @param result - The color to store the sampled color in.
76
- * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255.
77
+ * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255. Pass null for no normalization.
77
78
  * @returns The result color.
78
79
  */
79
- const TexelFetch2DRgbaToRef = (x, y, width, height, data, result, normalizeFunc = (value) => value / 255.0) => {
80
+ const TexelFetch2DRgbaToRef = (x, y, width, height, data, result, normalizeFunc = DefaultNormalize) => {
80
81
  const clampedTexelX = Clamp(x, 0, width - 1);
81
82
  const clampedTexelY = Clamp(y, 0, height - 1);
82
83
  const index = 4 * (clampedTexelY * width + clampedTexelX);
83
- result.r = normalizeFunc(data[index]);
84
- result.g = normalizeFunc(data[index + 1]);
85
- result.b = normalizeFunc(data[index + 2]);
86
- result.a = normalizeFunc(data[index + 3]);
84
+ if (normalizeFunc) {
85
+ result.r = normalizeFunc(data[index]);
86
+ result.g = normalizeFunc(data[index + 1]);
87
+ result.b = normalizeFunc(data[index + 2]);
88
+ result.a = normalizeFunc(data[index + 3]);
89
+ }
90
+ else {
91
+ result.r = data[index];
92
+ result.g = data[index + 1];
93
+ result.b = data[index + 2];
94
+ result.a = data[index + 3];
95
+ }
87
96
  return result;
88
97
  };
89
98
  //# sourceMappingURL=sampling.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sampling.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/sampling.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,KAAK,EAAE,MAAM,kCAAkC,CAAC;AAGzD,MAAM,kBAAkB,GAAG,GAAgB,EAAE;IACzC,OAAO;QACH,CAAC,EAAE,MAAM,CAAC,GAAG;QACb,CAAC,EAAE,MAAM,CAAC,GAAG;QACb,CAAC,EAAE,MAAM,CAAC,GAAG;QACb,CAAC,EAAE,MAAM,CAAC,GAAG;KAChB,CAAC;AACN,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AACvC,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AACvC,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AACvC,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AAEvC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,iBAAiB,CAC7B,CAAS,EACT,CAAS,EACT,OAAe,EACf,QAAgB,EAChB,IAA6C,EAC7C,MAAS,EACT,gBAAgB,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK;IA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G,EAAE,GAAG,eAAe,CAAC,CAAC,GAAG,EAAE,CAAC;IAC3G,MAAM,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,GAAG,EAAE,GAAG,eAAe,CAAC,CAAC,GAAG,EAAE,GAAG,cAAc,CAAC,CAAC,GAAG,EAAE,GAAG,eAAe,CAAC,CAAC,GAAG,EAAE,CAAC;IAC3G,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,qBAAqB,GAAG,CAC1B,CAAS,EACT,CAAS,EACT,KAAa,EACb,MAAc,EACd,IAA6C,EAC7C,MAAS,EACT,gBAAgB,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,EAC/C,EAAE;IACH,MAAM,aAAa,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;IAC7C,MAAM,aAAa,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC;IAC9C,MAAM,KAAK,GAAG,CAAC,GAAG,CAAC,aAAa,GAAG,KAAK,GAAG,aAAa,CAAC,CAAC;IAC1D,MAAM,CAAC,CAAC,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IACtC,MAAM,CAAC,CAAC,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IAC1C,MAAM,CAAC,CAAC,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IAC1C,MAAM,CAAC,CAAC,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IAC1C,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT License.\r\n\r\nimport { Clamp } from \"core/Maths/math.scalar.functions\";\r\nimport { type IColor4Like } from \"core/Maths/math.like\";\r\n\r\nconst MakeTempColor4Like = (): IColor4Like => {\r\n return {\r\n r: Number.NaN,\r\n g: Number.NaN,\r\n b: Number.NaN,\r\n a: Number.NaN,\r\n };\r\n};\r\n\r\nconst TmpColor1 = MakeTempColor4Like();\r\nconst TmpColor2 = MakeTempColor4Like();\r\nconst TmpColor3 = MakeTempColor4Like();\r\nconst TmpColor4 = MakeTempColor4Like();\r\n\r\n/**\r\n * Samples the texture data at the given uv coordinate using bilinear interpolation.\r\n * Note this will not match GPU sampling behavior exactly.\r\n * Currently assumes clamping behavior.\r\n * @param u - The u coordinate to sample.\r\n * @param v - The v coordinate to sample.\r\n * @param widthPx - The width of the texture in texels.\r\n * @param heightPx - The height of the texture in texels.\r\n * @param data - The texture data to sample.\r\n * @param result - The color to store the sample.\r\n * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255.\r\n * @returns The result color.\r\n */\r\nexport function Sample2DRgbaToRef<T extends IColor4Like>(\r\n u: number,\r\n v: number,\r\n widthPx: number,\r\n heightPx: number,\r\n data: Uint8Array | Uint16Array | Float32Array,\r\n result: T,\r\n normalizeFunc = (value: number) => value / 255.0\r\n): T {\r\n if (widthPx <= 0 || heightPx <= 0) {\r\n throw new Error(\"Sample2DRgbaToRef: widthPx and heightPx must be positive.\");\r\n }\r\n\r\n const expectedLength = widthPx * heightPx * 4;\r\n if (data.length < expectedLength) {\r\n throw new Error(`Sample2DRgbaToRef: data length (${data.length}) is less than required (${expectedLength}).`);\r\n }\r\n\r\n // Default to clamping behavior, but could support others.\r\n u = Clamp(u);\r\n v = Clamp(v);\r\n\r\n // Compute 4 nearest neighbor texels.\r\n const fractionalTexelX = Math.max(u * widthPx - 0.5, 0);\r\n const fractionalTexelY = Math.max(v * heightPx - 0.5, 0);\r\n const xLeft = Math.floor(fractionalTexelX);\r\n const xRight = Math.min(xLeft + 1, widthPx - 1);\r\n const yBottom = Math.floor(fractionalTexelY);\r\n const yTop = Math.min(yBottom + 1, heightPx - 1);\r\n\r\n // Sample nearest neighbor texels.\r\n const lowerLeftColor = TexelFetch2DRgbaToRef(xLeft, yBottom, widthPx, heightPx, data, TmpColor1, normalizeFunc);\r\n const upperLeftColor = TexelFetch2DRgbaToRef(xLeft, yTop, widthPx, heightPx, data, TmpColor2, normalizeFunc);\r\n const lowerRightColor = TexelFetch2DRgbaToRef(xRight, yBottom, widthPx, heightPx, data, TmpColor3, normalizeFunc);\r\n const upperRightColor = TexelFetch2DRgbaToRef(xRight, yTop, widthPx, heightPx, data, TmpColor4, normalizeFunc);\r\n\r\n // Compute weights.\r\n const tX = fractionalTexelX - xLeft;\r\n const tY = fractionalTexelY - yBottom;\r\n const oneMinusTX = 1.0 - tX;\r\n const oneMinusTY = 1.0 - tY;\r\n const w0 = oneMinusTX * oneMinusTY;\r\n const w1 = tX * oneMinusTY;\r\n const w2 = oneMinusTX * tY;\r\n const w3 = tX * tY;\r\n\r\n // Compute the result.\r\n result.r = lowerLeftColor.r * w0 + lowerRightColor.r * w1 + upperLeftColor.r * w2 + upperRightColor.r * w3;\r\n result.g = lowerLeftColor.g * w0 + lowerRightColor.g * w1 + upperLeftColor.g * w2 + upperRightColor.g * w3;\r\n result.b = lowerLeftColor.b * w0 + lowerRightColor.b * w1 + upperLeftColor.b * w2 + upperRightColor.b * w3;\r\n result.a = lowerLeftColor.a * w0 + lowerRightColor.a * w1 + upperLeftColor.a * w2 + upperRightColor.a * w3;\r\n return result;\r\n}\r\n\r\n/**\r\n * Fetches a texel from a 2D texture and stores the result in the given color.\r\n * @param x - The x coordinate in texels.\r\n * @param y - The y coordinate in texels.\r\n * @param width - The width of the texture in texels.\r\n * @param height - The height of the texture in texels.\r\n * @param data - The texture data to sample from.\r\n * @param result - The color to store the sampled color in.\r\n * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255.\r\n * @returns The result color.\r\n */\r\nconst TexelFetch2DRgbaToRef = <T extends IColor4Like>(\r\n x: number,\r\n y: number,\r\n width: number,\r\n height: number,\r\n data: Uint8Array | Uint16Array | Float32Array,\r\n result: T,\r\n normalizeFunc = (value: number) => value / 255.0\r\n): T => {\r\n const clampedTexelX = Clamp(x, 0, width - 1);\r\n const clampedTexelY = Clamp(y, 0, height - 1);\r\n const index = 4 * (clampedTexelY * width + clampedTexelX);\r\n result.r = normalizeFunc(data[index]);\r\n result.g = normalizeFunc(data[index + 1]);\r\n result.b = normalizeFunc(data[index + 2]);\r\n result.a = normalizeFunc(data[index + 3]);\r\n return result;\r\n};\r\n"]}
1
+ {"version":3,"file":"sampling.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/sampling.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,KAAK,EAAE,MAAM,kCAAkC,CAAC;AAGzD,MAAM,kBAAkB,GAAG,GAAgB,EAAE;IACzC,OAAO;QACH,CAAC,EAAE,MAAM,CAAC,GAAG;QACb,CAAC,EAAE,MAAM,CAAC,GAAG;QACb,CAAC,EAAE,MAAM,CAAC,GAAG;QACb,CAAC,EAAE,MAAM,CAAC,GAAG;KAChB,CAAC;AACN,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AACvC,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AACvC,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AACvC,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;AAEvC,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,CAAC;AAE1D;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,iBAAiB,CAC7B,CAAS,EACT,CAAS,EACT,OAAe,EACf,QAAgB,EAChB,IAA6C,EAC7C,MAAS,EACT,gBAAoD,gBAAgB;IAEpE,IAAI,OAAO,IAAI,CAAC,IAAI,QAAQ,IAAI,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;IACjF,CAAC;I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CAAC;IAC/B,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT License.\r\n\r\nimport { Clamp } from \"core/Maths/math.scalar.functions\";\r\nimport { type IColor4Like } from \"core/Maths/math.like\";\r\n\r\nconst MakeTempColor4Like = (): IColor4Like => {\r\n return {\r\n r: Number.NaN,\r\n g: Number.NaN,\r\n b: Number.NaN,\r\n a: Number.NaN,\r\n };\r\n};\r\n\r\nconst TmpColor1 = MakeTempColor4Like();\r\nconst TmpColor2 = MakeTempColor4Like();\r\nconst TmpColor3 = MakeTempColor4Like();\r\nconst TmpColor4 = MakeTempColor4Like();\r\n\r\nconst DefaultNormalize = (value: number) => value / 255.0;\r\n\r\n/**\r\n * Samples the texture data at the given uv coordinate using bilinear interpolation.\r\n * Note this will not match GPU sampling behavior exactly.\r\n * Currently assumes clamping behavior.\r\n * @param u - The u coordinate to sample.\r\n * @param v - The v coordinate to sample.\r\n * @param widthPx - The width of the texture in texels.\r\n * @param heightPx - The height of the texture in texels.\r\n * @param data - The texture data to sample.\r\n * @param result - The color to store the sample.\r\n * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255. Pass null for no normalization.\r\n * @returns The result color.\r\n */\r\nexport function Sample2DRgbaToRef<T extends IColor4Like>(\r\n u: number,\r\n v: number,\r\n widthPx: number,\r\n heightPx: number,\r\n data: Uint8Array | Uint16Array | Float32Array,\r\n result: T,\r\n normalizeFunc: ((value: number) => number) | null = DefaultNormalize\r\n): T {\r\n if (widthPx <= 0 || heightPx <= 0) {\r\n throw new Error(\"Sample2DRgbaToRef: widthPx and heightPx must be positive.\");\r\n }\r\n\r\n const expectedLength = widthPx * heightPx * 4;\r\n if (data.length < expectedLength) {\r\n throw new Error(`Sample2DRgbaToRef: data length (${data.length}) is less than required (${expectedLength}).`);\r\n }\r\n\r\n // Default to clamping behavior, but could support others.\r\n u = Clamp(u);\r\n v = Clamp(v);\r\n\r\n // Compute 4 nearest neighbor texels.\r\n const fractionalTexelX = Math.max(u * widthPx - 0.5, 0);\r\n const fractionalTexelY = Math.max(v * heightPx - 0.5, 0);\r\n const xLeft = Math.floor(fractionalTexelX);\r\n const xRight = Math.min(xLeft + 1, widthPx - 1);\r\n const yBottom = Math.floor(fractionalTexelY);\r\n const yTop = Math.min(yBottom + 1, heightPx - 1);\r\n\r\n // Sample nearest neighbor texels.\r\n const lowerLeftColor = TexelFetch2DRgbaToRef(xLeft, yBottom, widthPx, heightPx, data, TmpColor1, normalizeFunc);\r\n const upperLeftColor = TexelFetch2DRgbaToRef(xLeft, yTop, widthPx, heightPx, data, TmpColor2, normalizeFunc);\r\n const lowerRightColor = TexelFetch2DRgbaToRef(xRight, yBottom, widthPx, heightPx, data, TmpColor3, normalizeFunc);\r\n const upperRightColor = TexelFetch2DRgbaToRef(xRight, yTop, widthPx, heightPx, data, TmpColor4, normalizeFunc);\r\n\r\n // Compute weights.\r\n const tX = fractionalTexelX - xLeft;\r\n const tY = fractionalTexelY - yBottom;\r\n const oneMinusTX = 1.0 - tX;\r\n const oneMinusTY = 1.0 - tY;\r\n const w0 = oneMinusTX * oneMinusTY;\r\n const w1 = tX * oneMinusTY;\r\n const w2 = oneMinusTX * tY;\r\n const w3 = tX * tY;\r\n\r\n // Compute the result.\r\n result.r = lowerLeftColor.r * w0 + lowerRightColor.r * w1 + upperLeftColor.r * w2 + upperRightColor.r * w3;\r\n result.g = lowerLeftColor.g * w0 + lowerRightColor.g * w1 + upperLeftColor.g * w2 + upperRightColor.g * w3;\r\n result.b = lowerLeftColor.b * w0 + lowerRightColor.b * w1 + upperLeftColor.b * w2 + upperRightColor.b * w3;\r\n result.a = lowerLeftColor.a * w0 + lowerRightColor.a * w1 + upperLeftColor.a * w2 + upperRightColor.a * w3;\r\n return result;\r\n}\r\n\r\n/**\r\n * Fetches a texel from a 2D texture and stores the result in the given color.\r\n * @param x - The x coordinate in texels.\r\n * @param y - The y coordinate in texels.\r\n * @param width - The width of the texture in texels.\r\n * @param height - The height of the texture in texels.\r\n * @param data - The texture data to sample from.\r\n * @param result - The color to store the sampled color in.\r\n * @param normalizeFunc - The function to normalize the texel values. Default is to divide by 255. Pass null for no normalization.\r\n * @returns The result color.\r\n */\r\nconst TexelFetch2DRgbaToRef = <T extends IColor4Like>(\r\n x: number,\r\n y: number,\r\n width: number,\r\n height: number,\r\n data: Uint8Array | Uint16Array | Float32Array,\r\n result: T,\r\n normalizeFunc: ((value: number) => number) | null = DefaultNormalize\r\n): T => {\r\n const clampedTexelX = Clamp(x, 0, width - 1);\r\n const clampedTexelY = Clamp(y, 0, height - 1);\r\n const index = 4 * (clampedTexelY * width + clampedTexelX);\r\n if (normalizeFunc) {\r\n result.r = normalizeFunc(data[index]);\r\n result.g = normalizeFunc(data[index + 1]);\r\n result.b = normalizeFunc(data[index + 2]);\r\n result.a = normalizeFunc(data[index + 3]);\r\n } else {\r\n result.r = data[index];\r\n result.g = data[index + 1];\r\n result.b = data[index + 2];\r\n result.a = data[index + 3];\r\n }\r\n return result;\r\n};\r\n"]}
@@ -40,7 +40,7 @@ const SampleLutToRef = (properties, lutData, positionDistanceToOrigin, cosAngleL
40
40
  return;
41
41
  }
42
42
  ComputeLutUVToRef(properties, positionDistanceToOrigin, cosAngleLightToZenith, Uv);
43
- Sample2DRgbaToRef(Uv.x, Uv.y, LutWidthPx, LutHeightPx, lutData, result, UseHalfFloat ? FromHalfFloat : (value) => value / 255.0);
43
+ Sample2DRgbaToRef(Uv.x, Uv.y, LutWidthPx, LutHeightPx, lutData, result, null);
44
44
  const weight = Clamp(SmoothStep(1.0, 0.0, Clamp((Uv.x - TransmittanceMaxUnoccludedU) / TransmittanceHorizonRange)));
45
45
  result.r *= weight;
46
46
  result.g *= weight;
@@ -83,7 +83,7 @@ export class TransmittanceLut {
83
83
  * This is typically infrequent (once at startup), but also happens whenever the atmosphere's properties change.
84
84
  */
85
85
  this.onUpdatedObservable = new Observable();
86
- this._lutData = new Uint8Array(0);
86
+ this._lutData = new Float32Array(0);
87
87
  this._isDirty = true;
88
88
  this._isDisposed = false;
89
89
  this._needsReadPixels = false;
@@ -207,9 +207,20 @@ export class TransmittanceLut {
207
207
  return;
208
208
  }
209
209
  this._needsReadPixels = false;
210
- const value = await this.renderTarget.readPixels(0, 0, undefined, undefined, UseHalfFloat /* noDataConversion */);
210
+ const value = await this.renderTarget.readPixels(0, 0, undefined, undefined, true /* noDataConversion */);
211
211
  if (value && !this._isDisposed) {
212
- this._lutData = value;
212
+ const rawLutData = value;
213
+ const lutData = (this._lutData = new Float32Array(rawLutData.length));
214
+ if (rawLutData instanceof Uint16Array) {
215
+ for (let i = 0; i < rawLutData.length; i++) {
216
+ lutData[i] = FromHalfFloat(rawLutData[i]);
217
+ }
218
+ }
219
+ else {
220
+ for (let i = 0; i < rawLutData.length; i++) {
221
+ lutData[i] = rawLutData[i] / 255.0;
222
+ }
223
+ }
213
224
  this.onUpdatedObservable.notifyObservers();
214
225
  }
215
226
  }
@@ -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,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAGvD,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,6CAA6C,CAAC;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAG9D,MAAM,UAAU,GAAG,GAAG,CAAC;AACvB,MA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mosphere } 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, type IColor4Like, type IVector2Like, type 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 private _needsReadPixels = 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 list.push(\r\n ...(useWebGPU\r\n ? [import(\"./ShadersWGSL/fullscreenTriangle.vertex\"), import(\"./ShadersWGSL/transmittance.fragment\")]\r\n : [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 effectRenderer = this._effectRenderer!;\r\n effectRenderer.saveStates();\r\n\r\n const engine = this._atmosphere.scene.getEngine();\r\n engine.bindFramebuffer(this.renderTarget.renderTarget!, undefined, undefined, undefined, true);\r\n\r\n effectRenderer.setViewport();\r\n effectRenderer.applyEffectWrapper(effectWrapper);\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 this._needsReadPixels = true;\r\n\r\n // Defer readPixels to the end of the frame.\r\n this._atmosphere.scene.onAfterRenderObservable.addOnce(async () => {\r\n await this.readPixelsAsync();\r\n });\r\n\r\n return true;\r\n }\r\n\r\n /**\r\n * Reads back the LUT data from the GPU if a readback is pending.\r\n * @internal\r\n */\r\n public async readPixelsAsync(): Promise<void> {\r\n if (!this._needsReadPixels || this._isDisposed) {\r\n return;\r\n }\r\n this._needsReadPixels = false;\r\n\r\n const value = await this.renderTarget.readPixels(0, 0, undefined, undefined, UseHalfFloat /* noDataConversion */);\r\n if (value && !this._isDisposed) {\r\n this._lutData = value as Uint8Array | Uint16Array;\r\n this.onUpdatedObservable.notifyObservers();\r\n }\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,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,+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,CAAC;iBAAM,CAAC;gBACJ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;oBACzC,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;gBACvC,CAAC;YACL,CAAC;YACD,IAAI,CAAC,mBAAmB,CAAC,eAAe,EAAE,CAAC;QAC/C,CAAC;IACL,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, type IColor4Like, type IVector2Like, type 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: Float32Array,\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, null);\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: Float32Array = new Float32Array(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 private _needsReadPixels = 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 list.push(\r\n ...(useWebGPU\r\n ? [import(\"./ShadersWGSL/fullscreenTriangle.vertex\"), import(\"./ShadersWGSL/transmittance.fragment\")]\r\n : [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 effectRenderer = this._effectRenderer!;\r\n effectRenderer.saveStates();\r\n\r\n const engine = this._atmosphere.scene.getEngine();\r\n engine.bindFramebuffer(this.renderTarget.renderTarget!, undefined, undefined, undefined, true);\r\n\r\n effectRenderer.setViewport();\r\n effectRenderer.applyEffectWrapper(effectWrapper);\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 this._needsReadPixels = true;\r\n\r\n // Defer readPixels to the end of the frame.\r\n this._atmosphere.scene.onAfterRenderObservable.addOnce(async () => {\r\n await this.readPixelsAsync();\r\n });\r\n\r\n return true;\r\n }\r\n\r\n /**\r\n * Reads back the LUT data from the GPU if a readback is pending.\r\n * @internal\r\n */\r\n public async readPixelsAsync(): Promise<void> {\r\n if (!this._needsReadPixels || this._isDisposed) {\r\n return;\r\n }\r\n this._needsReadPixels = false;\r\n\r\n const value = await this.renderTarget.readPixels(0, 0, undefined, undefined, true /* noDataConversion */);\r\n if (value && !this._isDisposed) {\r\n const rawLutData = value as Uint8Array | Uint16Array;\r\n const lutData = (this._lutData = new Float32Array(rawLutData.length));\r\n if (rawLutData instanceof Uint16Array) {\r\n for (let i = 0; i < rawLutData.length; i++) {\r\n lutData[i] = FromHalfFloat(rawLutData[i]);\r\n }\r\n } else {\r\n for (let i = 0; i < rawLutData.length; i++) {\r\n lutData[i] = rawLutData[i] / 255.0;\r\n }\r\n }\r\n this.onUpdatedObservable.notifyObservers();\r\n }\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": "@onerjs/addons",
3
- "version": "8.49.4",
3
+ "version": "8.49.5",
4
4
  "main": "index.js",
5
5
  "module": "index.js",
6
6
  "types": "index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "postcompile": "build-tools -c add-js-to-es6"
19
19
  },
20
20
  "devDependencies": {
21
- "@onerjs/core": "8.49.4",
21
+ "@onerjs/core": "8.49.5",
22
22
  "@dev/addons": "^1.0.0",
23
23
  "@dev/build-tools": "^1.0.0"
24
24
  },