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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
    void main() {
        gl_Position = vec4( position, 1.0 );
    }
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
  uniform vec2 u_resolution;
  uniform vec4 u_mouse;
  uniform float u_time;
  uniform sampler2D u_noise;
  uniform sampler2D u_buffer;
  uniform sampler2D u_buffer2;
  uniform bool u_renderpass;
  uniform bool u_renderpass2;
  uniform int u_frame;
  uniform bool u_mousemoved;
  
  #define PI 3.141592653589793
  #define TAU 6.283185307179586

  const bool addNoise = false; // Whether to add noise to the rays
  const float decay = .99; // the amount to decay each sample by
  const float exposure = .9; // the screen exposure
  const float lightStrength = 3.5;
  const vec3 lightcolour = vec3(1.2, 1.15, 1.1)*2.; // the colour of the light
  const vec3 falloffcolour = vec3(.3); // the colour of the falloff
  const vec3 bgcolour = vec3(.1, .15, .15); // the base colour of the render
  const float falloff = .3;
  const int samples = 8; // The number of samples to take
  const float density = .98; // The density of the "smoke"
  const float weight = .25; // how heavily to apply each step of the supersample
  const int octaves = 1; // the number of octaves to generate in the FBM noise
  const float seed = 43758.5453123; // A random seed :)
  
  float random2d(vec2 uv) {
    uv /= 256.;
    vec4 tex = texture2D(u_noise, uv);
    return mix(tex.x, tex.y, tex.a);
  }
  vec2 random2(vec2 st, float seed){
      st = vec2( dot(st,vec2(127.1,311.7)),
                dot(st,vec2(269.5,183.3)) );
      return -1.0 + 2.0*fract(sin(st)*seed);
  }
  
  // Value Noise by Inigo Quilez - iq/2013
  // https://www.shadertoy.com/view/lsf3WH
  float noise(vec2 st, float seed) {
    vec3 x = vec3(st, 1.);
    vec3 p = floor(x);
    vec3 f = fract(x);
    f = f*f*(3.0-2.0*f);
    vec2 uv = (p.xy+vec2(37.0,17.0)*p.z) + f.xy;
    vec2 rg = texture2D(u_noise, (uv+0.5) / 256., 0.).yx - .5;
    return mix( rg.x, rg.y, f.z );
  }
  
  float fbm1(in vec2 _st, float seed) {
    float v = 0.0;
    float a = 0.5;
    vec2 shift = vec2(100.0);
    // Rotate to reduce axial bias
    mat2 rot = mat2(cos(0.5), sin(0.5),
                    -sin(0.5), cos(0.50));
    for (int i = 0; i < octaves; ++i) {
        v += a * noise(_st, seed);
        _st = rot * _st * 2.0 + shift;
        a *= 0.4;
    }
    return v + .4;
  }
  
  float pattern(vec2 uv, float seed, float time, inout vec2 q, inout vec2 r) {

    q = vec2( fbm1( uv + vec2(0.0,0.0), seed ),
                   fbm1( uv + vec2(5.2,1.3), seed ) );

    r = vec2( fbm1( uv + 4.0*q + vec2(1.7 - time / 2.,9.2), seed ),
                   fbm1( uv + 4.0*q + vec2(8.3 - time / 2.,2.8), seed ) );

    float rtn = fbm1( uv + 4.0*r, seed );

    return rtn;
  }

  float shapes(vec2 uv) {
    
    float tex = texture2D(u_buffer2, uv*.1).y;
    float shade1 = 1. - smoothstep(.0, 0.8, tex);
    // shade1 = mix(shade1, 0., smoothstep(.8, 0.2, tex)*.1);
    // shade1 -= smoothstep(.5, 0., texture2D(u_buffer2, uv*.2).y);
    
    return clamp(shade1, 0., 1.);
    
  }
  
  float occlusion(vec2 uv, vec2 lightpos, float objects) {
    
    return (1. - smoothstep(0.0, lightStrength, length(lightpos - uv))) * (1. - objects);
  }
  
  vec4 mainRender(vec2 uv, inout vec4 fragcolour) {
  
    float scale = 6.;
    uv *= scale;
    
    float exposure = exposure + (sin(u_time) * .5 + 1.) * .05;

    vec2 _uv = uv;
    vec2 lightpos = (vec2(u_mouse.x, u_mouse.y * -1.)) / u_resolution.y;
    lightpos = u_mouse.xy * scale;
    
    if(!u_mousemoved) {
      lightpos.x += cos(u_time * .25)*3.;
      lightpos.y += sin(u_time * .5)*2.;
    }
    
    float obj = shapes(uv);
    float map = occlusion(uv, lightpos, obj);

    float _pattern = 0.;
    vec2 q = vec2(0.);
    vec2 r = vec2(0.);
    if(addNoise) {
      _pattern = pattern(_uv * 3. , seed, u_time, q, r) / 2.;
    }

    vec2 dtc = (_uv - lightpos) * (1. / float(samples) * density);
    float illumination_decay = 1.;
    vec3 basecolour = bgcolour - obj * .02;

    for(int i=0; i<samples; i++) {
      _uv -= dtc;
      if(addNoise) {
        uv += _pattern / 16.;
      }
      
      float movement = u_time * 20. * float(i + 1);
      
      float dither = random2d(uv * 512. + mod(vec2(movement*sin(u_time * .5), -movement), 1000.)) * 2.;

      float stepped_map = occlusion(uv, lightpos, shapes(_uv+dtc*dither));
      stepped_map *= illumination_decay * weight;
      illumination_decay *= decay;

      map += stepped_map;
    }

    float l = length(lightpos - uv);

    vec3 lightcolour = mix(lightcolour, falloffcolour, l*falloff);

    vec3 colour = vec3(basecolour+map*exposure*lightcolour);
    // colour *= vec3(sin(u_time+uv.y)*0.2, cos(u_time+uv.x)*0.2, cos(u_time*.3+uv.x-uv.y)*0.3)*1.5+.5;
    
    fragcolour = vec4(colour,1.0);
    
    return fragcolour;
  }

  vec2 hash2(vec2 p)
  {
    vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy;
    return o;
  }
  
  vec3 hsb2rgb( in vec3 c ){
    vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),
                             6.0)-3.0)-1.0,
                     0.0,
                     1.0 );
    rgb = rgb*rgb*(3.0-2.0*rgb);
    return c.z * mix( vec3(1.0), rgb, c.y);
  }
  
  vec3 domain(vec2 z){
    return vec3(hsb2rgb(vec3(atan(z.y,z.x)/TAU,1.,1.)));
  }
  vec3 colour(vec2 z) {
      return domain(z);
  }

  // Shorthand, so that the texture lines read a little better.
  // Borrowed from Shane
  vec4 tx(vec2 p, sampler2D buffer){ return texture2D(buffer, p); }
  
  // 25 (or 9) tap Laplacian -- Gaussian Laplacian, to be more precise. I think of it as taking
  // the sum of the partial second derivatives of a blurry 2D height map... in each channel...
  // I think I'm making things more confusing, but it works anyway. :D Seriously, just look
  // up the Laplacian operator of a 2D function.
  // Borrowed from Shane
  vec4 Laplacian(vec2 p, sampler2D buffer) {

    // Kernel matrix dimension, and a half dimension calculation.
    const int mDim = 5, halfDim = (mDim - 1)/2;

//     float scale = .25;
//     float kernel[25];
//     kernel[0] = 1. * scale;
//     kernel[1] = 1. * scale;
//     kernel[2] = 1. * scale;
//     kernel[3] = 1. * scale;
//     kernel[4] = 1. * scale;

//     kernel[5] = 1. * scale;
//     kernel[6] = 1. * scale;
//     kernel[7] = 1. * scale;
//     kernel[8] = 1. * scale;
//     kernel[9] = 1. * scale;

//     kernel[10] = 1. * scale;
//     kernel[11] = 1. * scale;
//     kernel[12] = -24. * scale;
//     kernel[13] = 1. * scale;
//     kernel[14] = 1. * scale;

//     kernel[15] = 1. * scale;
//     kernel[16] = 1. * scale;
//     kernel[17] = 1. * scale;
//     kernel[18] = 1. * scale;
//     kernel[19] = 1. * scale;

//     kernel[20] = 1. * scale;
//     kernel[21] = 1. * scale;
//     kernel[22] = 1. * scale;
//     kernel[23] = 1. * scale;
//     kernel[24] = 1. * scale;
    
    
    
    float kernel[25];
    kernel[0] = 0.;
    kernel[1] = 0.;
    kernel[2] = 0.25;
    kernel[3] = 0.;
    kernel[4] = 0.;

    kernel[5] = 0.;
    kernel[6] = 0.25;
    kernel[7] = 0.50;
    kernel[8] = 0.25;
    kernel[9] = 0.;

    kernel[10] = 0.25;
    kernel[11] = 0.50;
    kernel[12] = -4.0;
    kernel[13] = 0.50;
    kernel[14] = 0.25;

    kernel[15] = 0.;
    kernel[16] = 0.25;
    kernel[17] = 0.50;
    kernel[18] = 0.25;
    kernel[19] = 0.;

    kernel[20] = 0.;
    kernel[21] = 0.;
    kernel[22] = 0.25;
    kernel[23] = 0.;
    kernel[24] = 0.;
    
    vec4 col = vec4(0);

    float px = 1./u_resolution.y; 

    for (int j=0; j<mDim; j++){
      for (int i=0; i<mDim; i++){ 
        col += kernel[j*mDim + i]*tx(p + vec2(i - halfDim, j - halfDim)*px, buffer);
      }
    }

    return col;
  }


  void render( out vec4 fragColor, in vec2 fragCoord, sampler2D thebuffer, int step ) {

    vec2 p = fragCoord.xy/u_resolution.xy;

    vec4 rdVal = texture2D(thebuffer, p);

    vec2 lap = Laplacian(p, thebuffer).xy;

    float mixamt = clamp(length(p-.5) * 2., 0., 1.);

    float feed = 0.04567;
    float kill = 0.06649;
    vec2 dAB = vec2(.19685, .05405);
    
    if(step == 1) {
      rdVal = texture2D(thebuffer, p * 1.00002 + 0.00005);
      feed = 0.0393167;
      kill = 0.059149;

      dAB = vec2(.22685, .08405)*.9;
    }
    
    float shade_raw = texture2D(u_buffer, (p+.5+vec2(u_time*.05, sin(u_time*.1)*.1))).y;
    float shade1 = smoothstep(0.3, 0.2, shade_raw);
    float shade2 = smoothstep(0.4, 0.0, shade_raw);
    
    if(step == 2) {
      // shade1 = 1.;
      feed = mix(0.040867, .049, shade1);
      kill = mix(0.0819149, .0576, shade1);
      dAB = mix(vec2(.06685, .03405)*.5, dAB, shade1);
    }

    const float t = 1.5; 

    vec2 diffusion = dAB*lap;

    vec2 reaction = vec2(rdVal.x*rdVal.y*rdVal.y)*vec2(-1, 1);

    vec2 feedKill = vec2(feed*(1. - rdVal.x), (kill + feed)*rdVal.y)*vec2(1, -1);
    vec2 delta = diffusion + reaction + feedKill;

    fragColor.xy = clamp(rdVal.xy + delta*t, 0., 1.);

    fragColor.zw = u_resolution.xy;
    if(step == 1) {
      if( u_frame<10 || u_mouse.z == 1. ) {
        vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
        vec2 q, r;
        float n = pattern(uv*10., 12345., u_time, q, r);
        n *= n*n*n*n*n*n*n*40.;
        
        fragColor.y = mix(fragColor.y, 1., n);
        fragColor.x = mix(fragColor.x, 0., n);
      }
    } else {
      if( u_frame<10) {
        vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
        vec2 q, r;
        float n = pattern(uv*10., 12345., u_time, q, r);
        n *= n*n*n*n*n*n*n*40.;
        
        fragColor.y = mix(fragColor.y, 1., n);
        fragColor.x = mix(fragColor.x, 0., n);
      }
    }

  }
  void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
    
    vec2 mouse = u_mouse.xy - uv;
    
    float shade = smoothstep(.1, .15, length(mouse));
    
    vec4 fragcolour = vec4(shade);
    
    if(u_mouse.z == 1.) {
      fragcolour = vec4(shade, 0., 0., 0.);
    } else if(u_mouse.a == 1.) {
      fragcolour = vec4(0., shade, 0., 0.);
    }
    
    // vec3 fragcolour = colour(uv);
    
    if(u_renderpass == true) {
      render( gl_FragColor, gl_FragCoord.xy, u_buffer, 1 );
    } else if(u_renderpass2 == true) {
      render( gl_FragColor, gl_FragCoord.xy, u_buffer2, 2 );
    } else {
  
      mainRender(uv, gl_FragColor);
    }

    // gl_FragColor = fragcolour;
  }
</script>


<div id="container" touch-action="none"></div>
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
}

#container {
  position: fixed;
  touch-action: none;
}
              
            
!

JS

              
                /*
Most of the stuff in here is just bootstrapping. Essentially it's just
setting ThreeJS up so that it renders a flat surface upon which to draw 
the shader. The only thing to see here really is the uniforms sent to 
the shader. Apart from that all of the magic happens in the HTML view
under the fragment shader.
*/

let container;
let camera, scene, renderer;
let uniforms;

let divisor = 1 / 8;
let textureFraction = 1 / 1;

let w = 2048;
let h = 1024;
let tw = 128.;
let th = 128.;
let tw2 = 512.;
let th2 = 512.;

let newmouse = {
  x: 0,
  y: 0
};

let loader=new THREE.TextureLoader();
let texture, rtTexture, rtTexture2;
let rt2Texture, rt2Texture2;
loader.setCrossOrigin("anonymous");
loader.load(
  'https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/noise.png',
  function do_something_with_texture(tex) {
    texture = tex;
    texture.wrapS = THREE.RepeatWrapping;
    texture.wrapT = THREE.RepeatWrapping;
    texture.minFilter = THREE.LinearFilter;
    init();
    animate();
  }
);

function init() {
  container = document.getElementById( 'container' );

  camera = new THREE.Camera();
  camera.position.z = 1;

  scene = new THREE.Scene();

  var geometry = new THREE.PlaneBufferGeometry( 2, 2 );
  
  rtTexture = new THREE.WebGLRenderTarget(tw,th);
  rtTexture2 = new THREE.WebGLRenderTarget(tw,th);
  rt2Texture = new THREE.WebGLRenderTarget(tw2,th2);
  rt2Texture2 = new THREE.WebGLRenderTarget(tw2,th2);

  uniforms = {
    u_time: { type: "f", value: 1.0 },
    u_resolution: { type: "v2", value: new THREE.Vector2() },
    u_noise: { type: "t", value: texture },
    u_buffer: { type: "t", value: rtTexture.texture },
    u_buffer2: { type: "t", value: rt2Texture.texture },
    u_mouse: { type: "v3", value: new THREE.Vector3() },
    u_frame: { type: "i", value: -1. },
    u_renderpass: { type: 'b', value: false },
    u_renderpass2: { type: 'b', value: false },
    u_mousemoved: { type: 'b', value: false }
  };

  var material = new THREE.ShaderMaterial( {
    uniforms: uniforms,
    vertexShader: document.getElementById( 'vertexShader' ).textContent,
    fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  } );
  material.extensions.derivatives = true;

  var mesh = new THREE.Mesh( geometry, material );
  scene.add( mesh );

  renderer = new THREE.WebGLRenderer();
  renderer.setPixelRatio( window.devicePixelRatio );

  container.appendChild( renderer.domElement );

  onWindowResize();
  window.addEventListener( 'resize', onWindowResize, false );
  

  document.addEventListener('pointermove', (e)=> {
    uniforms.u_mousemoved.value = true;
    let ratio = window.innerHeight / window.innerWidth;
    if(window.innerHeight > window.innerWidth) {
      newmouse.x = (e.pageX - window.innerWidth / 2) / window.innerWidth;
      newmouse.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1 * ratio;
    } else {
      newmouse.x = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
      newmouse.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
    }
    
    e.preventDefault();
  });
    document.addEventListener('pointerdown', (e)=> {
      if(e.button === 0) {
        uniforms.u_mouse.value.z = 1;
      } else if (e.button === 2) {
        uniforms.u_mouse.value.w = 1;
      }
      e.preventDefault();
    });
    document.addEventListener('pointerup', (e)=> {
      if(e.button === 0) {
        uniforms.u_mouse.value.z = 0;
      } else if (e.button === 2) {
        uniforms.u_mouse.value.w = 0;
      }
      e.preventDefault();
    });
}

