// 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 }
        

        size_1: { value: [500, 500], editor: { tooltip: '节点尺寸' } }
        center_1: { value: [0.1, 0.3], editor: { tooltip: '中心点 (左上角为原点)' } }
        width_1: { value: 0.3, editor: { tooltip: '宽 (目标宽度 / 节点宽度)' } }
        height_1: { value: 0.2, editor: { tooltip: '高 (目标高度 / 节点宽度)' } }
        round_1: { value: 0.1, editor: { tooltip: '圆角半径 (目标半径 / 节点宽度)' } }
        feather_1: { value: 0.05, editor: { tooltip: '边缘虚化宽度' } }

        size_2: { value: [500, 500], editor: { tooltip: '节点尺寸' } }
        center_2: { value: [0.5, 0.1], editor: { tooltip: '中心点 (左上角为原点)' } }
        width_2: { value: 0.2, editor: { tooltip: '宽 (目标宽度 / 节点宽度)' } }
        height_2: { value: 0.2, editor: { tooltip: '高 (目标高度 / 节点宽度)' } }
        round_2: { value: 0.01, editor: { tooltip: '圆角半径 (目标半径 / 节点宽度)' } }
        feather_2: { value: 0.01, editor: { tooltip: '边缘虚化宽度' } }

        size_3: { value: [500, 500], editor: { tooltip: '节点尺寸' } }
        center_3: { value: [0.8, 0.2], editor: { tooltip: '中心点 (左上角为原点)' } }
        width_3: { value: 0.8, editor: { tooltip: '宽 (目标宽度 / 节点宽度)' } }
        height_3: { value: 0.2, editor: { tooltip: '高 (目标高度 / 节点宽度)' } }
        round_3: { value: 0.01, editor: { tooltip: '圆角半径 (目标半径 / 节点宽度)' } }
        feather_3: { value: 0.01, editor: { tooltip: '边缘虚化宽度' } }

        size_4: { value: [0.5, 0.5], editor: { tooltip: '节点尺寸' } }
        center_4: { value: [0.5, 0.5], editor: { tooltip: '中心点 (左上角为原点)' } }
        width_4: { value: 0.5, editor: { tooltip: '宽 (目标宽度 / 节点宽度)' } }
        height_4: { value: 0.5, editor: { tooltip: '高 (目标高度 / 节点宽度)' } }
        round_4: { value: 0.1, editor: { tooltip: '圆角半径 (目标半径 / 节点宽度)' } }
        feather_4: { value: 0.05, editor: { tooltip: '边缘虚化宽度' } }
        num: { value: 3.0, editor: { tooltip: '挖洞的个数 最多支持4个' } }
}%

CCProgram sprite-vs %{
  precision highp float;
  #include <cc-global>
  #if USE_LOCAL
    #include <cc-local>
  #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;
    // uv0 = uv0 * 2.0;

    color = a_color;

    return pos;
  }
}%

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

  in vec4 color;

  #if USE_TEXTURE
    in vec2 uv0;
    #pragma builtin(local)
    uniform sampler2D cc_spriteTexture;
  #endif


  uniform Properties {
    
    vec2 center_1;
    vec2 size_1;
    float width_1;
    float height_1;
    float round_1;
    float feather_1;


    vec2 center_2;
    vec2 size_2;
    float width_2;
    float height_2;
    float round_2;
    float feather_2;

    vec2 center_3;
    vec2 size_3;
    float width_3;
    float height_3;
    float round_3;
    float feather_3;

    vec2 center_4;
    vec2 size_4;
    float width_4;
    float height_4;
    float round_4;
    float feather_4;

    float num;
  };

  vec4 dig(vec4 color, vec2 center,vec2 size,float width,float height,float round,float feather){
    // 边缘
    float ratio = size.x / size.y;
    float minX = center.x - (width / 2.0);
    float maxX = center.x + (width / 2.0);
    float minY = center.y - (height * ratio / 2.0);
    float maxY = center.y + (height * ratio / 2.0);
    if (uv0.x >= minX && uv0.x <= maxX && uv0.y >= minY && uv0.y <= maxY) {
      if (round == 0.0) discard; // 没有圆角则直接丢弃
      // 圆角处理
      float roundY = round * ratio;
      vec2 vertex;
      if (uv0.x <= minX + round) {
        if (uv0.y <= minY + roundY) {
          vertex = vec2(minX + round, (minY + roundY) / ratio); // 左上角
        } else if (uv0.y >= maxY - roundY) {
          vertex = vec2(minX + round, (maxY - roundY) / ratio); // 左下角
        } else {
          vertex = vec2(minX + round, uv0.y / ratio); // 左中
        }
      } else if (uv0.x >= maxX - round) {
        if (uv0.y <= minY + roundY){
          vertex = vec2(maxX - round, (minY + roundY) / ratio); // 右上角
        } else if (uv0.y >= maxY - roundY) {
          vertex = vec2(maxX - round, (maxY - roundY) / ratio); // 右下角
        } else {
          vertex = vec2(maxX - round, uv0.y / ratio); // 右中
        }
      } else if (uv0.y <= minY + roundY) {
        vertex = vec2(uv0.x, (minY + roundY) / ratio); // 上中
      } else if (uv0.y >= maxY - roundY) {
        vertex = vec2(uv0.x, (maxY - roundY) / ratio); // 下中
      } else {
        discard; // 中间
      }
      float dis = distance(vec2(uv0.x, uv0.y / ratio), vertex);
      color.a = smoothstep(round - feather, round, dis) * color.a;
    }
    return color;
  }

  vec4 frag () {
    vec4 outColor = color;

    #if USE_TEXTURE
      outColor *= CCSampleTexture(cc_spriteTexture, uv0);
      // #if IS_GRAY
      //   float gray  = 0.2126 * outColor.r + 0.7152 * outColor.g + 0.0722 * outColor.b;
      //   outColor.r = outColor.g = outColor.b = gray;
      // #endif
    #endif

    if(num <= 1.0){
      outColor = dig(outColor, center_1, size_1, width_1, height_1, round_1, feather_1);
      // outColor.r = 1.0 ;
    }
    else if(num <= 2.0){
      outColor = dig(outColor, center_1, size_1, width_1, height_1, round_1, feather_1);
      outColor = dig(outColor, center_2, size_2, width_2, height_2, round_2, feather_2);
      // outColor.r = 1.0 ;
    } 
    else if(num <= 3.0){
      outColor = dig(outColor, center_1, size_1, width_1, height_1, round_1, feather_1);
      outColor = dig(outColor, center_2, size_2, width_2, height_2, round_2, feather_2);
      outColor = dig(outColor, center_3, size_3, width_3, height_3, round_3, feather_3);
      // outColor.r = 1.0 ;
    } 
    else if(num <= 4.0){
      outColor = dig(outColor, center_1, size_1, width_1, height_1, round_1, feather_1);
      outColor = dig(outColor, center_2, size_2, width_2, height_2, round_2, feather_2);
      outColor = dig(outColor, center_3, size_3, width_3, height_3, round_3, feather_3);
      outColor = dig(outColor, center_4, size_4, width_4, height_4, round_4, feather_4);
      // outColor.r = 1.0 ;
    }else{
      outColor.r = 1.0 * num;
    }
    
    // outColor.a *= color.a;
    ALPHA_TEST(outColor);
    return outColor;
  }
}%
