float inrect(vec2 pos, float x1, float y1, float x2, float y2, float focus){
    return min(1.0, max(min(min(pos.x - x1, x2 - pos.x), min(pos.y - y1, y2 - pos.y)), 0.0) / focus);
}

float playershadow(){
    vec3 lookvector = look;
    lookvector.x*=2.0;
    vec3 pos = lookvector + vec3(0.4, 0.4, 0.4); 
    vec3 dir = vec3(-1.0, (1.25) * 0.31, 0.0);
    float factor = 1.0;
    
    if(pos.x < 0.2){
        factor = max(0.0, pos.x / 0.4 + 0.5);
    }
    
    pos += dir * pos.x;
    
    float focus = .04;
    float footwalk = sin((position.x - lookvector.x) * 2.0 + (position.z - lookvector.z) * 2.0);
    float handswalk = sin((position.x - lookvector.x) * 2.0 + (position.z - lookvector.z)) * .5;
    
    pos.yz -= vec2(.2, .4);
    
    float body = max(inrect(pos.yz, -1.5 + footwalk*.4, -0.25, 0.75, .1, focus), inrect(pos.yz, -1.5 - footwalk*.4, -.1, 0.75, 0.25, focus));
    float hands = max(inrect(pos.yz, -0.5 + footwalk * .1, -0.5, 0.25, .1, focus), inrect(pos.yz, -0.5 - footwalk * 0.1, -.1, 0.25, 0.5, focus));
    return min(1.0, max(body, hands)) * factor;
}


highp float cos_between_vecs(highp vec3 v1, highp vec3 v2){
 return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) / length(v1) / length(v2);
}

highp float randz(highp float n){
 return fract(cos(n) * 3782.288);
}

highp float noisez(highp float pos){
 highp float start = floor(pos);
 highp float smoothy = smoothstep(0.0, 1.0, fract(pos));
 return mix(randz(start), randz(start + 1.0), smoothy);}

highp float noise2d(highp float horizont, highp float forward){
 highp float start = floor(forward);
 highp float smoothy = smoothstep(0.0, 1.0, fract(forward));
 return mix(noisez(horizont + randz(start) * 1700.0), noisez(horizont + randz(start + 1.0) * 1700.0), smoothy);
}

highp float chuyen_dong(highp vec3 pos){
pos.z += TIME * 0.35;
 highp float chuyen_dong1 = noise2d(pos.z+pos.z+pos.z, pos.z+pos.z+pos.x+pos.x);
 highp float chuyen_dong2 = sin(chuyen_dong1) * 2.5 + 0.5;
float interp = sin(TIME * 2.5 + pos.z+pos.x+pos.z+pos.x) * sin(pos.z) * 0.35;
 return mix(chuyen_dong1, chuyen_dong2, interp);
}

highp vec3 ban_do_nuoc(highp vec3 pos){
 highp float value = (chuyen_dong(pos + vec3(0.6, 0.0, 0.0)) - chuyen_dong(pos));
 return vec3(0.0, 1.0, -value);
}

vec4 mat_troi_mau(highp vec3 pos, float day_flag){
 highp vec3 sun_pos = vec3(pos.x, 000.0, -000.0);
 float white = clamp(abs(day_flag - 0.7) / 0.3 + 0.2, 0.0, 1.0);
 vec4 sun_color = vec4(1.0, white, white, 1.0);
 sun_color.a = pow(max(0.0, 1.0 - length(sun_pos.xz - pos.xz) / 160.0), 0.6);
 return sun_color;
}

vec3 ToneMap(vec3 x) {
    float A = 2.35;
    float B = 0.05;
    float C = 2.40;
    float D = 0.40;
    float E = 0.40;
    float F = 1.0;
    
    x=x*(A*x+B)/(x*(C*x+D)+E/F);
  
return (x-dot(x,vec3(0.1)))*1.5+0.05;}

