// 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 }
        texture1: { value: white}
        texture2: { value: white}
        texture3: { value: white}
        texture4: { value: white}
        texture5: { value: white}
        texture6: { value: white}
        texture7: { value: white}
}%

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 vec3 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;
    float id = mod(a_texCoord.x,10.0);
    uv0.x = (a_texCoord.x - id)*0.000001;
    uv0.y = a_texCoord.y;
    uv0.z = id;

    #if SAMPLE_FROM_RT
      CC_HANDLE_RT_SAMPLE_FLIP(uv0.xy);
    #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 vec3 uv0;
    #pragma builtin(local)
    layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
    uniform sampler2D texture1;
    uniform sampler2D texture2;
    uniform sampler2D texture3;
    uniform sampler2D texture4;
    uniform sampler2D texture5;
    uniform sampler2D texture6;
    uniform sampler2D texture7;
  #endif

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

    #if USE_TEXTURE
    if(uv0.z<0.5)
      o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0.xy);
    else if(uv0.z<1.5)
      o *= CCSampleWithAlphaSeparated(texture1, uv0.xy);
     else if(uv0.z<2.5)
      o *= CCSampleWithAlphaSeparated(texture2, uv0.xy);
     else if(uv0.z<3.5)
      o *= CCSampleWithAlphaSeparated(texture3, uv0.xy);
     else if(uv0.z<4.5)
      o *= CCSampleWithAlphaSeparated(texture4, uv0.xy);
     else if(uv0.z<5.5)
      o *= CCSampleWithAlphaSeparated(texture5, uv0.xy);
     else if(uv0.z<6.5)
      o *= CCSampleWithAlphaSeparated(texture6, uv0.xy);
     else
      o *= CCSampleWithAlphaSeparated(texture7, uv0.xy);

      #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;
  }
}%
