Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
<canvas id="canvas" style="zoom:2"></canvas>
<script type="glsl">
    precision highp float;
    uniform float time;
    uniform vec2 resolution;
    uniform vec3 eye;
    
    #define MOD3 vec3(.16532,.17369,.15787)
    float Hash(vec3 p) {
        p  = fract(p * MOD3);
        p += dot(p.xyz, p.yzx + 19.19);
        return fract(p.x * p.y * p.z);
    }
    float hashMix(vec3 i, vec3 f, float y, float z) {
        return mix(Hash(i + vec3(0.,y,z)), Hash(i + vec3(1.,y,z)),f.x);
    }
    float Noise(in vec3 p) {
        vec3 i = floor(p), f = fract(p);
        f *= f * (3.0-2.0*f);
        return mix(mix(hashMix(i,f,0.,0.), hashMix(i,f,1.,0.), f.y),
                   mix(hashMix(i,f,0.,1.), hashMix(i,f,1.,1.), f.y), f.z);
    }

    float CloudsFBM(vec3 p) {
        float f = 0.5 * Noise(p); p = p * 3.1;
        f += 0.250000 * Noise(p); p = p * 3.2;
        f += 0.125000 * Noise(p); p = p * 3.3;
        f += 0.062500 * Noise(p); p = p * 3.4;
        f += 0.031250 * Noise(p); p = p * 3.5;
        f += 0.015625 * Noise(p);
        return f;
    }

    float MapClouds(vec3 p, vec3 cameraPosition){
        p /= 1000.; // cloud size
        float dist = length(p-cameraPosition);
        p.z += time / 100. * 50.; // clouds speed
        return CloudsFBM(p) +0.01*dist - .5;
    }

    vec3 CreateClouds(vec3 ro, vec3 rd, float upper, float lower) {
        float diff = upper - lower,
              beg = ((lower - ro.y) / rd.y),
              end = ((upper - ro.y) / rd.y);
        vec3 p = ro + rd*beg,
             add = rd * ((end - beg) / 44.0);
        p.y = 0.;
        vec2 shade, shadeSum;

        for (int i = 0; i < 44; i++) {
            if (shadeSum.y >= 1.0) break;
            shade.y = max(-MapClouds(p, ro), 0.0);
            shade.x = p.y / diff;
            shadeSum += shade * (1.0 - shadeSum.y);
            p += add;
        }
        shadeSum.x /= 10.0;
        shadeSum = min(shadeSum, 1.0);
        vec3 sky = mix(vec3(.0,.1,.4),vec3(.3,.6,.8),1.-rd.y);
        return mix(sky, min(vec3(pow(shadeSum.x, .4)), 1.0), shadeSum.y);
    }

    void main(void) {
        vec3 dir = normalize(vec3(gl_FragCoord.xy - resolution / 2., 
                                  -resolution.y / tan(3.14 / 6.))),
             f = normalize(-eye), 
             s = normalize(cross(f, vec3(.0,.1,.0))),
             worldDir = mat3(s,cross(s, f),-f) * dir,
             color = vec3(.0);
        if (worldDir.y > 0.) 
            color = CreateClouds(eye, worldDir, 3000., 4000.);
        gl_FragColor = vec4(color, 1.);
    }
</script>
</body>
              
            
!

CSS

              
                
              
            
!

JS

              
                    let gl = canvas.getContext('webgl');
    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,  3, -1, -1, 3, -1]), gl.STATIC_DRAW);

    let pid = gl.createProgram();
    shader(`attribute vec2 v;void main(void){gl_Position=vec4(v.xy,0.,1.);}`,gl.VERTEX_SHADER);
    shader(document.querySelector(`script[type="glsl"]`).textContent,gl.FRAGMENT_SHADER);
    gl.linkProgram(pid);
    gl.useProgram(pid);

    let v = gl.getAttribLocation(pid, "v");
    gl.vertexAttribPointer(v, 2, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(v);

    let resolution = gl.getUniformLocation(pid, 'resolution');
    let time = gl.getUniformLocation(pid, 'time');
    let eyeLoc = gl.getUniformLocation(pid, 'eye');

    gl.uniform2f(resolution, gl.drawingBufferWidth, gl.drawingBufferHeight);
    gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);

    var mouse, dragStart, theta = 0, phi = 0;

    addEventListener('mousemove', e => (mouse = e) && dragStart && rotate());
    addEventListener('mouseup', e => dragStart = null);
    addEventListener('mousedown', e => dragStart = {phi, theta, x: e.x, y: e.y});

    updateCameraPosition();
    requestAnimationFrame(draw);

    function draw(t) {
        gl.uniform1f(time, t/500);
        gl.drawArrays(gl.TRIANGLES, 0, 3);
        requestAnimationFrame(draw);
    }

    function shader(src, type) {
        let sid = gl.createShader(type);
        gl.shaderSource(sid, src);
        gl.compileShader(sid);
        var message = gl.getShaderInfoLog(sid);
        gl.attachShader(pid, sid);
        if (message.length > 0) {
            console.log(message, src.split("\n").map((str, i) => (""+(1+i))
                                    .padStart(4, "0")+": "+str).join("\n"));
            throw message;
        }
    }

    function updateCameraPosition() {
        gl.uniform3f(eyeLoc, 
                     Math.cos(phi) * Math.sin(theta),
                     Math.sin(phi),
                     Math.cos(phi) * Math.cos(theta));
    }

    function rotate() {
        if (!dragStart || !mouse) return;
        theta = dragStart.theta + (dragStart.x - mouse.x)/360;
        phi = dragStart.phi + (mouse.y - dragStart.y)/360;
        phi = Math.min(Math.max(phi, -Math.PI/2), Math.PI/2);
        updateCameraPosition()
    }

              
            
!
999px

Console