function onWindowResize( event ) {
  w = 2048;
  h = 1024;
  w = window.innerWidth;
  h = window.innerHeight;
  
  renderer.setSize( w, h );
  uniforms.u_resolution.value.x = renderer.domElement.width;
  uniforms.u_resolution.value.y = renderer.domElement.height;
  
  uniforms.u_frame.value = 0;
  
  // rtTexture = new THREE.WebGLRenderTarget(w * textureFraction, h * textureFraction);
  // rtTexture2 = new THREE.WebGLRenderTarget(w * textureFraction, h * textureFraction);
  rtTexture = new THREE.WebGLRenderTarget(tw, th);
  rtTexture2 = new THREE.WebGLRenderTarget(tw, th);
  rt2Texture = new THREE.WebGLRenderTarget(tw2, th2);
  rt2Texture2 = new THREE.WebGLRenderTarget(tw2, th2);
  
  rtTexture.texture.wrapS = THREE.RepeatWrapping;
  rtTexture.texture.wrapT = THREE.RepeatWrapping;
  rtTexture.texture.minFilter = THREE.LinearFilter;
  rtTexture.texture.magFilter = THREE.LinearFilter;
  rtTexture2.texture.wrapS = THREE.RepeatWrapping;
  rtTexture2.texture.wrapT = THREE.RepeatWrapping;
  rtTexture2.texture.minFilter = THREE.LinearFilter;
  rtTexture2.texture.magFilter = THREE.LinearFilter;
  
  
  rt2Texture.texture.wrapS = THREE.RepeatWrapping;
  rt2Texture.texture.wrapT = THREE.RepeatWrapping;
  rt2Texture.texture.minFilter = THREE.LinearFilter;
  rt2Texture.texture.magFilter = THREE.LinearFilter;
  rt2Texture2.texture.wrapS = THREE.RepeatWrapping;
  rt2Texture2.texture.wrapT = THREE.RepeatWrapping;
  rt2Texture2.texture.minFilter = THREE.LinearFilter;
  rt2Texture2.texture.magFilter = THREE.LinearFilter;
}

