#pragma language glsl3
#define NR_POINTLIGHTS 2
#ifdef VERTEX

in vec3 VertexNormal;
in vec4 VertexPattern;
in vec3 VertexWobble;
in vec4 VertexBone;
in vec4 VertexWeight;

uniform mat4 MVP;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;

uniform vec3 playerPosition = vec3(0,0,0);
uniform vec3 cameraPosition = vec3(0,0,0);
uniform vec3 fadePoint = vec3(0,0,0);

uniform float playerSpeed;
uniform float wobble;
uniform float time;
uniform float overlay;
uniform float skellyman;
uniform float wire=0;
uniform float onion = 0;
uniform float layer = 0;
uniform float fadeDist = 0;

uniform mat4 u_pose[32];

uniform mat4 shadowMVP;
uniform bool drawShadowed;
uniform int simple;
uniform float vector=0;

uniform float dither = 0;
uniform bool drawPatterned;
uniform bool dropShadow;

out float pointFade;


vec4 position(mat4 transform_projection, vec4 vertex_position) {

  vec4 vp = vertex_position;
  float w=0;
  if (vector==1) {
    vp.z=0;
    w = float(gl_VertexID)/100000;
  }
  vec4 end;
  vp.w = 1; // COMMENT FOR R
  

  if (fadeDist>0) {
    pointFade = (distance(fadePoint,vec3(modelMatrix*vp))+50)/fadeDist;
  }else{
    pointFade=1;
  }

   if (simple>0){
    return MVP*vp;
  }
 
  if (skellyman==1) {
    mat4 a = u_pose[int(VertexBone.x*255.0)] * VertexWeight.x +
    u_pose[int(VertexBone.y*255.0)] * VertexWeight.y +
    u_pose[int(VertexBone.z*255.0)] * VertexWeight.z +
    u_pose[int(VertexBone.w*255.0)] * VertexWeight.w;
    mat4 skeleton = a;

    vp = skeleton*vp;
  }

  if (wobble>0) {
      vp[0] = vp[0]+sin(wobble+vp[0])*0.2;
      vp[1] = vp[1]+cos(wobble+vp[1])*0.2;
      vp[2] = vp[2]+sin(wobble+vp[2])*0.2;
      vp[3] = vp[3];
  }

  if (drawShadowed) {
  }
  if (overlay>0 && overlay<=1) {
    vec4 end = transform_projection*vp;
    return  vec4(end[0],end[1],overlay*0.9,end[3]); 
  }

  if (overlay==2 ) {
    vec4 end = MVP*vp;
    return vec4(end[0],end[1],end[2]-0.01,end[3]);
  }

  if (overlay==3 ) {
    vec4 end = MVP*vp;
    return vec4(end[0],end[1],end[2]/10,end[3]);
  }
  


  if (wire>0) {
    end.z-=1;
  }

  // if (onion>0){
  //   end.z+=onion;
  // }
  

  // vp = vec4(floor(vp.x*5)/5,floor(vp.y*5)/5,floor(vp.z*5)/5,vp[3]);
  end = MVP*vp;


  if (layer!=0){
    end.z+=(layer-w)/distance(cameraPosition,vec3(modelMatrix*vp));
  }

  return  end;

}
#endif

#ifdef PIXEL

in float pointFade;

uniform vec3 colID;
uniform float select=0;
uniform float overlay=0;
uniform float wire=0;
uniform float frame=0;
uniform float onion = 0;
uniform vec4 tint = vec4(1,1,0,0.5);

uniform sampler2DShadow dynamicShadow;
uniform sampler2DShadow staticShadow;

uniform bool drawingShadow;
uniform int simple;
uniform float stripe=0;
uniform float time=0;
uniform float alpha = 1;
uniform float dither = 0;

uniform vec2 canvasSize=vec2(100,100);

float stripeMul = 1;

mat4 thresholdMatrix = mat4(
1.0 / 17.0,  9.0 / 17.0,  3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0,  5.0 / 17.0, 15.0 / 17.0,  7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0,  2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0,  8.0 / 17.0, 14.0 / 17.0,  6.0 / 17.0
);

float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
vec4 effect(vec4 color, sampler2D texturee, vec2 textureCoords, vec2 screen_coords) {

  vec4 texel = Texel(texturee, textureCoords);
  vec4 col = color;

  if (drawingShadow) {
    if (texel.a==1){
      // return vec4(1);
    }else{
      // discard;
    }
  }
  
  // float ran = rand(screen_coords);
  // if (screen_coords.x/love_ScreenSize.x<0.5+ran/2){
  //   discard;
  // }
  // texel.a = ran*10;

  // if ((overlay>0) && (overlay<=1)){
  //   return texel*col;
  // }

  stripeMul = 1;
  if (stripe!=0) {
    float strip = (screen_coords.x*2024+screen_coords.y*2024);
    stripeMul = fract(sin(strip+time));
    stripeMul = ((stripeMul+0.1)/1.1);
    if (stripe==-1) {
      return vec4(stripeMul,stripeMul,stripeMul,1);
    }else{
      stripeMul*=stripe;
      stripeMul-=0.5;
      // return vec4(vec3(stripeMul),1);
    }
  }



  float w = max(wire,frame);
  
  if (w==0 && texel.a==0) {
    // discard;
  }


  if (select==1 || w>0) {
    // return vec4(colID[0],colID[1],colID[2],w>0 ? w : 1 );
  }else {
    if (onion>0) {
      texel.a = tint.a*col.a;
      texel.r = ((col.r+tint.r*3)/4);
      texel.g = ((col.g+tint.g*3)/4);
      texel.b = ((col.b+tint.b*3)/4);

      return texel;
    }else{
      if (dither==1 ) {
        if ((0 - thresholdMatrix[int(mod(screen_coords.x,4))][int(mod(screen_coords.y,4))] )+0.5>0){
          discard;
        }
      }
      vec3 result = texel.rgb*col.rgb;
      result =   stripeMul<1 ? result + vec3( (1.0/pow(4-length(result)+1,4))*10*stripeMul ) : result;
      float a = col.a*texel.a*pointFade*alpha;
      // a=(a<0.5)?pow(a,1.1):1-pow(1-a,1.5);
      return vec4(result,a);
    }
  }
  return vec4(1,1,1,1);
}
#endif


