__init__.py 3.6 KB

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