mat3 GetLightmapTBN(vec3 viewPos){
    mat3 lightmapTBN=mat3(normalize(dFdx(viewPos)),normalize(dFdy(viewPos)),vec3(0.));
    lightmapTBN[2]=cross(lightmapTBN[0],lightmapTBN[1]);
    
    return lightmapTBN;
}

float DirectionalLightmap(float lightmap,float lightmapRaw,vec3 normal,mat3 lightmapTBN){
    if(lightmap<.001)return lightmap;
    
    vec2 deriv=vec2(dFdx(lightmapRaw),dFdy(lightmapRaw))*256.;
    vec3 dir=normalize(vec3(deriv.x*lightmapTBN[0]+
            .0005*lightmapTBN[2]+
        deriv.y*lightmapTBN[1]));
        
        float pwr=clamp(dot(normal,dir),-1.,1.);
        if(abs(pwr)>0.)
        pwr=pow(abs(pwr),DIRECTIONAL_LIGHTMAP_STRENGTH)*sign(pwr)*lightmap;
        if(length(deriv)>.001)
        lightmap=pow(lightmap,1.-pwr);
        
        return lightmap;
    }