Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4.  
  5. // These are defined by Kodelife, or whatever environment you are using.
  6. uniform float time;
  7. uniform vec2 resolution;
  8.  
  9. varying vec3 v_normal;
  10. varying vec2 v_texcoord;
  11.  
  12. // Define some constants
  13. const int steps = 128; // This is the maximum amount a ray can march.
  14. const float smallNumber = 0.001;
  15. const float maxDist = 10.; // This is the maximum distance a ray can travel.
  16.  
  17. float scene(vec3 position){
  18. // So this is different from the sphere equation above in that I am
  19. // splitting the position into its three different positions
  20. // and adding a 10th of a cos wave to the x position so it oscillates left
  21. // to right and a (positive) sin wave to the z position
  22. // so it will go back and forth.
  23. float sphere = length(
  24. vec3(
  25. position.x + cos(time)/10.,
  26. position.y,
  27. position.z+ sin(time) +1.)
  28. )-0.5;
  29.  
  30. // This is different from the ground equation because the UV is only
  31. // between -1 and 1 we want more than 1/2pi of a wave per length of the
  32. // screen so we multiply the position by a factor of 10 inside the trig
  33. // functions. Since sin and cos oscillate between -1 and 1, that would be
  34. // the entire height of the screen so we divide by a factor of 10.
  35. float ground = position.y + sin(position.x * 10.) / 10.
  36. + cos(position.z * 10.) / 10. + 1.;
  37.  
  38. // We want to return whichever one is closest to the ray, so we return the
  39. // minimum distance.
  40. return min(sphere,ground);
  41. }
  42.  
  43. vec4 trace (vec3 origin, vec3 direction){
  44.  
  45. float dist = 0.;
  46. float totalDistance = 0.;
  47. vec3 positionOnRay = origin;
  48.  
  49. for(int i = 0 ; i < steps; i++){
  50.  
  51. dist = scene(positionOnRay);
  52.  
  53. // Advance along the ray trajectory the amount that we know the ray
  54. // can travel without going through an object.
  55. positionOnRay += dist * direction;
  56.  
  57. // Total distance is keeping track of how much the ray has traveled
  58. // thus far.
  59. totalDistance += dist;
  60.  
  61. // If we hit an object or are close enough to an object,
  62. if (dist < smallNumber){
  63. // return the distance the ray had to travel normalized so be white
  64. // at the front and black in the back.
  65. return 1. - (vec4(totalDistance) / maxDist);
  66.  
  67. }
  68.  
  69. if (totalDistance > maxDist){
  70.  
  71. return vec4(0.); // Background color.
  72. }
  73. }
  74.  
  75. return vec4(0.);// Background color.
  76. }
  77.  
  78. // main is a reserved function that is going to be called first
  79. void main(void)
  80. {
  81. // We are redefining the UV coordinates (aka texcoords) to be 0,0 in the
  82. // middle of the screen this is because its easier to work with the camera at
  83. // (0,0) instead of (0.5,0.5) for the SDFs
  84. vec2 uv = -1. + 2. * v_texcoord;
  85.  
  86. // Unfortunately our screens are not square so we must account for that.
  87. uv.x *= (resolution.x / resolution.y);
  88.  
  89. vec3 rayOrigin = vec3(uv, 0.);
  90. vec3 camOrigin = vec3(0., 0., -1.);
  91. vec3 direction = camOrigin + rayOrigin;
  92.  
  93. // This reserved variable is what we must set the final color to
  94. gl_FragColor = trace(rayOrigin, direction);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement