def make_square(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, tex_len=None, tex_width=None): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x3, y3, z3) vertex.addData3(x4, y4, z4) D = Vec3(x1, y1, z1) C = Vec3(x2, y2, z2) B = Vec3(x3, y3, z3) A = Vec3(x4, y4, z4) normal_vec = (C - A).cross(D - B).normalize() normal.addData3(normal_vec) normal.addData3(normal_vec) normal.addData3(normal_vec) normal.addData3(normal_vec) side_len = math.sqrt((x3 - x1)**2 + (y3 - y1)**2) start_width = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) end_width = math.sqrt((x4 - x3)**2 + (y4 - y3)**2) texcoord.addData2f(0.0, (start_width / tex_width) if tex_width is not None else 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f((side_len / tex_len) if tex_len is not None else 1.0, 0.0) texcoord.addData2f( (side_len / tex_len) if tex_len is not None else 1.0, (end_width / tex_width) if tex_width is not None else 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 2) tris.addVertices(0, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def __init__(self, name): self.name = name self.poly_groups = [] # GeomVertexFormats # # GeomVertexFormat.getV3cpt2() - vertex, color, uv # GeomVertexFormat.getV3t2() - vertex, uv # GeomVertexFormat.getV3cp() - vertex, color # GeomVertexFormat.getV3n3t2() - vertex, normal, uv # GeomVertexFormat.getV3n3cpt2()- vertex, normal, rgba, uv # textured self.vdata = GeomVertexData(name, GeomVertexFormat.getV3n3cpt2(), Geom.UHStatic) # plain color filled polys # self.vdata = GeomVertexData(name, GeomVertexFormat.getV3cp(), Geom.UHStatic) self.vertex = GeomVertexWriter(self.vdata, 'vertex') self.vnormal = GeomVertexWriter(self.vdata, 'normal') self.color = GeomVertexWriter(self.vdata, 'color') self.texcoord = GeomVertexWriter(self.vdata, 'texcoord') self.root = NodePath(PandaNode(name + '_mesh'))
def makeSquare(x1, y1, z1, x2, y2, z2): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility # color.addData4f(1.0, 0.0, 0.0, 1.0) # color.addData4f(0.0, 1.0, 0.0, 1.0) # color.addData4f(0.0, 0.0, 1.0, 1.0) # color.addData4f(1.0, 0.0, 1.0, 1.0) color.addData4f(1.0, 1.0, 1.0, 1.0) color.addData4f(1.0, 1.0, 1.0, 1.0) color.addData4f(1.0, 1.0, 1.0, 1.0) color.addData4f(1.0, 1.0, 1.0, 1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def makeSquare(x1, y1, z1, x2, y2, z2): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) #initializing variables vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') #gotta draw the sqaure in the right plane #aligning vertexes to the right planes if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: #adding vertexes vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) #adding different colors to the vertex for visibility color.addData4f(1.0, 0.0, 0.0, 1.0) color.addData4f(0.0, 1.0, 0.0, 1.0) color.addData4f(0.0, 0.0, 1.0, 1.0) color.addData4f(1.0, 0.0, 1.0, 1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) #quads aren't directly supported by the geom interface tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) #initialize surface square = Geom(vdata) square.addPrimitive(tris) return square
def __init__(self): formatArray = GeomVertexArrayFormat() formatArray.addColumn( InternalName.make("drawFlag"), 1, Geom.NTUint8, Geom.COther) format = GeomVertexFormat(GeomVertexFormat.getV3n3cpt2()) format.addArray(formatArray) self.format = GeomVertexFormat.registerFormat(format) # bodydata = GeomVertexData("body vertices", format, Geom.UHStatic) self.barkTexture = loader.loadTexture("../asset/TreeGenerator/barkTexture.jpg")
def __init__(self, node: GeomNode, node_sea: GeomNode, map_dim: Tuple[int, int]): # self.v_data = GeomVertexData('hexmap', GeomVertexFormat.getV3n3cpt2(), Geom.UHStatic) self.map_point_mngr = MapPointMngr.instance() # self.map_point_mngr.set_v_data(self.v_data) self.map_point_mngr.set_map_dim(map_dim) self.map_dim = map_dim self.map: List[List[Hexagon]] = [] self.hex_rad = 1 self.hex_width = sqrt(3) * self.hex_rad self.hex_height = 2 * self.hex_rad self.geometries_v_data = [] self.nodes = [] self.connectable = [] chunk = -1 for j in range(self.map_dim[1]): for i in range(self.map_dim[0]): if i % HexMap.chunksize_x == 0: chunk += 1 v = GeomVertexData('hexmap', GeomVertexFormat.getV3n3cpt2(), Geom.UHStatic) self.geometries_v_data.append(v) self.map_point_mngr.set_v_data(v) self.map.append([]) x_center = i * self.hex_width + (j & 1) * self.hex_width / 2 y_center = j * self.hex_height * 3 / 4 ref_lvl = 3 if (i > 0 and i < self.map_dim[0] - 1) and (j > 0 and j < self.map_dim[1] - 1) else 1 # ref_lvl = 5 center = Vec3(x_center, y_center, 0) h = Hexagon(center, self.hex_width, self.hex_height, i, j, refinement_level=ref_lvl) if ref_lvl > 1: self.connectable.append(h) self.map[chunk].append(h) self.map_point_mngr.set_v_data(self.geometries_v_data[1]) self.connectable[0].connect(self.connectable[1]) self.connectable[1].connect(self.connectable[0]) self.geometries = [] for j in range(self.map_dim[1]): for c in range(ceil(self.map_dim[0] / HexMap.chunksize_x)): idx = c + j * ceil(self.map_dim[0] / HexMap.chunksize_x) g = Geom(self.geometries_v_data[idx]) self.geometries.append(g) for hexagon in self.map[idx]: for triangle in hexagon.triangulation: g.addPrimitive(triangle) if j == 0 or j == self.map_dim[1] - 1: node_sea.addGeom(g) else: node.addGeom(g) print(f"Node has {node.getNumGeoms()} geos")
def makeSquare(x1, y1, z1, x2, y2, z2): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') # color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility # color.addData4f(1.0, 0.0, 0.0, 1.0) # color.addData4f(0.0, 1.0, 0.0, 1.0) # color.addData4f(0.0, 0.0, 1.0, 1.0) # color.addData4f(1.0, 0.0, 1.0, 1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def Square(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('cube', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') ################################ # # FACE 1 # ################################ vert1 = LVector3(self.pos.getX(), self.pos.getY(), self.pos.getZ()) vert2 = LVector3(vert1.getX() + self.len, vert1.getY(), vert1.getZ()) vert3 = LVector3(vert2.getX(), vert2.getY() + self.wid, vert2.getZ()) vert4 = LVector3(vert1.getX(), vert1.getY() + self.wid, vert1.getZ()) vertex.addData3(vert1) vertex.addData3(vert2) vertex.addData3(vert3) vertex.addData3(vert4) normal = GeomVertexWriter(vdata, 'normal') norm = (vert4 - vert1).cross(vert2 - vert1) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color = GeomVertexWriter(vdata, 'color') color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord = GeomVertexWriter(vdata, 'texcoord') texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 3, 1) tris.addVertices(1, 3, 2) square = Geom(vdata) square.addPrimitive(tris) self.prim = square self.model = GeomNode(self.name) self.model.addGeom(self.prim)
def __init__(self): formatArray = GeomVertexArrayFormat() formatArray.addColumn(InternalName.make("drawFlag"), 1, Geom.NTUint8, Geom.COther) format = GeomVertexFormat(GeomVertexFormat.getV3n3cpt2()) format.addArray(formatArray) self.format = GeomVertexFormat.registerFormat(format) bodydata = GeomVertexData("body vertices", format, Geom.UHStatic) self.barkTexture = loader.loadTexture("barkTexture.jpg") treeNodePath = NodePath("Tree Holder") makeFractalTree(bodydata, treeNodePath, LVector3(4, 4, 7)) treeNodePath.setTexture(self.barkTexture, 1) treeNodePath.reparentTo(render) self.accept("q", self.regenTree) self.accept("w", self.addTree) self.accept("arrow_up", self.upIterations) self.accept("arrow_down", self.downIterations) self.accept("arrow_right", self.upCopies) self.accept("arrow_left", self.downCopies) self.numIterations = 11 self.numCopies = 4 self.upDownEvent = OnscreenText( text="Up/Down: Increase/Decrease the number of iterations (" + str(self.numIterations) + ")", parent=base.a2dTopLeft, align=TextNode.ALeft, style=1, fg=(1, 1, 1, 1), pos=(0.06, -0.22), scale=.05, mayChange=True) self.leftRightEvent = OnscreenText( text="Left/Right: Increase/Decrease branching (" + str(self.numCopies) + ")", parent=base.a2dTopLeft, align=TextNode.ALeft, style=1, fg=(1, 1, 1, 1), pos=(0.06, -0.28), scale=.05, mayChange=True)
def createTetraeder(self, iterations): ''' Note that the first and the last node of the Thetha_Range should be 0 and 180 Degrees because this will be just one time added to verticies ''' #Format format = GeomVertexFormat.getV3n3cpt2() #VertexData vdata = GeomVertexData('name', format, Geom.UHDynamic) ##VertexWriter vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') vertex.addData3f(0,1.434,-0.507) vertex.addData3f(-1.242,-0.717,-0.507) vertex.addData3f(1.242,-0.717,-0.507) vertex.addData3f(0,0,1.521) color.addData4f(1,1,1,1) color.addData4f(1,1,1,1) color.addData4f(1,1,1,1) color.addData4f(1,1,1,1) normal.addData3f(0,1.434,-0.507) normal.addData3f(-1.242,-0.717,-0.507) normal.addData3f(1.242,-0.717,-0.507) normal.addData3f(0,0,1.521) ### Create Geom geom = Geom(vdata) ### Create Primitives prim = GeomTriangles(Geom.UHStatic) prim.addVertices(0, 1, 2) prim.addVertices(0, 1, 3) prim.addVertices(1, 2, 3) prim.addVertices(2, 0, 3) prim.closePrimitive() geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) return node
def make_cube_geom(self, pos=(0, 0, 0), geom_color=(1, 1, 1, 0.5), faces_no_draw=(0, 0, 0, 0, 0, 0)): format = GeomVertexFormat.getV3n3cpt2() shift_x, shift_y, shift_z = pos cube_vertex_list = deepcopy(cube_data["vertexPositions"]) for vert in cube_vertex_list: vert[0] += shift_x vert[1] += shift_y vert[2] += shift_z vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') test_textcoords = [] # define available vertexes here [vertex.addData3(*v) for v in cube_vertex_list] [normal.addData3(*n) for n in cube_data["vertexNormals"]] [color.addData4f(*geom_color) for _ in cube_vertex_list] [texcoord.addData2f(*t) for t in texture_mapping] """ CREATE A NEW PRIMITIVE """ prim = GeomTriangles(Geom.UHStatic) # convert from numpy arr mapping to normal faces mapping... excluded_normals = [ normal_face_mapping[self.dict_mapping[i]] for i in range(len(faces_no_draw)) if faces_no_draw[i] == 1 ] # only use acceptable indices indices = [ ind for ind in cube_data['indices'] if cube_data['vertexNormals'][ind] not in excluded_normals ] [prim.addVertex(v) for v in indices] # N.B: this must correspond to a vertex defined in vdata geom = Geom(vdata) # create a new geometry geom.addPrimitive(prim) # add the primitive to the geometry return geom
def reconstruct(self): trianglator = Triangulator() #Add vertices to the trianglator for vertex in self.vertices: trianglator.addPolygonVertex(trianglator.addVertex(vertex)) trianglator.triangulate() #Prepare to create the primative self.vdata = GeomVertexData('floor', GeomVertexFormat.getV3n3cpt2(), Geom.UHStatic) vertexW = GeomVertexWriter(self.vdata, 'vertex') normalW = GeomVertexWriter(self.vdata, 'normal') colorW = GeomVertexWriter(self.vdata, 'color') texcoordW = GeomVertexWriter(self.vdata, 'texcoord') #Add vertices to the primative i = 0 while i < trianglator.getNumVertices(): vertex = trianglator.getVertex(i) vertexW.addData3f(vertex.x, vertex.y, 0.0) normalW.addData3f(0, 0, 1) colorW.addData4f(0.1, 0.1, 0.1, 0.5) texcoordW.addData2f(0.0, 1.0) i += 1 self.geom = Geom(self.vdata) #Add triangles to the primative i = 0 print(trianglator.getNumTriangles()) while i < trianglator.getNumTriangles(): tri = GeomTriangles(Geom.UHStatic) tri.addVertices(trianglator.getTriangleV0(i), trianglator.getTriangleV1(i), trianglator.getTriangleV2(i)) tri.closePrimitive() self.geom.addPrimitive(tri) i += 1 self.addGeom(self.geom)
def __init__(self): formatArray = GeomVertexArrayFormat() formatArray.addColumn( InternalName.make("drawFlag"), 1, Geom.NTUint8, Geom.COther) format = GeomVertexFormat(GeomVertexFormat.getV3n3cpt2()) format.addArray(formatArray) self.format = GeomVertexFormat.registerFormat(format) bodydata = GeomVertexData("body vertices", format, Geom.UHStatic) self.barkTexture = loader.loadTexture("barkTexture.jpg") treeNodePath = NodePath("Tree Holder") makeFractalTree(bodydata, treeNodePath, LVector3(4, 4, 7)) treeNodePath.setTexture(self.barkTexture, 1) treeNodePath.reparentTo(render) self.accept("q", self.regenTree) self.accept("w", self.addTree) self.accept("arrow_up", self.upIterations) self.accept("arrow_down", self.downIterations) self.accept("arrow_right", self.upCopies) self.accept("arrow_left", self.downCopies) self.numIterations = 11 self.numCopies = 4 self.upDownEvent = OnscreenText( text="Up/Down: Increase/Decrease the number of iterations (" + str( self.numIterations) + ")", parent=base.a2dTopLeft, align=TextNode.ALeft, style=1, fg=(1, 1, 1, 1), pos=(0.06, -0.22), scale=.05, mayChange=True) self.leftRightEvent = OnscreenText( text="Left/Right: Increase/Decrease branching (" + str( self.numCopies) + ")", parent=base.a2dTopLeft, align=TextNode.ALeft, style=1, fg=(1, 1, 1, 1), pos=(0.06, -0.28), scale=.05, mayChange=True)
def makeSquare(face, rhs=True): format=GeomVertexFormat.getV3n3cpt2() format=GeomVertexFormat.getV3n3t2() vdata=GeomVertexData('square', format, Geom.UHStatic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') #color=GeomVertexWriter(vdata, 'color') texcoord=GeomVertexWriter(vdata, 'texcoord') if not rhs: face = list(reversed(face)) normalvec = (face[1]-face[0]).cross(face[2]-face[0]) normalvec.normalize() f = 0.9 if rhs else 0.8 f = 1.0 for ver in face: vertex.addData3f(ver*f) normal.addData3f(normalvec) #color.addData3f((ver+1.0+2.0)*0.25) #color.addData3f(ver*0.0+1.0) if not normalvec.z: texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) texcoord.addData2f(0.0, 1.0) tri=GeomTriangles(Geom.UHStatic) tri.addVertices(0, 1, 2) tri.addVertices(2, 3, 0) tri.closePrimitive() square=Geom(vdata) square.addPrimitive(tri) return square
def reconstruct(self): trianglator = Triangulator() #Add vertices to the trianglator for vertex in self.vertices: trianglator.addPolygonVertex(trianglator.addVertex(vertex)) trianglator.triangulate() #Prepare to create the primative self.vdata = GeomVertexData('floor', GeomVertexFormat.getV3n3cpt2(), Geom.UHStatic) vertexW = GeomVertexWriter(self.vdata, 'vertex') normalW = GeomVertexWriter(self.vdata, 'normal') colorW = GeomVertexWriter(self.vdata, 'color') texcoordW = GeomVertexWriter(self.vdata, 'texcoord') #Add vertices to the primative i = 0 while i < trianglator.getNumVertices(): vertex = trianglator.getVertex(i) vertexW.addData3f(vertex.x,vertex.y,0.0) normalW.addData3f(0,0,1) colorW.addData4f(0.1,0.1,0.1,0.5) texcoordW.addData2f(0.0, 1.0) i+=1 self.geom = Geom(self.vdata) #Add triangles to the primative i = 0 print(trianglator.getNumTriangles()) while i < trianglator.getNumTriangles(): tri = GeomTriangles(Geom.UHStatic) tri.addVertices(trianglator.getTriangleV0(i),trianglator.getTriangleV1(i),trianglator.getTriangleV2(i)) tri.closePrimitive() self.geom.addPrimitive(tri) i+=1 self.addGeom(self.geom)
def make_cube_geom(pos=(0, 0, 0), geom_color=(1, 1, 1, 1)): format = GeomVertexFormat.getV3n3cpt2() shift_x, shift_y, shift_z = pos cube_vertex_list = deepcopy(cube_data["vertexPositions"]) for vert in cube_vertex_list: # vert[0] *= scale # vert[1] *= scale # vert[2] *= scale vert[0] += shift_x vert[1] += shift_y vert[2] += shift_z vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # define available vertexes here [normal.addData3(*n) for n in cube_data["vertexNormals"]] [vertex.addData3(*v) for v in cube_vertex_list] [color.addData4f(*geom_color) for _ in cube_vertex_list] [texcoord.addData2f(1, 1) for _ in cube_vertex_list] """ CREATE A NEW PRIMITIVE """ prim = GeomTriangles(Geom.UHStatic) # [prim.addVertex(v) for v in vertexes] indices = [ind for ind in cube_data['indices']] [prim.addVertex(v) for v in indices] # N.B: this must correspond to a vertex defined in vdata geom = Geom(vdata) # create a new geometry geom.addPrimitive(prim) # add the primitive to the geometry return geom
def __init__(self, name): self.name = name self.poly_groups = [] # GeomVertexFormats # # GeomVertexFormat.getV3cpt2() - vertex, color, uv # GeomVertexFormat.getV3t2() - vertex, uv # GeomVertexFormat.getV3cp() - vertex, color # GeomVertexFormat.getV3n3t2() - vertex, normal, uv # GeomVertexFormat.getV3n3cpt2()- vertex, normal, rgba, uv # textured self.vdata = GeomVertexData(name, GeomVertexFormat.getV3n3cpt2(), Geom.UHStatic) # plain color filled polys # self.vdata = GeomVertexData(name, GeomVertexFormat.getV3cp(), Geom.UHStatic) self.vertex = GeomVertexWriter(self.vdata, 'vertex') self.vnormal = GeomVertexWriter(self.vdata, 'normal') self.color = GeomVertexWriter(self.vdata, 'color') self.texcoord = GeomVertexWriter(self.vdata, 'texcoord') self.root = NodePath(PandaNode(name+'_mesh'))
x = random.randint(sx, ex) y = random.randint(sy, ey) z = self.world.map3d[x, y] if 0 < z < self.config.low_mount_level[1]: res[tree].append((x, y, z)) self[item] = res return res # Shit for f*****g trees formatArray = GeomVertexArrayFormat() formatArray.addColumn(InternalName.make("drawFlag"), 1, Geom.NTUint8, Geom.COther) treeform = GeomVertexFormat(GeomVertexFormat.getV3n3cpt2()) treeform.addArray(formatArray) treeform = GeomVertexFormat.registerFormat(treeform) # this draws the body of the tree. This draws a ring of vertices and connects the rings with # triangles to form the body. # this keepDrawing paramter tells the function wheter or not we're at an end # if the vertices before you were an end, dont draw branches to it def draw_body(nodePath, vdata, pos, vecList, radius=1, keepDrawing=True, numVertices=3): circleGeom = Geom(vdata) vertWriter = GeomVertexWriter(vdata, "vertex") colorWriter = GeomVertexWriter(vdata, "color") normalWriter = GeomVertexWriter(vdata, "normal")
def make_circle(x1, y1, z1, x2, y2, z2, inner_radius, end_inner_radius, arc, new_z1, new_z2): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('circle', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') ccw = arc < 0 arc = -arc if ccw else arc num_parts = 2 * (arc / 360)**-1 num_draw = round(num_parts * (arc / 360)) x3 = x1 - x2 y3 = y1 - y2 z3 = z1 - z2 x3, y3, z3 = normalized(x3, y3, z3) print(x3, y3) theta_offset = math.atan2(y1 - y2, x1 - x2) orig_radius = math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2) cur_x1 = 0 cur_y1 = 0 cur_z1 = 0 cur_x2 = 0 cur_y2 = 0 cur_z2 = 0 z1_diff = new_z1 - z1 z2_diff = new_z2 - z2 inner_radius_diff = end_inner_radius - inner_radius x1_interp = x1 + (inner_radius * x3) y1_interp = y1 + (inner_radius * y3) z1_interp = z1 + (inner_radius * z3) z2_interp = z2 + (inner_radius * z3) print(x1_interp, y1_interp) print(theta_offset) for i in range(num_draw + 1): theta = ((2 * -math.pi * i / num_parts) + math.pi) % (2 * math.pi) interp = (i / num_draw) inner_radius_interp = inner_radius + (inner_radius_diff * interp) # inner_radius_interp = inner_radius radius = orig_radius + inner_radius_interp x_outer = math.cos((-theta if ccw else theta) + theta_offset) * radius y_outer = math.sin((-theta if ccw else theta) + theta_offset) * radius x_inner = math.cos((-theta if ccw else theta) + theta_offset) * inner_radius_interp y_inner = math.sin((-theta if ccw else theta) + theta_offset) * inner_radius_interp print(theta, x_inner + x1_interp, y_inner + y1_interp, inner_radius_interp, theta + theta_offset) vertex.addData3(x1_interp + x_inner, y1_interp + y_inner, z1_interp + (z1_diff * interp)) cur_x1, cur_y1, cur_z1 = x1_interp + x_inner, y1_interp + y_inner, z1_interp + ( z1_diff * interp) normal.addData3( normalized(2 * (x1_interp + x_inner) - 1, 2 * (y1_interp + y_inner) - 1, 2 * (z1_interp + (z1_diff * interp)) - 1)) texcoord.addData2f( ((i / num_parts) * 2 * math.pi * inner_radius_interp) / 13, 1) vertex.addData3(x1_interp + x_outer, y1_interp + y_outer, z2_interp + (z2_diff * interp)) cur_x2, cur_y2, cur_z2 = x1_interp + x_outer, y1_interp + y_outer, z2_interp + ( z2_diff * interp) normal.addData3( normalized(2 * (x1_interp + x_outer) - 1, 2 * (y1_interp + y_outer) - 1, 2 * (z2_interp + (z2_diff * interp)) - 1)) texcoord.addData2f(((i / num_parts) * 2 * math.pi * radius) / 13, 0) tris = GeomTriangles(Geom.UHDynamic) for i in range(num_draw * 2): tris.addVertices(0 + i, 1 + i, 2 + i) circle = Geom(vdata) circle.addPrimitive(tris) return circle, cur_x1, cur_y1, cur_z1, cur_x2, cur_y2, cur_z2
def makeSquare(x1, y1, z1, x2, y2, z2, color): # This is straight copied off the Panda3d 'procedural cube' example https://github.com/panda3d/panda3d/blob/master/samples/procedural-cube/main.py#L11 format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color_setter = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility if np.sum(color) > 0: a = 1 # Alpha transparency else: a = 0 # Handling channel size 1 or 3 if np.size(color) == 1: for i in range(4): # Have to run this 4 times cuz each vertex on the square has a color setting color_setter.addData2f(color,a) elif np.size(color) == 3: r, g, b = color[0], color[1], color[2] for i in range(4): color_setter.addData4f(r,g,b,a) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def make_square(sq_color): # sq_color is a list of tuples, describing each vertex: # (r, g, b, a) for [bl, br, tr, tl] x1 = -1 y1 = -1 z1 = -1 x2 = 1 y2 = -1 z2 = 1 v_format = GeomVertexFormat.getV3n3cpt2() v_data = GeomVertexData('square', v_format, Geom.UHDynamic) vertex = GeomVertexWriter(v_data, 'vertex') normal = GeomVertexWriter(v_data, 'normal') color = GeomVertexWriter(v_data, 'color') tex_coord = GeomVertexWriter(v_data, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility # color.addData4f(1.0, 0.0, 0.0, 1.0) # color.addData4f(0.0, 1.0, 0.0, 1.0) # color.addData4f(0.0, 0.0, 1.0, 1.0) # color.addData4f(1.0, 0.0, 1.0, 1.0) color.addData4f(sq_color[0]) # (0, 0) bottom left color.addData4f(sq_color[1]) # (0.5, 0) bottom right color.addData4f(sq_color[2]) # (0.5, 0.5) top right color.addData4f(sq_color[3]) # (0, 0.5) top left tex_coord.addData2f(0.0, 1.0) tex_coord.addData2f(0.0, 0.0) tex_coord.addData2f(1.0, 0.0) tex_coord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(v_data) square.addPrimitive(tris) return square
def make_circle(x1, y1, z1, x2, y2, z2, inner_radius, end_inner_radius, arc, new_z1, new_z2, tex_len=None, tex_width=None, end_radius=None, rev=False): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('circle', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') ccw = arc < 0 arc = -arc if ccw else arc num_parts = 50 * (arc / 360)**-1 num_draw = round(num_parts * (arc / 360)) x3 = x1 - x2 y3 = y1 - y2 z3 = z1 - z2 x3, y3, z3 = normalized(x3, y3, z3) theta_offset = math.atan2(y1 - y2, x1 - x2) orig_radius = math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2) cur_x1 = x1 cur_y1 = y1 cur_z1 = z1 cur_x2 = x2 cur_y2 = y2 cur_z2 = z2 z1_diff = new_z1 - z1 z2_diff = new_z2 - z2 if end_radius is None: end_radius = orig_radius inner_radius_diff = end_inner_radius - inner_radius radius_diff = end_radius - orig_radius for i in range(num_draw + 1): theta = (math.pi - 2 * math.pi * (i / num_parts)) % (2 * math.pi) interp = (i / num_draw) inner_radius_interp = inner_radius + (inner_radius_diff * (i / num_parts)) radius = (orig_radius + (radius_diff * interp)) + inner_radius_interp radius_offset = 0 if rev: radius_offset = radius_diff * interp x1_interp = x1 + ((inner_radius + radius_offset) * x3) y1_interp = y1 + ((inner_radius + radius_offset) * y3) z1_interp = z1 + ((inner_radius + radius_offset) * z3) x_outer = (math.cos((-theta if ccw else theta) + theta_offset) * radius) + x1_interp y_outer = (math.sin((-theta if ccw else theta) + theta_offset) * radius) + y1_interp x_inner = (math.cos((-theta if ccw else theta) + theta_offset) * inner_radius_interp) + x1_interp y_inner = (math.sin((-theta if ccw else theta) + theta_offset) * inner_radius_interp) + y1_interp D = Vec3(cur_x1, cur_y1, cur_z1) C = Vec3(cur_x2, cur_y2, cur_z2) B = Vec3(x_inner, y_inner, z1_interp + (z1_diff * interp)) A = Vec3(x_outer, y_outer, z1_interp + (z2_diff * interp)) normal_vec = (C - A).cross(D - B).normalize() vertex.addData3(x_inner, y_inner, z1_interp + (z1_diff * interp)) cur_x1, cur_y1, cur_z1 = x_inner, y_inner, z1_interp + (z1_diff * interp) normal.addData3(normal_vec) texcoord.addData2f( (((i / num_parts) * 2 * math.pi * inner_radius_interp) / tex_len) if tex_len is not None else 1.0, (orig_radius / tex_width) if tex_width is not None else 1.0) vertex.addData3(x_outer, y_outer, z1_interp + (z2_diff * interp)) cur_x2, cur_y2, cur_z2 = x_outer, y_outer, z1_interp + (z2_diff * interp) normal.addData3(normal_vec) texcoord.addData2f((((i / num_parts) * 2 * math.pi * radius) / tex_len) if tex_len is not None else 1.0, 0) tris = GeomTriangles(Geom.UHDynamic) for i in range(num_draw * 2): tris.addVertices(0 + i, 1 + i, 2 + i) circle = Geom(vdata) circle.addPrimitive(tris) return circle, cur_x1, cur_y1, cur_z1, cur_x2, cur_y2, cur_z2
def Ramp(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') #The corner at Quadrant 2 on face 1 is the starting point of our rectangle ################################ # # FACE 1 # ################################ vert1 = LVector3(self.pos.getX(), self.pos.getY(), self.pos.getZ()) vert2 = LVector3(vert1.getX() + self.len, vert1.getY(), vert1.getZ()) vert3 = LVector3(vert2.getX(), vert2.getY() + self.wid, vert2.getZ()) vert4 = LVector3(vert1.getX(), vert1.getY() + self.wid, vert1.getZ()) vertex.addData3(vert1) vertex.addData3(vert2) vertex.addData3(vert3) vertex.addData3(vert4) normal = GeomVertexWriter(vdata, 'normal') norm = (vert4 - vert1).cross(vert2 - vert1) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color = GeomVertexWriter(vdata, 'color') color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord = GeomVertexWriter(vdata, 'texcoord') texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 3, 1) tris.addVertices(1, 3, 2) ##################################### # # FACE 2 # ##################################### vert5 = vert1 vert6 = vert2 vert7 = LVector3(vert3.getX(), vert3.getY(), vert3.getZ() + self.dep) vert8 = LVector3(vert4.getX(), vert4.getY(), vert4.getZ() + self.dep) vertex.addData3(vert5) vertex.addData3(vert6) vertex.addData3(vert7) vertex.addData3(vert8) norm = (vert8 - vert5).cross(vert6 - vert5) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(4, 7, 5) tris.addVertices(5, 7, 6) ######################################## # # FACE 3 # ######################################## vert9 = vert1 vert10 = vert2 vert11 = vert6 vert12 = vert5 vertex.addData3(vert9) vertex.addData3(vert10) vertex.addData3(vert11) vertex.addData3(vert12) norm = (vert12 - vert9).cross(vert10 - vert9) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(8, 11, 9) tris.addVertices(9, 11, 10) ############################################### # # FACE 4 # ############################################### vert13 = vert1 vert14 = vert4 vert15 = vert8 vert16 = vert5 vertex.addData3(vert13) vertex.addData3(vert14) vertex.addData3(vert15) vertex.addData3(vert16) norm = (vert16 - vert13).cross(vert14 - vert13) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(12, 15, 13) tris.addVertices(13, 15, 14) ############################################ # # FACE 5 # ############################################ vert17 = vert2 vert18 = vert3 vert19 = vert7 vert20 = vert6 vertex.addData3(vert17) vertex.addData3(vert18) vertex.addData3(vert19) vertex.addData3(vert20) norm = (vert20 - vert17).cross(vert18 - vert17) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(16, 19, 17) tris.addVertices(17, 19, 18) ramp = Geom(vdata) ramp.addPrimitive(tris) self.prim = ramp self.model = GeomNode(self.name) self.model.addGeom(self.prim)
def RingFaceGeometry(up, inner_radius, outer_radius, nbOfPoints): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('ring', format, Geom.UHStatic) vdata.unclean_set_num_rows(nbOfPoints) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') for i in range(nbOfPoints): angle = 2 * pi / nbOfPoints * i x = cos(angle) y = sin(angle) vertex.addData3f(outer_radius * x, outer_radius * y, 0) normal.addData3f(0, 0, up) color.addData4f(1, 1, 1, 1) texcoord.addData2f(1, 0) vertex.addData3f(inner_radius * x, inner_radius * y, 0) normal.addData3f(0, 0, up) color.addData4f(1, 1, 1, 1) texcoord.addData2f(0, 0) triangles = GeomTriangles(Geom.UHStatic) triangles.reserve_num_vertices(nbOfPoints - 1) for i in range(nbOfPoints - 1): if up < 0: triangles.addVertex(i * 2 + 0) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 2) triangles.closePrimitive() triangles.addVertex(i * 2 + 2) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 3) triangles.closePrimitive() else: triangles.addVertex(i * 2 + 2) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 0) triangles.closePrimitive() triangles.addVertex(i * 2 + 3) triangles.addVertex(i * 2 + 1) triangles.addVertex(i * 2 + 2) triangles.closePrimitive() if up < 0: triangles.addVertex((nbOfPoints - 1) * 2 + 0) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex(0) triangles.closePrimitive() triangles.addVertex(0) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex(1) triangles.closePrimitive() else: triangles.addVertex(0) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex((nbOfPoints - 1) * 2 + 0) triangles.closePrimitive() triangles.addVertex(1) triangles.addVertex((nbOfPoints - 1) * 2 + 1) triangles.addVertex(0) triangles.closePrimitive() geom = Geom(vdata) geom.addPrimitive(triangles) return geom
def Prism(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('cube', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') #################################### #First, we need to create the first face, and then base #the other faces on it. #################################### # # DIAGRAM OF A RECTANGULAR PRISM # |--------| # | Face 6 |<-Depth # |--------|--------|--------|--------| # | Face 4 | Face 2 | Face 3 | Face 1 | <-Width # | | | | | # |--------|--------|--------|--------| # | Face 5 | ^Length # |--------| #################################### #The corner at Quadrant 2 on face 1 is the starting point of our rectangle ################################ # # FACE 1 # ################################ vert1 = LVector3(self.pos.getX(), self.pos.getY(), self.pos.getZ()) vert2 = LVector3(vert1.getX() + self.len, vert1.getY(), vert1.getZ()) vert3 = LVector3(vert2.getX(), vert2.getY() + self.wid, vert2.getZ()) vert4 = LVector3(vert1.getX(), vert1.getY() + self.wid, vert1.getZ()) vertex.addData3(vert1) vertex.addData3(vert2) vertex.addData3(vert3) vertex.addData3(vert4) normal = GeomVertexWriter(vdata, 'normal') norm = (vert4 - vert1).cross(vert2 - vert1) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color = GeomVertexWriter(vdata, 'color') color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord = GeomVertexWriter(vdata, 'texcoord') texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 3, 1) tris.addVertices(1, 3, 2) ##################################### # # FACE 2 # ##################################### vert5 = LVector3(vert1.getX(), vert1.getY(), vert1.getZ() - self.dep) vert6 = LVector3(vert5.getX() + self.len, vert5.getY(), vert5.getZ()) vert7 = LVector3(vert6.getX(), vert6.getY() + self.wid, vert6.getZ()) vert8 = LVector3(vert5.getX(), vert5.getY() + self.wid, vert5.getZ()) vertex.addData3(vert5) vertex.addData3(vert6) vertex.addData3(vert7) vertex.addData3(vert8) norm = (vert8 - vert5).cross(vert6 - vert5) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(4, 7, 5) tris.addVertices(5, 7, 6) ######################################## # # FACE 3 # ######################################## vert9 = vert1 vert10 = vert2 vert11 = vert6 vert12 = vert5 vertex.addData3(vert9) vertex.addData3(vert10) vertex.addData3(vert11) vertex.addData3(vert12) norm = (vert12 - vert9).cross(vert10 - vert9) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(8, 11, 9) tris.addVertices(9, 11, 10) ########################################## # # FACE 4 # ########################################### vert13 = vert4 vert14 = vert3 vert15 = vert7 vert16 = vert8 vertex.addData3(vert13) vertex.addData3(vert14) vertex.addData3(vert15) vertex.addData3(vert16) norm = (vert16 - vert13).cross(vert14 - vert13) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(12, 15, 13) tris.addVertices(13, 15, 14) square = Geom(vdata) square.addPrimitive(tris) ############################################### # # FACE 5 # ############################################### vert17 = vert1 vert18 = vert4 vert19 = vert8 vert20 = vert5 vertex.addData3(vert17) vertex.addData3(vert18) vertex.addData3(vert19) vertex.addData3(vert20) norm = (vert20 - vert17).cross(vert18 - vert17) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(16, 19, 17) tris.addVertices(17, 19, 18) ############################################ # # FACE 6 # ############################################ vert21 = vert2 vert22 = vert3 vert23 = vert7 vert24 = vert6 vertex.addData3(vert21) vertex.addData3(vert22) vertex.addData3(vert23) vertex.addData3(vert24) norm = (vert21 - vert24).cross(vert21 - vert22) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(20, 23, 21) tris.addVertices(21, 23, 22) square = Geom(vdata) square.addPrimitive(tris) self.prim = square self.model = GeomNode(self.name) self.model.addGeom(self.prim)
def create_box(): # Format format = GeomVertexFormat.getV3n3cpt2() # VertexData vdata = GeomVertexData("name", format, Geom.UHDynamic) ##VertexWriter vertex = GeomVertexWriter(vdata, "vertex") normal = GeomVertexWriter(vdata, "normal") color = GeomVertexWriter(vdata, "color") texcoord = GeomVertexWriter(vdata, "texcoord") points = [(-1, -1, -1), (-1, 1, -1), (1, -1, -1), (1, 1, -1), (-1, -1, 1), (-1, 1, 1), (1, -1, 1), (1, 1, 1)] for p in points: vertex.addData3f(*p) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) ### Create Geom geom = Geom(vdata) ### Create Primitives prim = GeomTriangles(Geom.UHStatic) prim.addVertices(0, 1, 2) prim.addVertices(2, 1, 3) prim.addVertices(4, 5, 6) prim.addVertices(4, 6, 7) for i in range(8): for k in range(8): for t in range(8): prim.addVertices(i, k, t) prim.closePrimitive() geom.addPrimitive(prim) node = GeomNode("gnode") node.addGeom(geom) xc = sum(x for (x, y, z) in points) / float(len(points)) yc = sum(y for (x, y, z) in points) / float(len(points)) zc = sum(z for (x, y, z) in points) / float(len(points)) return GeomResult(node, (xc, yc, zc), GeomGenerator.volume_for_cube(points))
def get_foot_geom(): # Format format = GeomVertexFormat.getV3n3cpt2() # VertexData vdata = GeomVertexData("name", format, Geom.UHDynamic) ##VertexWriter vertex = GeomVertexWriter(vdata, "vertex") normal = GeomVertexWriter(vdata, "normal") color = GeomVertexWriter(vdata, "color") texcoord = GeomVertexWriter(vdata, "texcoord") Ax, Ay = (-10, -10) Bx, By = (10, -10) Cx, Cy = (0, 20) z0, z1 = -1, 1 points = [(Ax, Ay, z0), (Bx, By, z0), (Cx, Cy, z0), (Ax, Ay, z1), (Bx, By, z1), (Cx, Cy, z1)] for p in points: vertex.addData3f(*p) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 1, 1, 1) color.addData4f(1, 0, 1, 1) color.addData4f(1, 0, 1, 1) color.addData4f(1, 0, 1, 1) normal.addData3f(0, 0, -1) normal.addData3f(0, 0, -1) normal.addData3f(0, 0, -1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) normal.addData3f(0, 0, 1) ### Create Geom geom = Geom(vdata) ### Create Primitives prim = GeomTriangles(Geom.UHStatic) for i in range(6): for k in range(6): for t in range(6): prim.addVertices(i, k, t) prim.closePrimitive() geom.addPrimitive(prim) node = GeomNode("gnode") node.addGeom(geom) xc = sum(x for (x, y, z) in points) / float(len(points)) yc = sum(y for (x, y, z) in points) / float(len(points)) zc = sum(z for (x, y, z) in points) / float(len(points)) Ax, Ay = points[0][0:2] Bx, By = points[1][0:2] Cx, Cy = points[2][0:2] volume = abs(Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By) / 2) return GeomResult(node, (xc, yc, zc), volume)
y = random.randint(sy, ey) z = self.world.map3d[x, y] if 0 < z < self.config.low_mount_level[1]: res[tree].append((x, y, z)) self[item] = res return res # Shit for f*****g trees formatArray = GeomVertexArrayFormat() formatArray.addColumn(InternalName.make("drawFlag"), 1, Geom.NTUint8, Geom.COther) treeform = GeomVertexFormat(GeomVertexFormat.getV3n3cpt2()) treeform.addArray(formatArray) treeform = GeomVertexFormat.registerFormat(treeform) #this draws the body of the tree. This draws a ring of vertices and connects the rings with #triangles to form the body. #this keepDrawing paramter tells the function wheter or not we're at an end #if the vertices before you were an end, dont draw branches to it def draw_body(nodePath, vdata, pos, vecList, radius=1, keepDrawing=True, numVertices=3):
def createNewSphere(self, radius, height, width, vertexcolour = None, transparency = 1): ''' Note that the first and the last node of the Thetha_Range should be 0 and 180 Degrees because this will be just one time added to verticies ''' #Format format = GeomVertexFormat.getV3n3cpt2() #VertexData vdata = GeomVertexData('name', format, Geom.UHStatic) ##VertexWriter vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') if (vertexcolour == None): vertexcolour = [0.5, 0.5, 0.5] counter = 0 ### Create the Skeleton of the Sphere for j in range(width): for i in range(height): if (i == 0): vect = self.sphereToKartesian(radius, 0, 0) vertex.addData3f(vect[0],vect[1],vect[2]) normal.addData3f(vect[0],vect[1],vect[2]) color.addData4f(vertexcolour[0],vertexcolour[1],vertexcolour[2],transparency) texcoord.addData2f(1-(float(j)/(width)), (float(i)/(height))) elif (i == height-1): vect = self.sphereToKartesian(radius, 0, 180) vertex.addData3f(vect[0],vect[1],vect[2]) normal.addData3f(vect[0],vect[1],vect[2]) color.addData4f(vertexcolour[0],vertexcolour[1],vertexcolour[2],transparency) texcoord.addData2f(1-(float(j)/(width)), (float(i)/(height))) else: vect = self.sphereToKartesian(radius, (float(j)/(width-2)) * 360, (float(i)/(height)) * 180) vertex.addData3f(vect[0],vect[1],vect[2]) normal.addData3f(vect[0],vect[1],vect[2]) texcoord.addData2f(1-(float(j)/(width-2)), (float(i)/(height))) color.addData4f(vertexcolour[0],vertexcolour[1],vertexcolour[2],transparency) counter += 1 ### Create Geom geom = Geom(vdata) ### Create Top Trifans prim = self.createTrifans(0,range(1,width+1)) #geom.addPrimitive(prim) ### Create Belttristrips for i in range(0,height-2): prim = self.createTristrips(width, width*i) geom.addPrimitive(prim) ### Create Bottom Trifans lastpoint = width*height-1 a = [i-1 for i in range(lastpoint,lastpoint-width,-1)] prim = self.createTrifans(lastpoint,a) #geom.addPrimitive(prim) geomnode = GeomNode('gnode') geomnode.addGeom(geom) return geomnode