cesium 添加飞线

1.创建PolylineTrailLinkMaterialProperty.js 文件

/*
 * @Description: 飞线效果(参考开源代码)
 * @Version: 1.0
 * @Author: Julian
 * @Date: 2022-03-05 16:13:21
 * @LastEditors: Julian
 * @LastEditTime: 2022-03-05 17:39:38
 */
class PolylineTrailLinkMaterialProperty {
    constructor(options) {
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._speed = undefined;
        this._percent = undefined;
        this._gradient = undefined;
        this.color = options.color;
        this.speed = options.speed;
        this.percent = options.percent;
        this.gradient = options.gradient;
    };

    get isConstant() {
        return false;
    }

    get definitionChanged() {
        return this._definitionChanged;
    }

    getType(time) {
        return Cesium.Material.LineFlowMaterialType;
    }

    getValue(time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }

        result.color = Cesium.Property.getValueOrDefault(this._color, time, Cesium.Color.RED, result.color);
        result.speed = Cesium.Property.getValueOrDefault(this._speed, time, 5.0, result.speed);
        result.percent = Cesium.Property.getValueOrDefault(this._percent, time, 0.1, result.percent);
        result.gradient = Cesium.Property.getValueOrDefault(this._gradient, time, 0.01, result.gradient);
        return result
    }

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

Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
    color: Cesium.createPropertyDescriptor('color'),
    speed: Cesium.createPropertyDescriptor('speed'),
    percent: Cesium.createPropertyDescriptor('percent'),
    gradient: Cesium.createPropertyDescriptor('gradient'),
})

Cesium.PolylineTrailLinkMaterialProperty = PolylineTrailLinkMaterialProperty;
Cesium.Material.PolylineTrailLinkMaterialProperty = 'PolylineTrailLinkMaterialProperty';
Cesium.Material.LineFlowMaterialType = 'LineFlowMaterialType';
Cesium.Material.LineFlowMaterialSource =
    `
    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.LineFlowMaterialType, {
    fabric: {
        type: Cesium.Material.LineFlowMaterialType,
        uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 1.0),
            speed: 10.0,
            percent: 0.1,
            gradient: 0.01
        },
        source: Cesium.Material.LineFlowMaterialSource
    },
    translucent: function (material) {
        return true;
    }
})

export {
    PolylineTrailLinkMaterialProperty
}

2.引入PolylineTrailLinkMaterialProperty.js

import { PolylineTrailLinkMaterialProperty } from "../../utils/PolylineTrailLinkMaterialProperty";

3.添加线并使用材质

viewer.entities.add({
                polyline: {
                  positions: positions, // 线经纬度
                  material: new Cesium.PolylineTrailLinkMaterialProperty({
                    color: new Cesium.Color(1.0, 1.0, 0.0, 0.8),
                    speed: 5,
                    percent: 0.1,
                    gradient: 0.01,
                  }),
                },
              });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Cesium是一个用于创建地球上三维可视化的开源JavaScript库。它提供了丰富的功能和工具,可以在浏览器中展示地理空间数据。在Cesium添加图钉可以用于标记地球上的特定位置或者添加自定义的标记。 要在Cesium添加图钉,可以按照以下步骤进行操作: 1. 创建一个Cesium Viewer对象,用于显示地球场景。 2. 创建一个Entity对象,用于表示图钉的属性和位置。 3. 将Entity对象添加到Viewer的entities集合中,以便在地球上显示。 4. 可选:可以为图钉添加自定义的样式、标签、描述等属性。 下面是一个简单的示例代码,演示如何在Cesium添加一个图钉: ```javascript // 创建Cesium Viewer对象 var viewer = new Cesium.Viewer('cesiumContainer'); // 创建一个Entity对象 var pinEntity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883), billboard: { image: 'path/to/pin.png', verticalOrigin: Cesium.VerticalOrigin.BOTTOM } }); // 可选:为图钉添加标签和描述 pinEntity.label = { text: 'My Pin', showBackground: true, font: '14px sans-serif', horizontalOrigin: Cesium.HorizontalOrigin.CENTER, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, pixelOffset: new Cesium.Cartesian2(0, -20) }; pinEntity.description = 'This is a custom pin on the map.'; // 设置地球视角 viewer.zoomTo(pinEntity); ``` 这段代码创建了一个Cesium Viewer对象,并在地球上添加了一个图钉。图钉使用一个图片作为标记,并设置了位置和其他属性。你可以根据需要自定义图钉的样式和属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值