Spaces:
Running
Running
Stephen
commited on
Commit
·
511a384
1
Parent(s):
dd311a9
lets try this i guess yolo
Browse files- sdf_export.py +7 -4
sdf_export.py
CHANGED
@@ -24,19 +24,22 @@ def mesh_to_sdf_glsl(path, resolution=64):
|
|
24 |
# Normalize distances to [-1, 1]
|
25 |
distances = np.clip(distances, -1.0, 1.0)
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
glsl_array = ", ".join(f"{float(v):.6f}" for v in flat_distances)
|
30 |
|
31 |
glsl_code = f"""// Auto-generated SDF GLSL from mesh
|
|
|
32 |
float sdfData[{resolution**3}] = float[](
|
33 |
{glsl_array}
|
34 |
);
|
35 |
|
36 |
float SDF(vec3 p) {{
|
|
|
37 |
vec3 dim = vec3({resolution}.0);
|
38 |
-
vec3 uv = (p + 1.0) * 0.5 * dim;
|
39 |
ivec3 idx = ivec3(clamp(floor(uv), vec3(0.0), dim - 1.0));
|
|
|
|
|
40 |
int i = idx.x + idx.y * {resolution} + idx.z * {resolution * resolution};
|
41 |
return sdfData[i];
|
42 |
}}
|
|
|
24 |
# Normalize distances to [-1, 1]
|
25 |
distances = np.clip(distances, -1.0, 1.0)
|
26 |
|
27 |
+
# Convert to GLSL array
|
28 |
+
glsl_array = ", ".join(f"{float(v):.6f}" for v in distances.ravel())
|
|
|
29 |
|
30 |
glsl_code = f"""// Auto-generated SDF GLSL from mesh
|
31 |
+
// Resolution: {resolution}x{resolution}x{resolution}
|
32 |
float sdfData[{resolution**3}] = float[](
|
33 |
{glsl_array}
|
34 |
);
|
35 |
|
36 |
float SDF(vec3 p) {{
|
37 |
+
// Map from [-1,1] to [0,resolution-1]
|
38 |
vec3 dim = vec3({resolution}.0);
|
39 |
+
vec3 uv = (p + 1.0) * 0.5 * (dim - 1.0);
|
40 |
ivec3 idx = ivec3(clamp(floor(uv), vec3(0.0), dim - 1.0));
|
41 |
+
|
42 |
+
// Convert 3D index to 1D array index
|
43 |
int i = idx.x + idx.y * {resolution} + idx.z * {resolution * resolution};
|
44 |
return sdfData[i];
|
45 |
}}
|