-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathdiffuseSkyIrradianceLut.ts
More file actions
285 lines (246 loc) · 12.1 KB
/
diffuseSkyIrradianceLut.ts
File metadata and controls
285 lines (246 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { type Atmosphere } from "./atmosphere";
import { type AtmospherePhysicalProperties } from "./atmospherePhysicalProperties";
import { Clamp } from "core/Maths/math.scalar.functions";
import { Constants } from "core/Engines/constants";
import { EffectRenderer, EffectWrapper } from "core/Materials/effectRenderer";
import { FromHalfFloat } from "core/Misc/textureTools";
import { type IColor3Like, type IColor4Like, type IVector2Like, type IVector3Like } from "core/Maths/math.like";
import { type Nullable } from "core/types";
import { RenderTargetTexture } from "core/Materials/Textures/renderTargetTexture";
import { Sample2DRgbaToRef } from "./sampling";
import { ShaderLanguage } from "core/Materials/shaderLanguage";
import { ShaderStore } from "core/Engines/shaderStore";
import { Vector3Dot } from "core/Maths/math.vector.functions";
const RaySamples = 128;
const LutWidthPx = 64;
const LutHeightPx = 16;
const HalfTexelSize = { x: 0.5 / LutWidthPx, y: 0.5 / LutHeightPx };
const UnitToUVScale = { x: (LutWidthPx - 1.0) / LutWidthPx, y: (LutHeightPx - 1.0) / LutHeightPx };
const UvTemp = { x: Number.NaN, y: Number.NaN };
const Color4Temp = { r: Number.NaN, g: Number.NaN, b: Number.NaN, a: Number.NaN } as IColor4Like;
const ComputeLutUVToRef = (properties: AtmospherePhysicalProperties, radius: number, cosAngleLightToZenith: number, result: IVector2Like): void => {
const unitX = Clamp(0.5 + 0.5 * cosAngleLightToZenith);
const unitY = Clamp((radius - properties.planetRadius) / properties.atmosphereThickness);
result.x = unitX * UnitToUVScale.x + HalfTexelSize.x;
result.y = unitY * UnitToUVScale.y + HalfTexelSize.y;
};
/**
* The diffuse sky irradiance LUT is used to query the diffuse irradiance at a specified position.
*/
export class DiffuseSkyIrradianceLut {
private readonly _atmosphere: Atmosphere;
private _renderTarget: Nullable<RenderTargetTexture> = null;
private _effectWrapper: Nullable<EffectWrapper> = null;
private _effectRenderer: Nullable<EffectRenderer> = null;
private _isDirty = true;
private _isDisposed = false;
private _needsReadPixels = false;
private _lutData: Uint8Array | Uint16Array = new Uint16Array(0);
/**
* True if the LUT needs to be rendered.
*/
public get isDirty() {
return this._isDirty;
}
/**
* True if the LUT has been disposed.
*/
public get isDisposed(): boolean {
return this._isDisposed;
}
/**
* The render target used for this LUT.
* @throws if the LUT has been disposed.
*/
public get renderTarget(): RenderTargetTexture {
if (this._isDisposed || this._renderTarget === null) {
throw new Error();
}
return this._renderTarget;
}
/**
* True if the LUT data has been read back from the GPU.
*/
public get hasLutData(): boolean {
return this._lutData[0] !== undefined;
}
/**
* Constructs the {@link DiffuseSkyIrradianceLut}.
* @param atmosphere - The atmosphere to use.
*/
constructor(atmosphere: Atmosphere) {
this._atmosphere = atmosphere;
const scene = atmosphere.scene;
const engine = scene.getEngine();
const name = "atmo-diffuseSkyIrradiance";
const caps = engine.getCaps();
const textureType = caps.textureHalfFloatRender ? Constants.TEXTURETYPE_HALF_FLOAT : Constants.TEXTURETYPE_UNSIGNED_BYTE;
const renderTarget = (this._renderTarget = new RenderTargetTexture(name, { width: LutWidthPx, height: LutHeightPx }, scene, {
generateMipMaps: false,
type: textureType,
samplingMode: Constants.TEXTURE_BILINEAR_SAMPLINGMODE,
generateDepthBuffer: false,
gammaSpace: false,
}));
renderTarget.wrapU = Constants.TEXTURE_CLAMP_ADDRESSMODE;
renderTarget.wrapV = Constants.TEXTURE_CLAMP_ADDRESSMODE;
renderTarget.anisotropicFilteringLevel = 1;
renderTarget.skipInitialClear = true;
const atmosphereUbo = atmosphere.uniformBuffer;
const useUbo = atmosphereUbo.useUbo;
const useWebGPU = engine.isWebGPU && !EffectWrapper.ForceGLSL;
const uboName = useWebGPU ? "atmosphere" : atmosphereUbo.name;
this._effectWrapper = new EffectWrapper({
engine,
name,
vertexShader: "fullscreenTriangle",
fragmentShader: "diffuseSkyIrradiance",
attributeNames: ["position"],
uniformNames: ["depth", ...(useUbo ? [] : atmosphereUbo.getUniformNames())],
uniformBuffers: useUbo ? [uboName] : [],
defines: ["#define POSITION_VEC2", `#define NUM_SAMPLES ${RaySamples}u`, "#define CUSTOM_IRRADIANCE_FILTERING_INPUT", "#define CUSTOM_IRRADIANCE_FILTERING_FUNCTION"],
samplers: ["transmittanceLut", "multiScatteringLut"],
useShaderStore: true,
shaderLanguage: useWebGPU ? ShaderLanguage.WGSL : ShaderLanguage.GLSL,
extraInitializationsAsync: async () => {
await Promise.all(
useWebGPU
? [import("./ShadersWGSL/fullscreenTriangle.vertex"), import("./ShadersWGSL/diffuseSkyIrradiance.fragment")]
: [import("./Shaders/fullscreenTriangle.vertex"), import("./Shaders/diffuseSkyIrradiance.fragment")]
);
await ShaderStore.LoadPendingIncludesAsync();
// Replace the CUSTOM_IRRADIANCE_FILTERING_INPUT and CUSTOM_IRRADIANCE_FILTERING_FUNCTION placeholders.
// Note, the regex replacements look for lines that *only* contain these placeholder strings.
// Since buildShaders removes leading whitespace, the placeholders are expected to start at the beginning of the line.
const includeStore = useWebGPU ? ShaderStore.IncludesShadersStoreWGSL : ShaderStore.IncludesShadersStore;
let patchedInclude = includeStore["hdrFilteringFunctions"];
patchedInclude = patchedInclude.replace(/^CUSTOM_IRRADIANCE_FILTERING_INPUT\s*$/gm, "");
patchedInclude = patchedInclude.replace(
/^CUSTOM_IRRADIANCE_FILTERING_FUNCTION\s*$/gm,
useWebGPU ? "var c = integrateForIrradiance(n, Ls, vec3f(0., filteringInfo.x, 0.));" : "vec3 c = integrateForIrradiance(n, Ls, vec3(0., filteringInfo.x, 0.));"
);
// Replace the existing #include<hdrFilteringFunctions> with the patched include.
const shaderStore = useWebGPU ? ShaderStore.ShadersStoreWGSL : ShaderStore.ShadersStore;
let shader = shaderStore["diffuseSkyIrradiancePixelShader"];
shader = shader.replace("#include<hdrFilteringFunctions>", patchedInclude);
shaderStore["diffuseSkyIrradiancePixelShader"] = shader;
},
});
this._effectRenderer = new EffectRenderer(engine, {
// Full screen triangle.
indices: [0, 2, 1],
positions: [-1, -1, -1, 3, 3, -1],
});
// The sky irradiance will also be used for the environment texture.
scene.environmentTexture = renderTarget;
scene.environmentTexture.irradianceTexture = renderTarget;
scene.environmentIntensity = 1.0;
// Prevent the irradiance LUT from being rendered redundantly at the beginning of the frame.
scene.environmentTexture.isRenderTarget = false;
}
/**
* Gets the diffuse sky irradiance for a surface oriented along the geocentric normal.
* Resulting color is always in linear space.
* @param directionToLight - The direction to the light in world space.
* @param radius - The position's distance to the planet origin.
* @param cameraGeocentricNormal - The geocentric normal of the camera.
* @param lightIrradiance - The irradiance of the light.
* @param result - The color to store the result in.
* @returns The result color.
*/
public getDiffuseSkyIrradianceToRef<T extends IColor3Like>(
directionToLight: IVector3Like,
radius: number,
cameraGeocentricNormal: IVector3Like,
lightIrradiance: number,
result: T
): T {
const atmosphere = this._atmosphere;
const additionalDiffuseSkyIrradiance = atmosphere.additionalDiffuseSkyIrradiance;
const properties = atmosphere.physicalProperties;
if (this._lutData[0] === undefined || radius > properties.atmosphereRadius) {
result.r = additionalDiffuseSkyIrradiance.r;
result.g = additionalDiffuseSkyIrradiance.g;
result.b = additionalDiffuseSkyIrradiance.b;
return result;
}
const cosAngleLightToZenith = Vector3Dot(directionToLight, cameraGeocentricNormal);
ComputeLutUVToRef(properties, radius, cosAngleLightToZenith, UvTemp);
Sample2DRgbaToRef(UvTemp.x, UvTemp.y, LutWidthPx, LutHeightPx, this._lutData, Color4Temp, FromHalfFloat);
const intensity = atmosphere.diffuseSkyIrradianceIntensity;
result.r = intensity * (lightIrradiance * Color4Temp.r + additionalDiffuseSkyIrradiance.r);
result.g = intensity * (lightIrradiance * Color4Temp.g + additionalDiffuseSkyIrradiance.g);
result.b = intensity * (lightIrradiance * Color4Temp.b + additionalDiffuseSkyIrradiance.b);
return result;
}
/**
* Renders the LUT.
* @returns True if the LUT was rendered.
*/
public render(): boolean {
// Only need to render the LUT once.
const effectWrapper = this._effectWrapper;
if (!this._isDirty || !effectWrapper?.isReady() || !this._renderTarget?.isReady()) {
return false;
}
const effectRenderer = this._effectRenderer!;
effectRenderer.saveStates();
const engine = this._atmosphere.scene.getEngine();
engine.bindFramebuffer(this.renderTarget.renderTarget!, undefined, undefined, undefined, true);
effectRenderer.setViewport();
effectRenderer.applyEffectWrapper(effectWrapper);
const effect = effectWrapper.effect;
effectRenderer.bindBuffers(effect);
effect.setTexture("transmittanceLut", this._atmosphere.transmittanceLut!.renderTarget);
effect.setTexture("multiScatteringLut", this._atmosphere.multiScatteringLutRenderTarget);
this._atmosphere.bindUniformBufferToEffect(effect);
effect.setFloat("depth", 0.0);
effectRenderer.draw();
effectRenderer.restoreStates();
engine.restoreDefaultFramebuffer();
this._isDirty = false;
this._needsReadPixels = true;
// Defer readPixels to the end of the frame.
this._atmosphere.scene.onAfterRenderObservable.addOnce(async () => {
await this.readPixelsAsync();
});
return true;
}
/**
* Reads back the LUT data from the GPU if a readback is pending.
* @internal
*/
public async readPixelsAsync(): Promise<void> {
if (!this._needsReadPixels || this._isDisposed) {
return;
}
this._needsReadPixels = false;
const value = await this.renderTarget.readPixels(0, 0, undefined, true, true /* noDataConversion */);
if (value && !this._isDisposed) {
this._lutData = value as Uint8Array | Uint16Array;
}
}
/**
* Marks the LUT as needing to be rendered.
*/
public markDirty(): void {
this._isDirty = true;
}
/**
* Disposes the LUT.
*/
public dispose() {
if (this._renderTarget) {
this._renderTarget.irradianceTexture = null;
this._renderTarget.dispose();
}
this._renderTarget = null;
this._effectWrapper?.dispose();
this._effectWrapper = null;
this._effectRenderer?.dispose();
this._effectRenderer = null;
this._isDisposed = true;
}
}