Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330
  2.  
  3. uniform vec2 resolution;
  4. uniform float currentTime;
  5. uniform vec3 camPos;
  6. uniform vec3 camDir;
  7. uniform vec3 camUp;
  8. uniform sampler2D tex;
  9. uniform bool showStepDepth;
  10.  
  11. in vec3 pos;
  12.  
  13. out vec3 color;
  14.  
  15. #define PI 3.1415926535897932384626433832795
  16. #define RENDER_DEPTH 800
  17. #define CLOSE_ENOUGH 0.00001
  18.  
  19. #define BACKGROUND -1
  20. #define BALL 0
  21. #define BASE 1
  22.  
  23. #define GRADIENT(pt, func) vec3( \
  24.     func(vec3(pt.x + 0.0001, pt.y, pt.z)) - func(vec3(pt.x - 0.0001, pt.y, pt.z)), \
  25.     func(vec3(pt.x, pt.y + 0.0001, pt.z)) - func(vec3(pt.x, pt.y - 0.0001, pt.z)), \
  26.     func(vec3(pt.x, pt.y, pt.z + 0.0001)) - func(vec3(pt.x, pt.y, pt.z - 0.0001)))
  27.  
  28. const vec3 LIGHT_POS[] = vec3[](vec3(5, 18, 10));
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////
  31.  
  32. vec3 getBackground(vec3 dir) {
  33.   float u = 0.5 + atan(dir.z, -dir.x) / (2 * PI);
  34.   float v = 0.5 - asin(dir.y) / PI;
  35.   vec4 texColor = texture(tex, vec2(u, v));
  36.   return texColor.rgb;
  37. }
  38.  
  39. vec3 getRayDir() {
  40.   vec3 xAxis = normalize(cross(camDir, camUp));
  41.   return normalize(pos.x * (resolution.x / resolution.y) * xAxis + pos.y * camUp + 5 * camDir);
  42. }
  43.  
  44. ///////////////////////////////////////////////////////////////////////////////
  45.  
  46. float torusXZ(vec3 p, vec2 t) {
  47.   vec2 q = vec2(length(p.xz) - t.x, p.y);
  48.   return length(q) - t.y;
  49. }
  50.  
  51. float torusXY(vec3 p, vec2 t) {
  52.   vec2 q = vec2(length(p.xy) - t.x, p.z);
  53.   return length(q) - t.y;
  54. }
  55.  
  56. float torusYZ(vec3 p, vec2 t) {
  57.   vec2 q = vec2(length(p.yz) - t.x, p.x);
  58.   return length(q) - t.y;
  59. }
  60.  
  61. float plane(vec3 p) {
  62.   return p.y + 1;
  63. }
  64.  
  65. vec3 modulusPosition(vec3 p) {
  66.   return vec3(mod(p.x + 4, 8) - 4, p.y, mod(p.z + 4, 8) - 4);
  67. }
  68.  
  69. float sceneWithoutPlane(vec3 p) {
  70.   vec3 pos = p + vec3(0, 0, 4); // Tweak to fit output to required position
  71.   float torusXZSdf = torusXZ(modulusPosition(pos), vec2(3, 0.5));
  72.   float torusXYSdf = torusXY(modulusPosition(pos + vec3(4, 0, 0)), vec2(3, 0.5));
  73.   float torusYZSdf = torusYZ(modulusPosition(pos + vec3(0, 0, 4)), vec2(3, 0.5));
  74.   return min(min(torusXZSdf, torusXYSdf), torusYZSdf);
  75. }
  76.  
  77. float wholeScene(vec3 p) {
  78.   return min(sceneWithoutPlane(p), plane(p));
  79. }
  80.  
  81. ///////////////////////////////////////////////////////////////////////////////
  82.  
  83. vec3 getNormal(vec3 pt) {
  84.   return normalize(GRADIENT(pt, wholeScene));
  85. }
  86.  
  87. vec3 getProceduralTexture(vec3 pt) {
  88.   float sdf = sceneWithoutPlane(vec3(pt.x, -1, pt.z));
  89.   float modedSdf = mod(sdf, 5);
  90.   if (modedSdf >= 4.75) return vec3(0);
  91.  
  92.   float mixWeight = mod(modedSdf, 1);
  93.   return mix(vec3(0.4, 1, 0.4), vec3(0.4, 0.4, 1), mixWeight);
  94. }
  95.  
  96. vec3 getColor(vec3 pt) {
  97.   return pt.y <= CLOSE_ENOUGH - 1 ? getProceduralTexture(pt) : vec3(1);
  98. }
  99.  
  100. ///////////////////////////////////////////////////////////////////////////////
  101.  
  102. float shadow(vec3 pt, vec3 lightPos) {
  103.   vec3 lightDir = normalize(lightPos - pt);
  104.   float kd = 1;
  105.   int step = 0;
  106.   for (float t = 0.1;
  107.     t < length(lightPos - pt) && step < RENDER_DEPTH && kd > 0.001; ) {
  108.       float d = abs(wholeScene(pt + t * lightDir));
  109.     if (d < 0.001) {
  110.       kd = 0;
  111.     } else {
  112.       kd = min(kd, 16 * d / t);
  113.     }
  114.     t += d;
  115.     step++;
  116.   }
  117.   return kd;
  118. }
  119.  
  120. float shade(vec3 eye, vec3 pt, vec3 n) {
  121.   float val = 0;
  122.  
  123.   val += 0.1;  // Ambient
  124.  
  125.   for (int i = 0; i < LIGHT_POS.length(); i++) {
  126.     float illuminationFromSource = 0;
  127.     vec3 l = normalize(LIGHT_POS[i] - pt);
  128.     float nDotL = dot(n, l);
  129.     illuminationFromSource += max(nDotL, 0); // Diffuse
  130.  
  131.     if (!(nDotL < 0)) {
  132.       vec3 r = reflect(-l, n);
  133.       vec3 v = normalize(eye - pt);
  134.       illuminationFromSource += max(pow(dot(r, v), 256), 0); // Specular
  135.     }
  136.  
  137.     val += illuminationFromSource * shadow(pt, LIGHT_POS[i]);
  138.   }
  139.   return val;
  140. }
  141.  
  142. vec3 illuminate(vec3 camPos, vec3 rayDir, vec3 pt) {
  143.   vec3 c, n;
  144.   n = getNormal(pt);
  145.   c = getColor(pt);
  146.   return shade(camPos, pt, n) * c;
  147. }
  148.  
  149. ///////////////////////////////////////////////////////////////////////////////
  150.  
  151. vec3 raymarch(vec3 camPos, vec3 rayDir) {
  152.   int step = 0;
  153.   float t = 0;
  154.  
  155.   for (float d = 1000; step < RENDER_DEPTH && abs(d) > CLOSE_ENOUGH; t += abs(d)) {
  156.     d = wholeScene(camPos + t * rayDir);
  157.     step++;
  158.   }
  159.  
  160.   if (step == RENDER_DEPTH) {
  161.     return getBackground(rayDir);
  162.   } else if (showStepDepth) {
  163.     return vec3(float(step) / RENDER_DEPTH);
  164.   } else {
  165.     return illuminate(camPos, rayDir, camPos + t * rayDir);
  166.   }
  167. }
  168.  
  169. ///////////////////////////////////////////////////////////////////////////////
  170.  
  171. void main() {
  172.   color = raymarch(camPos, getRayDir());
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement