Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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;
  
  uniform vec2 u_resolution;
  uniform vec2 u_mouse;
  uniform float u_time;
  uniform sampler2D u_noise;
  
  uniform sampler2D u_buffer;
  uniform bool u_bufferpass;
  
  #define PI 3.14159265359
  #define TAU 6.28318530718
  
  // These awesome complex Math functions curtesy of 
  // https://github.com/mkovacs/reim/blob/master/reim.glsl
  vec2 cCis(float r);
  vec2 cLog(vec2 c); // principal value
  vec2 cInv(vec2 c);
  float cArg(vec2 c);
  float cAbs(vec2 c);
  
  vec2 cMul(vec2 a, vec2 b);
  vec2 cDiv(vec2 a, vec2 b);

  vec2 cCis(float r)
  {
    return vec2( cos(r), sin(r) );
  }
  vec2 cExp(vec2 c)
  {
    return exp(c.x) * cCis(c.y);
  }
  vec2 cConj(vec2 c)
  {
    return vec2(c.x, -c.y);
  }
  vec2 cInv(vec2 c)
  {
    return cConj(c) / dot(c, c);
  }
  vec2 cLog(vec2 c)
  {
    return vec2( log( cAbs(c) ), cArg(c) );
  }
  float cArg(vec2 c)
  {
    return atan(c.y, c.x);
  }
  float cAbs(vec2 c)
  {
    return length(c);
  }
  vec2 cMul(vec2 a, vec2 b)
  {
    return vec2(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
  }
  vec2 cDiv(vec2 a, vec2 b)
  {
    return cMul(a, cInv(b));
  }
  
  float r1 = 0.1;
  float r2 = 0.3;
  
  vec2 Droste(vec2 uv) {
    
    // r1 = .1 + u_mouse.x;
    r2 = .15 + max(u_mouse.y + .5, -.0);
    
    // float c = cos(u_time);
    // float s = sin(u_time);
    // uv *= mat2(c, -s, s, c);
    
    // 5. Take the tiled strips back to ordinary space.
    uv = cLog(uv); 
    // 4. Scale and rotate the strips
    float scale = log(r2/r1);
    float angle = atan(scale/PI);
    uv = cDiv(uv, cExp(vec2(0,angle))*cos(angle));
    // 3. this simulates zooming in the tile
    // uv -= u_time;
    // 2. Tile the strips
    uv.x = mod(uv.x,log(r2/r1)); 
    // 1. Take the annulus to a strip
    uv = cExp(uv)*r1;
    
    return uv;
  }
  
  vec3 hash3( vec2 p ) {
      vec3 q = vec3( dot(p,vec2(127.1,311.7)), 
             dot(p,vec2(269.5,183.3)), 
             dot(p,vec2(419.2,371.9)) );
    return fract(sin(q)*43758.5453);
  }
  
  vec2 getScreenSpace() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
    
    return uv;
  }
  
  const float colours = 3.;
  const vec4 colour1 = vec4(.1,.2,.8, 1.);
  const vec4 colour2 = vec4(.8,.3,.2, 1.);
  const vec4 colour3 = vec4(.1,.7,.2, 1.);
  
  vec4 getColour(float r) {
    float or = r;
    r = floor(r*(colours+1.));
    if(r == 0.) {
      return colour1;
    } else if(r == 1.) {
      return colour2+vec4(0, (sin(u_time*3. + or*10.) * or + or), 0., 0.);
    } else if(r == 2.) {
      return colour3;
    }
  }
  
  vec4 render(vec2 uv) {
    // uv *= 10.;
    
    // uv.x += u_time;
    
    float row = floor(uv.y);
    
    if(mod(row, 2.) == 0.) return vec4(0,0,0,1);
    
    vec4 rowval = texture2D(u_noise, vec2(.5, row/110.));
    float nf = rowval.r;
    nf *= nf;
    nf *= 5.;
    
    uv.x += u_time * nf * 3.;
    
    float noiseloopval = sin(uv.x*PI*.1)*floor(uv.y);
    noiseloopval = mod(uv.x*row, row*2.);
    
    vec2 uvid = floor(vec2( floor(noiseloopval), uv.y ));
    vec3 uvseed = hash3(uvid/PI);
    
    float shapefield = sin(fract(uv.y)*3.) * sin(fract(uv.x)*10.);
    vec4 colour = getColour(rowval.g*(1.-rowval.g*(sin(u_time*3.)*.5+.5))) * smoothstep(.2,.6,shapefield);
    colour += smoothstep(.9,1.,shapefield);
    colour += smoothstep(.99,1.,shapefield)*5.;
    
    // colour *= uvseed.x;
    
    return mix(vec4(0,0,0,1), colour, colour.a);
  }
  
  vec4 render_effect(vec2 uv, vec4 prev) {
    vec2 polar = vec2(atan(uv.x, uv.y)/PI, length(uv));

    vec4 c = render(polar);
    c += render(polar * vec2(2., .6) + vec2(0.,1./.6));
    c += render(polar * vec2(1., 1.2) + vec2(0.,1./1.2));

    uv = Droste(getScreenSpace())*20.;
    polar = vec2(atan(uv.x, uv.y)/PI, length(uv));

    c += render(polar * vec2(1., 2.2) + vec2(0.,2./2.2));
    
    return c;
  }

  void main() {
    vec4 prev = texture2D(u_buffer, gl_FragCoord.xy/u_resolution);
    if(u_bufferpass) {
      vec2 uv = Droste(getScreenSpace())*10.;
      // uv = mix(uv, getScreenSpace()*10., sin(u_time*2.)*.5+.5);
      // uv = getScreenSpace()*10.;

      gl_FragColor = prev * .94 + render_effect(uv, prev) * .05;
    } else {
      vec2 uv = Droste(getScreenSpace())*50.;
      
      gl_FragColor = prev;
    }
  }
  
