Skip to content

Commit 1baec1d

Browse files
authored
Merge pull request #12082 from CesiumGS/ibl-diffuse
Correct diffuse component for image-based lighting
2 parents 7b93161 + 3ffd571 commit 1baec1d

File tree

6 files changed

+77
-76
lines changed

6 files changed

+77
-76
lines changed

Apps/Sandcastle/gallery/Image-Based Lighting.html

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,63 +65,63 @@
6565
);
6666
}
6767

68-
const environmentMapURL =
69-
"https://cesium.com/public/SandcastleSampleData/kiara_6_afternoon_2k_ibl.ktx2";
7068
const modelURL = "../../SampleData/models/Pawns/Pawns.glb";
7169

7270
// This environment map was processed using Khronos's glTF IBL Sampler. To process your own:
7371
// 1 - Download and build the Khronos glTF IBL Sampler (https://github.com/KhronosGroup/glTF-IBL-Sampler).
7472
// 2 - Run `cli -inputPath /path/to/image.hdr -outCubeMap /path/to/output.ktx2`. Run `cli -h` for all options.
73+
const environmentMapURL =
74+
"https://cesium.com/public/SandcastleSampleData/kiara_6_afternoon_2k_ibl.ktx2";
7575

7676
// To generate the spherical harmonic coefficients below, use Google's Filament project:
7777
// 1 - Download the Filament release (https://github.com/google/filament/releases).
78-
// 2 - Run `cmgen --type=ktx --deploy=/path/to/output /path/to/image.hdr`. Other formats are also supported. Run `cmgen --help` for all options.
78+
// 2 - Run `cmgen --no-mirror --type=ktx --deploy=/path/to/output /path/to/image.hdr`.
79+
// Other formats are also supported. Run `cmgen --help` for all options.
7980
// 3 - Take the generated coefficients and load them in CesiumJS as shown below.
80-
8181
const L00 = new Cesium.Cartesian3(
82-
1.234709620475769,
83-
1.221461296081543,
84-
1.273156881332397
82+
1.234897375106812,
83+
1.221635103225708,
84+
1.273374080657959
8585
);
8686
const L1_1 = new Cesium.Cartesian3(
87-
1.135921120643616,
88-
1.171217799186707,
89-
1.287644743919373
87+
1.136140108108521,
88+
1.171419978141785,
89+
1.287894368171692
9090
);
9191
const L10 = new Cesium.Cartesian3(
92-
1.245193719863892,
93-
1.245591878890991,
94-
1.282818794250488
92+
1.245410919189453,
93+
1.245791077613831,
94+
1.283067107200623
9595
);
9696
const L11 = new Cesium.Cartesian3(
97-
-1.106930732727051,
98-
-1.112522482872009,
99-
-1.153198838233948
97+
1.107124328613281,
98+
1.112697005271912,
99+
1.153419137001038
100100
);
101101
const L2_2 = new Cesium.Cartesian3(
102-
-1.086226940155029,
103-
-1.079731941223145,
104-
-1.101912498474121
102+
1.08641505241394,
103+
1.079904079437256,
104+
1.10212504863739
105105
);
106106
const L2_1 = new Cesium.Cartesian3(
107-
1.189834713935852,
108-
1.185906887054443,
109-
1.214385271072388
107+
1.190043210983276,
108+
1.186099290847778,
109+
1.214627981185913
110110
);
111111
const L20 = new Cesium.Cartesian3(
112-
0.01778045296669,
113-
0.02013735473156,
114-
0.025313569232821
112+
0.017783647403121,
113+
0.020140396431088,
114+
0.025317270308733
115115
);
116116
const L21 = new Cesium.Cartesian3(
117-
-1.086826920509338,
118-
-1.084611177444458,
119-
-1.111204028129578
117+
1.087014317512512,
118+
1.084779262542725,
119+
1.111417651176453
120120
);
121121
const L22 = new Cesium.Cartesian3(
122-
-0.05241484940052,
123-
-0.048303380608559,
124-
-0.041960217058659
122+
-0.052426788955927,
123+
-0.048315055668354,
124+
-0.041973855346441
125125
);
126126
const coefficients = [L00, L1_1, L10, L11, L2_2, L2_1, L20, L21, L22];
127127

Apps/Sandcastle/gallery/glTF PBR Extensions.html

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,52 +79,53 @@
7979

