CocosCreator-Shader-Effect-.../assets/effects/sprite-point-light.effect

167 lines
3.3 KiB
Plaintext
Raw Normal View History

2020-01-10 09:44:43 +00:00
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2020-01-11 10:58:30 +00:00
// 点光/点扩散
2020-01-10 09:44:43 +00:00
// 原理:
2020-01-11 10:58:30 +00:00
// 1. 画圆
// 2. 圆心高亮(透明度=1.0),圆边缘不亮(透明度=0.0
2020-01-12 01:01:30 +00:00
// 3. 在原图像上方叠加圆
2020-01-10 09:44:43 +00:00
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: {
2020-01-11 16:35:56 +00:00
value: [1.0, 1.0, 0.0, 1.0],
editor: {
2020-01-10 15:05:44 +00:00
type: color,
tooltip: "发光颜色"
}
}
# 扩散起点坐标
centerPoint: {
2020-01-11 16:35:56 +00:00
value: [0.2, 0.2],
editor: {
2020-01-10 15:05:44 +00:00
tooltip: "扩散起点坐标"
}
}
# 扩散半径
radius: {
2020-01-11 16:35:56 +00:00
value: 0.4,
editor: {
2020-01-10 15:05:44 +00:00
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-11 16:35:56 +00:00
// 裁剪掉透明区域上的点光
2020-01-12 00:05:29 +00:00
// ps编辑器还不支持 bool 类型的样子,因此没在 CCEffect 中定义
bool cropAlpha;
// 是否启用迷雾效果
// ps编辑器还不支持 bool 类型的样子,因此没在 CCEffect 中定义
bool enableFog;
2020-02-17 01:32:23 +00:00
};
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-13 14:40:10 +00:00
vec4 addLightColor(vec4 textureColor, vec2 centerPoint, float radius, vec4 centerColor) {
2020-01-13 15:44:17 +00:00
// 计算当前 uv 到圆心起点的距离
2020-01-10 15:05:44 +00:00
float dis = distance(v_uv0, centerPoint);
2020-01-11 16:35:56 +00:00
float a = 1.0 ;
2020-01-12 00:05:29 +00:00
// 裁剪掉透明区域上的点光
2020-01-11 16:35:56 +00:00
if (cropAlpha) {
a *= step(0.01, textureColor.a);
}
2020-01-10 15:05:44 +00:00
2020-01-13 15:44:17 +00:00
// 裁剪掉圆范围外的uv迷雾效果
2020-01-12 00:05:29 +00:00
if (!enableFog) {
a *= step(dis, radius);
}
2020-01-10 15:05:44 +00:00
// 加入从中心往外渐变的效果
a *= 1.0 - (dis / radius);
2020-01-13 15:44:17 +00:00
// 计算出圆范围内,不同 uv 对应的实际颜色值
vec4 lightColor = centerColor * a;
2020-01-10 15:05:44 +00:00
2020-01-13 15:44:17 +00:00
// 混合颜色:在原始图像颜色上叠加圆颜色
return textureColor * textureColor.a + lightColor;
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
2020-01-13 14:40:10 +00:00
gl_FragColor = addLightColor(gl_FragColor, centerPoint, radius, centerColor);
2020-01-10 15:05:44 +00:00
#endif
2020-01-10 09:44:43 +00:00
}
}%