CocosCreator-Shader-Effect-.../assets/effects/sprite-local-diffusion.effect
2020-01-12 00:36:08 +08:00

165 lines
3.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
// 点光/点扩散
// 原理:
// 1. 画圆
// 2. 圆心高亮(透明度=1.0),圆边缘不亮(透明度=0.0
// 4. 在原图像上方叠加圆
CCEffect %{
techniques:
- passes:
- vert: vs
frag: fs
blendState:
targets:
- blend: true
rasterizerState:
cullMode: none
properties:
texture: { value: white }
alphaThreshold: { value: 0.5 }
# 扩散颜色
centerColor: {
value: [1.0, 1.0, 0.0, 1.0],
inspector: {
type: color,
tooltip: "发光颜色"
}
}
# 扩散起点坐标
centerPoint: {
value: [0.2, 0.2],
inspector: {
tooltip: "扩散起点坐标"
}
}
# 扩散半径
radius: {
value: 0.4,
inspector: {
tooltip: "扩散半径"
}
}
}%
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
#if ENABLE_DIFFUSION
uniform Diffusion {
// 扩散颜色
vec4 centerColor;
// 扩散起点坐标
vec2 centerPoint;
// 扩展半径
float radius;
// 裁剪掉透明区域上的点光
// ps编辑器还不支持 bool 类型的样子,因此没在 CCEffect 中定义 cropAlpha
bool cropAlpha;
}
/**
* 添加某个扩散点后混合后的纹理颜色
*/
vec4 addDiffusionColor(vec4 textureColor, vec2 centerPoint, float radius, vec4 centerColor) {
// 计算当前 uv 到扩散起点的距离
float dis = distance(v_uv0, centerPoint);
float a = 1.0 ;
// 裁剪掉透明区域上的点光
if (cropAlpha) {
a *= step(0.01, textureColor.a);
}
// 裁剪掉扩散范围外的uv
// a *= step(dis, radius);
// 加入从中心往外渐变的效果
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;
}
#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;
#if ENABLE_DIFFUSION
gl_FragColor = addDiffusionColor(gl_FragColor, centerPoint, radius, centerColor);
#endif
}
}%