Cesium流动线纹理

注:流动效果使用的图片是渐进色


colors.png
colors1.png
Cesium流动线效果.gif

1.扩展类PolylineTrailMaterialProperty (Cesium  1.67-1.68)

import Cesium from "cesium/Cesium";

export class PolylineTrailMaterialProperty {

    constructor(options) {

        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);

        this._definitionChanged = new Cesium.Event();

        this._color = undefined;

        this._colorSubscription = undefined;

        this.color = options.color;

        this.duration = options.duration;

        this.trailImage = options.trailImage;

        this._time = performance.now();

    }

}

Cesium.defineProperties(PolylineTrailMaterialProperty.prototype, {

    isConstant: {

        get: function() {

            return false;

        }

    },

    definitionChanged: {

        get: function() {

            return this._definitionChanged;

        }

    },

    color: Cesium.createPropertyDescriptor('color')

});

PolylineTrailMaterialProperty.prototype.getType = function(time) {

    return 'PolylineTrail';

}

PolylineTrailMaterialProperty.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.Material.PolylineTrailImage;

    result.image = this.trailImage;

    result.time = ((performance.now() - this._time) % this.duration) / this.duration;

    return result;

}

PolylineTrailMaterialProperty.prototype.equals = function(other) {

    return this === other ||

        (other instanceof PolylineTrailMaterialProperty &&

            Cesium.Property.equals(this._color, other._color))

}

Cesium.Material.PolylineTrailType = 'PolylineTrail';

Cesium.Material.PolylineTrailImage = "images/colors.png";

Cesium.Material.PolylineTrailSource = "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\

                                                      }";

Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailType, {

    fabric: {

        type: Cesium.Material.PolylineTrailType,

        uniforms: {

            color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),

            image: Cesium.Material.PolylineTrailImage,

            time: 0

        },

        source: Cesium.Material.PolylineTrailSource

    },

    translucent: function(material) {

        return true;

    }

});

Cesium.PolylineTrailMaterialProperty = PolylineTrailMaterialProperty;

2.使用

<template>

  <div class="fullSize" id="cesiumContainer"></div>

</template>

<script>

import "cesium/Widgets/widgets.css";

import Cesium from "cesium/Cesium";

import CesiumFactory from "@/js/cesium/CesiumFactory";

import PolylineTrailMaterialProperty from "@/js/cesium/overlay/PolylineTrailMaterialProperty";

