Spaces:
Sleeping
Sleeping
Stephen
commited on
Commit
·
e2969d6
1
Parent(s):
8ca6edb
initial commit
Browse files- README.md +11 -1
- app.py +20 -0
- requirements.txt +4 -0
- sdf_export.py +32 -0
README.md
CHANGED
@@ -11,4 +11,14 @@ license: mit
|
|
11 |
short_description: 3d meshes to sdf definitions in glsl
|
12 |
---
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
short_description: 3d meshes to sdf definitions in glsl
|
12 |
---
|
13 |
|
14 |
+
# Mesh to Embedded GLSL SDF Generator
|
15 |
+
|
16 |
+
Upload a 3D mesh file (.obj, .ply, .stl, etc.) and generate a fully embedded GLSL `float SDF(vec3 p)` function using a precomputed 64x64x64 Signed Distance Field.
|
17 |
+
|
18 |
+
### Features:
|
19 |
+
|
20 |
+
- Supports most common 3D formats
|
21 |
+
- Normalizes to [-1, 1] world-space
|
22 |
+
- Outputs both viewable and downloadable GLSL
|
23 |
+
|
24 |
+
Built with Gradio + mesh-to-sdf
|
app.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sdf_export import mesh_to_sdf_glsl
|
3 |
+
|
4 |
+
def generate_sdf_glsl(file_obj):
|
5 |
+
glsl_code = mesh_to_sdf_glsl(file_obj.name)
|
6 |
+
return glsl_code, ("sdf_generated.glsl", glsl_code)
|
7 |
+
|
8 |
+
iface = gr.Interface(
|
9 |
+
fn=generate_sdf_glsl,
|
10 |
+
inputs=gr.File(label="Upload 3D Mesh (.obj, .stl, .ply, .glb, etc.)"),
|
11 |
+
outputs=[
|
12 |
+
gr.Code(label="GLSL Output", language="glsl"),
|
13 |
+
gr.File(label="Download GLSL File")
|
14 |
+
],
|
15 |
+
title="Mesh to Embedded GLSL SDF Generator",
|
16 |
+
description="Upload a 3D mesh file to generate a fully embedded GLSL function with float[] SDF data."
|
17 |
+
)
|
18 |
+
|
19 |
+
if __name__ == "__main__":
|
20 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
trimesh
|
3 |
+
numpy
|
4 |
+
mesh-to-sdf
|
sdf_export.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import mesh_to_sdf
|
2 |
+
import trimesh
|
3 |
+
import numpy as np
|
4 |
+
import io
|
5 |
+
|
6 |
+
|
7 |
+
def mesh_to_sdf_glsl(path, resolution=64):
|
8 |
+
# Load mesh
|
9 |
+
mesh = trimesh.load(path)
|
10 |
+
|
11 |
+
# Generate voxel SDF
|
12 |
+
sdf = mesh_to_sdf.mesh_to_voxels(mesh, resolution)
|
13 |
+
sdf = np.clip(sdf, -1.0, 1.0) # Normalize to [-1, 1]
|
14 |
+
|
15 |
+
# Flatten for GLSL array
|
16 |
+
flat_sdf = sdf.flatten()
|
17 |
+
glsl_array = ", ".join(f"{v:.6f}" for v in flat_sdf)
|
18 |
+
|
19 |
+
glsl_code = f"""// Auto-generated SDF GLSL from mesh
|
20 |
+
float sdfData[{resolution**3}] = float[](
|
21 |
+
{glsl_array}
|
22 |
+
);
|
23 |
+
|
24 |
+
float SDF(vec3 p) {{
|
25 |
+
vec3 dim = vec3({resolution}.0);
|
26 |
+
vec3 uv = (p + 1.0) * 0.5 * dim;
|
27 |
+
ivec3 idx = ivec3(clamp(floor(uv), vec3(0.0), dim - 1.0));
|
28 |
+
int i = idx.x + idx.y * {resolution} + idx.z * {resolution * resolution};
|
29 |
+
return sdfData[i];
|
30 |
+
}}
|
31 |
+
"""
|
32 |
+
return glsl_code
|