Spaces:
Running on Zero
Running on Zero
Rawal Khirodkar commited on
Commit ·
409a27b
1
Parent(s): abde977
Pointmap: inject KHR_materials_unlit into the GLB → no lighting variation, just raw pixel color
Browse files
app.py
CHANGED
|
@@ -330,9 +330,57 @@ def _make_glb(image_pil_texture: Image.Image, pointmap_hwc: np.ndarray,
|
|
| 330 |
)
|
| 331 |
out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".glb").name
|
| 332 |
mesh.export(out_path)
|
|
|
|
| 333 |
return out_path
|
| 334 |
|
| 335 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
# -----------------------------------------------------------------------------
|
| 337 |
# Gradio handler
|
| 338 |
|
|
|
|
| 330 |
)
|
| 331 |
out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".glb").name
|
| 332 |
mesh.export(out_path)
|
| 333 |
+
_glb_inject_unlit(out_path) # → render with raw vertex color, no lighting
|
| 334 |
return out_path
|
| 335 |
|
| 336 |
|
| 337 |
+
# -----------------------------------------------------------------------------
|
| 338 |
+
# GLB post-processing: inject KHR_materials_unlit extension so Three.js skips
|
| 339 |
+
# all lighting calculations and renders surfels as their raw vertex colour.
|
| 340 |
+
|
| 341 |
+
import struct
|
| 342 |
+
import json
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
def _glb_inject_unlit(glb_path: str) -> None:
|
| 346 |
+
"""Patch a binary GLB to mark every material as KHR_materials_unlit.
|
| 347 |
+
glTF spec for the extension: surfaces render with `baseColor` only,
|
| 348 |
+
no shading from light sources or normals."""
|
| 349 |
+
with open(glb_path, "rb") as f:
|
| 350 |
+
data = f.read()
|
| 351 |
+
|
| 352 |
+
# GLB header: magic | version | total_length (12 bytes)
|
| 353 |
+
if data[:4] != b"glTF":
|
| 354 |
+
return
|
| 355 |
+
# First chunk: length(4) | type(4) | payload — JSON chunk.
|
| 356 |
+
json_len, json_type = struct.unpack_from("<II", data, 12)
|
| 357 |
+
if json_type != 0x4E4F534A: # "JSON"
|
| 358 |
+
return
|
| 359 |
+
json_bytes = data[20 : 20 + json_len]
|
| 360 |
+
bin_tail = data[20 + json_len :] # everything after = BIN chunk(s)
|
| 361 |
+
|
| 362 |
+
gltf = json.loads(json_bytes.rstrip(b" \x00").decode("utf-8"))
|
| 363 |
+
used = gltf.setdefault("extensionsUsed", [])
|
| 364 |
+
if "KHR_materials_unlit" not in used:
|
| 365 |
+
used.append("KHR_materials_unlit")
|
| 366 |
+
for mat in gltf.get("materials", []):
|
| 367 |
+
mat.setdefault("extensions", {})["KHR_materials_unlit"] = {}
|
| 368 |
+
|
| 369 |
+
new_json = json.dumps(gltf, separators=(",", ":")).encode("utf-8")
|
| 370 |
+
pad = (4 - len(new_json) % 4) % 4
|
| 371 |
+
new_json += b" " * pad
|
| 372 |
+
|
| 373 |
+
new_total = 12 + 8 + len(new_json) + len(bin_tail)
|
| 374 |
+
out = (
|
| 375 |
+
b"glTF" + struct.pack("<II", 2, new_total)
|
| 376 |
+
+ struct.pack("<II", len(new_json), 0x4E4F534A)
|
| 377 |
+
+ new_json
|
| 378 |
+
+ bin_tail
|
| 379 |
+
)
|
| 380 |
+
with open(glb_path, "wb") as f:
|
| 381 |
+
f.write(out)
|
| 382 |
+
|
| 383 |
+
|
| 384 |
# -----------------------------------------------------------------------------
|
| 385 |
# Gradio handler
|
| 386 |
|