#define vc_cloudSize 0.003

#ifdef isReflectPass
    const float vc_detailFade = 1.0;
#endif

float vc_getCloudMap(in vec3 pos) {
    const float bufferA = vc_depth*0.066;
    const float vc_marchLow = vc_lowEdge-bufferA;
    const float vc_marchHigh = vc_highEdge+bufferA;
    if (pos.y<vc_marchLow || pos.y>vc_marchHigh) return 0.0;

    const float bufferSpace = vc_depth*0.017;
    const float size        = vc_cloudSize;
    const float lowEdge     = vc_lowEdge+bufferSpace;
    const float highEdge    = vc_highEdge-bufferSpace;
    float tick              = animTick*0.03;
    vec3 windTick           = tick*vec3(1.0, -0.3, 0.0);

        pos.xz          = rotateCoord(pos.xz, pi*0.1);

    float lowCoverage   = smoothstep(pos.y, lowEdge, lowEdge+vc_depth*0.15);
        lowCoverage     = pow2(1.0-lowCoverage);
    float highCoverage  = smoothstep(pos.y, highEdge-vc_depth*0.66, highEdge);
        highCoverage   += smoothstep(pos.y, highEdge-vc_depth*0.15, highEdge)*0.4;
    float highFade      = smoothstep(pos.y, highEdge-vc_depth*0.15, highEdge);
        highFade        = 1.0-pow3(highFade);

    vec3 pos2           = pos.xyz;
        pos2.xz         = rotateCoord(pos2.xz, -pi*0.1);

    float shape     = worley2D(pos.xz, 2.2*size, windTick.xz)*2.0-1.0;
        shape      += perlin2D(pos2.xz, 5.4*size, windTick.xz*1.2)*0.75;

        shape      += (s_vcCoverage-1.0)*0.5;

    #ifdef setBiomeEffects
        float biomeCoverage = 0.25*biomeCold+0.25*biomeHot+0.65*biomeDesert-0.15*biomeTropic+0.35*biomeSnow;
        shape      -= biomeCoverage;
    #endif

        shape      += wetness*0.34;
        shape      -= highCoverage*0.36+sqrt(1.0-highFade)*0.4;
        shape      -= lowCoverage*0.25;   
        shape       = shape*0.5+0.5;
        shape      *= highFade;

    float coverage  = linStep(shape, 0.38, 0.68);

    return coverage;
}

float vc_shape(in vec3 pos, in float cloudMap) {
    const float bufferSpace = vc_depth*0.017;
    const float size        = vc_cloudSize;
    const float lowEdge     = vc_lowEdge+bufferSpace;
    const float highEdge    = vc_highEdge-bufferSpace;
    float tick              = animTick*0.03;
    vec3 windTick           = tick*vec3(1.0, -0.3, 0.0);

    float heightGradient    = smoothstep(pos.y, lowEdge, highEdge);

        pos.x          += exp2(heightGradient)*30.0;
        pos.xz          = rotateCoord(pos.xz, pi*0.1);

    vec3 pos2           = pos.xyz;
        pos2.xz         = rotateCoord(pos2.xz, -pi*0.1);

    float offset        = 0.0;

    if (vc_detailFade > 0.0) {
        offset += noise3D(pos.xyz, 3.2*size, -windTick.xyz*1.8)*2.5*vc_detailFade;

        #ifndef isReflectPass
            #if s_vcDetail>=3
                offset += noise3D(pos.xyz, 9.6*size, -windTick.xyz*2.1)*1.5*vc_detailFade;
            #endif
        #endif
    }

    vec3 pnoise         = (offset-vec3(0.5, 0.0, 0.1))*(12.0+heightGradient*2.0);

    vec3 posNoise       = pos+pnoise*1.0;
    vec3 pos2noise      = pos2+pnoise*1.3;

    float noiseFade     = smoothstep(posNoise.y, lowEdge+15.0, vc_altitude+60.0);
    float invNoiseFade  = 1.0-noiseFade*0.5;
    float lowFade       = linStep(posNoise.y, lowEdge, lowEdge+vc_depth*0.07);
        lowFade         = pow2(lowFade);

    //float localCoverage = noise2D(pos.xz, 0.035*size, windTick.xz*0.5)*0.5;

    float shape     = 0.0;
        shape       += getSlicedWorley(pos.xyz, 11.5*size, windTick.xyz*1.3)*0.72;
        //shape      += worleyVolumeHQ(posNoise.xyz, 11.5*size, windTick.xyz*1.3)*0.72;

    if (vc_detailFade > 0.0) {
        #if s_vcDetail>=2
            //shape      += worleyVolume(pos2.xyz, 21.2*size, windTick.xyz*1.3)*0.4;
            shape       += getSlicedWorley(pos2noise.xyz, 26.2*size, windTick.xyz*1.3)*0.4*vc_detailFade;
        #endif

        #ifndef isReflectPass
            #if s_vcDetail>=3
                shape  += noise3D(pos, 11.27*size, -windTick*1.7)*0.07*vc_detailFade;
            #endif

            #if s_vcDetail>=4
                shape  += noise3D(posNoise, 15.57*size, -windTick*2.2)*0.04*invNoiseFade*vc_detailFade;
            #endif
        #endif
    } else {
        shape += (1.0-vc_detailFade)*0.7;
    }

        shape      -= 0.02;
        shape      -= (1.0-cloudMap)*1.25;

        shape      *= 1.7*sqrt(cloudMap);

/*
    float fadeMod   = max(s_vcCoverage, 1.0);
        fadeMod     = pow2(fadeMod)*0.5+fadeMod*0.5;
  */  
    float coverage  = shape;
        coverage   *= lowFade;

        coverage    = max(coverage, 0.0);

    return coverage;
}