function animate(delta) {
  requestAnimationFrame( animate );
  render(delta);
}






let capturer = new CCapture( { 
  verbose: true, 
  framerate: 30,
  // motionBlurFrames: 4,
  quality: 90,
  format: 'webm',
  workersPath: 'js/'
 } );
let capturing = false;

isCapturing = function(val) {
  if(val === false && window.capturing === true) {
    capturer.stop();
    capturer.save();
  } else if(val === true && window.capturing === false) {
    capturer.start();
  }
  capturing = val;
}
toggleCapture = function() {
  isCapturing(!capturing);
}

window.addEventListener('keyup', function(e) { if(e.keyCode == 68) toggleCapture(); 
                         onWindowResize();                    });

let then = 0;
let odims = uniforms.u_resolution.value.clone();
function renderTexture(delta) {
  
  let odims = uniforms.u_resolution.value.clone();
  uniforms.u_resolution.value.x = tw;
  uniforms.u_resolution.value.y = th;

  uniforms.u_buffer.value = rtTexture2.texture;
  
  uniforms.u_renderpass.value = true;
  
  window.rtTexture = rtTexture;
  renderer.setRenderTarget(rtTexture);
  renderer.render( scene, camera, rtTexture, true );
  
  let buffer = rtTexture
  rtTexture = rtTexture2;
  rtTexture2 = buffer;
  
  uniforms.u_buffer.value = rtTexture.texture;
  // uniforms.u_resolution.value = odims;
  uniforms.u_renderpass.value = false;
  
  
  
  uniforms.u_resolution.value.x = tw2;
  uniforms.u_resolution.value.y = th2;

  uniforms.u_buffer2.value = rt2Texture2.texture;
  
  uniforms.u_renderpass2.value = true;
  
  window.rt2Texture = rt2Texture;
  renderer.setRenderTarget(rt2Texture);
  renderer.render( scene, camera, rt2Texture, true );
  
  buffer = rt2Texture;
  rt2Texture = rt2Texture2;
  rt2Texture2 = buffer;
  
  uniforms.u_buffer2.value = rt2Texture.texture;
  uniforms.u_resolution.value = odims;
  uniforms.u_renderpass2.value = false;
}
function render(delta) {
  uniforms.u_frame.value++;
  
  uniforms.u_mouse.value.x += ( newmouse.x - uniforms.u_mouse.value.x ) * divisor;
  uniforms.u_mouse.value.y += ( newmouse.y - uniforms.u_mouse.value.y ) * divisor;
  
  uniforms.u_time.value = delta * 0.0005;
  renderer.render( scene, camera );
  renderTexture();
  renderTexture();
  renderTexture();
  renderTexture();
  renderTexture();
  renderTexture();
  renderTexture();
  renderTexture();
  
  if(capturing) {
    capturer.capture( renderer.domElement );
  }
}
              
            
!
999px

Console