////////////////////////////////////////////////////////
//
// world shaders
//
///////////////////////////////////////////////////////

// worldtype:
//    e -> reserve envmap texture slot
//    r -> envmap reflection
//    R -> modulate envmap reflection with spec map
//    s -> spec
//    S -> spec map
//    g -> glow
//    G -> pulse glow
//    b -> blendmap
//    a -> transparent
//    A -> refractive
//    T -> triplanar
//    d -> detail

wtopt = [ >= (strstr $worldtype $arg1) 0 ]

worldvariantshader = [
    local worldtype
    worldtype = $arg2
    stype = (? (wtopt "e") 3 1)
    srow = -1
    if (wtopt "G") [
        stype = (+ $stype 0x10)
        defuniformparam "glowcolor" 1 1 1 // glow color
        defuniformparam "pulseglowspeed" 1     // pulse frequency (Hz)
        defuniformparam "pulseglowcolor" 0 0 0 // pulse glow color
    ] [if (wtopt "g") [
        defuniformparam "glowcolor" 1 1 1  // glow color
    ]]
    if (wtopt "R") [
        defuniformparam "envmin" 0 0 0 // minimum reflectivity
        defuniformparam "envscale" 1 1 1 // reflectivity map multiplier
    ] [if (wtopt "r") [
        defuniformparam "envmin" 0 0 0 // minimum reflectivity
        defuniformparam "envscale" 0.2 0.2 0.2 // reflectivity 
    ]]
    defuniformparam "gloss" 1 // glossiness
    if (wtopt "S") [
        defuniformparam "specscale" 2 2 2 // spec map multiplier
    ] [if (wtopt "s") [
        defuniformparam "specscale" 1 1 1 // spec multiplier
    ]]
    if (wtopt "T") [
        defuniformparam "triplanarbias" 0.3 0.3 0.3
        stype = (+ $stype 0x20)
    ]
    if (wtopt "b") [
        srow = 0
    ]
    if (wtopt "a") [
        if (wtopt "A") [stype = (+ $stype 4)]
        srow = 1
    ]
    variantshader $stype $arg1 $srow [
        in vec4 vvertex;
        in vec3 vnormal;
        in vec2 vtexcoord0;
        uniform mat4 camprojmatrix;
        uniform vec2 texgenscroll;
        out vec3 nvec;
        @(ginterpvert (|| $msaalight [&& $msaasamples [! (wtopt "a")]]))
        @(if (wtopt "T") [result [
            uniform vec2 texgenscale;
            out vec2 texcoordx, texcoordy, texcoordz;
            @(? (wtopt "d") [uniform vec2 detailscale;])
        ]] [result [
            out vec2 texcoord0;
        ]])
        @(? (wtopt "r") [uniform vec3 camera; out vec3 camvec;])
        @(? (wtopt "G") [uniform float millis; flat out float pulse;])
        @(? (wtopt "b") [
            uniform vec4 blendmapparams;
            out vec2 texcoord1;
        ])

        void main(void)
        {
            gl_Position = camprojmatrix * vvertex;
            @(if (wtopt "T") [result [
                texcoordx = vec2(vvertex.y, -vvertex.z) * texgenscale;
                texcoordy = vec2(vvertex.x, -vvertex.z) * texgenscale;
                texcoordz = vvertex.xy * @(? (wtopt "d") "detailscale" "texgenscale");
            ]] [result [
                texcoord0 = vtexcoord0 + texgenscroll;
            ]])
            @(? (wtopt "b") [
                texcoord1 = (vvertex.xy - blendmapparams.xy)*blendmapparams.zw;
            ])
            nvec = vnormal;

            @(gdepthpackvert (|| $msaalight [&& $msaasamples [! (wtopt "a")]]))

            @(? (wtopt "r") [camvec = camera - vvertex.xyz;])

            @(? (wtopt "G") [
                pulse = abs(fract(millis*pulseglowspeed.x)*2.0 - 1.0);
            ])
        }
    ] [
        @(if (wtopt "A") [result [
            @(gfetchdefs refractlight)
            uniform vec4 refractparams;
        ]])
        uniform vec4 colorparams;
        uniform sampler2D diffusemap;
        @(? (|| $msaalight [&& $msaasamples [! (wtopt "a")]]) [uniform float hashid;])
        in vec3 nvec;
        @(ginterpfrag (|| $msaalight [&& $msaasamples [! (wtopt "a")]]))
        @(if (wtopt "T") [result [
            in vec2 texcoordx, texcoordy, texcoordz;
            @(? (wtopt "d") [uniform sampler2D detaildiffusemap;])
        ]] [result [
            in vec2 texcoord0;
        ]])
        @(? (wtopt "g") [uniform sampler2D glowmap;])
        @(? (wtopt "G") [flat in float pulse;])
        @(? (wtopt "r") [uniform samplerCube envmap; in vec3 camvec;])
        @(? (wtopt "b") [
            uniform float blendlayer;
            uniform sampler2D blendmap;
            in vec2 texcoord1;
        ])

        void main(void)
        {
            vec3 normal = normalize(nvec);

            @(if (wtopt "T") [result [
                vec3 triblend = max(abs(normal) - triplanarbias.xyz, 0.001);
                triblend *= triblend;
                triblend /= triblend.x + triblend.y + triblend.z;

                vec4 diffusex = texture(diffusemap, texcoordx);   
                vec4 diffusey = texture(diffusemap, texcoordy);   
                vec4 diffusez = texture(@(? (wtopt "d") "detaildiffusemap" "diffusemap"), texcoordz);   
                vec4 diffuse = diffusex*triblend.x + diffusey*triblend.y + diffusez*triblend.z;
            ]] [result [
                vec4 diffuse = texture(diffusemap, texcoord0);   
            ]])

            gcolor.rgb = diffuse.rgb*colorparams.rgb;

            @(if (wtopt "r") [result [
                vec3 camvecn = normalize(camvec);
                float invfresnel = dot(camvecn, normal);
                vec3 rvec = 2.0*normal*invfresnel - camvecn;
                vec3 reflect = texture(envmap, rvec).rgb;
                @(? (wtopt "R") [
                    vec3 rmod = envscale.xyz*diffuse.a;
                ] [
                    #define rmod envscale.xyz
                ])
                gcolor.rgb = mix(gcolor.rgb, reflect, envmin.xyz + rmod * clamp(1.0 - invfresnel, 0.0, 1.0));
            ]])

            @(if (wtopt "g") [result [
                @(? (wtopt "T") [
                    vec3 glowx = texture(glowmap, texcoordx).rgb;   
                    vec3 glowy = texture(glowmap, texcoordy).rgb;   
                    vec3 glowz = texture(glowmap, texcoordz).rgb;   
                    vec3 glow = glowx*triblend.x + glowy*triblend.y + glowz*triblend.z;
                ] [
                    vec3 glow = texture(glowmap, texcoord0).rgb;
                ])
                glow *= @(? (wtopt "G") [mix(glowcolor.xyz, pulseglowcolor.xyz, pulse)] [glowcolor.xyz]); 
                @(if (wtopt "a") [result [
                    gglow.rgb = glow;
                ]] [gglowpack glow packnorm])
            ]] [? (wtopt "a") [
                gglow.rgb = vec3(0.0);
            ]])

            @(if (wtopt "a") [
                ? (wtopt "A") [
                    vec3 rlight = gfetch(refractlight, gl_FragCoord.xy).rgb;
                    gglow.rgb += rlight * refractparams.xyz;
                ] [
                    #define packnorm colorparams.a
                ]
            ])

            @(gnormpackdef normal packnorm)

            @(gdepthpackfrag (|| $msaalight [&& $msaasamples [! (wtopt "a")]]) "" hashid)

            @(? (wtopt "b") [
                float blend = abs(texture(blendmap, texcoord1).r - blendlayer);
                gcolor.rgb *= blend;
                gnormal.rgb *= blend;
                gnormal.a *= blendlayer;
            ])

            @(gspecpack [gloss.x] (if (wtopt "s") [? (wtopt "S") [diffuse.a*specscale.x] [specscale.x]]) (? (wtopt "b") blendlayer) blend)
        }
    ]
]

