__init__.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import torch
  2. import math
  3. from diff_gaussian_rasterization import GaussianRasterizationSettings, GaussianRasterizer
  4. from scene.gaussian_model import GaussianModel
  5. from utils.sh_utils import eval_sh
  6. def render(viewpoint_camera, pc : GaussianModel, pipe, bg_color : torch.Tensor, scaling_modifier = 1.0, override_color = None):
  7. """
  8. Render the scene.
  9. Background tensor (bg_color) must be on GPU!
  10. """
  11. # Create zero tensor. We will use it to make pytorch return gradients of the 2D (screen-space) means
  12. screenspace_points = torch.zeros_like(pc.get_xyz, dtype=pc.get_xyz.dtype, requires_grad=True, device="cuda") + 0
  13. try:
  14. screenspace_points.retain_grad()
  15. except:
  16. pass
  17. # Set up rasterization configuration
  18. tanfovx = math.tan(viewpoint_camera.FoVx * 0.5)
  19. tanfovy = math.tan(viewpoint_camera.FoVy * 0.5)
  20. raster_settings = GaussianRasterizationSettings(
  21. image_height=int(viewpoint_camera.image_height),
  22. image_width=int(viewpoint_camera.image_width),
  23. tanfovx=tanfovx,
  24. tanfovy=tanfovy,
  25. bg=bg_color,
  26. scale_modifier=scaling_modifier,
  27. viewmatrix=viewpoint_camera.world_view_transform,
  28. projmatrix=viewpoint_camera.full_proj_transform,
  29. sh_degree=pc.active_sh_degree,
  30. campos=viewpoint_camera.camera_center,
  31. prefiltered=False
  32. )
  33. rasterizer = GaussianRasterizer(raster_settings=raster_settings)
  34. means3D = pc.get_xyz
  35. means2D = screenspace_points
  36. opacity = pc.get_opacity
  37. # If precomputed 3d covariance is provided, use it. If not, then it will be computed from
  38. # scaling / rotation by the rasterizer.
  39. scales = None
  40. rotations = None
  41. cov3D_precomp = None
  42. if pipe.compute_cov3D_python:
  43. cov3D_precomp = pc.get_covariance(scaling_modifier)
  44. else:
  45. scales = pc.get_scaling
  46. rotations = pc.get_rotation
  47. # If precomputed colors are provided, use them. Otherwise, if it is desired to precompute colors
  48. # from SHs in Python, do it. If not, then SH -> RGB conversion will be done by rasterizer.
  49. shs = None
  50. colors_precomp = None
  51. if colors_precomp is None:
  52. if pipe.convert_SHs_python:
  53. shs_view = pc.get_features.transpose(1, 2).view(-1, 3, (pc.max_sh_degree+1)**2)
  54. dir_pp = (pc.get_xyz - viewpoint_camera.camera_center.repeat(pc.get_features.shape[0], 1))
  55. dir_pp_normalized = dir_pp/dir_pp.norm(dim=1, keepdim=True)
  56. sh2rgb = eval_sh(pc.active_sh_degree, shs_view, dir_pp_normalized)
  57. colors_precomp = torch.clamp_min(sh2rgb + 0.5, 0.0)
  58. else:
  59. shs = pc.get_features
  60. else:
  61. colors_precomp = override_color
  62. # Rasterize visible Gaussians to image, obtain their radii (on screen).
  63. rendered_image, radii = rasterizer(
  64. means3D = means3D,
  65. means2D = means2D,
  66. shs = shs,
  67. colors_precomp = colors_precomp,
  68. opacities = opacity,
  69. scales = scales,
  70. rotations = rotations,
  71. cov3D_precomp = cov3D_precomp)
  72. # Those Gaussians that were frustum culled or had a radius of 0 were not visible.
  73. # They will be excluded from value updates used in the splitting criteria.
  74. return {"render": rendered_image,
  75. "viewspace_points": screenspace_points,
  76. "visibility_filter" : radii > 0,
  77. "radii": radii}