GuLiMmo
2024-02-27 c229d46edd6bb7f731417ead8c4576bebbb36580
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as Cesium from 'cesium'
/**
 * 定义Cesium材质对象
 */
class ImageTrailMaterial {
  _definitionChanged: Cesium.Event<(...args: any[]) => void>;
  _color: undefined;
  _colorSubscription: undefined;
  _speed: undefined;
  _speedSubscription: undefined;
  _image: undefined;
  _imageSubscription: undefined;
  _repeat: undefined;
  _repeatSubscription: undefined;
  color: any;
  speed: any;
  image: any;
  repeat: Cesium.Cartesian2;
  constructor (options : any = {}) {
    this._definitionChanged = new Cesium.Event()
    this._color = undefined
    this._colorSubscription = undefined
    this._speed = undefined
    this._speedSubscription = undefined
    this._image = undefined
    this._imageSubscription = undefined
    this._repeat = undefined
    this._repeatSubscription = undefined
    this.color = options.color || Cesium.Color.fromBytes(0, 0, 255, 255)
    this.speed = options.speed || 1
    this.image = options.image
    this.repeat = new Cesium.Cartesian2(
      options.repeat?.x || 1,
      options.repeat?.y || 1
    )
  }
 
  get isConstant () {
    return false
  }
 
  get definitionChanged () {
    return this._definitionChanged
  }
 
  getType () {
    return Cesium.Material.ImageTrailMaterialType
  }
 
  getValue (time: any, result: any) {
    if (!result) {
      result = {}
    }
    result.color = Cesium.Property?.getValueOrUndefined(this._color, time)
    result.image = Cesium.Property?.getValueOrUndefined(this._image, time)
    result.repeat = Cesium.Property?.getValueOrUndefined(this._repeat, time)
    result.speed = this._speed
    return result
  }
 
  equals (other: this) {
    return (
      this === other ||
      (other instanceof ImageTrailMaterial &&
        Cesium.Property.equals(this._color, other._color) &&
        Cesium.Property.equals(this._image, other._image) &&
        Cesium.Property.equals(this._repeat, other._repeat) &&
        Cesium.Property.equals(this._speed, other._speed))
    )
  }
}
 
Object.defineProperties(ImageTrailMaterial.prototype, {
  color: Cesium.createPropertyDescriptor('color'),
  speed: Cesium.createPropertyDescriptor('speed'),
  image: Cesium.createPropertyDescriptor('image'),
  repeat: Cesium.createPropertyDescriptor('repeat'),
})
// 材质类型
Cesium.Material.ImageTrailMaterialType = 'PolylineImageTrail'
// 添加材质到缓冲区中
Cesium.Material._materialCache.addMaterial(
  Cesium.Material.ImageTrailMaterialType,
  {
    fabric: {
      type: Cesium.Material.ImageTrailMaterialType,
      // uniform变量
      uniforms: {
        color: new Cesium.Color(1.0, 0.0, 0.0, 0.7),
        image: Cesium.Material.DefaultImageId,
        speed: 1,
        repeat: new Cesium.Cartesian2(1, 1),
      },
      //
      source: `
                    uniform sampler2D image; 
                    uniform float speed;
                    uniform vec4 color;
                    uniform vec2 repeat;
                    
                    czm_material czm_getMaterial(czm_materialInput materialInput){
                        czm_material material=czm_getDefaultMaterial(materialInput);
                        vec2 st=repeat * materialInput.st;
                        float time=fract(czm_frameNumber*speed/1000.);
                        // st.s是横轴方向运动,st.t是纵轴,
                        // vec2(fract(st.s-time),st.t)是横轴按照时间变化而变化,纵轴保持正常不变化
                        vec4 colorImage=texture2D(image,vec2(fract(st.s-time),st.t));
                        vec4 fragColor;
                        fragColor.rgb=color.rgb / 1.0;
                        if(color.a==0.){
                            material.alpha=colorImage.a;
                            material.diffuse=colorImage.rgb;
                        }else{
                            material.alpha=colorImage.a*color.a;
                            material.diffuse=max(color.rgb*material.alpha*3.,color.rgb);
                        }
                        return material;
                    }
                    `
    },
    translucent: function () {
      return true
    },
  }
)
 
export default ImageTrailMaterial