def __init__(self, base): # Load texture tex = Loader(base).loadTexture((Path(path.realpath(__file__)).parent.parent.parent / "res/images/checkerboard.png").absolute()) tex.setMagfilter(SamplerState.FT_nearest) tex.setMinfilter(SamplerState.FT_nearest) # Set up vertex data vdata = GeomVertexData("floor_data", GeomVertexFormat.get_v3t2(), Geom.UHStatic) vdata.setNumRows(6) vertex = GeomVertexWriter(vdata, "vertex") texcoord = GeomVertexWriter(vdata, "texcoord") vertex.addData3(-5, -5, 0) texcoord.addData3(0, 0, 0) vertex.addData3(-5, 5, 0) texcoord.addData3(0, 10, 0) vertex.addData3(5, 5, 0) texcoord.addData3(10, 10, 0) vertex.addData3(5, 5, 0) texcoord.addData3(10, 10, 0) vertex.addData3(5, -5, 0) texcoord.addData3(10, 0, 0) vertex.addData3(-5, -5, 0) texcoord.addData3(0, 0, 0) # Create primitive prim = GeomTriangles(Geom.UHStatic) prim.addVertices(0, 1, 2) prim.addVertices(3, 4, 5) geom = Geom(vdata) geom.add_primitive(prim) # Initialize geometry node GeomNode.__init__(self, "floor") attrib = TextureAttrib.make(tex) state = RenderState.make(attrib) self.addGeom(geom, state)
def gen_geom(self, mesh_json): # Create vertex format geom_array = GeomVertexArrayFormat() geom_array.add_column("vertex", 3, Geom.NTFloat32, Geom.CPoint) has_normals = False has_texcoords = False has_weights = False if "normals" in mesh_json: geom_array.add_column("normal", 3, Geom.NTFloat32, Geom.CNormal) has_normals = True if "texcoords" in mesh_json: geom_array.add_column("texcoord", 3, Geom.NTFloat32, Geom.CTexcoord) has_texcoords = True if "weights" in mesh_json: geom_array.add_column("joint", 4, Geom.NTUint8, Geom.CIndex) geom_array.add_column("weight", 4, Geom.NTFloat32, Geom.COther) has_weights = True geom_format = GeomVertexFormat() geom_format.add_array(geom_array) geom_format = GeomVertexFormat.register_format(geom_format) # Set up vertex data vdata = GeomVertexData( str(random.randint(0, 255)) + "_vdata", geom_format, Geom.UHStatic) vcount = len(mesh_json["vertices"]) // 3 vdata.setNumRows(vcount) vertex = GeomVertexWriter(vdata, "vertex") for i in range(vcount): vertex.addData3(mesh_json["vertices"][3 * i], mesh_json["vertices"][3 * i + 1], mesh_json["vertices"][3 * i + 2]) if has_normals: normal = GeomVertexWriter(vdata, "normal") for i in range(vcount): normal.addData3(mesh_json["normals"][3 * i], mesh_json["normals"][3 * i + 1], mesh_json["normals"][3 * i + 2]) if has_texcoords: texcoord = GeomVertexWriter(vdata, "texcoord") for i in range(vcount): texcoord.addData2(mesh_json["texcoords"][2 * i], mesh_json["texcoords"][2 * i + 1]) if has_weights: joint = GeomVertexWriter(vdata, "joint") weight = GeomVertexWriter(vdata, "weight") for i in range(vcount): joint_count = len(mesh_json["joints"][i]) joint.addData4( 0 if joint_count < 1 else mesh_json["joints"][i][0], 0 if joint_count < 2 else mesh_json["joints"][i][1], 0 if joint_count < 3 else mesh_json["joints"][i][2], 0 if joint_count < 4 else mesh_json["joints"][i][3]) weight.addData4( 0 if joint_count < 1 else mesh_json["weights"][i][0], 0 if joint_count < 2 else mesh_json["weights"][i][1], 0 if joint_count < 3 else mesh_json["weights"][i][2], 0 if joint_count < 4 else mesh_json["weights"][i][3]) # Create primitive prim = GeomTriangles(Geom.UHStatic) for i in range(vcount // 3): prim.addVertices(3 * i, 3 * i + 1, 3 * i + 2) geom = Geom(vdata) geom.add_primitive(prim) # Load texture tex = None if "texture" in mesh_json: tex = Loader(EComponent.base).loadTexture( (Path("resources") / mesh_json["texture"]).absolute()) tex.setMagfilter(SamplerState.FT_nearest) tex.setMinfilter(SamplerState.FT_nearest) # Create new geometry node geom_node = GeomNode(str(random.randint(0, 255)) + "_node") if tex is None: geom_node.addGeom(geom) else: attrib = TextureAttrib.make(tex) state = RenderState.make(attrib) geom_node.addGeom(geom, state) if EComponent.panda_root_node is not None: self.geom_path = EComponent.panda_root_node.attach_new_node( geom_node) self.geom_path.set_shader_input("object_id", self.object_id) # Set shader if has_weights and self.geom_path is not None: self.geom_path.setTag("shader type", "skinned") bone_mats = PTA_LMatrix4f() for _ in range(100): bone_mats.push_back(helper.np_mat4_to_panda(np.identity(4))) self.geom_path.set_shader_input(f"boneMats", bone_mats) # Disable culling self.geom_path.node().setBounds(OmniBoundingVolume()) self.geom_path.node().setFinal(True)