cesium 实战008--场景效果 (雾)

场景效果 (雾)

效果图:
在这里插入图片描述

效果代码:


// import * as Cesium from "cesium";
class FogEffect {
  constructor(viewer, options) {
    if (!viewer) throw new Error("no viewer object!");
    options = options || {};
    this.visibility = Cesium.defaultValue(options.visibility, 0.1); // 能见度
    this.color = Cesium.defaultValue(
      options.color,
      new Cesium.Color(0.8, 0.8, 0.8, 0.5)
    );
    this.viewer = viewer;
    this.init();
  }

  init() {
    this.fogStage = new Cesium.PostProcessStage({
      name: "czm_fog",
      fragmentShader: this.fog(),
      uniforms: {
        visibility: () => {
          return this.visibility;
        },
        fogColor: () => {
          return this.color;
        },
      },
    });
    this.viewer.scene.postProcessStages.add(this.fogStage);
  }

  destroy() {
    if (!this.viewer || !this.fogStage) return;
    this.viewer.scene.postProcessStages.remove(this.fogStage);
    const isDestroyed = this.fogStage.isDestroyed();
    // 先检查是否被销毁过,如果已经被销毁过再调用destroy会报错
    if (!isDestroyed) {
      this.fogStage.destroy();
    }
    delete this.visibility;
    delete this.color;
  }

  show(visible) {
    this.fogStage.enabled = visible;
  }

  fog() {
    return "uniform sampler2D colorTexture;\n\
       uniform sampler2D depthTexture;\n\
       uniform float visibility;\n\
       uniform vec4 fogColor;\n\
       in vec2 v_textureCoordinates; \n\
       out vec4 fragColor;\n\
       void main(void) \n\
       { \n\
          vec4 origcolor = texture(colorTexture, v_textureCoordinates); \n\
          float depth = czm_readDepth(depthTexture, v_textureCoordinates); \n\
          vec4 depthcolor = texture(depthTexture, v_textureCoordinates); \n\
          float f = visibility * (depthcolor.r - 0.3) / 0.2; \n\
          if (f < 0.0) f = 0.0; \n\
          else if (f > 1.0) f = 1.0; \n\
          fragColor = mix(origcolor, fogColor, f); \n\
       }\n";
  }
}

export default FogEffect;

引用代码

import FogEffect from "@/utils/cesiumCtrl/fog"

使用代码:

    // 大雾
    FogShow() {
      let that = this
      this.Foginstance = new FogEffect(that.webGlobe, {
        visibility: 0.1,
        color: new Cesium.Color(0.8, 0.8, 0.8, 0.3),
      })
    },
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值