Skip to content

Instantly share code, notes, and snippets.

@MarcusLlewellyn
Created November 21, 2019 16:48
Show Gist options
  • Save MarcusLlewellyn/2726885d4cc8be086a6755f05fbd99ff to your computer and use it in GitHub Desktop.
Save MarcusLlewellyn/2726885d4cc8be086a6755f05fbd99ff to your computer and use it in GitHub Desktop.
Hifi fragment shader that rotates a white square on a black background.
// Reelased into the Public Domain by Michael Bailey.
// Reminder that all GLSL code is run from top to bottom. This is why the main
// function getProceduralFragmentWith Position must always be on the bottom.
// Other functions above that must be created before they can be used.
// This function creates draws a simple rectangle.
// position: 0.0 to 1.0 value for the rectangle's center position.
// scale: x and y values with a 0.0 to 0.1
float rectshape(vec2 position, vec2 scale){
scale = vec2(0.5) - scale * 0.5;
vec2 shaper = vec2(step(scale.x, position.x), step(scale.y, position.y));
shaper *= vec2(step(scale.x, 1.0 - position.x), step(scale.y, 1.0 - position.y));
return shaper.x * shaper.y;
}
// This function rotates the entire fragment by the supplied angle value.
mat2 rotate(float angle){
return mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
}
// This is the main entry point for a fragment shader on High Fidelity
float getProceduralFragmentWithPosition(inout ProceduralFragmentWithPosition proceduralData) {
// Setup the initial coordinate and color values.
vec2 coord = _texCoord0.st;
vec3 color = vec3(0.0);
// Why is the coordinate system being offset twice? Matrix math.
coord -= vec2(0.5);
// Rotate the entire fragment using a global time variable.
coord = rotate(iGlobalTime) * coord;
coord += vec2(0.5);
// Draw a square using the rectshape function.
color += vec3(rectshape(coord, vec2(0.3, 0.3)));
// Send out color data to High Fidelity's data structure.
proceduralData.diffuse = color;
// This is required. Return the emmissive amount, which defaults to 0.0.
return 0.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment