2019-12-19 02:52:32 +00:00
|
|
|
|
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2019-12-20 04:32:27 +00:00
|
|
|
|
// 内发光特效
|
|
|
|
|
// 原理: 采样周边像素alpha取平均值,叠加发光效果
|
2019-12-19 02:52:32 +00:00
|
|
|
|
CCEffect %{
|
|
|
|
|
techniques:
|
|
|
|
|
- passes:
|
|
|
|
|
- vert: vs
|
|
|
|
|
frag: fs
|
|
|
|
|
blendState:
|
|
|
|
|
targets:
|
|
|
|
|
- blend: true
|
|
|
|
|
rasterizerState:
|
|
|
|
|
cullMode: none
|
|
|
|
|
properties:
|
|
|
|
|
texture: { value: white }
|
|
|
|
|
alphaThreshold: { value: 0.5 }
|
2019-12-24 09:07:17 +00:00
|
|
|
|
|
|
|
|
|
# 自定义参数
|
|
|
|
|
# 发光颜色
|
|
|
|
|
glowColor: {
|
|
|
|
|
value: [1.0, 1.0, 0.0, 1.0],
|
|
|
|
|
editor: {
|
|
|
|
|
type: color,
|
|
|
|
|
displayName: "发光颜色"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# 发光宽度
|
|
|
|
|
glowColorSize: {
|
|
|
|
|
value: 0.01,
|
|
|
|
|
editor: {
|
|
|
|
|
displayName: "发光宽度(百分比)"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# 发光透明度阈值
|
|
|
|
|
#只有超过这个透明度的点才会发光,一般用于解决图像边缘存在渐变透明的时,决定哪些透明度的边缘点发光,具体可以操作一下就知道
|
|
|
|
|
glowThreshold: {
|
|
|
|
|
value: 0.1,
|
|
|
|
|
editor: {
|
|
|
|
|
displayName: "发光阈值(百分比)"
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-19 02:52:32 +00:00
|
|
|
|
}%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CCProgram vs %{
|
|
|
|
|
precision highp float;
|
|
|
|
|
|
|
|
|
|
#include <cc-global>
|
|
|
|
|
#include <cc-local>
|
|
|
|
|
|
|
|
|
|
in vec3 a_position;
|
|
|
|
|
in vec4 a_color;
|
|
|
|
|
out vec4 v_color;
|
|
|
|
|
|
|
|
|
|
#if USE_TEXTURE
|
|
|
|
|
in vec2 a_uv0;
|
|
|
|
|
out vec2 v_uv0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void main () {
|
|
|
|
|
vec4 pos = vec4(a_position, 1);
|
|
|
|
|
|
|
|
|
|
#if CC_USE_MODEL
|
|
|
|
|
pos = cc_matViewProj * cc_matWorld * pos;
|
|
|
|
|
#else
|
|
|
|
|
pos = cc_matViewProj * pos;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if USE_TEXTURE
|
|
|
|
|
v_uv0 = a_uv0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
v_color = a_color;
|
|
|
|
|
|
|
|
|
|
gl_Position = pos;
|
|
|
|
|
}
|
|
|
|
|
}%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CCProgram fs %{
|
|
|
|
|
precision highp float;
|
|
|
|
|
|
|
|
|
|
#include <alpha-test>
|
|
|
|
|
|
|
|
|
|
in vec4 v_color;
|
|
|
|
|
|
|
|
|
|
#if USE_TEXTURE
|
|
|
|
|
in vec2 v_uv0;
|
|
|
|
|
uniform sampler2D texture;
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-12-20 04:32:27 +00:00
|
|
|
|
#if SHOW_INNER_GLOW
|
2019-12-19 02:52:32 +00:00
|
|
|
|
|
2019-12-20 04:32:27 +00:00
|
|
|
|
uniform glow {
|
|
|
|
|
// 发光颜色
|
|
|
|
|
vec4 glowColor;
|
|
|
|
|
// 发光范围
|
|
|
|
|
float glowColorSize;
|
2019-12-24 09:07:17 +00:00
|
|
|
|
// 发光阈值
|
|
|
|
|
float glowThreshold;
|
2019-12-19 02:52:32 +00:00
|
|
|
|
// 特别地,必须是 vec4 先于 float 声明
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-20 04:32:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取指定角度方向,距离为xxx的像素的透明度
|
|
|
|
|
*
|
|
|
|
|
* @param angle 角度 [0.0, 360.0]
|
2019-12-23 15:34:57 +00:00
|
|
|
|
* @param dist 距离 [0.0, 1.0]
|
2019-12-20 04:32:27 +00:00
|
|
|
|
*
|
|
|
|
|
* @return alpha [0.0, 1.0]
|
|
|
|
|
*/
|
2019-12-23 15:34:57 +00:00
|
|
|
|
float getColorAlpha(float angle, float dist) {
|
2019-12-20 04:32:27 +00:00
|
|
|
|
// 角度转弧度,公式为:弧度 = 角度 * (pi / 180)
|
|
|
|
|
float radian = angle * 0.01745329252; // 这个浮点数是 pi / 180
|
2019-12-23 15:34:57 +00:00
|
|
|
|
vec4 color = texture(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian)));
|
2019-12-20 07:59:56 +00:00
|
|
|
|
return color.a;
|
2019-12-19 07:45:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 04:32:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取指定距离的周边像素的透明度平均值
|
|
|
|
|
*
|
2019-12-23 15:34:57 +00:00
|
|
|
|
* @param dist 距离 [0.0, 1.0]
|
2019-12-20 04:32:27 +00:00
|
|
|
|
*
|
|
|
|
|
* @return average alpha [0.0, 1.0]
|
|
|
|
|
*/
|
2019-12-23 15:34:57 +00:00
|
|
|
|
float getAverageAlpha(float dist) {
|
2019-12-20 04:32:27 +00:00
|
|
|
|
float totalAlpha = 0.0;
|
2019-12-23 15:34:57 +00:00
|
|
|
|
// 以30度为一个单位,那么「周边」就由0到360度中共计12个点的组成
|
|
|
|
|
totalAlpha += getColorAlpha(0.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(30.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(60.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(90.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(120.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(150.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(180.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(210.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(240.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(270.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(300.0, dist);
|
|
|
|
|
totalAlpha += getColorAlpha(330.0, dist);
|
2019-12-24 09:07:17 +00:00
|
|
|
|
return totalAlpha * 0.0833; // 1 / 12 = 0.08333
|
2019-12-19 02:52:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 04:32:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取发光的透明度
|
|
|
|
|
*/
|
|
|
|
|
float getGlowAlpha() {
|
2019-12-23 15:34:57 +00:00
|
|
|
|
// 如果发光宽度为0
|
|
|
|
|
if (glowColorSize == 0.0) {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 原来已经接近透明的点不处理
|
|
|
|
|
// 一般图像边缘会有一些半透明点,这些透明点我们不处理,让它保持原样,否则就是会有内描边效果
|
2019-12-20 07:59:56 +00:00
|
|
|
|
vec4 srcColor = texture(texture, v_uv0);
|
2019-12-23 15:34:57 +00:00
|
|
|
|
if (srcColor.a < 0.1) {
|
2019-12-20 07:59:56 +00:00
|
|
|
|
return srcColor.a;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 15:34:57 +00:00
|
|
|
|
// 将传入的指定距离,平均分成10圈,求出每一圈的平均透明度,
|
|
|
|
|
// 然后求和取平均值,那么就可以得到该点的平均透明度
|
2019-12-20 04:32:27 +00:00
|
|
|
|
float totalAlpha = 0.0;
|
2019-12-23 15:34:57 +00:00
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.1);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.2);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.3);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.4);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.5);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.6);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.7);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.8);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 0.9);
|
|
|
|
|
totalAlpha += getAverageAlpha(glowColorSize * 1.0);
|
|
|
|
|
return totalAlpha * 0.1;
|
2019-12-20 04:32:27 +00:00
|
|
|
|
}
|
2019-12-19 07:45:29 +00:00
|
|
|
|
|
2019-12-19 02:52:32 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void main () {
|
|
|
|
|
vec4 o = vec4(1, 1, 1, 1);
|
|
|
|
|
|
|
|
|
|
#if USE_TEXTURE
|
|
|
|
|
o *= texture(texture, v_uv0);
|
|
|
|
|
#if CC_USE_ALPHA_ATLAS_TEXTURE
|
|
|
|
|
o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
o *= v_color;
|
|
|
|
|
|
|
|
|
|
ALPHA_TEST(o);
|
|
|
|
|
|
|
|
|
|
gl_FragColor = o;
|
|
|
|
|
|
2019-12-20 04:32:27 +00:00
|
|
|
|
#if SHOW_INNER_GLOW
|
2019-12-20 07:59:56 +00:00
|
|
|
|
// 目标颜色(图像)
|
2019-12-20 04:32:27 +00:00
|
|
|
|
vec4 color_dest = o;
|
2019-12-19 02:52:32 +00:00
|
|
|
|
|
2019-12-20 07:59:56 +00:00
|
|
|
|
float alpha = getGlowAlpha();
|
|
|
|
|
// 此时我们得到的是内部透明度为1,靠近边缘的为接近0的透明度
|
|
|
|
|
// 而内发光恰恰相反,是需要内部透明度为0,靠近边缘的接近1的透明度
|
|
|
|
|
// 因此我们需要翻转一下透明度
|
|
|
|
|
// 如果图像边缘有大量渐变,那么如果我们取 0.0 的话,那么可能边缘会出现锯齿
|
|
|
|
|
// 因此我们取0.1作为翻转临界值,0.1也不是绝对的,可以自行修改这里的值
|
2019-12-24 09:07:17 +00:00
|
|
|
|
if (alpha > glowThreshold) {
|
2019-12-20 07:59:56 +00:00
|
|
|
|
alpha = 1.0 - alpha;
|
2019-12-20 08:11:45 +00:00
|
|
|
|
// 给点调料,让靠近边缘的更加亮
|
|
|
|
|
alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;
|
2019-12-20 07:59:56 +00:00
|
|
|
|
}
|
2019-12-24 09:07:17 +00:00
|
|
|
|
// 源颜色(内发光)
|
2019-12-20 07:59:56 +00:00
|
|
|
|
vec4 color_src = glowColor * alpha;
|
2019-12-19 02:52:32 +00:00
|
|
|
|
|
2019-12-20 04:32:27 +00:00
|
|
|
|
// 按照这个顺序,源颜色就是内发光颜色,目标颜色就是图案颜色色
|
2019-12-19 02:52:32 +00:00
|
|
|
|
// 所以命名就是 color_src, color_dest
|
|
|
|
|
|
|
|
|
|
// 按照混合颜色规则 http://docs.cocos.com/creator/manual/zh/advanced-topics/ui-auto-batch.html#blend-%E6%A8%A1%E5%BC%8F
|
2019-12-20 04:32:27 +00:00
|
|
|
|
// 要在图案上方,叠加一个内发光,将两者颜色混合起来,那么最终选择的混合模式如下:
|
2019-12-20 07:59:56 +00:00
|
|
|
|
//
|
|
|
|
|
// (内发光)color_src: GL_SRC_ALPHA
|
|
|
|
|
// (原图像)color_dest: GL_ONE
|
|
|
|
|
//
|
2019-12-19 02:52:32 +00:00
|
|
|
|
// 即最终颜色如下:
|
2019-12-20 07:59:56 +00:00
|
|
|
|
// color_src * GL_SRC_ALPHA + color_dest * GL_ONE
|
|
|
|
|
|
|
|
|
|
gl_FragColor = color_src * color_src.a + color_dest;
|
2019-12-19 02:52:32 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}%
|