美文网首页开源
Cesium雾效果

Cesium雾效果

作者: happy_port | 来源:发表于2021-03-24 18:42 被阅读0次

本文是一个下雾的效果

  1. 下雾效果的类代码:
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._show = Cesium.defaultValue(options.show, !0);
        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);
        this.fogStage.destroy();
        delete this.visibility;
        delete this.color;
    }

    show(visible) {
        this._show = visible;
        this.fogState.enabled = this._show;
    }

    fog() {
        return "uniform sampler2D colorTexture;\n\
         uniform sampler2D depthTexture;\n\
         uniform float visibility;\n\
         uniform vec4 fogColor;\n\
         varying vec2 v_textureCoordinates; \n\
         void main(void) \n\
         { \n\
            vec4 origcolor = texture2D(colorTexture, v_textureCoordinates); \n\
            float depth = czm_readDepth(depthTexture, v_textureCoordinates); \n\
            vec4 depthcolor = texture2D(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\
            gl_FragColor = mix(origcolor, fogColor, f); \n\
         }\n";
    }
}
  1. 页面使用方式
    引入上面的js即可
new Cesium.FogEffect(viewer, {
            visibility: 0.2,
            color: new Cesium.Color(0.8, 0.8, 0.8, 0.3)
        });
  1. 效果图


    cesium_fog.png

相关文章

  • Cesium雾效果

    本文是一个下雾的效果 下雾效果的类代码: 页面使用方式引入上面的js即可 效果图cesium_fog.png

  • WebGL地震波

    在Cesium中实现地震波效果

  • Cesium实现淹没分析

    代码思路 Cesium的淹没效果主要使用到的是Cesium库中polygon的extrudedHeight属性。通...

  • Cesium卷帘

    cesium实现卷帘效果比较简单,在官网也提供了相应的例子[https://sandcastle.cesium.c...

  • Cesium雨效果

    作为一个WebGL的小白,在前人大牛的肩膀上参考代码,自己整理了一个雨效果图,仅供参考 Cesium版本1.79....

  • Cesium下雪效果

    上一篇写了一个下雨的效果,本文是一个下雪的效果 下雪效果的类代码: 页面使用方式引入上面的js即可 效果图cesi...

  • Cesium 3d 热力图

    Cesium heatmap3d效果热力图先上效果图: 代码地址: https://github.com/happ...

  • Cesium gltf模型姿态动态调整

    Cesium 动态调整 gltf模型姿态:包含中心点、平移、旋转、缩放。 效果 代码

  • Cesium圆形波纹效果

    因gif截图问题,动画效果看起来不够流畅。 1. 扩展实现类CircleWaveMaterialProperty ...

  • Cesium1.87+ 实现建筑泛光效果

    代码 Cesium 1.87+增加了customShader自定义着色器,实现建筑泛光效果示例代码如下: 效果

网友评论

    本文标题:Cesium雾效果

    本文链接:https://www.haomeiwen.com/subject/yemhhltx.html