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

84 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-01-10 15:05:44 +00:00
const { ccclass, property } = cc._decorator;
@ccclass
2020-01-12 01:01:30 +00:00
export default class PointLightCtrlComponent extends cc.Component {
2020-01-13 14:40:10 +00:00
private _pointLightUBO: PointLightUBO = new PointLightUBO();
2020-01-10 15:05:44 +00:00
onEnable() {
this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
2020-01-11 10:58:30 +00:00
this.node.on("on_property_change", this._onPropertyChange, this);
2020-01-10 15:05:44 +00:00
}
onDisable() {
this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
2020-01-11 10:58:30 +00:00
this.node.off("on_property_change", this._onPropertyChange, this);
2020-01-10 15:05:44 +00:00
}
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坐标系并归一化
2020-01-11 10:58:30 +00:00
// OpenGl 坐标系原点在左上角
2020-01-13 14:40:10 +00:00
this._pointLightUBO.centerPoint = cc.v2(
2020-01-10 15:05:44 +00:00
this.node.anchorX + touchPointInNodeSpace.x / this.node.width,
1 - (this.node.anchorY + touchPointInNodeSpace.y / this.node.height)
);
2020-01-11 16:35:56 +00:00
this._updateMaterial();
2020-01-10 15:05:44 +00:00
}
2020-01-13 14:40:10 +00:00
private _onPropertyChange(pointLightUBO: PointLightUBO) {
this._pointLightUBO.centerColor = pointLightUBO.centerColor;
this._pointLightUBO.radius = pointLightUBO.radius;
this._pointLightUBO.cropAlpha = pointLightUBO.cropAlpha;
this._pointLightUBO.enableFog = pointLightUBO.enableFog;
2020-01-11 16:35:56 +00:00
this._updateMaterial();
2020-01-10 15:05:44 +00:00
}
2020-01-11 16:35:56 +00:00
private _updateMaterial() {
2020-01-10 15:05:44 +00:00
this.getComponents(cc.RenderComponent).forEach(renderComponent => {
let material: cc.Material = renderComponent.getMaterial(0);
2020-01-13 14:40:10 +00:00
material.setProperty("centerColor", this._pointLightUBO.centerColor);
material.setProperty("centerPoint", this._pointLightUBO.centerPoint);
material.setProperty("radius", this._pointLightUBO.radius);
material.setProperty("cropAlpha", this._pointLightUBO.cropAlpha);
material.setProperty("enableFog", this._pointLightUBO.enableFog);
2020-01-10 15:05:44 +00:00
renderComponent.setMaterial(0, material);
});
}
}
2020-01-11 16:35:56 +00:00
2020-01-13 14:40:10 +00:00
export class PointLightUBO {
2020-01-11 16:35:56 +00:00
/**
*
*/
centerColor: cc.Color = cc.Color.YELLOW;
/**
* ([0.0, 1.0], [0.0, 1.0])
*/
2020-01-13 14:40:10 +00:00
centerPoint: cc.Vec2 = cc.v2(0.5, 0.5);
2020-01-11 16:35:56 +00:00
/**
* [0.0, 1.0]
*/
radius: number = 0.5;
/**
*
*/
cropAlpha: boolean = true;
2020-01-12 00:05:29 +00:00
/**
*
*/
enableFog: boolean = false;
2020-01-11 16:35:56 +00:00
}