cesium 动态水流效果

一.创建 PolylineTrailLinkMaterialProperty.js

import * as Cesium from "cesium/Cesium";

const PolylineTrailLinkMaterialProperty = (function() {
    function PolylineTrailLinkMaterialProperty(options) {
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
    
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._image = undefined;
        this._duration = (new Date()).getTime();
        this._colorSubscription = undefined;
        
        this.image = options.imgurl;
        this.color = options.color;
        this.duration = options.duration || 3000;
    
        this.isTranslucent = function () {
            return true;
        }
    }
    //1.69版本使用这个
     Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
    //Cesium.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
        isConstant: {
            get: function () {
                return false;
            }
        },
        definitionChanged: {
            get: function () {
                return this._definitionChanged;
            }
        },
        color: Cesium.createPropertyDescriptor('color'),
        image: Cesium.createPropertyDescriptor("image"),
        duration: Cesium.createPropertyDescriptor("duration")
    });
    PolylineTrailLinkMaterialProperty.prototype.getType = function (time) {
        return 'PolylineTrailLink';
    }
    PolylineTrailLinkMaterialProperty.prototype.getValue = function (time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        result.image = Cesium.Property.getValueOrUndefined(this._image, time);
        result.time = (((new Date()).getTime() - this._duration) % this.duration) / this.duration;
        return result;
    }
    PolylineTrailLinkMaterialProperty.prototype.equals = function (other) {
        return this === other ||
            (other instanceof PolylineTrailLinkMaterialProperty &&
                Cesium.Property.equals(this._color, other._color)&&
                Cesium.Property.equals(this._duration, other._duration)&&
                Cesium.Property.equals(this._image, other._image))
    }
    Cesium.PolylineTrailLinkMaterialProperty = PolylineTrailLinkMaterialProperty;
    
    Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink';
    Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
        fabric: {
            type: Cesium.Material.PolylineTrailLinkType,
            uniforms: {
                color: new Cesium.Color(0.0, 0.0, 1.0, 0.5),
                image: Cesium.Material.DefaultImageId,
                time: 20
            },
            source:"czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                    {\n\
                        czm_material material = czm_getDefaultMaterial(materialInput);\n\
                        vec2 st = materialInput.st;\n\
                        vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\
                        material.alpha = colorImage.a * color.a;\n\
                        material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
                        return material;\n\
                    }"
        },
        translucent: function (material) {
            return true;
        }
    });
})();
export {
    PolylineTrailLinkMaterialProperty
}

样式注意事项:

1.多重纹理

uniform vec2 repeat;
//...
vec4 textureValue = texture2D(image, fract(repeat * materialInput.st));

2.三重纹理

//三重纹理
vec4 colorImage = texture2D(image, vec2(fract((st.s-time)*3.0), st.t));\n\

3.不重复纹理

vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\

4.速度与time有关

二、调用js(路径换成项目的路径)

import {PolylineTrailLinkMaterialProperty} from "../assets/js/line"; //cesiumfun

三、方法中调用

let item = viewer.entities.add({
    name: 'PolylineTrail',
    polyline: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights([
            lon, lat, 2500,
            lon + 10, lat, 2500,
            lon + 10, lat + 10, 2500
        ]),
        width: 15,
        material: new Cesium.PolylineTrailLinkMaterialProperty({
            color:Cesium.Color.ORANGE, 
            duration:3000,
            imgurl:"./colors1.png"
        })
    }
});

viewer.entities.add({
    name: 'PolylineTrai1l',
    polyline: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights([
            lon+1, lat+1, 2500,
            lon + 11, lat+1, 2500,
            lon + 11, lat + 11, 2500
        ]),
        width: 15,
        material: new Cesium.PolylineTrailLinkMaterialProperty({
            color:Cesium.Color.ORANGE, 
            duration:1000,
            imgurl:"./colors2.png"
        })
    }
});
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值