// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
CCEffect %{
  techniques:
  - name: opaque
    passes:
    - vert: unlit-vs:vert
      frag: unlit-fs:frag
      properties: &props
        noiseTexture:    { value: black }
        mainTexture:    { value: grey }
        textureMoveSpeed: { value: [0, 0] }
        tilingOffset:   { value: [1, 1, 0, 0] }
        noiseStrength:   { value: [1, 1] }
        noiseMoveSpeed:   { value: [0, 0] }
        mainColor:      { value: [1, 1, 1, 1], editor: { type: color } }
        colorScale:     { value: [1, 1, 1], target: colorScaleAndCutoff.xyz }
        alphaThreshold: { value: 0.5, target: colorScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } }
        color:          { target: mainColor, editor: { visible: false } } # backward compability
      migrations: &migs
        properties:
          mainColor:    { formerlySerializedAs: color }
  - name: transparent
    passes:
    - vert: unlit-vs:vert
      frag: unlit-fs:frag
      depthStencilState: &d1
        depthTest: true
        depthWrite: false
      blendState:
        targets:
        - blend: true
          blendSrc: src_alpha
          blendDst: one_minus_src_alpha
          blendDstAlpha: one_minus_src_alpha
      properties: *props
      migrations: *migs
  - name: add
    passes:
    - vert: unlit-vs:vert
      frag: unlit-fs:frag
      rasterizerState: &r1 { cullMode: none }
      depthStencilState: *d1
      blendState:
        targets:
        - blend: true
          blendSrc: src_alpha
          blendDst: one
          blendSrcAlpha: src_alpha
          blendDstAlpha: one
      properties: *props
      migrations: *migs
  - name: alpha-blend
    passes:
    - vert: unlit-vs:vert
      frag: unlit-fs:frag
      rasterizerState: *r1
      depthStencilState: *d1
      blendState:
        targets:
        - blend: true
          blendSrc: src_alpha
          blendDst: one_minus_src_alpha
          blendSrcAlpha: src_alpha
          blendDstAlpha: one_minus_src_alpha
      properties: *props
      migrations: *migs
}%

CCProgram unlit-vs %{
  precision highp float;
  #include <input>
  #include <cc-global>
  #include <cc-local-batch>

  #if USE_VERTEX_COLOR
    in vec4 a_color;
    out vec4 v_color;
  #endif

  #if USE_TEXTURE
    in vec2 a_texCoord;
    out vec2 v_uv;
    out vec2 screen_uv;
    uniform TexCoords {
      vec4 tilingOffset; 
    };
  #endif

  vec4 vert () {
    vec4 position;
    CCVertInput(position);

    mat4 matWorld;
    CCGetWorldMatrix(matWorld);

    #if USE_TEXTURE
      vec2 uv = a_texCoord;
      #if FLIP_UV
        uv.y = 1.0 - v_uv.y;
      #endif
      v_uv = uv * tilingOffset.xy + tilingOffset.zw;
    #endif

    #if USE_VERTEX_COLOR
      v_color = a_color;
    #endif

    vec4 outPos = cc_matProj * (cc_matView * matWorld) * position;
    screen_uv.x = (outPos.x / outPos.z + 1.0) * 0.5;
    screen_uv.y = 1.0 - (outPos.y / outPos.z + 1.0) * 0.5;

    return outPos;
  }
}%

CCProgram unlit-fs %{
  precision mediump float;
  #include <output>

  #if USE_ALPHA_TEST
    #pragma define ALPHA_TEST_CHANNEL options([a, r, g, b])
  #endif

  #if USE_TEXTURE
    in vec2 v_uv;
    uniform sampler2D mainTexture;

    #if USE_TEXTURE_MOVE
    uniform ConstTextureMove{
          vec2 textureMoveSpeed;
    };
    #endif
  #endif

  #if USE_NOISE_TEXTURE
    uniform sampler2D noiseTexture;
    uniform ConstNoiseTexture{
      vec2 noiseStrength;
    };

    #if USE_NOISE_MOVE
    uniform ConstNoiseMove{
      vec2 noiseMoveSpeed;
    };
    #endif
  #endif

  uniform Constant {
    vec4 mainColor;
    vec4 colorScaleAndCutoff;
  };

  #if USE_VERTEX_COLOR
    in vec4 v_color;
  #endif

  vec4 frag () {
    vec4 o = mainColor;
    o.rgb *= colorScaleAndCutoff.xyz;

    #if USE_VERTEX_COLOR
      o *= v_color;
    #endif

    #if USE_TEXTURE
        vec2 uv = v_uv;

        #if USE_TEXTURE_MOVE
          uv.x = uv.x + cc_time.x * textureMoveSpeed.x;
          uv.y = uv.y + cc_time.x * textureMoveSpeed.y;
        #endif

        #if USE_NOISE_TEXTURE
          vec2 noise_uv = v_uv;
          #if USE_NOISE_MOVE
            vec2 speed = vec2(cc_time.x * noiseMoveSpeed.x ,cc_time.x * noiseMoveSpeed.y);
            noise_uv.xy = v_uv.xy + speed.xy;
          #endif
          
          vec4 offset = texture(noiseTexture,noise_uv);
          uv.x = uv.x + (offset.x - 0.5) * noiseStrength.x; 
          uv.y = uv.y + (offset.y - 0.5) * noiseStrength.y; 
        #endif
          o *= texture(mainTexture, uv);
    #endif

    #if USE_ALPHA_TEST
      if (o.ALPHA_TEST_CHANNEL < colorScaleAndCutoff.w) discard;
    #endif

    return CCFragOutput(o);
  }
}%
