Truchet 6

FEATURED

In this example below you will see how to do a Truchet 6 with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by Liam Egan, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright Liam Egan ©

Technologies

  • HTML
  • CSS
  • JavaScript
    <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 PI 3.141592653589793
  #define TAU 6.283185307179586
  
  const float lineWidth = .6;
  const float shadeWidth = .65;

  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);
  }
  
  vec4 tile(vec2 uv) {
    uv *= 6.;
    return vec4(floor(uv), fract(uv));
  }
  
  float shade(float df, float width, float aa) {
    return smoothstep(.5-width, .5+aa-width, df) - smoothstep(.5-aa+width, .5+width, df);
  }
  
  float shade(float df, float width) {
    return shade(df, width, 0.01);
  }
  
  vec3 truchet(vec4 tile, float lw, float sw) {
    vec2 hash = hash2(tile.xy);
    float id = floor(hash.x*4.);
    vec3 colour = vec3(0.);
    
    if(id == 0.) {
      // top-bottom, left-right
      float df = tile.w;
      colour += shade(df, lw);
      df = tile.z;
      colour -= shade(df, sw, .1);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lw);
    } else if(id == 1.) {
      // bottom-right, top-left
      float df = length(tile.zw - vec2(1., 0.));
      colour += shade(df, lw);
      df = length(tile.zw - vec2(0., 1.));
      colour -= shade(df, sw, .1);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lw);
    } else if(id == 2.) {
      // left-right, top-bottom
      float df = tile.z;
      colour += shade(df, lw);
      df = tile.w;
      colour -= shade(df, sw, .1);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lw);
    } else {
      // left-bottom, top-right
      float df = length(tile.zw);
      colour += shade(df, lw);
      df = length(tile.zw - 1.);
      colour -= shade(df, sw, .1);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lw);
    }
    
    return clamp(colour, 0., 1.);
  }

  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 *= 2.;
    uv *= mat2(c, -s, s, c);
    
    float lw = lineWidth * clamp(1. - length(uv) * .9, .0, lineWidth);
    float sw = shadeWidth * clamp(1. - length(uv) * .7, .08, shadeWidth);
    
    const float f = .99;
    const int samples = 6;
    const float weight = .65;
    const float decay = .7;
    float illumination_decay = 1.;
    
    uv.y += y;
    uv.x += x;
    vec4 _tile = tile(uv);
    vec3 fragcolour = truchet(_tile, lw, sw);
    fragcolour = vec3(0.);
    // fragcolour *= texture2D(u_noise, uv * 255.).x;
    
    for(int i = 0; i < samples; i++) {
      uv.y -= y;
      uv.x -= x;
      uv *= f;
      uv.y += y;
      uv.x += x;
      _tile = tile(uv);
      
      vec3 sample = truchet(_tile, lw, sw) * texture2D(u_noise, uv * 155.).x;
      sample *= illumination_decay * weight;
      illumination_decay *= decay;
      fragcolour += sample;
      
      // fragcolour = mix(fragcolour, truchet(_tile, lw, sw), texture2D(u_noise, uv * 155.).x );
    }

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


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

















/*Downloaded from https://www.codeseek.co/shubniggurath/truchet-6-mKWZyW */
    body {
  margin: 0;
  padding: 0;
}

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


/*Downloaded from https://www.codeseek.co/shubniggurath/truchet-6-mKWZyW */
    /*
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: 60,
  // 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 = delta * 0.0005;
  renderer.render( scene, camera );
  
  if(capturing) {
    capturer.capture( renderer.domElement );
  }
}

Comments