CocosCreator-Shader-Effect-.../assets/effects/sprite-point-light.effect
2020-02-17 10:23:27 +08:00

183 lines
3.7 KiB
Plaintext
Raw Permalink 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, 1.0, 0.0, 1.0],
editor: {
type: color,
tooltip: "发光颜色"
}
}
# 扩散起点坐标
centerPoint: {
value: [0.2, 0.2],
editor: {
tooltip: "扩散起点坐标"
}
}
# 扩散半径
radius: {
value: 0.4,
editor: {
tooltip: "扩散半径"
}
}
# 裁剪掉透明区域上的光
cropAlpha: {
value: 1.0,
editor: {
tooltip: "是否裁剪透明区域上的光。0不启用非0启用"
}
}
# 是否启用迷雾效果
enableFog: {
value: 0.0,
editor: {
tooltip: "是否启用迷雾效果。0不启用非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;
#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 类型的样子因此用float来定义
float cropAlpha;
// 是否启用迷雾效果
// ps编辑器还不支持 bool 类型的样子因此用float来定义
float enableFog;
};
/**
* 添加某个扩散点后混合后的纹理颜色
*/
vec4 addLightColor(vec4 textureColor, vec2 centerPoint, float radius, vec4 centerColor) {
// 计算当前 uv 到圆心起点的距离
float dis = distance(v_uv0, centerPoint);
float a = 1.0 ;
// 裁剪掉透明区域上的点光
if (bool(cropAlpha)) {
a *= step(0.01, textureColor.a);
}
// 裁剪掉光束范围外的uv迷雾效果
if (!bool(enableFog)) {
a *= step(dis, radius);
}
// 加入从中心往外渐变的效果
a *= 1.0 - (dis / radius);
// 计算出圆范围内,不同 uv 对应的实际颜色值
vec4 lightColor = centerColor * a;
// 混合颜色:在原始图像颜色上叠加圆颜色
return textureColor * textureColor.a + lightColor;
}
#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 = addLightColor(gl_FragColor, centerPoint, radius, centerColor);
#endif
}
}%