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

81 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-01-16 15:27:09 +00:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class RoundCornerCropV2EffectScene extends cc.Component {
private _radiuSlider: cc.Slider = null;
private _radiuLabel: cc.Label = null;
private _examplesParentNode: cc.Node = null;
onLoad() {
// 关闭动态合图
cc.dynamicAtlasManager.enabled = false;
this._radiuSlider = cc.find("Canvas/Content/Controller/RadiusSlider/Slider").getComponent(cc.Slider);
this._radiuLabel = cc.find("Canvas/Content/Controller/RadiusSlider/ValueLabel").getComponent(cc.Label);
this._examplesParentNode = cc.find("Canvas/Content/Examples");
}
onEnable() {
this._radiuSlider.node.on("slide", this._onSliderChanged, this);
}
onDisable() {
this._radiuSlider.node.off("slide", this._onSliderChanged, this);
}
start() {
this._onSliderChanged();
}
private _onSliderChanged() {
// 计算半径px
let radiusInPx = Math.floor(100 * this._radiuSlider.progress);
this._radiuLabel.string = radiusInPx + "";
// 更新材质
this._updateRenderComponentMaterial({
radiusInPx: radiusInPx
});
}
/**
*
*
* 1.
* 2. unitform
* 3.
*/
private _updateRenderComponentMaterial(param: {
/**
* px
*/
radiusInPx: number;
}) {
this._examplesParentNode.children.forEach(childNode => {
childNode.getComponents(cc.RenderComponent).forEach(renderComponent => {
// 计算半径px分别相对于纹理宽高的比例也叫归一化
2020-01-16 15:33:46 +00:00
let xRadiux = param.radiusInPx / childNode.width;
// 约束范围在区间 [0.0, 0.5]
2020-01-16 15:27:09 +00:00
xRadiux = xRadiux >= 0.5 ? 0.5 : xRadiux;
2020-01-16 15:33:46 +00:00
let yRadius = param.radiusInPx / childNode.height;
2020-01-16 15:27:09 +00:00
yRadius = yRadius >= 0.5 ? 0.5 : yRadius;
2020-01-16 15:33:46 +00:00
if (childNode.name === "Rectangle1") cc.log(`${childNode.name} : (${xRadiux}, ${yRadius})`);
2020-01-16 15:27:09 +00:00
// 更新材质
let material: cc.Material = renderComponent.getMaterial(0);
// 圆角x轴半径长度相对于纹理宽度[0.0, 0.5]
material.setProperty("xRadius", xRadiux);
// 圆角y轴半径长度相对于纹理高度[0.0, 0.5]
material.setProperty("yRadius", yRadius);
renderComponent.setMaterial(0, material);
});
});
}
}