CocosCreator-Shader-Effect-.../assets/scripts/FlashLightCtrlComponent.ts

98 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2020-01-13 06:32:24 +00:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class FlashLightCtrlComponent extends cc.Component {
private _flashLightUBO: FlashLightUBO = new FlashLightUBO();
onEnable() {
this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
this.node.on("on_property_change", this._onPropertyChange, this);
}
onDisable() {
this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
this.node.off("on_property_change", this._onPropertyChange, this);
}
private _onTouchStart(event: cc.Event.EventTouch) {
this._onTouchMove(event);
}
private _onTouchMove(event: cc.Event.EventTouch) {
let touchPointInWorldSpace = event.getLocation();
let touchPointInNodeSpace = this.node.convertToNodeSpaceAR(touchPointInWorldSpace);
// 将触摸点转换为OPENGL坐标系并归一化
// OpenGl 坐标系原点在左上角
2020-01-13 09:42:53 +00:00
this._flashLightUBO.lightCenterPoint = cc.v2(
2020-01-13 06:32:24 +00:00
this.node.anchorX + touchPointInNodeSpace.x / this.node.width,
1 - (this.node.anchorY + touchPointInNodeSpace.y / this.node.height)
);
this._updateMaterial();
}
private _onPropertyChange(localDiffusionUniform: FlashLightUBO) {
2020-01-13 09:42:53 +00:00
this._flashLightUBO.lightColor = localDiffusionUniform.lightColor;
this._flashLightUBO.lightAngle = localDiffusionUniform.lightAngle;
this._flashLightUBO.lightWidth = localDiffusionUniform.lightWidth;
this._flashLightUBO.enableGradient = localDiffusionUniform.enableGradient;
2020-01-13 06:32:24 +00:00
this._flashLightUBO.cropAlpha = localDiffusionUniform.cropAlpha;
this._flashLightUBO.enableFog = localDiffusionUniform.enableFog;
this._updateMaterial();
}
private _updateMaterial() {
this.getComponents(cc.RenderComponent).forEach(renderComponent => {
let material: cc.Material = renderComponent.getMaterial(0);
2020-01-13 09:42:53 +00:00
material.setProperty("lightColor", this._flashLightUBO.lightColor);
material.setProperty("lightCenterPoint", this._flashLightUBO.lightCenterPoint);
material.setProperty("lightAngle", this._flashLightUBO.lightAngle);
material.setProperty("lightWidth", this._flashLightUBO.lightWidth);
material.setProperty("enableGradient", this._flashLightUBO.enableGradient);
2020-01-13 06:32:24 +00:00
material.setProperty("cropAlpha", this._flashLightUBO.cropAlpha);
material.setProperty("enableFog", this._flashLightUBO.enableFog);
renderComponent.setMaterial(0, material);
});
}
}
export class FlashLightUBO {
/**
*
*/
2020-01-13 09:42:53 +00:00
lightColor: cc.Color = cc.Color.YELLOW;
2020-01-13 06:32:24 +00:00
/**
* ([0.0, 1.0], [0.0, 1.0])
*/
2020-01-13 09:42:53 +00:00
lightCenterPoint: cc.Vec2 = cc.v2(0.5, 0.5);
2020-01-13 06:32:24 +00:00
/**
2020-01-13 09:42:53 +00:00
* [0.0, 180.0]
2020-01-13 06:32:24 +00:00
*/
2020-01-13 09:42:53 +00:00
lightAngle: number = 45;
/**
2020-01-13 14:14:07 +00:00
* [0.0, +]
2020-01-13 09:42:53 +00:00
*/
lightWidth: number = 0.5;
/**
*
*/
enableGradient: boolean = true;
2020-01-13 06:32:24 +00:00
/**
*
*/
cropAlpha: boolean = true;
/**
*
*/
enableFog: boolean = false;
}