Advertisement
Pixezy

Cutoff SDF Sphere include file

Sep 28th, 2024
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float saturate (float s){
  2.     return clamp(s,0,1);
  3. }
  4.  
  5. float remap(float value, float inputMin, float inputMax, float outputMin, float outputMax) {
  6.     return outputMin + (outputMax - outputMin) * (value - inputMin) / (inputMax - inputMin);
  7. }
  8.  
  9. float sdSphere(vec3 p, float s){
  10.     return length(p)-s;
  11. }
  12.  
  13. //--- simplex noise 3D functions
  14. vec3 mod289(vec3 x) {
  15.   return x - floor(x * (1.0 / 289.0)) * 289.0;
  16. }
  17.  
  18. vec4 mod289_v4(vec4 x) {
  19.   return x - floor(x * (1.0 / 289.0)) * 289.0;
  20. }
  21.  
  22. vec4 permute(vec4 x) {
  23.      return mod289_v4(((x*34.0)+10.0)*x);
  24. }
  25.  
  26. vec4 taylorInvSqrt(vec4 r)
  27. {
  28.   return 1.79284291400159 - 0.85373472095314 * r;
  29. }
  30. // Simplex 3D noise
  31. float snoise(vec3 v){
  32.  
  33.   const vec2  C = vec2(1.0/6.0, 1.0/3.0) ;
  34.   const vec4  D = vec4(0.0, 0.5, 1.0, 2.0);
  35.  
  36.   vec3 i  = floor(v + dot(v, C.yyy) );
  37.   vec3 x0 =   v - i + dot(i, C.xxx) ;
  38.  
  39.   vec3 g = step(x0.yzx, x0.xyz);
  40.   vec3 l = 1.0 - g;
  41.   vec3 i1 = min( g.xyz, l.zxy );
  42.   vec3 i2 = max( g.xyz, l.zxy );
  43.  
  44.   vec3 x1 = x0 - i1 + C.xxx;
  45.   vec3 x2 = x0 - i2 + C.yyy;
  46.   vec3 x3 = x0 - D.yyy;
  47.  
  48.   i = mod289(i);
  49.   vec4 p = permute( permute( permute(
  50.              i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
  51.            + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
  52.            + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
  53.  
  54.   float n_ = 0.142857142857;
  55.   vec3  ns = n_ * D.wyz - D.xzx;
  56.  
  57.   vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
  58.  
  59.   vec4 x_ = floor(j * ns.z);
  60.   vec4 y_ = floor(j - 7.0 * x_ );
  61.  
  62.   vec4 x = x_ *ns.x + ns.yyyy;
  63.   vec4 y = y_ *ns.x + ns.yyyy;
  64.   vec4 h = 1.0 - abs(x) - abs(y);
  65.  
  66.   vec4 b0 = vec4( x.xy, y.xy );
  67.   vec4 b1 = vec4( x.zw, y.zw );
  68.   vec4 s0 = floor(b0)*2.0 + 1.0;
  69.   vec4 s1 = floor(b1)*2.0 + 1.0;
  70.   vec4 sh = -step(h, vec4(0.0));
  71.  
  72.   vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
  73.   vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
  74.  
  75.   vec3 p0 = vec3(a0.xy,h.x);
  76.   vec3 p1 = vec3(a0.zw,h.y);
  77.   vec3 p2 = vec3(a1.xy,h.z);
  78.   vec3 p3 = vec3(a1.zw,h.w);
  79.  
  80.   vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
  81.   p0 *= norm.x;
  82.   p1 *= norm.y;
  83.   p2 *= norm.z;
  84.   p3 *= norm.w;
  85.  
  86.   vec4 m = max(0.5 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
  87.   m = m * m;
  88.   return 105.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
  89.                                 dot(p2,x2), dot(p3,x3) ) );
  90. }
  91. //--- end noise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement