varying vec2 texCoord;
//varying vec4 vColor;
uniform sampler2D colorMap;
uniform sampler2D unsplash;
uniform samplerCube envMap;
#define sssMap unsplash
uniform int dosss;
uniform int dounsplash;
uniform int twister;
uniform float iTime;

uniform float testvalue;

vec3 iResolution=vec3(1920.0,1080.0,1.0);

uniform vec3 rayorigin;
uniform vec3 raydirection;
uniform vec3 upvector;

uniform float fov;

//uniform int abc;

/*
void main (void)
{
	gl_FragColor = texture2D(colorMap, texCoord);
}
*/

mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
	vec3 cw = normalize(ta-ro);
	vec3 cp = vec3(sin(cr), cos(cr),0.0);
	vec3 cu = normalize( cross(cw,cp) );
	vec3 cv =          ( cross(cu,cw) );
    return mat3( cu, cv, cw );
}

void main (void)
{
//vec2 nutexcoord;
//	nutexcoord=texCoord;
//	nutexcoord.x=1.0-nutexcoord.x;

    vec3 pos;
    vec2 fragCoord=gl_FragCoord.xy;

    vec2 uv = fragCoord.xy/iResolution.xy;
	vec2 p = uv*2.0-1.0;
	p.x*=(iResolution.x/iResolution.y);
	//vec3 col = vec3(0.05);
    vec3 ro = vec3(0.0,0.0,-2.0);
    vec3 rd = normalize(vec3(p,0.0)-ro);

    float fov=1.2;

    vec3 bg;
    vec3 sss;
    if (dounsplash==0){
        ro=rayorigin;// /vec3(20.0);
        vec3 ta=raydirection;// /vec3(20.0);
        mat3 ca = setCamera( ro, ta, 0.0 );
        rd = ca * normalize( vec3(p.x,p.y,fov ) );
        rd = ca * normalize( vec3(p.x,p.y,fov ) );
        //col = render( ro, rd, fragCoord);
        bg=textureCube(envMap,vec3(rd.x,rd.y,rd.z)).xyz; ///background
        sss=texture2D(sssMap, texCoord).rgb;
    }
    else {
        vec2 uv2=uv; uv2.y/=(4096.0/1080.0); uv2.y+=(clamp(iTime/500.0*1.25,0.0,1.0)*(4096.0-1080.0)/4096.0);
        bg=texture2D(unsplash, uv2).rgb;
    }
    if (twister==1.0)
        bg=textureCube(envMap,vec3(rd.x,rd.y,-rd.z)).xyz; ///background
    ///bg=vec3(1,1,0);
    vec4 col=texture2D(colorMap, texCoord);
    if (dosss==1){
        col.rgb*=0.85;
        col.rgb+=(1.0-col.a)*2.0*sss;
        col.a*=1.05;
    }
    ///float a=clamp(col.a+0.33,0.0,0.999);
    float a=clamp(col.a,0.0,0.999);
    vec3 res=mix(bg.rgb,col.rgb,a);//col.rgb*a+bg.rgb*(1.0-a);
	gl_FragColor = vec4(res,1.0);
	//gl_FragColor = texture2D(colorMap, texCoord)*vColor;
	//gl_FragColor = texture2D(colorMap, nutexcoord);
}
