3.6.3shader求教

// Copyright © 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 }
        miansize: { value: [500.0, 500.0], editor: { tooltip: ‘节点尺寸’ } }
        }%

CCProgram sprite-vs %{
precision highp float;
#include
#if USE_LOCAL
#include
#endif

in vec3 a_position;
in vec2 a_texCoord;
in vec4 a_color;

out vec4 color;
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;

return pos;

}
}%

CCProgram sprite-fs %{
precision highp float;
#include
#include

in vec4 color;

#if USE_TEXTURE
in vec2 uv0;

#pragma builtin(local)
layout(set = 2, binding = 11) uniform sampler2D cc_spriteTexture;

#endif

uniform Properties {
vec2 miansize;
};

float rand(vec2 co) {
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

const float RADIUS = 5.0;

// 获取模糊颜色
vec4 getBlurColor (vec2 pos) {
vec4 color = vec4(0); // 初始颜色
float sum = 0.0; // 总权重
// 卷积过程
for (float r = -RADIUS; r <= RADIUS; r++) { // 水平方向
for (float c = -RADIUS; c <= RADIUS; c++) { // 垂直方向
vec2 target = pos + vec2(r / miansize.x, c / miansize.y); // 目标像素位置
float weight = (RADIUS - abs®) * (RADIUS - abs©); // 计算权重
color += texture(cc_spriteTexture, target) * weight; // 累加颜色
sum += weight; // 累加权重
}
}
color /= sum; // 求出平均值
return color;
}

vec4 frag () {
vec4 o = vec4(1, 1, 1, 1);

#if USE_TEXTURE
  o = getBlurColor(uv0); // 获取模糊后的颜色
  o.a = color.a; // 还原透明度
  #if IS_GRAY
    float gray  = 0.2126 * o.r + 0.7152 * o.g + 0.0722 * o.b;
    o.r = o.g = o.b = gray;
  #endif
#endif

o *= color;
ALPHA_TEST(o);
return o;

}
}%
论坛上拿的3.4.1的高斯模糊效果器脚本,3.6.3导入报错。有大佬帮忙看下哪里有语句有问题么,编译器一直通过不了。

报错信息呢?

image 不好意思 没传上去之前

image

正好在写,顺手帮你改好了
image

代码:

// 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 }
        miansize: { value: [500.0, 500.0] }
}%

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

  out vec4 color;
  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;

    return pos;
  }
}%

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

  in vec4 color;

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

  uniform Properties {
    vec2 miansize;
  };

  const float RADIUS = 5.0;

  // 获取模糊颜色
  vec4 getBlurColor (vec2 pos) {
    vec4 color = vec4(0);
    float sum = 0.0;
    // 卷积过程
    for (float r = -RADIUS; r <= RADIUS; r++) { // 水平方向
      for (float c = -RADIUS; c <= RADIUS; c++) { // 垂直方向
        vec2 target = pos + vec2(r / miansize.x, c / miansize.y); // 目标像素位置
        float weight = (RADIUS - abs(r)) * (RADIUS - abs(c)); // 计算权重
        color += texture(cc_spriteTexture, target) * weight; // 累加颜色
        sum += weight; // 累加权重
      }
    }
    color /= sum; // 求出平均值
    return color;
  }

  float rand(vec2 co) {
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  }

  vec4 frag () {
    vec4 o = vec4(1, 1, 1, 1);

    #if USE_TEXTURE
      o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0);
      o = getBlurColor(uv0); // 获取模糊后的颜色
      #if IS_GRAY
        float gray  = 0.2126 * o.r + 0.7152 * o.g + 0.0722 * o.b;
        o.r = o.g = o.b = gray;
      #endif
    #endif

    o *= color;
    ALPHA_TEST(o);
    return o;
  }
}%

3赞

谢谢大佬!