Advertisement
Rakshalpha

RayMarching

Oct 11th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. float length(float2 v){
  2.     return sqrt(v.x * v.x + v.y * v.y)
  3. }
  4.  
  5. float signedDstToCircle(float2 p, float2 centre, float radius){
  6.     return length(centre - p) - radius;
  7. }
  8.  
  9. float signedDstToBox(float2 p, float2 centre, float2 size){
  10.     float2 offset = abs(p - centre) - size;
  11.     float unsignedDst = length(max(offset, 0));
  12.     float dstInsideBox = max(min(offset, 0));
  13.     return unsignedDist + dstInsideBox;
  14. }
  15.  
  16. float signedDstToScene(float2 p){
  17. float dstToScene = maxDst;
  18.  
  19.     for(int i = 0; i < numCircles; i++){
  20.         float dstToCircle = signedDstToCircle(p, circles[i].centre, circles[i].radius);
  21.         dstToScene = min(dstToCircle, dstToScene);
  22.     }
  23.  
  24.     for(int i = 0; i < numBoxes; i++) {
  25.         float dstToBox = signedDstToBox(p, boxes[i].centre, boxes[i].size);
  26.         dstToScene = min(dstToBox, dstToScene);
  27.     }
  28.  
  29.     return dstToScene;
  30. }
  31.  
  32. float signedDstToScene (float3 p) {
  33.     float dstToCube = CubeDistance(p, cube.centre, cube.size);
  34.     float dstToSphere = SphereDistance(p, sphere.centre, sphere.radius);
  35.  
  36.     return max(dstToCube, dstToSphere * -1);
  37. }
  38.  
  39. float smoothMin(float dstA, float dstB, float k){
  40.     float h = max(k - abs(dstA - dstB), 0) / k;
  41.     return min(dstA, dstB) - h * h * h * k * 1/6.0;
  42. }
  43.  
  44. float signedDstToScene(float3 p){
  45.     float dstA = dot(sin(p * paramA), 1);
  46.     float dstB = dot(p % paramB, 1);
  47.     return lerp(dstA, dstB, t);
  48. }
  49.  
  50. float mandelbulb(float3 position){
  51.     float3 z = position;
  52.     float dr = 1;
  53.     float r;
  54.  
  55.     for(int i = 0; i < 15; i++){
  56.         r = length(z); 
  57.         if(r > 2)
  58.             break;
  59.  
  60.         float theta = acos(z.z / r) * power;
  61.         float phi = atan2(z.y, z.x) * power;
  62.         float zr = pow(r, power - 1) * power * dr + 1;
  63.  
  64.         z = zr * float3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta));
  65.         z += position;
  66.     }
  67.     return 0.5 * log(r) * r / dr;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement