Cesium从入门到放弃:模型压平

编程入门 行业动态 更新时间:2024-10-25 10:31:24

Cesium从<a href=https://www.elefans.com/category/jswz/34/1770026.html style=入门到放弃:模型压平"/>

Cesium从入门到放弃:模型压平

模型压平这个功能在Cesium界很神秘,其他功能百度一下都能找到几行代码,找不到代码也能找到思路,只有这个,找到的全是视频,下面是我研究后的一点思路,供大家参考。

思考

  1. 模型压平究竟做了什么?
    简单来说就是给定一个平面,如果模型的高度大于该平面,该将其高度设为该平面的高度,否则保持不变。
if (position.z > planeHeight) {position.z = planeHeight;
}

核心就是上面这行代码,哪么下一个问题来了, position是什么坐标。

  1. 应该在什么坐标系中比较模型和平面的高度?
    我们在顶点着色器中可以拿到的坐标特别多,他们是模型在不同坐标系中的顶点坐标,理论上,我们可以在任意坐标系下计算,只需要让平面和模型在同一坐标系下即可。但是最符合人们思维习惯的应该是在以模型中心为原点的,Z轴垂直该平面向上的坐标系中计算

代码

cesium1.84以上,支持用户对模型添加自定义着色器,如此,我们甚至不需要修改源码,就可以实现该功能

const inverseModelLocal = new Matrix4();
const modelLocal = new Matrix4();
const model = new Cesium3DTileset({// url: '../Apps/SampleData/Cesium3DTiles/Tilesets/Tileset/tileset.json',url: './dayanta/tileset.json',customShader: new CustomShader({uniforms: {u_inverseLocalModel: {type: UniformType.MAT4,value: inverseModelLocal},u_localModel: {type: UniformType.MAT4,value: modelLocal},u_planeHeight: {type: UniformType.FLOAT,value: -20}},vertexShaderText: `void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput) {vec4 positionWC = u_inverseLocalModel * czm_model * vec4(vsInput.attributes.positionMC, 1.0);  if (positionWC.z > u_planeHeight) {positionWC.z = u_planeHeight;}vsOutput.positionMC = (czm_inverseModel * u_localModel * positionWC).xyz;   }`,})
});
model.readyPromise.then((v) => {const center = v.boundingSphere.center;model.localView = Transforms.eastNorthUpToFixedFrame(center, viewer.scene.globe.ellipsoid, modelLocal);      model.localViewInverse = Matrix4.inverse(modelLocal, inverseModelLocal);})viewer.flyTo(model)

附上一张效果图

TODO

  1. 多个矩形区域的压平
  2. 压平后闪面问题如何解决

更多推荐

Cesium从入门到放弃:模型压平

本文发布于:2024-02-08 21:59:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1675539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:入门   模型   Cesium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!