8080
// To generate the spherical harmonic coefficients below, use Google's Filament project:
8181
// 1 - Download the Filament release (https://github.com/google/filament/releases).
82-
// 2 - Run `cmgen --type=ktx --deploy=/path/to/output /path/to/image.hdr`. Other formats are also supported. Run `cmgen --help` for all options.
82+
// 2 - Run `cmgen --no-mirror --type=ktx --deploy=/path/to/output /path/to/image.hdr`.
83+
// Other formats are also supported. Run `cmgen --help` for all options.
8384
// 3 - Take the generated coefficients and load them in CesiumJS as shown below.
8485
const L00 = new Cesium.Cartesian3(
85-
1.234709620475769,
86-
1.221461296081543,
87-
1.273156881332397
86+
1.234897375106812,
87+
1.221635103225708,
88+
1.273374080657959
8889
);
8990
const L1_1 = new Cesium.Cartesian3(
90-
1.135921120643616,
91-
1.171217799186707,
92-
1.287644743919373
91+
1.136140108108521,
92+
1.171419978141785,
93+
1.287894368171692
9394
);
9495
const L10 = new Cesium.Cartesian3(
95-
1.245193719863892,
96-
1.245591878890991,
97-
1.282818794250488
96+
1.245410919189453,
97+
1.245791077613831,
98+
1.283067107200623
9899
);
99100
const L11 = new Cesium.Cartesian3(
100-
-1.106930732727051,
101-
-1.112522482872009,
102-
-1.153198838233948
101+
1.107124328613281,
102+
1.112697005271912,
103+
1.153419137001038
103104
);
104105
const L2_2 = new Cesium.Cartesian3(
105-
-1.086226940155029,
106-
-1.079731941223145,
107-
-1.101912498474121
106+
1.08641505241394,
107+
1.079904079437256,
108+
1.10212504863739
108109
);
109110
const L2_1 = new Cesium.Cartesian3(
110-
1.189834713935852,
111-
1.185906887054443,
112-
1.214385271072388
111+
1.190043210983276,
112+
1.186099290847778,
113+
1.214627981185913
113114
);
114115
const L20 = new Cesium.Cartesian3(
115-
0.01778045296669,
116-
0.02013735473156,
117-
0.025313569232821
116+
0.017783647403121,
117+
0.020140396431088,
118+
0.025317270308733
118119
);
119120
const L21 = new Cesium.Cartesian3(
120-
-1.086826920509338,
121-
-1.084611177444458,
122-
-1.111204028129578
121+
1.087014317512512,
122+
1.084779262542725,
123+
1.111417651176453
123124
);
124125
const L22 = new Cesium.Cartesian3(
125-
-0.05241484940052,
126-
-0.048303380608559,
127-
-0.041960217058659
126+
-0.052426788955927,
127+
-0.048315055668354,
128+
-0.041973855346441
128129
);
129130
const coefficients = [L00, L1_1, L10, L11, L2_2, L2_1, L20, L21, L22];
130131

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- Updated geometric self-shadowing function to improve direct lighting on models using physically-based rendering. [#12063](https://github.com/CesiumGS/cesium/pull/12063)
1010
- Fixed environment map LOD selection in image-based lighting. [#12070](https://github.com/CesiumGS/cesium/pull/12070)
11+
- Corrected calculation of diffuse component in image-based lighting. [#12082](https://github.com/CesiumGS/cesium/pull/12082)
1112

1213
### 1.119 - 2024-07-01
1314

packages/engine/Source/Scene/ImageBasedLighting.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ Object.defineProperties(ImageBasedLighting.prototype, {
186186
* </p>
187187
*
188188
* These values can be obtained by preprocessing the environment map using the <code>cmgen</code> tool of
189-
* {@link https://github.com/google/filament/releases|Google's Filament project}. This will also generate a KTX file that can be
190-
* supplied to {@link Model#specularEnvironmentMaps}.
189+
* {@link https://github.com/google/filament/releases|Google's Filament project}.
190+
* Be sure to use the <code>--no-mirror</code> option in <code>cmgen</code>.
191191
*
192192
* @memberof ImageBasedLighting.prototype
193193
*

packages/engine/Source/Shaders/Model/ImageBasedLightingStageFS.glsl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ float getSunLuminance(vec3 positionWC, vec3 normalEC, vec3 lightDirectionEC)
121121
) {
122122
vec3 viewDirectionEC = -normalize(positionEC);
123123
vec3 positionWC = vec3(czm_inverseView * vec4(positionEC, 1.0));
124-
vec3 reflectionWC = normalize(czm_inverseViewRotation * normalize(reflect(viewDirectionEC, normalEC)));
124+
vec3 reflectionWC = normalize(czm_inverseViewRotation * reflect(viewDirectionEC, normalEC));
125125
vec3 skyMetrics = getProceduralSkyMetrics(positionWC, reflectionWC);
126126

127127
float roughness = material.roughness;
@@ -202,34 +202,33 @@ vec3 textureIBL(
202202
vec3 lightDirectionEC,
203203
czm_modelMaterial material
204204
) {
205-
// Find the direction in which to sample the environment map
206-
vec3 cubeDir = normalize(model_iblReferenceFrameMatrix * normalize(reflect(-viewDirectionEC, normalEC)));
207-
208205
#ifdef DIFFUSE_IBL
209-
vec3 diffuseContribution = computeDiffuseIBL(cubeDir) * material.diffuse;
206+
vec3 normalMC = normalize(model_iblReferenceFrameMatrix * normalEC);
207+
vec3 diffuseContribution = computeDiffuseIBL(normalMC) * material.diffuse;
210208
#else
211209
vec3 diffuseContribution = vec3(0.0);
212210
#endif
213211

214-
float roughness = material.roughness;
215-
216212
#ifdef USE_ANISOTROPY
217-
// Update environment map sampling direction to account for anisotropic distortion of specular reflection
213+
// Bend normal to account for anisotropic distortion of specular reflection
218214
vec3 anisotropyDirection = material.anisotropicB;
219215
vec3 anisotropicTangent = cross(anisotropyDirection, viewDirectionEC);
220216
vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection);
221-
float bendFactor = 1.0 - material.anisotropyStrength * (1.0 - roughness);
217+
float bendFactor = 1.0 - material.anisotropyStrength * (1.0 - material.roughness);
222218
float bendFactorPow4 = bendFactor * bendFactor * bendFactor * bendFactor;
223219
vec3 bentNormal = normalize(mix(anisotropicNormal, normalEC, bendFactorPow4));
224-
cubeDir = normalize(model_iblReferenceFrameMatrix * normalize(reflect(-viewDirectionEC, bentNormal)));
220+
vec3 reflectEC = reflect(-viewDirectionEC, bentNormal);
221+
#else
222+
vec3 reflectEC = reflect(-viewDirectionEC, normalEC);
225223
#endif
226224

227225
#ifdef SPECULAR_IBL
226+
vec3 reflectMC = normalize(model_iblReferenceFrameMatrix * reflectEC);
228227
float NdotV = abs(dot(normalEC, viewDirectionEC)) + 0.001;
229228
vec3 halfwayDirectionEC = normalize(viewDirectionEC + lightDirectionEC);
230229
float VdotH = clamp(dot(viewDirectionEC, halfwayDirectionEC), 0.0, 1.0);
231230
vec3 f0 = material.specular;
232-
vec3 specularContribution = computeSpecularIBL(cubeDir, NdotV, VdotH, f0, roughness);
231+
vec3 specularContribution = computeSpecularIBL(reflectMC, NdotV, VdotH, f0, material.roughness);
233232
#else
234233
vec3 specularContribution = vec3(0.0);
235234
#endif

packages/engine/Source/Shaders/Model/LightingStageFS.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ vec3 addClearcoatReflection(vec3 baseLayerColor, vec3 position, vec3 lightDirect
4040

4141
#ifdef SPECULAR_IBL
4242
// Find the direction in which to sample the environment map
43-
vec3 cubeDir = normalize(model_iblReferenceFrameMatrix * normalize(reflect(-viewDirection, normal)));
44-
vec3 iblColor = computeSpecularIBL(cubeDir, NdotV, NdotV, f0, roughness);
43+
vec3 reflectMC = normalize(model_iblReferenceFrameMatrix * reflect(-viewDirection, normal));
44+
vec3 iblColor = computeSpecularIBL(reflectMC, NdotV, NdotV, f0, roughness);
4545
color += iblColor * material.occlusion;
4646
#elif defined(USE_IBL_LIGHTING)
4747
vec3 positionWC = vec3(czm_inverseView * vec4(position, 1.0));
48-
vec3 reflectionWC = normalize(czm_inverseViewRotation * normalize(reflect(viewDirection, normal)));
48+
vec3 reflectionWC = normalize(czm_inverseViewRotation * reflect(viewDirection, normal));
4949
vec3 skyMetrics = getProceduralSkyMetrics(positionWC, reflectionWC);
5050

5151
vec3 specularIrradiance = getProceduralSpecularIrradiance(reflectionWC, skyMetrics, roughness);
@@ -57,7 +57,7 @@ vec3 addClearcoatReflection(vec3 baseLayerColor, vec3 position, vec3 lightDirect
5757
#endif
5858
float maximumComponent = czm_maximumComponent(lightColorHdr);
5959
vec3 clampedLightColor = lightColorHdr / max(maximumComponent, 1.0);
60-
color += clampedLightColor* iblColor * material.occlusion;
60+
color += clampedLightColor * iblColor * material.occlusion;
6161
#endif
6262

6363
float clearcoatFactor = material.clearcoatFactor;

0 commit comments

Comments
 (0)