diff --git a/assets/effects/sprite-flash-light.effect b/assets/effects/sprite-flash-light.effect index b33bdaf..932dab4 100644 --- a/assets/effects/sprite-flash-light.effect +++ b/assets/effects/sprite-flash-light.effect @@ -37,7 +37,7 @@ CCEffect %{ } # 光束倾斜角度 lightAngle: { - valu: 45.0, + value: 36.0, inspctor: { tooltip: "光束倾斜角度", range: [0.0, 1.0], @@ -46,19 +46,35 @@ CCEffect %{ # 光束宽度 lightWidth: { - value: 0.4, + value: 0.2, inspector: { tooltip: "光束宽度" } } # 启用光束渐变 - enablGradient: { - value: 0, + enableGradient: { + value: 1.0, inspecator: { tooltip: "是否启用光束渐变。0:不启用,非0:启用" } } + + # 裁剪掉透明区域上的光 + cropAlpha: { + value: 1.0, + inspecator: { + tooltip: "是否裁剪透明区域上的光。0:不启用,非0:启用" + } + } + + # 是否启用迷雾效果 + enableFog: { + value: 0.0, + inspecator: { + tooltip: "是否启用迷雾效果。0:不启用,非0:启用" + } + } }% @@ -124,49 +140,70 @@ CCProgram fs %{ float lightWidth; // 启用光束渐变 - float enablGradient; + // ps:编辑器还不支持 bool 类型的样子,因此用float来定义 + float enableGradient; - // 裁剪掉透明区域上的点光 - // ps:编辑器还不支持 bool 类型的样子,因此没在 CCEffect 中定义 - bool cropAlpha; + // 裁剪掉透明区域上的光 + // ps:编辑器还不支持 bool 类型的样子,因此用float来定义 + float cropAlpha; // 是否启用迷雾效果 - // ps:编辑器还不支持 bool 类型的样子,因此没在 CCEffect 中定义 - bool enableFog; + // ps:编辑器还不支持 bool 类型的样子,因此用float来定义 + float enableFog; } - // /** - // * 添加光束颜色 - // */ - // vec4 addLightColor(vec4 textureColor, vec2 lightCenterPoint, float radius, vec4 lightColor) { - // // 计算当前 uv 到 光束 的距离 - // float dis = distance(v_uv0, lightCenterPoint); + /** + * 添加光束颜色 + */ + vec4 addLightColor(vec4 textureColor, vec4 lightColor, vec2 lightCenterPoint, float lightAngle, float lightWidth) { + // 计算当前 uv 到 光束 的距离 + float angleInRadians = radians(lightAngle); - // float a = 1.0 ; + // 角度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)); - // // 裁剪掉透明区域上的点光 - // if (cropAlpha) { - // a *= step(0.01, textureColor.a); - // } + // 以当前点画一条平行于X轴的线,假设此线和光束中心线相交的点为D点 + // 那么 + // D.y = uv0.y + // D.x = lightOffsetX + D.y / tan(angle) + float dx = lightOffsetX + (1.0 - v_uv0.y) / tan(angleInRadians); - // // 裁剪掉扩散范围外的uv(迷雾效果) - // if (!enableFog) { - // a *= step(dis, radius); - // } + // D 到当前 uv0 的距离就是 + // dis = |uv0.x - D.x| + float offsetDis = abs(v_uv0.x - dx); - // // 加入从中心往外渐变的效果 - // a *= 1.0 - (dis / radius); + // 当前点到光束中心线的的垂直距离就好算了 + 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); + } - // // 加点料,让中心点更加亮 - // // 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(迷雾效果) + if (!bool(enableFog)) { + a *= step(dis, lightWidth * 0.5); + } - // // 计算出扩散范围内,不同 uv 对应的实际扩散颜色值 - // vec4 diffusionColor = lightColor * a; + // 加入从中心往外渐变的效果 + if (bool(enableGradient)) { + a *= 1.0 - dis / (lightWidth * 0.5); + } - // // 混合颜色:在原始图像颜色上叠加扩散颜色 - // return textureColor * textureColor.a + diffusionColor; - // } + // 计算出扩散范围内,不同 uv 对应的实际扩散颜色值 + vec4 finalLightColor = lightColor * a; + + // 混合颜色:在原始图像颜色上叠加扩散颜色 + return textureColor * textureColor.a + finalLightColor; + } #endif void main () { @@ -186,7 +223,7 @@ CCProgram fs %{ gl_FragColor = o; #if ENABLE_DIFFUSION - // gl_FragColor = addLightColor(gl_FragColor, lightCenterPoint, radius, lightColor); + gl_FragColor = addLightColor(gl_FragColor, lightColor, lightCenterPoint, lightAngle, lightWidth); #endif } }% diff --git a/assets/effects/sprite-flash-light.effect.meta b/assets/effects/sprite-flash-light.effect.meta index 358e143..cf9fa69 100644 --- a/assets/effects/sprite-flash-light.effect.meta +++ b/assets/effects/sprite-flash-light.effect.meta @@ -5,11 +5,11 @@ { "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\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\n #endif\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 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 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_DIFFUSION\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_DIFFUSION\nuniform Light {\n\n vec4 lightColor;\n\n vec2 lightCenterPoint;\n\n float lightAngle;\n\n float lightWidth;\n\n float enablGradient;\n\n bool cropAlpha;\n\n bool enableFog;\n}\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\n #endif\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 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 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_DIFFUSION\n gl_FragColor = addLightColor(gl_FragColor, lightColor, lightCenterPoint, lightAngle, lightWidth);\n #endif\n}\n" } } ], diff --git a/assets/materials/sprite-flash-light.mtl b/assets/materials/sprite-flash-light.mtl index 9caa44b..c7ff1b0 100644 --- a/assets/materials/sprite-flash-light.mtl +++ b/assets/materials/sprite-flash-light.mtl @@ -10,5 +10,15 @@ "USE_TEXTURE": true, "ENABLE_DIFFUSION": true }, - "_props": {} + "_props": { + "lightColor": { + "__type__": "cc.Color", + "r": 255, + "g": 245, + "b": 0, + "a": 255 + }, + "lightAngle": 36, + "lightWidth": 0.2 + } } \ No newline at end of file diff --git a/assets/scenes/FlashLightEffectScene.fire b/assets/scenes/FlashLightEffectScene.fire index 2323051..55986a7 100755 --- a/assets/scenes/FlashLightEffectScene.fire +++ b/assets/scenes/FlashLightEffectScene.fire @@ -78,10 +78,10 @@ "_active": true, "_components": [ { - "__id__": 137 + "__id__": 166 }, { - "__id__": 138 + "__id__": 167 } ], "_prefab": null, @@ -171,7 +171,7 @@ "array": [ 0, 0, - 393.1484700242669, + 418.2902700278839, 0, 0, 0, @@ -245,13 +245,13 @@ "__id__": 9 }, { - "__id__": 121 + "__id__": 150 } ], "_active": true, "_components": [ { - "__id__": 136 + "__id__": 165 } ], "_prefab": null, @@ -453,16 +453,22 @@ "__id__": 95 }, { - "__id__": 107 + "__id__": 112 + }, + { + "__id__": 124 + }, + { + "__id__": 136 } ], "_active": true, "_components": [ { - "__id__": 119 + "__id__": 148 }, { - "__id__": 120 + "__id__": 149 } ], "_prefab": null, @@ -477,7 +483,7 @@ "_contentSize": { "__type__": "cc.Size", "width": 576, - "height": 480 + "height": 540 }, "_anchorPoint": { "__type__": "cc.Vec2", @@ -1322,7 +1328,7 @@ "ctor": "Float64Array", "array": [ 0, - -100, + -90, 0, 0, 0, @@ -2083,7 +2089,7 @@ "ctor": "Float64Array", "array": [ 0, - -170, + -150, 0, 0, 0, @@ -2844,7 +2850,7 @@ "ctor": "Float64Array", "array": [ 0, - -240, + -210, 0, 0, 0, @@ -3559,7 +3565,7 @@ }, { "__type__": "cc.Node", - "_name": "RadiuSlider", + "_name": "LightWidthSlider", "_objFlags": 0, "_parent": { "__id__": 9 @@ -3605,7 +3611,7 @@ "ctor": "Float64Array", "array": [ 0, - -310, + -270, 0, 0, 0, @@ -3708,8 +3714,8 @@ } ], "_useOriginalSize": false, - "_string": "扩散半径:", - "_N$string": "扩散半径:", + "_string": "光束宽度:", + "_N$string": "光束宽度:", "_fontSize": 40, "_lineHeight": 40, "_enableWrapText": true, @@ -3987,7 +3993,7 @@ "__type__": "TypedArray", "ctor": "Float64Array", "array": [ - 92.16000000000005, + 46.08000000000001, 0, 0, 0, @@ -4166,7 +4172,7 @@ "_N$handle": { "__id__": 88 }, - "_N$progress": 0.4, + "_N$progress": 0.2, "_id": "c2uNTLxCdF2oHJ+IS0EBLe" }, { @@ -4275,8 +4281,8 @@ } ], "_useOriginalSize": false, - "_string": "0.10", - "_N$string": "0.10", + "_string": "0.20", + "_N$string": "0.20", "_fontSize": 40, "_lineHeight": 40, "_enableWrapText": true, @@ -4320,7 +4326,7 @@ }, { "__type__": "cc.Node", - "_name": "CropAlphaToggle", + "_name": "LightAngleSlider", "_objFlags": 0, "_parent": { "__id__": 9 @@ -4331,12 +4337,15 @@ }, { "__id__": 99 + }, + { + "__id__": 108 } ], "_active": true, "_components": [ { - "__id__": 106 + "__id__": 111 } ], "_prefab": null, @@ -4363,7 +4372,1348 @@ "ctor": "Float64Array", "array": [ 0, - -380, + -330, + 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": "d4eYBJQBZL8Yl1IH+loEm+" + }, + { + "__type__": "cc.Node", + "_name": "SliderDescLabel", + "_objFlags": 0, + "_parent": { + "__id__": 95 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 97 + }, + { + "__id__": 98 + } + ], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 144, + "height": 40 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 1, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + -144, + 0, + 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": "ceRhALLDNHFqXlENE+38yj" + }, + { + "__type__": "cc.Label", + "_name": "SliderDescLabel