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

              
                <canvas id="webgl" width="500" height="1758"></canvas> 

<script id="vertexShader" type="x-shader/x-vertex">
  attribute vec4 a_position;
  
  uniform mat4 u_modelViewMatrix;
  uniform mat4 u_projectionMatrix;
  
  void main() {
    gl_Position = a_position;
  }
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
 precision highp float;
  precision highp int;
  
  uniform vec2 u_resolution;
  uniform vec2 u_mouse;
  uniform float u_time;
  uniform sampler2D u_noise;
  
  // movement variables
  vec3 movement = vec3(.0);
  
  const int maxIterations = 64;
  const float stopThreshold = 0.01;
  const float stepScale = .2;
  const float eps = 0.005;
  const vec3 clipColour = vec3(0.);
  const vec3 fogColour = vec3(0.);
  
  const vec3 light1_position = vec3(0, 1., -1.);
  const vec3 light1_colour = vec3(.8, .8, .85);
  
  struct Surface {
    int object_id;
    float distance;
    vec3 position;
    vec3 colour;
    float ambient;
    float spec;
  };
  
  // ---------------------------------------------
  // Math
  // ---------------------------------------------
  float hash( vec3 p ) {
      return fract(sin(dot(p,vec3(127.1,311.7, 74.7)))*43758.5453123);
  }

  float hash( vec2 p ) {
      return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453123);
  }

  float hash( float p ) {
      return fract(sin(p)*43758.5453123);
  }
  vec3 hash33(vec3 p){  
      float n = sin(dot(p, vec3(7, 157, 113)));    
      return fract(vec3(2097152, 262144, 32768)*n); 
  }

  float hash2Interleaved( vec2 x )
  {
      // between random & dithered pattern
      // good for jittering and blur as well as blue noise :)
      // http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
      vec3 magic = vec3( 0.06711056, 0.00583715, 52.9829189 );
      return fract( magic.z * fract( dot( x, magic.xy ) ) );
  }

  vec4 noised( vec3 x )
  {
    // http://www.iquilezles.org/www/articles/gradientnoise/gradientnoise.htm
      vec3 p = floor(x);
      vec3 w = fract(x);

      vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0);
      vec3 du = 30.0*w*w*(w*(w-2.0)+1.0);

      float a = hash( p+vec3(0,0,0) );
      float b = hash( p+vec3(1,0,0) );
      float c = hash( p+vec3(0,1,0) );
      float d = hash( p+vec3(1,1,0) );
      float e = hash( p+vec3(0,0,1) );
      float f = hash( p+vec3(1,0,1) );
      float g = hash( p+vec3(0,1,1) );
      float h = hash( p+vec3(1,1,1) );

      float k0 =   a;
      float k1 =   b - a;
      float k2 =   c - a;
      float k3 =   e - a;
      float k4 =   a - b - c + d;
      float k5 =   a - c - e + g;
      float k6 =   a - b - e + f;
      float k7 = - a + b + c - d + e - f - g + h;

      return vec4( -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z), 
                        2.0* du * vec3( k1 + k4*u.y + k6*u.z + k7*u.y*u.z,
                                        k2 + k5*u.z + k4*u.x + k7*u.z*u.x,
                                        k3 + k6*u.x + k5*u.y + k7*u.x*u.y ) ).yzwx;
  }
  float noise( vec3 x )
  {
    // http://www.iquilezles.org/www/articles/gradientnoise/gradientnoise.htm
      vec3 p = floor(x);
      vec3 w = fract(x);

      vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0);
      vec3 du = 30.0*w*w*(w*(w-2.0)+1.0);

      float a = hash( p+vec3(0,0,0) );
      float b = hash( p+vec3(1,0,0) );
      float c = hash( p+vec3(0,1,0) );
      float d = hash( p+vec3(1,1,0) );
      float e = hash( p+vec3(0,0,1) );
      float f = hash( p+vec3(1,0,1) );
      float g = hash( p+vec3(0,1,1) );
      float h = hash( p+vec3(1,1,1) );

      float k0 =   a;
      float k1 =   b - a;
      float k2 =   c - a;
      float k3 =   e - a;
      float k4 =   a - b - c + d;
      float k5 =   a - c - e + g;
      float k6 =   a - b - e + f;
      float k7 = - a + b + c - d + e - f - g + h;
      return -1.0+2.0*(k0 + k1*u.x + k2*u.y + k3*u.z + k4*u.x*u.y + k5*u.y*u.z + k6*u.z*u.x + k7*u.x*u.y*u.z);
  }
  
  float fbm( vec3 p ) {
    // return 0.;
      float d = 0.;
      float amp = 0.5;

      for(int i=0; i<4; i++) {
          vec4 rnd = noised(p) * amp;
          d += rnd.w;

          p += rnd.xyz * amp;
          amp *= .5;
          p *= 2.;
      }
      return d;
  }
  
  #define TAU 6.2831853w0718
  
  mat2 rot;
  
  // This function describes the world in distances from any given 3 dimensional point in space
  float world(in vec3 position, inout int object_id) {
    float spiral = dot(cos(position.xyz), sin(position.zxy));
    spiral *= 2. + sin(position.z) * .5;
    float n = fbm(position*2.+u_time)*2.;
    // n *= exp(n);
    
    return smoothstep(-1., 1., n) - spiral - .5;
    // return sin(length(position + u_time)) * fbm(position*3.) - spiral;
  }
  float world(in vec3 position) {
    int dummy = 0;
    return world(position, dummy);
  }
  
  vec3 getObjectColour(int object_id) {
    float modid = mod(float(object_id), 5.);
    if(modid == 0.) {
      return vec3(.0, 0., 0.5);
    } else if(modid == 1.) {
      return vec3(.5, 0.5, 0.);
    } else if(modid == 2.) {
      return vec3(.5, 0.5, 0.5);
    } else if(modid == 3.) {
      return vec3(.0, 0.5, 0.5);
    } else if(modid == 4.) {
      return vec3(.0, 0.5, 0.);
    }
    return vec3(.5, 0., 0.);
  }
  
  Surface getSurface(int object_id, float rayDepth, vec3 sp) {
    return Surface(
      object_id, 
      rayDepth, 
      sp, 
      getObjectColour(object_id), 
      .5, 
      200.);
  }
  
  const float minDist = .3;
  
  // The raymarch loop
  Surface rayMarch(vec3 ro, vec3 rd, float start, float end, inout vec3 sceneColour) {
    float sceneDist = 1e4;
    float rayDepth = 0.5*texture2D( u_noise, (gl_FragCoord.xy/u_resolution.x)*2. ).x;
    int object_id = 0;
    float density = 0.;
    
    // rd += (texture2D( u_noise, gl_FragCoord.xy/u_resolution.x ).y - .5)*.01;
    
    for(int i = 0; i < maxIterations; i++) {
      vec3 pos = ro + rd * rayDepth;
      sceneDist = world(pos, object_id);
      
      // if(sceneDist < -3.) {
      //   break;
      // }
      
      // if(sceneDist<1.) {
        float zMod = sin(u_time*5.+pos.z+pos.x)*.3+.5;
        sceneDist = (2.+zMod)-sceneDist;

        float fog = 1. - (rayDepth * .001);

        float localDensity = (minDist - sceneDist)*step(sceneDist, minDist)*.001;
        float normalisedDensity = (1. - density)*localDensity;
        float weightedDensity = normalisedDensity*2.;

        density += weightedDensity*.5;

        // sceneColour += mix(vec3(1., 0., 10.), vec3(1.), 1./(sceneDist+1.)) * normalisedDensity*(sceneDist*3. - .1);
        sceneColour += mix(
          vec3(1.), 
          vec3(2.,3.,zMod*zMod*10.) * normalisedDensity*(sceneDist*1. - .1), 
          fog
        );
      // }
      
      if(density > 1.) {
        break;
      }
      
      if(rayDepth > end) {
        break;
      }
      
      if(sceneDist < stopThreshold) {
        rayDepth += .01;
      } else {
        rayDepth += sceneDist * stepScale;
      }
    }
    
    return getSurface(object_id, rayDepth, ro + rd * rayDepth);
  }
  
  vec3 path(float z) {
    return vec3(cos(z*.5)+sin(-z*.3)*4.,sin(z*.2)+cos(z*.3+.5)*2.,z);
    return vec3(0,0,-100.+z);
  }

  void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
    // uv += dot(uv, uv)*.2;
    
    // movement
    movement = path(u_time*3.);
    
    // Camera and look-at
    vec3 cam = vec3(0);
    vec3 lookAt = vec3(0);
    
    // add movement
    lookAt += path(u_time*3.+1.);
    cam += movement;
    
    // Unit vectors
    vec3 forward = normalize(lookAt - cam);
    vec3 right = normalize(vec3(forward.z, 0., -forward.x));
    vec3 up = normalize(cross(forward, right));
    
    // FOV
    float FOV = 1.5;
    
    // Ray origin and ray direction
    vec3 ro = cam;
    vec3 rd = normalize(forward + FOV * uv.x * right + FOV * uv.y * up);
    
    float d = movement.x*.3;
    float s = sin(d);
    float c = cos(d);
    rd.xy *= mat2(c, -s, s, c);
    
    // Ray marching
    const float clipNear = 0.;
    const float clipFar = 32.;
    vec3 sceneColour = vec3(0.);
    Surface objectSurface = rayMarch(ro, rd, clipNear, clipFar, sceneColour);
    if(objectSurface.distance > clipFar) {
      gl_FragColor = vec4(vec3(1.), 1.);
      return;
    }
    
    gl_FragColor = vec4(sceneColour, 1.);
  }
  
