完善内发光效果

This commit is contained in:
caizhitao 2019-12-20 16:11:45 +08:00
parent 5da364c862
commit 6d9bd2c8ce
3 changed files with 7 additions and 5 deletions

View File

@ -171,6 +171,8 @@ CCProgram fs %{
// 因此我们取0.1作为翻转临界值0.1也不是绝对的,可以自行修改这里的值 // 因此我们取0.1作为翻转临界值0.1也不是绝对的,可以自行修改这里的值
if (alpha > 0.1) { if (alpha > 0.1) {
alpha = 1.0 - alpha; alpha = 1.0 - alpha;
// 给点调料,让靠近边缘的更加亮
alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;
} }
vec4 color_src = glowColor * alpha; vec4 color_src = glowColor * alpha;

View File

@ -5,11 +5,11 @@
{ {
"glsl1": { "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", "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 SHOW_INNER_GLOW\n\nuniform vec4 glowColor;\nuniform float glowColorSize;\n\n/**\n * 获取指定角度方向距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param distance 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float distance) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = texture2D(texture, v_uv0 + vec2(distance * cos(radian), distance * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param distance 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getDistanceAverageAlpha(float distance) {\n float totalAlpha = 0.0;\n totalAlpha += getColorAlpha(0.0, distance);\n totalAlpha += getColorAlpha(30.0, distance);\n totalAlpha += getColorAlpha(60.0, distance);\n totalAlpha += getColorAlpha(90.0, distance);\n totalAlpha += getColorAlpha(120.0, distance);\n totalAlpha += getColorAlpha(150.0, distance);\n totalAlpha += getColorAlpha(180.0, distance);\n totalAlpha += getColorAlpha(210.0, distance);\n totalAlpha += getColorAlpha(240.0, distance);\n totalAlpha += getColorAlpha(270.0, distance);\n totalAlpha += getColorAlpha(300.0, distance);\n totalAlpha += getColorAlpha(330.0, distance);\n return totalAlpha * (1.0 / 12.0);\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n vec4 srcColor = texture2D(texture, v_uv0);\n if (srcColor.a < 0.0000000001) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * (1.0 / 10.0);\n}\n\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 gl_FragColor = o;\n\n #if SHOW_INNER_GLOW\n\n vec4 color_dest = o;\n\n float alpha = getGlowAlpha();\n\n if (alpha > 0.1) {\n alpha = 1.0 - alpha;\n }\n vec4 color_src = glowColor * alpha;\n\n gl_FragColor = color_src * color_src.a + color_dest;\n #endif\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 SHOW_INNER_GLOW\n\nuniform vec4 glowColor;\nuniform float glowColorSize;\n\n/**\n * 获取指定角度方向距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param distance 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float distance) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = texture2D(texture, v_uv0 + vec2(distance * cos(radian), distance * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param distance 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getDistanceAverageAlpha(float distance) {\n float totalAlpha = 0.0;\n totalAlpha += getColorAlpha(0.0, distance);\n totalAlpha += getColorAlpha(30.0, distance);\n totalAlpha += getColorAlpha(60.0, distance);\n totalAlpha += getColorAlpha(90.0, distance);\n totalAlpha += getColorAlpha(120.0, distance);\n totalAlpha += getColorAlpha(150.0, distance);\n totalAlpha += getColorAlpha(180.0, distance);\n totalAlpha += getColorAlpha(210.0, distance);\n totalAlpha += getColorAlpha(240.0, distance);\n totalAlpha += getColorAlpha(270.0, distance);\n totalAlpha += getColorAlpha(300.0, distance);\n totalAlpha += getColorAlpha(330.0, distance);\n return totalAlpha * (1.0 / 12.0);\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n vec4 srcColor = texture2D(texture, v_uv0);\n if (srcColor.a < 0.0000000001) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * (1.0 / 10.0);\n}\n\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 gl_FragColor = o;\n\n #if SHOW_INNER_GLOW\n\n vec4 color_dest = o;\n\n float alpha = getGlowAlpha();\n\n if (alpha > 0.1) {\n alpha = 1.0 - alpha;\n\n alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;\n }\n vec4 color_src = glowColor * alpha;\n\n gl_FragColor = color_src * color_src.a + color_dest;\n #endif\n}\n"
}, },
"glsl3": { "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", "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 SHOW_INNER_GLOW\n\nuniform glow {\n\n vec4 glowColor;\n\n float glowColorSize;\n\n};\n\n/**\n * 获取指定角度方向距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param distance 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float distance) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = texture(texture, v_uv0 + vec2(distance * cos(radian), distance * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param distance 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getDistanceAverageAlpha(float distance) {\n float totalAlpha = 0.0;\n totalAlpha += getColorAlpha(0.0, distance);\n totalAlpha += getColorAlpha(30.0, distance);\n totalAlpha += getColorAlpha(60.0, distance);\n totalAlpha += getColorAlpha(90.0, distance);\n totalAlpha += getColorAlpha(120.0, distance);\n totalAlpha += getColorAlpha(150.0, distance);\n totalAlpha += getColorAlpha(180.0, distance);\n totalAlpha += getColorAlpha(210.0, distance);\n totalAlpha += getColorAlpha(240.0, distance);\n totalAlpha += getColorAlpha(270.0, distance);\n totalAlpha += getColorAlpha(300.0, distance);\n totalAlpha += getColorAlpha(330.0, distance);\n return totalAlpha * (1.0 / 12.0);\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n vec4 srcColor = texture(texture, v_uv0);\n if (srcColor.a < 0.0000000001) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * (1.0 / 10.0);\n}\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 gl_FragColor = o;\n\n #if SHOW_INNER_GLOW\n\n vec4 color_dest = o;\n\n float alpha = getGlowAlpha();\n\n if (alpha > 0.1) {\n alpha = 1.0 - alpha;\n }\n vec4 color_src = glowColor * alpha;\n\n gl_FragColor = color_src * color_src.a + color_dest;\n #endif\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 SHOW_INNER_GLOW\n\nuniform glow {\n\n vec4 glowColor;\n\n float glowColorSize;\n\n};\n\n/**\n * 获取指定角度方向距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param distance 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float distance) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = texture(texture, v_uv0 + vec2(distance * cos(radian), distance * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param distance 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getDistanceAverageAlpha(float distance) {\n float totalAlpha = 0.0;\n totalAlpha += getColorAlpha(0.0, distance);\n totalAlpha += getColorAlpha(30.0, distance);\n totalAlpha += getColorAlpha(60.0, distance);\n totalAlpha += getColorAlpha(90.0, distance);\n totalAlpha += getColorAlpha(120.0, distance);\n totalAlpha += getColorAlpha(150.0, distance);\n totalAlpha += getColorAlpha(180.0, distance);\n totalAlpha += getColorAlpha(210.0, distance);\n totalAlpha += getColorAlpha(240.0, distance);\n totalAlpha += getColorAlpha(270.0, distance);\n totalAlpha += getColorAlpha(300.0, distance);\n totalAlpha += getColorAlpha(330.0, distance);\n return totalAlpha * (1.0 / 12.0);\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n vec4 srcColor = texture(texture, v_uv0);\n if (srcColor.a < 0.0000000001) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getDistanceAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * (1.0 / 10.0);\n}\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 gl_FragColor = o;\n\n #if SHOW_INNER_GLOW\n\n vec4 color_dest = o;\n\n float alpha = getGlowAlpha();\n\n if (alpha > 0.1) {\n alpha = 1.0 - alpha;\n\n alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;\n }\n vec4 color_src = glowColor * alpha;\n\n gl_FragColor = color_src * color_src.a + color_dest;\n #endif\n}\n"
} }
} }
], ],

View File

@ -15,11 +15,11 @@
"texture": null, "texture": null,
"glowColor": { "glowColor": {
"__type__": "cc.Vec4", "__type__": "cc.Vec4",
"x": 0, "x": 1,
"y": 0, "y": 0,
"z": 1, "z": 0,
"w": 1 "w": 1
}, },
"glowColorSize": 0.5 "glowColorSize": 0.1
} }
} }