worldshader = [
    worldtype = $arg2
    stype = (? (wtopt "e") 3 1)
    if (wtopt "T") [
        stype = (+ $stype 0x20)
    ]
    defershader $stype $arg1 [
        worldvariantshader @arg1 @arg2
        worldvariantshader @arg1 @(concatword $arg2 "b")
        worldvariantshader @arg1 @(concatword $arg2 "a")
        worldvariantshader @arg1 @(concatword $arg2 "aA")
    ]
]

worldshader "stdworld" ""
forceshader "stdworld"

worldshader "specworld" "s"
worldshader "specmapworld" "sS"

worldshader "glowworld" "g"
worldshader "pulseglowworld" "gG"

worldshader "envworld" "er"
worldshader "envspecworld" "esr"
worldshader "envspecmapworld" "esSrR"
worldshader "envglowworld" "erg"
worldshader "envpulseglowworld" "ergG"

worldshader "triplanarworld" "T"
worldshader "triplanardetailworld" "Td"

// bumptype:
//    e -> reserve envmap texture slot
//    r -> envmap reflection
//    R -> modulate envmap reflection with spec map
//    s -> spec
//    S -> spec map
//    p -> parallax
//    g -> glow
//    G -> pulse glow
//    b -> blendmap
//    a -> transparent
//    A -> refractive
//    T -> triplanar
//    d -> detail

