Skip to content

Commit a0e14ca

Browse files
gkioxarifacebook-github-bot
authored andcommitted
flat shading fix
Summary: Make flat shading differentiable again Currently test fails with P130944403 which looks weird. Reviewed By: nikhilaravi Differential Revision: D21567106 fbshipit-source-id: 65995b64739e08397b3d021b65625e3c377cd1a5
1 parent 728179e commit a0e14ca

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

pytorch3d/renderer/mesh/shading.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,21 @@ def flat_shading(meshes, fragments, lights, cameras, materials, texels) -> torch
153153
face_normals = meshes.faces_normals_packed() # (V, 3)
154154
faces_verts = verts[faces]
155155
face_coords = faces_verts.mean(dim=-2) # (F, 3, XYZ) mean xyz across verts
156-
pixel_coords = face_coords[fragments.pix_to_face]
157-
pixel_normals = face_normals[fragments.pix_to_face]
156+
157+
# Replace empty pixels in pix_to_face with 0 in order to interpolate.
158+
mask = fragments.pix_to_face == -1
159+
pix_to_face = fragments.pix_to_face.clone()
160+
pix_to_face[mask] = 0
161+
162+
N, H, W, K = pix_to_face.shape
163+
idx = pix_to_face.view(N * H * W * K, 1).expand(N * H * W * K, 3)
164+
165+
# gather pixel coords
166+
pixel_coords = face_coords.gather(0, idx).view(N, H, W, K, 3)
167+
pixel_coords[mask] = 0.0
168+
# gather pixel normals
169+
pixel_normals = face_normals.gather(0, idx).view(N, H, W, K, 3)
170+
pixel_normals[mask] = 0.0
158171

159172
# Calculate the illumination at each face
160173
ambient, diffuse, specular = _apply_lighting(

0 commit comments

Comments
 (0)