Convert a Pytorch3D meshes's textures from TexturesUV to TexturesVertex. Args: meshes (Meshes): input Meshes. Returns: Meshes: converted Meshes.
(meshes: Meshes)
| 90 | |
| 91 | |
| 92 | def texture_uv2vc(meshes: Meshes) -> Meshes: |
| 93 | """Convert a Pytorch3D meshes's textures from TexturesUV to TexturesVertex. |
| 94 | |
| 95 | Args: |
| 96 | meshes (Meshes): input Meshes. |
| 97 | |
| 98 | Returns: |
| 99 | Meshes: converted Meshes. |
| 100 | """ |
| 101 | assert isinstance(meshes.textures, TexturesUV) |
| 102 | device = meshes.device |
| 103 | vert_uv = meshes.textures.verts_uvs_padded() |
| 104 | batch_size = vert_uv.shape[0] |
| 105 | verts_features = [] |
| 106 | num_verts = meshes.verts_padded().shape[1] |
| 107 | for index in range(batch_size): |
| 108 | face_uv = vert_uv[index][meshes.textures.faces_uvs_padded() |
| 109 | [index].view(-1)] |
| 110 | |
| 111 | img = meshes.textures._maps_padded[index] |
| 112 | width, height, _ = img.shape |
| 113 | |
| 114 | face_uv = face_uv * torch.Tensor([width - 1, height - 1 |
| 115 | ]).long().to(device) |
| 116 | |
| 117 | face_uv[:, 0] = torch.clip(face_uv[:, 0], 0, width - 1) |
| 118 | face_uv[:, 1] = torch.clip(face_uv[:, 1], 0, height - 1) |
| 119 | face_uv = face_uv.long() |
| 120 | faces = meshes.faces_padded() |
| 121 | verts_rgb = torch.zeros(1, num_verts, 3).to(device) |
| 122 | verts_rgb[:, faces.view(-1)] = img[height - 1 - face_uv[:, 1], |
| 123 | face_uv[:, 0]] |
| 124 | verts_features.append(verts_rgb) |
| 125 | verts_features = torch.cat(verts_features) |
| 126 | |
| 127 | meshes = meshes.clone() |
| 128 | meshes.textures = TexturesVertex(verts_features) |
| 129 | return meshes |
| 130 | |
| 131 | |
| 132 | def load_objs_as_meshes(files: List[str], |