Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "Common/ShaderLib/GLSLCompat.glsllib"
  2.  
  3. #define PI                      3.14159265
  4.  
  5. #define MTL_BACKGROUND          -1.0
  6. #define MTL_GROUND              1.0
  7. #define MTL_AX                  2.0
  8. #define MTL_AY                  3.0
  9. #define MTL_AZ                  4.0
  10. #define MTL_OBJ1                5.0
  11. #define MTL_OBJ2                6.0
  12. #define MTL_GREY                7.0
  13.  
  14. #define NORMAL_EPS              0.01
  15.  
  16. #define NEAR_CLIP_PLANE         0.01
  17. #define FAR_CLIP_PLANE          1000.0
  18. #define MAX_RAYCAST_STEPS       200
  19. #define STEP_DAMPING            0.90
  20. #define DIST_EPSILON            0.002
  21.  
  22. #define GLOBAL_LIGHT_COLOR      vec3(0.8,1.0,0.9)
  23. #define SPEC_COLOR              vec3(0.8, 0.90, 0.60)
  24. #define BACKGROUND_COLOR        vec3(0.3, 0.342, 0.5)
  25.  
  26. #define CAM_DIST                5.0
  27. #define CAM_H                   2.0
  28. #define CAM_FOV_FACTOR          2.5
  29. #define LOOK_AT_H               0.0
  30.  
  31. #define LOOK_AT                 vec3(0.0, LOOK_AT_H, 0.0)
  32.  
  33. #define Point3 vec3
  34.  
  35. varying vec4 vertColor;
  36. varying vec3 normal;
  37.  
  38. varying float PointSize;
  39. varying vec4 Position;
  40. varying float voxelSize;
  41. varying vec2 resolution;
  42. varying vec2 center;
  43. varying vec3 camPos;
  44. varying vec3 camDir;
  45.  
  46. varying vec3 objectViewDir;
  47. varying vec3 objectPos;
  48. varying Point3 boxCenter;
  49.  
  50. /**
  51.  * Signed distance function for a cube centered at the origin
  52.  * with width = height = length = 2.0
  53.  */
  54. float cubeSDF(vec3 p) {
  55.     // If d.x < 0, then -1 < p.x < 1, and same logic applies to p.y, p.z
  56.     // So if all components of d are negative, then p is inside the unit cube
  57.     vec3 d = abs(p) - vec3(0.5);
  58.  
  59.     // Assuming p is inside the cube, how far is it from the surface?
  60.     // Result will be negative or zero.
  61.     float insideDistance = min(max(d.x, max(d.y, d.z)), 0.0);
  62.  
  63.     // Assuming p is outside the cube, how far is it from the surface?
  64.     // Result will be positive or zero.
  65.     float outsideDistance = length(max(d, 0.0));
  66.  
  67.     return insideDistance + outsideDistance;
  68. }
  69. float sphere(vec3 p, float r) { return length(p) - r; }
  70. vec2 planet(vec3 p){
  71.  
  72.     vec2 base = vec2(sphere(p,voxelSize), MTL_GREY);
  73.  
  74.     return base;
  75. }
  76.  
  77.  
  78. vec2 map(vec3 p) {
  79.     //vec2 res = grid(p);
  80.     //vec2 cs = coordSys(p);
  81.     //add(res, cs);
  82.     vec2 res = planet(p);
  83.     // res.x = opU(res.x, vec2(distf(p), MTL_GREY).x);
  84.  
  85.  
  86.     return res;
  87. }
  88.  
  89. vec2 rayMarch(in vec3 ro, in vec3 rd) {
  90.     float t = NEAR_CLIP_PLANE;
  91.     float m = MTL_BACKGROUND;
  92.     for (int i=0; i < MAX_RAYCAST_STEPS; i++) {
  93.         vec2 res = map(ro + rd*t);
  94.         if (res.x < DIST_EPSILON || t>FAR_CLIP_PLANE) break;
  95.         t += res.x*STEP_DAMPING;
  96.         m = res.y;
  97.     }
  98.  
  99.     if (t > FAR_CLIP_PLANE) m = MTL_BACKGROUND;
  100.     return vec2(t, m);
  101. }
  102.  
  103. vec3 calcNormal(in vec3 p)
  104. {
  105.     vec2 d = vec2(NORMAL_EPS, 0.0);
  106.     return normalize(vec3(
  107.         map(p + d.xyy).x - map(p - d.xyy).x,
  108.         map(p + d.yxy).x - map(p - d.yxy).x,
  109.         map(p + d.yyx).x - map(p - d.yyx).x));
  110. }
  111.  
  112. vec3 applyFog(vec3 col, float dist) {
  113.     return mix(col, BACKGROUND_COLOR, 1.0 - exp(-0.0015*dist*dist));
  114. }
  115.  
  116.  
  117. vec3 getMaterialColor(float matID) {
  118.     vec3 col = BACKGROUND_COLOR;
  119.     if (matID <= MTL_GROUND) col = vec3(0.3, 0.3, 0.5);
  120.     else if (matID <= MTL_AX) col = vec3(1.0, 0.0, 0.0);
  121.     else if (matID <= MTL_AY) col = vec3(0.0, 1.0, 0.0);
  122.     else if (matID <= MTL_AZ) col = vec3(0.0, 0.0, 1.0);
  123.     else if (matID <= MTL_OBJ1) col = vec3(0.8, 0.8, 1.8);
  124.     else if (matID <= MTL_OBJ2) col = vec3(1.4, 1.3, 0.3);
  125.     else if (matID <= MTL_GREY) col = vec3(0.8, 0.8, 0.8);
  126.     else col = vec3(0.7, 0.7, 1.8);
  127.     return col;
  128. }
  129.  
  130.  
  131. vec3 render(in vec3 ro, in vec3 rd) {
  132.     vec2 res = rayMarch(ro, rd);
  133.     float t = res.x;
  134.     float mtlID = res.y;
  135.     vec3  lig = -rd;
  136.     vec3 pos = ro + t*rd;
  137.     vec3 nor = calcNormal(pos);
  138.     vec3 mtlColor = getMaterialColor(mtlID);
  139.  
  140.     float ambient = 0.05;
  141.     float diffuse = clamp(dot(nor, lig), 0.0, 1.0);
  142.  
  143.     vec3 col = mtlColor*(ambient + GLOBAL_LIGHT_COLOR*diffuse);
  144.     col = applyFog(col, t);
  145.  
  146.     return vec3(clamp(col, 0.0, 1.0));
  147. }
  148. vec3 getRayDir(vec3 camPos, vec3 viewDir, vec2 pixelPos) {
  149.     vec3 camRight = normalize(cross(viewDir, vec3(0.0, 1.0, 0.0)));
  150.     vec3 camUp = normalize(cross(camRight, viewDir));
  151.     return normalize(pixelPos.x*(camRight*-1.) + pixelPos.y*camUp + CAM_FOV_FACTOR*viewDir);
  152. }
  153.  
  154.  
  155. void main() {
  156.  
  157.     vec3 camTarget = (normalize(camDir)*1.0f);
  158.     camTarget = camTarget+camPos;
  159.  
  160.     vec2 q = gl_FragCoord.xy/resolution.xy;
  161.     vec2 p = -1.0+2.0*q;
  162.     p.x *= resolution.x/resolution.y;
  163.     vec3 rayDirVec = normalize(objectViewDir);
  164.     vec3 rayStartPos = objectPos;
  165.  
  166.  
  167.     vec3 rayDir = getRayDir(camPos,normalize(camTarget - camPos), p);
  168.     vec3 color = render(camPos, rayDir);
  169.     if(color == BACKGROUND_COLOR){discard;}
  170.     gl_FragColor = vec4(color, 1.0);
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement