vec2 puddleRand(vec2 c){
    mat2 m = mat2(12.9898,.16180,78.233,.31415);
	return fract(sin(m * c) * vec2(43758.5453, 14142.1));
}

vec2 puddleNoise(vec2 p){
	vec2 co = floor(p);
	vec2 mu = fract(p);
	mu = 3.*mu*mu-2.*mu*mu*mu;
	vec2 a = puddleRand((co+vec2(0.,0.)));
	vec2 b = puddleRand((co+vec2(1.,0.)));
	vec2 c = puddleRand((co+vec2(0.,1.)));
	vec2 d = puddleRand((co+vec2(1.,1.)));
	return mix(mix(a, b, mu.x), mix(c, d, mu.x), mu.y);
}


float getWetness(vec3 pos, vec3 normal, float skyLightMap) {
	float normalElevation = max(normal.y, 0.0);

	float covered = 28.0/32.0;
    float uncovered = 31.0/32.0;

    float coverAmount = map(skyLightMap, covered, uncovered, 0.0, 1.0);

	float puddle = puddleNoise(pos.xz*0.3).x - puddleNoise(pos.xz*0.6).x*0.5 + puddleNoise(pos.xz*2.0).x*0.3;
	puddle = max(pow(puddle, 2.0), 0.00);
    return clamp(puddle*wetness*coverAmount*normalElevation*1.9, 0.0, 1.0);
}

void applyRainWetness(inout vec2 specularMap, inout vec3 normal, in vec3 geoNormal, in float wetFactor) {
    float isMetal = float(specularMap.y > 220.0/255.0);
    wetFactor = mix(wetFactor, 0.0, isMetal);
    specularMap.x = mix(specularMap.x, 0.0, wetFactor*1.5);
    specularMap.y = mix(specularMap.y, 0.02, wetFactor);
    specularMap.x = clamp(specularMap.x, 0.089, 1.0);
    normal = mix(normal, geoNormal, clamp(wetFactor, 0.0, 1.0));
}