export default {

  name: "CesiumDynamicLine1",

  data() {

    return {

      viewer: null

    };

  },

  mounted() {

    const factory = new CesiumFactory();

    this.viewer = factory.getViewer();

    this.create();

  },

  methods: {

    create() {

      //创建射线

      var data = {

        center: {

          id: 0,

          lon: 114.302312702,

          lat: 30.598026044,

          size: 20,

          color: Cesium.Color.PURPLE

        },

        points: [

          {

            id: 1,

            lon: 115.028495718,

            lat: 30.200814617,

            color: Cesium.Color.YELLOW,

            size: 15

          },

          {

            id: 2,

            lon: 110.795000473,

            lat: 32.638540762,

            color: Cesium.Color.RED,

            size: 15

          },

          {

            id: 3,

            lon: 111.267729446,

            lat: 30.698151246,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 4,

            lon: 112.126643144,

            lat: 32.058588576,

            color: Cesium.Color.GREEN,

            size: 15

          },

          {

            id: 5,

            lon: 114.885884938,

            lat: 30.395401912,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 6,

            lon: 112.190419415,

            lat: 31.043949588,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 7,

            lon: 113.903569642,

            lat: 30.93205405,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 8,

            lon: 112.226648859,

            lat: 30.367904255,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 9,

            lon: 114.86171677,

            lat: 30.468634833,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 10,

            lon: 114.317846048,

            lat: 29.848946148,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 11,

            lon: 113.371985426,

            lat: 31.70498833,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 12,

            lon: 109.468884533,

            lat: 30.289012191,

            color: Cesium.Color.BLUE,

            size: 15

          },

          {

            id: 13,

            lon: 113.414585069,

            lat: 30.368350431,

            color: Cesium.Color.SALMON,

            size: 15

          },

          {

            id: 14,

            lon: 112.892742589,

            lat: 30.409306203,

            color: Cesium.Color.WHITE,

            size: 15

          },

          {

            id: 15,

            lon: 113.16085371,

            lat: 30.667483468,

            color: Cesium.Color.SALMON,

            size: 15

          },

          {

            id: 16,

            lon: 110.670643354,

            lat: 31.74854078,

            color: Cesium.Color.PINK,

            size: 15

          }

        ],

        options: {

          name: "",

          polyline: {

            width: 2,

            material: [Cesium.Color.GREEN, 3000]

          }

        }

      };

      this.createFlyLines(data);

    },

    createFlyLines(data) {

      const center = data.center;

      const cities = data.points;

      const startPoint = Cesium.Cartesian3.fromDegrees(

        center.lon,

        center.lat,

        0

      );

      //中心点

      this.viewer.entities.add({

        position: startPoint,

        point: {

          pixelSize: center.size,

          color: center.color

        }

      });

      //大批量操作时,临时禁用事件可以提高性能

      this.viewer.entities.suspendEvents();

      //散点

      cities.map(city => {

        let material = new Cesium.PolylineTrailMaterialProperty({

          color: Cesium.Color.GREEN,

          duration: 3000,

          trailImage: "images/colors1.png"

        });

        const endPoint = Cesium.Cartesian3.fromDegrees(city.lon, city.lat, 0);

        this.viewer.entities.add({

          position: endPoint,

          point: {

            pixelSize: city.size - 10,

            color: city.color

          }

        });

        this.viewer.entities.add({

          polyline: {

            positions: this.generateCurve(startPoint, endPoint),

            width: 2,

            material: material

          }

        });

      });

      this.viewer.entities.resumeEvents();

      this.viewer.flyTo(this.viewer.entities);

    },

    /**

    * 生成流动曲线

    * @param startPoint 起点

    * @param endPoint 终点

    * @returns {Array}

    */

    generateCurve(startPoint, endPoint) {

      let addPointCartesian = new Cesium.Cartesian3();

      Cesium.Cartesian3.add(startPoint, endPoint, addPointCartesian);

      let midPointCartesian = new Cesium.Cartesian3();

      Cesium.Cartesian3.divideByScalar(addPointCartesian, 2, midPointCartesian);

      let midPointCartographic = Cesium.Cartographic.fromCartesian(

        midPointCartesian

      );

      midPointCartographic.height =

        Cesium.Cartesian3.distance(startPoint, endPoint) / 5;

      let midPoint = new Cesium.Cartesian3();

      Cesium.Ellipsoid.WGS84.cartographicToCartesian(

        midPointCartographic,

        midPoint

      );

      let spline = new Cesium.CatmullRomSpline({

        times: [0.0, 0.5, 1.0],

        points: [startPoint, midPoint, endPoint]

      });

      let curvePoints = [];

      for (let i = 0, len = 200; i < len; i++) {

        curvePoints.push(spline.evaluate(i / len));

      }

      return curvePoints;

    }

  }

};

</script>

<style lang="scss" >

</style>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容

  • 因gif截图问题,动画效果看起来不够流畅。 1.调用方法 <!DOCTYPE html> 圆形波纹 ...
    happy_port阅读 5,919评论 11 6
  • 那株不愿向着阳光的桃花 傲然的独自生长 只因在这山间中 一览江畔人家 萤火 鸣虫 鸿雁 借骤雨中的涟漪 与...
    陌允诺阅读 268评论 0 1
  • 今天上午办公之余,闲来伸伸懒腰,转到窗台,看见榛子,随手嗑了几个,竟嗑出些许感慨来。 榛子这东西,睹物可以思人。 ...
    放下皆得阅读 2,689评论 20 188
  • 拼车!拼车!拼车回家!有没有2月12号从惠州白石或者深圳坪山回湖北黄冈的还没买到车票的老乡,我们一起拼车回家,还差...
    飘_a1ad阅读 262评论 0 0