Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
70a3b95f10 | ||
|
6402c68fbc | ||
|
dccf05cfde | ||
|
1ff0ee3d40 | ||
|
209e22fcca | ||
|
e7e084b958 | ||
|
acf72e0e4f | ||
|
c95329e7ab | ||
|
9160ad38b7 | ||
|
f6f93bad2c | ||
|
45e22b866a | ||
|
9ca7bd9a02 | ||
|
69b863c490 | ||
|
6286aebb8c | ||
|
ae865a7c5f | ||
|
40e905fc81 | ||
|
9f28ef4305 | ||
|
ba90b2170c | ||
|
7e197c0892 | ||
|
6ac69a841a | ||
|
f39e39e248 | ||
|
27e819fb2c |
@@ -1,5 +1,13 @@
|
|||||||
# ChangeLog
|
# ChangeLog
|
||||||
|
|
||||||
|
## 0.5.0 (2020-01-13)
|
||||||
|
|
||||||
|
- 加入扫光特效
|
||||||
|
|
||||||
|
## 0.4.0 (2020-01-12)
|
||||||
|
|
||||||
|
- 加入点光特效
|
||||||
|
|
||||||
## 0.3.0 (2020-01-09)
|
## 0.3.0 (2020-01-09)
|
||||||
|
|
||||||
- 加入圆角裁剪特效
|
- 加入圆角裁剪特效
|
||||||
|
25
README.md
25
README.md
@@ -1,16 +1,16 @@
|
|||||||
# Cocos Creator Shader Effect Demo
|
# Cocos Creator Shader Effect Demo
|
||||||
|
|
||||||
[](CHANGELOG.md)
|
[](CHANGELOG.md)
|
||||||
[](LICENSE)
|
[](LICENSE)
|
||||||
[](http://www.cocos.com/creator)
|
[](http://www.cocos.com/creator)
|
||||||
|
|
||||||
## 项目说明
|
## 一、项目说明
|
||||||
|
|
||||||
1. 此项目为我在学习过程中的一些分享和实现,因此项目名字以 **`Demo`** 为后缀。
|
1. 此项目为我在学习过程中的一些分享和实现,因此项目名字以 **`Demo`** 为后缀。
|
||||||
2. 项目重点在于 **「渔」**,不在于 **「鱼」** 。
|
2. 项目重点在于 **「渔」**,不在于 **「鱼」** 。
|
||||||
3. 如果你有意将此Demo中的效果加入到你的项目中,**请认真评估是否适合你的项目使用!**
|
3. 如果你有意将此Demo中的效果加入到你的项目中,**请认真评估是否适合你的项目使用!**
|
||||||
|
|
||||||
## 系列文章
|
## 二、系列文章
|
||||||
|
|
||||||
* [Cocos Creator Shader Effect 系列 - 0 - 前言](https://www.jianshu.com/p/20b906d7269c)
|
* [Cocos Creator Shader Effect 系列 - 0 - 前言](https://www.jianshu.com/p/20b906d7269c)
|
||||||
* [Cocos Creator Shader Effect 系列 - 1 - 材质,Effect,Inspector,纹理之间的关系](https://www.jianshu.com/p/ca28666d25d2)
|
* [Cocos Creator Shader Effect 系列 - 1 - 材质,Effect,Inspector,纹理之间的关系](https://www.jianshu.com/p/ca28666d25d2)
|
||||||
@@ -22,11 +22,18 @@
|
|||||||
|
|
||||||
* 编写中...
|
* 编写中...
|
||||||
|
|
||||||
|
|
||||||
## 特效预览
|
|
||||||
|
|
||||||
那么现在,我们先来轻松地看下有哪些特效效果吧~
|
那么现在,我们先来轻松地看下有哪些特效效果吧~
|
||||||
|
|
||||||
|
## 三、特效预览
|
||||||
|
|
||||||
|
### 扫光(实现原理,可能在编写中,催更可到底部扫码,支持急件~🤣)(2020.01.13更新)
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### 点光(实现原理,可能在编写中,催更可到底部扫码,支持急件~🤣)(2020.01.12更新)
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
### 内发光([实现原理](https://www.jianshu.com/p/326b73f86ecc))
|
### 内发光([实现原理](https://www.jianshu.com/p/326b73f86ecc))
|
||||||
|
|
||||||

|

|
||||||
@@ -56,16 +63,14 @@
|
|||||||
|
|
||||||
### 外描边(完善中...)
|
### 外描边(完善中...)
|
||||||
|
|
||||||
## TODO
|
## 四、TODO
|
||||||
|
|
||||||
* [ ] 图像裁剪
|
|
||||||
* [ ] 图像模糊
|
* [ ] 图像模糊
|
||||||
* [ ] 闪光
|
|
||||||
* [ ] 波浪
|
* [ ] 波浪
|
||||||
* [ ] 雨滴
|
* [ ] 雨滴
|
||||||
* [ ] ...
|
* [ ] ...
|
||||||
|
|
||||||
## 支持一下作者吧
|
## 五、支持一下作者吧
|
||||||
|
|
||||||
如果此项目对你学习和理解Shader有帮助,不妨支持一下我吧~
|
如果此项目对你学习和理解Shader有帮助,不妨支持一下我吧~
|
||||||
|
|
||||||
|
235
assets/effects/sprite-flash-light.effect
Normal file
235
assets/effects/sprite-flash-light.effect
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
// 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 }
|
||||||
|
|
||||||
|
# 光束颜色
|
||||||
|
lightColor: {
|
||||||
|
value: [1.0, 1.0, 0.0, 1.0],
|
||||||
|
inspector: {
|
||||||
|
type: color,
|
||||||
|
tooltip: "光束颜色"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 光束中心点坐标
|
||||||
|
lightCenterPoint: {
|
||||||
|
value: [0.2, 0.2],
|
||||||
|
inspector: {
|
||||||
|
tooltip: "光束中心点坐标"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 光束倾斜角度
|
||||||
|
lightAngle: {
|
||||||
|
value: 36.0,
|
||||||
|
inspctor: {
|
||||||
|
tooltip: "光束倾斜角度",
|
||||||
|
range: [0.0, 1.0],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 光束宽度
|
||||||
|
lightWidth: {
|
||||||
|
value: 0.2,
|
||||||
|
inspector: {
|
||||||
|
tooltip: "光束宽度"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 启用光束渐变
|
||||||
|
enableGradient: {
|
||||||
|
value: 1.0,
|
||||||
|
inspecator: {
|
||||||
|
tooltip: "是否启用光束渐变。0:不启用,非0:启用"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 裁剪掉透明区域上的光
|
||||||
|
cropAlpha: {
|
||||||
|
value: 1.0,
|
||||||
|
inspecator: {
|
||||||
|
tooltip: "是否裁剪透明区域上的光。0:不启用,非0:启用"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 是否启用迷雾效果
|
||||||
|
enableFog: {
|
||||||
|
value: 0.0,
|
||||||
|
inspecator: {
|
||||||
|
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_LIGHT
|
||||||
|
uniform Light {
|
||||||
|
// 光束颜色
|
||||||
|
vec4 lightColor;
|
||||||
|
|
||||||
|
// 光束中心点坐标
|
||||||
|
vec2 lightCenterPoint;
|
||||||
|
|
||||||
|
// 光束倾斜角度
|
||||||
|
float lightAngle;
|
||||||
|
|
||||||
|
// 光束宽度
|
||||||
|
float lightWidth;
|
||||||
|
|
||||||
|
// 启用光束渐变
|
||||||
|
// ps:编辑器还不支持 bool 类型的样子,因此用float来定义
|
||||||
|
float enableGradient;
|
||||||
|
|
||||||
|
// 裁剪掉透明区域上的光
|
||||||
|
// ps:编辑器还不支持 bool 类型的样子,因此用float来定义
|
||||||
|
float cropAlpha;
|
||||||
|
|
||||||
|
// 是否启用迷雾效果
|
||||||
|
// ps:编辑器还不支持 bool 类型的样子,因此用float来定义
|
||||||
|
float enableFog;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加光束颜色
|
||||||
|
*/
|
||||||
|
vec4 addLightColor(vec4 textureColor, vec4 lightColor, vec2 lightCenterPoint, float lightAngle, float lightWidth) {
|
||||||
|
// 边界值处理,没有宽度就返回原始颜色
|
||||||
|
if (lightWidth <= 0.0) {
|
||||||
|
return textureColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算当前 uv 到 光束 的距离
|
||||||
|
float angleInRadians = radians(lightAngle);
|
||||||
|
|
||||||
|
// 角度0与非0不同处理
|
||||||
|
float dis = 0.0;
|
||||||
|
if (mod(lightAngle, 180.0) != 0.0) {
|
||||||
|
// 计算光束中心线下方与X轴交点的X坐标
|
||||||
|
// 1.0 - lightCenterPoint.y 是将转换为OpenGL坐标系,下文的 1.0 - y 类似
|
||||||
|
float lightOffsetX = lightCenterPoint.x - ((1.0 - lightCenterPoint.y) / tan(angleInRadians));
|
||||||
|
|
||||||
|
// 以当前点画一条平行于X轴的线,假设此线和光束中心线相交的点为D点
|
||||||
|
// 那么
|
||||||
|
// D.y = uv0.y
|
||||||
|
// D.x = lightOffsetX + D.y / tan(angle)
|
||||||
|
float dx = lightOffsetX + (1.0 - v_uv0.y) / tan(angleInRadians);
|
||||||
|
|
||||||
|
// D 到当前 uv0 的距离就是
|
||||||
|
// dis = |uv0.x - D.x|
|
||||||
|
float offsetDis = abs(v_uv0.x - dx);
|
||||||
|
|
||||||
|
// 当前点到光束中心线的的垂直距离就好算了
|
||||||
|
dis = sin(angleInRadians) * offsetDis;
|
||||||
|
} else {
|
||||||
|
dis = abs(v_uv0.y - lightCenterPoint.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
float a = 1.0 ;
|
||||||
|
// 裁剪掉透明区域上的点光
|
||||||
|
if (bool(cropAlpha)) {
|
||||||
|
a *= step(0.01, textureColor.a);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 裁剪掉光束范围外的uv(迷雾效果)
|
||||||
|
if (!bool(enableFog)) {
|
||||||
|
a *= step(dis, lightWidth * 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入从中心往外渐变的效果
|
||||||
|
if (bool(enableGradient)) {
|
||||||
|
a *= 1.0 - dis / (lightWidth * 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算出扩散范围内,不同 uv 对应的实际扩散颜色值
|
||||||
|
vec4 finalLightColor = lightColor * a;
|
||||||
|
|
||||||
|
// 混合颜色:在原始图像颜色上叠加扩散颜色
|
||||||
|
return textureColor * textureColor.a + finalLightColor;
|
||||||
|
}
|
||||||
|
#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_LIGHT
|
||||||
|
gl_FragColor = addLightColor(gl_FragColor, lightColor, lightCenterPoint, lightAngle, lightWidth);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}%
|
17
assets/effects/sprite-flash-light.effect.meta
Normal file
17
assets/effects/sprite-flash-light.effect.meta
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.23",
|
||||||
|
"uuid": "e9682cd1-a19c-4fcb-ad8c-cf1783b805e6",
|
||||||
|
"compiledShaders": [
|
||||||
|
{
|
||||||
|
"glsl1": {
|
||||||
|
"vert": "\nprecision highp float;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matWorld;\n\nattribute vec3 a_position;\nattribute vec4 a_color;\nvarying vec4 v_color;\n\n#if USE_TEXTURE\nattribute vec2 a_uv0;\nvarying vec2 v_uv0;\n#endif\n\nvoid main () {\n vec4 pos = vec4(a_position, 1);\n\n #if CC_USE_MODEL\n pos = cc_matViewProj * cc_matWorld * pos;\n #else\n pos = cc_matViewProj * pos;\n #endif\n\n #if USE_TEXTURE\n v_uv0 = a_uv0;\n #endif\n\n v_color = a_color;\n\n gl_Position = pos;\n}\n",
|
||||||
|
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform float alphaThreshold;\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nvarying vec4 v_color;\n\n#if USE_TEXTURE\nvarying vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_LIGHT\nuniform vec4 lightColor;\nuniform vec2 lightCenterPoint;\nuniform float lightAngle;\nuniform float lightWidth;\nuniform float enableGradient;\nuniform float cropAlpha;\nuniform float enableFog;\n/**\n * 添加光束颜色\n */\nvec4 addLightColor(vec4 textureColor, vec4 lightColor, vec2 lightCenterPoint, float lightAngle, float lightWidth) {\n\n if (lightWidth <= 0.0) {\n return textureColor;\n }\n\n float angleInRadians = radians(lightAngle);\n\n float dis = 0.0;\n if (mod(lightAngle, 180.0) != 0.0) {\n\n float lightOffsetX = lightCenterPoint.x - ((1.0 - lightCenterPoint.y) / tan(angleInRadians));\n\n float dx = lightOffsetX + (1.0 - v_uv0.y) / tan(angleInRadians);\n\n float offsetDis = abs(v_uv0.x - dx);\n\n dis = sin(angleInRadians) * offsetDis;\n } else {\n dis = abs(v_uv0.y - lightCenterPoint.y);\n }\n \n float a = 1.0 ;\n\n if (bool(cropAlpha)) {\n a *= step(0.01, textureColor.a);\n }\n\n if (!bool(enableFog)) {\n a *= step(dis, lightWidth * 0.5);\n }\n\n if (bool(enableGradient)) {\n a *= 1.0 - dis / (lightWidth * 0.5);\n }\n\n vec4 finalLightColor = lightColor * a;\n\n return textureColor * textureColor.a + finalLightColor;\n}\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture2D(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n gl_FragColor = o;\n \n #if ENABLE_LIGHT\n gl_FragColor = addLightColor(gl_FragColor, lightColor, lightCenterPoint, lightAngle, lightWidth);\n #endif\n}\n"
|
||||||
|
},
|
||||||
|
"glsl3": {
|
||||||
|
"vert": "\nprecision highp float;\nuniform CCGlobal {\n vec4 cc_time;\n\n vec4 cc_screenSize;\n\n vec4 cc_screenScale;\n\n vec4 cc_nativeSize;\n\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n\n vec4 cc_exposure;\n\n vec4 cc_mainLitDir;\n\n vec4 cc_mainLitColor;\n\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n\nin vec3 a_position;\nin vec4 a_color;\nout vec4 v_color;\n\n#if USE_TEXTURE\nin vec2 a_uv0;\nout vec2 v_uv0;\n#endif\n\nvoid main () {\n vec4 pos = vec4(a_position, 1);\n\n #if CC_USE_MODEL\n pos = cc_matViewProj * cc_matWorld * pos;\n #else\n pos = cc_matViewProj * pos;\n #endif\n\n #if USE_TEXTURE\n v_uv0 = a_uv0;\n #endif\n\n v_color = a_color;\n\n gl_Position = pos;\n}\n",
|
||||||
|
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform ALPHA_TEST {\n float alphaThreshold;\n }\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nin vec4 v_color;\n\n#if USE_TEXTURE\nin vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_LIGHT\nuniform Light {\n\n vec4 lightColor;\n\n vec2 lightCenterPoint;\n\n float lightAngle;\n\n float lightWidth;\n\n float enableGradient;\n\n float cropAlpha;\n\n float enableFog;\n}\n\n/**\n * 添加光束颜色\n */\nvec4 addLightColor(vec4 textureColor, vec4 lightColor, vec2 lightCenterPoint, float lightAngle, float lightWidth) {\n\n if (lightWidth <= 0.0) {\n return textureColor;\n }\n\n float angleInRadians = radians(lightAngle);\n\n float dis = 0.0;\n if (mod(lightAngle, 180.0) != 0.0) {\n\n float lightOffsetX = lightCenterPoint.x - ((1.0 - lightCenterPoint.y) / tan(angleInRadians));\n\n float dx = lightOffsetX + (1.0 - v_uv0.y) / tan(angleInRadians);\n\n float offsetDis = abs(v_uv0.x - dx);\n\n dis = sin(angleInRadians) * offsetDis;\n } else {\n dis = abs(v_uv0.y - lightCenterPoint.y);\n }\n \n float a = 1.0 ;\n\n if (bool(cropAlpha)) {\n a *= step(0.01, textureColor.a);\n }\n\n if (!bool(enableFog)) {\n a *= step(dis, lightWidth * 0.5);\n }\n\n if (bool(enableGradient)) {\n a *= 1.0 - dis / (lightWidth * 0.5);\n }\n\n vec4 finalLightColor = lightColor * a;\n\n return textureColor * textureColor.a + finalLightColor;\n}\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n gl_FragColor = o;\n \n #if ENABLE_LIGHT\n gl_FragColor = addLightColor(gl_FragColor, lightColor, lightCenterPoint, lightAngle, lightWidth);\n #endif\n}\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
166
assets/effects/sprite-point-light.effect
Normal file
166
assets/effects/sprite-point-light.effect
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
// 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],
|
||||||
|
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 中定义
|
||||||
|
bool cropAlpha;
|
||||||
|
|
||||||
|
// 是否启用迷雾效果
|
||||||
|
// ps:编辑器还不支持 bool 类型的样子,因此没在 CCEffect 中定义
|
||||||
|
bool enableFog;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加某个扩散点后混合后的纹理颜色
|
||||||
|
*/
|
||||||
|
vec4 addLightColor(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(迷雾效果)
|
||||||
|
if (!enableFog) {
|
||||||
|
a *= step(dis, radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入从中心往外渐变的效果
|
||||||
|
a *= 1.0 - (dis / radius);
|
||||||
|
|
||||||
|
// 计算出扩散范围内,不同 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 = addLightColor(gl_FragColor, centerPoint, radius, centerColor);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}%
|
17
assets/effects/sprite-point-light.effect.meta
Normal file
17
assets/effects/sprite-point-light.effect.meta
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.23",
|
||||||
|
"uuid": "72a182fc-08a6-4faa-8e36-8bd84b4a0b53",
|
||||||
|
"compiledShaders": [
|
||||||
|
{
|
||||||
|
"glsl1": {
|
||||||
|
"vert": "\nprecision highp float;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matWorld;\n\nattribute vec3 a_position;\nattribute vec4 a_color;\nvarying vec4 v_color;\n\n#if USE_TEXTURE\nattribute vec2 a_uv0;\nvarying vec2 v_uv0;\n#endif\n\nvoid main () {\n vec4 pos = vec4(a_position, 1);\n\n #if CC_USE_MODEL\n pos = cc_matViewProj * cc_matWorld * pos;\n #else\n pos = cc_matViewProj * pos;\n #endif\n\n #if USE_TEXTURE\n v_uv0 = a_uv0;\n #endif\n\n v_color = a_color;\n\n gl_Position = pos;\n}\n",
|
||||||
|
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform float alphaThreshold;\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nvarying vec4 v_color;\n\n#if USE_TEXTURE\nvarying vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_DIFFUSION\nuniform vec4 centerColor;\nuniform vec2 centerPoint;\nuniform float radius;\nuniform bool cropAlpha;\nuniform bool enableFog;\n/**\n * 添加某个扩散点后混合后的纹理颜色\n */\nvec4 addLightColor(vec4 textureColor, vec2 centerPoint, float radius, vec4 centerColor) {\n\n float dis = distance(v_uv0, centerPoint);\n\n float a = 1.0 ;\n\n if (cropAlpha) {\n a *= step(0.01, textureColor.a);\n }\n\n if (!enableFog) {\n a *= step(dis, radius);\n }\n\n a *= 1.0 - (dis / radius);\n\n vec4 diffusionColor = centerColor * a;\n\n return textureColor * textureColor.a + diffusionColor;\n}\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture2D(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n gl_FragColor = o;\n \n #if ENABLE_DIFFUSION\n gl_FragColor = addLightColor(gl_FragColor, centerPoint, radius, centerColor);\n #endif\n}\n"
|
||||||
|
},
|
||||||
|
"glsl3": {
|
||||||
|
"vert": "\nprecision highp float;\nuniform CCGlobal {\n vec4 cc_time;\n\n vec4 cc_screenSize;\n\n vec4 cc_screenScale;\n\n vec4 cc_nativeSize;\n\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n\n vec4 cc_exposure;\n\n vec4 cc_mainLitDir;\n\n vec4 cc_mainLitColor;\n\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n\nin vec3 a_position;\nin vec4 a_color;\nout vec4 v_color;\n\n#if USE_TEXTURE\nin vec2 a_uv0;\nout vec2 v_uv0;\n#endif\n\nvoid main () {\n vec4 pos = vec4(a_position, 1);\n\n #if CC_USE_MODEL\n pos = cc_matViewProj * cc_matWorld * pos;\n #else\n pos = cc_matViewProj * pos;\n #endif\n\n #if USE_TEXTURE\n v_uv0 = a_uv0;\n #endif\n\n v_color = a_color;\n\n gl_Position = pos;\n}\n",
|
||||||
|
"frag": "\nprecision highp float;\n\n#if USE_ALPHA_TEST\n \n uniform ALPHA_TEST {\n float alphaThreshold;\n }\n#endif\n\nvoid ALPHA_TEST (in vec4 color) {\n #if USE_ALPHA_TEST\n if (color.a < alphaThreshold) discard;\n #endif\n}\n\nvoid ALPHA_TEST (in float alpha) {\n #if USE_ALPHA_TEST\n if (alpha < alphaThreshold) discard;\n #endif\n}\n\nin vec4 v_color;\n\n#if USE_TEXTURE\nin vec2 v_uv0;\nuniform sampler2D texture;\n#endif\n\n#if ENABLE_DIFFUSION\nuniform Diffusion {\n\n vec4 centerColor;\n\n vec2 centerPoint;\n\n float radius;\n\n bool cropAlpha;\n\n bool enableFog;\n}\n\n/**\n * 添加某个扩散点后混合后的纹理颜色\n */\nvec4 addLightColor(vec4 textureColor, vec2 centerPoint, float radius, vec4 centerColor) {\n\n float dis = distance(v_uv0, centerPoint);\n\n float a = 1.0 ;\n\n if (cropAlpha) {\n a *= step(0.01, textureColor.a);\n }\n\n if (!enableFog) {\n a *= step(dis, radius);\n }\n\n a *= 1.0 - (dis / radius);\n\n vec4 diffusionColor = centerColor * a;\n\n return textureColor * textureColor.a + diffusionColor;\n}\n#endif\n\nvoid main () {\n vec4 o = vec4(1, 1, 1, 1);\n\n #if USE_TEXTURE\n o *= texture(texture, v_uv0);\n #if CC_USE_ALPHA_ATLAS_TEXTURE\n o.a *= texture2D(texture, v_uv0 + vec2(0, 0.5)).r;\n #endif\n #endif\n\n o *= v_color;\n\n ALPHA_TEST(o);\n\n gl_FragColor = o;\n \n #if ENABLE_DIFFUSION\n gl_FragColor = addLightColor(gl_FragColor, centerPoint, radius, centerColor);\n #endif\n}\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
24
assets/materials/sprite-flash-light.mtl
Normal file
24
assets/materials/sprite-flash-light.mtl
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"__type__": "cc.Material",
|
||||||
|
"_name": "",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"_native": "",
|
||||||
|
"_effectAsset": {
|
||||||
|
"__uuid__": "e9682cd1-a19c-4fcb-ad8c-cf1783b805e6"
|
||||||
|
},
|
||||||
|
"_defines": {
|
||||||
|
"USE_TEXTURE": true,
|
||||||
|
"ENABLE_LIGHT": true
|
||||||
|
},
|
||||||
|
"_props": {
|
||||||
|
"lightColor": {
|
||||||
|
"__type__": "cc.Color",
|
||||||
|
"r": 255,
|
||||||
|
"g": 245,
|
||||||
|
"b": 0,
|
||||||
|
"a": 255
|
||||||
|
},
|
||||||
|
"lightAngle": 36,
|
||||||
|
"lightWidth": 0.2
|
||||||
|
}
|
||||||
|
}
|
6
assets/materials/sprite-flash-light.mtl.meta
Normal file
6
assets/materials/sprite-flash-light.mtl.meta
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.2",
|
||||||
|
"uuid": "43a22f18-72fc-4399-b5ae-8305705861f4",
|
||||||
|
"dataAsSubAsset": null,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
16
assets/materials/sprite-point-light.mtl
Normal file
16
assets/materials/sprite-point-light.mtl
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"__type__": "cc.Material",
|
||||||
|
"_name": "",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"_native": "",
|
||||||
|
"_effectAsset": {
|
||||||
|
"__uuid__": "72a182fc-08a6-4faa-8e36-8bd84b4a0b53"
|
||||||
|
},
|
||||||
|
"_defines": {
|
||||||
|
"USE_TEXTURE": true,
|
||||||
|
"ENABLE_DIFFUSION": true
|
||||||
|
},
|
||||||
|
"_props": {
|
||||||
|
"radius": 0.4
|
||||||
|
}
|
||||||
|
}
|
6
assets/materials/sprite-point-light.mtl.meta
Normal file
6
assets/materials/sprite-point-light.mtl.meta
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.2",
|
||||||
|
"uuid": "d88d982e-4ed7-4f90-9566-2ac2fef4bcb3",
|
||||||
|
"dataAsSubAsset": null,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
7640
assets/scenes/FlashLightEffectScene.fire
Executable file
7640
assets/scenes/FlashLightEffectScene.fire
Executable file
File diff suppressed because it is too large
Load Diff
7
assets/scenes/FlashLightEffectScene.fire.meta
Normal file
7
assets/scenes/FlashLightEffectScene.fire.meta
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.2.5",
|
||||||
|
"uuid": "54bdad42-93aa-4869-a465-c0eac37bf0d2",
|
||||||
|
"asyncLoadAssets": false,
|
||||||
|
"autoReleaseAssets": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
6290
assets/scenes/PointLightEffectScene.fire
Executable file
6290
assets/scenes/PointLightEffectScene.fire
Executable file
File diff suppressed because it is too large
Load Diff
7
assets/scenes/PointLightEffectScene.fire.meta
Normal file
7
assets/scenes/PointLightEffectScene.fire.meta
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.2.5",
|
||||||
|
"uuid": "7d64bd80-8e2c-4c7c-8d65-ea69b0c4b3d3",
|
||||||
|
"asyncLoadAssets": false,
|
||||||
|
"autoReleaseAssets": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
@@ -78,10 +78,10 @@
|
|||||||
"_active": true,
|
"_active": true,
|
||||||
"_components": [
|
"_components": [
|
||||||
{
|
{
|
||||||
"__id__": 60
|
"__id__": 67
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 61
|
"__id__": 68
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_prefab": null,
|
"_prefab": null,
|
||||||
@@ -171,7 +171,7 @@
|
|||||||
"array": [
|
"array": [
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
418.2902700278839,
|
491.0364039457767,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
@@ -245,13 +245,13 @@
|
|||||||
"__id__": 9
|
"__id__": 9
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 56
|
"__id__": 63
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_active": true,
|
"_active": true,
|
||||||
"_components": [
|
"_components": [
|
||||||
{
|
{
|
||||||
"__id__": 59
|
"__id__": 66
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_prefab": null,
|
"_prefab": null,
|
||||||
@@ -444,7 +444,7 @@
|
|||||||
"_active": true,
|
"_active": true,
|
||||||
"_components": [
|
"_components": [
|
||||||
{
|
{
|
||||||
"__id__": 55
|
"__id__": 62
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 14
|
"__id__": 14
|
||||||
@@ -516,10 +516,10 @@
|
|||||||
"__id__": 13
|
"__id__": 13
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 53
|
"__id__": 60
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 54
|
"__id__": 61
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_prefab": null,
|
"_prefab": null,
|
||||||
@@ -735,15 +735,18 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 44
|
"__id__": 44
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__id__": 51
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_active": true,
|
"_active": true,
|
||||||
"_components": [
|
"_components": [
|
||||||
{
|
{
|
||||||
"__id__": 51
|
"__id__": 58
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 52
|
"__id__": 59
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_prefab": null,
|
"_prefab": null,
|
||||||
@@ -2384,6 +2387,324 @@
|
|||||||
"_N$affectedByScale": false,
|
"_N$affectedByScale": false,
|
||||||
"_id": "9d2gAh+o1KZ4USUhuVUsCm"
|
"_id": "9d2gAh+o1KZ4USUhuVUsCm"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"__type__": "cc.Node",
|
||||||
|
"_name": "PointLight",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"_parent": {
|
||||||
|
"__id__": 15
|
||||||
|
},
|
||||||
|
"_children": [
|
||||||
|
{
|
||||||
|
"__id__": 52
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__id__": 55
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_active": true,
|
||||||
|
"_components": [
|
||||||
|
{
|
||||||
|
"__id__": 57
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_prefab": null,
|
||||||
|
"_opacity": 255,
|
||||||
|
"_color": {
|
||||||
|
"__type__": "cc.Color",
|
||||||
|
"r": 255,
|
||||||
|
"g": 255,
|
||||||
|
"b": 255,
|
||||||
|
"a": 255
|
||||||
|
},
|
||||||
|
"_contentSize": {
|
||||||
|
"__type__": "cc.Size",
|
||||||
|
"width": 260,
|
||||||
|
"height": 320.4
|
||||||
|
},
|
||||||
|
"_anchorPoint": {
|
||||||
|
"__type__": "cc.Vec2",
|
||||||
|
"x": 0.5,
|
||||||
|
"y": 0.5
|
||||||
|
},
|
||||||
|
"_trs": {
|
||||||
|
"__type__": "TypedArray",
|
||||||
|
"ctor": "Float64Array",
|
||||||
|
"array": [
|
||||||
|
218,
|
||||||
|
-512.5999999999999,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"_eulerAngles": {
|
||||||
|
"__type__": "cc.Vec3",
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"_skewX": 0,
|
||||||
|
"_skewY": 0,
|
||||||
|
"_is3DNode": false,
|
||||||
|
"_groupIndex": 0,
|
||||||
|
"groupIndex": 0,
|
||||||
|
"_id": "60x1EOPINMuJ5xqPn+AgU5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__type__": "cc.Node",
|
||||||
|
"_name": "Desc",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"_parent": {
|
||||||
|
"__id__": 51
|
||||||
|
},
|
||||||
|
"_children": [],
|
||||||
|
"_active": true,
|
||||||
|
"_components": [
|
||||||
|
{
|
||||||
|
"__id__": 53
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__id__": 54
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_prefab": null,
|
||||||
|
"_opacity": 255,
|
||||||
|
"_color": {
|
||||||
|
"__type__": "cc.Color",
|
||||||
|
"r": 255,
|
||||||
|
"g": 255,
|
||||||
|
"b": 255,
|
||||||
|
"a": 255
|
||||||
|
},
|
||||||
|
"_contentSize": {
|
||||||
|
"__type__": "cc.Size",
|
||||||
|
"width": 260,
|
||||||
|
"height": 50.4
|
||||||
|
},
|
||||||
|
"_anchorPoint": {
|
||||||
|
"__type__": "cc.Vec2",
|
||||||
|
"x": 0.5,
|
||||||
|
"y": 0.5
|
||||||
|
},
|
||||||
|
"_trs": {
|
||||||
|
"__type__": "TypedArray",
|
||||||
|
"ctor": "Float64Array",
|
||||||
|
"array": [
|
||||||
|
0,
|
||||||
|
135,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"_eulerAngles": {
|
||||||
|
"__type__": "cc.Vec3",
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"_skewX": 0,
|
||||||
|
"_skewY": 0,
|
||||||
|
"_is3DNode": false,
|
||||||
|
"_groupIndex": 0,
|
||||||
|
"groupIndex": 0,
|
||||||
|
"_id": "58AdOvxJ9EKYM3yHqisHUF"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__type__": "cc.Widget",
|
||||||
|
"_name": "",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"node": {
|
||||||
|
"__id__": 52
|
||||||
|
},
|
||||||
|
"_enabled": true,
|
||||||
|
"alignMode": 1,
|
||||||
|
"_target": null,
|
||||||
|
"_alignFlags": 40,
|
||||||
|
"_left": 0,
|
||||||
|
"_right": 0,
|
||||||
|
"_top": 0,
|
||||||
|
"_bottom": 0,
|
||||||
|
"_verticalCenter": 0,
|
||||||
|
"_horizontalCenter": 0,
|
||||||
|
"_isAbsLeft": true,
|
||||||
|
"_isAbsRight": true,
|
||||||
|
"_isAbsTop": true,
|
||||||
|
"_isAbsBottom": true,
|
||||||
|
"_isAbsHorizontalCenter": true,
|
||||||
|
"_isAbsVerticalCenter": true,
|
||||||
|
"_originalWidth": 120,
|
||||||
|
"_originalHeight": 0,
|
||||||
|
"_id": "ccMi2kSldI4IzJzG4/RyT2"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__type__": "cc.Label",
|
||||||
|
"_name": "",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"node": {
|
||||||
|
"__id__": 52
|
||||||
|
},
|
||||||
|
"_enabled": true,
|
||||||
|
"_materials": [
|
||||||
|
{
|
||||||
|
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_useOriginalSize": false,
|
||||||
|
"_string": "点光",
|
||||||
|
"_N$string": "点光",
|
||||||
|
"_fontSize": 40,
|
||||||
|
"_lineHeight": 40,
|
||||||
|
"_enableWrapText": true,
|
||||||
|
"_N$file": null,
|
||||||
|
"_isSystemFontUsed": true,
|
||||||
|
"_spacingX": 0,
|
||||||
|
"_batchAsBitmap": false,
|
||||||
|
"_N$horizontalAlign": 1,
|
||||||
|
"_N$verticalAlign": 1,
|
||||||
|
"_N$fontFamily": "Arial",
|
||||||
|
"_N$overflow": 2,
|
||||||
|
"_N$cacheMode": 0,
|
||||||
|
"_id": "e2MJykEBlF9ZsiZ6vzfV6Q"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__type__": "cc.Node",
|
||||||
|
"_name": "Sprite",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"_parent": {
|
||||||
|
"__id__": 51
|
||||||
|
},
|
||||||
|
"_children": [],
|
||||||
|
"_active": true,
|
||||||
|
"_components": [
|
||||||
|
{
|
||||||
|
"__id__": 56
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_prefab": null,
|
||||||
|
"_opacity": 255,
|
||||||
|
"_color": {
|
||||||
|
"__type__": "cc.Color",
|
||||||
|
"r": 255,
|
||||||
|
"g": 255,
|
||||||
|
"b": 255,
|
||||||
|
"a": 255
|
||||||
|
},
|
||||||
|
"_contentSize": {
|
||||||
|
"__type__": "cc.Size",
|
||||||
|
"width": 195,
|
||||||
|
"height": 270
|
||||||
|
},
|
||||||
|
"_anchorPoint": {
|
||||||
|
"__type__": "cc.Vec2",
|
||||||
|
"x": 0.5,
|
||||||
|
"y": 0.5
|
||||||
|
},
|
||||||
|
"_trs": {
|
||||||
|
"__type__": "TypedArray",
|
||||||
|
"ctor": "Float64Array",
|
||||||
|
"array": [
|
||||||
|
0,
|
||||||
|
-25.200000000000003,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"_eulerAngles": {
|
||||||
|
"__type__": "cc.Vec3",
|
||||||
|
"x": 0,
|
||||||
|
"y": 0,
|
||||||
|
"z": 0
|
||||||
|
},
|
||||||
|
"_skewX": 0,
|
||||||
|
"_skewY": 0,
|
||||||
|
"_is3DNode": false,
|
||||||
|
"_groupIndex": 0,
|
||||||
|
"groupIndex": 0,
|
||||||
|
"_id": "70dC+CnmpL2ah71VbAY11G"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__type__": "cc.Sprite",
|
||||||
|
"_name": "",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"node": {
|
||||||
|
"__id__": 55
|
||||||
|
},
|
||||||
|
"_enabled": true,
|
||||||
|
"_materials": [
|
||||||
|
{
|
||||||
|
"__uuid__": "d88d982e-4ed7-4f90-9566-2ac2fef4bcb3"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"_srcBlendFactor": 770,
|
||||||
|
"_dstBlendFactor": 771,
|
||||||
|
"_spriteFrame": {
|
||||||
|
"__uuid__": "31bc895a-c003-4566-a9f3-2e54ae1c17dc"
|
||||||
|
},
|
||||||
|
"_type": 0,
|
||||||
|
"_sizeMode": 1,
|
||||||
|
"_fillType": 0,
|
||||||
|
"_fillCenter": {
|
||||||
|
"__type__": "cc.Vec2",
|
||||||
|
"x": 0,
|
||||||
|
"y": 0
|
||||||
|
},
|
||||||
|
"_fillStart": 0,
|
||||||
|
"_fillRange": 0,
|
||||||
|
"_isTrimmedMode": true,
|
||||||
|
"_atlas": null,
|
||||||
|
"_id": "74TX/9jtpCxIoI+3LrKOr0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__type__": "cc.Layout",
|
||||||
|
"_name": "",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"node": {
|
||||||
|
"__id__": 51
|
||||||
|
},
|
||||||
|
"_enabled": true,
|
||||||
|
"_layoutSize": {
|
||||||
|
"__type__": "cc.Size",
|
||||||
|
"width": 260,
|
||||||
|
"height": 320.4
|
||||||
|
},
|
||||||
|
"_resize": 1,
|
||||||
|
"_N$layoutType": 2,
|
||||||
|
"_N$padding": 0,
|
||||||
|
"_N$cellSize": {
|
||||||
|
"__type__": "cc.Size",
|
||||||
|
"width": 40,
|
||||||
|
"height": 40
|
||||||
|
},
|
||||||
|
"_N$startAxis": 0,
|
||||||
|
"_N$paddingLeft": 0,
|
||||||
|
"_N$paddingRight": 0,
|
||||||
|
"_N$paddingTop": 0,
|
||||||
|
"_N$paddingBottom": 0,
|
||||||
|
"_N$spacingX": 0,
|
||||||
|
"_N$spacingY": 0,
|
||||||
|
"_N$verticalDirection": 1,
|
||||||
|
"_N$horizontalDirection": 0,
|
||||||
|
"_N$affectedByScale": false,
|
||||||
|
"_id": "87SQn11LJK8LSoPIN0g/TI"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"__type__": "cc.Widget",
|
"__type__": "cc.Widget",
|
||||||
"_name": "",
|
"_name": "",
|
||||||
@@ -2541,10 +2862,10 @@
|
|||||||
"_active": true,
|
"_active": true,
|
||||||
"_components": [
|
"_components": [
|
||||||
{
|
{
|
||||||
"__id__": 57
|
"__id__": 64
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 58
|
"__id__": 65
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_prefab": null,
|
"_prefab": null,
|
||||||
@@ -2600,7 +2921,7 @@
|
|||||||
"_name": "",
|
"_name": "",
|
||||||
"_objFlags": 0,
|
"_objFlags": 0,
|
||||||
"node": {
|
"node": {
|
||||||
"__id__": 56
|
"__id__": 63
|
||||||
},
|
},
|
||||||
"_enabled": true,
|
"_enabled": true,
|
||||||
"_materials": [
|
"_materials": [
|
||||||
@@ -2630,7 +2951,7 @@
|
|||||||
"_name": "",
|
"_name": "",
|
||||||
"_objFlags": 0,
|
"_objFlags": 0,
|
||||||
"node": {
|
"node": {
|
||||||
"__id__": 56
|
"__id__": 63
|
||||||
},
|
},
|
||||||
"_enabled": true,
|
"_enabled": true,
|
||||||
"alignMode": 1,
|
"alignMode": 1,
|
||||||
|
97
assets/scripts/FlashLightCtrlComponent.ts
Normal file
97
assets/scripts/FlashLightCtrlComponent.ts
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
const { ccclass, property } = cc._decorator;
|
||||||
|
|
||||||
|
@ccclass
|
||||||
|
export default class FlashLightCtrlComponent extends cc.Component {
|
||||||
|
private _flashLightUBO: FlashLightUBO = new FlashLightUBO();
|
||||||
|
|
||||||
|
onEnable() {
|
||||||
|
this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
|
||||||
|
this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
|
||||||
|
this.node.on("on_property_change", this._onPropertyChange, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
onDisable() {
|
||||||
|
this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
|
||||||
|
this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
|
||||||
|
this.node.off("on_property_change", this._onPropertyChange, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onTouchStart(event: cc.Event.EventTouch) {
|
||||||
|
this._onTouchMove(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onTouchMove(event: cc.Event.EventTouch) {
|
||||||
|
let touchPointInWorldSpace = event.getLocation();
|
||||||
|
let touchPointInNodeSpace = this.node.convertToNodeSpaceAR(touchPointInWorldSpace);
|
||||||
|
|
||||||
|
// 将触摸点转换为OPENGL坐标系并归一化
|
||||||
|
// OpenGl 坐标系原点在左上角
|
||||||
|
this._flashLightUBO.lightCenterPoint = cc.v2(
|
||||||
|
this.node.anchorX + touchPointInNodeSpace.x / this.node.width,
|
||||||
|
1 - (this.node.anchorY + touchPointInNodeSpace.y / this.node.height)
|
||||||
|
);
|
||||||
|
|
||||||
|
this._updateMaterial();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onPropertyChange(localDiffusionUniform: FlashLightUBO) {
|
||||||
|
this._flashLightUBO.lightColor = localDiffusionUniform.lightColor;
|
||||||
|
this._flashLightUBO.lightAngle = localDiffusionUniform.lightAngle;
|
||||||
|
this._flashLightUBO.lightWidth = localDiffusionUniform.lightWidth;
|
||||||
|
this._flashLightUBO.enableGradient = localDiffusionUniform.enableGradient;
|
||||||
|
this._flashLightUBO.cropAlpha = localDiffusionUniform.cropAlpha;
|
||||||
|
this._flashLightUBO.enableFog = localDiffusionUniform.enableFog;
|
||||||
|
this._updateMaterial();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updateMaterial() {
|
||||||
|
this.getComponents(cc.RenderComponent).forEach(renderComponent => {
|
||||||
|
let material: cc.Material = renderComponent.getMaterial(0);
|
||||||
|
material.setProperty("lightColor", this._flashLightUBO.lightColor);
|
||||||
|
material.setProperty("lightCenterPoint", this._flashLightUBO.lightCenterPoint);
|
||||||
|
material.setProperty("lightAngle", this._flashLightUBO.lightAngle);
|
||||||
|
material.setProperty("lightWidth", this._flashLightUBO.lightWidth);
|
||||||
|
material.setProperty("enableGradient", this._flashLightUBO.enableGradient);
|
||||||
|
material.setProperty("cropAlpha", this._flashLightUBO.cropAlpha);
|
||||||
|
material.setProperty("enableFog", this._flashLightUBO.enableFog);
|
||||||
|
renderComponent.setMaterial(0, material);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class FlashLightUBO {
|
||||||
|
/**
|
||||||
|
* 中心点颜色
|
||||||
|
*/
|
||||||
|
lightColor: cc.Color = cc.Color.YELLOW;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中心点坐标 ([0.0, 1.0], [0.0, 1.0])
|
||||||
|
*/
|
||||||
|
lightCenterPoint: cc.Vec2 = cc.v2(0.5, 0.5);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 光束角度 [0.0, 180.0]
|
||||||
|
*/
|
||||||
|
lightAngle: number = 45;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 光束宽度 [0.0, +∞]
|
||||||
|
*/
|
||||||
|
lightWidth: number = 0.5;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否启用光束渐变
|
||||||
|
*/
|
||||||
|
enableGradient: boolean = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否裁剪掉透明区域上的点光
|
||||||
|
*/
|
||||||
|
cropAlpha: boolean = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否开启战争迷雾效果
|
||||||
|
*/
|
||||||
|
enableFog: boolean = false;
|
||||||
|
}
|
9
assets/scripts/FlashLightCtrlComponent.ts.meta
Normal file
9
assets/scripts/FlashLightCtrlComponent.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.5",
|
||||||
|
"uuid": "3edca38f-0f12-489f-847a-0d89d9e55c6c",
|
||||||
|
"isPlugin": false,
|
||||||
|
"loadPluginInWeb": true,
|
||||||
|
"loadPluginInNative": true,
|
||||||
|
"loadPluginInEditor": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
111
assets/scripts/FlashLightEffectScene.ts
Normal file
111
assets/scripts/FlashLightEffectScene.ts
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
import FlashLightCtrlComponent, { FlashLightUBO } from "./FlashLightCtrlComponent";
|
||||||
|
|
||||||
|
const { ccclass, property } = cc._decorator;
|
||||||
|
|
||||||
|
@ccclass
|
||||||
|
export default class FlashLightEffectScene extends cc.Component {
|
||||||
|
private _redSlider: cc.Slider = null;
|
||||||
|
private _redSliderLabel: cc.Label = null;
|
||||||
|
private _greenSlider: cc.Slider = null;
|
||||||
|
private _greenSliderLabel: cc.Label = null;
|
||||||
|
private _blueSlider: cc.Slider = null;
|
||||||
|
private _blueSliderLabel: cc.Label = null;
|
||||||
|
private _alphaSlider: cc.Slider = null;
|
||||||
|
private _alphaSliderLabel: cc.Label = null;
|
||||||
|
private _lightWidthSlider: cc.Slider = null;
|
||||||
|
private _lightWidthSliderLabel: cc.Label = null;
|
||||||
|
private _lightAngleSlider: cc.Slider = null;
|
||||||
|
private _lightAngleSliderLabel: cc.Label = null;
|
||||||
|
|
||||||
|
private _enableGradientToggle: cc.Toggle = null;
|
||||||
|
private _cropAlphaToggle: cc.Toggle = null;
|
||||||
|
private _enableFogToggle: cc.Toggle = null;
|
||||||
|
|
||||||
|
private _examplesParentNode: cc.Node = null;
|
||||||
|
|
||||||
|
onLoad() {
|
||||||
|
cc.dynamicAtlasManager.enabled = false;
|
||||||
|
|
||||||
|
this._redSlider = cc.find("Canvas/Content/Controller/ColorRedSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._redSliderLabel = cc.find("Canvas/Content/Controller/ColorRedSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._greenSlider = cc.find("Canvas/Content/Controller/ColorGreenSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._greenSliderLabel = cc.find("Canvas/Content/Controller/ColorGreenSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._blueSlider = cc.find("Canvas/Content/Controller/ColorBlueSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._blueSliderLabel = cc.find("Canvas/Content/Controller/ColorBlueSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._alphaSlider = cc.find("Canvas/Content/Controller/ColorAlphaSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._alphaSliderLabel = cc.find("Canvas/Content/Controller/ColorAlphaSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._lightWidthSlider = cc.find("Canvas/Content/Controller/LightWidthSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._lightWidthSliderLabel = cc.find("Canvas/Content/Controller/LightWidthSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._lightAngleSlider = cc.find("Canvas/Content/Controller/LightAngleSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._lightAngleSliderLabel = cc.find("Canvas/Content/Controller/LightAngleSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
|
||||||
|
this._enableGradientToggle = cc.find("Canvas/Content/Controller/EnableGradientToggle/Toggle").getComponent(cc.Toggle);
|
||||||
|
this._cropAlphaToggle = cc.find("Canvas/Content/Controller/CropAlphaToggle/Toggle").getComponent(cc.Toggle);
|
||||||
|
this._enableFogToggle = cc.find("Canvas/Content/Controller/EnableFogToggle/Toggle").getComponent(cc.Toggle);
|
||||||
|
|
||||||
|
// 代码添加控制脚本
|
||||||
|
this._examplesParentNode = cc.find("Canvas/Content/Examples");
|
||||||
|
this._examplesParentNode.children.forEach(childNode => {
|
||||||
|
childNode.addComponent(FlashLightCtrlComponent);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onEnable() {
|
||||||
|
this._redSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._greenSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._blueSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._alphaSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._lightWidthSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._lightAngleSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
|
||||||
|
this._enableGradientToggle.node.on("toggle", this._onPropertyChanged, this);
|
||||||
|
this._cropAlphaToggle.node.on("toggle", this._onPropertyChanged, this);
|
||||||
|
this._enableFogToggle.node.on("toggle", this._onPropertyChanged, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
onDisable() {
|
||||||
|
this._redSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._greenSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._blueSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._alphaSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._lightWidthSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._lightAngleSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
|
||||||
|
this._enableGradientToggle.node.off("toggle", this._onPropertyChanged, this);
|
||||||
|
this._cropAlphaToggle.node.off("toggle", this._onPropertyChanged, this);
|
||||||
|
this._enableFogToggle.node.off("toggle", this._onPropertyChanged, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
start() {
|
||||||
|
this._onPropertyChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onPropertyChanged() {
|
||||||
|
// 更新进度条值 Label 文本
|
||||||
|
this._redSliderLabel.string = `${this._redSlider.progress.toFixed(2)} | ${Math.round(255 * this._redSlider.progress)}`;
|
||||||
|
this._greenSliderLabel.string = `${this._greenSlider.progress.toFixed(2)} | ${Math.round(255 * this._greenSlider.progress)}`;
|
||||||
|
this._blueSliderLabel.string = `${this._blueSlider.progress.toFixed(2)} | ${Math.round(255 * this._blueSlider.progress)}`;
|
||||||
|
this._alphaSliderLabel.string = `${this._alphaSlider.progress.toFixed(2)} | ${Math.round(255 * this._alphaSlider.progress)}`;
|
||||||
|
this._lightWidthSliderLabel.string = `${this._lightWidthSlider.progress.toFixed(2)}`;
|
||||||
|
|
||||||
|
let angle = 180 * this._lightAngleSlider.progress;
|
||||||
|
this._lightAngleSliderLabel.string = `${this._lightAngleSlider.progress.toFixed(2)} | ${angle.toFixed(2)}`;
|
||||||
|
|
||||||
|
// 通知子节点更新材质
|
||||||
|
this._examplesParentNode.children.forEach(childNode => {
|
||||||
|
childNode.emit("on_property_change", <FlashLightUBO>{
|
||||||
|
lightColor: cc.color(
|
||||||
|
Math.round(255 * this._redSlider.progress),
|
||||||
|
Math.round(255 * this._greenSlider.progress),
|
||||||
|
Math.round(255 * this._blueSlider.progress),
|
||||||
|
Math.round(255 * this._alphaSlider.progress)
|
||||||
|
),
|
||||||
|
lightAngle: angle,
|
||||||
|
lightWidth: this._lightWidthSlider.progress,
|
||||||
|
enableGradient: this._enableGradientToggle.isChecked,
|
||||||
|
cropAlpha: this._cropAlphaToggle.isChecked,
|
||||||
|
enableFog: this._enableFogToggle.isChecked
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
9
assets/scripts/FlashLightEffectScene.ts.meta
Normal file
9
assets/scripts/FlashLightEffectScene.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.5",
|
||||||
|
"uuid": "6d3aff77-2683-4417-8960-2a5fd5d0757c",
|
||||||
|
"isPlugin": false,
|
||||||
|
"loadPluginInWeb": true,
|
||||||
|
"loadPluginInNative": true,
|
||||||
|
"loadPluginInEditor": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
83
assets/scripts/PointLightCtrlComponent.ts
Normal file
83
assets/scripts/PointLightCtrlComponent.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
const { ccclass, property } = cc._decorator;
|
||||||
|
|
||||||
|
@ccclass
|
||||||
|
export default class PointLightCtrlComponent extends cc.Component {
|
||||||
|
private _pointLightUBO: PointLightUBO = new PointLightUBO();
|
||||||
|
|
||||||
|
onEnable() {
|
||||||
|
this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
|
||||||
|
this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
|
||||||
|
this.node.on("on_property_change", this._onPropertyChange, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
onDisable() {
|
||||||
|
this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchStart, this);
|
||||||
|
this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
|
||||||
|
this.node.off("on_property_change", this._onPropertyChange, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onTouchStart(event: cc.Event.EventTouch) {
|
||||||
|
this._onTouchMove(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onTouchMove(event: cc.Event.EventTouch) {
|
||||||
|
let touchPointInWorldSpace = event.getLocation();
|
||||||
|
let touchPointInNodeSpace = this.node.convertToNodeSpaceAR(touchPointInWorldSpace);
|
||||||
|
|
||||||
|
// 将触摸点转换为OPENGL坐标系并归一化
|
||||||
|
// OpenGl 坐标系原点在左上角
|
||||||
|
this._pointLightUBO.centerPoint = cc.v2(
|
||||||
|
this.node.anchorX + touchPointInNodeSpace.x / this.node.width,
|
||||||
|
1 - (this.node.anchorY + touchPointInNodeSpace.y / this.node.height)
|
||||||
|
);
|
||||||
|
|
||||||
|
this._updateMaterial();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onPropertyChange(pointLightUBO: PointLightUBO) {
|
||||||
|
this._pointLightUBO.centerColor = pointLightUBO.centerColor;
|
||||||
|
this._pointLightUBO.radius = pointLightUBO.radius;
|
||||||
|
this._pointLightUBO.cropAlpha = pointLightUBO.cropAlpha;
|
||||||
|
this._pointLightUBO.enableFog = pointLightUBO.enableFog;
|
||||||
|
this._updateMaterial();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updateMaterial() {
|
||||||
|
this.getComponents(cc.RenderComponent).forEach(renderComponent => {
|
||||||
|
let material: cc.Material = renderComponent.getMaterial(0);
|
||||||
|
material.setProperty("centerColor", this._pointLightUBO.centerColor);
|
||||||
|
material.setProperty("centerPoint", this._pointLightUBO.centerPoint);
|
||||||
|
material.setProperty("radius", this._pointLightUBO.radius);
|
||||||
|
material.setProperty("cropAlpha", this._pointLightUBO.cropAlpha);
|
||||||
|
material.setProperty("enableFog", this._pointLightUBO.enableFog);
|
||||||
|
renderComponent.setMaterial(0, material);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PointLightUBO {
|
||||||
|
/**
|
||||||
|
* 中心点颜色
|
||||||
|
*/
|
||||||
|
centerColor: cc.Color = cc.Color.YELLOW;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中心点坐标 ([0.0, 1.0], [0.0, 1.0])
|
||||||
|
*/
|
||||||
|
centerPoint: cc.Vec2 = cc.v2(0.5, 0.5);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩散半径 [0.0, 1.0]
|
||||||
|
*/
|
||||||
|
radius: number = 0.5;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否裁剪掉透明区域上的点光
|
||||||
|
*/
|
||||||
|
cropAlpha: boolean = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否开启战争迷雾效果
|
||||||
|
*/
|
||||||
|
enableFog: boolean = false;
|
||||||
|
}
|
9
assets/scripts/PointLightCtrlComponent.ts.meta
Normal file
9
assets/scripts/PointLightCtrlComponent.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.5",
|
||||||
|
"uuid": "ababac41-a736-4cbf-ad03-248962dd06e7",
|
||||||
|
"isPlugin": false,
|
||||||
|
"loadPluginInWeb": true,
|
||||||
|
"loadPluginInNative": true,
|
||||||
|
"loadPluginInEditor": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
94
assets/scripts/PointLightEffectScene.ts
Normal file
94
assets/scripts/PointLightEffectScene.ts
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
import PointLightCtrlComponent, { PointLightUBO } from "./PointLightCtrlComponent";
|
||||||
|
|
||||||
|
const { ccclass, property } = cc._decorator;
|
||||||
|
|
||||||
|
@ccclass
|
||||||
|
export default class PointLightEffectScene extends cc.Component {
|
||||||
|
private _redSlider: cc.Slider = null;
|
||||||
|
private _redSliderLabel: cc.Label = null;
|
||||||
|
private _greenSlider: cc.Slider = null;
|
||||||
|
private _greenSliderLabel: cc.Label = null;
|
||||||
|
private _blueSlider: cc.Slider = null;
|
||||||
|
private _blueSliderLabel: cc.Label = null;
|
||||||
|
private _alphaSlider: cc.Slider = null;
|
||||||
|
private _alphaSliderLabel: cc.Label = null;
|
||||||
|
private _radiuSlider: cc.Slider = null;
|
||||||
|
private _radiuSliderLabel: cc.Label = null;
|
||||||
|
|
||||||
|
private _cropAlphaToggle: cc.Toggle = null;
|
||||||
|
private _enableFogToggle: cc.Toggle = null;
|
||||||
|
|
||||||
|
private _examplesParentNode: cc.Node = null;
|
||||||
|
|
||||||
|
onLoad() {
|
||||||
|
cc.dynamicAtlasManager.enabled = false;
|
||||||
|
|
||||||
|
this._redSlider = cc.find("Canvas/Content/Controller/ColorRedSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._redSliderLabel = cc.find("Canvas/Content/Controller/ColorRedSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._greenSlider = cc.find("Canvas/Content/Controller/ColorGreenSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._greenSliderLabel = cc.find("Canvas/Content/Controller/ColorGreenSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._blueSlider = cc.find("Canvas/Content/Controller/ColorBlueSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._blueSliderLabel = cc.find("Canvas/Content/Controller/ColorBlueSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._alphaSlider = cc.find("Canvas/Content/Controller/ColorAlphaSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._alphaSliderLabel = cc.find("Canvas/Content/Controller/ColorAlphaSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
this._radiuSlider = cc.find("Canvas/Content/Controller/RadiuSlider/Slider").getComponent(cc.Slider);
|
||||||
|
this._radiuSliderLabel = cc.find("Canvas/Content/Controller/RadiuSlider/ValueLabel").getComponent(cc.Label);
|
||||||
|
|
||||||
|
this._cropAlphaToggle = cc.find("Canvas/Content/Controller/CropAlphaToggle/Toggle").getComponent(cc.Toggle);
|
||||||
|
this._enableFogToggle = cc.find("Canvas/Content/Controller/EnableFogToggle/Toggle").getComponent(cc.Toggle);
|
||||||
|
|
||||||
|
// 代码添加控制脚本
|
||||||
|
this._examplesParentNode = cc.find("Canvas/Content/Examples");
|
||||||
|
this._examplesParentNode.children.forEach(childNode => {
|
||||||
|
childNode.addComponent(PointLightCtrlComponent);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onEnable() {
|
||||||
|
this._redSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._greenSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._blueSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._alphaSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._radiuSlider.node.on("slide", this._onPropertyChanged, this);
|
||||||
|
this._cropAlphaToggle.node.on("toggle", this._onPropertyChanged, this);
|
||||||
|
this._enableFogToggle.node.on("toggle", this._onPropertyChanged, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
onDisable() {
|
||||||
|
this._redSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._greenSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._blueSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._alphaSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._radiuSlider.node.off("slide", this._onPropertyChanged, this);
|
||||||
|
this._cropAlphaToggle.node.off("toggle", this._onPropertyChanged, this);
|
||||||
|
this._enableFogToggle.node.off("toggle", this._onPropertyChanged, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
start() {
|
||||||
|
this._onPropertyChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onPropertyChanged() {
|
||||||
|
// 更新进度条值 Label 文本
|
||||||
|
this._redSliderLabel.string = `${this._redSlider.progress.toFixed(2)} | ${Math.round(255 * this._redSlider.progress)}`;
|
||||||
|
this._greenSliderLabel.string = `${this._greenSlider.progress.toFixed(2)} | ${Math.round(255 * this._greenSlider.progress)}`;
|
||||||
|
this._blueSliderLabel.string = `${this._blueSlider.progress.toFixed(2)} | ${Math.round(255 * this._blueSlider.progress)}`;
|
||||||
|
this._alphaSliderLabel.string = `${this._alphaSlider.progress.toFixed(2)} | ${Math.round(255 * this._alphaSlider.progress)}`;
|
||||||
|
this._radiuSliderLabel.string = `${this._radiuSlider.progress.toFixed(2)}`;
|
||||||
|
|
||||||
|
// 通知子节点更新材质
|
||||||
|
this._examplesParentNode.children.forEach(childNode => {
|
||||||
|
childNode.emit("on_property_change", <PointLightUBO>{
|
||||||
|
centerColor: cc.color(
|
||||||
|
Math.round(255 * this._redSlider.progress),
|
||||||
|
Math.round(255 * this._greenSlider.progress),
|
||||||
|
Math.round(255 * this._blueSlider.progress),
|
||||||
|
Math.round(255 * this._alphaSlider.progress)
|
||||||
|
),
|
||||||
|
radius: this._radiuSlider.progress,
|
||||||
|
cropAlpha: this._cropAlphaToggle.isChecked,
|
||||||
|
enableFog: this._enableFogToggle.isChecked
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
9
assets/scripts/PointLightEffectScene.ts.meta
Normal file
9
assets/scripts/PointLightEffectScene.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.5",
|
||||||
|
"uuid": "3a212efa-8118-4c79-a0f7-a51f36262ff7",
|
||||||
|
"isPlugin": false,
|
||||||
|
"loadPluginInWeb": true,
|
||||||
|
"loadPluginInNative": true,
|
||||||
|
"loadPluginInEditor": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
BIN
static/effects/2d-sprite-flash-light.gif
Normal file
BIN
static/effects/2d-sprite-flash-light.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.1 MiB |
BIN
static/effects/2d-sprite-point-light.gif
Normal file
BIN
static/effects/2d-sprite-point-light.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.5 MiB |
Binary file not shown.
Before Width: | Height: | Size: 182 KiB After Width: | Height: | Size: 267 KiB |
Reference in New Issue
Block a user