@onerjs/addons 8.25.3 → 8.25.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.
- package/atmosphere/Shaders/ShadersInclude/atmosphereFragmentDeclaration.d.ts +5 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereFragmentDeclaration.js +12 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereFragmentDeclaration.js.map +1 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereFunctions.d.ts +6 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereFunctions.js +234 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereFunctions.js.map +1 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereUboDeclaration.d.ts +5 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereUboDeclaration.js +12 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereUboDeclaration.js.map +1 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereVertexDeclaration.d.ts +5 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereVertexDeclaration.js +12 -0
- package/atmosphere/Shaders/ShadersInclude/atmosphereVertexDeclaration.js.map +1 -0
- package/atmosphere/Shaders/ShadersInclude/depthFunctions.d.ts +5 -0
- package/atmosphere/Shaders/ShadersInclude/depthFunctions.js +19 -0
- package/atmosphere/Shaders/ShadersInclude/depthFunctions.js.map +1 -0
- package/atmosphere/Shaders/aerialPerspective.fragment.d.ts +9 -0
- package/atmosphere/Shaders/aerialPerspective.fragment.js +25 -0
- package/atmosphere/Shaders/aerialPerspective.fragment.js.map +1 -0
- package/atmosphere/Shaders/compositeAerialPerspective.fragment.d.ts +10 -0
- package/atmosphere/Shaders/compositeAerialPerspective.fragment.js +72 -0
- package/atmosphere/Shaders/compositeAerialPerspective.fragment.js.map +1 -0
- package/atmosphere/Shaders/compositeGlobeAtmosphere.fragment.d.ts +10 -0
- package/atmosphere/Shaders/compositeGlobeAtmosphere.fragment.js +96 -0
- package/atmosphere/Shaders/compositeGlobeAtmosphere.fragment.js.map +1 -0
- package/atmosphere/Shaders/compositeSky.fragment.d.ts +10 -0
- package/atmosphere/Shaders/compositeSky.fragment.js +75 -0
- package/atmosphere/Shaders/compositeSky.fragment.js.map +1 -0
- package/atmosphere/Shaders/diffuseSkyIrradiance.fragment.d.ts +13 -0
- package/atmosphere/Shaders/diffuseSkyIrradiance.fragment.js +49 -0
- package/atmosphere/Shaders/diffuseSkyIrradiance.fragment.js.map +1 -0
- package/atmosphere/Shaders/fullscreenTriangle.vertex.d.ts +7 -0
- package/atmosphere/Shaders/fullscreenTriangle.vertex.js +31 -0
- package/atmosphere/Shaders/fullscreenTriangle.vertex.js.map +1 -0
- package/atmosphere/Shaders/multiScattering.fragment.d.ts +9 -0
- package/atmosphere/Shaders/multiScattering.fragment.js +21 -0
- package/atmosphere/Shaders/multiScattering.fragment.js.map +1 -0
- package/atmosphere/Shaders/skyView.fragment.d.ts +9 -0
- package/atmosphere/Shaders/skyView.fragment.js +20 -0
- package/atmosphere/Shaders/skyView.fragment.js.map +1 -0
- package/atmosphere/Shaders/transmittance.fragment.d.ts +9 -0
- package/atmosphere/Shaders/transmittance.fragment.js +20 -0
- package/atmosphere/Shaders/transmittance.fragment.js.map +1 -0
- package/atmosphere/atmosphere.d.ts +418 -0
- package/atmosphere/atmosphere.js +1446 -0
- package/atmosphere/atmosphere.js.map +1 -0
- package/atmosphere/atmosphereOptions.d.ts +135 -0
- package/atmosphere/atmosphereOptions.js +4 -0
- package/atmosphere/atmosphereOptions.js.map +1 -0
- package/atmosphere/atmospherePBRMaterialPlugin.d.ts +72 -0
- package/atmosphere/atmospherePBRMaterialPlugin.js +230 -0
- package/atmosphere/atmospherePBRMaterialPlugin.js.map +1 -0
- package/atmosphere/atmospherePerCameraVariables.d.ts +102 -0
- package/atmosphere/atmospherePerCameraVariables.js +189 -0
- package/atmosphere/atmospherePerCameraVariables.js.map +1 -0
- package/atmosphere/atmospherePhysicalProperties.d.ts +154 -0
- package/atmosphere/atmospherePhysicalProperties.js +305 -0
- package/atmosphere/atmospherePhysicalProperties.js.map +1 -0
- package/atmosphere/atmospherePhysicalPropertiesOptions.d.ts +52 -0
- package/atmosphere/atmospherePhysicalPropertiesOptions.js +4 -0
- package/atmosphere/atmospherePhysicalPropertiesOptions.js.map +1 -0
- package/atmosphere/diffuseSkyIrradianceLut.d.ts +63 -0
- package/atmosphere/diffuseSkyIrradianceLut.js +199 -0
- package/atmosphere/diffuseSkyIrradianceLut.js.map +1 -0
- package/atmosphere/index.d.ts +6 -0
- package/atmosphere/index.js +9 -0
- package/atmosphere/index.js.map +1 -0
- package/atmosphere/sampling.d.ts +15 -0
- package/atmosphere/sampling.js +89 -0
- package/atmosphere/sampling.js.map +1 -0
- package/atmosphere/test/unit/sampling.test.d.ts +1 -0
- package/atmosphere/test/unit/sampling.test.js +77 -0
- package/atmosphere/test/unit/sampling.test.js.map +1 -0
- package/atmosphere/transmittanceLut.d.ts +68 -0
- package/atmosphere/transmittanceLut.js +207 -0
- package/atmosphere/transmittanceLut.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/msdfText/paragraphOptions.d.ts +1 -1
- package/msdfText/paragraphOptions.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// MIT License
|
|
3
|
+
import { MaterialDefines } from "@onerjs/core/Materials/materialDefines.js";
|
|
4
|
+
import { MaterialPluginBase } from "@onerjs/core/Materials/materialPluginBase.js";
|
|
5
|
+
class AtmospherePBRMaterialDefines extends MaterialDefines {
|
|
6
|
+
/**
|
|
7
|
+
* Constructs the {@link AtmospherePBRMaterialDefines}.
|
|
8
|
+
* @param useAerialPerspectiveLut - Whether to use the aerial perspective LUT.
|
|
9
|
+
*/
|
|
10
|
+
constructor(useAerialPerspectiveLut) {
|
|
11
|
+
super();
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
13
|
+
this.APPLY_AERIAL_PERSPECTIVE_INTENSITY = false;
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
15
|
+
this.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = false;
|
|
16
|
+
this.USE_AERIAL_PERSPECTIVE_LUT = useAerialPerspectiveLut;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const UboArray = [{ name: "inverseViewportSize", size: 2, type: "vec2" }];
|
|
20
|
+
const MakeUniforms = (atmosphere) => ({
|
|
21
|
+
ubo: UboArray,
|
|
22
|
+
fragment: "uniform vec2 inverseViewportSize;\n",
|
|
23
|
+
externalUniforms: atmosphere.uniformBuffer.getUniformNames(),
|
|
24
|
+
});
|
|
25
|
+
const PluginName = "AtmospherePBRMaterialPlugin";
|
|
26
|
+
const PluginPriority = 600;
|
|
27
|
+
/**
|
|
28
|
+
* Adds shading logic to a PBRMaterial that provides radiance, diffuse sky irradiance, and aerial perspective from the atmosphere.
|
|
29
|
+
*/
|
|
30
|
+
export class AtmospherePBRMaterialPlugin extends MaterialPluginBase {
|
|
31
|
+
/**
|
|
32
|
+
* Constructs the {@link AtmospherePBRMaterialPlugin}.
|
|
33
|
+
* @param material - The material to apply the plugin to.
|
|
34
|
+
* @param _atmosphere - The atmosphere to use for shading.
|
|
35
|
+
* @param _isAerialPerspectiveEnabled - Whether to apply aerial perspective.
|
|
36
|
+
*/
|
|
37
|
+
constructor(material, _atmosphere, _isAerialPerspectiveEnabled = false) {
|
|
38
|
+
super(material, PluginName, PluginPriority, {
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
40
|
+
USE_CUSTOM_REFLECTION: _atmosphere.diffuseSkyIrradianceLut !== null,
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
42
|
+
CUSTOM_FRAGMENT_BEFORE_FOG: _isAerialPerspectiveEnabled,
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
44
|
+
USE_AERIAL_PERSPECTIVE_LUT: _isAerialPerspectiveEnabled && _atmosphere.isAerialPerspectiveLutEnabled,
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
46
|
+
APPLY_AERIAL_PERSPECTIVE_INTENSITY: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveIntensity !== 1.0,
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
48
|
+
APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveRadianceBias !== 0.0,
|
|
49
|
+
}, false, // addPluginToList -- false because we need to control when this is added to the list
|
|
50
|
+
true, // enable
|
|
51
|
+
true // resolveIncludes
|
|
52
|
+
);
|
|
53
|
+
this._atmosphere = _atmosphere;
|
|
54
|
+
this._isAerialPerspectiveEnabled = _isAerialPerspectiveEnabled;
|
|
55
|
+
this.doNotSerialize = true;
|
|
56
|
+
// This calls `getCode` so we need to do this after having initialized the class fields.
|
|
57
|
+
this._pluginManager._addPlugin(this);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @override
|
|
61
|
+
*/
|
|
62
|
+
getUniformBuffersNames(_ubos) {
|
|
63
|
+
const uniformBuffer = this._atmosphere.uniformBuffer;
|
|
64
|
+
if (uniformBuffer.useUbo) {
|
|
65
|
+
_ubos.push(uniformBuffer.name);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @override
|
|
70
|
+
*/
|
|
71
|
+
getUniforms() {
|
|
72
|
+
return MakeUniforms(this._atmosphere);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @override
|
|
76
|
+
*/
|
|
77
|
+
isReadyForSubMesh() {
|
|
78
|
+
let isReady = true;
|
|
79
|
+
const atmosphere = this._atmosphere;
|
|
80
|
+
if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {
|
|
81
|
+
const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;
|
|
82
|
+
isReady = isReady && !!aerialPerspectiveLutRenderTarget?.isReady();
|
|
83
|
+
}
|
|
84
|
+
const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;
|
|
85
|
+
isReady = isReady && !!transmittanceLutRenderTarget?.isReady();
|
|
86
|
+
return isReady;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* @override
|
|
90
|
+
*/
|
|
91
|
+
getActiveTextures(_activeTextures) {
|
|
92
|
+
const atmosphere = this._atmosphere;
|
|
93
|
+
if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {
|
|
94
|
+
const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;
|
|
95
|
+
if (aerialPerspectiveLutRenderTarget) {
|
|
96
|
+
_activeTextures.push(aerialPerspectiveLutRenderTarget);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;
|
|
100
|
+
if (transmittanceLutRenderTarget) {
|
|
101
|
+
_activeTextures.push(transmittanceLutRenderTarget);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @override
|
|
106
|
+
*/
|
|
107
|
+
bindForSubMesh(uniformBuffer) {
|
|
108
|
+
const atmosphere = this._atmosphere;
|
|
109
|
+
const engine = atmosphere.scene.getEngine();
|
|
110
|
+
// Bind the atmosphere's uniform buffer to the effect.
|
|
111
|
+
const effect = uniformBuffer.currentEffect;
|
|
112
|
+
if (effect) {
|
|
113
|
+
this._atmosphere.bindUniformBufferToEffect(effect);
|
|
114
|
+
}
|
|
115
|
+
const width = engine.getRenderWidth();
|
|
116
|
+
const height = engine.getRenderHeight();
|
|
117
|
+
uniformBuffer.updateFloat2("inverseViewportSize", 1.0 / width, 1.0 / height);
|
|
118
|
+
if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {
|
|
119
|
+
const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;
|
|
120
|
+
uniformBuffer.setTexture("aerialPerspectiveLut", aerialPerspectiveLutRenderTarget);
|
|
121
|
+
}
|
|
122
|
+
const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;
|
|
123
|
+
uniformBuffer.setTexture("transmittanceLut", transmittanceLutRenderTarget);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @override
|
|
127
|
+
*/
|
|
128
|
+
prepareDefines(defines) {
|
|
129
|
+
const lastUseAerialPerspectiveLut = defines.USE_AERIAL_PERSPECTIVE_LUT;
|
|
130
|
+
const lastApplyAerialPerspectiveIntensity = defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY;
|
|
131
|
+
const lastApplyAerialPerspectiveRadianceBias = defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS;
|
|
132
|
+
defines.USE_AERIAL_PERSPECTIVE_LUT = this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled;
|
|
133
|
+
defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveIntensity !== 1.0;
|
|
134
|
+
defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveRadianceBias !== 0.0;
|
|
135
|
+
if (lastUseAerialPerspectiveLut !== defines.USE_AERIAL_PERSPECTIVE_LUT ||
|
|
136
|
+
lastApplyAerialPerspectiveIntensity !== defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY ||
|
|
137
|
+
lastApplyAerialPerspectiveRadianceBias !== defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS) {
|
|
138
|
+
defines.markAllAsDirty();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @override
|
|
143
|
+
*/
|
|
144
|
+
getSamplers(samplers) {
|
|
145
|
+
samplers.push("transmittanceLut");
|
|
146
|
+
if (this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled) {
|
|
147
|
+
samplers.push("aerialPerspectiveLut");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @override
|
|
152
|
+
*/
|
|
153
|
+
getCustomCode(shaderType) {
|
|
154
|
+
// Assumed inputs are light0, vPositionW, normalW.
|
|
155
|
+
// Only works for directional lights.
|
|
156
|
+
if (shaderType !== "fragment") {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
const useUbo = this._atmosphere.scene.getEngine().supportsUniformBuffers;
|
|
160
|
+
const directionToLightSnippet = useUbo ? "-light0.vLightData.xyz" : "-vLightData0.xyz";
|
|
161
|
+
const useAtmosphereUbo = this._atmosphere.uniformBuffer.useUbo;
|
|
162
|
+
const atmosphereImportSnippet = useAtmosphereUbo ? "#include<atmosphereUboDeclaration>" : "#include<atmosphereFragmentDeclaration>";
|
|
163
|
+
return {
|
|
164
|
+
CUSTOM_FRAGMENT_DEFINITIONS: this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled
|
|
165
|
+
? `uniform sampler2D transmittanceLut;\r\nprecision highp sampler2DArray;\r\nuniform sampler2DArray aerialPerspectiveLut;\r\n${atmosphereImportSnippet}\r\n#include<atmosphereFunctions>`
|
|
166
|
+
: `uniform sampler2D transmittanceLut;\r\n${atmosphereImportSnippet}\r\n#include<atmosphereFunctions>`,
|
|
167
|
+
CUSTOM_LIGHT0_COLOR: `
|
|
168
|
+
{
|
|
169
|
+
vec3 positionGlobal = 0.001 * vPositionW + vec3(0., planetRadius, 0.);
|
|
170
|
+
float positionRadius = length(positionGlobal);
|
|
171
|
+
vec3 geocentricNormal = positionGlobal / positionRadius;
|
|
172
|
+
vec3 directionToLight = ${directionToLightSnippet};
|
|
173
|
+
float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);
|
|
174
|
+
diffuse0 = lightIntensity * sampleTransmittanceLut(transmittanceLut, positionRadius, cosAngleLightToZenith);
|
|
175
|
+
}
|
|
176
|
+
`,
|
|
177
|
+
CUSTOM_REFLECTION: `
|
|
178
|
+
{
|
|
179
|
+
vec3 positionGlobal = 0.001 * vPositionW + vec3(0., planetRadius, 0.);
|
|
180
|
+
float positionRadius = length(positionGlobal);
|
|
181
|
+
vec3 geocentricNormal = positionGlobal / positionRadius;
|
|
182
|
+
|
|
183
|
+
vec3 directionToLight = ${directionToLightSnippet};
|
|
184
|
+
float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);
|
|
185
|
+
|
|
186
|
+
vec2 uv = vec2(0.5 + 0.5 * cosAngleLightToZenith, (positionRadius - planetRadius) / atmosphereThickness);
|
|
187
|
+
float irradianceScaleT = 0.5 * dot(normalW, geocentricNormal) + 0.5;
|
|
188
|
+
float irradianceScale = ((-0.6652 * irradianceScaleT) + 1.5927) * irradianceScaleT + 0.1023;
|
|
189
|
+
vec3 environmentIrradiance = lightIntensity * sampleReflection(irradianceSampler, uv).rgb;
|
|
190
|
+
|
|
191
|
+
// Add a contribution here to estimate indirect lighting.
|
|
192
|
+
const float r = 0.2;
|
|
193
|
+
float indirect = getLuminance(environmentIrradiance) / max(0.00001, 1. - r);
|
|
194
|
+
environmentIrradiance *= irradianceScale;
|
|
195
|
+
environmentIrradiance += indirect;
|
|
196
|
+
|
|
197
|
+
environmentIrradiance += additionalDiffuseSkyIrradiance;
|
|
198
|
+
|
|
199
|
+
const float diffuseBrdf = 1. / PI;
|
|
200
|
+
environmentIrradiance *= diffuseBrdf * diffuseSkyIrradianceIntensity;
|
|
201
|
+
|
|
202
|
+
reflectionOut.environmentIrradiance = environmentIrradiance;
|
|
203
|
+
reflectionOut.environmentRadiance.rgb = reflectionOut.environmentIrradiance;
|
|
204
|
+
}
|
|
205
|
+
`,
|
|
206
|
+
// TODO: Support full ray marching if USE_AERIAL_PERSPECTIVE_LUT is disabled.
|
|
207
|
+
CUSTOM_FRAGMENT_BEFORE_FOG: `
|
|
208
|
+
#if USE_AERIAL_PERSPECTIVE_LUT
|
|
209
|
+
{
|
|
210
|
+
vec3 positionGlobal = 0.001 * vPositionW + vec3(0., planetRadius, 0.);
|
|
211
|
+
float distanceFromCamera = distance(positionGlobal, cameraPositionGlobal);
|
|
212
|
+
|
|
213
|
+
vec4 aerialPerspective = vec4(0.);
|
|
214
|
+
if (sampleAerialPerspectiveLut(
|
|
215
|
+
gl_FragCoord.xy * inverseViewportSize,
|
|
216
|
+
true,
|
|
217
|
+
distanceFromCamera,
|
|
218
|
+
NumAerialPerspectiveLutLayers,
|
|
219
|
+
AerialPerspectiveLutKMPerSlice,
|
|
220
|
+
AerialPerspectiveLutRangeKM,
|
|
221
|
+
aerialPerspective)) {
|
|
222
|
+
finalColor = aerialPerspective + (1. - aerialPerspective.a) * finalColor;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
#endif
|
|
226
|
+
`,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=atmospherePBRMaterialPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atmospherePBRMaterialPlugin.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/atmospherePBRMaterialPlugin.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,cAAc;AAKd,OAAO,EAAE,eAAe,EAAE,kDAAuC;AACjE,OAAO,EAAE,kBAAkB,EAAE,qDAA0C;AAIvE,MAAM,4BAA6B,SAAQ,eAAe;IAQtD;;;OAGG;IACH,YAAY,uBAAgC;QACxC,KAAK,EAAE,CAAC;QAVZ,gEAAgE;QACzD,uCAAkC,GAAG,KAAK,CAAC;QAClD,gEAAgE;QACzD,2CAAsC,GAAG,KAAK,CAAC;QAQlD,IAAI,CAAC,0BAA0B,GAAG,uBAAuB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EACvB,IAAI,CAAC,2BAA2B,IAAI,IAAI,CAAC,WAAW,CAAC,6BAA6B;gBAC9E,CAAC,CAAC,6HAA6H,uBAAuB,mCAAmC;gBACzL,CAAC,CAAC,0CAA0C,uBAAuB,mCAAmC;YAC9G,mBAAmB,EAAE;;;;;0CAKS,uBAAuB;;;;CAIhE;YACW,iBAAiB,EAAE;;;;;;0CAMW,uBAAuB;;;;;;;;;;;;;;;;;;;;;;CAsBhE;YACW,6EAA6E;YAC7E,0BAA0B,EAAE;;;;;;;;;;;;;;;;;;;CAmBvC;SACQ,CAAC;IACN,CAAC;CACJ","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// MIT License\r\n\r\nimport type { Atmosphere } from \"./atmosphere\";\r\nimport type { BaseTexture } from \"core/Materials/Textures/baseTexture\";\r\nimport type { Material } from \"core/Materials/material\";\r\nimport { MaterialDefines } from \"core/Materials/materialDefines\";\r\nimport { MaterialPluginBase } from \"core/Materials/materialPluginBase\";\r\nimport type { Nullable } from \"core/types\";\r\nimport type { UniformBuffer } from \"core/Materials/uniformBuffer\";\r\n\r\nclass AtmospherePBRMaterialDefines extends MaterialDefines {\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public USE_AERIAL_PERSPECTIVE_LUT: boolean;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public APPLY_AERIAL_PERSPECTIVE_INTENSITY = false;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n public APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = false;\r\n\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 UboArray = [{ name: \"inverseViewportSize\", size: 2, type: \"vec2\" }];\r\nconst MakeUniforms = (atmosphere: Atmosphere) => ({\r\n ubo: UboArray,\r\n fragment: \"uniform vec2 inverseViewportSize;\\n\",\r\n externalUniforms: atmosphere.uniformBuffer.getUniformNames(),\r\n});\r\n\r\nconst PluginName = \"AtmospherePBRMaterialPlugin\";\r\nconst PluginPriority = 600;\r\n\r\n/**\r\n * Adds shading logic to a PBRMaterial that provides radiance, diffuse sky irradiance, and aerial perspective from the atmosphere.\r\n */\r\nexport class AtmospherePBRMaterialPlugin extends MaterialPluginBase {\r\n /**\r\n * Constructs the {@link AtmospherePBRMaterialPlugin}.\r\n * @param material - The material to apply the plugin to.\r\n * @param _atmosphere - The atmosphere to use for shading.\r\n * @param _isAerialPerspectiveEnabled - Whether to apply aerial perspective.\r\n */\r\n constructor(\r\n material: Material,\r\n private readonly _atmosphere: Atmosphere,\r\n private readonly _isAerialPerspectiveEnabled = false\r\n ) {\r\n super(\r\n material,\r\n PluginName,\r\n PluginPriority,\r\n {\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n USE_CUSTOM_REFLECTION: _atmosphere.diffuseSkyIrradianceLut !== null,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n CUSTOM_FRAGMENT_BEFORE_FOG: _isAerialPerspectiveEnabled,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n USE_AERIAL_PERSPECTIVE_LUT: _isAerialPerspectiveEnabled && _atmosphere.isAerialPerspectiveLutEnabled,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n APPLY_AERIAL_PERSPECTIVE_INTENSITY: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveIntensity !== 1.0,\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS: _isAerialPerspectiveEnabled && _atmosphere.aerialPerspectiveRadianceBias !== 0.0,\r\n },\r\n false, // addPluginToList -- false because we need to control when this is added to the list\r\n true, // enable\r\n true // resolveIncludes\r\n );\r\n\r\n this.doNotSerialize = true;\r\n\r\n // This calls `getCode` so we need to do this after having initialized the class fields.\r\n this._pluginManager._addPlugin(this);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getUniformBuffersNames(_ubos: string[]): void {\r\n const uniformBuffer = this._atmosphere.uniformBuffer;\r\n if (uniformBuffer.useUbo) {\r\n _ubos.push(uniformBuffer.name);\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getUniforms() {\r\n return MakeUniforms(this._atmosphere);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override isReadyForSubMesh(): boolean {\r\n let isReady = true;\r\n const atmosphere = this._atmosphere;\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n isReady = isReady && !!aerialPerspectiveLutRenderTarget?.isReady();\r\n }\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n isReady = isReady && !!transmittanceLutRenderTarget?.isReady();\r\n return isReady;\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getActiveTextures(_activeTextures: BaseTexture[]): void {\r\n const atmosphere = this._atmosphere;\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n if (aerialPerspectiveLutRenderTarget) {\r\n _activeTextures.push(aerialPerspectiveLutRenderTarget);\r\n }\r\n }\r\n\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n if (transmittanceLutRenderTarget) {\r\n _activeTextures.push(transmittanceLutRenderTarget);\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override bindForSubMesh(uniformBuffer: UniformBuffer): void {\r\n const atmosphere = this._atmosphere;\r\n const engine = atmosphere.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 const width = engine.getRenderWidth();\r\n const height = engine.getRenderHeight();\r\n uniformBuffer.updateFloat2(\"inverseViewportSize\", 1.0 / width, 1.0 / height);\r\n\r\n if (this._isAerialPerspectiveEnabled && atmosphere.isAerialPerspectiveLutEnabled) {\r\n const aerialPerspectiveLutRenderTarget = atmosphere.aerialPerspectiveLutRenderTarget;\r\n uniformBuffer.setTexture(\"aerialPerspectiveLut\", aerialPerspectiveLutRenderTarget);\r\n }\r\n const transmittanceLutRenderTarget = atmosphere.transmittanceLut?.renderTarget ?? null;\r\n uniformBuffer.setTexture(\"transmittanceLut\", transmittanceLutRenderTarget);\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override prepareDefines(defines: AtmospherePBRMaterialDefines): void {\r\n const lastUseAerialPerspectiveLut = defines.USE_AERIAL_PERSPECTIVE_LUT;\r\n const lastApplyAerialPerspectiveIntensity = defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY;\r\n const lastApplyAerialPerspectiveRadianceBias = defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS;\r\n defines.USE_AERIAL_PERSPECTIVE_LUT = this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled;\r\n defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveIntensity !== 1.0;\r\n defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS = this._isAerialPerspectiveEnabled && this._atmosphere.aerialPerspectiveRadianceBias !== 0.0;\r\n if (\r\n lastUseAerialPerspectiveLut !== defines.USE_AERIAL_PERSPECTIVE_LUT ||\r\n lastApplyAerialPerspectiveIntensity !== defines.APPLY_AERIAL_PERSPECTIVE_INTENSITY ||\r\n lastApplyAerialPerspectiveRadianceBias !== defines.APPLY_AERIAL_PERSPECTIVE_RADIANCE_BIAS\r\n ) {\r\n defines.markAllAsDirty();\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getSamplers(samplers: string[]): void {\r\n samplers.push(\"transmittanceLut\");\r\n if (this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled) {\r\n samplers.push(\"aerialPerspectiveLut\");\r\n }\r\n }\r\n\r\n /**\r\n * @override\r\n */\r\n public override getCustomCode(shaderType: string): Nullable<Record<string, string>> {\r\n // Assumed inputs are light0, vPositionW, normalW.\r\n // Only works for directional lights.\r\n if (shaderType !== \"fragment\") {\r\n return null;\r\n }\r\n\r\n const useUbo = this._atmosphere.scene.getEngine().supportsUniformBuffers;\r\n const directionToLightSnippet = useUbo ? \"-light0.vLightData.xyz\" : \"-vLightData0.xyz\";\r\n\r\n const useAtmosphereUbo = this._atmosphere.uniformBuffer.useUbo;\r\n const atmosphereImportSnippet = useAtmosphereUbo ? \"#include<atmosphereUboDeclaration>\" : \"#include<atmosphereFragmentDeclaration>\";\r\n\r\n return {\r\n CUSTOM_FRAGMENT_DEFINITIONS:\r\n this._isAerialPerspectiveEnabled && this._atmosphere.isAerialPerspectiveLutEnabled\r\n ? `uniform sampler2D transmittanceLut;\\r\\nprecision highp sampler2DArray;\\r\\nuniform sampler2DArray aerialPerspectiveLut;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`\r\n : `uniform sampler2D transmittanceLut;\\r\\n${atmosphereImportSnippet}\\r\\n#include<atmosphereFunctions>`,\r\n CUSTOM_LIGHT0_COLOR: `\r\n {\r\n vec3 positionGlobal = 0.001 * vPositionW + vec3(0., planetRadius, 0.);\r\n float positionRadius = length(positionGlobal);\r\n vec3 geocentricNormal = positionGlobal / positionRadius;\r\n vec3 directionToLight = ${directionToLightSnippet};\r\n float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n diffuse0 = lightIntensity * sampleTransmittanceLut(transmittanceLut, positionRadius, cosAngleLightToZenith);\r\n }\r\n`,\r\n CUSTOM_REFLECTION: `\r\n {\r\n vec3 positionGlobal = 0.001 * vPositionW + vec3(0., planetRadius, 0.);\r\n float positionRadius = length(positionGlobal);\r\n vec3 geocentricNormal = positionGlobal / positionRadius;\r\n\r\n vec3 directionToLight = ${directionToLightSnippet};\r\n float cosAngleLightToZenith = dot(directionToLight, geocentricNormal);\r\n\r\n vec2 uv = vec2(0.5 + 0.5 * cosAngleLightToZenith, (positionRadius - planetRadius) / atmosphereThickness);\r\n float irradianceScaleT = 0.5 * dot(normalW, geocentricNormal) + 0.5;\r\n float irradianceScale = ((-0.6652 * irradianceScaleT) + 1.5927) * irradianceScaleT + 0.1023;\r\n vec3 environmentIrradiance = lightIntensity * sampleReflection(irradianceSampler, uv).rgb;\r\n\r\n // Add a contribution here to estimate indirect lighting.\r\n const float r = 0.2;\r\n float indirect = getLuminance(environmentIrradiance) / max(0.00001, 1. - r);\r\n environmentIrradiance *= irradianceScale;\r\n environmentIrradiance += indirect;\r\n\r\n environmentIrradiance += additionalDiffuseSkyIrradiance;\r\n\r\n const float diffuseBrdf = 1. / PI;\r\n environmentIrradiance *= diffuseBrdf * diffuseSkyIrradianceIntensity;\r\n\r\n reflectionOut.environmentIrradiance = environmentIrradiance;\r\n reflectionOut.environmentRadiance.rgb = reflectionOut.environmentIrradiance;\r\n }\r\n`,\r\n // TODO: Support full ray marching if USE_AERIAL_PERSPECTIVE_LUT is disabled.\r\n CUSTOM_FRAGMENT_BEFORE_FOG: `\r\n #if USE_AERIAL_PERSPECTIVE_LUT\r\n {\r\n vec3 positionGlobal = 0.001 * vPositionW + vec3(0., planetRadius, 0.);\r\n float distanceFromCamera = distance(positionGlobal, cameraPositionGlobal);\r\n\r\n vec4 aerialPerspective = vec4(0.);\r\n if (sampleAerialPerspectiveLut(\r\n gl_FragCoord.xy * inverseViewportSize,\r\n true,\r\n distanceFromCamera,\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"]}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { Camera } from "@onerjs/core/Cameras/camera.js";
|
|
2
|
+
import type { IMatrixLike, IVector3Like, IVector4Like } from "@onerjs/core/Maths/math.like.js";
|
|
3
|
+
/**
|
|
4
|
+
* Variables that are used to render the atmosphere and are computed per-camera.
|
|
5
|
+
*/
|
|
6
|
+
export declare class AtmospherePerCameraVariables {
|
|
7
|
+
private _inverseViewProjectionMatrixWithoutTranslation;
|
|
8
|
+
private _directionToLightRelativeToCameraGeocentricNormal;
|
|
9
|
+
private _cosAngleLightToZenith;
|
|
10
|
+
private _cameraRadius;
|
|
11
|
+
private _clampedCameraRadius;
|
|
12
|
+
private _cameraHeight;
|
|
13
|
+
private _clampedCameraHeight;
|
|
14
|
+
private _cameraPositionGlobal;
|
|
15
|
+
private _clampedCameraPositionGlobal;
|
|
16
|
+
private _cosCameraHorizonAngleFromZenith;
|
|
17
|
+
private _sinCameraAtmosphereHorizonAngleFromNadir;
|
|
18
|
+
private _cameraGeocentricNormal;
|
|
19
|
+
private _cameraForward;
|
|
20
|
+
private _cameraNearPlane;
|
|
21
|
+
private _cameraPosition;
|
|
22
|
+
private _viewport;
|
|
23
|
+
private _lastViewMatrix;
|
|
24
|
+
private _lastProjectionMatrix;
|
|
25
|
+
private _inverseViewMatrixWithoutTranslation;
|
|
26
|
+
private _inverseProjectionMatrix;
|
|
27
|
+
/**
|
|
28
|
+
* The inverse view projection matrix is used to unproject rays.
|
|
29
|
+
* To avoid precision issues, the translation part of the matrix has been removed.
|
|
30
|
+
*/
|
|
31
|
+
get inverseViewProjectionMatrixWithoutTranslation(): IMatrixLike;
|
|
32
|
+
/**
|
|
33
|
+
* The direction to the light relative to the geocentric normal under the camera.
|
|
34
|
+
*/
|
|
35
|
+
get directionToLightRelativeToCameraGeocentricNormal(): IVector3Like;
|
|
36
|
+
/**
|
|
37
|
+
* The cosine of the angle between the light direction and zenith.
|
|
38
|
+
*/
|
|
39
|
+
get cosAngleLightToZenith(): number;
|
|
40
|
+
/**
|
|
41
|
+
* The distance from the camera to the planet origin in kilometers.
|
|
42
|
+
*/
|
|
43
|
+
get cameraRadius(): number;
|
|
44
|
+
/**
|
|
45
|
+
* The distance from the camera to the planet origin, clamped to the planet radius offset, in kilometers.
|
|
46
|
+
*/
|
|
47
|
+
get clampedCameraRadius(): number;
|
|
48
|
+
/**
|
|
49
|
+
* The height of the camera above the planet surface in kilometers.
|
|
50
|
+
*/
|
|
51
|
+
get cameraHeight(): number;
|
|
52
|
+
/**
|
|
53
|
+
* The height of the camera above the planet surface, clamped to the planet radius offset, in kilometers.
|
|
54
|
+
*/
|
|
55
|
+
get clampedCameraHeight(): number;
|
|
56
|
+
/**
|
|
57
|
+
* The camera position in global space kilometers.
|
|
58
|
+
*/
|
|
59
|
+
get cameraPositionGlobal(): IVector3Like;
|
|
60
|
+
/**
|
|
61
|
+
* The camera position, clamped to the planet radius offset, in global space kilometers.
|
|
62
|
+
*/
|
|
63
|
+
get clampedCameraPositionGlobal(): IVector3Like;
|
|
64
|
+
/**
|
|
65
|
+
* The cosine of the angle from the zenith to the horizon of the planet, measured from the camera position.
|
|
66
|
+
*/
|
|
67
|
+
get cosCameraHorizonAngleFromZenith(): number;
|
|
68
|
+
/**
|
|
69
|
+
* The sine of the angle from the nadir to the horizon of the atmosphere, measured from the camera position.
|
|
70
|
+
*/
|
|
71
|
+
get sinCameraAtmosphereHorizonAngleFromNadir(): number;
|
|
72
|
+
/**
|
|
73
|
+
* The geocentric normal of the camera in global space i.e., the normalization of {@link cameraPositionGlobal}.
|
|
74
|
+
*/
|
|
75
|
+
get cameraGeocentricNormal(): IVector3Like;
|
|
76
|
+
/**
|
|
77
|
+
* The camera's forward direction in world space.
|
|
78
|
+
*/
|
|
79
|
+
get cameraForward(): IVector3Like;
|
|
80
|
+
/**
|
|
81
|
+
* The distance to the near plane of the camera.
|
|
82
|
+
*/
|
|
83
|
+
get cameraNearPlane(): number;
|
|
84
|
+
/**
|
|
85
|
+
* The camera's position in world space.
|
|
86
|
+
*/
|
|
87
|
+
get cameraPosition(): IVector3Like;
|
|
88
|
+
/**
|
|
89
|
+
* The viewport for the camera.
|
|
90
|
+
*/
|
|
91
|
+
get viewport(): IVector4Like;
|
|
92
|
+
/**
|
|
93
|
+
* Updates the variables.
|
|
94
|
+
* @param camera - The camera to update the variables for.
|
|
95
|
+
* @param planetRadius - The radius of the planet in kilometers.
|
|
96
|
+
* @param planetRadiusWithOffset - The radius of the planet with the offset in kilometers.
|
|
97
|
+
* @param atmosphereRadius - The radius of the atmosphere in kilometers.
|
|
98
|
+
* @param directionToLight - The direction to the light in world space.
|
|
99
|
+
* @param originHeight - The height of the origin (distance from planet's surface) in kilometers.
|
|
100
|
+
*/
|
|
101
|
+
update(camera: Camera, planetRadius: number, planetRadiusWithOffset: number, atmosphereRadius: number, directionToLight: IVector3Like, originHeight: number): void;
|
|
102
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// MIT License
|
|
3
|
+
import { Matrix, Vector3, Vector4 } from "@onerjs/core/Maths/math.vector.js";
|
|
4
|
+
import { Ray } from "@onerjs/core/Culling/ray.core.js";
|
|
5
|
+
import { Vector3Dot } from "@onerjs/core/Maths/math.vector.functions.js";
|
|
6
|
+
const TempRay = new Ray(Vector3.Zero(), Vector3.Zero());
|
|
7
|
+
/**
|
|
8
|
+
* Variables that are used to render the atmosphere and are computed per-camera.
|
|
9
|
+
*/
|
|
10
|
+
export class AtmospherePerCameraVariables {
|
|
11
|
+
constructor() {
|
|
12
|
+
this._inverseViewProjectionMatrixWithoutTranslation = Matrix.Identity();
|
|
13
|
+
this._directionToLightRelativeToCameraGeocentricNormal = Vector3.Up();
|
|
14
|
+
this._cosAngleLightToZenith = 0;
|
|
15
|
+
this._cameraRadius = 0;
|
|
16
|
+
this._clampedCameraRadius = 0;
|
|
17
|
+
this._cameraHeight = 0;
|
|
18
|
+
this._clampedCameraHeight = 0;
|
|
19
|
+
this._cameraPositionGlobal = new Vector3();
|
|
20
|
+
this._clampedCameraPositionGlobal = new Vector3();
|
|
21
|
+
this._cosCameraHorizonAngleFromZenith = 0;
|
|
22
|
+
this._sinCameraAtmosphereHorizonAngleFromNadir = 0;
|
|
23
|
+
this._cameraGeocentricNormal = Vector3.Up();
|
|
24
|
+
this._cameraForward = Vector3.Down();
|
|
25
|
+
this._cameraNearPlane = 0;
|
|
26
|
+
this._cameraPosition = new Vector3();
|
|
27
|
+
this._viewport = new Vector4();
|
|
28
|
+
this._lastViewMatrix = Matrix.Identity();
|
|
29
|
+
this._lastProjectionMatrix = Matrix.Identity();
|
|
30
|
+
this._inverseViewMatrixWithoutTranslation = Matrix.Identity();
|
|
31
|
+
this._inverseProjectionMatrix = Matrix.Identity();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The inverse view projection matrix is used to unproject rays.
|
|
35
|
+
* To avoid precision issues, the translation part of the matrix has been removed.
|
|
36
|
+
*/
|
|
37
|
+
get inverseViewProjectionMatrixWithoutTranslation() {
|
|
38
|
+
return this._inverseViewProjectionMatrixWithoutTranslation;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The direction to the light relative to the geocentric normal under the camera.
|
|
42
|
+
*/
|
|
43
|
+
get directionToLightRelativeToCameraGeocentricNormal() {
|
|
44
|
+
return this._directionToLightRelativeToCameraGeocentricNormal;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The cosine of the angle between the light direction and zenith.
|
|
48
|
+
*/
|
|
49
|
+
get cosAngleLightToZenith() {
|
|
50
|
+
return this._cosAngleLightToZenith;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The distance from the camera to the planet origin in kilometers.
|
|
54
|
+
*/
|
|
55
|
+
get cameraRadius() {
|
|
56
|
+
return this._cameraRadius;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The distance from the camera to the planet origin, clamped to the planet radius offset, in kilometers.
|
|
60
|
+
*/
|
|
61
|
+
get clampedCameraRadius() {
|
|
62
|
+
return this._clampedCameraRadius;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The height of the camera above the planet surface in kilometers.
|
|
66
|
+
*/
|
|
67
|
+
get cameraHeight() {
|
|
68
|
+
return this._cameraHeight;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The height of the camera above the planet surface, clamped to the planet radius offset, in kilometers.
|
|
72
|
+
*/
|
|
73
|
+
get clampedCameraHeight() {
|
|
74
|
+
return this._clampedCameraHeight;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The camera position in global space kilometers.
|
|
78
|
+
*/
|
|
79
|
+
get cameraPositionGlobal() {
|
|
80
|
+
return this._cameraPositionGlobal;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The camera position, clamped to the planet radius offset, in global space kilometers.
|
|
84
|
+
*/
|
|
85
|
+
get clampedCameraPositionGlobal() {
|
|
86
|
+
return this._clampedCameraPositionGlobal;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The cosine of the angle from the zenith to the horizon of the planet, measured from the camera position.
|
|
90
|
+
*/
|
|
91
|
+
get cosCameraHorizonAngleFromZenith() {
|
|
92
|
+
return this._cosCameraHorizonAngleFromZenith;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The sine of the angle from the nadir to the horizon of the atmosphere, measured from the camera position.
|
|
96
|
+
*/
|
|
97
|
+
get sinCameraAtmosphereHorizonAngleFromNadir() {
|
|
98
|
+
return this._sinCameraAtmosphereHorizonAngleFromNadir;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The geocentric normal of the camera in global space i.e., the normalization of {@link cameraPositionGlobal}.
|
|
102
|
+
*/
|
|
103
|
+
get cameraGeocentricNormal() {
|
|
104
|
+
return this._cameraGeocentricNormal;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The camera's forward direction in world space.
|
|
108
|
+
*/
|
|
109
|
+
get cameraForward() {
|
|
110
|
+
return this._cameraForward;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The distance to the near plane of the camera.
|
|
114
|
+
*/
|
|
115
|
+
get cameraNearPlane() {
|
|
116
|
+
return this._cameraNearPlane;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The camera's position in world space.
|
|
120
|
+
*/
|
|
121
|
+
get cameraPosition() {
|
|
122
|
+
return this._cameraPosition;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The viewport for the camera.
|
|
126
|
+
*/
|
|
127
|
+
get viewport() {
|
|
128
|
+
return this._viewport;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Updates the variables.
|
|
132
|
+
* @param camera - The camera to update the variables for.
|
|
133
|
+
* @param planetRadius - The radius of the planet in kilometers.
|
|
134
|
+
* @param planetRadiusWithOffset - The radius of the planet with the offset in kilometers.
|
|
135
|
+
* @param atmosphereRadius - The radius of the atmosphere in kilometers.
|
|
136
|
+
* @param directionToLight - The direction to the light in world space.
|
|
137
|
+
* @param originHeight - The height of the origin (distance from planet's surface) in kilometers.
|
|
138
|
+
*/
|
|
139
|
+
update(camera, planetRadius, planetRadiusWithOffset, atmosphereRadius, directionToLight, originHeight) {
|
|
140
|
+
this._cameraNearPlane = camera.minZ;
|
|
141
|
+
this._cameraForward.copyFrom(camera.getForwardRayToRef(TempRay, 1).direction);
|
|
142
|
+
const engine = camera.getScene().getEngine();
|
|
143
|
+
this._viewport.copyFromFloats(0.0, 0.0, engine.getRenderWidth(), engine.getRenderHeight());
|
|
144
|
+
// Compute inverse view projection matrix, but remove the translational component to increase precision.
|
|
145
|
+
const viewMatrix = camera.getViewMatrix();
|
|
146
|
+
const projectionMatrix = camera.getProjectionMatrix();
|
|
147
|
+
if (!this._lastViewMatrix.equals(viewMatrix) || !this._lastProjectionMatrix.equals(projectionMatrix)) {
|
|
148
|
+
this._lastViewMatrix.copyFrom(viewMatrix);
|
|
149
|
+
this._lastViewMatrix.setTranslation(Vector3.ZeroReadOnly);
|
|
150
|
+
this._lastViewMatrix.invertToRef(this._inverseViewMatrixWithoutTranslation);
|
|
151
|
+
this._lastProjectionMatrix.copyFrom(projectionMatrix);
|
|
152
|
+
this._lastProjectionMatrix.invertToRef(this._inverseProjectionMatrix);
|
|
153
|
+
this._inverseProjectionMatrix.multiplyToRef(this._inverseViewMatrixWithoutTranslation, this._inverseViewProjectionMatrixWithoutTranslation);
|
|
154
|
+
}
|
|
155
|
+
// Compute the global space position of the camera in kilometers.
|
|
156
|
+
this._cameraPosition.copyFrom(camera.globalPosition);
|
|
157
|
+
this._cameraPosition.scaleToRef(1.0 / 1000.0, this._cameraPositionGlobal);
|
|
158
|
+
this._cameraPositionGlobal.y += planetRadius + originHeight;
|
|
159
|
+
this._cameraHeight = this._cameraPositionGlobal.y - planetRadius;
|
|
160
|
+
// Clamp the camera parameters.
|
|
161
|
+
this._cameraRadius = this._cameraPositionGlobal.length();
|
|
162
|
+
this._clampedCameraRadius = this._cameraRadius;
|
|
163
|
+
this._cameraPositionGlobal.normalizeToRef(this._cameraGeocentricNormal);
|
|
164
|
+
if (this._clampedCameraRadius < planetRadiusWithOffset) {
|
|
165
|
+
this._clampedCameraRadius = planetRadiusWithOffset;
|
|
166
|
+
this._cameraGeocentricNormal.scaleToRef(planetRadiusWithOffset, this._clampedCameraPositionGlobal);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
this._clampedCameraPositionGlobal.copyFrom(this._cameraPositionGlobal);
|
|
170
|
+
}
|
|
171
|
+
this._cosCameraHorizonAngleFromZenith = ComputeCosHorizonAngleFromZenith(planetRadius, this._clampedCameraRadius);
|
|
172
|
+
this._sinCameraAtmosphereHorizonAngleFromNadir = Math.min(1.0, atmosphereRadius / this._clampedCameraRadius);
|
|
173
|
+
this._clampedCameraHeight = this._clampedCameraRadius - planetRadius;
|
|
174
|
+
// Compute the direction to the light relative to the camera's geocentric normal.
|
|
175
|
+
{
|
|
176
|
+
this._cosAngleLightToZenith = Vector3Dot(directionToLight, this._cameraGeocentricNormal);
|
|
177
|
+
const lightZenithSinAngle = Math.sqrt(Math.max(0.0, 1.0 - this._cosAngleLightToZenith * this._cosAngleLightToZenith));
|
|
178
|
+
this._directionToLightRelativeToCameraGeocentricNormal.copyFromFloats(lightZenithSinAngle, this._cosAngleLightToZenith, 0.0);
|
|
179
|
+
this._directionToLightRelativeToCameraGeocentricNormal.normalize();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const ComputeCosHorizonAngleFromZenith = (planetRadius, radius) => {
|
|
184
|
+
const sinHorizonAngleFromNadir = Math.min(1, planetRadius / radius);
|
|
185
|
+
const cosHorizonAngleFromNadir = Math.sqrt(1 - sinHorizonAngleFromNadir * sinHorizonAngleFromNadir);
|
|
186
|
+
const cosHorizonAngleFromZenith = -cosHorizonAngleFromNadir;
|
|
187
|
+
return cosHorizonAngleFromZenith;
|
|
188
|
+
};
|
|
189
|
+
//# sourceMappingURL=atmospherePerCameraVariables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atmospherePerCameraVariables.js","sourceRoot":"","sources":["../../../../dev/addons/src/atmosphere/atmospherePerCameraVariables.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,cAAc;AAId,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,0CAA+B;AAClE,OAAO,EAAE,GAAG,EAAE,yCAA8B;AAC5C,OAAO,EAAE,UAAU,EAAE,oDAAyC;AAE9D,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;AAExD;;GAEG;AACH,MAAM,OAAO,4BA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AAE,GAAG,CAAC,CAAC;YAC7H,IAAI,CAAC,iDAAiD,CAAC,SAAS,EAAE,CAAC;QACvE,CAAC;IACL,CAAC;CACJ;AAED,MAAM,gCAAgC,GAAG,CAAC,YAAoB,EAAE,MAAc,EAAU,EAAE;IACtF,MAAM,wBAAwB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC,CAAC;IACpE,MAAM,wBAAwB,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,wBAAwB,GAAG,wBAAwB,CAAC,CAAC;IACpG,MAAM,yBAAyB,GAAG,CAAC,wBAAwB,CAAC;IAC5D,OAAO,yBAAyB,CAAC;AACrC,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// MIT License\r\n\r\nimport type { Camera } from \"core/Cameras/camera\";\r\nimport type { IMatrixLike, IVector3Like, IVector4Like } from \"core/Maths/math.like\";\r\nimport { Matrix, Vector3, Vector4 } from \"core/Maths/math.vector\";\r\nimport { Ray } from \"core/Culling/ray.core\";\r\nimport { Vector3Dot } from \"core/Maths/math.vector.functions\";\r\n\r\nconst TempRay = new Ray(Vector3.Zero(), Vector3.Zero());\r\n\r\n/**\r\n * Variables that are used to render the atmosphere and are computed per-camera.\r\n */\r\nexport class AtmospherePerCameraVariables {\r\n private _inverseViewProjectionMatrixWithoutTranslation = Matrix.Identity();\r\n private _directionToLightRelativeToCameraGeocentricNormal = Vector3.Up();\r\n private _cosAngleLightToZenith = 0;\r\n private _cameraRadius = 0;\r\n private _clampedCameraRadius = 0;\r\n private _cameraHeight = 0;\r\n private _clampedCameraHeight = 0;\r\n private _cameraPositionGlobal = new Vector3();\r\n private _clampedCameraPositionGlobal = new Vector3();\r\n private _cosCameraHorizonAngleFromZenith = 0;\r\n private _sinCameraAtmosphereHorizonAngleFromNadir = 0;\r\n private _cameraGeocentricNormal = Vector3.Up();\r\n private _cameraForward = Vector3.Down();\r\n private _cameraNearPlane = 0;\r\n private _cameraPosition = new Vector3();\r\n private _viewport = new Vector4();\r\n private _lastViewMatrix = Matrix.Identity();\r\n private _lastProjectionMatrix = Matrix.Identity();\r\n private _inverseViewMatrixWithoutTranslation = Matrix.Identity();\r\n private _inverseProjectionMatrix = Matrix.Identity();\r\n\r\n /**\r\n * The inverse view projection matrix is used to unproject rays.\r\n * To avoid precision issues, the translation part of the matrix has been removed.\r\n */\r\n public get inverseViewProjectionMatrixWithoutTranslation(): IMatrixLike {\r\n return this._inverseViewProjectionMatrixWithoutTranslation;\r\n }\r\n\r\n /**\r\n * The direction to the light relative to the geocentric normal under the camera.\r\n */\r\n public get directionToLightRelativeToCameraGeocentricNormal(): IVector3Like {\r\n return this._directionToLightRelativeToCameraGeocentricNormal;\r\n }\r\n\r\n /**\r\n * The cosine of the angle between the light direction and zenith.\r\n */\r\n public get cosAngleLightToZenith(): number {\r\n return this._cosAngleLightToZenith;\r\n }\r\n\r\n /**\r\n * The distance from the camera to the planet origin in kilometers.\r\n */\r\n public get cameraRadius(): number {\r\n return this._cameraRadius;\r\n }\r\n\r\n /**\r\n * The distance from the camera to the planet origin, clamped to the planet radius offset, in kilometers.\r\n */\r\n public get clampedCameraRadius(): number {\r\n return this._clampedCameraRadius;\r\n }\r\n\r\n /**\r\n * The height of the camera above the planet surface in kilometers.\r\n */\r\n public get cameraHeight(): number {\r\n return this._cameraHeight;\r\n }\r\n\r\n /**\r\n * The height of the camera above the planet surface, clamped to the planet radius offset, in kilometers.\r\n */\r\n public get clampedCameraHeight(): number {\r\n return this._clampedCameraHeight;\r\n }\r\n\r\n /**\r\n * The camera position in global space kilometers.\r\n */\r\n public get cameraPositionGlobal(): IVector3Like {\r\n return this._cameraPositionGlobal;\r\n }\r\n\r\n /**\r\n * The camera position, clamped to the planet radius offset, in global space kilometers.\r\n */\r\n public get clampedCameraPositionGlobal(): IVector3Like {\r\n return this._clampedCameraPositionGlobal;\r\n }\r\n\r\n /**\r\n * The cosine of the angle from the zenith to the horizon of the planet, measured from the camera position.\r\n */\r\n public get cosCameraHorizonAngleFromZenith(): number {\r\n return this._cosCameraHorizonAngleFromZenith;\r\n }\r\n\r\n /**\r\n * The sine of the angle from the nadir to the horizon of the atmosphere, measured from the camera position.\r\n */\r\n public get sinCameraAtmosphereHorizonAngleFromNadir(): number {\r\n return this._sinCameraAtmosphereHorizonAngleFromNadir;\r\n }\r\n\r\n /**\r\n * The geocentric normal of the camera in global space i.e., the normalization of {@link cameraPositionGlobal}.\r\n */\r\n public get cameraGeocentricNormal(): IVector3Like {\r\n return this._cameraGeocentricNormal;\r\n }\r\n\r\n /**\r\n * The camera's forward direction in world space.\r\n */\r\n public get cameraForward(): IVector3Like {\r\n return this._cameraForward;\r\n }\r\n\r\n /**\r\n * The distance to the near plane of the camera.\r\n */\r\n public get cameraNearPlane(): number {\r\n return this._cameraNearPlane;\r\n }\r\n\r\n /**\r\n * The camera's position in world space.\r\n */\r\n public get cameraPosition(): IVector3Like {\r\n return this._cameraPosition;\r\n }\r\n\r\n /**\r\n * The viewport for the camera.\r\n */\r\n public get viewport(): IVector4Like {\r\n return this._viewport;\r\n }\r\n\r\n /**\r\n * Updates the variables.\r\n * @param camera - The camera to update the variables for.\r\n * @param planetRadius - The radius of the planet in kilometers.\r\n * @param planetRadiusWithOffset - The radius of the planet with the offset in kilometers.\r\n * @param atmosphereRadius - The radius of the atmosphere in kilometers.\r\n * @param directionToLight - The direction to the light in world space.\r\n * @param originHeight - The height of the origin (distance from planet's surface) in kilometers.\r\n */\r\n public update(camera: Camera, planetRadius: number, planetRadiusWithOffset: number, atmosphereRadius: number, directionToLight: IVector3Like, originHeight: number): void {\r\n this._cameraNearPlane = camera.minZ;\r\n this._cameraForward.copyFrom(camera.getForwardRayToRef(TempRay, 1).direction);\r\n\r\n const engine = camera.getScene().getEngine();\r\n this._viewport.copyFromFloats(0.0, 0.0, engine.getRenderWidth(), engine.getRenderHeight());\r\n\r\n // Compute inverse view projection matrix, but remove the translational component to increase precision.\r\n const viewMatrix = camera.getViewMatrix();\r\n const projectionMatrix = camera.getProjectionMatrix();\r\n if (!this._lastViewMatrix.equals(viewMatrix) || !this._lastProjectionMatrix.equals(projectionMatrix)) {\r\n this._lastViewMatrix.copyFrom(viewMatrix);\r\n this._lastViewMatrix.setTranslation(Vector3.ZeroReadOnly);\r\n this._lastViewMatrix.invertToRef(this._inverseViewMatrixWithoutTranslation);\r\n\r\n this._lastProjectionMatrix.copyFrom(projectionMatrix);\r\n this._lastProjectionMatrix.invertToRef(this._inverseProjectionMatrix);\r\n this._inverseProjectionMatrix.multiplyToRef(this._inverseViewMatrixWithoutTranslation, this._inverseViewProjectionMatrixWithoutTranslation);\r\n }\r\n\r\n // Compute the global space position of the camera in kilometers.\r\n this._cameraPosition.copyFrom(camera.globalPosition);\r\n this._cameraPosition.scaleToRef(1.0 / 1000.0, this._cameraPositionGlobal);\r\n this._cameraPositionGlobal.y += planetRadius + originHeight;\r\n this._cameraHeight = this._cameraPositionGlobal.y - planetRadius;\r\n\r\n // Clamp the camera parameters.\r\n this._cameraRadius = this._cameraPositionGlobal.length();\r\n this._clampedCameraRadius = this._cameraRadius;\r\n this._cameraPositionGlobal.normalizeToRef(this._cameraGeocentricNormal);\r\n if (this._clampedCameraRadius < planetRadiusWithOffset) {\r\n this._clampedCameraRadius = planetRadiusWithOffset;\r\n this._cameraGeocentricNormal.scaleToRef(planetRadiusWithOffset, this._clampedCameraPositionGlobal);\r\n } else {\r\n this._clampedCameraPositionGlobal.copyFrom(this._cameraPositionGlobal);\r\n }\r\n\r\n this._cosCameraHorizonAngleFromZenith = ComputeCosHorizonAngleFromZenith(planetRadius, this._clampedCameraRadius);\r\n this._sinCameraAtmosphereHorizonAngleFromNadir = Math.min(1.0, atmosphereRadius / this._clampedCameraRadius);\r\n this._clampedCameraHeight = this._clampedCameraRadius - planetRadius;\r\n\r\n // Compute the direction to the light relative to the camera's geocentric normal.\r\n {\r\n this._cosAngleLightToZenith = Vector3Dot(directionToLight, this._cameraGeocentricNormal);\r\n const lightZenithSinAngle = Math.sqrt(Math.max(0.0, 1.0 - this._cosAngleLightToZenith * this._cosAngleLightToZenith));\r\n this._directionToLightRelativeToCameraGeocentricNormal.copyFromFloats(lightZenithSinAngle, this._cosAngleLightToZenith, 0.0);\r\n this._directionToLightRelativeToCameraGeocentricNormal.normalize();\r\n }\r\n }\r\n}\r\n\r\nconst ComputeCosHorizonAngleFromZenith = (planetRadius: number, radius: number): number => {\r\n const sinHorizonAngleFromNadir = Math.min(1, planetRadius / radius);\r\n const cosHorizonAngleFromNadir = Math.sqrt(1 - sinHorizonAngleFromNadir * sinHorizonAngleFromNadir);\r\n const cosHorizonAngleFromZenith = -cosHorizonAngleFromNadir;\r\n return cosHorizonAngleFromZenith;\r\n};\r\n"]}
|