Cesium飛線

在學習Cesium過程中,發(fā)現Cesium的Material可以自定義自己所需的材質,看到網上其他資源有實現城市飛線的效果,感覺挺酷炫,于是經過一凡探索,實現了一下效果

飛線效果.gif

分別采用了Primitive和Entity方式調用實現:

addFlyintLineByPrimitive(positions) {
                    // primitive方式添加
                    const primitive = new Cesium.Primitive({
                        geometryInstances: new Cesium.GeometryInstance({
                            geometry: new Cesium.PolylineGeometry({
                                positions: positions,
                                width: 1.0,
                                vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
                            })
                        }),
                        appearance: new Cesium.PolylineMaterialAppearance({
                            material: Cesium.Material.fromType(Cesium.Material.PolylineFlowType, {
                                speed: 10 * Math.random(),
                                color: Cesium.Color.CYAN,
                                percent: 0.1,
                                gradient: 0.01
                            }),
                        })
                    });
                    this.viewer.scene.primitives.add(primitive);
                },
                addFlyingLineByEntity(positions) {
                    //entity方式添加
                    this.viewer.entities.add({
                        polyline: {
                            positions: positions,
                            width: 2.0,
                            material: new Cesium.PolylineFlowMaterialProperty({
                                speed: 6 * Math.random(),
                                color: Cesium.Color.CYAN,
                                percent: 0.1,
                                gradient: 0.01
                            })
                        }
                    })
                },

PolylineFlowMaterialProperty 類:

class PolylineFlowMaterialProperty {
    constructor(options) {
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged = new Cesium.Event();
        this._color = void 0;
        this._speed = void 0;
        this._percent = void 0;
        this._gradient = void 0;
        this._colorSubscription = void 0;
        this.color = options.color;
        this.speed = options.speed;
        this.percent = options.percent;
        this.gradient = options.gradient;
        this._time = performance.now();
    }
}


Object.defineProperties(PolylineFlowMaterialProperty.prototype, {
    isConstant: {
        get: function() {
            return Cesium.Property.isConstant(this._color) &&
                Cesium.Property.isConstant(this._speed) &&
                Cesium.Property.isConstant(this._percent) &&
                Cesium.Property.isConstant(this._gradient);
        }
    },
    definitionChanged: {
        get: function() {
            return this._definitionChanged;
        }
    },
    color: Cesium.createPropertyDescriptor("color"),
    speed: Cesium.createPropertyDescriptor("speed"),
    percent: Cesium.createPropertyDescriptor("percent"),
    gradient: Cesium.createPropertyDescriptor("gradient"),
})


PolylineFlowMaterialProperty.prototype.getType = function(time) {
    return 'PolylineFlow';
    //return Cesium.Material.PolylineFlowType;
}

PolylineFlowMaterialProperty.prototype.getValue = function(time, result) {
    Cesium.defined(result) || (result = {});
    result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.CYAN, result.color);
    result.percent = Cesium.Property.getValueOrDefault(this._percent, time, 0.1, result.percent);
    result.gradient = Cesium.Property.getValueOrDefault(this._gradient, time, 0.01, result.gradient);
    result.speed = Cesium.Property.getValueOrDefault(this._speed, time, 1, result.speed);
    result.time = (performance.now() - this._time) % this._speed / this._speed;
    return result;
}

PolylineFlowMaterialProperty.prototype.equals = function(other) {
    return this === other ||
        (other instanceof PolylineFlowMaterialProperty &&
            Cesium.Property.equals(this._color, other._color) && Cesium.Property.equals(this._speed, other._speed) &&
            Cesium.Property.equals(this._gradient, other._gradient) && Cesium.Property.equals(this._percent, other._percent));
}


Cesium.Material.PolylineFlowType = 'PolylineFlow';
Cesium.Material.PolylineFlowSource = `
        uniform vec4 color;
        uniform float speed;
        uniform float percent;
        uniform float gradient;

        czm_material czm_getMaterial(czm_materialInput materialInput){
        czm_material material = czm_getDefaultMaterial(materialInput);
        vec2 st = materialInput.st;
        float t =fract(czm_frameNumber * speed / 1000.0);
        t *= (1.0 + percent);
        float alpha = smoothstep(t- percent, t, st.s) * step(-t, -st.s);
        alpha += gradient;
        material.diffuse = color.rgb;
        material.alpha = alpha;
        return material;
        }
`;
Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineFlowType, {
    fabric: {
        type: Cesium.Material.PolylineFlowType,
        uniforms: {
            color: Cesium.Color.CYAN,
            speed: 1,
            gradient: 0.01,
            percent: 0.1,
            time: 0
        },
        source: Cesium.Material.PolylineFlowSource
    },
    translucent: !0
});

Cesium.PolylineFlowMaterialProperty = PolylineFlowMaterialProperty;;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容