Skip to content

Instantly share code, notes, and snippets.

@notlion
Last active December 14, 2017 11:09
Show Gist options
  • Save notlion/5215335 to your computer and use it in GitHub Desktop.
Save notlion/5215335 to your computer and use it in GitHub Desktop.
GLSL Snippets
// Largely, these are taken from
// http://pouet.net/topic.php?which=7931&page=1&x=28&y=8 and
// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm and
// organized here for convenience.
// Euler Rotation
void rX(inout vec3 p, float t) {
float c = cos(t), s = sin(t); vec3 q = p;
p.y = c * q.y - s * q.z;
p.z = s * q.y + c * q.z;
}
void rY(inout vec3 p, float t) {
float c = cos(t), s = sin(t); vec3 q = p;
p.x = c * q.x + s * q.z;
p.z = -s * q.x + c * q.z;
}
void rZ(inout vec3 p, float t) {
float c = cos(t), s = sin(t); vec3 q = p;
p.x = c * q.x - s * q.y;
p.y = s * q.x + c * q.y;
}
// Signed Distance Fields
float sdBox(vec3 p, vec3 b) {
vec3 d = abs(p) - b;
return min(max(d.x, max(d.y, d.z)), 0.) + length(max(d, 0.));
}
float sdSphere(vec3 p, float s) {
return length(p) - s;
}
// Distance Field Boolean Ops
float opU(float d1, float d2) { return min(d1, d2); }
float opS(float d1, float d2) { return max(-d1, d2); }
float opI(float d1, float d2) { return max(d1, d2); }
// Gradient Sampling
vec3 grad(vec3 p, float w) {
vec3 x = vec3(w, 0., 0.), y = vec3(0., w, 0.), z = vec3(0., 0., w);
return vec3(
noise(p + x) - noise(p - x),
noise(p + y) - noise(p - y),
noise(p + z) - noise(p - z)
) / (2. * d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment