Cesium-Examples/examples/cesiumEx/7.2.2、CustomShader_3DTiles.html

78 lines
2.7 KiB
HTML
Raw Permalink Normal View History

<!--********************************************************************
* by jiawanlong
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="./../../libs/cesium/Cesium1.98/Widgets/widgets.css" />
<script type="text/javascript" src="./../../libs/cesium/Cesium1.98/Cesium.js"></script>
</head>
<body style="
margin: 0;
overflow: hidden;
background: #fff;
width: 100%;
height: 100%;
position: absolute; top: 0;
">
<div id="map" style="margin: 0 auto; width: 100%; height: 100%"></div>
<script type="text/javascript">
Cesium.Ion.defaultAccessToken =
2025-06-07 11:30:00 +00:00
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyMjBhMzcxMC0wNjBiLTRmYjItYjY1MC0wMzAwMzMyMGUyMmEiLCJpZCI6MzAzNzc3LCJpYXQiOjE3NDc2Mzk0NTV9.E_90aKtVdzRGlU2z48VwJ4mWvl-uuDkfQBCOO6zbzn4";
const viewer = new Cesium.Viewer("map", {});
viewer.scene.debugShowFramesPerSecond = true;
/*
1. 顶点着色器Vertex Shader坐标变换、传递数据给片段着色器、动态变形
2. 片段着色器Fragment Shader计算颜色、应用光照、后处理效果
*/
const customShader = new Cesium.CustomShader({
vertexShaderText: `
void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput) {
// vec3 position = vsInput.attributes.positionMC;
// position.y = position.y + 10.5;
// vsOutput.positionMC = position;
}
`,
fragmentShaderText: `
void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) {
float height = fsInput.attributes.positionMC.y;
vec3 lowColor = vec3(0.0, 0.0, 1.0); // 蓝色
vec3 highColor = vec3(1.0, 0.0, 0.0); // 红色
// material.diffuse = mix(lowColor, highColor, smoothstep(0.0, 200.0, height));
if(height > 30.0){
material.diffuse = lowColor;
}else{
material.diffuse = highColor;
}
}
`,
});
const tileset = new Cesium.Cesium3DTileset({
url: "./data/tileset.json",
customShader: customShader
});
tileset.readyPromise
.then(function (tileset) {
viewer.scene.primitives.add(tileset);
viewer.zoomTo(tileset)
})
.catch(function (error) {
console.log(error);
});
</script>
</body>
</html>