Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Raymarching Terrain Basics
  3. */
  4. #define MARCH_STEPS 62
  5.  
  6. #define MIN_DIST .0
  7. #define MAX_DIST 4.
  8. #define EPSILON .001
  9. #define NUM_OCTAVES 8
  10.  
  11. // toggle for cave
  12. #define CAVE 1
  13.  
  14.  
  15. /*
  16.     Random, Noise and FBM
  17. */
  18. float random(in vec2 st) {
  19.     return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.1);
  20. }
  21.  
  22. float noise(vec2 p) {
  23.     vec2 i = ceil(p);
  24.     vec2 f = fract(p);
  25.     vec2 u = f * f * (3. - 2. * f);
  26.     float a = random(i);
  27.     float b = random(i + vec2(1., 0.));
  28.     float c = random(i + vec2(0., 1.));
  29.     float d = random(i + vec2(1., 1.));
  30.     return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
  31. }
  32.  
  33. float fbm(in vec2 p) {
  34.     float s = 0.0;
  35.     float m = 0.0;
  36.     float a = 0.5; 
  37.     for(int i = 0; i < NUM_OCTAVES; i++) {
  38.         s += a * noise(p);
  39.         m += a;
  40.         a *= 0.5;
  41.         p *= 2.0;
  42.     }
  43.     return s / m;
  44. }
  45.  
  46. /*
  47.     Raymarch functions
  48. */
  49.  
  50. // get position along dist
  51. vec3 get_position(vec3 o, vec3 d, float t) {
  52.     return o + d * t;
  53. }
  54.  
  55. // terrain map function
  56. float map(vec3 p) {  
  57.    
  58.     // y-offset based on fbm
  59.     float yd = .2 + (
  60.         (.8 * fbm(p.xz)) +
  61.         (.1 * fbm(p.xz * 10.))
  62.     );
  63.     float y = p.y + yd;
  64.    
  65.    
  66.  
  67.     return dot(
  68.         vec3(p.x, y, p.z),
  69.         vec3(0., 1., 0.)
  70.     );
  71. #endif
  72.    
  73. }
  74.  
  75. // find the closest point along ray
  76. float trace_ray(vec3 origin, vec3 direction) {
  77.     float depth = MIN_DIST;
  78.     for (int i = 0; i < MARCH_STEPS; i++) {
  79.         float dist = map(get_position(origin, direction, depth));
  80.         depth += dist;
  81.     }
  82.     return depth;
  83. }
  84.  
  85. // projected ray
  86. vec3 ray_direction(vec2 uv, float fov) {
  87.     float z = 2. / tan(radians(fov) / 2.0);
  88.     return normalize(vec3(uv, z));
  89. }
  90.  
  91. /*
  92.     Main shader
  93. */
  94. void mainImage(out vec4 O, in vec2 I) {
  95.     vec2 R  = iResolution.xy;
  96.     vec2 uv = (2. * I  - R) / R.y;
  97.     //uv.y *= -1.;
  98.     vec3 eye = vec3(0., -.05, iTime);
  99.     vec3 dir = ray_direction(uv, 90.);
  100.     //float dist = trace_ray(eye, dir);
  101.     float dist = trace_ray(eye, dir);
  102.     O = vec4(vec3(1. / dist), 1.);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement