import * as Cesium from 'cesium' /** * 定义Cesium材质对象 */ class ImageTrailMaterial { constructor(options = {}) { this._definitionChanged = new Cesium.Event() this._color = void 0 this._colorSubscription = void 0 this._speed = void 0 this._speedSubscription = void 0 this._image = void 0 this._imageSubscription = void 0 this._repeat = void 0 this._repeatSubscription = void 0 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, result) { 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) { 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.); vec4 colorImage=texture(image,vec2(fract(st.s-time),st.t)); // 修改 texture2D 为 texture material.alpha = colorImage.a * color.a; material.diffuse = (colorImage.rgb+color.rgb)/2.0; return material; } `, }, translucent: function () { return true }, }) export default ImageTrailMaterial //之前的航线材质编写 // 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; // } // `