其他
[Cocos Creator] 一个全能的挖孔 Shader
本文由“壹伴编辑器”提供技术支
前言
本文由“壹伴编辑器”提供技术支
效果展示
protected onLoad() {
this.startBtn.on('touchend', this.onStartBtnClick, this);
this.oneBtn.on('touchend', this.onOneBtnClick, this);
this.twoBtn.on('touchend', this.onTwoBtnClick, this);
}
protected start() {
this.hollowOut.nodeSize(); // 将遮罩镂空设为节点大小
this.touchBlocker.setTarget(this.startBtn); // 设置可点击节点
}
private async onStartBtnClick() {
this.touchBlocker.blockAll(); // 屏蔽所有点击
await this.hollowOut.rectTo(1, this.oneBtn.getPosition(), this.oneBtn.width + 10, this.oneBtn.height + 10, 5, 5);
this.touchBlocker.setTarget(this.oneBtn); // 设置可点击节点
}
private async onOneBtnClick() {
this.hollowOut.nodeSize(); // 将遮罩镂空设为节点大小
this.touchBlocker.blockAll(); // 屏蔽所有点击
await this.hollowOut.rectTo(1, this.twoBtn.getPosition(), this.twoBtn.width + 10, this.twoBtn.height + 10, 5, 5);
this.touchBlocker.setTarget(this.twoBtn); // 设置可点击节点
}
private onTwoBtnClick() {
this.hollowOut.nodeSize(); // 将遮罩镂空设为节点大小
this.touchBlocker.passAll(); // 放行所有点击
}
本文由“壹伴编辑器”提供技术支
整体思路
代码实现
cc.dynamicAtlasManager.enabled = false;
CCProgram fs %{
precision highp float;
in vec2 v_uv0;
in vec4 v_color;
uniform sampler2D texture;
uniform BaseParams {
vec2 center;
float ratio;
};
uniform RectParams {
float width;
float height;
float round;
float feather;
};
void main () {
vec4 color = v_color;
color *= texture(texture, v_uv0);
// 边缘
float minX = center.x - (width / 2.0);
float maxX = center.x + (width / 2.0);
float minY = center.y - (height * ratio / 2.0);
float maxY = center.y + (height * ratio / 2.0);
if (v_uv0.x >= minX && v_uv0.x <= maxX && v_uv0.y >= minY && v_uv0.y <= maxY) {
if (round == 0.0) discard; // 没有圆角则直接丢弃
// 圆角处理
float roundY = round * ratio;
vec2 vertex;
if (v_uv0.x <= minX + round) {
if (v_uv0.y <= minY + roundY) {
vertex = vec2(minX + round, (minY + roundY) / ratio); // 左上角
} else if (v_uv0.y >= maxY - roundY) {
vertex = vec2(minX + round, (maxY - roundY) / ratio); // 左下角
} else {
vertex = vec2(minX + round, v_uv0.y / ratio); // 左中
}
} else if (v_uv0.x >= maxX - round) {
if (v_uv0.y <= minY + roundY){
vertex = vec2(maxX - round, (minY + roundY) / ratio); // 右上角
} else if (v_uv0.y >= maxY - roundY) {
vertex = vec2(maxX - round, (maxY - roundY) / ratio); // 右下角
} else {
vertex = vec2(maxX - round, v_uv0.y / ratio); // 右中
}
} else if (v_uv0.y <= minY + roundY) {
vertex = vec2(v_uv0.x, (minY + roundY) / ratio); // 上中
} else if (v_uv0.y >= maxY - roundY) {
vertex = vec2(v_uv0.x, (maxY - roundY) / ratio); // 下中
} else {
discard; // 中间
}
float dis = distance(vec2(v_uv0.x, v_uv0.y / ratio), vertex);
color.a = smoothstep(round - feather, round, dis);
} else {
color.a = 1.0;
}
color.a *= v_color.a;
gl_FragColor = color;
}
}%
* 渲染
* @param keepUpdating 是否每帧自动更新
*/
private render(keepUpdating: boolean) {
if (!this.material) this.getMaterial();
switch (this.shape) {
case Shape.Rect:
this.rect(this.center, this.width, this.height, this.round, this.feather, keepUpdating);
break;
case Shape.Circle:
this.circle(this.center, this.radius, this.feather, keepUpdating);
break;
}
}
/**
* 矩形镂空
* @param center 中心坐标
* @param width 宽
* @param height 高
* @param round 圆角半径
* @param feather 边缘虚化宽度
* @param keepUpdating 是否每帧自动更新
*/
public rect(center?: cc.Vec2, width?: number, height?: number, round?: number, feather?: number, keepUpdating: boolean = false) {
this.shape = Shape.Rect;
if (center !== null) this.center = center;
if (width !== null) this.width = width;
if (height !== null) this.height = height;
if (round !== null) {
this.round = round >= 0 ? round : 0;
let min = Math.min(this.width / 2, this.height / 2);
this.round = this.round <= min ? this.round : min;
}
if (feather !== null) {
this.feather = feather >= 0 ? feather : 0;
this.feather = this.feather <= this.round ? this.feather : this.round;
}
this.material.setProperty('ratio', this.getRatio());
this.material.setProperty('center', this.getCenter(this.center));
this.material.setProperty('width', this.getWidth(this.width));
this.material.setProperty('height', this.getHeight(this.height));
this.material.setProperty('round', this.getRound(this.round));
this.material.setProperty('feather', this.getFeather(this.feather));
this.keepUpdating = keepUpdating;
}
/**
* 圆形镂空
* @param center 中心坐标
* @param radius 半径
* @param feather 边缘虚化宽度
* @param keepUpdating 是否每帧自动更新
*/
public circle(center?: cc.Vec2, radius?: number, feather?: number, keepUpdating: boolean = false) {
this.shape = Shape.Circle;
if (center !== null) this.center = center;
if (radius !== null) this.radius = radius;
if (feather !== null) this.feather = feather >= 0 ? feather : 0;
this.material.setProperty('ratio', this.getRatio());
this.material.setProperty('center', this.getCenter(this.center));
this.material.setProperty('width', this.getWidth(this.radius * 2));
this.material.setProperty('height', this.getHeight(this.radius * 2));
this.material.setProperty('round', this.getRound(this.radius));
this.material.setProperty('feather', this.getFeather(this.feather));
this.keepUpdating = keepUpdating;
}
/**
* 缓动镂空(矩形)
* @param time 时间
* @param center 中心坐标
* @param width 宽
* @param height 高
* @param round 圆角半径
* @param feather 边缘虚化宽度
*/
public rectTo(time: number, center: cc.Vec2, width: number, height: number, round: number = 0, feather: number = 0): Promise<void> {
return new Promise(res => {
cc.Tween.stopAllByTarget(this);
this.tweenRes && this.tweenRes();
this.tweenRes = res;
if (round > width / 2) round = width / 2;
if (round > height / 2) round = height / 2;
if (feather > round) feather = round;
this.shape = Shape.Rect;
cc.tween<HollowOut>(this)
.call(() => this.keepUpdating = true)
.to(time, {
center: center,
width: width,
height: height,
round: round,
feather: feather
})
.call(() => {
this.scheduleOnce(() => {
this.keepUpdating = false;
this.tweenRes();
this.tweenRes = null;
});
})
.start();
});
}
/**
* 缓动镂空(圆形)
* @param time 时间
* @param center 中心坐标
* @param radius 半径
* @param feather 边缘虚化宽度
*/
public circleTo(time: number, center: cc.Vec2, radius: number, feather: number = 0): Promise<void> {
return new Promise(res => {
cc.Tween.stopAllByTarget(this);
this.tweenRes && this.tweenRes();
this.tweenRes = res;
this.shape = Shape.Circle;
cc.tween<HollowOut>(this)
.call(() => this.keepUpdating = true)
.to(time, {
center: center,
radius: radius,
feather: feather
})
.call(() => {
this.scheduleOnce(() => {
this.keepUpdating = false;
this.tweenRes();
this.tweenRes = null;
});
})
.start();
});
}
/**
* 取消所有挖孔
*/
public reset() {
this.rect(cc.v2(), 0, 0, 0, 0);
}
/**
* 挖孔设为节点大小(就整个都挖没了)
*/
public nodeSize() {
this.rect(this.node.getPosition(), this.node.width, this.node.height, 0, 0);
}
/**
* 获取中心点
* @param center
*/
private getCenter(center: cc.Vec2) {
let x = (center.x + (this.node.width / 2)) / this.node.width;
let y = (-center.y + (this.node.height / 2)) / this.node.height;
return cc.v2(x, y);
}
/**
* 获取节点宽高比
*/
private getRatio() {
return this.node.width / this.node.height;
}
/**
* 获取挖孔宽度
* @param width
*/
private getWidth(width: number) {
return width / this.node.width;
}
/**
* 获取挖孔高度
* @param height
*/
private getHeight(height: number) {
return height / this.node.width;
}
/**
* 获取圆角半径
* @param round
*/
private getRound(round: number) {
return round / this.node.width;
}
/**
* 获取边缘虚化宽度
* @param feather
*/
private getFeather(feather: number) {
return feather / this.node.width;
}
使用方法
本文由“壹伴编辑器”提供技术支
结束语
本文由“壹伴编辑器”提供技术支
扫描二维码
获取更多精彩
文弱书生陈皮皮