(self, img, verts, cam, angle=None, axis=None, mesh_filename=None, color=[1.0, 1.0, 0.9],
cam_pose=np.eye(4))
| 106 | """ |
| 107 | |
| 108 | def render(self, img, verts, cam, angle=None, axis=None, mesh_filename=None, color=[1.0, 1.0, 0.9], |
| 109 | cam_pose=np.eye(4)): |
| 110 | mesh = trimesh.Trimesh(vertices=verts, faces=self.faces, process=False) |
| 111 | Rx = trimesh.transformations.rotation_matrix(math.radians(180), [1, 0, 0]) |
| 112 | # Rx = trimesh.transformations.rotation_matrix(math.radians(-90), [1, 0, 0]) |
| 113 | mesh.apply_transform(Rx) |
| 114 | |
| 115 | if mesh_filename is not None: |
| 116 | mesh.export(mesh_filename) |
| 117 | |
| 118 | if angle and axis: |
| 119 | R = trimesh.transformations.rotation_matrix(math.radians(angle), axis) |
| 120 | mesh.apply_transform(R) |
| 121 | |
| 122 | sx, sy, tx, ty = cam |
| 123 | |
| 124 | camera = WeakPerspectiveCamera( |
| 125 | scale=[sx, sy], |
| 126 | translation=[tx, ty], |
| 127 | zfar=100000. |
| 128 | ) |
| 129 | |
| 130 | material = pyrender.MetallicRoughnessMaterial( |
| 131 | metallicFactor=0.0, # 0.0 for no specular lighting |
| 132 | # metallicFactor=0.7, # 0.0 for no specular lighting |
| 133 | alphaMode='OPAQUE', |
| 134 | baseColorFactor=(color[0], color[1], color[2], 1.0) |
| 135 | ) |
| 136 | |
| 137 | mesh = pyrender.Mesh.from_trimesh(mesh, material=material) |
| 138 | |
| 139 | mesh_node = self.scene.add(mesh, 'mesh') |
| 140 | |
| 141 | cam_node = self.scene.add(camera, pose=cam_pose) |
| 142 | |
| 143 | if self.wireframe: |
| 144 | render_flags = RenderFlags.RGBA | RenderFlags.ALL_WIREFRAME |
| 145 | else: |
| 146 | render_flags = RenderFlags.RGBA |
| 147 | |
| 148 | rgb, _ = self.renderer.render(self.scene, flags=render_flags) |
| 149 | if rgb.shape[-1]==3: |
| 150 | # Debug |
| 151 | # 0 not distinguish alpha |
| 152 | valid_mask = (rgb[:, :, -1] > 0)[:, :, np.newaxis] |
| 153 | output_img = rgb * valid_mask + (1 - valid_mask) * img |
| 154 | elif rgb.shape[-1]==4: |
| 155 | # valid_mask = (rgb[:, :, -1] > 128)[:, :, np.newaxis] |
| 156 | # output_img = rgb[:, :, :-1] * valid_mask + (1 - valid_mask) * img |
| 157 | |
| 158 | # # output alpha |
| 159 | valid_mask = (rgb[:, :, -1] > 128)[:, :] |
| 160 | output_img = np.copy(rgb) |
| 161 | output_img[:, :, -1] *= valid_mask |
| 162 | # output_img = img |
| 163 | else: |
| 164 | raise ValueError(f"rgb shape {rgb.shape[-1]} is not correct!") |
| 165 | image = output_img.astype(np.uint8) |
no test coverage detected