cronos3k commited on
Commit
6ab7fac
·
verified ·
1 Parent(s): b7adca8

Update trellis/utils/postprocessing_utils.py

Browse files
trellis/utils/postprocessing_utils.py CHANGED
@@ -456,3 +456,53 @@ def to_glb(
456
  vertices = vertices @ np.array([[1, 0, 0], [0, 0, -1], [0, 1, 0]])
457
  mesh = trimesh.Trimesh(vertices, faces, visual=trimesh.visual.TextureVisuals(uv=uvs, image=texture))
458
  return mesh
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456
  vertices = vertices @ np.array([[1, 0, 0], [0, 0, -1], [0, 1, 0]])
457
  mesh = trimesh.Trimesh(vertices, faces, visual=trimesh.visual.TextureVisuals(uv=uvs, image=texture))
458
  return mesh
459
+
460
+
461
+ @spaces.GPU
462
+ def export_full_quality_glb(
463
+ state: dict,
464
+ req: gr.Request,
465
+ ) -> Tuple[str, str]:
466
+ """
467
+ Export a full-quality GLB file directly from the 3D model state,
468
+ skipping unnecessary optimization steps.
469
+ """
470
+ user_dir = os.path.join(TMP_DIR, str(req.session_hash))
471
+ gs, mesh, trial_id = unpack_state(state)
472
+
473
+ # Generate full quality GLB directly
474
+ vertices = mesh.vertices.cpu().numpy()
475
+ faces = mesh.faces.cpu().numpy()
476
+
477
+ # Just do the parametrization and texture baking
478
+ vertices, faces, uvs = postprocessing_utils.parametrize_mesh(vertices, faces)
479
+
480
+ # Generate texture at full resolution
481
+ observations, extrinsics, intrinsics = postprocessing_utils.render_multiview(gs, resolution=2048, nviews=100)
482
+ masks = [np.any(observation > 0, axis=-1) for observation in observations]
483
+ extrinsics = [extrinsics[i].cpu().numpy() for i in range(len(extrinsics))]
484
+ intrinsics = [intrinsics[i].cpu().numpy() for i in range(len(intrinsics))]
485
+
486
+ texture = postprocessing_utils.bake_texture(
487
+ vertices, faces, uvs,
488
+ observations, masks, extrinsics, intrinsics,
489
+ texture_size=2048, # Maximum texture resolution
490
+ mode='opt', # Use optimization mode for best quality
491
+ lambda_tv=0.01 # Light smoothing to prevent artifacts
492
+ )
493
+
494
+ # Create the mesh with full quality settings
495
+ mesh = trimesh.Trimesh(
496
+ vertices,
497
+ faces,
498
+ visual=trimesh.visual.TextureVisuals(
499
+ uv=uvs,
500
+ image=Image.fromarray(texture)
501
+ )
502
+ )
503
+
504
+ # Export
505
+ glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
506
+ mesh.export(glb_path)
507
+
508
+ return glb_path, glb_path