Spaces:
Sleeping
Sleeping
Stephen
commited on
Commit
·
7805e3b
1
Parent(s):
eb4d6e4
change back to -1..1 and remove mesh transforms
Browse files- sdf_export.py +7 -35
sdf_export.py
CHANGED
@@ -12,33 +12,6 @@ def mesh_to_sdf_glsl(path, resolution=32):
|
|
12 |
print(f" Bounds: {mesh.bounds}")
|
13 |
print(f" Extents: {mesh.extents}")
|
14 |
|
15 |
-
# Center the mesh properly in all dimensions
|
16 |
-
bounds = mesh.bounds
|
17 |
-
center = (bounds[0] + bounds[1]) / 2.0
|
18 |
-
mesh.vertices -= center
|
19 |
-
print(f"\nAfter centering:")
|
20 |
-
print(f" Center: {center}")
|
21 |
-
print(f" Bounds: {mesh.bounds}")
|
22 |
-
print(f" Extents: {mesh.extents}")
|
23 |
-
|
24 |
-
# Calculate the maximum extent in each dimension
|
25 |
-
extents = np.max(np.abs(mesh.vertices), axis=0)
|
26 |
-
max_extent = np.max(extents)
|
27 |
-
print(f"\nExtents calculation:")
|
28 |
-
print(f" Per-dimension extents: {extents}")
|
29 |
-
print(f" Max extent: {max_extent}")
|
30 |
-
|
31 |
-
# Scale to fit in [0, 1] cube while preserving aspect ratio
|
32 |
-
scale = 0.9 / max_extent
|
33 |
-
mesh.vertices *= scale
|
34 |
-
# Shift to [0, 1] space
|
35 |
-
mesh.vertices += 0.5
|
36 |
-
print(f"\nAfter scaling and shifting:")
|
37 |
-
print(f" Scale factor: {scale}")
|
38 |
-
print(f" Bounds: {mesh.bounds}")
|
39 |
-
print(f" Extents: {mesh.extents}")
|
40 |
-
print(f" Vertex range: [{np.min(mesh.vertices)}, {np.max(mesh.vertices)}]")
|
41 |
-
|
42 |
# Convert vertices and faces to the correct types
|
43 |
vertices = mesh.vertices.astype(np.float32)
|
44 |
faces = mesh.faces.astype(np.uint32)
|
@@ -61,13 +34,12 @@ def mesh_to_sdf_glsl(path, resolution=32):
|
|
61 |
print(f" NaN values: {np.isnan(distances).sum()}")
|
62 |
print(f" Inf values: {np.isinf(distances).sum()}")
|
63 |
|
64 |
-
# Normalize distances to [-1, 1]
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
distances = distances * scale
|
69 |
print(f"\nAfter normalization:")
|
70 |
-
print(f"
|
71 |
print(f" New range: [{np.min(distances)}, {np.max(distances)}]")
|
72 |
print(f" Mean distance: {np.mean(distances)}")
|
73 |
print(f" Std distance: {np.std(distances)}")
|
@@ -105,9 +77,9 @@ float sdfData{i}[{len(chunks[i].split(','))}] = float[](
|
|
105 |
# Add the SDF function that combines chunks
|
106 |
glsl_code += f"""
|
107 |
float SDF(vec3 p) {{
|
108 |
-
// Map from [
|
109 |
vec3 dim = vec3({resolution}.0);
|
110 |
-
vec3 uv = p * (dim - 1.0);
|
111 |
|
112 |
// Add a small offset to avoid boundary issues
|
113 |
uv = clamp(uv, vec3(0.0), dim - 1.0);
|
|
|
12 |
print(f" Bounds: {mesh.bounds}")
|
13 |
print(f" Extents: {mesh.extents}")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Convert vertices and faces to the correct types
|
16 |
vertices = mesh.vertices.astype(np.float32)
|
17 |
faces = mesh.faces.astype(np.uint32)
|
|
|
34 |
print(f" NaN values: {np.isnan(distances).sum()}")
|
35 |
print(f" Inf values: {np.isinf(distances).sum()}")
|
36 |
|
37 |
+
# Normalize distances to [-1, 1] range
|
38 |
+
# This preserves the sign information which is important for SDF
|
39 |
+
max_dist = np.max(np.abs(distances))
|
40 |
+
distances = distances / max_dist
|
|
|
41 |
print(f"\nAfter normalization:")
|
42 |
+
print(f" Max distance: {max_dist}")
|
43 |
print(f" New range: [{np.min(distances)}, {np.max(distances)}]")
|
44 |
print(f" Mean distance: {np.mean(distances)}")
|
45 |
print(f" Std distance: {np.std(distances)}")
|
|
|
77 |
# Add the SDF function that combines chunks
|
78 |
glsl_code += f"""
|
79 |
float SDF(vec3 p) {{
|
80 |
+
// Map from [-1,1] to [0,resolution-1]
|
81 |
vec3 dim = vec3({resolution}.0);
|
82 |
+
vec3 uv = (p + 1.0) * 0.5 * (dim - 1.0);
|
83 |
|
84 |
// Add a small offset to avoid boundary issues
|
85 |
uv = clamp(uv, vec3(0.0), dim - 1.0);
|