2019-12-25 01:59:19 +00:00
|
|
|
|
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2020-02-17 02:05:42 +00:00
|
|
|
|
|
2019-12-25 01:59:19 +00:00
|
|
|
|
CCEffect %{
|
|
|
|
|
techniques:
|
|
|
|
|
- passes:
|
|
|
|
|
- vert: vs
|
|
|
|
|
frag: fs
|
|
|
|
|
blendState:
|
|
|
|
|
targets:
|
|
|
|
|
- blend: true
|
|
|
|
|
rasterizerState:
|
|
|
|
|
cullMode: none
|
|
|
|
|
properties:
|
|
|
|
|
texture: { value: white }
|
|
|
|
|
alphaThreshold: { value: 0.5 }
|
|
|
|
|
# 自定义参数
|
|
|
|
|
# 发光颜色
|
|
|
|
|
glowColor: {
|
|
|
|
|
value: [1.0, 1.0, 0.0, 1.0],
|
2020-02-17 01:38:49 +00:00
|
|
|
|
editor: {
|
2019-12-25 01:59:19 +00:00
|
|
|
|
type: color,
|
|
|
|
|
tooltip: "发光颜色"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# 发光宽度
|
|
|
|
|
glowColorSize: {
|
2019-12-25 02:51:37 +00:00
|
|
|
|
value: 0.15,
|
2020-02-17 01:38:49 +00:00
|
|
|
|
editor: {
|
2019-12-25 01:59:19 +00:00
|
|
|
|
tooltip: "发光宽度",
|
|
|
|
|
range: [0.0, 1.0],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# 发光透明度阈值
|
2019-12-25 02:51:37 +00:00
|
|
|
|
# 只有小于等于这个透明度的点才会发光
|
2019-12-25 01:59:19 +00:00
|
|
|
|
glowThreshold: {
|
2019-12-25 02:51:37 +00:00
|
|
|
|
value: 1.0,
|
2020-02-17 01:38:49 +00:00
|
|
|
|
editor: {
|
2019-12-25 01:59:19 +00:00
|
|
|
|
tooltip: "发光阈值",
|
|
|
|
|
range: [0.0, 1.0]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2019-12-25 10:51:43 +00:00
|
|
|
|
|
|
|
|
|
// // 左移
|
|
|
|
|
// v_uv0 = a_uv0 + vec2(0.1, 0.0);
|
|
|
|
|
// // 右移
|
|
|
|
|
// v_uv0 = a_uv0 + vec2(-0.1, 0.0);
|
|
|
|
|
// // 上移
|
|
|
|
|
// v_uv0 = a_uv0 + vec2(0.0, 0.1);
|
|
|
|
|
// // 下移
|
|
|
|
|
// v_uv0 = a_uv0 + vec2(0.0, -0.1);
|
|
|
|
|
|
|
|
|
|
// // x轴拉伸
|
|
|
|
|
// v_uv0 = a_uv0 * vec2(0.5, 1.0);
|
|
|
|
|
// // x轴压缩
|
|
|
|
|
// v_uv0 = a_uv0 * vec2(2.0, 1.0);
|
|
|
|
|
// // y轴拉伸
|
|
|
|
|
// v_uv0 = a_uv0 * vec2(1.0, 0.5);
|
|
|
|
|
// // y轴压缩
|
|
|
|
|
// v_uv0 = a_uv0 * vec2(1.0, 2.0);
|
|
|
|
|
|
2019-12-25 14:45:22 +00:00
|
|
|
|
// 先缩小一倍,然后在移动回去中间
|
|
|
|
|
// v_uv0 = a_uv0 * 2.0 - 0.5;
|
2019-12-25 01:59:19 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
v_color = a_color;
|
|
|
|
|
|
2019-12-25 10:51:43 +00:00
|
|
|
|
// pos *= vec4(2.0, 2.0, 1.0, 1.0);
|
2019-12-25 01:59:19 +00:00
|
|
|
|
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-25 10:51:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取纹理uv颜色
|
|
|
|
|
*
|
2020-01-01 02:46:13 +00:00
|
|
|
|
* 主要实现:超出边界的统一返回 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 的颜色
|
|
|
|
|
*
|
|
|
|
|
* 和实际在浏览器上显示(超出边界即为透明)的有区别,为了统一,这里适配一下,这样子,在编辑器上预览的效果就能和实际浏览器的保持一致
|
2019-12-25 10:51:43 +00:00
|
|
|
|
*/
|
2020-01-01 02:46:13 +00:00
|
|
|
|
vec4 getTextureColor(sampler2D texture, vec2 v_uv0) {
|
2019-12-25 10:51:43 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 02:20:03 +00:00
|
|
|
|
#if SHOW_OUTTER_GLOW
|
2019-12-25 01:59:19 +00:00
|
|
|
|
|
|
|
|
|
uniform glow {
|
|
|
|
|
// 发光颜色
|
|
|
|
|
vec4 glowColor;
|
|
|
|
|
// 发光范围
|
|
|
|
|
float glowColorSize;
|
|
|
|
|
// 发光阈值
|
|
|
|
|
float glowThreshold;
|
|
|
|
|
// 特别地,必须是 vec4 先于 float 声明
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-25 10:51:43 +00:00
|
|
|
|
|
2019-12-25 01:59:19 +00:00
|
|
|
|
/**
|
|
|
|
|
* 获取指定角度方向,距离为xxx的像素的透明度
|
|
|
|
|
*
|
|
|
|
|
* @param angle 角度 [0.0, 360.0]
|
|
|
|
|
* @param dist 距离 [0.0, 1.0]
|
|
|
|
|
*
|
|
|
|
|
* @return alpha [0.0, 1.0]
|
|
|
|
|
*/
|
|
|
|
|
float getColorAlpha(float angle, float dist) {
|
|
|
|
|
// 角度转弧度,公式为:弧度 = 角度 * (pi / 180)
|
|
|
|
|
float radian = angle * 0.01745329252; // 这个浮点数是 pi / 180
|
2020-01-01 02:46:13 +00:00
|
|
|
|
vec4 color = getTextureColor(texture, v_uv0 + vec2(dist * cos(radian), dist * sin(radian)));
|
2019-12-25 01:59:19 +00:00
|
|
|
|
return color.a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定距离的周边像素的透明度平均值
|
|
|
|
|
*
|
|
|
|
|
* @param dist 距离 [0.0, 1.0]
|
|
|
|
|
*
|
|
|
|
|
* @return average alpha [0.0, 1.0]
|
|
|
|
|
*/
|
|
|
|
|
float getAverageAlpha(float dist) {
|
|
|
|
|
float totalAlpha = 0.0;
|
|
|
|
|
// 以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);
|
|
|
|
|
return totalAlpha * 0.0833; // 1 / 12 = 0.08333
|
2019-12-25 02:51:37 +00:00
|
|
|
|
|
|
|
|
|
// // for 循环写法据说耗性能
|
|
|
|
|
// float totalAlpha = 0.0;
|
|
|
|
|
// const float count = 12.0;
|
|
|
|
|
// const float angle = 360.0 / count;
|
|
|
|
|
// for (float i = 0.0; i < count; i += 1.0) {
|
|
|
|
|
// totalAlpha += getColorAlpha(angle * i, dist) ;
|
|
|
|
|
// }
|
|
|
|
|
// return totalAlpha / count;
|
2019-12-25 01:59:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取发光的透明度
|
|
|
|
|
*/
|
|
|
|
|
float getGlowAlpha() {
|
|
|
|
|
// 如果发光宽度为0,直接返回0.0透明度,减少计算量
|
|
|
|
|
if (glowColorSize == 0.0) {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 02:20:03 +00:00
|
|
|
|
// // 因为我们是要做内发光,所以如果点本来是透明的或者接近透明的
|
|
|
|
|
// // 那么就意味着这个点是图像外的透明点或者图像内透明点(如空洞)之类的
|
|
|
|
|
// // 内发光的话,这些透明点我们不用处理,让它保持原样,否则就是会有内描边或者一点扩边的效果
|
|
|
|
|
// // 同时也是提前直接结束,减少计算量
|
|
|
|
|
// vec4 srcColor = texture(texture, v_uv0);
|
|
|
|
|
// if (srcColor.a <= glowThreshold) {
|
|
|
|
|
// return srcColor.a;
|
|
|
|
|
// }
|
2019-12-25 01:59:19 +00:00
|
|
|
|
|
|
|
|
|
// 将传入的指定距离,平均分成10圈,求出每一圈的平均透明度,
|
|
|
|
|
// 然后求和取平均值,那么就可以得到该点的平均透明度
|
|
|
|
|
float totalAlpha = 0.0;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void main () {
|
|
|
|
|
vec4 o = vec4(1, 1, 1, 1);
|
|
|
|
|
|
|
|
|
|
#if USE_TEXTURE
|
2020-02-17 02:05:42 +00:00
|
|
|
|
o *= texture(texture, v_uv0);
|
2019-12-25 01:59:19 +00:00
|
|
|
|
#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-25 02:20:03 +00:00
|
|
|
|
#if SHOW_OUTTER_GLOW
|
2019-12-25 01:59:19 +00:00
|
|
|
|
// 获取发光透明度
|
|
|
|
|
// 此时我们得到的是内部透明度为1,靠近边缘的为接近0的透明度,其他位置为0的透明度
|
|
|
|
|
float alpha = getGlowAlpha();
|
|
|
|
|
|
2019-12-25 02:51:37 +00:00
|
|
|
|
// 而外发光是从边缘开始的,那么什么算是边缘呢?
|
|
|
|
|
// 一般图像边缘是存在渐变,即从图像内 1.0-> 0.0 图像外,那么发光边缘我们可以这样子定义
|
|
|
|
|
// 当该点的透明度小于一个阈值,那么我们就当该点为一个发光点
|
|
|
|
|
if (alpha <= glowThreshold) {
|
|
|
|
|
// 然后以阈值作为标量,重新将透明度归一化
|
|
|
|
|
alpha = alpha / glowThreshold;
|
|
|
|
|
|
|
|
|
|
// 给点调料,让靠近边缘的更加亮
|
|
|
|
|
alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;
|
|
|
|
|
} else {
|
|
|
|
|
// 然后这里大于阈值的基本就是便是图像本身,不需要发光,那么将发光透明度置为0
|
|
|
|
|
alpha = 0.0;
|
|
|
|
|
}
|
2019-12-25 02:20:03 +00:00
|
|
|
|
|
2019-12-25 02:51:37 +00:00
|
|
|
|
// 忽略阈值,直接处理
|
|
|
|
|
// // 给点调料,让靠近边缘的更加亮
|
|
|
|
|
// alpha = -1.0 * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) * (alpha - 1.0) + 1.0;
|
2019-12-25 02:20:03 +00:00
|
|
|
|
|
|
|
|
|
// 外发光颜色
|
|
|
|
|
vec4 color_dest = glowColor * alpha;
|
|
|
|
|
vec4 color_src = o;
|
|
|
|
|
|
|
|
|
|
// 本次我们将先画外发光作为背景,然后在上方放图像
|
|
|
|
|
//
|
2019-12-25 01:59:19 +00:00
|
|
|
|
// 按照混合颜色规则 http://docs.cocos.com/creator/manual/zh/advanced-topics/ui-auto-batch.html#blend-%E6%A8%A1%E5%BC%8F
|
|
|
|
|
//
|
2019-12-25 02:20:03 +00:00
|
|
|
|
// 最终选择的混合模式如下:
|
|
|
|
|
//
|
|
|
|
|
// (原图像)color_src: GL_SRC_ALPHA
|
|
|
|
|
// (外发光)color_dest: GL_ONE_MINUS_SRC_ALPHAA
|
2019-12-25 01:59:19 +00:00
|
|
|
|
//
|
|
|
|
|
// 即最终颜色如下:
|
2019-12-25 02:20:03 +00:00
|
|
|
|
// color_src * GL_SRC_ALPHA + color_dest * GL_ONE_MINUS_SRC_ALPHAA
|
2019-12-25 01:59:19 +00:00
|
|
|
|
|
2019-12-25 02:20:03 +00:00
|
|
|
|
gl_FragColor = color_src * color_src.a + color_dest * (1.0 - color_src.a);
|
2019-12-25 01:59:19 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}%
|