class RainEffect {
|
constructor(viewer, options) {
|
if (!viewer) throw new Error("no viewer object!");
|
options = options || {};
|
//倾斜角度,负数向右,正数向左
|
this.tiltAngle = Cesium.defaultValue(options.tiltAngle, -0.6);
|
this.rainSize = Cesium.defaultValue(options.rainSize, 0.3);
|
this.rainSpeed = Cesium.defaultValue(options.rainSpeed, 60.0);
|
this.rainDensity = Cesium.defaultValue(options.rainDensity, 30.0);
|
this.color = Cesium.defaultValue(options.color, new Cesium.Color(0.6, 0.7, 0.8, 1.0));
|
this.viewer = viewer;
|
this.init();
|
}
|
|
init() {
|
this.rainStage = new Cesium.PostProcessStage({
|
name: "czm_rain",
|
fragmentShader: this.rain(),
|
uniforms: {
|
tiltAngle: () => {
|
return this.tiltAngle;
|
},
|
rainSize: () => {
|
return this.rainSize;
|
},
|
rainColor: () => {
|
return this.color;
|
},
|
rainSpeed: () => {
|
return this.rainSpeed;
|
},
|
rainDensity: () => {
|
return this.rainDensity;
|
},
|
},
|
});
|
this.viewer.scene.postProcessStages.add(this.rainStage);
|
}
|
|
destroy() {
|
if (!this.viewer || !this.rainStage) return;
|
this.viewer.scene.postProcessStages.remove(this.rainStage);
|
const isDestroyed = this.rainStage.isDestroyed();
|
// 先检查是否被销毁过,如果已经被销毁过再调用destroy会报错
|
if (!isDestroyed) {
|
this.rainStage.destroy();
|
}
|
delete this.tiltAngle;
|
delete this.rainSize;
|
delete this.rainSpeed;
|
delete this.rainDensity;
|
}
|
|
show(visible) {
|
this.rainStage.enabled = visible;
|
}
|
rain() {
|
return "uniform sampler2D colorTexture;\n\
|
in vec2 v_textureCoordinates;\n\
|
uniform float tiltAngle;\n\
|
uniform vec4 rainColor;\n\
|
uniform float rainSize;\n\
|
uniform float rainSpeed;\n\
|
uniform float rainDensity;\n\
|
float hash(float x) {\n\
|
return fract(sin(x * 133.3) * 13.13);\n\
|
}\n\
|
out vec4 fragColor;\n\
|
void main(void) {\n\
|
float time = czm_frameNumber * (rainSpeed / 10000.0);\n\
|
vec2 resolution = czm_viewport.zw;\n\
|
vec2 uv = (gl_FragCoord.xy * 2. - resolution.xy) / min(resolution.x, resolution.y);\n\
|
vec3 c = rainColor.rgb;\n\
|
float a = tiltAngle;\n\
|
float si = sin(a), co = cos(a);\n\
|
uv *= mat2(co, -si, si, co);\n\
|
uv *= length(uv + vec2(0, 4.9)) / (rainSize * 3.0 + 0.1) + 0.5;\n\
|
// 限制最大密度值,使用对数函数来压缩高密度范围\n\
|
float cappedDensity = min(rainDensity, 30.0 + (rainDensity - 30.0) * 0.1);\n\
|
float densityScale = pow(cappedDensity / 200.0, 0.4); // 使用0.4次幂进一步压缩\n\
|
// 减少基础雨滴数量\n\
|
float v = 1. - sin(hash(floor(uv.x * (25.0 + densityScale * 50.0))) * 2.);\n\
|
// 使用更严格的阈值函数,让高密度时增加更少的雨\n\
|
float densityThreshold = 0.96 - (0.15 * log(1.0 + cappedDensity / 50.0));\n\
|
float b = clamp(abs(sin(20. * time * v + uv.y * (5. / (2. + v)))) - densityThreshold, 0., 1.) * 20.;\n\
|
// 计算雨滴颜色\n\
|
vec3 finalColor = rainColor.rgb * v * b;\n\
|
// 调整混合系数\n\
|
float mixFactor = min(b * 0.05, 0.5);\n\
|
fragColor = mix(texture(colorTexture, v_textureCoordinates), vec4(finalColor, 1), mixFactor);\n\
|
}\n\
|
";
|
}
|
}
|
|
export default RainEffect;
|