btopt = [ >= (strstr $bumptype $arg1) 0 ]

bumpvariantshader = [
    local bumptype
    bumptype = $arg2
    stype = (? (btopt "e") 3 1)
    srow = -1
    if (btopt "G") [
        stype = (+ $stype 0x10)
        defuniformparam "glowcolor" 1 1 1 // glow color
        defuniformparam "pulseglowspeed" 1     // pulse frequency (Hz)
        defuniformparam "pulseglowcolor" 0 0 0 // pulse glow color
    ] [if (btopt "g") [
        defuniformparam "glowcolor" 1 1 1  // glow color
    ]]
    defuniformparam "gloss" 1 // glossiness
    if (btopt "S") [
        defuniformparam "specscale" 2 2 2 // spec map multiplier
    ] [if (btopt "s") [
        defuniformparam "specscale" 1 1 1 // spec multiplier
    ]]
    if (btopt "p") [
        defuniformparam "parallaxscale" 0.06 -0.03 // parallax scaling
    ]
    if (btopt "R") [
        defuniformparam "envmin" 0 0 0 // minimum reflectivity
        defuniformparam "envscale" 1 1 1 // reflectivity map multiplier
    ] [if (btopt "r") [
        defuniformparam "envmin" 0 0 0 // minimum reflectivity
        defuniformparam "envscale" 0.2 0.2 0.2 // reflectivity 
    ]]
    if (btopt "T") [
        defuniformparam "triplanarbias" 0.3 0.3 0.3
        stype = (+ $stype 0x20)
    ]
    if (btopt "b") [
        srow = 0
    ]
    if (btopt "a") [
        if (btopt "A") [stype = (+ $stype 4)]
        srow = 1
    ]
    variantshader $stype $arg1 $srow [
        in vec4 vvertex, vtangent;
        in vec3 vnormal;
        in vec2 vtexcoord0;
        uniform mat4 camprojmatrix;
        uniform vec2 texgenscroll;
        @(ginterpvert (|| $msaalight [&& $msaasamples [! (btopt "a")]] [btopt "A"]))
        @(if (btopt "T") [result [
            uniform vec2 texgenscale;
            out vec2 texcoordx, texcoordy, texcoordz;
            out vec3 normal;
            out vec3 tangentx, tangenty, tangentz;
            @(? (btopt "d") [uniform vec2 detailscale;])
        ]] [result [
            out vec2 texcoord0;
            out mat3 world;
        ]])
        @(? (|| (btopt "p") (btopt "r") (btopt "T")) [
            uniform vec3 camera;
            out vec3 camvec;
        ])
        @(? (btopt "G") [uniform float millis; flat out float pulse;])
        @(? (btopt "b") [
            uniform vec4 blendmapparams;
            out vec2 texcoord1;
        ])

        void main(void)
        {
            gl_Position = camprojmatrix * vvertex;

            @(gdepthpackvert (|| $msaalight [&& $msaasamples [! (btopt "a")]] [btopt "A"]))

            @(if (btopt "T") [result [
                texcoordx = vec2(vvertex.y, -vvertex.z) * texgenscale;
                texcoordy = vec2(vvertex.x, -vvertex.z) * texgenscale;
                texcoordz = vvertex.xy * @(? (btopt "d") "detailscale" "texgenscale");

                normal = vnormal;
                tangentx = normalize(vec3(1.001, 0.0, 0.0) - vnormal*vnormal.x);
                tangenty = normalize(vec3(0.0, 1.001, 0.0) - vnormal*vnormal.y);
                tangentz = normalize(vec3(0.0, 0.0, -1.001) + vnormal*vnormal.z);
            ]] [result [
                texcoord0 = vtexcoord0 + texgenscroll;                

                vec3 bitangent = cross(vnormal, vtangent.xyz) * vtangent.w;
                // calculate tangent -> world transform
                world = mat3(vtangent.xyz, bitangent, vnormal);
            ]])

            @(? (|| (btopt "p") (btopt "r") (btopt "T")) [
                camvec = camera - vvertex.xyz;
            ])

            @(? (btopt "b") [
                texcoord1 = (vvertex.xy - blendmapparams.xy)*blendmapparams.zw;
            ])

            @(? (btopt "G") [
                pulse = abs(fract(millis*pulseglowspeed.x)*2.0 - 1.0);
            ])
        }
    ] [
        @(if (btopt "A") [result [
            @(gfetchdefs [refractlight refractmask])
            uniform vec4 refractparams;
            uniform float refractdepth;
        ]])
        uniform vec4 colorparams;
        uniform sampler2D diffusemap, normalmap;
        @(? (|| $msaalight [&& $msaasamples [! (btopt "a")]]) [uniform float hashid;])
        in mat3 world;
        @(ginterpfrag (|| $msaalight [&& $msaasamples [! (btopt "a")]] [btopt "A"]))
        @(if (btopt "T") [result [
            in vec2 texcoordx, texcoordy, texcoordz;
            in vec3 normal, tangentx, tangenty, tangentz;
            @(? (btopt "d") [uniform sampler2D detaildiffusemap, detailnormalmap;])
        ]] [result [
            in vec2 texcoord0;
        ]])
        @(? (|| (btopt "p") (btopt "r") (btopt "T")) [in vec3 camvec;])
        @(? (btopt "g") [uniform sampler2D glowmap;])
        @(? (btopt "G") [flat in float pulse;])
        @(? (btopt "r") [uniform samplerCube envmap;])
        @(? (btopt "b") [
            uniform float blendlayer;
            uniform sampler2D blendmap;
            in vec2 texcoord1;
        ])

        void main(void)
        {
            @(? (|| (btopt "p") (btopt "r") (btopt "T")) [
                vec3 camvecn = normalize(camvec);
            ])

            @(if (btopt "T") [result [
                vec3 triblend = max(abs(normal) - triplanarbias.xyz, 0.001);
                triblend *= triblend;
                triblend /= triblend.x + triblend.y + triblend.z;

                #define worldx mat3(tangenty, tangentz, normal)
                #define worldy mat3(tangentx, tangentz, normal)
                #define worldz mat3(tangentx, tangenty, normal)

                @(if (btopt "p") [result [
                    float heightx = texture(normalmap, texcoordx).a;
                    float heighty = texture(normalmap, texcoordy).a;
                    float heightz = texture(@(? (btopt "d") "detailnormalmap" "normalmap"), texcoordz).a;
                    vec3 camvect = camvecn * mat3(tangentx, tangenty, tangentz);

                    vec2 dtcx = texcoordx + camvect.yz*(heightx*parallaxscale.x + parallaxscale.y);
                    vec2 dtcy = texcoordy + camvect.xz*(heighty*parallaxscale.x + parallaxscale.y);
                    vec2 dtcz = texcoordz + camvect.xy*(heightz*parallaxscale.x + parallaxscale.y);
                ]] [result [
                    #define dtcx texcoordx
                    #define dtcy texcoordy
                    #define dtcz texcoordz
                ]])

                vec4 diffusex = texture(diffusemap, dtcx);
                vec4 diffusey = texture(diffusemap, dtcy);
                vec4 diffusez = texture(@(? (btopt "d") "detaildiffusemap" "diffusemap"), dtcz);
                vec4 diffuse = diffusex*triblend.x + diffusey*triblend.y + diffusez*triblend.z;

                vec3 bumpx = (texture(normalmap, dtcx).rgb*2.0 - 1.0)*triblend.x;
                vec3 bumpy = (texture(normalmap, dtcy).rgb*2.0 - 1.0)*triblend.y;
                vec3 bumpz = (texture(@(? (btopt "d") "detailnormalmap" "normalmap"), dtcz).rgb*2.0 - 1.0)*triblend.z;
                vec3 bumpw = normalize(worldx*bumpx + worldy*bumpy + worldz*bumpz);

                @(? (btopt "A") [
                    vec2 bump = bumpx.xy + bumpy.xy + bumpz.xy;
                ])
            ]] [result [
                @(? (btopt "p") [
                    float height = texture(normalmap, texcoord0).a;
                    vec2 dtc = texcoord0 + (camvecn * world).xy*(height*parallaxscale.x + parallaxscale.y);
                ] [
                    #define dtc texcoord0
                ])

                vec4 diffuse = texture(diffusemap, dtc);

                vec3 bump = texture(normalmap, dtc).rgb*2.0 - 1.0;
                vec3 bumpw = normalize(world * bump);
            ]])

            gcolor.rgb = diffuse.rgb*colorparams.rgb;

            @(if (btopt "r") [result [
                float invfresnel = dot(camvecn, bumpw);
                vec3 rvec = 2.0*bumpw*invfresnel - camvecn;
                vec3 reflect = texture(envmap, rvec).rgb;
                @(? (btopt "R") [
                    vec3 rmod = envscale.xyz*diffuse.a;
                ] [
                    #define rmod envscale.xyz
                ])
                gcolor.rgb = mix(gcolor.rgb, reflect, envmin.xyz + rmod * clamp(1.0 - invfresnel, 0.0, 1.0));
            ]])

            @(if (btopt "g") [result [
                @(? (btopt "T") [
                    vec3 glowx = texture(glowmap, dtcx).rgb;   
                    vec3 glowy = texture(glowmap, dtcy).rgb;   
                    vec3 glowz = texture(glowmap, dtcz).rgb;   
                    vec3 glow = glowx*triblend.x + glowy*triblend.y + glowz*triblend.z;
                ] [
                    vec3 glow = texture(glowmap, dtc).rgb;
                ])
                glow *= @(? (btopt "G") [mix(glowcolor.xyz, pulseglowcolor.xyz, pulse)] [glowcolor.xyz]); 
                @(if (btopt "a") [result [
                    gglow.rgb = glow;
                ]] [gglowpack glow packnorm])
            ]] [? (btopt "a") [
                gglow.rgb = vec3(0.0);
            ]])

            @(if (btopt "a") [
                ? (btopt "A") [
                    vec2 rtc = bump.xy*refractparams.w;
                    float rmask = clamp(refractdepth*(lineardepth - dot(gfetch(refractmask, gl_FragCoord.xy + rtc).rgb, gdepthunpackparams)), 0.0, 1.0);
                    vec3 rlight = gfetch(refractlight, gl_FragCoord.xy + rtc*rmask).rgb;
                    gglow.rgb += rlight * refractparams.xyz;
                ] [
                    #define packnorm colorparams.a
                ]
            ])

            @(gnormpackdef bumpw packnorm)

            @(gdepthpackfrag (|| $msaalight [&& $msaasamples [! (btopt "a")]]) "" hashid)

            @(? (btopt "b") [
                float blend = abs(texture(blendmap, texcoord1).r - blendlayer);
                gcolor.rgb *= blend;
                gnormal.rgb *= blend;
                gnormal.a *= blendlayer;
            ])

            @(gspecpack [gloss.x] (if (btopt "s") [? (btopt "S") [diffuse.a*specscale.x] [specscale.x]]) (? (btopt "b") blendlayer) blend)
        }
    ]
]

