Spaces:
Sleeping
Sleeping
Benjamin Auquite commited on
feat: texture cpu-demo mesh with input photo; full UI rewrite
Browse files
src/imageezgen3d/exporters.py
CHANGED
|
@@ -14,9 +14,16 @@ class SimpleMesh:
|
|
| 14 |
vertices: tuple[tuple[float, float, float], ...]
|
| 15 |
faces: tuple[tuple[int, int, int], ...]
|
| 16 |
color: tuple[float, float, float, float]
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
-
def make_box_mesh(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
w, d, h = width / 2, depth / 2, height / 2
|
| 21 |
vertices = (
|
| 22 |
(-w, -d, -h), (w, -d, -h), (w, d, -h), (-w, d, -h),
|
|
@@ -27,7 +34,7 @@ def make_box_mesh(width: float, depth: float, height: float, color: tuple[float,
|
|
| 27 |
(0, 4, 5), (0, 5, 1), (1, 5, 6), (1, 6, 2),
|
| 28 |
(2, 6, 7), (2, 7, 3), (3, 7, 4), (3, 4, 0),
|
| 29 |
)
|
| 30 |
-
return SimpleMesh(vertices=vertices, faces=faces, color=color)
|
| 31 |
|
| 32 |
|
| 33 |
def write_obj(mesh: SimpleMesh, path: Path) -> None:
|
|
@@ -79,28 +86,66 @@ def _pad4(data: bytes, pad_byte: bytes) -> bytes:
|
|
| 79 |
def write_glb(mesh: SimpleMesh, path: Path) -> None:
|
| 80 |
positions = b"".join(struct.pack("<3f", *vertex) for vertex in mesh.vertices)
|
| 81 |
indices = b"".join(struct.pack("<H", index) for face in mesh.faces for index in face)
|
| 82 |
-
|
| 83 |
-
indices_offset = len(positions_padded)
|
| 84 |
-
bin_blob = _pad4(positions_padded + indices, b"\x00")
|
| 85 |
mins = [min(vertex[i] for vertex in mesh.vertices) for i in range(3)]
|
| 86 |
maxs = [max(vertex[i] for vertex in mesh.vertices) for i in range(3)]
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
"asset": {"version": "2.0", "generator": "ImageEZGen3D CPU demo"},
|
| 89 |
"scene": 0,
|
| 90 |
"scenes": [{"nodes": [0]}],
|
| 91 |
"nodes": [{"mesh": 0, "name": "ImageEZGen3D Draft"}],
|
| 92 |
-
"materials": [
|
| 93 |
-
"meshes": [{"primitives": [{"attributes": {"POSITION": 0}, "indices": 1, "material": 0}]}],
|
| 94 |
"buffers": [{"byteLength": len(bin_blob)}],
|
| 95 |
"bufferViews": [
|
| 96 |
{"buffer": 0, "byteOffset": 0, "byteLength": len(positions), "target": 34962},
|
| 97 |
{"buffer": 0, "byteOffset": indices_offset, "byteLength": len(indices), "target": 34963},
|
|
|
|
| 98 |
],
|
| 99 |
"accessors": [
|
| 100 |
{"bufferView": 0, "componentType": 5126, "count": len(mesh.vertices), "type": "VEC3", "min": mins, "max": maxs},
|
| 101 |
{"bufferView": 1, "componentType": 5123, "count": len(mesh.faces) * 3, "type": "SCALAR"},
|
|
|
|
| 102 |
],
|
| 103 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
json_blob = _pad4(json.dumps(gltf, separators=(",", ":")).encode("utf-8"), b" ")
|
| 105 |
total_length = 12 + 8 + len(json_blob) + 8 + len(bin_blob)
|
| 106 |
header = struct.pack("<4sII", b"glTF", 2, total_length)
|
|
|
|
| 14 |
vertices: tuple[tuple[float, float, float], ...]
|
| 15 |
faces: tuple[tuple[int, int, int], ...]
|
| 16 |
color: tuple[float, float, float, float]
|
| 17 |
+
b64_image: str | None = None
|
| 18 |
|
| 19 |
|
| 20 |
+
def make_box_mesh(
|
| 21 |
+
width: float,
|
| 22 |
+
depth: float,
|
| 23 |
+
height: float,
|
| 24 |
+
color: tuple[float, float, float, float],
|
| 25 |
+
b64_image: str | None = None,
|
| 26 |
+
) -> SimpleMesh:
|
| 27 |
w, d, h = width / 2, depth / 2, height / 2
|
| 28 |
vertices = (
|
| 29 |
(-w, -d, -h), (w, -d, -h), (w, d, -h), (-w, d, -h),
|
|
|
|
| 34 |
(0, 4, 5), (0, 5, 1), (1, 5, 6), (1, 6, 2),
|
| 35 |
(2, 6, 7), (2, 7, 3), (3, 7, 4), (3, 4, 0),
|
| 36 |
)
|
| 37 |
+
return SimpleMesh(vertices=vertices, faces=faces, color=color, b64_image=b64_image)
|
| 38 |
|
| 39 |
|
| 40 |
def write_obj(mesh: SimpleMesh, path: Path) -> None:
|
|
|
|
| 86 |
def write_glb(mesh: SimpleMesh, path: Path) -> None:
|
| 87 |
positions = b"".join(struct.pack("<3f", *vertex) for vertex in mesh.vertices)
|
| 88 |
indices = b"".join(struct.pack("<H", index) for face in mesh.faces for index in face)
|
| 89 |
+
|
|
|
|
|
|
|
| 90 |
mins = [min(vertex[i] for vertex in mesh.vertices) for i in range(3)]
|
| 91 |
maxs = [max(vertex[i] for vertex in mesh.vertices) for i in range(3)]
|
| 92 |
+
|
| 93 |
+
# Orthographic XZ projection: maps the input image across the box
|
| 94 |
+
# The front face (y-axis) will display the image facing forward
|
| 95 |
+
x_span = (maxs[0] - mins[0]) or 1.0
|
| 96 |
+
z_span = (maxs[2] - mins[2]) or 1.0
|
| 97 |
+
uvs = b"".join(
|
| 98 |
+
struct.pack(
|
| 99 |
+
"<2f",
|
| 100 |
+
(v[0] - mins[0]) / x_span, # u: left to right
|
| 101 |
+
1.0 - (v[2] - mins[2]) / z_span, # v: bottom to top (flip Z)
|
| 102 |
+
)
|
| 103 |
+
for v in mesh.vertices
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
positions_padded = _pad4(positions, b"\x00")
|
| 107 |
+
indices_padded = _pad4(indices, b"\x00")
|
| 108 |
+
uvs_padded = _pad4(uvs, b"\x00")
|
| 109 |
+
|
| 110 |
+
indices_offset = len(positions_padded)
|
| 111 |
+
uvs_offset = indices_offset + len(indices_padded)
|
| 112 |
+
|
| 113 |
+
bin_blob = _pad4(positions_padded + indices_padded + uvs_padded, b"\x00")
|
| 114 |
+
|
| 115 |
+
mat: dict = {
|
| 116 |
+
"pbrMetallicRoughness": {
|
| 117 |
+
"baseColorFactor": list(mesh.color),
|
| 118 |
+
"metallicFactor": 0.0,
|
| 119 |
+
"roughnessFactor": 0.72,
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
gltf: dict = {
|
| 123 |
"asset": {"version": "2.0", "generator": "ImageEZGen3D CPU demo"},
|
| 124 |
"scene": 0,
|
| 125 |
"scenes": [{"nodes": [0]}],
|
| 126 |
"nodes": [{"mesh": 0, "name": "ImageEZGen3D Draft"}],
|
| 127 |
+
"materials": [mat],
|
| 128 |
+
"meshes": [{"primitives": [{"attributes": {"POSITION": 0, "TEXCOORD_0": 2}, "indices": 1, "material": 0}]}],
|
| 129 |
"buffers": [{"byteLength": len(bin_blob)}],
|
| 130 |
"bufferViews": [
|
| 131 |
{"buffer": 0, "byteOffset": 0, "byteLength": len(positions), "target": 34962},
|
| 132 |
{"buffer": 0, "byteOffset": indices_offset, "byteLength": len(indices), "target": 34963},
|
| 133 |
+
{"buffer": 0, "byteOffset": uvs_offset, "byteLength": len(uvs), "target": 34962},
|
| 134 |
],
|
| 135 |
"accessors": [
|
| 136 |
{"bufferView": 0, "componentType": 5126, "count": len(mesh.vertices), "type": "VEC3", "min": mins, "max": maxs},
|
| 137 |
{"bufferView": 1, "componentType": 5123, "count": len(mesh.faces) * 3, "type": "SCALAR"},
|
| 138 |
+
{"bufferView": 2, "componentType": 5126, "count": len(mesh.vertices), "type": "VEC2"},
|
| 139 |
],
|
| 140 |
}
|
| 141 |
+
|
| 142 |
+
if mesh.b64_image:
|
| 143 |
+
gltf["images"] = [{"uri": "data:image/jpeg;base64," + mesh.b64_image}]
|
| 144 |
+
gltf["samplers"] = [{"magFilter": 9729, "minFilter": 9987, "wrapS": 33071, "wrapT": 33071}]
|
| 145 |
+
gltf["textures"] = [{"sampler": 0, "source": 0}]
|
| 146 |
+
mat["pbrMetallicRoughness"]["baseColorTexture"] = {"index": 0}
|
| 147 |
+
mat["pbrMetallicRoughness"]["baseColorFactor"] = [1.0, 1.0, 1.0, 1.0]
|
| 148 |
+
|
| 149 |
json_blob = _pad4(json.dumps(gltf, separators=(",", ":")).encode("utf-8"), b" ")
|
| 150 |
total_length = 12 + 8 + len(json_blob) + 8 + len(bin_blob)
|
| 151 |
header = struct.pack("<4sII", b"glTF", 2, total_length)
|