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

97 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-12-26 15:28:27 +00:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class MosaicEffectScene extends cc.Component {
private _xMosaicCountSlider: cc.Slider = null;
private _xMosaicCountSliderLabel: cc.Label = null;
private _yMosaicCountSlider: cc.Slider = null;
private _yMosaicCountSliderLabel: cc.Label = null;
2020-01-01 08:31:44 +00:00
private _mosaicCountSlider: cc.Slider = null;
private _mosaicCountSliderLabel: cc.Label = null;
2019-12-26 15:28:27 +00:00
private _examplesParentNode: cc.Node = null;
onLoad() {
// 关闭动态合图
cc.dynamicAtlasManager.enabled = false;
this._xMosaicCountSlider = cc.find("Canvas/Content/Sliders/XMosaicCountSlider/Slider").getComponent(cc.Slider);
this._xMosaicCountSliderLabel = cc.find("Canvas/Content/Sliders/XMosaicCountSlider/ValueLabel").getComponent(cc.Label);
this._yMosaicCountSlider = cc.find("Canvas/Content/Sliders/YMosaicCountSlider/Slider").getComponent(cc.Slider);
this._yMosaicCountSliderLabel = cc.find("Canvas/Content/Sliders/YMosaicCountSlider/ValueLabel").getComponent(cc.Label);
2020-01-01 08:31:44 +00:00
this._mosaicCountSlider = cc.find("Canvas/Content/Sliders/MosaicCountSlider/Slider").getComponent(cc.Slider);
this._mosaicCountSliderLabel = cc.find("Canvas/Content/Sliders/MosaicCountSlider/ValueLabel").getComponent(cc.Label);
2019-12-26 15:28:27 +00:00
this._examplesParentNode = cc.find("Canvas/Content/Examples");
}
onEnable() {
this._xMosaicCountSlider.node.on("slide", this._onSliderChanged, this);
this._yMosaicCountSlider.node.on("slide", this._onSliderChanged, this);
2020-01-01 08:31:44 +00:00
this._mosaicCountSlider.node.on("slide", this._onSliderChangedTogether, this);
2019-12-26 15:28:27 +00:00
}
onDisable() {
this._xMosaicCountSlider.node.off("slide", this._onSliderChanged, this);
this._yMosaicCountSlider.node.off("slide", this._onSliderChanged, this);
2020-01-01 08:31:44 +00:00
this._mosaicCountSlider.node.off("slide", this._onSliderChangedTogether, this);
2019-12-26 15:28:27 +00:00
}
start() {
this._onSliderChanged();
}
2020-01-01 08:31:44 +00:00
private _onSliderChangedTogether() {
let mosaicCount = Math.round(this._mosaicCountSlider.progress * 300);
this._mosaicCountSliderLabel.string = `${mosaicCount}`;
// 更新材质
this._updateRenderComponentMaterial({
xBlockCount: mosaicCount,
yBlockCount: mosaicCount
});
}
2019-12-26 15:28:27 +00:00
private _onSliderChanged() {
2019-12-27 15:08:32 +00:00
let xMosaicCount = Math.round(this._xMosaicCountSlider.progress * 300);
2019-12-26 15:28:27 +00:00
this._xMosaicCountSliderLabel.string = `${xMosaicCount}`;
2019-12-27 15:08:32 +00:00
let yMosaicCount = Math.round(this._yMosaicCountSlider.progress * 300);
2019-12-26 15:28:27 +00:00
this._yMosaicCountSliderLabel.string = `${yMosaicCount}`;
// 更新材质
this._updateRenderComponentMaterial({
xBlockCount: xMosaicCount,
yBlockCount: yMosaicCount
});
}
/**
*
*
* 1.
* 2. unitform
* 3.
*/
private _updateRenderComponentMaterial(param: {
/**
* X轴方块数量 [1.0, ]
*/
xBlockCount: number;
/**
* Y轴方块数量 [1.0, ]
*/
yBlockCount: number;
}) {
this._examplesParentNode.children.forEach(childNode => {
childNode.getComponents(cc.RenderComponent).forEach(renderComponent => {
let material: cc.Material = renderComponent.getMaterial(0);
material.setProperty("xBlockCount", param.xBlockCount);
material.setProperty("yBlockCount", param.yBlockCount);
renderComponent.setMaterial(0, material);
});
});
}
}