完善内外发光
This commit is contained in:
parent
f88effce4f
commit
f4179fe39c
@ -104,6 +104,28 @@ CCProgram fs %{
|
|||||||
// 特别地,必须是 vec4 先于 float 声明
|
// 特别地,必须是 vec4 先于 float 声明
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取纹理uv颜色
|
||||||
|
*
|
||||||
|
* 主要实现:超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)
|
||||||
|
*
|
||||||
|
* 在 Cocos Creator 2.2.1 的编辑器中,超出边界的uv并不是返回 vec4(0.0, 0.0, 0.0, 0.0),实际返回为
|
||||||
|
*
|
||||||
|
* * 超出左边界的uv,返回 v_uv0.x = 0 的颜色
|
||||||
|
* * 超出右边界的uv,返回 v_uv0.x = 1 的颜色
|
||||||
|
* * 超出上边界的uv,返回 v_uv0.y = 1 的颜色
|
||||||
|
* * 超出下边界的uv,返回 v_uv0.y = 0 的颜色
|
||||||
|
*
|
||||||
|
* 和实际在浏览器上显示(超出边界即为透明)的有区别,为了统一,这里适配一下,这样子,在编辑器上预览的效果就能和实际浏览器的保持一致
|
||||||
|
*/
|
||||||
|
vec4 getTextureColor(sampler2D texture, vec2 v_uv0) {
|
||||||
|
if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {
|
||||||
|
return vec4(0.0, 0.0, 0.0, 0.0);
|
||||||
|
}
|
||||||
|
return texture(texture, v_uv0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定角度方向,距离为xxx的像素的透明度
|
* 获取指定角度方向,距离为xxx的像素的透明度
|
||||||
*
|
*
|
||||||
@ -115,7 +137,7 @@ CCProgram fs %{
|
|||||||
float getColorAlpha(float angle, float dist) {
|
float getColorAlpha(float angle, float dist) {
|
||||||
// 角度转弧度,公式为:弧度 = 角度 * (pi / 180)
|
// 角度转弧度,公式为:弧度 = 角度 * (pi / 180)
|
||||||
float radian = angle * 0.01745329252; // 这个浮点数是 pi / 180
|
float radian = angle * 0.01745329252; // 这个浮点数是 pi / 180
|
||||||
vec4 color = texture(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian)));
|
vec4 color = getTextureColor(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian)));
|
||||||
return color.a;
|
return color.a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +181,7 @@ CCProgram fs %{
|
|||||||
// 那么就意味着这个点是图像外的透明点或者图像内透明点(如空洞)之类的
|
// 那么就意味着这个点是图像外的透明点或者图像内透明点(如空洞)之类的
|
||||||
// 内发光的话,这些透明点我们不用处理,让它保持原样,否则就是会有内描边或者一点扩边的效果
|
// 内发光的话,这些透明点我们不用处理,让它保持原样,否则就是会有内描边或者一点扩边的效果
|
||||||
// 同时也是提前直接结束,减少计算量
|
// 同时也是提前直接结束,减少计算量
|
||||||
vec4 srcColor = texture(texture, v_uv0);
|
vec4 srcColor = getTextureColor(texture, v_uv0);
|
||||||
if (srcColor.a <= glowThreshold) {
|
if (srcColor.a <= glowThreshold) {
|
||||||
return srcColor.a;
|
return srcColor.a;
|
||||||
}
|
}
|
||||||
@ -186,7 +208,7 @@ CCProgram fs %{
|
|||||||
vec4 o = vec4(1, 1, 1, 1);
|
vec4 o = vec4(1, 1, 1, 1);
|
||||||
|
|
||||||
#if USE_TEXTURE
|
#if USE_TEXTURE
|
||||||
o *= texture(texture, v_uv0);
|
o *= getTextureColor(texture, v_uv0);
|
||||||
#if CC_USE_ALPHA_ATLAS_TEXTURE
|
#if CC_USE_ALPHA_ATLAS_TEXTURE
|
||||||
o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;
|
o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;
|
||||||
#endif
|
#endif
|
||||||
|
@ -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;\nuniform float glowThreshold;\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = texture2D(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n vec4 srcColor = texture2D(texture, v_uv0);\n if (srcColor.a <= glowThreshold) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\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 > glowThreshold) {\n\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\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;\nuniform float glowThreshold;\n\n/**\n * 获取纹理uv颜色 \n *\n * 主要实现:超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)\n * \n * 在 Cocos Creator 2.2.1 的编辑器中,超出边界的uv并不是返回 vec4(0.0, 0.0, 0.0, 0.0),实际返回为\n * \n * * 超出左边界的uv,返回 v_uv0.x = 0 的颜色\n * * 超出右边界的uv,返回 v_uv0.x = 1 的颜色\n * * 超出上边界的uv,返回 v_uv0.y = 1 的颜色\n * * 超出下边界的uv,返回 v_uv0.y = 0 的颜色\n *\n * 和实际在浏览器上显示(超出边界即为透明)的有区别,为了统一,这里适配一下,这样子,在编辑器上预览的效果就能和实际浏览器的保持一致\n */\nvec4 getTextureColor(sampler2D texture, vec2 v_uv0) {\n if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n }\n return texture2D(texture, v_uv0);\n}\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = getTextureColor(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n vec4 srcColor = getTextureColor(texture, v_uv0);\n if (srcColor.a <= glowThreshold) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\n}\n\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= getTextureColor(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 > glowThreshold) {\n\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\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 float glowThreshold;\n\n};\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = texture(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n vec4 srcColor = texture(texture, v_uv0);\n if (srcColor.a <= glowThreshold) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\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 > glowThreshold) {\n\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\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 float glowThreshold;\n\n};\n\n/**\n * 获取纹理uv颜色 \n *\n * 主要实现:超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)\n * \n * 在 Cocos Creator 2.2.1 的编辑器中,超出边界的uv并不是返回 vec4(0.0, 0.0, 0.0, 0.0),实际返回为\n * \n * * 超出左边界的uv,返回 v_uv0.x = 0 的颜色\n * * 超出右边界的uv,返回 v_uv0.x = 1 的颜色\n * * 超出上边界的uv,返回 v_uv0.y = 1 的颜色\n * * 超出下边界的uv,返回 v_uv0.y = 0 的颜色\n *\n * 和实际在浏览器上显示(超出边界即为透明)的有区别,为了统一,这里适配一下,这样子,在编辑器上预览的效果就能和实际浏览器的保持一致\n */\nvec4 getTextureColor(sampler2D texture, vec2 v_uv0) {\n if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n }\n return texture(texture, v_uv0);\n}\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = getTextureColor(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n vec4 srcColor = getTextureColor(texture, v_uv0);\n if (srcColor.a <= glowThreshold) {\n return srcColor.a;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\n}\n\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= getTextureColor(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 > glowThreshold) {\n\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\n vec4 color_src = glowColor * alpha;\n\n gl_FragColor = color_src * color_src.a + color_dest;\n #endif\n}\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -119,9 +119,18 @@ CCProgram fs %{
|
|||||||
/**
|
/**
|
||||||
* 获取纹理uv颜色
|
* 获取纹理uv颜色
|
||||||
*
|
*
|
||||||
* 超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)
|
* 主要实现:超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)
|
||||||
|
*
|
||||||
|
* 在 Cocos Creator 2.2.1 的编辑器中,超出边界的uv并不是返回 vec4(0.0, 0.0, 0.0, 0.0),实际返回为
|
||||||
|
*
|
||||||
|
* * 超出左边界的uv,返回 v_uv0.x = 0 的颜色
|
||||||
|
* * 超出右边界的uv,返回 v_uv0.x = 1 的颜色
|
||||||
|
* * 超出上边界的uv,返回 v_uv0.y = 1 的颜色
|
||||||
|
* * 超出下边界的uv,返回 v_uv0.y = 0 的颜色
|
||||||
|
*
|
||||||
|
* 和实际在浏览器上显示(超出边界即为透明)的有区别,为了统一,这里适配一下,这样子,在编辑器上预览的效果就能和实际浏览器的保持一致
|
||||||
*/
|
*/
|
||||||
vec4 getTexture(sampler2D texture, vec2 v_uv0) {
|
vec4 getTextureColor(sampler2D texture, vec2 v_uv0) {
|
||||||
if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {
|
if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {
|
||||||
return vec4(0.0, 0.0, 0.0, 0.0);
|
return vec4(0.0, 0.0, 0.0, 0.0);
|
||||||
}
|
}
|
||||||
@ -152,7 +161,7 @@ CCProgram fs %{
|
|||||||
float getColorAlpha(float angle, float dist) {
|
float getColorAlpha(float angle, float dist) {
|
||||||
// 角度转弧度,公式为:弧度 = 角度 * (pi / 180)
|
// 角度转弧度,公式为:弧度 = 角度 * (pi / 180)
|
||||||
float radian = angle * 0.01745329252; // 这个浮点数是 pi / 180
|
float radian = angle * 0.01745329252; // 这个浮点数是 pi / 180
|
||||||
vec4 color = getTexture(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian)));
|
vec4 color = getTextureColor(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian)));
|
||||||
return color.a;
|
return color.a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,7 +239,7 @@ CCProgram fs %{
|
|||||||
vec4 o = vec4(1, 1, 1, 1);
|
vec4 o = vec4(1, 1, 1, 1);
|
||||||
|
|
||||||
#if USE_TEXTURE
|
#if USE_TEXTURE
|
||||||
o *= getTexture(texture, v_uv0);
|
o *= getTextureColor(texture, v_uv0);
|
||||||
#if CC_USE_ALPHA_ATLAS_TEXTURE
|
#if CC_USE_ALPHA_ATLAS_TEXTURE
|
||||||
o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;
|
o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;
|
||||||
#endif
|
#endif
|
||||||
|
@ -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\n\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\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\n\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\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/**\n * 获取纹理uv颜色 \n *\n * 超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)\n */\nvec4 getTexture(sampler2D texture, vec2 v_uv0) {\n if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n }\n return texture2D(texture, v_uv0);\n}\n\n#if SHOW_OUTTER_GLOW\n\nuniform vec4 glowColor;\nuniform float glowColorSize;\nuniform float glowThreshold;\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = getTexture(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\n}\n\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= getTexture(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_OUTTER_GLOW\n\n float alpha = getGlowAlpha();\n\n if (alpha <= glowThreshold) {\n\n alpha = alpha / glowThreshold;\n\n alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;\n } else {\n\n alpha = 0.0;\n }\n\n vec4 color_dest = glowColor * alpha;\n vec4 color_src = o;\n\n gl_FragColor = color_src * color_src.a + color_dest * (1.0 - color_src.a);\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/**\n * 获取纹理uv颜色 \n *\n * 主要实现:超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)\n * \n * 在 Cocos Creator 2.2.1 的编辑器中,超出边界的uv并不是返回 vec4(0.0, 0.0, 0.0, 0.0),实际返回为\n * \n * * 超出左边界的uv,返回 v_uv0.x = 0 的颜色\n * * 超出右边界的uv,返回 v_uv0.x = 1 的颜色\n * * 超出上边界的uv,返回 v_uv0.y = 1 的颜色\n * * 超出下边界的uv,返回 v_uv0.y = 0 的颜色\n *\n * 和实际在浏览器上显示(超出边界即为透明)的有区别,为了统一,这里适配一下,这样子,在编辑器上预览的效果就能和实际浏览器的保持一致\n */\nvec4 getTextureColor(sampler2D texture, vec2 v_uv0) {\n if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n }\n return texture2D(texture, v_uv0);\n}\n\n#if SHOW_OUTTER_GLOW\n\nuniform vec4 glowColor;\nuniform float glowColorSize;\nuniform float glowThreshold;\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = getTextureColor(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\n}\n\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= getTextureColor(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_OUTTER_GLOW\n\n float alpha = getGlowAlpha();\n\n if (alpha <= glowThreshold) {\n\n alpha = alpha / glowThreshold;\n\n alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;\n } else {\n\n alpha = 0.0;\n }\n\n vec4 color_dest = glowColor * alpha;\n vec4 color_src = o;\n\n gl_FragColor = color_src * color_src.a + color_dest * (1.0 - color_src.a);\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\n\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\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\n\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\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/**\n * 获取纹理uv颜色 \n *\n * 超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)\n */\nvec4 getTexture(sampler2D texture, vec2 v_uv0) {\n if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n }\n return texture(texture, v_uv0);\n}\n\n#if SHOW_OUTTER_GLOW\n\nuniform glow {\n\n vec4 glowColor;\n\n float glowColorSize;\n\n float glowThreshold;\n\n};\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = getTexture(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\n}\n\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= getTexture(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_OUTTER_GLOW\n\n float alpha = getGlowAlpha();\n\n if (alpha <= glowThreshold) {\n\n alpha = alpha / glowThreshold;\n\n alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;\n } else {\n\n alpha = 0.0;\n }\n\n vec4 color_dest = glowColor * alpha;\n vec4 color_src = o;\n\n gl_FragColor = color_src * color_src.a + color_dest * (1.0 - color_src.a);\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/**\n * 获取纹理uv颜色 \n *\n * 主要实现:超出边界的统一返回 vec4(0.0, 0.0, 0.0, 0.0)\n * \n * 在 Cocos Creator 2.2.1 的编辑器中,超出边界的uv并不是返回 vec4(0.0, 0.0, 0.0, 0.0),实际返回为\n * \n * * 超出左边界的uv,返回 v_uv0.x = 0 的颜色\n * * 超出右边界的uv,返回 v_uv0.x = 1 的颜色\n * * 超出上边界的uv,返回 v_uv0.y = 1 的颜色\n * * 超出下边界的uv,返回 v_uv0.y = 0 的颜色\n *\n * 和实际在浏览器上显示(超出边界即为透明)的有区别,为了统一,这里适配一下,这样子,在编辑器上预览的效果就能和实际浏览器的保持一致\n */\nvec4 getTextureColor(sampler2D texture, vec2 v_uv0) {\n if (v_uv0.x > 1.0 || v_uv0.x < 0.0 || v_uv0.y > 1.0 || v_uv0.y < 0.0) {\n return vec4(0.0, 0.0, 0.0, 0.0);\n }\n return texture(texture, v_uv0);\n}\n\n#if SHOW_OUTTER_GLOW\n\nuniform glow {\n\n vec4 glowColor;\n\n float glowColorSize;\n\n float glowThreshold;\n\n};\n\n/**\n * 获取指定角度方向,距离为xxx的像素的透明度\n *\n * @param angle 角度 [0.0, 360.0]\n * @param dist 距离 [0.0, 1.0]\n *\n * @return alpha [0.0, 1.0]\n */\nfloat getColorAlpha(float angle, float dist) {\n\n float radian = angle * 0.01745329252;\n\n vec4 color = getTextureColor(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian))); \n return color.a;\n}\n\n/**\n * 获取指定距离的周边像素的透明度平均值\n *\n * @param dist 距离 [0.0, 1.0]\n *\n * @return average alpha [0.0, 1.0]\n */\nfloat getAverageAlpha(float dist) {\n float totalAlpha = 0.0;\n\n totalAlpha += getColorAlpha(0.0, dist);\n totalAlpha += getColorAlpha(30.0, dist);\n totalAlpha += getColorAlpha(60.0, dist);\n totalAlpha += getColorAlpha(90.0, dist);\n totalAlpha += getColorAlpha(120.0, dist);\n totalAlpha += getColorAlpha(150.0, dist);\n totalAlpha += getColorAlpha(180.0, dist);\n totalAlpha += getColorAlpha(210.0, dist);\n totalAlpha += getColorAlpha(240.0, dist);\n totalAlpha += getColorAlpha(270.0, dist);\n totalAlpha += getColorAlpha(300.0, dist);\n totalAlpha += getColorAlpha(330.0, dist);\n return totalAlpha * 0.0833;\n\n}\n\n/**\n * 获取发光的透明度\n */\nfloat getGlowAlpha() {\n\n if (glowColorSize == 0.0) {\n return 0.0;\n }\n\n float totalAlpha = 0.0;\n totalAlpha += getAverageAlpha(glowColorSize * 0.1);\n totalAlpha += getAverageAlpha(glowColorSize * 0.2);\n totalAlpha += getAverageAlpha(glowColorSize * 0.3);\n totalAlpha += getAverageAlpha(glowColorSize * 0.4);\n totalAlpha += getAverageAlpha(glowColorSize * 0.5);\n totalAlpha += getAverageAlpha(glowColorSize * 0.6);\n totalAlpha += getAverageAlpha(glowColorSize * 0.7);\n totalAlpha += getAverageAlpha(glowColorSize * 0.8);\n totalAlpha += getAverageAlpha(glowColorSize * 0.9);\n totalAlpha += getAverageAlpha(glowColorSize * 1.0);\n return totalAlpha * 0.1;\n}\n\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= getTextureColor(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_OUTTER_GLOW\n\n float alpha = getGlowAlpha();\n\n if (alpha <= glowThreshold) {\n\n alpha = alpha / glowThreshold;\n\n alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;\n } else {\n\n alpha = 0.0;\n }\n\n vec4 color_dest = glowColor * alpha;\n vec4 color_src = o;\n\n gl_FragColor = color_src * color_src.a + color_dest * (1.0 - color_src.a);\n #endif\n}\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -5184,7 +5184,7 @@
|
|||||||
"_contentSize": {
|
"_contentSize": {
|
||||||
"__type__": "cc.Size",
|
"__type__": "cc.Size",
|
||||||
"width": 384,
|
"width": 384,
|
||||||
"height": 459.33
|
"height": 435.33
|
||||||
},
|
},
|
||||||
"_anchorPoint": {
|
"_anchorPoint": {
|
||||||
"__type__": "cc.Vec2",
|
"__type__": "cc.Vec2",
|
||||||
@ -5446,7 +5446,7 @@
|
|||||||
"ctor": "Float64Array",
|
"ctor": "Float64Array",
|
||||||
"array": [
|
"array": [
|
||||||
0,
|
0,
|
||||||
-348,
|
-336,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
@ -5726,7 +5726,7 @@
|
|||||||
"ctor": "Float64Array",
|
"ctor": "Float64Array",
|
||||||
"array": [
|
"array": [
|
||||||
0,
|
0,
|
||||||
-442.665,
|
-418.665,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
@ -5820,7 +5820,7 @@
|
|||||||
"_layoutSize": {
|
"_layoutSize": {
|
||||||
"__type__": "cc.Size",
|
"__type__": "cc.Size",
|
||||||
"width": 384,
|
"width": 384,
|
||||||
"height": 459.33
|
"height": 435.33
|
||||||
},
|
},
|
||||||
"_resize": 1,
|
"_resize": 1,
|
||||||
"_N$layoutType": 2,
|
"_N$layoutType": 2,
|
||||||
@ -5836,7 +5836,7 @@
|
|||||||
"_N$paddingTop": 0,
|
"_N$paddingTop": 0,
|
||||||
"_N$paddingBottom": 0,
|
"_N$paddingBottom": 0,
|
||||||
"_N$spacingX": 48,
|
"_N$spacingX": 48,
|
||||||
"_N$spacingY": 48,
|
"_N$spacingY": 36,
|
||||||
"_N$verticalDirection": 1,
|
"_N$verticalDirection": 1,
|
||||||
"_N$horizontalDirection": 0,
|
"_N$horizontalDirection": 0,
|
||||||
"_N$affectedByScale": false,
|
"_N$affectedByScale": false,
|
||||||
|
Loading…
Reference in New Issue
Block a user