第六步来到1840行
加入
vec4 CloudColor3(vec4 worldPosition, float sunglow, vec3 worldLightVector)
{
float cloudHeight = Cloud3Height;
float cloudDepth = 150.0f;
float cloudUpperHeight = cloudHeight + (cloudDepth / 2.0f);
float cloudLowerHeight = cloudHeight - (cloudDepth / 2.0f);
if (worldPosition.y < cloudLowerHeight || worldPosition.y > cloudUpperHeight)
return vec4(0.0f);
else
{
vec3 p = worldPosition.xyz / 150.0f;
float t = frameTimeCounter * VOLUMETRIC_CLOUD_SPEED ;
#ifdef Volumetric_Cloud_Type
p.x -= t * 0.02f;
vec3 p1 = p * vec3(1.0f, 0.5f, 1.0f) + vec3(0.0f, t * 0.01f, 0.0f);
float noise = Get3DNoise2(p) * 1.0f;p *= 4.0f;p.x += t * 0.02f; vec3 p2 = p;
noise += (1.0f - abs(Get3DNoise2(p) * 3.0f - 1.0f)) * 0.20f;p *= 3.0f;p.xz += t * 0.05f;
noise += (1.0f - abs(Get3DNoise2(p) * 3.0f - 1.5f)-0.2) * 0.065f;p.xz -=t * 0.165f;p.xz += t * 0.05f;
noise += (1.0f - abs(Get3DNoise2(p) * 3.0f - 1.0f)) * 0.05f;p *= 2.0f;
noise += (1.0 - abs(Get3DNoise3(p) * 2.0 - 1.0)) * 0.015f;
noise /= 1.2f;
#else
t *= 0.0095;
p.x *= 0.5f;
p.x -= t * 0.01f;
vec3 p1 = p * vec3(1.0f, 0.5f, 1.0f) + vec3(0.0f, t * 0.01f, 0.0f);
float noise = Get3DNoise1(p * vec3(1.0f, 0.5f, 1.0f) + vec3(0.0f, t * 0.01f, 0.0f)) * 1.3;p *= 2.0f;p.x -= t * 0.557f;vec3 p2 = p;
noise += (2.0f - abs(Get3DNoise1(p) * 2.0f - 0.0f)) * (0.35f);p *= 3.0f;p.xz -= t * 0.905f;p.x *= 2.0f;vec3 p3 = p; float largeNoise = noise;
noise += (3.0f - abs(Get3DNoise1(p) * 3.0f - 0.0f)) * (0.085f);p *= 3.0f;p.xz -= t * 3.905f;vec3 p4 = p;
noise += (3.0f - abs(Get3DNoise1(p) * 3.0f - 0.0f)) * (0.035f);p *= 3.0f;p.xz -= t * 3.905f;
noise += ((Get3DNoise1(p))) * (0.04f);p *= 3.0f;
noise /= 2.375f;
#endif
const float lightOffset = 0.3f;
float heightGradient = clamp(( - (cloudLowerHeight - worldPosition.y) / (cloudDepth * 1.0f)), 0.0f, 1.0f);
float heightGradient2 = clamp(( - (cloudLowerHeight - (worldPosition.y + worldLightVector.y * lightOffset * 150.0f)) / (cloudDepth * 1.0f)), 0.0f, 1.0f);
float cloudAltitudeWeight = 1.0f - clamp(distance(worldPosition.y, cloudHeight) / (cloudDepth / 2.0f), 0.0f, 1.0f);
cloudAltitudeWeight = (-cos(cloudAltitudeWeight * 3.1415f)) * 0.5 + 0.5;
cloudAltitudeWeight = pow(cloudAltitudeWeight, mix(0.33f, 0.8f, rainStrength));
float cloudAltitudeWeight2 = 1.0f - clamp(distance(worldPosition.y + worldLightVector.y * lightOffset * 150.0f, cloudHeight) / (cloudDepth / 2.0f), 0.0f, 1.0f);
cloudAltitudeWeight2 = (-cos(cloudAltitudeWeight2 * 3.1415f)) * 0.5 + 0.5;
cloudAltitudeWeight2 = pow(cloudAltitudeWeight2, mix(0.33f, 0.8f, rainStrength));
noise *= cloudAltitudeWeight;
//cloud edge
float rainy = mix(wetness, 1.0f, rainStrength);
float coverage = Vol_Cloud_Coverage + rainy * 0.335;
coverage = mix(coverage, 0.77f, rainStrength);
float dist = length(worldPosition.xz - cameraPosition.xz);
coverage *= max(0.0f, 1.0f - dist / 40000.0f);
float density = 0.90f;
noise = GetCoverage(coverage, density, noise);
noise = pow(noise, 1.5);
if (noise <= 0.001f)
{
return vec4(0.0f, 0.0f, 0.0f, 0.0f);
}
float sundiff = Get3DNoise3(p1 + worldLightVector.xyz * lightOffset);
sundiff += (1.0 - abs(Get3DNoise3(p2 + worldLightVector.xyz * lightOffset / 2.0f) * 1.0f - 0.5f) - 0.1) * 0.55f;
sundiff *= 0.955f;
sundiff *= cloudAltitudeWeight2;
float preCoverage = sundiff;
sundiff = -GetCoverage(coverage * 1.0f, density * 0.5, sundiff);
float sundiff2 = -GetCoverage(coverage * 1.0f, 0.0, preCoverage);
float firstOrder = pow(clamp(sundiff * 1.2f + 1.7f, 0.0f, 1.0f), 8.0f);
float secondOrder = pow(clamp(sundiff2 * 1.2f + 1.1f, 0.0f, 1.0f), 4.0f);
float anisoBackFactor = mix(clamp(pow(noise, 1.6f) * 2.5f, 0.0f, 1.0f), 1.0f, pow(sunglow, 1.0f));
firstOrder *= anisoBackFactor * 0.99 + 0.01;
secondOrder *= anisoBackFactor * 1.19 + 0.9;
float directLightFalloff = clamp(pow(-(cloudLowerHeight - worldPosition.y) / cloudDepth, 3.5f), 0.0f, 1.0f);
directLightFalloff *= mix(clamp(pow(noise, 0.9f), 0.0f, 1.0f), clamp(pow(1.0f - noise, 10.3f), 0.0f, 0.5f), pow(sunglow, 1.2f));
vec3 colorDirect = colorSunlight * 4.5f;
colorDirect = mix(colorDirect, colorDirect * vec3(0.1f, 0.2f, 0.3f)*2, timeMidnight);
colorDirect = mix(colorDirect, colorDirect * vec3(0.2f, 0.2f, 0.2f), rainStrength);
colorDirect *= 1.0f + pow(sunglow, 4.0f) * 100.0f;
vec3 colorAmbient = mix(colorSkylight, colorSunlight, 0.15f) * 0.10965f;
colorAmbient = mix(colorAmbient, vec3(0.04) * Luminance(colorSkylight), vec3(rainStrength));
colorAmbient *= mix(1.0f, 0.3f, timeMidnight);
vec3 colorBounced = colorBouncedSunlight * 0.35f;
colorBounced *= pow((1.0f - heightGradient), 8.0f);
colorBounced *= anisoBackFactor + 0.5;
colorBounced *= 1.0 - rainStrength;
vec3 color = mix(colorAmbient, colorDirect, vec3(directLightFalloff));
color += colorBounced;
color *= 8.0f;
vec4 result = vec4(color.rgb, noise);
return result;
}
}
void CalculateClouds (inout vec3 color, inout float cloudAlpha, vec4 worldPos, vec3 viewDir, MaterialMask mask, vec3 worldLightVector, vec3 lightVector)
{
vec2 coord = texcoord.st * 2.0f;
float cloudHeight = 150.0f;
float cloudDepth = 140.0f;
float cloudDensity = 0.89f;
float startingRayDepth = far - 5.0f;
float rayDepth = startingRayDepth;
float rayIncrement = far / CLOUD_DISPERSE;
#ifdef SOFT_FLUFFY_CLOUDS
rayDepth += CalculateDitherPattern1() * rayIncrement;
#else
rayDepth += CalculateDitherPattern2() * rayIncrement;
#endif
int i = 0;
vec3 cloudColor3 = colorSunlight;
vec4 cloudSum = vec4(0.0f);
cloudSum.rgb = color.rgb;
float sunglow = min(CalculateSunglow(viewDir, lightVector), 2.0);
float cloudDistanceMult = 400.0f / far;
float surfaceDistance = length(worldPos);
while (rayDepth > 0.0f) {
//determine worldspace ray position
vec4 rayPosition = GetCloudPosition(texcoord.st, LinearToExpDepth(rayDepth), cloudDistanceMult);
float rayDistance = length((rayPosition.xyz - cameraPosition.xyz) / cloudDistanceMult);
vec4 proximity = CloudColor3(rayPosition, sunglow/1.2, worldLightVector);
proximity.a *= cloudDensity;
if (surfaceDistance < rayDistance * cloudDistanceMult && mask.sky == 0.0)
proximity.a = 0.0f;
cloudSum.rgb = mix( cloudSum.rgb, proximity.rgb, vec3(min(1.0f, proximity.a * cloudDensity)) );
cloudSum.a += proximity.a * cloudDensity;
cloudAlpha += proximity.a;
//Increment ray
rayDepth -= rayIncrement;
i++;
}
color.rgb = mix(color.rgb, cloudSum.rgb, vec3(min(1.0f, cloudSum.a * 50.0f)));
if (cloudSum.a > 0.00f)
{
//materialMask.volumeCloud = 1.0;
}
}