From 1c5012ab65f969f03450e22c5c83e7cf460f416c Mon Sep 17 00:00:00 2001 From: caizhitao Date: Mon, 20 Jan 2020 22:42:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0=E6=A8=A1?= =?UTF-8?q?=E7=B3=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/effects/sprite-gaussian-blur.effect | 41 ++++++++++++++++--- .../effects/sprite-gaussian-blur.effect.meta | 4 +- assets/materials/sprite-gaussian-blur.mtl | 3 +- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/assets/effects/sprite-gaussian-blur.effect b/assets/effects/sprite-gaussian-blur.effect index dca12f0..de02ce4 100644 --- a/assets/effects/sprite-gaussian-blur.effect +++ b/assets/effects/sprite-gaussian-blur.effect @@ -79,6 +79,7 @@ CCProgram fs %{ // 定义标准方差值 #define stdDev 0.84089642 + // #define stdDev 1.5 // 定义π #define pi 3.141592653589793 @@ -86,12 +87,25 @@ CCProgram fs %{ // // 接收外部变量 // uniform GaussianBlur { // } + - float getGaussianBlur() + /** + * 获取权重(对应二维高斯函数公式) + * + * @param x + * @param y + */ + float getWeight(float x, float y) { + return (1.0 / (2.0 * pi * pow(stdDev, 2.0))) * pow(1.0 / e, (pow(x, 2.0) + pow(y, 2.0)) / (2.0 * pow(stdDev, 2.0))); + } + /** + * 获取权重的加权平均值 + */ + float getAverageWeight(float x, float y, float totalWeight) { + return getWeight(x, y) / totalWeight; + } - // float matrix[4]; - // float matrix[0] = 0.0; #endif void main () { @@ -108,8 +122,25 @@ CCProgram fs %{ ALPHA_TEST(o); - #if ENABLE_GAUSSIAN_BLUR - #endif gl_FragColor = o; + #if ENABLE_GAUSSIAN_BLUR + const float size = ceil(stdDev * 6.0); + float divider = 0.01; + float totalWeight = 0.0; + for(float x = -floor(size / 2.0); x<= floor(size/ 2.0); x++) { + for (float y = -floor(size / 2.0); y<= floor(size/ 2.0); y++) { + totalWeight += getWeight(x, y); + } + } + + vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0); + for(float x = -floor(size / 2.0); x<= floor(size/ 2.0); x++) { + for (float y = -floor(size / 2.0); y<= floor(size/ 2.0); y++) { + float weight = getAverageWeight(x, y, totalWeight); + finalColor += texture(texture, v_uv0 + vec2(divider * x, divider * y)) * weight; + } + } + gl_FragColor = finalColor; + #endif } }% diff --git a/assets/effects/sprite-gaussian-blur.effect.meta b/assets/effects/sprite-gaussian-blur.effect.meta index 0010b86..a5f529f 100644 --- a/assets/effects/sprite-gaussian-blur.effect.meta +++ b/assets/effects/sprite-gaussian-blur.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_GAUSSIAN_BLUR\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 #if ENABLE_GAUSSIAN_BLUR\n #endif\n gl_FragColor = o;\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_GAUSSIAN_BLUR\n\n/**\n * 获取权重(对应二维高斯函数公式)\n * \n * @param x \n * @param y\n */\nfloat getWeight(float x, float y) {\n return (1.0 / (2.0 * 3.141592653589793 * pow(0.84089642, 2.0))) * pow(1.0 / 2.718281828459045, (pow(x, 2.0) + pow(y, 2.0)) / (2.0 * pow(0.84089642, 2.0)));\n}\n\n/**\n * 获取权重的加权平均值\n */\nfloat getAverageWeight(float x, float y, float totalWeight) {\n return getWeight(x, y) / totalWeight;\n}\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 #if ENABLE_GAUSSIAN_BLUR\n const float size = ceil(0.84089642 * 6.0);\n float divider = 0.01; \n float totalWeight = 0.0; \n for(float x = -floor(size / 2.0); x<= floor(size/ 2.0); x++) {\n for (float y = -floor(size / 2.0); y<= floor(size/ 2.0); y++) {\n totalWeight += getWeight(x, y);\n }\n }\n\n vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0);\n for(float x = -floor(size / 2.0); x<= floor(size/ 2.0); x++) {\n for (float y = -floor(size / 2.0); y<= floor(size/ 2.0); y++) {\n float weight = getAverageWeight(x, y, totalWeight);\n finalColor += texture2D(texture, v_uv0 + vec2(divider * x, divider * y)) * weight;\n }\n }\n gl_FragColor = finalColor;\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_GAUSSIAN_BLUR\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 #if ENABLE_GAUSSIAN_BLUR\n #endif\n gl_FragColor = o;\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_GAUSSIAN_BLUR\n\n/**\n * 获取权重(对应二维高斯函数公式)\n * \n * @param x \n * @param y\n */\nfloat getWeight(float x, float y) {\n return (1.0 / (2.0 * 3.141592653589793 * pow(0.84089642, 2.0))) * pow(1.0 / 2.718281828459045, (pow(x, 2.0) + pow(y, 2.0)) / (2.0 * pow(0.84089642, 2.0)));\n}\n\n/**\n * 获取权重的加权平均值\n */\nfloat getAverageWeight(float x, float y, float totalWeight) {\n return getWeight(x, y) / totalWeight;\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 #if ENABLE_GAUSSIAN_BLUR\n const float size = ceil(0.84089642 * 6.0);\n float divider = 0.01; \n float totalWeight = 0.0; \n for(float x = -floor(size / 2.0); x<= floor(size/ 2.0); x++) {\n for (float y = -floor(size / 2.0); y<= floor(size/ 2.0); y++) {\n totalWeight += getWeight(x, y);\n }\n }\n\n vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0);\n for(float x = -floor(size / 2.0); x<= floor(size/ 2.0); x++) {\n for (float y = -floor(size / 2.0); y<= floor(size/ 2.0); y++) {\n float weight = getAverageWeight(x, y, totalWeight);\n finalColor += texture(texture, v_uv0 + vec2(divider * x, divider * y)) * weight;\n }\n }\n gl_FragColor = finalColor;\n #endif\n}\n" } } ], diff --git a/assets/materials/sprite-gaussian-blur.mtl b/assets/materials/sprite-gaussian-blur.mtl index 34b9c7c..7ffa495 100644 --- a/assets/materials/sprite-gaussian-blur.mtl +++ b/assets/materials/sprite-gaussian-blur.mtl @@ -7,7 +7,8 @@ "__uuid__": "41f4d474-d707-45bb-af93-637573f92d54" }, "_defines": { - "USE_TEXTURE": true + "USE_TEXTURE": true, + "ENABLE_GAUSSIAN_BLUR": true }, "_props": {} } \ No newline at end of file