优化demo
This commit is contained in:
@@ -2,20 +2,20 @@ const { ccclass, property } = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class LocalDiffusionCtrl extends cc.Component {
|
||||
private color: cc.Color = cc.Color.RED;
|
||||
private _centerPointPos: cc.Vec2 = cc.v2(0.5, 0.5);
|
||||
private _centerColor: cc.Color = cc.Color.RED;
|
||||
private _radius: number = 0.2;
|
||||
|
||||
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(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
|
||||
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, 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(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
|
||||
this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
|
||||
this.node.off("on_property_change", this._onPropertyChange, this);
|
||||
}
|
||||
|
||||
private _onTouchStart(event: cc.Event.EventTouch) {
|
||||
@@ -27,24 +27,29 @@ export default class LocalDiffusionCtrl extends cc.Component {
|
||||
let touchPointInNodeSpace = this.node.convertToNodeSpaceAR(touchPointInWorldSpace);
|
||||
|
||||
// 将触摸点转换为OPENGL坐标系并归一化
|
||||
let normalizePos = cc.v2(
|
||||
// OpenGl 坐标系原点在左上角
|
||||
this._centerPointPos = cc.v2(
|
||||
this.node.anchorX + touchPointInNodeSpace.x / this.node.width,
|
||||
1 - (this.node.anchorY + touchPointInNodeSpace.y / this.node.height)
|
||||
);
|
||||
|
||||
this._updateMaterial({
|
||||
centerColor: this.color,
|
||||
certerPoint: normalizePos,
|
||||
radius: 0.6
|
||||
centerColor: this._centerColor,
|
||||
certerPoint: this._centerPointPos,
|
||||
radius: this._radius
|
||||
});
|
||||
}
|
||||
|
||||
private _onTouchEnd(event: cc.Event.EventTouch) {
|
||||
this._onTouchCancel(event);
|
||||
private _onPropertyChange(color: cc.Color, radius: number) {
|
||||
this._centerColor = color;
|
||||
this._radius = radius;
|
||||
this._updateMaterial({
|
||||
centerColor: this._centerColor,
|
||||
certerPoint: this._centerPointPos,
|
||||
radius: this._radius
|
||||
});
|
||||
}
|
||||
|
||||
private _onTouchCancel(event: cc.Event.EventTouch) {}
|
||||
|
||||
private _updateMaterial(param: {
|
||||
/**
|
||||
* 中心点颜色
|
||||
@@ -63,11 +68,7 @@ export default class LocalDiffusionCtrl extends cc.Component {
|
||||
}) {
|
||||
this.getComponents(cc.RenderComponent).forEach(renderComponent => {
|
||||
let material: cc.Material = renderComponent.getMaterial(0);
|
||||
material.setProperty("centerColor", cc.v4(1.0, 1.0, 0.0, 1.0));
|
||||
material.setProperty(
|
||||
"centerColor",
|
||||
cc.v4(param.centerColor.getR() / 255, param.centerColor.getG() / 255, param.centerColor.getB() / 255, param.centerColor.getA() / 255)
|
||||
);
|
||||
material.setProperty("centerColor", param.centerColor);
|
||||
material.setProperty("centerPoint", param.certerPoint);
|
||||
material.setProperty("radius", param.radius);
|
||||
renderComponent.setMaterial(0, material);
|
||||
|
@@ -1,27 +1,65 @@
|
||||
import LocalDiffusionCtrl from "./LocalDiffusionCtrl";
|
||||
|
||||
const { ccclass, property } = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class LocalDiffusionEffectScene extends cc.Component {
|
||||
private _oldLevelSlider: cc.Slider = null;
|
||||
private _oldLevelSliderLabel: cc.Label = null;
|
||||
private _redSlider: cc.Slider = null;
|
||||
private _redSliderLabel: cc.Label = null;
|
||||
|
||||
private _greenSlider: cc.Slider = null;
|
||||
private _greenSliderLabel: cc.Label = null;
|
||||
|
||||
private _blueSlider: cc.Slider = null;
|
||||
private _blueSliderLabel: cc.Label = null;
|
||||
|
||||
private _alphaSlider: cc.Slider = null;
|
||||
private _alphaSliderLabel: cc.Label = null;
|
||||
|
||||
private _radiuSlider: cc.Slider = null;
|
||||
private _radiuSliderLabel: cc.Label = null;
|
||||
|
||||
private _examplesParentNode: cc.Node = null;
|
||||
|
||||
onLoad() {
|
||||
cc.dynamicAtlasManager.enabled = false;
|
||||
|
||||
this._oldLevelSlider = cc.find("Canvas/Content/Sliders/OldLevelSlider/Slider").getComponent(cc.Slider);
|
||||
this._oldLevelSliderLabel = cc.find("Canvas/Content/Sliders/OldLevelSlider/ValueLabel").getComponent(cc.Label);
|
||||
|
||||
this._redSlider = cc.find("Canvas/Content/Sliders/ColorRedSlider/Slider").getComponent(cc.Slider);
|
||||
this._redSliderLabel = cc.find("Canvas/Content/Sliders/ColorRedSlider/ValueLabel").getComponent(cc.Label);
|
||||
|
||||
this._greenSlider = cc.find("Canvas/Content/Sliders/ColorGreenSlider/Slider").getComponent(cc.Slider);
|
||||
this._greenSliderLabel = cc.find("Canvas/Content/Sliders/ColorGreenSlider/ValueLabel").getComponent(cc.Label);
|
||||
|
||||
this._blueSlider = cc.find("Canvas/Content/Sliders/ColorBlueSlider/Slider").getComponent(cc.Slider);
|
||||
this._blueSliderLabel = cc.find("Canvas/Content/Sliders/ColorBlueSlider/ValueLabel").getComponent(cc.Label);
|
||||
|
||||
this._alphaSlider = cc.find("Canvas/Content/Sliders/ColorAlphaSlider/Slider").getComponent(cc.Slider);
|
||||
this._alphaSliderLabel = cc.find("Canvas/Content/Sliders/ColorAlphaSlider/ValueLabel").getComponent(cc.Label);
|
||||
|
||||
this._radiuSlider = cc.find("Canvas/Content/Sliders/RadiuSlider/Slider").getComponent(cc.Slider);
|
||||
this._radiuSliderLabel = cc.find("Canvas/Content/Sliders/RadiuSlider/ValueLabel").getComponent(cc.Label);
|
||||
|
||||
// 代码添加控制脚本
|
||||
this._examplesParentNode = cc.find("Canvas/Content/Examples");
|
||||
this._examplesParentNode.children.forEach(childNode => {
|
||||
childNode.addComponent(LocalDiffusionCtrl);
|
||||
});
|
||||
}
|
||||
|
||||
onEnable() {
|
||||
this._oldLevelSlider.node.on("slide", this._onSliderChanged, this);
|
||||
this._redSlider.node.on("slide", this._onSliderChanged, this);
|
||||
this._greenSlider.node.on("slide", this._onSliderChanged, this);
|
||||
this._blueSlider.node.on("slide", this._onSliderChanged, this);
|
||||
this._alphaSlider.node.on("slide", this._onSliderChanged, this);
|
||||
this._radiuSlider.node.on("slide", this._onSliderChanged, this);
|
||||
}
|
||||
|
||||
onDisable() {
|
||||
this._oldLevelSlider.node.off("slide", this._onSliderChanged, this);
|
||||
this._redSlider.node.off("slide", this._onSliderChanged, this);
|
||||
this._greenSlider.node.off("slide", this._onSliderChanged, this);
|
||||
this._blueSlider.node.off("slide", this._onSliderChanged, this);
|
||||
this._alphaSlider.node.off("slide", this._onSliderChanged, this);
|
||||
this._radiuSlider.node.off("slide", this._onSliderChanged, this);
|
||||
}
|
||||
|
||||
start() {
|
||||
@@ -29,54 +67,25 @@ export default class LocalDiffusionEffectScene extends cc.Component {
|
||||
}
|
||||
|
||||
private _onSliderChanged() {
|
||||
this._oldLevelSliderLabel.string = `${this._oldLevelSlider.progress.toFixed(2)}`;
|
||||
// 更新进度条值 Label 文本
|
||||
this._redSliderLabel.string = `${this._redSlider.progress.toFixed(2)} | ${Math.round(255 * this._redSlider.progress)}`;
|
||||
this._greenSliderLabel.string = `${this._greenSlider.progress.toFixed(2)} | ${Math.round(255 * this._greenSlider.progress)}`;
|
||||
this._blueSliderLabel.string = `${this._blueSlider.progress.toFixed(2)} | ${Math.round(255 * this._blueSlider.progress)}`;
|
||||
this._alphaSliderLabel.string = `${this._alphaSlider.progress.toFixed(2)} | ${Math.round(255 * this._alphaSlider.progress)}`;
|
||||
this._radiuSliderLabel.string = `${this._radiuSlider.progress.toFixed(2)}`;
|
||||
|
||||
// // 更新材质
|
||||
// this._updateRenderComponentMaterial({
|
||||
// oldLevel: this._oldLevelSlider.progress
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新渲染组件的材质
|
||||
*
|
||||
* 1. 获取材质
|
||||
* 2. 给材质的 unitform 变量赋值
|
||||
* 3. 重新将材质赋值回去
|
||||
*/
|
||||
private _updateRenderComponentMaterial(param: {
|
||||
/**
|
||||
* 中心点颜色
|
||||
*/
|
||||
centerColor: cc.Color;
|
||||
|
||||
/**
|
||||
* 中心点坐标 ([0.0, 1.0], [0.0, 1.0])
|
||||
*/
|
||||
certerPoint: cc.Vec2;
|
||||
|
||||
/**
|
||||
* 扩散半径 [0.0, 1.0]
|
||||
*/
|
||||
radius: number;
|
||||
}) {
|
||||
// 通知子节点更新材质
|
||||
this._examplesParentNode.children.forEach(childNode => {
|
||||
childNode.getComponents(cc.RenderComponent).forEach(renderComponent => {
|
||||
let material: cc.Material = renderComponent.getMaterial(0);
|
||||
// material.setProperty("centerColor", cc.v4(1.0, 1.0, 0.0, 1.0));
|
||||
material.setProperty(
|
||||
"centerColor",
|
||||
cc.v4(
|
||||
param.centerColor.getR() / 255,
|
||||
param.centerColor.getG() / 255,
|
||||
param.centerColor.getB() / 255,
|
||||
param.centerColor.getA() / 255
|
||||
)
|
||||
);
|
||||
material.setProperty("centerPoint", param.certerPoint);
|
||||
material.setProperty("radius", param.radius);
|
||||
renderComponent.setMaterial(0, material);
|
||||
});
|
||||
childNode.emit(
|
||||
"on_property_change",
|
||||
cc.color(
|
||||
Math.round(255 * this._redSlider.progress),
|
||||
Math.round(255 * this._greenSlider.progress),
|
||||
Math.round(255 * this._blueSlider.progress),
|
||||
Math.round(255 * this._alphaSlider.progress)
|
||||
),
|
||||
this._radiuSlider.progress
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user