bumpshader = [
    bumptype = $arg2
    stype = (? (btopt "e") 3 1)
    if (btopt "T") [
        stype = (+ $stype 0x20)
    ]
    defershader $stype $arg1 [
        bumpvariantshader @arg1 @arg2
        bumpvariantshader @arg1 @(concatword $arg2 "b")
        bumpvariantshader @arg1 @(concatword $arg2 "a")
        bumpvariantshader @arg1 @(concatword $arg2 "aA")
    ]
]

bumpshader "bumpworld" ""
bumpshader "bumpspecworld" "s"
bumpshader "bumpspecmapworld" "sS"

bumpshader "bumpglowworld" "g"
bumpshader "bumpspecglowworld" "sg"
bumpshader "bumpspecmapglowworld" "sSg"

bumpshader "bumppulseglowworld" "gG"
bumpshader "bumpspecpulseglowworld" "sgG"
bumpshader "bumpspecmappulseglowworld" "sSgG"

bumpshader "bumpparallaxworld" "p"
bumpshader "bumpspecparallaxworld" "ps"
bumpshader "bumpspecmapparallaxworld" "psS"

bumpshader "bumpparallaxglowworld" "pg"
bumpshader "bumpspecparallaxglowworld" "psg"
bumpshader "bumpspecmapparallaxglowworld" "psSg"

