Created
March 21, 2017 22:05
-
-
Save Chlumsky/f930166096875442717299c211455957 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math_constants> | |
#include <affine_transform> | |
#include <lighting> | |
param int sides = 3 : logrange(3, 12); | |
param float zRotation : range(-PI, PI); | |
param float xRotation : range(-PI, PI); | |
glsl struct FragmentData { | |
vec3 normal; | |
vec4 color; | |
}; | |
glsl vec3 crystalCoord(int index) { | |
int triangle = index/3; | |
index %= 3; | |
vec3 coord; | |
if (index == 0) { | |
coord = vec3(0.0, 0.0, 1.0); | |
} | |
if (index == 1) { | |
float a = TAU*float(triangle)/float(sides); | |
coord = 0.5*vec3(sin(a), cos(a), 0.0); | |
} | |
if (index == 2) { | |
float a = TAU*float(triangle - 1)/float(sides); | |
coord = 0.5*vec3(sin(a), cos(a), 0.0); | |
} | |
if (triangle >= sides) | |
coord.xz *= -1.0; | |
return coord; | |
} | |
glsl vec3 crystalNormal(int index) { | |
int triangle = index/3; | |
vec3 a = crystalCoord(3*triangle + 0); | |
vec3 b = crystalCoord(3*triangle + 1); | |
vec3 c = crystalCoord(3*triangle + 2); | |
return normalize(cross(b-a, c-a)); | |
} | |
glsl vec4 crystalVertex(out FragmentData fd, int index) { | |
vec3 coord = crystalCoord(index); | |
coord = rotateZ(coord, zRotation); | |
coord = rotateX(coord, xRotation); | |
coord.z -= 2.5; | |
fd.normal = crystalNormal(index); | |
fd.color = vec4(1.0, 1.0, 1.0, 1.0); | |
return projectPerspectiveHFOV(coord, 0.5*PI, 1.0/256.0, 256.0); | |
} | |
glsl vec4 crystalFragment(in FragmentData fd) { | |
vec3 lightDirection = normalize(vec3(1.0, 2.0, 3.0)); | |
float lightIntensity = 0.5 + 0.5*diffuseLight(lightDirection, fd.normal); | |
return vec4(lightIntensity * fd.color.rgb, 1.0); | |
} | |
model Crystal : | |
fragment_data(FragmentData), | |
vertex(crystalVertex, triangles, 2*3*sides), | |
fragment(crystalFragment), | |
wireframe(false), cull(backface), | |
multisample(4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment