top of page
  • Writer's pictureQI jiajing

GLSL Snippets - Touchdesigner

Updated: Feb 14, 2021



// Example Pixel Shader  02/14/2020

// uniform float exampleUniform;

out vec4 fragColor;
uniform vec2 uRes;
uniform float osc;
uniform float speed;
vec3 red = vec3(235,12,38);
float PI = 3.1415926;
vec3 pink = vec3 (1.,.1,.35);


float draw_heart (vec2 _st, float CX, float CY, float speed){
	_st.x *= .75;
	_st.y -=sqrt(abs(_st.x))*.4 - .05;
	float d = length(_st - vec2 (CX, CY)) - speed *.001; 
	return d;
}


float draw_square (float CX, float CY, float Height,float Width,float blr, vec2 _st){
	vec2 Center = vec2(CX,CY);
	float disY =smoothstep(CY - Height/2.0-blr,CY - Height/2.0+blr,_st.t) - smoothstep(CY + Height/2.0 -blr,CY + Height/2.0 + blr,_st.t);
	float disX =smoothstep(CX - Width/2.0 - blr,CX - Width/2.0 + blr,_st.s) - smoothstep(CX + Width/2.0 - blr, CX + Width/2.0 + blr,_st.s);
	float Square = disY * disX;
	return Square;
}


vec2 rotate2d(vec2 _st, float _angle){
	_st = _st - .5;
    _st =  mat2(cos(_angle),-sin(_angle),
                sin(_angle),cos(_angle)) * _st;
    //_st -= .5;
    return _st;
}


vec2 canvas (vec2 _st, float amplifier){
	_st = _st * amplifier; 
// offset and animate rows
	_st.x += step(1., mod(_st.y,2.))*.5 *osc; 
	_st.y -= step(0., mod(_st.y,2.))*.5 *osc; 
// change aspect ratio
	_st /= vec2(1.,1.);
	_st = fract(_st);
	return _st;
}


void main()
{;	
	vec2 st = vec2(vUV);

	float myheart = fract(draw_heart(st-.5, .0,.0,speed)*4);

	vec2 aspect = uRes/uRes.x;
	st = st * aspect;
	st = canvas (st,10.);
	st = rotate2d (st, -PI /4.);

	
	float square1 = draw_square(-.2,.3,.5,.5,.007,st);
	float square2 = draw_square(-.2,.4,.1,.1,.007,st);

	float square = square1 - square2;
	


	vec4 outsquare = vec4(.0-vec3 (square)+pink,.0);
	vec4 outheart = vec4(vec3(myheart,.0,.05)+(1.-vec3 (myheart)),1.);

	vec4 compose = outsquare + outheart;

	vec4 color = compose;
	fragColor = TDOutputSwizzle(color);
}



//updated 20210115 by Jiajing Qi
uniform float osc;  //Add osc to glsl Vectors in Touchdesigner

out vec4 fragColor;


float draw_Circle(float CX,float CY,float Radius, float blr){ 
	vec2 Center = vec2(CX,CY); 
	float Circle = smoothstep (Radius/2 - blr, Radius/2 + blr, length(vUV.st - vec2(CX,CY))); 
	return Circle; 
}
void main()
{
	float r = abs(sin(vUV.y*9.+ osc));
	float myCircle = draw_Circle(.5,.5,r,.9);
	vec4 color = vec4(vec3(myCircle),1.);
	fragColor = TDOutputSwizzle(color);
}

109 views0 comments

Recent Posts

See All
bottom of page