</script>
              
            
!

CSS

              
                body {
  margin:0;
}

canvas {
  height: 100vh !important;
  position: fixed;
  width: 100vw !important;
}
              
            
!

JS

              
                console.clear();

const twodWebGL = new WTCGL(
  document.querySelector('canvas#webgl'), 
  document.querySelector('script#vertexShader').textContent, 
  document.querySelector('script#fragmentShader').textContent,
  window.innerWidth,
  window.innerHeight,
  1,
  false
);
twodWebGL.startTime = -1000;

let debounce;
window.addEventListener('resize', () => {
  clearInterval(debounce);
  debounce = setInterval(() => {
    twodWebGL.resize(window.innerWidth, window.innerHeight);
  }, 100);
});






// track mouse move
let mousepos = [0,0];
const u_mousepos = twodWebGL.addUniform('mouse', WTCGL.TYPE_V2, mousepos);
window.addEventListener('pointermove', (e) => {
  let ratio = window.innerHeight / window.innerWidth;
  if(window.innerHeight > window.innerWidth) {
    mousepos[0] = (e.pageX - window.innerWidth / 2) / window.innerWidth;
    mousepos[1] = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1 * ratio;
  } else {
    mousepos[0] = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
    mousepos[1] = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
  }
  twodWebGL.addUniform('mouse', WTCGL.TYPE_V2, mousepos);
});









// Load all our textures. We only initiate the instance once all images are loaded.
const textures = [
  {
    name: 'noise',
    url: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/noise.png',
    type: WTCGL.IMAGETYPE_TILE,
    img: null
  }
];
const loadImage = function (imageObject) {
  let img = document.createElement('img');
  img.crossOrigin="anonymous";
  
  return new Promise((resolve, reject) => {
    img.addEventListener('load', (e) => {
      imageObject.img = img;
      resolve(imageObject);
    });
    img.addEventListener('error', (e) => {
      reject(e);
    });
    img.src = imageObject.url
  });
}
const loadTextures = function(textures) {
  return new Promise((resolve, reject) => {
    const loadTexture = (pointer) => {
      if(pointer >= textures.length || pointer > 10) {
        resolve(textures);
        return;
      };
      const imageObject = textures[pointer];

      const p = loadImage(imageObject);
      p.then(
        (result) => {
          twodWebGL.addTexture(result.name, result.type, result.img);
        },
        (error) => {
          console.log('error', error)
        }).finally((e) => {
          loadTexture(pointer+1);
      });
    }
    loadTexture(0);
  });
  
}

loadTextures(textures).then(
  (result) => {
    twodWebGL.initTextures();
    // twodWebGL.render();
    twodWebGL.running = true;
  },
  (error) => {
    console.log('error');
  }
);
              
            
!
999px

Console