Cesium 高度雾

原理大家都知道,但是怎么计算当前像素点的高程呢?相信你也问了gpt很多次,但是确实现在的AI还无法给你想要的答案。

在后处理中计算目前了解到有两种方法,这里就用简单的一种吧。

关键点就是地球半径 废话不多说 直接ctrl c ctrl v吧

记得开启深度检测

/**
 * @description: 高度雾效果
 * @param {*}
 * @return {*}
 */
import * as Cesium from "cesium";

export default class HeightFogEffect {
  constructor(viewer, options) {
    if (!viewer) throw new Error("no viewer object!");
    options = options || {};
    this.viewer = viewer;
    this.height = options.height||100
    this.init();
  }

  init() {
    this.heightFogStage = new Cesium.PostProcessStage({
      name: "czm_heightfog",
      fragmentShader: this.fog(),
      uniforms: {
        alpha: 0.88,
        height: this.height,
        earthRadius: ()=>{
            return Cesium.Cartesian3.magnitude(this.viewer.camera.positionWC) - this.viewer.camera.positionCartographic.height;
        }
      },
    });
    this.viewer.scene.postProcessStages.add(this.heightFogStage);
  }

  destroy() {
    if (!this.viewer || !this.heightFogStage) return;
    this.viewer.scene.postProcessStages.remove(this.heightFogStage);
    Cesium.destroyObject(this.heightFogStage);
  }

  show(visible) {
    this.heightFogStage.enabled = visible;
  }
  changeHeight(value) {
    this.heightFogStage.uniforms.height = value
  }
  changeAlpha(value) {
    this.heightFogStage.uniforms.alpha = value
  }

  fog() {
    return `uniform sampler2D colorTexture;
    uniform sampler2D depthTexture;
    uniform float alpha;
    uniform float height;
    uniform float earthRadius;
    in vec2 v_textureCoordinates;

    float getDepth(in vec4 depth){
      float z_window = czm_unpackDepth(depth);
      z_window = czm_reverseLogDepth(z_window);
      float n_range = czm_depthRange.near;
      float f_range = czm_depthRange.far;
      return (2.0 * z_window - n_range - f_range) / (f_range - n_range);
    }
  
    float getHeight(in vec4 currentDepth){
      float h = 0.0;
      float depth = czm_unpackDepth(currentDepth);
      if (depth == 0.0) {
        h = czm_infinity;
      }
      vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, depth);
      vec4 positionWC = czm_inverseView * eyeCoordinate;
      vec3 car3 = positionWC.xyz / positionWC.w;
      h = length(car3.xyz) - earthRadius;
      return h;
    }
  
    void main(){
      vec4 color = texture(colorTexture, v_textureCoordinates);
      vec4 currentDepth = texture(depthTexture, v_textureCoordinates);

      float depth = getDepth(currentDepth);
      float pointHeight = getHeight(currentDepth);

      if(depth>=1.0){
         out_FragColor = color;
         return;
      }

      float fog = height/pointHeight-1.;
      out_FragColor = mix(color,vec4(1.,1.,1.,1.),clamp(fog, 0.0, 1.0)*alpha);
    }`
  }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值