bumpshader "bumpparallaxpulseglowworld" "pgG"
bumpshader "bumpspecparallaxpulseglowworld" "psgG"
bumpshader "bumpspecmapparallaxpulseglowworld" "psSgG"

bumpshader "bumpenvworld" "er"
bumpshader "bumpenvspecworld" "esr"
bumpshader "bumpenvspecmapworld" "esSrR"

bumpshader "bumpenvglowworld" "erg"
bumpshader "bumpenvspecglowworld" "eosrg"
bumpshader "bumpenvspecmapglowworld" "esSrRg"

bumpshader "bumpenvpulseglowworld" "eorgG"
bumpshader "bumpenvspecpulseglowworld" "esrgG"
bumpshader "bumpenvspecmappulseglowworld" "esSrRgG"

bumpshader "bumpenvparallaxworld" "epr"
bumpshader "bumpenvspecparallaxworld" "epsr"
bumpshader "bumpenvspecmapparallaxworld" "epsSrR"

bumpshader "bumpenvparallaxglowworld" "eprg"
bumpshader "bumpenvspecparallaxglowworld" "epsrg"
bumpshader "bumpenvspecmapparallaxglowworld" "epsSrRg"

bumpshader "bumpenvparallaxpulseglowworld" "eprgG"
bumpshader "bumpenvspecparallaxpulseglowworld" "epsrgG"
bumpshader "bumpenvspecmapparallaxpulseglowworld" "epsSrRgG"

