// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

CCEffect %{
  techniques:
  - passes:
    - vert: vs
      frag: fs
      blendState:
        targets:
        - blend: true
      rasterizerState:
        cullMode: none
      properties:
        texture: { value: white }
        x_count: { value: 100.0 }
        y_count: { value: 100.0 }
}%


CCProgram vs %{
  precision highp float;

  #include <cc-global>

  in vec3 a_position;
  in vec4 a_color;
  out vec4 v_color;

  #if USE_TEXTURE
  in vec2 a_uv0;
  out vec2 v_uv0;
  #endif

  void main () {
    vec4 pos = vec4(a_position, 1);

    #if CC_USE_MODEL
    pos = cc_matViewProj * cc_matWorld * pos;
    #else
    pos = cc_matViewProj * pos;
    #endif

    #if USE_TEXTURE
    v_uv0 = a_uv0;
    #endif

    v_color = a_color;

    gl_Position = pos;
  }
}%


CCProgram fs %{
  precision highp float;

  in vec4 v_color;

  #if USE_TEXTURE
  in vec2 v_uv0;
  uniform sampler2D texture;
  #endif

  #if USE_MASAIC
  uniform ARGS{
    float x_count;
    float y_count;
  };

  vec2 getUvMapPos() {
    float block_w = 1.0 / x_count;
    float block_x_idx = floor(v_uv0.x / block_w);

    float block_h = 1.0 / y_count;
    float block_y_idx = floor(v_uv0.y / block_h);

    return vec2(block_w * (block_x_idx + 0.5), block_h * (block_y_idx + 0.5));
  }
  #endif

  void main () {
    vec4 o = vec4(1, 1, 1, 1);
    vec2 realPos = v_uv0;

    #if USE_MASAIC
      realPos = getUvMapPos();
    #endif

    o *= texture(texture, realPos);
    o *= v_color;

    gl_FragColor = o;
  }
}%
