Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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 vec2 u_mouse;
  uniform float u_time;
  uniform sampler2D u_noise;

  vec2 hash2(vec2 p)
  {
    vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy;
    return o;
  }
  
  vec2 cellPoint(vec2 p) {
    vec2 point = hash2(p) * .9 - .5 * .5;
    vec2 pointrandom = hash2(p + 100.);
    return p + .5 + point * vec2(cos(u_time + pointrandom.x * 10.), sin(u_time + pointrandom.y * 10.));
  }
  
  // Fast distance to edge-Voronoi.
  // Since seed positions are heavily restricted,
  // 3x3 check is enough to search for a closest point
  // as well as search for a pair of neighbours.
  // Courtesy of tomkh - https://www.shadertoy.com/user/tomkh
  vec2 edgeVoronoi(vec2 p) {
     vec2 h, pH = floor(p);

     vec2 mh = cellPoint(pH) - p;
     float md = 8.0;
     for (int j=-1; j<=1; ++j )
     for (int i=-1; i<=1; ++i ) {
        h = cellPoint(pH + vec2(i,j)) - p;
        float d = dot(h, h);
        if (d < md) {
           md = d;
           mh = h;
        }
     }

     const float eps = .0001;
     float ed = 8.0;

     for (int j=-1; j<=1; ++j )
     for (int i=-1; i<=1; ++i ) {
        h = cellPoint(pH + vec2(i,j)) - p;
        if (dot(h-mh, h-mh) > eps)
           ed = min( ed, dot( 0.5*(h+mh), normalize(h-mh) ) );
     }
     return vec2(sqrt(md),ed);
  }

  void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
    
    uv *= 5.;
    
    vec2 voronoi = edgeVoronoi(uv);
    
    vec3 colour = vec3(1.);
    colour = mix(vec3(0., 0., .5), colour, 1.-voronoi.x * .5);
    colour = mix(colour, vec3(0.5, 0.5, .5), smoothstep(.4, .5, fract(voronoi.y * 10.)) * smoothstep(.56, .5, fract(voronoi.y * 10.)));
    colour = mix(vec3(1., 0., 0.), colour, smoothstep(.04, .05, voronoi.x));
    colour = mix(vec3(0.), colour, smoothstep(.0, .01, voronoi.y));

    gl_FragColor = vec4(colour,1.0);
  }
</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 loader=new THREE.TextureLoader();
let texture;
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 );

  uniforms = {
    u_time: { type: "f", value: 1.0 },
    u_resolution: { type: "v2", value: new THREE.Vector2() },
    u_noise: { type: "t", value: texture },
    u_mouse: { type: "v2", value: new THREE.Vector2() }
  };

  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)=> {
    let ratio = window.innerHeight / window.innerWidth;
    uniforms.u_mouse.value.x = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
    uniforms.u_mouse.value.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
    
    e.preventDefault();
  });
}

function onWindowResize( event ) {
  renderer.setSize( window.innerWidth, window.innerHeight );
  uniforms.u_resolution.value.x = renderer.domElement.width;
  uniforms.u_resolution.value.y = renderer.domElement.height;
}

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

function render() {
  uniforms.u_time.value += 0.01;
  renderer.render( scene, camera );
}
              
            
!
999px

Console