///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//BEGIN_VERT///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#version 120

attribute highp vec3 position;
attribute highp vec2 uv;

uniform highp mat4 agk_World;
uniform highp mat4 agk_ViewProj;
uniform highp vec4 uvBounds0;   // This comes from SetObjectUVScale( ObjID, TexID, 1.0, 1.0 )

//varying highp vec2 vFragPos2D;
varying highp vec3 vFragPos3D;
varying highp vec2 vFragPosUV;

void main() {
    vFragPosUV = uv * uvBounds0.xy + uvBounds0.zw;

    highp vec4 pos = agk_World * vec4(position,1.0);
    vFragPos3D = pos.xyz;
    gl_Position = agk_ViewProj * pos;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//BEGIN_FRAG///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#version 120

uniform vec3 agk_CameraPos;
uniform vec2 agk_resolution;

//uniform vec3  uCamAxisY;   // Grid.*
uniform vec3  uGridAxes;
uniform float uGridLineWidth;

uniform float uSttngGridIncrementMaj;  // Sttng.*
uniform vec4  uSttngGridColorMaj;
//uniform vec4  uSttngGridColorMin;
//uniform float uSttngGridLineWidth;

//varying highp vec2 vFragPos2D;  gl_FragCoord pretty much covers this
varying highp vec3 vFragPos3D; // 3D Position of Fragment
varying highp vec2 vFragPosUV; // UV Position of Fragment

#ifndef GAMMA_TO_LINEAR
#define GAMMA_TO_LINEAR 0.45454545454545454545454545454545454545454545454545
#endif
#ifndef LINEAR_TO_GAMMA
#define LINEAR_TO_GAMMA 2.2
#endif
#ifndef SQUARED_TO_LINEAR
#define SQUARED_TO_LINEAR 0.70710678118654752440084436210484903928483593768847
#endif
#ifndef LINEAR_TO_SQUARED
#define LINEAR_TO_SQUARED 2.0
#endif

void main() {
    // Crappy Grid Drawing.  @todo: better grid drawing...

    float FragPosA;
    float FragPosB;
    if (uGridAxes.x > 0.5) {
        FragPosA = vFragPos3D.x;
        if      (uGridAxes.y > 0.5) { FragPosB = vFragPos3D.y; }
        else if (uGridAxes.z > 0.5) { FragPosB = vFragPos3D.z; }
    } else if (uGridAxes.y > 0.5) {
        FragPosA = vFragPos3D.y;
        FragPosB = vFragPos3D.z;
    }

    //float LineWidth = uGridLineWidth;
    //float LineWidth = uGridLineWidth*(abs(agk_CameraPos.y)*0.03+asin(dot(uCamAxisY, vec3(0.0, 1.0, 0.0) ))*0.011111);
    //float LineWidth = fwidth(distance(agk_CameraPos, vFragPos3D));
    float LineWidth = uGridLineWidth + fwidth(distance(agk_CameraPos, vFragPos3D));

    float End   = uSttngGridIncrementMaj - LineWidth;
    float Start =                          LineWidth;

    float NormalizeMultiplier = 1.0 / LineWidth;

    float ModFragPosA = mod(FragPosA, uSttngGridIncrementMaj);
    float NrmModFragPosA;
    if      (ModFragPosA >= End  ) { NrmModFragPosA = 1.0 - abs(uSttngGridIncrementMaj-ModFragPosA) * NormalizeMultiplier; }
    else if (ModFragPosA <= Start) { NrmModFragPosA = 1.0 - abs(                       ModFragPosA) * NormalizeMultiplier; }

    float ModFragPosB = mod(FragPosB, uSttngGridIncrementMaj);
    float NrmModFragPosB;
    if      (ModFragPosB >= End  ) { NrmModFragPosB = 1.0 - abs(uSttngGridIncrementMaj-ModFragPosB) * NormalizeMultiplier; }
    else if (ModFragPosB <= Start) { NrmModFragPosB = 1.0 - abs(                       ModFragPosB) * NormalizeMultiplier; }

    vec4 TheColor;
    if      ((ModFragPosA >= End  ) && (ModFragPosB >= End  )) { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosA > NrmModFragPosB ? NrmModFragPosA : NrmModFragPosB) ); }
    else if ((ModFragPosA <= Start) && (ModFragPosB <= Start)) { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosA > NrmModFragPosB ? NrmModFragPosA : NrmModFragPosB) ); }
    else if ((ModFragPosB >= End  ) && (ModFragPosA <= Start)) { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosB > NrmModFragPosA ? NrmModFragPosB : NrmModFragPosA) ); }
    else if ((ModFragPosB <= Start) && (ModFragPosA >= End  )) { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosB > NrmModFragPosA ? NrmModFragPosB : NrmModFragPosA) ); }
    else if  (ModFragPosA >= End  )                            { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosA) ); }
    else if  (ModFragPosA <= Start)                            { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosA) ); }
    else if  (ModFragPosB >= End  )                            { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosB) ); }
    else if  (ModFragPosB <= Start)                            { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, smoothstep(0.0, 1.0, NrmModFragPosB) ); }
    else                                                       { TheColor = vec4(uSttngGridColorMaj.r, uSttngGridColorMaj.g, uSttngGridColorMaj.b, 0.0); }

    float CloseFade = 1.0;
    if ((agk_CameraPos.y > -1.0) && (agk_CameraPos.y <  1.0)) { CloseFade = abs(agk_CameraPos.y); }

    gl_FragColor = vec4(TheColor.r, TheColor.g, TheColor.b, TheColor.a * uSttngGridColorMaj.a * vFragPosUV.x * CloseFade);
    //gl_FragColor = vec4(gl_FragCoord.x / agk_resolution.x, gl_FragCoord.y / agk_resolution.y, 0.0, 1.0);
    //gl_FragColor = vec4(gl_FragCoord.x / agk_resolution.x, 0.0, 0.0, 1.0);
    //gl_FragColor = vec4(0.0, gl_FragCoord.y / agk_resolution.y, 0.0, 1.0);
    //gl_FragColor = vec4(vec3(  pow(gl_FragCoord.w, SQUARED_TO_LINEAR)  ), 1.0);
    //gl_FragColor = vec4(vec3(  pow(pow(gl_FragCoord.w, SQUARED_TO_LINEAR), LINEAR_TO_GAMMA)  ), 1.0);
    //gl_FragColor = vec4(vec3(gl_FragCoord.w), 1.0);


    //gl_FragColor = vec4(vec3(vFragPosUV.x), 1.0);

    //gl_FragColor = vec4(vec3(pow(vFragPosUV.x, LINEAR_TO_GAMMA)+noise1(gl_FragCoord.x+(gl_FragCoord.y*agk_resolution.x))), 1.0);

    //TheColor = vec4(vec3(  pow(vFragPosUV.x, LINEAR_TO_GAMMA) + (fract(sin(dot(gl_FragCoord.xy ,vec2(12.9898,78.233))) * 43758.5453)*0.01)  ), 1.0);
    //gl_FragColor = TheColor;

}

