Truchet 3

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

All of the constants that you need to modify this exist at the top of the HTML view.lineWidth = .09;

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

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 = .12;
  const float shadeWidth = .17;
  const float outlineWidth = .14;
  
  const float initialscale = 2.;
  const float factor = .6;
  const int layers = 3;
  const vec3 shadeBetweenLayers = vec3(.2, .2, .2);
  const float speed_multitplier = .9;
  
  // const float initialscale = 5.5;
  // const float factor = .7;
  // const int layers = 10;
  // const float shadeBetweenLayers = 0.1;
  
  // const float multiplier = .7;
  // const float zoomSpeed = 2.;

  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 *= 8.;
    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);
  }
  
  vec4 truchet(vec4 tile) {
    vec2 hash = hash2(tile.xy);
    float id = floor(hash.x*7.);
    vec3 colour = vec3(0.);
    float opacity = 0.;
    
    if(id == 0.) {
      // top-bottom, left-right
      float df = tile.w;
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      df = tile.z;
      colour -= shade(df, shadeWidth);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
    } else if(id == 1.) {
      // bottom-right, top-left
      float df = length(tile.zw - vec2(1., 0.));
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      df = length(tile.zw - vec2(0., 1.));
      colour -= shade(df, shadeWidth);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
    } else if(id == 2.) {
      // left-right, top-bottom
      float df = tile.z;
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      df = tile.w;
      colour -= shade(df, shadeWidth);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
    } else if(id == 3.) {
      // left-bottom, top-right
      float df = length(tile.zw);
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      df = length(tile.zw - 1.);
      colour -= shade(df, shadeWidth);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      
    } else if(id == 4.) {
      // left-bottom, top-right
      float df;
      
      vec2 hash_top = hash2(tile.xy + vec2(0., -1.));
      float id_top = floor(hash_top.x*7.);
      vec2 hash_bottom = hash2(tile.xy + vec2(0., 1.));
      float id_bottom = floor(hash_bottom.x*7.);
      
      if(id_top != 4.) {
        df = length(tile.zw - vec2(.5, 0.));
        colour += smoothstep(lineWidth, lineWidth-.01, df);
        opacity += smoothstep(outlineWidth, outlineWidth-.01, df);
      }
      
      if(id_bottom != 4.) {
        df = length(tile.zw - vec2(.5, 1.));
        colour += smoothstep(lineWidth, lineWidth-.01, df);
        opacity += smoothstep(outlineWidth, outlineWidth-.01, df);
      }
      
      df = tile.w;
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      
    } else if(id == 5.) {
      // left-bottom, top-right
      float df;
      
      vec2 hash_left = hash2(tile.xy + vec2(-1., 0));
      float id_left = floor(hash_left.x*7.);
      vec2 hash_right = hash2(tile.xy + vec2(1., 0));
      float id_right = floor(hash_right.x*7.);
      
      if(id_left != 5.) {
        df = length(tile.zw - vec2(.0, .5));
        colour += smoothstep(lineWidth, lineWidth-.01, df);
        opacity += smoothstep(outlineWidth, outlineWidth-.01, df);
      }
      
      if(id_right != 5.) {
        df = length(tile.zw - vec2(1., .5));
        colour += smoothstep(lineWidth, lineWidth-.01, df);
        opacity += smoothstep(outlineWidth, outlineWidth-.01, df);
      }
      
      df = tile.z;
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      
      
    } else {
      // roundabout
      float df = tile.w;
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      df = tile.z;
      colour += shade(df, lineWidth);
      opacity += shade(df, outlineWidth);
      colour = clamp(colour, 0., 1.);
      
      df = length(tile.zw - .5) * 2.;
      float cutout = smoothstep(.5, .51, 1.-df);
      colour -= cutout;
      opacity -= cutout * 4.;
      opacity = clamp(opacity, 0., 1.);
      colour = clamp(colour, 0., 1.);
      colour += shade(df, lineWidth*2.);
      opacity += shade(df, outlineWidth*2.);
    }
    
    return clamp(vec4(colour, opacity), 0., 1.);
  }
  // vec3 renderLayer(int layer, int layers, vec2 uv, inout float opacity) {
  // }

  void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
    
    vec3 fragcolour = vec3(0.4);
    
    float t = sin(u_time * .2);
    float c = cos(t);
    float s = sin(t);
    
    uv *= initialscale;
    uv *= mat2(c, -s, s, c);
    
    for(int i = 0; i < layers; i ++) {
      uv.y += u_time*.2*speed_multitplier;
      uv.x += 100. + u_time*.05*speed_multitplier;
      vec4 _tile = tile(uv);
      vec4 layer = truchet(_tile);
      fragcolour -= shadeBetweenLayers;
      fragcolour = mix(layer.rgb, fragcolour, 1. - layer.a);
      
      if(i < layers - 1) {
        vec2 _uv = uv * factor * .99;
        _uv.y += u_time*.25*speed_multitplier;
        _uv.x += 100. + u_time*.1*speed_multitplier;

        _tile = tile(_uv);
        vec4 _layer = truchet(_tile);
        fragcolour -= _layer.a * .25 * layer.a;
      }
      
      uv *= factor;
      
    }

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


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

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

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


/*Downloaded from https://www.codeseek.co/shubniggurath/truchet-3-PabXvb */
    /*
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: 6,
  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