Files
sdk4.0/src/Obj/Materail/PolylineFlowMaterialProperty.js

132 lines
4.1 KiB
JavaScript
Raw Normal View History

2025-07-03 16:28:48 +08:00
/*
* @Description: 流动线
*/
function PolylineFlow() {
class PolylineFlowMaterialProperty {
constructor(options) {
this._definitionChanged = new Cesium.Event();
this._color = undefined;
this._speed = undefined;
2025-07-07 16:21:01 +08:00
this._rotate = undefined;
2025-07-03 16:28:48 +08:00
this.color = new Cesium.Color.fromCssColorString(options.color || "rgba(255,255,255,1)");
2025-07-07 16:21:01 +08:00
this.speed = options.speed != undefined ? options.speed : 1.0;//速度
2025-07-03 16:28:48 +08:00
this.lineBackAlpha = options.lineBackAlpha || 0.05;
2025-07-07 16:21:01 +08:00
this.rotate = options.rotate;
2025-07-03 16:28:48 +08:00
}
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
);
2025-07-07 16:21:01 +08:00
result.rotate = Cesium.Property.getValueOrDefault(
this._rotate,
time,
true,
result.rotate
);
2025-07-03 16:28:48 +08:00
result.lineBackAlpha = this.lineBackAlpha;
2025-07-09 11:26:33 +08:00
result.frameTime = Cesium.getTimestamp();
// result.frameNumber = Cesium.getTimestamp();
2025-07-03 16:28:48 +08:00
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"),
2025-07-07 16:21:01 +08:00
rotate: Cesium.createPropertyDescriptor("rotate"),
2025-07-03 16:28:48 +08:00
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);
//获取stuv
vec2 st = materialInput.st;
//获取当前帧数10秒内变化0-1
2025-07-07 16:21:01 +08:00
// float time = fract(czm_frameNumber * speed / 60.0);
2025-07-09 11:26:33 +08:00
// float time = fract(abs(speed) * czm_frameNumber * 0.01);
float time = fract(frameTime / 1000.0 / abs(speed));
2025-07-03 16:28:48 +08:00
//长度1/10
2025-07-07 16:21:01 +08:00
// 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);
2025-07-03 16:28:48 +08:00
//平滑过渡函数
2025-07-07 16:21:01 +08:00
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;
2025-07-03 16:28:48 +08:00
//光带轨迹(不会完全透明)
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,
2025-07-07 16:21:01 +08:00
rotate: true,
2025-07-09 11:26:33 +08:00
frameTime: Cesium.getTimestamp(),
2025-07-03 16:28:48 +08:00
lineBackAlpha: 0.05,
},
source: Cesium.Material.PolylineFlowMaterialSource,
},
translucent: function (material) {
return true;
},
}
);
}
export { PolylineFlow }