114 lines
3.3 KiB
JavaScript
114 lines
3.3 KiB
JavaScript
/*
|
||
* @Description: 流动线
|
||
*/
|
||
function PolylineFlow() {
|
||
class PolylineFlowMaterialProperty {
|
||
constructor(options) {
|
||
this._definitionChanged = new Cesium.Event();
|
||
this._color = undefined;
|
||
this._speed = undefined;
|
||
this.color = new Cesium.Color.fromCssColorString(options.color || "rgba(255,255,255,1)");
|
||
this.speed = options.speed || 0.25;//速度
|
||
this.lineBackAlpha = options.lineBackAlpha || 0.05;
|
||
}
|
||
|
||
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.lineBackAlpha = this.lineBackAlpha;
|
||
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"),
|
||
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);
|
||
//长度1/10
|
||
time = time * (1.0 + 0.1);
|
||
//平滑过渡函数
|
||
float alpha = smoothstep(time-0.1,time,st.s) * step(-time,-st.s);
|
||
//光带轨迹(不会完全透明)
|
||
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,
|
||
lineBackAlpha: 0.05,
|
||
},
|
||
source: Cesium.Material.PolylineFlowMaterialSource,
|
||
},
|
||
translucent: function (material) {
|
||
return true;
|
||
},
|
||
}
|
||
);
|
||
}
|
||
|
||
export { PolylineFlow }
|