Advertisement
Guest User

Untitled

a guest
Dec 6th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // raymarch2.bonzo
  2.  
  3. #define tt iTime
  4. #define res iResolution
  5.  
  6. const int MAX_MARCHING_STEPS = 255;
  7. const float MIN_DIST = 0.0;
  8. const float MAX_DIST = 100.0;
  9. const float EPSILON = 0.0001;
  10.  
  11. float stillNoise (vec2 uv) {
  12.     return sin(dot(uv.xy,vec2(130.9898,500.233)));}
  13.  
  14. /* Signed distance function for a sphere centered at "position" */
  15. float sphereSDF(vec3 samplePoint, float radius, vec3 position) {
  16.     samplePoint = vec3(samplePoint.x+position.x, samplePoint.y+position.y, samplePoint.z+position.z);
  17.     return length(samplePoint) - radius * stillNoise(samplePoint.xy);
  18.   }
  19.  
  20. /*
  21.  * Signed distance function describing the scene.
  22.  *
  23.  * Absolute value of the return value indicates the distance to the surface.
  24.  * Sign indicates whether the point is inside or outside the surface,
  25.  * negative indicating inside.
  26.  */
  27. float sceneSDF(vec3 samplePoint) {
  28.     return sphereSDF(samplePoint, .5, vec3(sin(tt),0.,0.));
  29. }
  30.  
  31. /*
  32.  * Return the shortest distance from the camera position to the scene surface along
  33.  * the marching direction. If no part of the surface is found between start and end,
  34.  * return end.
  35.  *
  36.  * cam: the camera position, acting as the origin of the ray
  37.  * marchingDirection: the normalized direction to march in
  38.  * minDepth: the starting distance away from the camera
  39.  * maxDepth: the max distance away from the camera to march before giving up
  40.  */
  41. float shortestDistanceToSurface(vec3 cam, vec3 marchingDirection, float minDepth, float maxDepth) {
  42.     float depth = minDepth;
  43.     for (int i = 0; i < MAX_MARCHING_STEPS; i++) {
  44.         float dist = sceneSDF(cam + depth * marchingDirection);
  45.         if (dist < EPSILON) {
  46.             return depth;
  47.         }
  48.         depth += dist;
  49.         if (depth >= maxDepth) {
  50.             return maxDepth;
  51.         }
  52.     }
  53.     return maxDepth;
  54. }
  55.            
  56.  
  57. /*
  58.  * Return the normalized direction to march in from the cam point for a single pixel.
  59.  *
  60.  * fieldOfView: vertical field of view in degrees
  61.  * size: resolution of the output image
  62.  * fragCoord: the x,y coordinate of the pixel in the output image
  63.  */
  64. vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) {
  65.     vec2 xy = fragCoord - size / 2.0;
  66.     float z = size.y / tan(radians(fieldOfView) / 2.0);
  67.     return normalize(vec3(xy, -z));
  68. }
  69.  
  70.  
  71. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  72. {
  73.     vec3 dir = rayDirection(45.0, res.xy, fragCoord);
  74.     vec3 cam = vec3(0., 0., 5.);
  75.     float dist = shortestDistanceToSurface(cam, dir, MIN_DIST, MAX_DIST);
  76.    
  77.     if (dist > MAX_DIST - EPSILON) {
  78.         // Didn't hit anything >> black
  79.         fragColor = vec4(0., 0., 0., 0.);
  80.         return;
  81.     }
  82.    
  83.     fragColor = vec4(1., 1., 1., 1.);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement