Spaces:
Sleeping
Sleeping
Stephen
commited on
Commit
·
b02879d
1
Parent(s):
e7bae15
allow various precision
Browse files- app.py +13 -4
- sdf_export.py +3 -3
app.py
CHANGED
@@ -4,8 +4,8 @@ import tempfile
|
|
4 |
import os
|
5 |
|
6 |
|
7 |
-
def generate_sdf_glsl(file_obj):
|
8 |
-
glsl_code = mesh_to_sdf_glsl(file_obj.name)
|
9 |
|
10 |
# Create a temporary file for the GLSL code
|
11 |
with tempfile.NamedTemporaryFile(mode="w", suffix=".glsl", delete=False) as f:
|
@@ -17,13 +17,22 @@ def generate_sdf_glsl(file_obj):
|
|
17 |
|
18 |
iface = gr.Interface(
|
19 |
fn=generate_sdf_glsl,
|
20 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
outputs=[
|
22 |
gr.Textbox(label="GLSL Output"),
|
23 |
gr.File(label="Download GLSL File"),
|
24 |
],
|
25 |
title="Mesh to Embedded GLSL SDF Generator",
|
26 |
-
description="Upload a 3D mesh file to generate a fully embedded GLSL function with float[] SDF data.",
|
27 |
)
|
28 |
|
29 |
if __name__ == "__main__":
|
|
|
4 |
import os
|
5 |
|
6 |
|
7 |
+
def generate_sdf_glsl(file_obj, resolution):
|
8 |
+
glsl_code = mesh_to_sdf_glsl(file_obj.name, resolution)
|
9 |
|
10 |
# Create a temporary file for the GLSL code
|
11 |
with tempfile.NamedTemporaryFile(mode="w", suffix=".glsl", delete=False) as f:
|
|
|
17 |
|
18 |
iface = gr.Interface(
|
19 |
fn=generate_sdf_glsl,
|
20 |
+
inputs=[
|
21 |
+
gr.File(label="Upload 3D Mesh (.obj, .stl, .ply, .glb, etc.)"),
|
22 |
+
gr.Slider(
|
23 |
+
minimum=16,
|
24 |
+
maximum=64,
|
25 |
+
step=16,
|
26 |
+
value=32,
|
27 |
+
label="Resolution (16=low, 32=medium, 64=high)",
|
28 |
+
),
|
29 |
+
],
|
30 |
outputs=[
|
31 |
gr.Textbox(label="GLSL Output"),
|
32 |
gr.File(label="Download GLSL File"),
|
33 |
],
|
34 |
title="Mesh to Embedded GLSL SDF Generator",
|
35 |
+
description="Upload a 3D mesh file to generate a fully embedded GLSL function with float[] SDF data. Lower resolution = smaller file size and faster loading.",
|
36 |
)
|
37 |
|
38 |
if __name__ == "__main__":
|
sdf_export.py
CHANGED
@@ -3,7 +3,7 @@ import numpy as np
|
|
3 |
import mesh2sdf
|
4 |
|
5 |
|
6 |
-
def mesh_to_sdf_glsl(path, resolution=
|
7 |
# Load mesh
|
8 |
mesh = trimesh.load(path)
|
9 |
|
@@ -24,8 +24,8 @@ 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 |
-
# Convert to GLSL array
|
28 |
-
glsl_array = ", ".join(f"{float(v):.
|
29 |
|
30 |
glsl_code = f"""// Auto-generated SDF GLSL from mesh
|
31 |
// Resolution: {resolution}x{resolution}x{resolution}
|
|
|
3 |
import mesh2sdf
|
4 |
|
5 |
|
6 |
+
def mesh_to_sdf_glsl(path, resolution=32):
|
7 |
# Load mesh
|
8 |
mesh = trimesh.load(path)
|
9 |
|
|
|
24 |
# Normalize distances to [-1, 1]
|
25 |
distances = np.clip(distances, -1.0, 1.0)
|
26 |
|
27 |
+
# Convert to GLSL array with less precision
|
28 |
+
glsl_array = ", ".join(f"{float(v):.4f}" for v in distances.ravel())
|
29 |
|
30 |
glsl_code = f"""// Auto-generated SDF GLSL from mesh
|
31 |
// Resolution: {resolution}x{resolution}x{resolution}
|