Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): support IBL for terrain (#529)
- Loading branch information
Showing
20 changed files
with
618 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
57 changes: 57 additions & 0 deletions
57
web/src/beta/lib/core/engines/Cesium/Shaders/OverriddenShaders/GlobeFS/Customs.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This file refers this implementation: | ||
// https://github.com/takram-design-engineering/plateau-view/blob/8ea8bf1d5ef64319d92d0eb05b936cca7f1a2e8f/libs/cesium/src/shaders/imageBasedLightingStage.glsl | ||
|
||
// Derived from: | ||
// https://github.com/CesiumGS/cesium/blob/1.106/packages/engine/Source/Shaders/Model/ImageBasedLightingStageFS.glsl | ||
// Specular term was removed, as I never apply it on terrain. | ||
vec3 reearth_imageBasedLightingStage(vec3 positionEC, vec3 normalEC, | ||
vec3 lightDirectionEC, vec3 lightColorHdr, | ||
czm_pbrParameters pbrParameters) { | ||
vec3 v = -positionEC; | ||
vec3 n = normalEC; | ||
vec3 l = normalize(lightDirectionEC); | ||
vec3 h = normalize(v + l); | ||
float NdotV = abs(dot(n, v)) + 0.001; | ||
float VdotH = clamp(dot(v, h), 0.0, 1.0); | ||
|
||
const mat3 yUpToZUp = mat3(-1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0); | ||
// Reference frame matrix can be computed only by world position and normal. | ||
mat3 referenceFrameMatrix = | ||
czm_transpose(czm_eastNorthUpToEyeCoordinates(v_positionMC, normalEC)); | ||
vec3 cubeDir = | ||
normalize(yUpToZUp * referenceFrameMatrix * normalize(reflect(-v, n))); | ||
vec3 diffuseIrradiance = | ||
czm_sphericalHarmonics(cubeDir, u_reearth_sphericalHarmonicCoefficients); | ||
|
||
return pbrParameters.diffuseColor * diffuseIrradiance; | ||
} | ||
|
||
vec4 reearth_computeImageBasedLightingColor(vec4 color) { | ||
if (u_reearth_globeImageBasedLighting) { | ||
czm_pbrParameters pbrParameters; | ||
pbrParameters.diffuseColor = color.rgb; | ||
|
||
vec3 normalEC = normalize(v_normalEC); | ||
vec3 lighting = | ||
czm_pbrLighting(v_positionEC, normalEC, czm_lightDirectionEC, | ||
czm_lightColorHdr, pbrParameters); | ||
lighting += reearth_imageBasedLightingStage( | ||
v_positionEC, normalEC, czm_lightDirectionEC, | ||
czm_lightColorHdr, pbrParameters) * | ||
u_vertexShadowDarkness; | ||
|
||
#ifdef HDR | ||
lighting = czm_acesTonemapping(lighting); | ||
lighting = czm_linearToSrgb(lighting); | ||
#endif | ||
|
||
return vec4(color.rgb * lighting, color.a); | ||
} else { | ||
float diffuseIntensity = clamp( | ||
czm_getLambertDiffuse(czm_lightDirectionEC, normalize(v_normalEC)) * | ||
u_lambertDiffuseMultiplier + | ||
u_vertexShadowDarkness, | ||
0.0, 1.0); | ||
return vec4(color.rgb * czm_lightColor * diffuseIntensity, color.a); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
web/src/beta/lib/core/engines/Cesium/Shaders/OverriddenShaders/GlobeFS/Definitions.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
uniform vec3 u_reearth_sphericalHarmonicCoefficients[9]; | ||
uniform bool u_reearth_globeImageBasedLighting; | ||
|
||
vec4 reearth_computeImageBasedLightingColor(vec4 color); |
13 changes: 13 additions & 0 deletions
13
web/src/beta/lib/core/engines/Cesium/Shaders/OverriddenShaders/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# OverriddenShaders | ||
|
||
This is a set of shaders to override Cesium's shader. | ||
|
||
The structure have to be as follow. | ||
|
||
``` | ||
ShaderName/ | ||
Customs.glsl ... Define your own custom functions | ||
Definitions.glsl ... Definition for each variable or function. | ||
``` | ||
|
||
You must set `reearth_` prefix to the name of the function like `reearth_own_function()`. Also you must set `reearth_` to the name of the uniform and attribute like `u_reearth_variable` or `v_reearth_variable`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
web/src/beta/lib/core/engines/Cesium/helpers/sphericalHaromic.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Cartesian3 } from "cesium"; | ||
import { expect, test } from "vitest"; | ||
|
||
import { arrayToCartecian3 } from "./sphericalHaromic"; | ||
|
||
test("arrayToCartecian3", () => { | ||
expect( | ||
arrayToCartecian3( | ||
[ | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
], | ||
2, | ||
), | ||
).toEqual([ | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
new Cartesian3(2, 2, 2), | ||
]); | ||
}); |
27 changes: 27 additions & 0 deletions
27
web/src/beta/lib/core/engines/Cesium/helpers/sphericalHaromic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Cartesian3 } from "cesium"; | ||
|
||
const sphericalHarmonicCoefficientsScratch = [ | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
new Cartesian3(), | ||
]; | ||
|
||
export const arrayToCartecian3 = ( | ||
sphericalHarmonicCoefficients: [x: number, y: number, z: number][] | undefined, | ||
intensity: number | undefined, | ||
) => | ||
sphericalHarmonicCoefficients?.length === 9 | ||
? sphericalHarmonicCoefficients.map((cartesian, index) => | ||
Cartesian3.multiplyByScalar( | ||
new Cartesian3(...cartesian), | ||
intensity ?? 1.0, | ||
sphericalHarmonicCoefficientsScratch[index], | ||
), | ||
) | ||
: undefined; |
Oops, something went wrong.