bumpshader "triplanarbumpworld" "T"
bumpshader "triplanarbumpspecworld" "Ts"
bumpshader "triplanarbumpspecmapworld" "TsS"
bumpshader "triplanarbumpparallaxworld" "Tp"

bumpshader "triplanarbumpspecparallaxworld" "Tps"
bumpshader "triplanarbumpspecmapparallaxworld" "TpsS"

bumpshader "triplanardetailbumpworld" "Td"
bumpshader "triplanardetailbumpspecworld" "Tds"
bumpshader "triplanardetailbumpspecmapworld" "TdsS"
bumpshader "triplanardetailbumpparallaxworld" "Tdp"

bumpshader "triplanardetailbumpspecparallaxworld" "Tdps"
bumpshader "triplanardetailbumpspecmapparallaxworld" "TdpsS"

shader 0 shadowmapworld [
    in vec4 vvertex;
    uniform mat4 shadowmatrix;
    void main(void)
    {
        gl_Position = shadowmatrix * vvertex;
    }
] [
    void main(void)
    {
    }
]

defershader 1 "rsmworld" [
  loop i 2 [
    variantshader 1 "rsmworld" (- $i 1) [
        in vec4 vvertex;
        in vec3 vnormal;
        in vec2 vtexcoord0;
        uniform mat4 rsmmatrix;
        uniform vec2 texgenscroll;
        uniform vec4 colorparams;
        uniform vec3 rsmdir;
        out vec4 normal;
        out vec2 texcoord0;
        @(? (= $i 1) [
            uniform vec4 blendmapparams;
            out vec2 texcoord1;
        ])
        void main(void)
        {
            gl_Position = rsmmatrix * vvertex;
            texcoord0 = vtexcoord0 + texgenscroll;
            @(if (= $i 1) [result [
                texcoord1 = (vvertex.xy - blendmapparams.xy)*blendmapparams.zw;
            ]])
            normal = vec4(vnormal, dot(vnormal, rsmdir));
        }
    ] [
        uniform vec4 colorparams;
        uniform sampler2D diffusemap;
        in vec4 normal;
        in vec2 texcoord0;
        @(? (= $i 1) [
            uniform float blendlayer;
            uniform sampler2D blendmap;
            in vec2 texcoord1;
        ])
        layout(location = 0) out vec4 gcolor;
        layout(location = 1) out vec4 gnormal;
        void main(void)
        {
            vec4 diffuse = texture(diffusemap, texcoord0);   

            @(if (= $i 2) [result [
                #define alpha 1.0
            ]] [result [
                #define alpha colorparams.a
            ]])

            gcolor.rgb = normal.w*diffuse.rgb*colorparams.rgb;
            gnormal = vec4(normal.xyz*0.5+0.5, 0.0);

            @(if (= $i 1) [result [
                float blend = abs(texture(blendmap, texcoord1).r - blendlayer);
                gcolor.rgb *= blend;
                gcolor.a = blendlayer;
                gnormal *= blend;
            ]] [result [
                gcolor.a = alpha;
            ]])
        }
    ]
  ]
]

