def create_scene(self, hws, alphas, N): batch = [] for i in range(N): scene = [] for mesh_name in self.models: hw = hws[mesh_name] alpha = alphas[mesh_name] N, K, _ = hw.shape for k in range(K): c = self.colors[mesh_name].clone() c[..., 3] = alpha[i, k] textures = TexturesVertex(verts_features=[c]) m = Meshes(verts=[self.verts[mesh_name].clone()], faces=[self.faces[mesh_name].clone()], textures=textures) t = Translate(y=hw[i, k, 0], x=hw[i, k, 1], z=torch.zeros(1, device=self.device), device=str(self.device)) m = m.update_padded(t.transform_points(m.verts_padded())) scene += [m] batch += [join_meshes_as_scene(scene)] batch = join_meshes_as_batch(batch) return batch
def read( self, path: PathOrStr, include_textures: bool, device, path_manager: PathManager, **kwargs, ) -> Optional[Meshes]: if not endswith(path, self.known_suffixes): return None names_meshes_list = load_meshes( path=path, path_manager=path_manager, include_textures=include_textures, ) meshes_list = [mesh for name, mesh in names_meshes_list] mesh = join_meshes_as_scene(meshes_list) return mesh.to(device)
def test_my_renderer(): vert, normal, st, color, face = load_blender_ply_mesh( '../data/meshes/background.ply') # V, _ = vert.shape # meshes = Meshes( # verts=[vert.to(device)], # faces=[face.to(device)], # textures=TexturesVertex([torch.cat((color.to(device), torch.ones(V, 1, device=device)), dim=1)]) # ) # vert = torch.tensor([ # [-1, -1, 0], # [1, -1, 0], # [1, 1, 0], # [-1, 1, 0] # ], dtype=torch.float) * 30 x = 80 y = 93 vert = torch.tensor([[-x, -y, 0], [x, -y, 0], [x, y, 0], [-x, y, 0]], dtype=torch.float) face = torch.LongTensor([[0, 1, 2], [0, 2, 3]]) meshes = Meshes(verts=[vert.to(device)], faces=[face.to(device)], textures=TexturesVertex([color.to(device)])) scene = join_meshes_as_scene(meshes) zbuffer_renderer = MeshRenderer(rasterizer=MeshRasterizer( cameras=cameras, raster_settings=raster_settings, ), shader=IdentityShader()) plot_channels(render(zbuffer_renderer, scene))
def _add_mesh_trace( fig: go.Figure, # pyre-ignore[11] meshes: Meshes, trace_name: str, subplot_idx: int, ncols: int, lighting: Lighting, ): # pragma: no cover """ Adds a trace rendering a Meshes object to the passed in figure, with a given name and in a specific subplot. Args: fig: plotly figure to add the trace within. meshes: Meshes object to render. It can be batched. trace_name: name to label the trace with. subplot_idx: identifies the subplot, with 0 being the top left. ncols: the number of subplots per row. lighting: a Lighting object that specifies the Mesh3D lighting. """ mesh = join_meshes_as_scene(meshes) mesh = mesh.detach().cpu() verts = mesh.verts_packed() faces = mesh.faces_packed() # If mesh has vertex colors or face colors, use them # for figure, otherwise use plotly's default colors. verts_rgb = None faces_rgb = None if isinstance(mesh.textures, TexturesVertex): verts_rgb = mesh.textures.verts_features_packed() verts_rgb.clamp_(min=0.0, max=1.0) verts_rgb = torch.tensor(255.0) * verts_rgb if isinstance(mesh.textures, TexturesAtlas): atlas = mesh.textures.atlas_packed() # If K==1 if atlas.shape[1] == 1 and atlas.shape[3] == 3: faces_rgb = atlas[:, 0, 0] # Reposition the unused vertices to be "inside" the object # (i.e. they won't be visible in the plot). verts_used = torch.zeros((verts.shape[0], ), dtype=torch.bool) verts_used[torch.unique(faces)] = True verts_center = verts[verts_used].mean(0) verts[~verts_used] = verts_center row, col = subplot_idx // ncols + 1, subplot_idx % ncols + 1 fig.add_trace( go.Mesh3d( x=verts[:, 0], y=verts[:, 1], z=verts[:, 2], vertexcolor=verts_rgb, facecolor=faces_rgb, i=faces[:, 0], j=faces[:, 1], k=faces[:, 2], lighting=lighting, name=trace_name, ), row=row, col=col, ) # Access the current subplot's scene configuration plot_scene = "scene" + str(subplot_idx + 1) current_layout = fig["layout"][plot_scene] # update the bounds of the axes for the current trace max_expand = (verts.max(0)[0] - verts.min(0)[0]).max() _update_axes_bounds(verts_center, max_expand, current_layout)
def mesh(self): return join_meshes_as_scene( [model.mesh for _, model in self.models.items()])
for k in range(K): c = colors[mesh_name].clone() c[..., 3] = c[..., 3] * alpha[i, k] textures = TexturesVertex(verts_features=[c]) m = Meshes(verts=[verts[mesh_name].clone()], faces=[faces[mesh_name].clone()], textures=textures) #m = meshes[mesh_name].clone().detach().to(device) t = Translate(y=hw[i, k, 0], x=hw[i, k, 1], z=torch.zeros(1, device=device), device=str(device)) m = m.update_padded(t.transform_points(m.verts_padded())) scene += [m] teapot_mesh = join_meshes_as_scene(scene) #for i, (name, particles) in particles_per_mesh: #textures = TexturesVertex(verts_features=colors) # meshes = Meshes( # verts=verts, # faces=faces, # textures=textures, # ) # model_transforms = torch.zeros(len(models), 3) # model_transforms[models['red_paddle']] = torch.tensor([0.0, 50.0, 0.0]) # # t = Translate(model_transforms).to(device)
green = white * torch.tensor([0.0, 1.0, 0.0]) blue = white * torch.tensor([0.0, 0.0, 1.0]) def height(z): v = verts.clone() v[:, 2] = torch.full((4, ), fill_value=z) return v meshes = Meshes(verts=[height(1.0), height(0.0), height(-1.0)], faces=[faces, faces, faces], textures=TexturesVertex([red, green, blue])) scene = join_meshes_as_scene(meshes) class IdentityShader(nn.Module): """ shader that simply returns the raw texels """ def __init__(self): super().__init__() def forward(self, fragments, meshes, **kwargs) -> torch.Tensor: texels = meshes.sample_textures(fragments) return texels cameras = FoVOrthographicCameras(device=device,