</script>
            
          
!
            
              body {
  margin:0;
}

canvas {
  position: fixed;
}
            
          
!
            
              console.clear();

w = 1024;
w = 1024;

const twodWebGL = new WTCGL(
  document.querySelector('canvas#webgl'), 
  document.querySelector('script#vertexShader').textContent, 
  document.querySelector('script#fragmentShader').textContent,
  window.innerWidth,
  window.innerHeight,
  2
);
twodWebGL.startTime = -100 + Math.random() * 50;

let fb1 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
let fb2 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
let activeFB = fb1;

let timeout;

window.addEventListener('resize', () => {
  clearTimeout(timeout);
  timeout = setTimeout(() => {
    twodWebGL.resize(window.innerWidth, window.innerHeight);
    fb1 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
    fb2 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
  }, 100);
});

twodWebGL.onRun = (delta) => {
  let _ctx = twodWebGL._ctx;
  
  // find the active texture based on the index
  uniform = _ctx.getUniformLocation(twodWebGL._program, `u_buffer`);
  // Set the texture unit to the uniform
  _ctx.uniform1i(uniform, 5);
  _ctx.activeTexture(_ctx.TEXTURE5);
  // Finally, bind the texture
  _ctx.bindTexture(_ctx.TEXTURE_2D, activeFB.frameTexture);
  activeFB = activeFB === fb1 ? fb2 : fb1;
  
  twodWebGL.addUniform('bufferpass', WTCGL.TYPE_BOOL, true);
  // twodWebGL.resize(1024, 1024);
  twodWebGL.render(activeFB);
  twodWebGL.addUniform('bufferpass', WTCGL.TYPE_BOOL, false);
  // twodWebGL.resize(window.innerWidth, window.innerHeight);
  
  oldmousepos[0] += (mousepos[0] - oldmousepos[0])*.02;
  oldmousepos[1] += (mousepos[1] - oldmousepos[1])*.02;
  
  // oldmousepos = mousepos;
  
  twodWebGL.addUniform('mouse', WTCGL.TYPE_V2, oldmousepos);
}




// track mouse move
let oldmousepos = [0,0];
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;
  }
});









// 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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console