vec3 acesFilm(vec3 x) { 
	float A = 3.5;
	float B = 0.1;
	float C = 0.7;
	float D = 0.6;
	float E = 0.02;
	float F = 0.5;
return (((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F);
}


highp float random(highp float x){
	return fract(sin(x) * 428.123);
}

highp float srandom(highp float x){
	highp float r1 = random(floor(x));
	highp float r2 = random(floor(x) + 1.0);
	return mix(r1, r2, smoothstep(0.0, 1.0, fract(x)));
}

highp float srandom2d(highp float x, highp float y){
	highp float r1 = srandom(x + floor(y) * 100.0);
	highp float r2 = srandom(x + floor(y) * 100.0 + 100.0);
	return mix(r1, r2, smoothstep(0.0, 1.0, fract(y)));
}

highp float WavingWater(highp vec3 pos, float sizescale, float timescale){
 highp float time = TIME * timescale;
 pos *= sizescale;
 pos.z += TIME;
 highp float interpolate = sin(time + (pos.x + pos.z) / 4.0) * 0.5 + 0.5;
 highp float horizont = pos.x;
 highp float forward = pos.z + srandom2d((pos.x + pos.z) / 7.0, time / 2.0) * 2.0;
 highp float rand1 = srandom2d(horizont / 7.0 + 2526.278, forward);
 highp float rand2 = srandom2d(horizont / 8.0 - 4772.789, forward);
 return max(mix(rand1, rand2, interpolate) - 0.33, 0.0);
}


const float PI = 3.141592;

float cmag(float dist, float t){
	float phase = sin(dist*2.-t*0.3);
	float mp = mod(phase,PI*30.);
	return (mp>0.8&&mp<PI*1.) ? sin(phase) : 0.;
 }

vec2 dw(vec2 p, float t){
	t *= 90.;
	p *= 130.;
	float l = length(p);
	float s = sin(l-t);
	s /= l;
	s *= clamp(1.-l*0.02,0.08,1.);
	float x = (p.x*s);
	float y = (p.y*s);
	float a = cmag(l*0.05,t);
	return vec2(a)*vec2(x,y);
}

vec3 normal_rain(vec2 p,float times){
	vec2 N = dw(p,times);
	N = max(N,dw(p+0.25,times-2.));
	N = max(N,dw(p-0.2,times-1.));
	N = max(N,dw(p-vec2(0.,0.3),times+1.));
	N = max(N,dw(p+vec2(0.5,-0.5),times+0.5));
	N = max(N,dw(p-vec2(0.5,-0.5),times+0.5));
	N = max(N,dw(p+0.5,times+0.5));
	N = max(N,dw(p-0.5,times+0.5));
	N = max(N,dw(p+vec2(0.35,0.),times-0.5));
	N =max(N,dw(p+vec2(0.35,0.2),times-1.5));
	N = max(N,dw(p+vec2(0.,0.35),times+1.5));

	return normalize(vec3(clamp(N,0.,1.),.5));
}

highp float rand(highp vec2 p){ 
	return fract(cos(p.x +p.y * 335.0+sin(p.x*1.011+p.y*p.y)) * 735.552);
}

highp float noise(highp vec2 p) { 
	highp vec2 i = floor(p); 
	highp vec2 f = fract(p); 
	highp vec2 u = pow(f,vec2(2.))*(2. - 1.*f); 

	highp float a = rand(i+vec2(0.,0.));
	highp float b = rand(i+vec2(1.,0.));
	highp float c = rand(i+vec2(0.,1.));
	highp float d = rand(i+vec2(1.,1.));

	highp float tengah = mix(a, b, u.x);
	highp float pinggir = mix(c, d, u.x);
	highp float volume = mix(tengah, pinggir, u.y);
	return volume;
}

vec3 rain_splash(vec3 wp,vec3 p,vec3 d){
	float times = TIME*0.1;
	vec3 light = vec3(-0.5,1.,0.8);
	vec3 N = normal_rain(p.xy,times);
	vec3 np = normalize(wp);
	N *= clamp(pow(noise(np.xz*1.3)*2.,2.5),0.,1.);
	return vec3(dot(N,light)); 
}

highp float ray_sun(highp vec3 wp,vec3 d){
    highp vec3 np = normalize(wp);
    highp float sun = abs(1.-length(np.yz));
    d.r = clamp(d.r*1.2-0.3,0.,0.4);
    return sun*d.r;
}