css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <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;
  
  // #define GRID
  // #define ANIMATING
  
  const float lineWidth = .3;
  const float shadeWidth = .35;
  
  #define PI 3.141592653589793
  #define TAU 6.283185307179586
  
  // Helper vector. If you're doing anything that involves regular triangles or hexagons, the
  // 30-60-90 triangle will be involved in some way, which has sides of 1, sqrt(3) and 2.
  const vec2 s = vec2(1, 1.7320508);

  vec2 hash2(vec2 p)
  {
    vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy;
    return o;
  }
  vec3 hash32(vec2 p)
  {
    vec3 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xyz;
    return o;
  }
  
	float ease(float t, float b, float c, float d) {
    
		float s=1.70158;
    float p=0.;
    float a=c;
		if (t==0.) return b;
    if ((t/=d)==1.) return b+c;
    if (p <= 0.) p=d*.3;
		if (a < abs(c)) { 
      a=c;
      s=p/6.;
    } else {
      s = p/TAU * asin(c/a);
    }
		return a*pow(2.,-12.*t) * sin( (t*d-s)*TAU/p ) + c + b;
	}
  
  
  // hex(uv)
  // getHex(uv)
  // -------------
  // These very very helpful functions are by Gary Warne.
  // -------------
  float hex(in vec2 p){

      p = abs(p);

      return max(dot(p, s*.5), p.x); // Hexagon.

  }
  
  vec4 getHex(vec2 p){

      vec4 hC = floor(vec4(p, p - vec2(.5, 1))/s.xyxy) + .5;

      // Centering the coordinates with the hexagon centers above.
      vec4 h = vec4(p - hC.xy*s, p - (hC.zw + .5)*s);

      return dot(h.xy, h.xy)<dot(h.zw, h.zw) ? vec4(h.xy, hC.xy) : vec4(h.zw, hC.zw + vec2(.5, 1));

  }
  
  // const float ringsize = .285;
  // const float angle_increment = TAU/3.;
  // float distance = .57;
  const float ringsize = .285;
  const float angle_increment = TAU/3.;
  float distance = .57;
  
  float shade(float df, float width, float aa, float ringsize) {
    return smoothstep(ringsize-width, ringsize+aa-width, df) - smoothstep(ringsize-aa+width, ringsize+width, df);
  }
  
  float shade(float df, float width, float aa) {
    return shade(df, width, aa, ringsize);
    // return smoothstep(ringsize-width, ringsize+aa-width, df) - smoothstep(ringsize-aa+width, ringsize+width, df);
  }
  
  float shade(float df, float width) {
    return shade(df, width, 0.005);
  }
  
  vec3 truchet(vec4 tile) {
    vec3 hash = hash32(tile.zw);
    float id = floor(hash.x*6.);
    vec4 _tile = mod(tile, 1.);
    float initial_rotation = 0.;
    
    float iso = hex(tile.xy);
    vec3 colour = vec3(1. - iso * 2.);
    colour = vec3(0.);
    
    float t = u_time * .1;
    
    float animation_offset = hash.y * 2. + u_time;
    float animation_time = t + animation_offset;
    float prev_rotation = floor(animation_time / 6.);
    float next_rotation = floor(animation_time / 6.) - 1.;
    
    const float sides = 3.;
    
    float _shade;
    
    
    if(id >= 3.) {
      initial_rotation = TAU / 6.;
    }
    #ifdef ANIMATING
      float easeVal = clamp(mod(animation_time, 6.) - 4.5, 0., 1.5);
      easeVal = ease(easeVal, prev_rotation, prev_rotation-next_rotation, 1.5);
      initial_rotation += TAU / 6. * easeVal;
    #endif
    
    
    for(float i = 1.; i <= sides; i++) {
      float _i = i - .25;
      float increment = initial_rotation + angle_increment * _i;
      float mask = shade(length(tile.xy + vec2(cos(increment), sin(increment)) * distance), .15, 0.01, ringsize);
      colour = clamp(colour, 0., 1.);
      colour += mask;
    }
    
    
    return colour;
    
  }

  void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
    
    float y = u_time*.2;
    float x = sin(u_time*.2);
    float c = cos(u_time * .3);
    float s = sin(u_time * .3);
    
    uv *= mat2(c, -s, s, c);
    
    uv.y += y;
    uv.x += x;
    
    uv *= 5.;
    
    
    vec4 hex_uv = getHex(uv);
    
    vec3 colour = truchet(hex_uv);
    
    #ifdef GRID
      float iso = hex(hex_uv.xy);
      colour = mix(colour, vec3(1., 1., 0.), smoothstep(.49, .5, iso));
    #endif

    gl_FragColor = vec4(colour,1.0);
  }
</script>


<div id="container" touch-action="none"></div>
            
          
!
            
              body {
  margin: 0;
  padding: 0;
}

#container {
  position: fixed;
  touch-action: none;
}
            
          
!
            
              /*
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(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(); });

let then = 0;
function render(delta) {
  
  uniforms.u_time.value = -9 + delta * 0.0005;
  renderer.render( scene, camera );
  
  if(capturing) {
    capturer.capture( renderer.domElement );
  }
}
            
          
!
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.
Loading ..................

Console