Skip to content

Instantly share code, notes, and snippets.

@mkeeter
Last active February 1, 2021 02:35
Show Gist options
  • Save mkeeter/667eab7eb4eb59cd9bdf6dbd995b74ad to your computer and use it in GitHub Desktop.
Save mkeeter/667eab7eb4eb59cd9bdf6dbd995b74ad to your computer and use it in GitHub Desktop.
Generated shader for Cornell box with spheres
bool trace(inout uint seed, inout vec3 pos, inout vec3 dir, inout vec4 color)
{
float best_dist = 1e8;
uint best_hit = 0;
float dist;
dist = hit_sphere(pos, dir, vec3(0.0e+00, 6.05000019e+00, 0.0e+00), 5.01999998e+00);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 1;
}
dist = hit_plane(pos, dir, vec3(0.0e+00, 0.0e+00, 1.0e+00), -1.0e+00);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 2;
}
dist = hit_plane(pos, dir, vec3(1.0e+00, 0.0e+00, 0.0e+00), -1.0e+00);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 3;
}
dist = hit_plane(pos, dir, vec3(-1.0e+00, 0.0e+00, 0.0e+00), -1.0e+00);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 4;
}
dist = hit_plane(pos, dir, vec3(0.0e+00, -1.0e+00, 0.0e+00), -1.04999995e+00);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 5;
}
dist = hit_plane(pos, dir, vec3(0.0e+00, 1.0e+00, 0.0e+00), -1.0e+00);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 6;
}
dist = hit_plane(pos, dir, vec3(0.0e+00, 0.0e+00, -1.0e+00), -1.0e+00);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 7;
}
dist = hit_sphere(pos, dir, vec3(-3.00000011e-01, -6.00000023e-01, -2.00000002e-01), 4.00000005e-01);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 8;
}
dist = hit_sphere(pos, dir, vec3(5.0e-01, -6.99999988e-01, 3.00000011e-01), 3.00000011e-01);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 9;
}
dist = hit_sphere(pos, dir, vec3(1.00000001e-01, -8.00000011e-01, 5.0e-01), 2.00000002e-01);
if (dist > SURFACE_EPSILON && dist < best_dist) {
best_dist = dist;
best_hit = 10;
}
// If we missed all objects, terminate immediately with blackness
if (best_hit == 0) {
color = vec4(0);
return true;
}
pos = pos + dir*best_dist;
const uvec4 key_arr[] = {
uvec4(0), // Dummy
uvec4(1, 1, 2, 1),
uvec4(2, 1, 1, 1),
uvec4(2, 2, 1, 2),
uvec4(2, 3, 1, 4),
uvec4(2, 4, 1, 1),
uvec4(2, 5, 1, 1),
uvec4(2, 6, 1, 1),
uvec4(1, 2, 1, 3),
uvec4(1, 3, 3, 1),
uvec4(1, 4, 4, 1),
};
uvec4 key = key_arr[best_hit];
// Calculate normal based on shape type and sub-index
vec3 norm = vec3(0);
switch (key.x) {
case SHAPE_SPHERE: {
// Sphere centers
const vec3 data[] = {
vec3(0), // Dummy
vec3(0.0e+00, 6.05000019e+00, 0.0e+00),
vec3(-3.00000011e-01, -6.00000023e-01, -2.00000002e-01),
vec3(5.0e-01, -6.99999988e-01, 3.00000011e-01),
vec3(1.00000001e-01, -8.00000011e-01, 5.0e-01),
};
norm = norm_sphere(pos, data[key.y]);
break;
}
case SHAPE_INFINITE_PLANE: {
// Plane normals
const vec3 data[] = {
vec3(0), // Dummy
vec3(0.0e+00, 0.0e+00, 1.0e+00),
vec3(1.0e+00, 0.0e+00, 0.0e+00),
vec3(-1.0e+00, 0.0e+00, 0.0e+00),
vec3(0.0e+00, -1.0e+00, 0.0e+00),
vec3(0.0e+00, 1.0e+00, 0.0e+00),
vec3(0.0e+00, 0.0e+00, -1.0e+00),
};
norm = norm_plane(data[key.y]);
break;
}
case SHAPE_FINITE_PLANE: {
// Plane normals
const vec3 data[] = {
vec3(0), // Dummy
};
norm = norm_plane(data[key.y]);
break;
}
}
// Calculate material behavior based on mat type and sub-index
switch (key.z) {
case MAT_DIFFUSE: {
const vec3 data[] = {
vec3(0), // Dummy
vec3(1.0e+00, 1.0e+00, 1.0e+00),
vec3(1.0e+00, 1.00000001e-01, 1.00000001e-01),
vec3(1.00000001e-01, 1.00000001e-01, 1.0e+00),
vec3(1.00000001e-01, 1.0e+00, 1.00000001e-01),
};
return mat_diffuse(seed, color, dir, norm, data[key.w]);
}
case MAT_LIGHT: {
const vec3 data[] = {
vec3(0), // Dummy
vec3(4.0e+00, 4.0e+00, 4.0e+00),
};
return mat_light(color, data[key.w]);
}
case MAT_METAL: {
// R, G, B, fuzz
const vec4 data[] = {
vec4(0), // Dummy
vec4(1.0e+00, 1.0e+00, 5.0e-01, 1.00000001e-01),
};
vec4 m = data[key.w];
return mat_metal(seed, color, dir, norm, m.xyz, m.w);
}
case MAT_GLASS: {
// R, G, B, eta
const vec2 data[] = {
vec2(0), // Dummy
vec2(1.29999995e+00, 1.30000000e-03),
};
vec2 m = data[key.w];
return mat_glass(seed, color, dir, norm, m.x, m.y);
}
case MAT_LASER: {
// R, G, B, focus
const vec4 data[] = {
vec4(0), // Dummy
};
vec4 m = data[key.w];
return mat_laser(color, dir, norm, m.xyz, m.w);
}
}
// Reaching here is an error, so set the color to green and terminate
color = vec4(0, 1, 0, 0);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment