Create a gist now

Instantly share code, notes, and snippets.

@paniq /shader.glsl Secret
Created Aug 29, 2015

// VERTEX SHADER ------------------------------------------------------------
#version 150
out int face_index;
void main () {
face_index = gl_VertexID;
}
// GEOMETRY SHADER ------------------------------------------------------------
#version 150
#extension GL_ARB_shader_bit_encoding : enable
layout(points) in;
layout(max_vertices = 6, triangle_strip) out;
vec3 hue2rgb(float hue) {
return clamp(
abs(mod(hue * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0,
0.0, 1.0);
}
vec3 normhue(float n) {
return hue2rgb(0.66667*(1.0 - clamp(n,0.0,1.0)));
}
vec3 hsl2rgb(vec3 c) {
vec3 rgb = hue2rgb(c.x);
return c.z + c.y * (rgb - 0.5) * (1.0 - abs(2.0 * c.z - 1.0));
}
vec3 hsv2rgb(vec3 c) {
vec3 rgb = hue2rgb(c.x);
return c.z * mix(vec3(1.0), rgb, c.y);
}
vec3 hsv2rgbc(vec3 c) {
vec3 rgb = hue2rgb(c.x);
// rgb = smoothstep(vec3(0.0),vec3(1.0),rgb);
rgb = rgb * rgb * (3.0 - 2.0*rgb);
return c.z * mix(vec3(1.0), rgb, c.y);
}
// ratio: 3 = neon, 4 = refracted, 5+ = approximate white
vec3 physhue2rgb(float hue, float ratio) {
return smoothstep(
vec3(0.0),vec3(1.0),
abs(mod(hue + vec3(0.0,1.0,2.0)*(1.0/ratio),1.0)*2.0-1.0));
}
in int[1] face_index;
uint uread (usamplerBuffer smp, int index) {
return texelFetch(smp, index) . r;
}
uniform usamplerBuffer face_buffer;
out vec3 col;
ivec3 iread3 (usamplerBuffer smp, int index) {
return ivec3(uread(smp, index), uread(smp, index + 1), uread(smp, index + 2));
}
out vec3 vxid;
layout(std140) uniform _WorldParams {
mat3 camera_aim;
vec3 camera_origin;
vec3 mtx_proj;
float time;
} WorldParams;
vec4 transform_vertex (vec3 p) {
p . xy = (p . xy - WorldParams . camera_origin . xy) * WorldParams . mtx_proj . xy;
p . xy;
return vec4(p, 1);
}
void emit_tri (vec3 p0, vec3 p1, vec3 p2) {
vxid = vec3(1, 0, 0);
gl_Position = transform_vertex(p0);
EmitVertex();
vxid = vec3(0, 1, 0);
gl_Position = transform_vertex(p1);
EmitVertex();
vxid = vec3(0, 0, 1);
gl_Position = transform_vertex(p2);
EmitVertex();
EndPrimitive();
}
float fread (usamplerBuffer smp, int index) {
return uintBitsToFloat(uread(smp, index));
}
vec2 fread2 (usamplerBuffer smp, int index) {
return vec2(fread(smp, index), fread(smp, index + 1));
}
uniform usamplerBuffer vertex_buffer;
vec3 read_vertex (int index) {
return vec3(fread2(vertex_buffer, index * 5), 0);
}
void main () {
int face_offset;
face_offset = face_index[0] * 7;
if (uread(face_buffer, face_offset + 6) == uint(0)) {
col = hsl2rgb(vec3(float(face_index[0]) * 0.166670, 0.900000, 0.900000));
ivec3 face_verts;
face_verts = iread3(face_buffer, face_offset);
emit_tri(read_vertex(face_verts[0]), read_vertex(face_verts[1]), read_vertex(face_verts[2]));
}
}
// FRAGMENT SHADER ------------------------------------------------------------
#version 150
in vec3 vxid;
float min3 (vec3 v) {
return min(min(v . x, v . y), v . z);
}
out vec4 color;
float max3 (vec3 v) {
return max(max(v . x, v . y), v . z);
}
void main () {
vec3 d;
d = 1.500000 * fwidth(vxid);
vec3 a3;
a3 = smoothstep(vec3(0), d, vxid);
float ef;
ef = min3(a3);
vec3 corners;
corners = smoothstep(float(20) * d, vec3(0), float(1) - vxid);
vec3 edges;
edges = smoothstep(float(5) * d, vec3(0), vxid);
color = ef * vec4(mix(mix(vec3(1), edges, max3(edges)), corners, max3(corners)), 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment