CocosCreator-Shader-Effect-.../assets/effects/sprite-local-diffusion.effect

163 lines
3.6 KiB
Plaintext
Raw Normal View History

2020-01-10 09:44:43 +00:00
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
// 老照片特效
// 原理:
// r = 0.393 * r + 0.769 * g + 0.189 * b;
// g = 0.349 * r + 0.686 * g + 0.168 * b;
// b = 0.272 * r + 0.534 * g + 0.131 * b;
CCEffect %{
techniques:
- passes:
- vert: vs
frag: fs
blendState:
targets:
- blend: true
rasterizerState:
cullMode: none
properties:
texture: { value: white }
alphaThreshold: { value: 0.5 }
2020-01-10 15:05:44 +00:00
# 扩散颜色
centerColor: {
value: [1.0, 1.0, 0.0, 1.0],
inspector: {
type: color,
tooltip: "发光颜色"
}
}
# 扩散起点坐标
centerPoint: {
value: [0.5, 0.5],
2020-01-10 09:44:43 +00:00
inspector: {
2020-01-10 15:05:44 +00:00
tooltip: "扩散起点坐标"
}
}
# 扩散半径
radius: {
value: 0.2,
inspector: {
tooltip: "扩散半径"
2020-01-10 09:44:43 +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
2020-01-10 15:05:44 +00:00
#if ENABLE_DIFFUSION
uniform Diffusion {
// 扩散颜色
vec4 centerColor;
// 扩散起点坐标
vec2 centerPoint;
// 扩展半径
float radius;
2020-01-10 09:44:43 +00:00
}
/**
2020-01-10 15:05:44 +00:00
* 添加某个扩散点后混合后的纹理颜色
2020-01-10 09:44:43 +00:00
*/
2020-01-10 15:05:44 +00:00
vec4 addDiffusionColor(vec4 textureColor, vec2 centerPoint, float radius, vec4 centerColor) {
// 计算当前 uv 到扩散起点的距离
float dis = distance(v_uv0, centerPoint);
// 第一步裁剪
// 找出扩散范围扩散范围为1.0透明度其他范围为0.0透明度
float a = step(dis, radius);
// // 第二步裁剪
// // 本来透明的uv不要
// a *= step(0.01, textureColor.a);
// 加入从中心往外渐变的效果
a *= 1.0 - (dis / radius);
// 加点料,让中心点更加亮
// a = -1.0 * (a - 1.0) * (a - 1.0) + 1.0;
// a = -1.0 * (a - 1.0) * (a - 1.0) * (a - 1.0) * (a - 1.0) + 1.0;
// 计算出扩散范围内,不同 uv 对应的实际扩散颜色值
vec4 diffusionColor = centerColor * a;
// 混合颜色:在原始图像颜色上叠加扩散颜色
return textureColor * textureColor.a + diffusionColor;
2020-01-10 09:44:43 +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;
2020-01-10 15:05:44 +00:00
#if ENABLE_DIFFUSION
gl_FragColor = addDiffusionColor(gl_FragColor, centerPoint, radius, centerColor);
// gl_FragColor = addDiffusionColor(gl_FragColor, vec2(0.7, 0.), 0.2, vec4(1.0, 1.0, 0.0, 1.0));
// gl_FragColor = addDiffusionColor(gl_FragColor, vec2(0.8, 0.), 0.2, vec4(1.0, 1.0, 0.0, 1.0));
// gl_FragColor = addDiffusionColor(gl_FragColor, vec2(0.9, 0.), 0.2, vec4(1.0, 1.0, 0.0, 1.0));
// gl_FragColor = addDiffusionColor(gl_FragColor, vec2(0.2, 0.), 0.2, vec4(1.0, 1.0, 0.0, 1.0));
#endif
2020-01-10 09:44:43 +00:00
}
}%