def _make_fullscreen_tri(self): """ Creates the oversized triangle used for rendering """ vformat = GeomVertexFormat.get_v3() vdata = GeomVertexData("vertices", vformat, Geom.UH_static) vdata.set_num_rows(3) vwriter = GeomVertexWriter(vdata, "vertex") vwriter.add_data3f(-1, 0, -1) vwriter.add_data3f(3, 0, -1) vwriter.add_data3f(-1, 0, 3) gtris = GeomTriangles(Geom.UH_static) gtris.add_next_vertices(3) geom = Geom(vdata) geom.add_primitive(gtris) geom_node = GeomNode("gn") geom_node.add_geom(geom) geom_node.set_final(True) geom_node.set_bounds(OmniBoundingVolume()) tri = NodePath(geom_node) tri.set_depth_test(False) tri.set_depth_write(False) tri.set_attrib(TransparencyAttrib.make(TransparencyAttrib.M_none), 10000) tri.set_color(Vec4(1)) tri.set_bin("unsorted", 10) tri.reparent_to(self._node) self._tri = tri
def make_grid(num_ticks=10, step=1.0): """Make a grid geometry. Keyword Arguments: step {float} -- step in meters (default: {1.0}) num_ticks {int} -- ticks number per axis (default: {5}) Returns: Geom -- p3d geometry """ ticks = np.arange(-num_ticks // 2, num_ticks // 2 + 1) * step vformat = GeomVertexFormat.get_v3() vdata = GeomVertexData('vdata', vformat, Geom.UHStatic) vdata.uncleanSetNumRows(len(ticks) * 4) vertex = GeomVertexWriter(vdata, 'vertex') for t in ticks: vertex.addData3(t, ticks[0], 0) vertex.addData3(t, ticks[-1], 0) vertex.addData3(ticks[0], t, 0) vertex.addData3(ticks[-1], t, 0) prim = GeomLines(Geom.UHStatic) prim.addNextVertices(len(ticks) * 4) geom = Geom(vdata) geom.addPrimitive(prim) return geom
def create_hidden_area_mesh(self, mask): """ Using the provided mask configuration, create the mesh that will cover the area not visible from the HMD """ gvf = GeomVertexFormat.get_v3() gvd = GeomVertexData('gvd', gvf, Geom.UH_static) geom = Geom(gvd) gvw = GeomVertexWriter(gvd, InternalName.get_vertex()) for i in range(mask.unTriangleCount * 3): vertex = mask.pVertexData[i] # The clip space in Panda3D has [-1, 1] coordinates, the received coordinates are in [0, 1] gvw.add_data3(vertex[0] * 2 - 1, vertex[1] * 2 - 1, -1) prim = GeomTriangles(Geom.UH_static) for i in range(mask.unTriangleCount): prim.add_vertices(i * 3, i * 3 + 1, i * 3 + 2) geom.add_primitive(prim) node = GeomNode('hidden-area-mesh') node.add_geom(geom) return node
def make_points(vertices, colors=None, texture_coords=None, geom=None): """Make or update existing points set geometry. Arguments: root_path {str} -- path to the group's root node name {str} -- node name within a group vertices {list} -- point coordinates (and other data in a point cloud format) Keyword Arguments: colors {list} -- colors (default: {None}) texture_coords {list} -- texture coordinates (default: {None}) geom {Geom} -- geometry to update (default: {None}) Returns: Geom -- p3d geometry """ if not isinstance(vertices, np.ndarray): vertices = np.asarray(vertices, dtype=np.float32) if colors is not None: if not isinstance(colors, np.ndarray): colors = np.asarray(colors) if colors.dtype != np.uint8: colors = np.uint8(colors * 255) vertices = np.column_stack(( vertices.view(dtype=np.uint32).reshape(-1, 3), colors.view(dtype=np.uint32))) if texture_coords is not None: if not isinstance(texture_coords, np.ndarray): texture_coords = np.asarray(texture_coords) vertices = np.column_stack(( vertices.view(dtype=np.uint32).reshape(-1, 3), texture_coords.view(dtype=np.uint32).reshape(-1, 2))) data = vertices.tostring() if geom is None: if vertices.strides[0] == 12: vformat = GeomVertexFormat.get_v3() elif vertices.strides[0] == 16: vformat = GeomVertexFormat.get_v3c4() elif vertices.strides[0] == 20: vformat = GeomVertexFormat.get_v3t2() else: raise ViewerError('Incompatible point clout format: {},{}'.format( vertices.dtype, vertices.shape)) vdata = GeomVertexData('vdata', vformat, Geom.UHDynamic) vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = GeomPoints(Geom.UHDynamic) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() geom = Geom(vdata) geom.add_primitive(prim) else: vdata = geom.modify_vertex_data() vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = geom.modify_primitive(0) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() return geom
def __make_points(vertices, colours=None, geom: Geom = None) -> Geom: """ This function is largely inspired by panda3d_viewer's implementation. Copyright (c) 2020, Igor Kalevatykh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ if not isinstance(vertices, np.ndarray): vertices = np.asarray(vertices, dtype=np.float32) if colours is not None: if not isinstance(colours, np.ndarray): colours = np.asarray(colours) if colours.dtype != np.uint8: colours = np.uint8(colours * 255) vertices = np.column_stack((vertices.view(dtype=np.uint32).reshape(-1, 3), colours.view(dtype=np.uint32))) data = vertices.tostring() if geom is None: if vertices.strides[0] == 12: vformat = GeomVertexFormat.get_v3() elif vertices.strides[0] == 16: vformat = GeomVertexFormat.get_v3c4() else: raise ValueError('Incompatible point clout format: {},{}'.format(vertices.dtype, vertices.shape)) vdata = GeomVertexData('vdata', vformat, Geom.UHDynamic) vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = GeomPoints(Geom.UHDynamic) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() geom = Geom(vdata) geom.add_primitive(prim) else: vdata = geom.modify_vertex_data() vdata.unclean_set_num_rows(len(vertices)) vdata.modify_array_handle(0).set_subdata(0, len(data), data) prim = geom.modify_primitive(0) prim.clear_vertices() prim.add_consecutive_vertices(0, len(vertices)) prim.close_primitive() return geom