diff --git a/pytorch3d/renderer/mesh/rasterizer.py b/pytorch3d/renderer/mesh/rasterizer.py index 424cdb44..04044f5f 100644 --- a/pytorch3d/renderer/mesh/rasterizer.py +++ b/pytorch3d/renderer/mesh/rasterizer.py @@ -37,7 +37,7 @@ class RasterizationSettings: bin_size: Optional[int] = None, max_faces_per_bin: Optional[int] = None, perspective_correct: bool = False, - clip_barycentric_coords: bool = False, + clip_barycentric_coords: Optional[bool] = None, cull_backfaces: bool = False, ): self.image_size = image_size @@ -125,6 +125,14 @@ class MeshRasterizer(nn.Module): """ meshes_screen = self.transform(meshes_world, **kwargs) raster_settings = kwargs.get("raster_settings", self.raster_settings) + + # By default, turn on clip_barycentric_coords if blur_radius > 0. + # When blur_radius > 0, a face can be matched to a pixel that is outside the + # face, resulting in negative barycentric coordinates. + clip_barycentric_coords = raster_settings.clip_barycentric_coords + if clip_barycentric_coords is None: + clip_barycentric_coords = raster_settings.blur_radius > 0.0 + # TODO(jcjohns): Should we try to set perspective_correct automatically # based on the type of the camera? pix_to_face, zbuf, bary_coords, dists = rasterize_meshes( @@ -135,7 +143,7 @@ class MeshRasterizer(nn.Module): bin_size=raster_settings.bin_size, max_faces_per_bin=raster_settings.max_faces_per_bin, perspective_correct=raster_settings.perspective_correct, - clip_barycentric_coords=raster_settings.clip_barycentric_coords, + clip_barycentric_coords=clip_barycentric_coords, cull_backfaces=raster_settings.cull_backfaces, ) return Fragments(