分享一个3.x的性能比较好的图片模糊shader

// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
CCEffect %{
  techniques:
  - passes:
    - vert: sprite-vs:vert
      frag: sprite-fs:frag
      depthStencilState:
        depthTest: false
        depthWrite: false
      blendState:
        targets:
        - blend: true
          blendSrc: src_alpha
          blendDst: one_minus_src_alpha
          blendDstAlpha: one_minus_src_alpha
      rasterizerState:
        cullMode: none
      properties:
        alphaThreshold: { value: 0.5 }
        uvOffset: {value: .3}
}%

CCProgram sprite-vs %{
  precision highp float;
  #include <cc-global>
  #if USE_LOCAL
    #include <cc-local>
  #endif
  #if SAMPLE_FROM_RT
    #include <common>
  #endif
  in vec3 a_position;
  in vec2 a_texCoord;
  in vec4 a_color;

  out vec4 pos0;
  out vec2 uv0;

  vec4 vert () {
    vec4 pos = vec4(a_position, 1);

    #if USE_LOCAL
      pos = cc_matWorld * pos;
    #endif

    #if USE_PIXEL_ALIGNMENT
      pos = cc_matView * pos;
      pos.xyz = floor(pos.xyz);
      pos = cc_matProj * pos;
    #else
      pos = cc_matViewProj * pos;
    #endif

    uv0 = a_texCoord;

    #if SAMPLE_FROM_RT
      CC_HANDLE_RT_SAMPLE_FLIP(uv0);
    #endif
    // color = a_color;
    pos0 = pos;
    return pos;
  }
}%

CCProgram sprite-fs %{
  precision highp float;
  #include <embedded-alpha>
  #include <alpha-test>

  in vec4 pos0;

  uniform Properties {
    float uvOffset;
  };

  #if USE_TEXTURE
    in vec2 uv0;
    #pragma builtin(local)
    layout(set = 2, binding = 11) uniform sampler2D cc_spriteTexture;
  #endif

  vec4 frag () {
    // vec3 test;
    vec4 s1 = texture(cc_spriteTexture, uv0 + vec2(uvOffset, 0.00));
    vec4 s2 = texture(cc_spriteTexture, uv0 + vec2(-uvOffset, 0.00));
    vec4 s3 = texture(cc_spriteTexture, uv0 + vec2(0.00, uvOffset));
    vec4 s4 = texture(cc_spriteTexture, uv0 + vec2(0.00, -uvOffset));

    vec4 texCol = texture(cc_spriteTexture, uv0);

    vec4 o;
    float pct = .2;
    // vec4 s2 = texture()
    #if USE_TEXTURE
      // o.a = color.a; // 还原透明度
      o = texCol * (1.0 - pct * 4.0) + s1 * pct + s2 * pct + s3 * pct + s4 * pct;
    #endif
    // o *= pos0;
    ALPHA_TEST(o);
    return o;
  }
}%

效果图:

9赞

没人回复你我回复你,每一个懂得分享的人都是伟大的人!

你是cocos forum最靓的仔

:+1::+1: