Cesium创建小汽车闯红灯后的闪光效果

406 阅读1分钟

功能

需基于cesium编写小汽车闯红灯后的闪光效果

代码

创建光亮遮罩层类BrightnessMask

function defaultValue(a, b) {
    if (a !== undefined && a !== null) {
        return a;
    }
    return b;
}
function defined(value) {
    return value !== undefined && value !== null;
}
defaultValue.EMPTY_OBJECT = Object.freeze({});
function BrightnessMask(options) {
    options = defaultValue(options, defaultValue.EMPTY_OBJECT);
    var _brightness = defaultValue(options.brightness, 1);
    Object.defineProperty(this, "brightness", {
        get: function () {
            return _brightness
        },
        set: function (value) {
            _brightness = !defined(value) ? 1 : value
            this.instance.uniforms.brightness = _brightness
        },
        enumerable:true
    })
    this.instance = _cesium.scene.postProcessStages.add(Cesium.PostProcessStageLibrary.createBrightnessStage())
    this.instance.enabled = true;
    this.instance.uniforms.brightness = this._brightness
}
BrightnessMask.prototype.destroy = function () {
    if(this.instance){
        _cesium.scene.postProcessStages.remove(this.instance)
    }
}

如何使用该类?

window.brightnessMask = new BrightnessMask();
var handler = null;
function blink() {
	brightnessMask.brightness = 2.0;
	var time = new Date().getTime();
	handler && handler();
	handler = _cesium.scene.preUpdate.addEventListener(function(){
		if (brightnessMask.brightness == 2.0 && new Date().getTime() - time > 16){
			brightnessMask.brightness = 1.0;
		}
	});
}

当汽车闯红灯时,直接调用blink()即可