////////////////////////////////////////////////
//
// UI shaders
//
////////////////////////////////////////////////

lazyshader 0 "modelpreview" [
    attribute vec4 vvertex;
    attribute vec2 vtexcoord0;
    varying vec2 texcoord0;

    void main(void)
    {
        gl_Position = vvertex;
        texcoord0 = vtexcoord0; 
    }
] [
    @(gfetchdefs [tex0 tex1 tex3])
    uniform vec3 camera;
    uniform mat4 worldmatrix;
    uniform vec4 lightscale;
    uniform vec3 sunlightdir;
    uniform vec3 sunlightcolor;
    uniform float cutout;
    varying vec2 texcoord0;
    fragdata(0) vec4 fragcolor;

    void main(void)
    {
        vec4 normal = gfetch(tex1, texcoord0);
        if(normal.x + normal.y == cutout) discard;

        normal.xyz = normal.xyz*2.0 - 1.0;
        @(if $usepacknorm [result [
            float glowscale = dot(normal.xyz, normal.xyz);
            normal.xyz *= inversesqrt(glowscale);
            @(unpacknorm glowscale)
        ]] [result [
           #define glowscale normal.a
        ]])

        vec4 diffuse = gfetch(tex0, texcoord0);
        vec3 glow = diffuse.rgb * (1.0 - glowscale);
        diffuse.rgb *= glowscale;

        vec3 light = diffuse.rgb * lightscale.rgb;
        light += glow.rgb * lightscale.a;

        float sunfacing = dot(sunlightdir, normal.xyz);
        if(sunfacing > 0.0)
        {
            @(gdepthunpack depth [gfetch(tex3, texcoord0)] [
                vec3 pos = (worldmatrix * vec4(depth*texcoord0, depth, 1.0)).xyz;
            ] [
                vec4 pos = worldmatrix * vec4(texcoord0, depth, 1.0);
                pos.xyz /= pos.w;
            ])
            @(unpackspec)
            float sunspec = pow(clamp(sunfacing*facing - dot(camdir, sunlightdir), 0.0, 1.0), gloss) * specscale;
            light += (diffuse.rgb*sunfacing + sunspec) * sunlightcolor;
        }

        fragcolor.rgb = light;
        fragcolor.a = 1.0;
    }
]

                                                                                                                                                                             
