完善圆角裁剪demo

This commit is contained in:
caizhitao 2020-01-16 23:05:32 +08:00
parent c64f1ab144
commit bfe61e6494
13 changed files with 2025 additions and 128 deletions

View File

@ -1,5 +1,7 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
// 圆角裁剪
//
// 圆角裁剪(仅支持正方形纹理)
//
// 原理https://www.cnblogs.com/jqm304775992/p/4987793.html
// 代码:复制 yanjifa/shaderDemor 的 https://github.com/yanjifa/shaderDemo/blob/master/assets/Effect/CircleAvatar.effect
@ -17,7 +19,7 @@ CCEffect %{
texture: { value: white }
alphaThreshold: { value: 0.5 }
# 圆角半径
roundCornerRadius: {
radius: {
value: 0.1,
inspector: {
tooltip: "圆角半径",
@ -77,7 +79,7 @@ CCProgram fs %{
#if ENABLE_ROUNDCORNER
uniform RoundCorner {
// 圆角半径
float roundCornerRadius;
float radius;
}
#endif
@ -97,7 +99,7 @@ CCProgram fs %{
#if ENABLE_ROUNDCORNER
// 约束圆角半径范围在 [0.0, 0.5]
float radius = clamp(0.0, 0.5, roundCornerRadius);
float circleRadius = clamp(0.0, 0.5, radius);
// 将纹理uv往左上偏移实现偏移后的坐标系原点在纹理中心
vec2 uv = v_uv0.xy - vec2(0.5, 0.5);
@ -106,8 +108,8 @@ CCProgram fs %{
// abs(uv.x) , abs(uv.y) : 将第二、三、四象限的点都投影到第一象限上,这样子只需要处理第一象限的情况就可以,简化判断
// 0.5 - radius : 计算出第一象限的圆角所在圆的圆心坐标
// (rx, ry) : 偏移映射后的 新的uv 坐标,相对于 第一象限圆角坐在圆心坐标 的相对坐标
float rx = abs(uv.x) - (0.5 - radius);
float ry = abs(uv.y) - (0.5 - radius);
float rx = abs(uv.x) - (0.5 - circleRadius);
float ry = abs(uv.y) - (0.5 - circleRadius);
// 区分 以第一象限圆角所在圆心坐标为原点的坐标的四个象限
//
@ -117,26 +119,26 @@ CCProgram fs %{
// 第四象限 mx = 1, my = 0
//
// 当 mx * my 时只要等于1那就是标识第一象限实际对应圆角区域所在矩形否则就是第二、三、四象限
float mx = step(0.5 - radius, abs(uv.x));
float my = step(0.5 - radius, abs(uv.y));
float mx = step(0.5 - circleRadius, abs(uv.x));
float my = step(0.5 - circleRadius, abs(uv.y));
// 计算相对uv坐标到圆心的距离
float len = length(vec2(rx, ry));
// mx * my = 0 时代表非圆角区域a 值为1代表完全采用原始纹理的透明度
// mx * my = 1 时,代表园所所在矩形区域
// step(radius, len) 可以区分出圆角所在矩形区域的 圆角区域 和 非圆角区域
// step(circleRadius, len) 可以区分出圆角所在矩形区域的 圆角区域 和 非圆角区域
// 其中圆角区域值为0非圆角区域值为1
// 当为圆角区域时a 值为1代表完全采用原始纹理透明度
// 当为非圆角区域时a 值为0代表完全透明
// 至此已经实现圆角裁剪
//
// smoothstep(0., radius * 0.01, len - radius) 是用于抗锯齿优化
// smoothstep(0., circleRadius * 0.01, len - circleRadius) 是用于抗锯齿优化
// 原理:针对点在非圆角区域的情况,针对点在大于「圆半径一点点」地方的区域,进行平滑过渡,以实现抗锯齿
// 其中,
// 「圆半径一点点」用 radius * 0.01 表示0.01 可自行改变)
// 点在大于圆半径的区域用 len - radius ,此值会在 [0.0, radius * 0.01] 之间时会平滑过渡
float a = 1.0 - mx * my * step(radius, len) * smoothstep(0., radius * 0.01, len - radius);
// 「圆半径一点点」用 circleRadius * 0.01 表示0.01 可自行改变)
// 点在大于圆半径的区域用 len - circleRadius ,此值会在 [0.0, circleRadius * 0.01] 之间时会平滑过渡
float a = 1.0 - mx * my * step(circleRadius, len) * smoothstep(0., circleRadius * 0.01, len - circleRadius);
o = vec4(o.rgb, o.a * a);
#endif
gl_FragColor = o;

View File

@ -5,11 +5,11 @@
{
"glsl1": {
"vert": "\nprecision highp float;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matWorld;\n\nattribute vec3 a_position;\nattribute vec4 a_color;\nvarying vec4 v_color;\n\n#if USE_TEXTURE\nattribute vec2 a_uv0;\nvarying vec2 v_uv0;\n#endif\n\nvoid main () {\n vec4 pos = vec4(a_position, 1);\n\n #if CC_USE_MODEL\n pos = cc_matViewProj * cc_matWorld * pos;\n #else\n pos = cc_matViewProj * pos;\n #endif\n\n #if USE_TEXTURE\n v_uv0 = a_uv0;\n #endif\n\n v_color = a_color;\n\n gl_Position = pos;\n}\n",
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform float alphaThreshold;\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nvarying vec4 v_color;\n\n#if USE_TEXTURE\nvarying vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_ROUNDCORNER\nuniform float roundCornerRadius;\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture2D(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n #if ENABLE_ROUNDCORNER\n\n float radius = clamp(0.0, 0.5, roundCornerRadius);\n\n vec2 uv = v_uv0.xy - vec2(0.5, 0.5);\n\n float rx = abs(uv.x) - (0.5 - radius);\n float ry = abs(uv.y) - (0.5 - radius);\n\n float mx = step(0.5 - radius, abs(uv.x));\n float my = step(0.5 - radius, abs(uv.y));\n\n float len = length(vec2(rx, ry));\n\n float a = 1.0 - mx * my * step(radius, len) * smoothstep(0., radius * 0.01, len - radius);\n o = vec4(o.rgb, o.a * a);\n #endif\n gl_FragColor = o;\n}\n"
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform float alphaThreshold;\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nvarying vec4 v_color;\n\n#if USE_TEXTURE\nvarying vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_ROUNDCORNER\nuniform float radius;\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture2D(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n #if ENABLE_ROUNDCORNER\n\n float circleRadius = clamp(0.0, 0.5, radius);\n\n vec2 uv = v_uv0.xy - vec2(0.5, 0.5);\n\n float rx = abs(uv.x) - (0.5 - circleRadius);\n float ry = abs(uv.y) - (0.5 - circleRadius);\n\n float mx = step(0.5 - circleRadius, abs(uv.x));\n float my = step(0.5 - circleRadius, abs(uv.y));\n\n float len = length(vec2(rx, ry));\n\n float a = 1.0 - mx * my * step(circleRadius, len) * smoothstep(0., circleRadius * 0.01, len - circleRadius);\n o = vec4(o.rgb, o.a * a);\n #endif\n gl_FragColor = o;\n}\n"
},
"glsl3": {
"vert": "\nprecision highp float;\nuniform CCGlobal {\n vec4 cc_time;\n\n vec4 cc_screenSize;\n\n vec4 cc_screenScale;\n\n vec4 cc_nativeSize;\n\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n\n vec4 cc_exposure;\n\n vec4 cc_mainLitDir;\n\n vec4 cc_mainLitColor;\n\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n\nin vec3 a_position;\nin vec4 a_color;\nout vec4 v_color;\n\n#if USE_TEXTURE\nin vec2 a_uv0;\nout vec2 v_uv0;\n#endif\n\nvoid main () {\n vec4 pos = vec4(a_position, 1);\n\n #if CC_USE_MODEL\n pos = cc_matViewProj * cc_matWorld * pos;\n #else\n pos = cc_matViewProj * pos;\n #endif\n\n #if USE_TEXTURE\n v_uv0 = a_uv0;\n #endif\n\n v_color = a_color;\n\n gl_Position = pos;\n}\n",
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform ALPHA_TEST {\n float alphaThreshold;\n }\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nin vec4 v_color;\n\n#if USE_TEXTURE\nin vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_ROUNDCORNER\nuniform RoundCorner {\n\n float roundCornerRadius;\n}\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n #if ENABLE_ROUNDCORNER\n\n float radius = clamp(0.0, 0.5, roundCornerRadius);\n\n vec2 uv = v_uv0.xy - vec2(0.5, 0.5);\n\n float rx = abs(uv.x) - (0.5 - radius);\n float ry = abs(uv.y) - (0.5 - radius);\n\n float mx = step(0.5 - radius, abs(uv.x));\n float my = step(0.5 - radius, abs(uv.y));\n\n float len = length(vec2(rx, ry));\n\n float a = 1.0 - mx * my * step(radius, len) * smoothstep(0., radius * 0.01, len - radius);\n o = vec4(o.rgb, o.a * a);\n #endif\n gl_FragColor = o;\n}\n"
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform ALPHA_TEST {\n float alphaThreshold;\n }\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nin vec4 v_color;\n\n#if USE_TEXTURE\nin vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_ROUNDCORNER\nuniform RoundCorner {\n\n float radius;\n}\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n #if ENABLE_ROUNDCORNER\n\n float circleRadius = clamp(0.0, 0.5, radius);\n\n vec2 uv = v_uv0.xy - vec2(0.5, 0.5);\n\n float rx = abs(uv.x) - (0.5 - circleRadius);\n float ry = abs(uv.y) - (0.5 - circleRadius);\n\n float mx = step(0.5 - circleRadius, abs(uv.x));\n float my = step(0.5 - circleRadius, abs(uv.y));\n\n float len = length(vec2(rx, ry));\n\n float a = 1.0 - mx * my * step(circleRadius, len) * smoothstep(0., circleRadius * 0.01, len - circleRadius);\n o = vec4(o.rgb, o.a * a);\n #endif\n gl_FragColor = o;\n}\n"
}
}
],

View File

@ -1,5 +1,7 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
//
// 圆角裁剪(支持任意宽高纹理)
//
// 原理:
// 1. 正方形纹理的圆角原理参考 https://www.cnblogs.com/jqm304775992/p/4987793.html
// 2. 正方形纹理的圆角代码参考 yanjifa/shaderDemor 的 https://github.com/yanjifa/shaderDemo/blob/master/assets/Effect/CircleAvatar.effect

View File

@ -10,7 +10,5 @@
"USE_TEXTURE": true,
"ENABLE_ROUNDCORNER": true
},
"_props": {
"roundCornerRadius": 0.1
}
"_props": {}
}

View File

@ -0,0 +1,14 @@
{
"__type__": "cc.Material",
"_name": "",
"_objFlags": 0,
"_native": "",
"_effectAsset": {
"__uuid__": "7c24b57e-e819-4fc9-a8d2-b06cf61b782d"
},
"_defines": {
"USE_TEXTURE": true,
"ENABLE_ROUNDCORNER": true
},
"_props": {}
}

View File

@ -0,0 +1,6 @@
{
"ver": "1.0.2",
"uuid": "a86e8864-5390-443f-b41b-b38e9d584c43",
"dataAsSubAsset": null,
"subMetas": {}
}

File diff suppressed because it is too large Load Diff

View File

@ -171,7 +171,7 @@
"array": [
0,
0,
418.2902700278839,
351.60631393648214,
0,
0,
0,
@ -1779,100 +1779,6 @@
"_atlas": null,
"_id": "4bH5hzKe9LsLQeXjLynIHQ"
},
{
"__type__": "cc.Node",
"_name": "video_btn",
"_objFlags": 0,
"_parent": {
"__id__": 29
},
"_children": [],
"_active": false,
"_components": [
{
"__id__": 39
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 50,
"height": 50
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
-631,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "c73V7fMuVE0aD5fGtmyzmF"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 38
},
"_enabled": true,
"_materials": [
{
"__uuid__": "642c2d0e-7eb6-4d65-96f2-d6e0d0305310"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "54142b08-a163-426e-a75e-4c7b21046413"
},
"_type": 0,
"_sizeMode": 1,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": "f43fJCjNdOS5VHAEhp0yDU"
},
{
"__type__": "cc.Node",
"_name": "SystemFont",
@ -1884,7 +1790,7 @@
"_active": true,
"_components": [
{
"__id__": 41
"__id__": 39
}
],
"_prefab": null,
@ -1940,12 +1846,12 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 40
"__id__": 38
},
"_enabled": true,
"_materials": [
{
"__uuid__": "642c2d0e-7eb6-4d65-96f2-d6e0d0305310"
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_useOriginalSize": false,
@ -1965,6 +1871,100 @@
"_N$cacheMode": 0,
"_id": "d1whc7H8RHdrROcYj+2Qh1"
},
{
"__type__": "cc.Node",
"_name": "video_btn",
"_objFlags": 0,
"_parent": {
"__id__": 29
},
"_children": [],
"_active": false,
"_components": [
{
"__id__": 41
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 50,
"height": 50
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
-631,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "c73V7fMuVE0aD5fGtmyzmF"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 40
},
"_enabled": true,
"_materials": [
{
"__uuid__": "642c2d0e-7eb6-4d65-96f2-d6e0d0305310"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "54142b08-a163-426e-a75e-4c7b21046413"
},
"_type": 0,
"_sizeMode": 1,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": "f43fJCjNdOS5VHAEhp0yDU"
},
{
"__type__": "cc.Node",
"_name": "BmFont",
@ -2037,7 +2037,7 @@
"_enabled": true,
"_materials": [
{
"__uuid__": "642c2d0e-7eb6-4d65-96f2-d6e0d0305310"
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_useOriginalSize": false,

View File

@ -0,0 +1,7 @@
{
"ver": "1.2.5",
"uuid": "a2b4bb7d-94e6-4c58-a455-1d0f28d54666",
"asyncLoadAssets": false,
"autoReleaseAssets": false,
"subMetas": {}
}

View File

@ -1,9 +1,9 @@
const { ccclass, property } = cc._decorator;
@ccclass
export default class RoundCornerCropEffectScene extends cc.Component {
private _roundCornerRadiuSlider: cc.Slider = null;
private _roundCornerRadiuLabel: cc.Label = null;
export default class RoundCornerCropV1EffectScene extends cc.Component {
private _radiuSlider: cc.Slider = null;
private _radiuLabel: cc.Label = null;
private _examplesParentNode: cc.Node = null;
@ -11,18 +11,18 @@ export default class RoundCornerCropEffectScene extends cc.Component {
// 关闭动态合图
cc.dynamicAtlasManager.enabled = false;
this._roundCornerRadiuSlider = cc.find("Canvas/Content/Sliders/RoundCornerRadiusSlider/Slider").getComponent(cc.Slider);
this._roundCornerRadiuLabel = cc.find("Canvas/Content/Sliders/RoundCornerRadiusSlider/ValueLabel").getComponent(cc.Label);
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._roundCornerRadiuSlider.node.on("slide", this._onSliderChanged, this);
this._radiuSlider.node.on("slide", this._onSliderChanged, this);
}
onDisable() {
this._roundCornerRadiuSlider.node.off("slide", this._onSliderChanged, this);
this._radiuSlider.node.off("slide", this._onSliderChanged, this);
}
start() {
@ -30,11 +30,11 @@ export default class RoundCornerCropEffectScene extends cc.Component {
}
private _onSliderChanged() {
this._roundCornerRadiuLabel.string = `${this._roundCornerRadiuSlider.progress.toFixed(2)}`;
this._radiuLabel.string = `${this._radiuSlider.progress.toFixed(2)}`;
// 更新材质
this._updateRenderComponentMaterial({
roundCornerRadius: this._roundCornerRadiuSlider.progress
roundCornerRadius: this._radiuSlider.progress
});
}
@ -54,7 +54,7 @@ export default class RoundCornerCropEffectScene extends cc.Component {
this._examplesParentNode.children.forEach(childNode => {
childNode.getComponents(cc.RenderComponent).forEach(renderComponent => {
let material: cc.Material = renderComponent.getMaterial(0);
material.setProperty("roundCornerRadius", param.roundCornerRadius);
material.setProperty("radius", param.roundCornerRadius);
renderComponent.setMaterial(0, material);
});
});

View File

@ -1,6 +1,6 @@
{
"ver": "1.0.5",
"uuid": "d3d2bee7-2173-436c-a11c-94a31b2054c2",
"uuid": "d27658c5-a966-4e50-a162-09c7af01579e",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,