sdf.c (1065B)
1 #include "perlin.h" 2 #include "sdf.h" 3 4 #include <math.h> 5 6 f32 perlinSDF(const Vec3 p) 7 { 8 return perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f) 9 + 0.5f * perlin(p[0] * 0.05f, p[1] * 0.05f, p[2] * 0.05f) 10 + 0.1f * perlin(p[0] * 0.1f, p[1] * 0.1f, p[2] * 0.1f); 11 } 12 13 f32 caveSDF(const Vec3 p) 14 { 15 f32 o1 = perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f); 16 17 return o1; 18 } 19 20 f32 terrainSDF(const Vec3 p) 21 { 22 f32 o1 = 50.0f * perlin(p[0] * 0.005f, 0.0f, p[2] * 0.005f); 23 f32 o2 = 2.0f * perlin(p[0] * 0.050f, 0.0f, p[2] * 0.050f); 24 f32 o3 = 1.0f * perlin(p[0] * 0.100f, 0.0f, p[2] * 0.100f); 25 26 f32 surface = p[1] - o1 - o2 - o3; 27 f32 cave = perlinSDF(p); 28 29 f32 k = 32.0f; 30 f32 h = 0.5f - 0.5f * (cave - surface) / k; 31 if (h < 0.0f) 32 { 33 h = 0.0f; 34 } 35 if (h > 1.0f) 36 { 37 h = 1.0f; 38 } 39 40 f32 a = cave * (1.0f - h) + surface * h; 41 f32 b = k * h * (1.0f - h); 42 43 return a + b; 44 } 45 46 f32 sphereSDF(const Vec3 p) 47 { 48 return sqrtf(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]) - 25.0f; 49 }