Assignment 3

For this assignment I worked on applying last week’s course content to my sketch. I added a grid as well as used noise to alter the trapezoids sides. I used the clamp function to limit the distortion of the noise and sine waves to distort the various lines in the grid.

download-4

vec2 createGrid(in vec2 st, in vec2 grid, out vec2 indices){
 st*= grid;
 indices = floor(st);
 st= fract(st);
 return(st);
}

// Funtion to check the grid and give each cell a different pattern
bool gridCheck(in float gridName, in float check, in float div){
 bool res;
 if(mod(gridName, div)==check){
 res= true;
 }else{
 res=false;
 }
 return(res);
}

//Noise Function referenced from The Book of Shaders
vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
float snoise(vec2 v){
 const vec4 C = vec4(0.211324865405187, 0.366025403784439,
 -0.577350269189626, 0.024390243902439);
 vec2 i = floor(v + dot(v, C.yy) );
 vec2 x0 = v - i + dot(i, C.xx);
 vec2 i1;
 i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
 vec4 x12 = x0.xyxy + C.xxzz;
 x12.xy -= i1;
 i = mod(i, 289.0);
 vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
 + i.x + vec3(0.0, i1.x, 1.0 ));
 vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
 dot(x12.zw,x12.zw)), 0.0);
 m = m*m ;
 m = m*m ;
 vec3 x = 2.0 * fract(p * C.www) - 1.0;
 vec3 h = abs(x) - 0.5;
 vec3 ox = floor(x + 0.5);
 vec3 a0 = x - ox;
 m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
 vec3 g;
 g.x = a0.x * x0.x + h.x * x0.y;
 g.yz = a0.yz * x12.xz + h.yz * x12.yw;
 return 130.0 * dot(m, g);
}

// Function fot trapezoid
float rect(vec2 st, vec2 size, vec2 loc,float skew, float stretch, vec2 wh){
 
 float result = 1.0;
 
 vec2 border = (1.0-size)/2.000;

 result = step( border.x, mix (st.x,st.y,skew)+-loc.x);
 result *= step( border.x, wh.x-mix (st.x,st.y,(skew))+loc.x);
 result *= step( border.y, mix (st.x,st.y,stretch)+-loc.y);
 result *= step( border.y, wh.y-mix (st.x,st.y,stretch)+loc.y);
 
 return result;
}

// Main code
void main() {

 //Variables
 vec2 st = gl_FragCoord.xy/u_resolution.xy;
 st.x *= u_resolution.x/u_resolution.y;
 
 float g = clamp((snoise( vec2(1.5, st.y + u_time * 0.904)) ),0.1,0.3);
 float g1 = snoise( vec2(0., st.y + u_time * 0.904));
 float g2 = clamp((snoise( vec2(0.5, st.y + u_time * 0.512)) ),0.1,0.9);
 
 vec2 indices;
 float grid= 10.;
 
createGrid(st, vec2(2), indices);
 float row= indices.x;
 float col=indices.y;
 vec3 colour = vec3(0.610,0.700,0.642);
 

//Upper Right grid
if( gridCheck(row,1.,2.) && gridCheck(col,1.,2.) ){
 
 st= createGrid(st, vec2(10.), indices);
 
 } 
//Left cube face
 //Variables
 vec2 wH1= vec2(0.9,1.22); // Width and height varible to make the layers uniform
 vec2 loc1= vec2 (-0.132,-0.204); //Location varible to make the layers uniform
 
 //Colour mixing to get the right colour
 
 vec3 colourA1 = vec3(0.537,0.251,0.725); 
 colourA1.r = rect(st, vec2 (0.472),loc1,-g,1.434, vec2(wH1));
 colourA1.g = rect(st, vec2 (0.400),loc1,g,1.434, vec2(wH1));
 
 vec3 colourA2 = vec3(0.831,0.885,0.812);
 
 //Bottom Left Grid
 if( gridCheck(row,0.,4.) && gridCheck(col,0.,2.) ){
 
 st= createGrid(st+g2, vec2(10.), indices);
 
 }
 
 vec3 colourA = mix(colourA1,colourA2,0.460);
 
//Right cube face
 //Variables
 vec2 wH2= vec2(0.98,0.97);
 vec2 loc2= vec2 (0.172,-0.052);
 float z= sin((st.y+u_time)*(6.28*0.276));
 
 //Colour mixing to get the right colour
 
 vec3 colourB1= vec3(0.128,0.485,0.580);
 colourB1.r = rect(st, vec2 (0.448),vec2 (0.228,-0.044),-g,0.858, vec2(0.95,0.97));
 colourB1.g = rect(st, vec2 (0.400),loc2,g,0.858, vec2(wH2));
 
 vec3 colourB2= vec3(0.795,0.192,0.503);
 
 vec3 colourB = mix(colourB1,colourB2,0.156);
 
//Upper right sub grid
 if( gridCheck(row,1.,2.) && gridCheck(col,1.,2.) ){
 
 st= createGrid(st+z, vec2(5.), indices);
 
 } 
//Upper Right Grid
 if( gridCheck(row,0.,2.) && gridCheck(col,1.,2.) ){
 
 st= createGrid(st+z, vec2(5.), indices);
 
 } 
//Upper Left grid
 if( gridCheck(row,1.,2.) && gridCheck(col,0.,2.) ){
 
 st= createGrid(st+z, vec2(5.), indices);
 
 } 
 
// mixing the faces and animation
float amt= sin((st.x+u_time)*(6.28*0.276)+-0.028)*cos((st.x+u_time)*(6.28*0.276)+0.5);
colour= mix(colourA, colourB,amt);

 

Assignment 2

For this assignment I wanted to try to make 3D figures using what we learned. I edited the code so that I can have diagonal lines, in doing that I made a trapezoid. IĀ used 2 of the shapes to create each of the faces and overlayed another effect so that there is a red outline. I used a sin function to animate it and try to make it look like there is light circling the object but it did not seem to work.

download-6download-7

// Function for trapezoid
float rect(vec2 st, vec2 size, vec2 loc,float skew, float stretch, vec2 wh){
 
 float result = 1.0;
 
 vec2 border = (1.0-size)/2.000;

result = step( border.x, mix (st.x,st.y,skew)+-loc.x);
 result *= step( border.x, wh.x-mix (st.x,st.y,(skew))+loc.x);
 result *= step( border.y, mix (st.x,st.y,stretch)+-loc.y);
 result *= step( border.y, wh.y-mix (st.x,st.y,stretch)+loc.y);
 
 return result;
}
void main() {
 
 vec2 st = gl_FragCoord.xy/u_resolution.xy;
 st.x *= u_resolution.x/u_resolution.y;
 
//Left cube face
 //Variables
 vec2 wH1= vec2(0.9,1.22); // Width and height varible to make the layers uniform
 vec2 loc1= vec2 (-0.132,-0.204); //Location varible to make the layers uniform
 
 //Colour mixing to get the right colour
 
 vec3 colourA1 = vec3(0.554,1.000,0.673); 
 colourA1.r = rect(st, vec2 (0.472),loc1,0.,1.434, vec2(wH1));
 colourA1.g = rect(st, vec2 (0.400),loc1,0.,1.434, vec2(wH1));
 
 vec3 colourA2 = vec3(0.554,1.000,0.673);
 colourA2.r = rect(st, vec2 (0.400),loc1,0.,1.434, vec2(wH1));
 
 vec3 colourA = mix(colourA1,colourA2,0.460);
 
//Left cube face
 //Variables
 vec2 wH2= vec2(0.98,0.97);
 vec2 loc2= vec2 (0.172,-0.052);
 
 //Colour mixing to get the right colour
 
 vec3 colourB1= vec3(0.767,0.785,0.595);
 colourB1.r = rect(st, vec2 (0.448),vec2 (0.228,-0.044),0.,0.858, vec2(0.95,0.97));
 colourB1.g = rect(st, vec2 (0.400),loc2,0.,0.858, vec2(wH2));
 
 vec3 colourB2= vec3(0.990,0.985,0.995);
 colourB2.r = rect(st, vec2 (0.400),loc2,0.,0.858, vec2(wH2));
 
 vec3 colourB = mix(colourB1,colourB2,-0.172);
 
// mixing the faces and animation
float amt= sin((st.x+u_time)*(6.28*0.276)+0.5)*cos((st.x+u_time)*(6.28*0.276)+0.5);
vec3 colour= mix(colourA, colourB,amt);
 result;
}

Assignment 1

For the first Assignment I mostly reviewed and tried out what was shown in the coding tutorial.

assignment1bcodecapture3

I created a gradient that combines 4 colours and did that through multiple uses of the mix function.

download-1cadecapture2

After understanding the basics I got a lot more experimental, especially in animating the shader. I layered various functions on top of each other. The resulting animation can’t really be seen here but I took a screenshot of it.

download-2codecapture