// 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 }
        alphaThreshold: { value: 0.5 }
        u_UVLeftTop: { 
          value: [0.0, 0.0],
          inspector: {
            tooltip: "左顶点百分比",
            range: [0.0, 1.0]
          }
        }
        u_UVRightBottom: { 
          value: [1.0, 1.0],
           inspector: {
            tooltip: "有下顶点百分比",
            range: [0.0, 1.0]
          }
        }
}%


CCProgram vs %{
  precision highp float;

  #include <cc-global>
  #include <cc-local>

  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;

  #include <alpha-test>

  in vec4 v_color;

  uniform Props {
    float edge;
  };

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


  uniform ARGS{
      vec2 u_UVLeftTop;
      vec2 u_UVRightBottom;
  };

  float insideBox(vec2 v, vec2 bottomLeft, vec2 topRight) {
      vec2 s = step(bottomLeft, v) - step(topRight, v);
      return s.x * s.y;
  }

  float insideBox2(vec2 v, vec2 bottomLeft, vec2 topRight) {
      float lineWidth = 0.003;

      vec2 s = step(bottomLeft+lineWidth, v) - step(topRight-lineWidth, v);
      return s.x * s.y;
  }
  void main () {
      // vec4 o = vec4(1, 1, 1, 1);

      // o *= texture(texture, v_uv0);
      // o *= v_color;

      // float t = insideBox(v_uv0, u_UVLeftTop, u_UVRightBottom);
      // o.a = t * o.a;
      // gl_FragColor = o;

       vec4 o = vec4(1, 1, 1, 1);
      
      #if USE_TEXTURE
      o *= texture(texture, v_uv0);
      #endif
      
      o *= v_color;
      
      // 检查当前像素是否在指定的矩形区域内
      vec2 uv = v_uv0;
      if (uv.x < u_UVLeftTop.x || uv.x > u_UVRightBottom.x || 
          uv.y < u_UVLeftTop.y || uv.y > u_UVRightBottom.y) {
          o.a = 0.0;
      }
      
      gl_FragColor = o;
  }
}%
