CocosCreator-Shader-Effect-.../assets/effects/sprite-local-diffusion.effect
2020-01-11 18:58:30 +08:00

163 lines
3.4 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
// 3. 在原图像上方叠加圆
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, 0.0, 0.0, 1.0],
inspector: {
type: color,
tooltip: "发光颜色"
}
}
# 扩散起点坐标
centerPoint: {
value: [0.5, 0.5],
inspector: {
tooltip: "扩散起点坐标"
}
}
# 扩散半径
radius: {
value: 0.2,
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;
// // 因为现在cc不支持 vec2 数组,所以只能用 vec4 数组
// // 而一个 vec4 就包含两个 vec2 ,共计 4 个属性所以如果数组是4就表示有8个点了
// vec4 points[4];
// 扩散起点坐标
vec2 centerPoint;
// 扩展半径
float radius;
}
/**
* 添加某个扩散点后混合后的纹理颜色
*/
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;
}
#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
}
}%