132 lines
4.1 KiB
JavaScript
132 lines
4.1 KiB
JavaScript
/*
|
||
* @Description: 流动线
|
||
*/
|
||
function PolylineFlow() {
|
||
class PolylineFlowMaterialProperty {
|
||
constructor(options) {
|
||
this._definitionChanged = new Cesium.Event();
|
||
this._color = undefined;
|
||
this._speed = undefined;
|
||
this._rotate = undefined;
|
||
this.color = new Cesium.Color.fromCssColorString(options.color || "rgba(255,255,255,1)");
|
||
this.speed = options.speed != undefined ? options.speed : 1.0;//速度
|
||
this.lineBackAlpha = options.lineBackAlpha || 0.05;
|
||
this.rotate = options.rotate;
|
||
}
|
||
|
||
get isConstant() {
|
||
return false;
|
||
}
|
||
|
||
get definitionChanged() {
|
||
return this._definitionChanged;
|
||
}
|
||
|
||
getType(time) {
|
||
return Cesium.Material.PolylineFlowMaterialType;
|
||
}
|
||
|
||
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,
|
||
10,
|
||
result.speed
|
||
);
|
||
result.rotate = Cesium.Property.getValueOrDefault(
|
||
this._rotate,
|
||
time,
|
||
true,
|
||
result.rotate
|
||
);
|
||
result.lineBackAlpha = this.lineBackAlpha;
|
||
result.frameTime = Cesium.getTimestamp();
|
||
// result.frameNumber = Cesium.getTimestamp();
|
||
return result;
|
||
}
|
||
|
||
equals(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.lineBackAlpha, other.lineBackAlpha))
|
||
);
|
||
}
|
||
}
|
||
|
||
Object.defineProperties(PolylineFlowMaterialProperty.prototype, {
|
||
color: Cesium.createPropertyDescriptor("color"),
|
||
speed: Cesium.createPropertyDescriptor("speed"),
|
||
rotate: Cesium.createPropertyDescriptor("rotate"),
|
||
transparency: Cesium.createPropertyDescriptor("lineBackAlpha"),
|
||
});
|
||
|
||
Cesium.PolylineFlowMaterialProperty = PolylineFlowMaterialProperty;
|
||
Cesium.Material.PolylineFlowMaterialProperty = "PolylineFlowMaterialProperty";
|
||
Cesium.Material.PolylineFlowMaterialType = "PolylineFlowMaterialType";
|
||
Cesium.Material.PolylineFlowMaterialSource = `
|
||
uniform vec4 color;
|
||
uniform float speed;
|
||
uniform float lineBackAlpha;
|
||
|
||
czm_material czm_getMaterial(czm_materialInput materialInput)
|
||
{
|
||
//生成默认的基础材质
|
||
czm_material material = czm_getDefaultMaterial(materialInput);
|
||
//获取st(uv)
|
||
vec2 st = materialInput.st;
|
||
//获取当前帧数,10秒内变化0-1
|
||
// float time = fract(czm_frameNumber * speed / 60.0);
|
||
// float time = fract(abs(speed) * czm_frameNumber * 0.01);
|
||
float time = fract(frameTime / 1000.0 / abs(speed));
|
||
//长度1/10
|
||
// time = time * (1.0 + 0.1);
|
||
float staticAlpha = rotate?smoothstep(0.0,1.0, 1.0-st.s) * step(-1.0,-(1.0-st.s)):smoothstep(0.0,1.0, st.s) * step(-1.0,-st.s);
|
||
|
||
//平滑过渡函数
|
||
float alpha1 = smoothstep(time-0.1,time,1.0-st.s) * step(-time,- (1.0-st.s));
|
||
float alpha2 = smoothstep(time-0.1,time,st.s) * step(-time,- st.s);
|
||
float alpha =(speed== 0.0)? staticAlpha:(speed < 0.0)?alpha2:alpha1;
|
||
//光带轨迹(不会完全透明)
|
||
alpha += lineBackAlpha;
|
||
material.alpha = alpha;
|
||
material.diffuse = color.rgb;
|
||
return material;
|
||
}
|
||
`;
|
||
|
||
Cesium.Material._materialCache.addMaterial(
|
||
Cesium.Material.PolylineFlowMaterialType,
|
||
{
|
||
fabric: {
|
||
type: Cesium.Material.PolylineFlowMaterialType,
|
||
uniforms: {
|
||
color: new Cesium.Color(1.0, 1.0, 1.0, 1.0),
|
||
speed: 0.1,
|
||
rotate: true,
|
||
frameTime: Cesium.getTimestamp(),
|
||
lineBackAlpha: 0.05,
|
||
},
|
||
source: Cesium.Material.PolylineFlowMaterialSource,
|
||
},
|
||
translucent: function (material) {
|
||
return true;
|
||
},
|
||
}
|
||
);
|
||
}
|
||
|
||
export { PolylineFlow }
|