-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add water mask material #12149
Merged
Merged
Add water mask material #12149
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
Apps/Sandcastle/gallery/Globe Materials – Water Mask Elevation Map.html
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,144 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" | ||
/> | ||
<meta name="description" content="Apply materials to the globe." /> | ||
<meta name="cesium-sandcastle-labels" content="Showcases" /> | ||
<title>Cesium Demo</title> | ||
<script type="text/javascript" src="../Sandcastle-header.js"></script> | ||
<script | ||
type="text/javascript" | ||
src="../../../Build/CesiumUnminified/Cesium.js" | ||
nomodule | ||
></script> | ||
<script type="module" src="../load-cesium-es6.js"></script> | ||
</head> | ||
<body | ||
class="sandcastle-loading" | ||
data-sandcastle-bucket="bucket-requirejs.html" | ||
> | ||
<style> | ||
@import url(../templates/bucket.css); | ||
</style> | ||
<div id="cesiumContainer" class="fullSize"></div> | ||
<div id="loadingOverlay"><h1>Loading...</h1></div> | ||
<div id="toolbar"> | ||
<div id="zoomButtons"></div> | ||
</div> | ||
<script id="cesium_sandcastle_script"> | ||
window.startup = async function (Cesium) { | ||
"use strict"; | ||
//Sandcastle_Begin | ||
const viewer = new Cesium.Viewer("cesiumContainer", { | ||
terrain: Cesium.Terrain.fromWorldTerrain({ | ||
requestVertexNormals: true, // Needed for hillshading | ||
requestWaterMask: true, // Needed to distinguish land from water | ||
}), | ||
timeline: false, | ||
animation: false, | ||
}); | ||
|
||
// Create a globe material for shading elevation only on land | ||
const customElevationMaterial = new Cesium.Material({ | ||
fabric: { | ||
type: "ElevationLand", | ||
materials: { | ||
waterMaskMaterial: { | ||
type: "WaterMask", | ||
}, | ||
elevationRampMaterial: { | ||
type: "ElevationRamp", | ||
}, | ||
}, | ||
components: { | ||
diffuse: "elevationRampMaterial.diffuse", | ||
alpha: "1.0 - waterMaskMaterial.alpha", // We'll need the inverse of the watermask to shade land | ||
}, | ||
}, | ||
translucent: false, | ||
}); | ||
|
||
const minHeight = -414.0; // approximate dead sea elevation | ||
const maxHeight = 8777.0; // approximate everest elevation | ||
const elevationRamp = [0.0, 0.045, 0.45, 0.5, 0.55, 1.0]; | ||
function getColorRamp() { | ||
const ramp = document.createElement("canvas"); | ||
ramp.width = 100; | ||
ramp.height = 1; | ||
const ctx = ramp.getContext("2d"); | ||
|
||
const values = elevationRamp; | ||
|
||
const grd = ctx.createLinearGradient(0, 0, 100, 0); | ||
|
||
// See https://gis.stackexchange.com/questions/25099/choosing-colour-ramp-to-use-for-elevation | ||
grd.addColorStop(values[0], "#344f31"); | ||
grd.addColorStop(values[1], "#5b8742"); | ||
grd.addColorStop(values[2], "#e6daa5"); | ||
grd.addColorStop(values[3], "#fdc771"); | ||
grd.addColorStop(values[4], "#b99d89"); | ||
grd.addColorStop(values[5], "#f0f0f0"); | ||
|
||
ctx.fillStyle = grd; | ||
ctx.fillRect(0, 0, 100, 1); | ||
|
||
return ramp; | ||
} | ||
|
||
const globe = viewer.scene.globe; | ||
const material = customElevationMaterial; | ||
const shadingUniforms = | ||
material.materials.elevationRampMaterial.uniforms; | ||
shadingUniforms.minimumHeight = minHeight; | ||
shadingUniforms.maximumHeight = maxHeight; | ||
shadingUniforms.image = getColorRamp(); | ||
|
||
globe.material = material; | ||
globe.showWaterEffect = false; | ||
globe.enableLighting = true; | ||
globe.maximumScreenSpaceError = 0.5; // Load higher resolution tiles for more detailed shading | ||
|
||
// Light the scene with a hillshade effect similar to https://pro.arcgis.com/en/pro-app/latest/tool-reference/3d-analyst/how-hillshade-works.htm | ||
const scene = viewer.scene; | ||
scene.light = new Cesium.DirectionalLight({ | ||
direction: new Cesium.Cartesian3(1, 0, 0), // Updated every frame | ||
}); | ||
|
||
// Update the light position base on the camera | ||
const scratchNormal = new Cesium.Cartesian3(); | ||
scene.preRender.addEventListener(function (scene, time) { | ||
const surfaceNormal = globe.ellipsoid.geodeticSurfaceNormal( | ||
scene.camera.positionWC, | ||
scratchNormal | ||
); | ||
const negativeNormal = Cesium.Cartesian3.negate( | ||
surfaceNormal, | ||
surfaceNormal | ||
); | ||
scene.light.direction = Cesium.Cartesian3.normalize( | ||
Cesium.Cartesian3.add( | ||
negativeNormal, | ||
scene.camera.rightWC, | ||
surfaceNormal | ||
), | ||
scene.light.direction | ||
); | ||
}); | ||
//Sandcastle_End | ||
}; | ||
if (typeof Cesium !== "undefined") { | ||
window.startupCalled = true; | ||
window.startup(Cesium).catch((error) => { | ||
"use strict"; | ||
console.error(error); | ||
}); | ||
Sandcastle.finishedLoading(); | ||
} | ||
</script> | ||
</body> | ||
</html> |
Binary file added
BIN
+21.6 KB
Apps/Sandcastle/gallery/Globe Materials – Water Mask Elevation Map.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(applyDayNightAlpha << 32)
will return 0 or 1, which conflicts withsceneMode
and may introduce potential bugs, see #12260