Skip to content

Instantly share code, notes, and snippets.

@dghez
Forked from Forenard/TruchetTiling.glsl
Created November 13, 2023 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dghez/cc33ffa252ee287fc2daff098b70c83d to your computer and use it in GitHub Desktop.
Save dghez/cc33ffa252ee287fc2daff098b70c83d to your computer and use it in GitHub Desktop.
TruchetTiling.glsl
#version 300 es
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
out vec4 outColor;
uniform vec2 resolution;
uniform float time;
const float PI=acos(-1.);
#define remap(x,a,b,c,d) ((c)+((d)-(c))*((x)-(a))/((b)-(a)))
#define remapc(x,a,b,c,d) clamp(remap(x,a,b,c,d),min(c,d),max(c,d))
// Hash without Sine
// MIT License...
/* Copyright (c)2014 David Hoskins.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
//----------------------------------------------------------------------------------------
// 1 out, 1 in...
float hash11(float p)
{
p = fract(p * .1031);
p *= p + 33.33;
p *= p + p;
return fract(p);
}
//----------------------------------------------------------------------------------------
// 1 out, 2 in...
float hash12(vec2 p)
{
vec3 p3 = fract(vec3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
//----------------------------------------------------------------------------------------
// 1 out, 3 in...
float hash13(vec3 p3)
{
p3 = fract(p3 * .1031);
p3 += dot(p3, p3.zyx + 31.32);
return fract((p3.x + p3.y) * p3.z);
}
//----------------------------------------------------------------------------------------
// 1 out 4 in...
float hash14(vec4 p4)
{
p4 = fract(p4 * vec4(.1031, .1030, .0973, .1099));
p4 += dot(p4, p4.wzxy+33.33);
return fract((p4.x + p4.y) * (p4.z + p4.w));
}
//----------------------------------------------------------------------------------------
// 2 out, 1 in...
vec2 hash21(float p)
{
vec3 p3 = fract(vec3(p) * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx+p3.yz)*p3.zy);
}
//----------------------------------------------------------------------------------------
/// 2 out, 2 in...
vec2 hash22(vec2 p)
{
vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx+33.33);
return fract((p3.xx+p3.yz)*p3.zy);
}
//----------------------------------------------------------------------------------------
/// 2 out, 3 in...
vec2 hash23(vec3 p3)
{
p3 = fract(p3 * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx+33.33);
return fract((p3.xx+p3.yz)*p3.zy);
}
vec2 rot(vec2 p,float a)
{
float c=cos(a),s=sin(a);
return mat2(c,-s,s,c)*p;
}
float sdSeg(vec2 p, vec2 a, vec2 b ) { vec2 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ); }
const float div=6.;
const float off=.5/div;
vec2 divf(vec2 uv)
{
return fract(uv*div);
}
vec2 divi(vec2 uv)
{
return floor(uv*div);
}
vec2 tci(vec2 uv)
{
return round(uv*2.)*.5;
}
const float w=.1;
const float r=.3;
float q0(vec2 uv)
{
float c=0.;
c+=smoothstep(r,r*.9,length(uv));
return c;
}
float q1(vec2 uv,vec2 p0)
{
float c=0.;
vec2 anc=p0*(w*2.+r);
c+=smoothstep(r,r*.9,length(uv-anc));
float d=sdSeg(uv,anc,p0);
c+=smoothstep(w,w*.9,d);
return c;
}
float q2(vec2 uv,vec2 p0,vec2 p1)
{
float c=0.;
float co=abs(dot(p0,p1));
if(co>.5)
{
float d=sdSeg(uv,p0,p1);
c+=smoothstep(w,w*.9,d);
}
else
{
vec2 anc=p0+p1;
c+=smoothstep(w,w*.9,abs(length(uv-anc)-1.));
}
return c;
}
void main(void) {
vec2 fc=gl_FragCoord.xy,res=resolution.xy;
vec2 uv=fc/res;
vec2 suv=(fc*2.-res)/res.x;
suv.y+=time*.5;
vec2 uvf=divf(suv);
vec2 uvi=divi(suv);
vec2 cen=uvi+.5;
const vec2[4] dir = vec2[4](vec2(1,0),vec2(0,1),vec2(-1,0),vec2(0,-1));
vec2[4] quv;
int co=0;
float th=mix(.2,.8,sin(time)*.5+.5);
for(int i=0;i<4;i++)
{
vec2 id=tci(cen+dir[i]*.5);
if(hash12(id)<th)quv[co++]=dir[i];
}
vec2 sm=smoothstep(.45,.5,abs(uvf-.5));
float c=dot(vec2(1),sm);
vec2 p=uvf*2.-1.;
if(co==0)c+=q0(p);
if(co==1)c+=q1(p,quv[0]);
if(co==2)c+=q2(p,quv[0],quv[1]);
if(co==3)
{
if(hash12(cen+.42)<.5)
c+=q1(p,quv[0]),c+=q2(p,quv[1],quv[2]);
else
c+=q1(p,quv[1]),c+=q2(p,quv[0],quv[2]);
}
if(co==4)
{
if(hash12(cen)<.5)
c+=q2(p,quv[0],quv[1]),c+=q2(p,quv[2],quv[3]);
else
c+=q2(p,quv[0],quv[2]),c+=q2(p,quv[1],quv[3]);
}
outColor = vec4(c,c,c, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment