// Fork of "Volumetric Clouds I" by ewanovitch. https://shadertoy.com/view/ftVSWW
// 2021-12-28 20:16:11

#define MAX_STEPS 128
#define MIN_DIST 0.01
#define MAX_DIST 100.

struct PointLight {
    vec3 pos;
    vec3 colour;
};

// ------------------------------------------------------------------
// SDF utilities
//
// ----------------
// The MIT License
// Copyright © 2013 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Thanks Inigo Quilez :)

float union_(float a, float b) {
    return min(a, b);
}

float intersection(float a, float b) {
    return max(a, b);
}

float smoothUnion(float a, float b, float smoothing) {
    float h = clamp(0.5 + 0.5 * (b-a)/smoothing, 0., 1.);
    return mix(b, a, h) - smoothing*h*(1.0-h);
}

float smoothIntersection(float a, float b, float smoothing) {
    float h = clamp(0.5 - 0.5 * (b-a)/smoothing, 0., 1.);
    return mix(b, a, h) + smoothing*h*(1.0-h);

}

vec3 repeat(vec3 pt, vec3 centre) {
    return mod(pt + 0.5*centre, centre) - 0.5*centre;
}

float sphereDist(vec3 pt, vec4 sphere) {
    return length(pt - sphere.xyz) - sphere.w;
}


float rand(vec3 n) {
    return fract(sin(dot(n, vec3(12.9898, 4.1414, 9.1919))) * 43758.5453);
}

float hash(vec3 p)  // replace this by something better
{
    p  = fract( p*0.3183099+.1 );
	p *= 17.0;
    return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}

float noise( in vec3 x )
{
    vec3 i = floor(x);
    vec3 f = fract(x);
    f = f*f*(3.0-2.0*f);

    return mix(mix(mix( hash(i+vec3(0,0,0)),
                        hash(i+vec3(1,0,0)),f.x),
                   mix( hash(i+vec3(0,1,0)),
                        hash(i+vec3(1,1,0)),f.x),f.y),
               mix(mix( hash(i+vec3(0,0,1)),
                        hash(i+vec3(1,0,1)),f.x),
                   mix( hash(i+vec3(0,1,1)),
                        hash(i+vec3(1,1,1)),f.x),f.y),f.z);
}

float layeredNoise(vec3 q, float scales[6], int nScales) {
    float f = 0.;
    for (int i = 0; i < nScales; i++) {
        f += scales[i]*noise( q ); q = q*2.01;
    }
    return f;
}

const float noiseScales[6] = float[6](
  1./2.,
  1./4.,
  1./8.,
  1./16.,
  1./32.,
  1./64.
);

// ------------------------------------------------------------------
// Ray marched clouds

const vec3 backgroundColour = vec3(0.3, 0.8, 0.98);
const vec3 cloudColour = vec3(.9);

float saturate(float x) {
    return x > 0. ? 1. : 0.;
}

float densityFunction(vec3 pt) {
    float insideSphere = clamp(-sphereDist(pt, vec4(0., 0., 0., .375)), 0., 1.);
    return layeredNoise(pt * 15., noiseScales, 6) * insideSphere;
}

vec3 marchVolume(vec3 ro, vec3 rd) {
    vec3 p = ro;
    float t = 0.;
    float stepSize = 0.1;
    float densityAttenuation = 6.;
    float density = 0.;
    vec3 colourAccum = vec3(0.);
    while (t < 1.) {
        density += densityFunction(p) * densityAttenuation;
        colourAccum = vec3(
            layeredNoise(p * 3., noiseScales, 2),
            layeredNoise(p * 4., noiseScales, 2),
            layeredNoise(p * 5., noiseScales, 2)
        ) * densityAttenuation * 0.5;
        p += normalize(rd - ro) * t;
        t += stepSize;
    }
    return density * colourAccum;
}

// ------------------------------------------------------------------
// Shader entry point

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    // Normalised device coordinates with aspect ratio: (-1, 1) x (-aspect, aspect)
    vec2 uv = (fragCoord-0.5*iResolution.xy)/iResolution.y;

    // Camera model - rotating around the look-at point with time
    float rotSpeed = 0.05;
    float camDist = 0.9 + sin(iTime * rotSpeed)*.6;
    float zoom = 1.;
    vec3 ro = vec3(camDist * sin(iTime * rotSpeed), 1, -camDist * cos(iTime * rotSpeed));
    vec3 lookAt = vec3(0.);

    // Camera -> image plane
    vec3 forward = normalize(lookAt - ro);
    vec3 right = cross(vec3(0., 1., 0.), forward);
    vec3 up = cross(forward, right);
    vec3 centre = ro + forward * zoom;

    // Shoot ray through pixel into scene
    vec3 pixelPos = centre + uv.x*right + uv.y*up;
    vec3 rd = normalize(pixelPos - ro);

    // Intersect with scene
    vec3 col = marchVolume(ro, rd);
    fragColor = vec4(col,1);
}
