////////////////////////////////////////////////
//
// grass shaders
//
////////////////////////////////////////////////

// grasstype:
//   b -> blendmap

grassopt = [ >= (strstr $grasstype $arg1) 0 ]

grassvariantshader = [
    local grasstype
    grasstype = $arg2
    variantshader 0 $arg1 (? (grassopt "b") 0 -1) [
        attribute vec4 vvertex, vcolor;
        attribute vec2 vtexcoord0;
        uniform mat4 camprojmatrix;
        @(ginterpvert)
        varying vec2 texcoord0;
        varying vec4 colorscale;
        @(? (grassopt "b") [uniform vec4 blendmapparams; varying vec2 texcoord1;])
        void main(void)
        {
            gl_Position = camprojmatrix * vvertex;
            colorscale = vcolor;
            texcoord0 = vtexcoord0; 
            @(? (grassopt "b") [
                texcoord1 = (vvertex.xy - blendmapparams.xy)*blendmapparams.zw;
            ])
            @(gdepthpackvert)
        }
    ] [
        uniform sampler2D tex0;
        uniform float grasstest;
        @(ginterpfrag)
        varying vec2 texcoord0;
        varying vec4 colorscale;
        @(? (grassopt "b") [uniform sampler2D tex1; varying vec2 texcoord1;])
        void main(void)
        {
            vec4 color = texture2D(tex0, texcoord0) * colorscale;
            @(? (grassopt "b") [
                color.a *= texture2D(tex1, texcoord1).r;
            ])
            if(color.a <= grasstest)
                discard;
            gcolor = vec4(color.rgb, 0.0);
            @(gnormpack [vec3(0.5, 0.5, 1.0)])
            @(gdepthpackfrag)
        }
    ]
]

grassshader = [
    shadername = (concatword "grass" $arg1)
    grassvariantshader $shadername $arg1
    grassvariantshader $shadername (concatword $arg1 "b")
]

