def createColoredUnitDisk(color_vec4=Vec4(0., 0., 1., 1.), num_of_verts=10, origin_point=Vec3(0., 0., 0.), radius=1.): # Own Geometry # format = GeomVertexFormat.getV3c4t2() format = GeomVertexFormat.getV3c4() vdata = GeomVertexData("colored_circle", format, Geom.UHStatic) vdata.setNumRows(4) vertexPosWriter = GeomVertexWriter(vdata, "vertex") # num_of_verts = 10 # phi = 0. r = radius # origin_point_x = 0. # origin_point_z = 0. vertexPosWriter.addData3f(origin_point[0], origin_point[1], origin_point[2]) circle_points = math_utils.get_circle_vertices(num_of_verts=num_of_verts, radius=r) circle_points[:,0] += origin_point[0] circle_points[:,1] += origin_point[1] circle_points[:,2] += origin_point[2] _normal_vector_info = Vec3(0., 1., 0.) # this is returned just as info about the normal vector of the generated geometry for p in circle_points: vertexPosWriter.addData3f(p[0], 0, p[1]) # for i in range(num_of_verts): # phi += 2. * np.pi / num_of_verts # x = r * np.cos(phi) # z = r * np.sin(phi) # vertexPosWriter.addData3f(x, 0, z) # let's also add color to each vertex colorWriter = GeomVertexWriter(vdata, "color") colorWriter.addData4f(color_vec4) # origin point for i in range(num_of_verts): colorWriter.addData4f(color_vec4) # make primitives and assign vertices to them (primitives and primitive # groups can be made independently from vdata, and are later assigned # to vdata) tris = GeomTrifans(Geom.UHStatic) # the first vertex is a vertex that all triangles share tris.add_consecutive_vertices(0, num_of_verts+1) tris.addVertex(1) tris.closePrimitive() # the 1st primitive is finished # make a Geom object to hold the primitives geom = Geom(vdata) geom.addPrimitive(tris) geom_node = GeomNode("colored_circle_node") geom_node.addGeom(geom) return geom_node, _normal_vector_info
def makeSelectRect(): ctup = (1, 1, 1, 1) fmt = GeomVertexFormat.getV3c4() vertexData = GeomVertexData('points', fmt, Geom.UHDynamic) points = ( #makes nice for Tristrips (0, 0, 0), (0, 0, 1), (1, 0, 0), (1, 0, 1), ) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for point in points: verts.addData3f(*point) color.addData4f(*ctup) boxLines = GeomLinestrips(Geom.UHDynamic) boxLines.addVertices(0, 1, 3, 2) boxLines.addVertex(0) boxLines.closePrimitive() boxTris = GeomTristrips(Geom.UHDynamic) boxTris.addConsecutiveVertices(0, 3) boxTris.closePrimitive() box = Geom(vertexData) box.addPrimitive(boxLines) #box.addPrimitive(boxTris) return box
def create_GeomNode_Single_Point(color_vec4=Vec4(1., 1., 1., 1.)): # ---- step 1: create point at (0,0,0) and close the primitive format = GeomVertexFormat.getV3c4() vdata = GeomVertexData("colored_point", format, Geom.UHStatic) vdata.setNumRows(4) # add color to each vertex colorWriter = GeomVertexWriter(vdata, "color") # add a vertex position to each vertex vertexPosWriter = GeomVertexWriter(vdata, "vertex") # just one origin point vertex, it gets transformed later # to it's intended position vertexPosWriter.addData3f(0., 0., 0.) colorWriter.addData4f(color_vec4) # build the primitive pointsprimitive = GeomPoints(Geom.UHStatic) pointsprimitive.addVertex(0) pointsprimitive.closePrimitive() # this resets all the data contained in the vertexPosWriter and colorWriter # ----- step 3: make a GeomNode out of the Geom (to which the Primitives have been added) # make a Geom object to hold the primitives geom = Geom(vdata) geom.addPrimitive(pointsprimitive) geom_node = GeomNode("colored_point_node") geom_node.addGeom(geom) return geom_node
def add_plane(map_width, map_height): # Prepare the vertex format writers v_fmt = GeomVertexFormat.getV3n3c4() v_data = GeomVertexData('TerrainData', v_fmt, Geom.UHStatic) vertex = GeomVertexWriter(v_data, 'vertex') normal = GeomVertexWriter(v_data, 'normal') color = GeomVertexWriter(v_data, 'color') #texcoord = GeomVertexWriter(v_data, 'texcoord') # Create a primitive prim = GeomTrifans(Geom.UHStatic) poly_color = (uniform(0, 0.05), uniform(0, 0.5), uniform(0.5, 1), 0.5, ) for i, point in enumerate([ (-map_width/2, -map_height/2), (map_width/2, -map_height/2), (map_width/2, map_height/2), (-map_width/2, map_height/2), ]): x, y = point vertex.addData3f(x, y, 0) normal.addData3f(0, 0, 1) color.addData4f(*poly_color) #texcoord.addData2f(1, 0) prim.addVertex(i) prim.addVertex(0) prim.closePrimitive() # Add to the scene graph geom = Geom(v_data) geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) nodePath = render.attachNewNode(node) nodePath.setTwoSided(True) nodePath.setAlphaScale(0.5)
def makePoints(n=1000): """ make a cloud of points that are a single node VS branching and making subnodes to control display """ #points = np.random.uniform(-10,10,(n,4)) points = np.random.randn(n, 3) colors = np.random.rand(n, 4) fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for point, clr4 in zip(points, colors): #for point in points: verts.addData3f(*point) #color.addData4f(*point) color.addData4f(*clr4) #color.addData4f(.1,.1,.1,1) #pointCloud = GeomLinestrips(Geom.UHStatic) #this is f*****g cool! pointCloud = GeomTristrips(Geom.UHStatic) #this is f*****g cool! #pointCloud = GeomPoints(Geom.UHStatic) #pointCloud.addVerticies(*range(n)) pointCloud.addConsecutiveVertices(0, n) #warning may error since n-1? pointCloud.closePrimitive() cloud = Geom(vertexData) cloud.addPrimitive(pointCloud) return cloud
def addPoint(self, x, y, wheel_dir, force): """ Adds a point to the skid mark trail. The coordinates are in the world frame. The force is a number between zero and one, where one is the max force and makes the biggest and darkest skid mark. The argument 'wheel_dir' is given in degrees, where zero points toward the positive X axis. """ h = self.wheel_width * 0.5 rads = (wheel_dir - 90) * math.pi / 180.0 x0b, y0b = x + h * math.cos(rads), y + h * math.sin(rads) rads = (wheel_dir + 90) * math.pi / 180.0 x1b, y1b = x + h * math.cos(rads), y + h * math.sin(rads) x0a, y0a, x1a, y1a = self.lastpoints self.lastpoints = (x0b, y0b, x1b, y1b) if not self.havelast: self.havelast = True return indx = self.nextrect * 4 self.nextrect += 1 if self.nextrect >= self.nrects: self.nextrect = 0 vtx = GeomVertexWriter(self.vdata, "vertex") cx = GeomVertexWriter(self.vdata, "color") vtx.setRow(indx) cx.setRow(indx) c = self.forceToColor(force) vtx.addData3f(x0a, y0a, self.zpos) vtx.addData3f(x1a, y1a, self.zpos) vtx.addData3f(x0b, y0b, self.zpos) vtx.addData3f(x1b, y1b, self.zpos) cx.addData4f(*c) cx.addData4f(*c) cx.addData4f(*c) cx.addData4f(*c)
def make_geom(vertices, normals, colors, texcoords): if panda3d is None: raise ImportError("Cannot locate Panda3D") format = "V3" if normals is not None: assert len(normals) == len(vertices) format += "n3" if colors is not None: assert len(colors) == len(vertices) format += "cp" if texcoords is not None: assert len(texcoords) == len(vertices) format += "t2" format = getattr(GeomVertexFormat, "get" + format)() vdata = GeomVertexData("", format, Geom.UHStatic) v_vertex = GeomVertexWriter(vdata, 'vertex') if normals is not None: v_normals = GeomVertexWriter(vdata, 'normal') if colors is not None: v_colors = GeomVertexWriter(vdata, 'color') if texcoords is not None: v_texcoords = GeomVertexWriter(vdata, 'texcoord') for n in range(len(vertices)): v_vertex.addData3f(*vertices[n]) if normals is not None: v_normals.addData3f(*normals[n]) if colors is not None: v_colors.addData4f(*colors[n]) if texcoords is not None: v_texcoords.addData2f(*texcoords[n]) return Geom(vdata)
def makePoints(n=1000): """ make a cloud of points that are a single node VS branching and making subnodes to control display """ #points = np.random.uniform(-10,10,(n,4)) points = np.random.randn(n,3) colors = np.random.rand(n,4) fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for point,clr4 in zip(points,colors): #for point in points: verts.addData3f(*point) #color.addData4f(*point) color.addData4f(*clr4) #color.addData4f(.1,.1,.1,1) #pointCloud = GeomLinestrips(Geom.UHStatic) #this is f*****g cool! pointCloud = GeomTristrips(Geom.UHStatic) #this is f*****g cool! #pointCloud = GeomPoints(Geom.UHStatic) #pointCloud.addVerticies(*range(n)) pointCloud.addConsecutiveVertices(0,n) #warning may error since n-1? pointCloud.closePrimitive() cloud = Geom(vertexData) cloud.addPrimitive(pointCloud) return cloud
def makeSelectRect(): ctup = (1,1,1,1) fmt = GeomVertexFormat.getV3c4() vertexData = GeomVertexData('points', fmt, Geom.UHDynamic) points = ( #makes nice for Tristrips (0,0,0), (0,0,1), (1,0,0), (1,0,1), ) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for point in points: verts.addData3f(*point) color.addData4f(*ctup) boxLines = GeomLinestrips(Geom.UHDynamic) boxLines.addVertices(0,1,3,2) boxLines.addVertex(0) boxLines.closePrimitive() boxTris = GeomTristrips(Geom.UHDynamic) boxTris.addConsecutiveVertices(0,3) boxTris.closePrimitive() box = Geom(vertexData) box.addPrimitive(boxLines) #box.addPrimitive(boxTris) return box
def pg_draw_tris(pg, render): format = GeomVertexFormat.getV3c4() vdata = GeomVertexData('pgtris', format, Geom.UHStatic) vdata.setNumRows(len(pg.nodes)) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') prim = GeomTriangles(Geom.UHStatic) for pt in pg.nodes: vertex.addData3f(pt.x, pt.y, pt.z) color.addData4f(random.random(), random.random(), random.random(), 1.0) for pt in pg.nodes: if len(pt.conn) > 0: for i, cpt in enumerate(pt.conn): next_cpt = pt.conn[(i + 1) % len(pt.conn)] prim.addVertices(pt.idx, cpt.idx, next_cpt.idx) print("%d - %d - %d" % (pt.idx, cpt.idx, next_cpt.idx)) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('TheTris') node.addGeom(geom) nodePath = render.attachNewNode(node) nodePath.setPos(0, 10, 0)
def __build_ocean_mesh(self): vdata = GeomVertexData('data', GeomVertexFormat.getV3n3c4t2(), Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') color = GeomVertexWriter(vdata, 'color') axes = [Vec3.unitX(), Vec3.unitY(), Vec3.unitZ()] face = 0 for x in range(3): for s in [-1, 1]: for i in range(self.__n + 1): for j in range(self.__n + 1): a = (i * 1.0 / self.__n) * (pi / 2) - (pi / 4) b = (j * 1.0 / self.__n) * (pi / 2) - (pi / 4) xAxis = axes[(x + 3) % 3] yAxis = axes[(x + 4) % 3] zAxis = axes[(x + 5) % 3] v = (xAxis * (-cos(a) * sin(b)) + yAxis * (sin(a) * cos(b)) + zAxis * (cos(a) * cos(b))) * s v.normalize() normal.addData3f(v) vertex.addData3f(v * self.__radius) texcoord.addData2f(i * 1.0, j * 1.0) color.addData4f(self.__ocean_color) face = face + 1 prim = self.__heightmap_primitive() geom = Geom(vdata) geom.addPrimitive(prim) return geom
def primitives(self, vdata): vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') n = len(self.border) # Points for p in self.border: vertex.addData3f(p.x, p.y, p.z) color.addData4f(0.5, 0.5, 0.5, 0.0) for p in self.border: vertex.addData3f(p.x, p.y, p.z + self.top) color.addData4f(1.0, 1.0, 1.0, 0.0) # Wall wall = GeomTristrips(Geom.UHStatic) for i in range(n): wall.addVertices(i, i + n) wall.addVertices(0, n) wall.closePrimitive() yield wall # Ceiling if self.cover: ceil = GeomTristrips(Geom.UHStatic) ceil.addConsecutiveVertices(n, n) ceil.addVertex(n) ceil.closePrimitive() yield ceil
def draw(self): if self.rendered_mesh != None: self.reset_draw() format=GeomVertexFormat.getV3n3cp() vdata=GeomVertexData('tri', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') color=GeomVertexWriter(vdata, 'color') v_mapping = {} i=0 for v in self.verts.values(): vertex.addData3f(v.pos.x,v.pos.y,v.pos.z) normal.addData3f(v.norm.x, v.norm.y, v.norm.z) color.addData4f(v.color[0],v.color[1],v.color[2],v.color[3]) v_mapping[v.ID] = i i += 1 mesh = Geom(vdata) for f in self.faces.values(): tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v_mapping[f.v1.ID]) tri.addVertex(v_mapping[f.v2.ID]) tri.addVertex(v_mapping[f.v3.ID]) tri.closePrimitive() mesh.addPrimitive(tri) snode = GeomNode(self.name) snode.addGeom(mesh) self.rendered_mesh = render.attachNewNode(snode) self.rendered_mesh.setTwoSided(True)
def draw(self): if self.rendered_mesh != None: self.reset_draw() format = GeomVertexFormat.getV3n3cp() vdata = GeomVertexData('tri', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') v_mapping = {} i = 0 for v in self.verts.values(): vertex.addData3f(v.pos.x, v.pos.y, v.pos.z) normal.addData3f(v.norm.x, v.norm.y, v.norm.z) color.addData4f(v.color[0], v.color[1], v.color[2], v.color[3]) v_mapping[v.ID] = i i += 1 mesh = Geom(vdata) for f in self.faces.values(): tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(v_mapping[f.v1.ID]) tri.addVertex(v_mapping[f.v2.ID]) tri.addVertex(v_mapping[f.v3.ID]) tri.closePrimitive() mesh.addPrimitive(tri) snode = GeomNode(self.name) snode.addGeom(mesh) self.rendered_mesh = render.attachNewNode(snode) self.rendered_mesh.setTwoSided(True)
def __build_Star_Sphere(self, bg_stars): from panda3d.core import GeomVertexWriter, GeomVertexFormat, GeomVertexData from panda3d.core import Geom, GeomNode, GeomPoints, AmbientLight self.star_sphere_np.removeNode() # Fill GeomVertexData. vformat = GeomVertexFormat.getV3c4() vdata = GeomVertexData("Data", vformat, Geom.UHStatic) vertices = GeomVertexWriter(vdata, "vertex") colours = GeomVertexWriter(vdata, "color") for coords in bg_stars: x, y, z = coords vertices.addData3f(x, y, z) colours.addData4f(1, 1, 1, 1) # Render bg stars. bg_stars = GeomPoints(Geom.UHStatic) bg_stars.addNextVertices(_env.STAR_COUNT) bg_stars_geom = Geom(vdata) bg_stars_geom.addPrimitive(bg_stars) star_sphere = GeomNode("star_sphere") star_sphere.addGeom(bg_stars_geom) star_sphere_np = NodePath(star_sphere) star_sphere_np.reparentTo(self.NP) return star_sphere_np
def draw_rain_mesh(self): _format = GeomVertexFormat.get_v3cp() self.rain_vdata = GeomVertexData('rain', _format, Geom.UHDynamic) self.rain_vdata.setNumRows(self.n_points**2) vertex = GeomVertexWriter(self.rain_vdata, 'vertex') color = GeomVertexWriter(self.rain_vdata, 'color') for j in range(self.n_points): for i in range(self.n_points): # Rain Vertices vertex.addData3f(self.x[j][i], self.y[j][i], self.n_points) # Rain Colors color.addData4f(0.3, 0.3, 1, 0) # Rain Primitive prim = GeomPoints(Geom.UHDynamic) for j in range(self.n_points): for i in range(self.n_points): prim.add_vertices(j * (self.n_points) + i, j * (self.n_points) + i, j * (self.n_points) + i) geom = Geom(self.rain_vdata) prim.closePrimitive() geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) rain_nodePath = render.attachNewNode(node) rain_nodePath.setTransparency(TransparencyAttrib.MAlpha) rain_nodePath.setAntialias(AntialiasAttrib.MAuto) rain_nodePath.setRenderModeThickness(2) rain_nodePath.setPos(-50, -50, 0)
def __init__(self, base, obj, **kwargs): super(GalaxyView, self).__init__(base, obj, **kwargs) array = GeomVertexArrayFormat() array.addColumn(InternalName.make('vertex'), 3, Geom.NTFloat32, Geom.CPoint) array.addColumn(InternalName.make('color'), 4, Geom.NTFloat32, Geom.CColor) array.addColumn(InternalName.make('size'), 1, Geom.NTFloat32, Geom.COther) gmformat = GeomVertexFormat() gmformat.addArray(array) gmformat = GeomVertexFormat.registerFormat(gmformat) vdata = GeomVertexData('points', gmformat, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') size = GeomVertexWriter(vdata, 'size') self.node = NodePath('galaxy') self.node.reparentTo(self.base.render) self.node.setTransparency(TransparencyAttrib.MAlpha) lumsort = sorted([star.luminosity for star in self.obj.stars]) #highest_luminosity = lumsort[-1] median_luminosity = lumsort[len(lumsort) / 2] for star in self.obj.stars: vertex.addData3f(star.galpos.x, star.galpos.y, star.galpos.z) color.addData4f(star.red, star.green, star.blue, 1.0) #size.addData1f(min(100, max(5, 10-star.magnitude/2))) sizeval = 10 + log(star.luminosity) size.addData1f(min(30, max(10, sizeval))) prim = GeomPoints(Geom.UHStatic) prim.addConsecutiveVertices(0, len(self.obj.stars)) prim.closePrimitive() geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) galaxy_node = self.node.attachNewNode(node) galaxy_node.setRenderModeThickness(1) ts = TextureStage.getDefault() #TextureStage('ts') #ts.setMode(TextureStage.MGlow) galaxy_node.setTexGen(ts, TexGenAttrib.MPointSprite) galaxy_node.setTexture( ts, self.base.loader.loadTexture('texture/flare.png')) #galaxy_node.setRenderModePerspective(True) galaxy_node.setBin("unsorted", 1) galaxy_node.setDepthWrite(0) galaxy_node.setTransparency(1) self.setup_glow_shader() """
def create_geom(self, sidelength): # Set up the vertex arrays vformat = GeomVertexFormat.getV3n3c4() vdata = GeomVertexData("Data", vformat, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') geom = Geom(vdata) # Write vertex data for x in range(0, sidelength): for y in range(0, sidelength): # vertex_number = x * sidelength + y v_x, v_y, v_z = self.map_b[(x, y)] n_x, n_y, n_z = 0.0, 0.0, 1.0 c_r, c_g, c_b, c_a = 0.5, 0.5, 0.5, 0.5 vertex.addData3f(v_x, v_y, v_z) normal.addData3f(n_x, n_y, n_z) color.addData4f(c_r, c_g, c_b, c_a) # Add triangles for x in range(0, sidelength - 1): for y in range(0, sidelength - 1): # The vertex arrangement (y up, x right) # 2 3 # 0 1 v_0 = x * sidelength + y v_1 = x * sidelength + (y + 1) v_2 = (x + 1) * sidelength + y v_3 = (x + 1) * sidelength + (y + 1) if (x+y)%1 == 0: # An even square tris = GeomTriangles(Geom.UHStatic) tris.addVertices(v_0, v_2, v_3) tris.closePrimitive() geom.addPrimitive(tris) tris = GeomTriangles(Geom.UHStatic) tris.addVertices(v_3, v_1, v_0) tris.closePrimitive() geom.addPrimitive(tris) else: # An odd square tris = GeomTriangles(Geom.UHStatic) tris.addVertices(v_1, v_0, v_2) tris.closePrimitive() geom.addPrimitive(tris) tris = GeomTriangles(Geom.UHStatic) tris.addVertices(v_2, v_3, v_1) tris.closePrimitive() geom.addPrimitive(tris) # Create the actual node node = GeomNode('geom_node') node.addGeom(geom) # Remember GeomVertexWriters to adjust vertex data later #self.vertex_writer = vertex #self.color_writer = color self.vdata = vdata return node
def create_geom(self, sidelength): # Set up the vertex arrays vformat = GeomVertexFormat.getV3n3c4() vdata = GeomVertexData("Data", vformat, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') geom = Geom(vdata) # Write vertex data # Vertex data for the poles needs to be different for key, value in self.map.items(): v_x, v_y, v_z, asteroid_color = value n_x, n_y, n_z = 1, 1, 1 #c_r, c_g, c_b, c_a = asteroid_color, asteroid_color, asteroid_color, 1 c_r, c_g, c_b, c_a = asteroid_color, asteroid_color, asteroid_color, 1 vertex.addData3f(v_x, v_y, v_z) normal.addData3f(n_x, n_y, n_z) color.addData4f(c_r, c_g, c_b, c_a) #Create triangles #top of sphere verts_per_row = int(360 / self.step) + 1 for vert in range(1, verts_per_row + 1): tris = GeomTriangles(Geom.UHStatic) tris.addVertices(vert + 1, 0, vert) tris.closePrimitive() geom.addPrimitive(tris) #middle of shpere for row in range(1, int(180 / self.step) - 1): for vert_iir in range( 0, verts_per_row - 1): # vert_iir = vertex index in row, not vertex number vert_number = verts_per_row * row + vert_iir + 1 vert_up_row = vert_number - verts_per_row #Bottom Triangle in the sphere tris = GeomTriangles(Geom.UHStatic) tris.add_vertices(vert_up_row, vert_number, vert_up_row + 1) tris.close_primitive() geom.addPrimitive(tris) #Top triangle of square tris = GeomTriangles(Geom.UHStatic) tris.add_vertices(vert_number + 1, vert_up_row + 1, vert_number) tris.close_primitive() geom.addPrimitive(tris) #bottom of sphere last_vert = len(self.map) - 1 for vert in range(last_vert - verts_per_row, last_vert): tris = GeomTriangles(Geom.UHStatic) tris.add_vertices(vert - 1, last_vert, vert) tris.close_primitive() geom.addPrimitive(tris) # Create the actual node node = GeomNode('geom_node') node.addGeom(geom) return node
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
class GroundGen(object): def __init__(self, max_r): self.max_r = max_r format = GeomVertexFormat.getV3c4() vdata = GeomVertexData('point', format, Geom.UHDynamic) self._pos_writer = GeomVertexWriter(vdata, 'vertex') self._color_writer = GeomVertexWriter(vdata, 'color') line_num = 60 vdata.setNumRows(line_num) angles = np.linspace(0, np.pi * 2 - np.pi * 2 / line_num, line_num) other_rgba = (0., 0., 0.3, 0.1) other2_rgba = (0.1, 0.1, 0.4, 0.4) axis_rgba = (0.2, 0.2, 0.5, 1.0) max_r = 250 for indx, angle in enumerate(angles): if indx % 5 == 0: rgba = axis_rgba else: rgba = other_rgba self._pos_writer.addData3d(0, 0, 0.) self._color_writer.addData4f(rgba[0], rgba[1], rgba[2], rgba[3]) self._pos_writer.addData3d(max_r * np.sin(angle), max_r * np.cos(angle), 0.) self._color_writer.addData4f(rgba[0], rgba[1], rgba[2], rgba[3]) grnd_prmtv = GeomLines(Geom.UHStatic) grnd_prmtv.addConsecutiveVertices(0, 2 * line_num) grnd_prmtv.closePrimitive() ground_geom = Geom(vdata) ground_geom.addPrimitive(grnd_prmtv) snode = GeomNode('ground_lines') snode.addGeom(ground_geom) self.points_node = base.render.attachNewNode(snode) self.points_node.setTwoSided(True) for rad in range(int(max_r)): color = axis_rgba pp = makeArc(angleDegrees=360, numSteps=160, scale=rad, color=color) tn = TextNode('dd') tn.setText(str(rad)) tn.setTextScale(0.2) tn.setTextColor(color) text_geom = GeomNode('text') text_geom.addChild(tn) tp = NodePath(text_geom) tp.setPos((0, rad - 0.2, 0)) tp.setHpr((0, -90, 0)) tp.reparentTo(self.points_node) pp.reparentTo(self.points_node)
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 update_geom(self, points, colors, sizes): geom = self.instance.children[0].node().modify_geom(0) vdata = geom.modify_vertex_data() vdata.unclean_set_num_rows(len(points)) vwriter = GeomVertexRewriter(vdata, InternalName.get_vertex()) colorwriter = GeomVertexWriter(vdata, InternalName.get_color()) sizewriter = GeomVertexWriter(vdata, InternalName.get_size()) for (point, color, size) in zip(points, colors, sizes): vwriter.addData3f(*point) colorwriter.addData4f(*color) sizewriter.addData1f(size)
def makeGeom(index_counter, array, ctup, i, pipe, geomType=GeomPoints): """ multiprocessing capable geometery maker """ #man = indexMan(('127.0.0.1',5000), authkey='none') #man.connect() #index = man.index() index = {} fmt = GeomVertexFormat.getV3c4() vertexData = GeomVertexData( 'points', fmt, Geom.UHDynamic ) #FIXME use the index for these too? with setPythonTag, will have to 'reserve' some #vertexData.setPythonTag('uid',index.reserve()) #maybe we don't need this? the geom should have it all? cloudGeom = Geom(vertexData) #cloudGeom.setPythonTag('uid',index.reserve()) cloudNode = GeomNode('bin %s selectable' % (i)) uid = next(index_counter) index[uid] = None cloudNode.setPythonTag( 'uid', uid ) #FIXME we return cloudnode elsewhere... maybe on the other end we can set the uid in the index properly? points = array verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for point in points: index[next(index_counter)] = [ point, cloudNode.getPythonTag('uid'), None ] #FIXME we're gonna need a decode on the other end? verts.addData3f(*point) color.addData4f(*ctup) points = geomType(Geom.UHDynamic) points.addConsecutiveVertices(0, len(array)) points.closePrimitive() cloudGeom.addPrimitive(points) cloudNode.addGeom( cloudGeom ) #TODO figure out if it is faster to add and subtract Geoms from geom nodes... #output[i] = cloudNode #print('ping',{i:cloudNode}) #pipe.send((i,)) #out = q.get() #print('pong',out) #q.put(out) if pipe == None: return cloudNode, index pipe.send( cloudNode.encodeToBamStream()) #FIXME make this return a pointer NOPE pipe.send(index) #FIXME make this return a pointer NOPE
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 set_points(self, position, color): assert len(position) == len(color) self.vertexData.setNumRows(len(position)) self.primitive.clearVertices() for i in range(len(position)): self.primitive.addVertex(i) self.primitive.closePrimitive() positionWriter = GeomVertexWriter(self.vertexData, "vertex") colorWriter = GeomVertexWriter(self.vertexData, "color") for p, c in zip(position, color): positionWriter.addData3f(p[0], p[1], p[2]) colorWriter.addData4f(c[0], c[1], c[2], c[3])
def createTriangle(v1, v2, v3, is_flat=False): x1 = v1.x y1 = v1.y z1 = v1.z x2 = v2.x y2 = v2.y z2 = v2.z x3 = v3.x y3 = v3.y z3 = v3.z format=GeomVertexFormat.getV3n3cp() vdata=GeomVertexData('tri', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') color=GeomVertexWriter(vdata, 'color') vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x3, y3, z3) if is_flat: normVector = norm(Vec3( (x1 + x2 + x3)/3.0, (y1 + y2 + y3)/3.0, (z1+ z2+ z3)/3.0)) normal.addData3f(normVector) normal.addData3f(normVector) normal.addData3f(normVector) else: normal.addData3f(norm(Vec3(x1,y1,z1))) normal.addData3f(norm(Vec3(x2,y2,z2))) normal.addData3f(norm(Vec3(x3,y3,z3))) #adding different colors to the vertex for visibility color.addData4f(0.5,0.5,0.5,1.0) color.addData4f(0.5,0.5,0.5,1.0) color.addData4f(0.5,0.5,0.5,1.0) tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(0) tri.addVertex(1) tri.addVertex(2) tri.closePrimitive() output_tri = Geom(vdata) output_tri.addPrimitive(tri) return output_tri
def update_geometry(self): # The geosphere itself vertex = GeomVertexWriter(self.sphere_vdata, 'vertex') normal = GeomVertexWriter(self.sphere_vdata, 'normal') # u_map and v_map are in [-pi, pi] u_map_list = [(float(u) / float(self.res[0]) - 0.5) * 2.0 * pi for u in range(0, self.res[0] + 1)] v_map_list = [(float(v) / float(self.res[1]) - 0.5) * 2.0 * pi for v in range(0, self.res[1] + 1)] if self.unwrap_state == 0.0: # Flat map for v_map in v_map_list: for u_map in u_map_list: vertex.addData3f(u_map, 0.0, v_map / 2.0) normal.addData3f(0.0, -1.0, 0.0) else: # Non-flat map sphere_radius = 1.0 / self.unwrap_state sphere_offset = sphere_radius - self.unwrap_state for v_map in v_map_list: for u_map in u_map_list: u_sphere = u_map / sphere_radius v_sphere = v_map / sphere_radius # And this, kids, is why you should pay attention in trigonometry. v_x, v_y, v_z = sin(u_sphere) * cos(v_sphere/2.0) * sphere_radius, \ -cos(u_sphere) * cos(v_sphere/2.0) * sphere_radius + sphere_offset, \ sin(v_sphere / 2.0) * sphere_radius n_x_un, n_y_un, n_z_un = v_x, sphere_offset - v_y, v_z # FIXME: This is a lie. length = sqrt(n_x_un**2 + n_y_un**2 + n_z_un**2) n_x, n_y, n_z = n_x_un / length, n_y_un / length, n_z_un / length vertex.addData3f(v_x, v_y, v_z) normal.addData3f(n_x, n_y, n_z) # The connections between bases segs_per_connection = 30 vertex = GeomVertexWriter(self.connections_vdata, 'vertex') color = GeomVertexWriter(self.connections_vdata, 'color') for c_1_uv, c_2_uv in self.connections: # s will be [0.0, 1.0] for s in [float(c)/float(segs_per_connection+1) for c in range(0, segs_per_connection+2)]: u = (c_1_uv[0] * s) + (c_2_uv[0] * (1.0 - s)) v = (c_1_uv[1] * s) + (c_2_uv[1] * (1.0 - s)) (v_x, v_y, v_z), (n_x, n_y, n_z) = self.uv_to_xyz(u, v) min_height = 0.0001 * (1.0 - self.unwrap_state) max_height = (0.2 - min_height) * self.unwrap_state seg_height = (1.0 - (abs(s-0.5) * 2.0)**2.0) * max_height + min_height vertex.addData3f(v_x + n_x*seg_height, v_y + n_y*seg_height, v_z + n_z*seg_height) color.addData4f(1, 1, 1, 1) for c in range(0, len(self.connections)): for s in range(0, segs_per_connection+1): seg = GeomLines(Geom.UHDynamic) seg.addVertices(c*(segs_per_connection+2) + s, c*(segs_per_connection+2) + s + 1) seg.closePrimitive() self.connections_geom.addPrimitive(seg)
def makeAxis(): #FIXME make this scale based on zoom??? """ x y z r g b """ colors = ( (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,0,0,1), (0,1,0,1), (0,0,1,1), ) points = ( (0,0,0), (0,0,0), (0,0,0), (1,0,0), (0,1,0), (0,0,1), ) fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color #fmt = GeomVertexFormat.getV3() #3 component vertex, w/ 4 comp color vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for p,c in zip(points,colors): verts.addData3f(*p) color.addData4f(*c) axisX = GeomLinestrips(Geom.UHStatic) axisX.addVertices(0,3) axisX.closePrimitive() axisY = GeomLinestrips(Geom.UHStatic) axisY.addVertices(1,4) axisY.closePrimitive() axisZ = GeomLinestrips(Geom.UHStatic) axisZ.addVertices(2,5) axisZ.closePrimitive() axis = Geom(vertexData) axis.addPrimitive(axisX) axis.addPrimitive(axisY) axis.addPrimitive(axisZ) return axis
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 create_colored_polygon2d_GeomNode_from_point_cloud(point_cloud, color_vec4=Vec4(0., 0., 1., 1.)): # for triangulation (with tripy) the elements of point_cloud # must be 3-tuples instead of np.arrays triangles = tripy_modified.earclip( [tuple(elem) for elem in point_cloud]) triangles = np.array(triangles) # convert tuples to np.arrays # naive rendering: abusing the concept of an index buffer # format = GeomVertexFormat.getV3c4t2() format = GeomVertexFormat.getV3c4() vdata = GeomVertexData("colored_polygon", format, Geom.UHStatic) vdata.setNumRows(4) # make a Geom object to hold the primitives geom = Geom(vdata) # give it a reference to the # add position and color to each vertex vertex_pos_writer = GeomVertexWriter(vdata, "vertex") vertex_color_writer = GeomVertexWriter(vdata, "color") # fill vertex buffer for triangle in triangles: vertex_pos_writer.addData3f(triangle[0][0], 0, triangle[0][1]) # z is up vertex_pos_writer.addData3f(triangle[1][0], 0, triangle[1][1]) vertex_pos_writer.addData3f(triangle[2][0], 0, triangle[2][1]) vertex_color_writer.addData4f(color_vec4) vertex_color_writer.addData4f(color_vec4) vertex_color_writer.addData4f(color_vec4) # create the GeomPrimitive (just one) by filling the index buffer # in stages. The documentation says 'Each GeomPrimitive object actually # stores several different individual primitives, each of which is # represwended simply as a list of vertex numbers, indexing into the # vertices stored in the associated GeomVertexData' tris = GeomTriangles(Geom.UHStatic) # derived from GeomPrimitive # for idx, triangle in enumerate(triangles): # tris.addVertex(idx*3 + 0) # tris.addVertex(idx*3 + 1) # tris.addVertex(idx*3 + 2) # # close the current primitive (not the GeomPrimitive!) tris.add_consecutive_vertices(0, 3*len(triangles)) tris.closePrimitive() geom.addPrimitive(tris) geom_node = GeomNode("colored_polygon_node") geom_node.addGeom(geom) return geom_node
def createColoredUnitQuadGeomNode(color_vec4=Vec4(0., 0., 1., 1.), center_it=False): # Own Geometry # format = GeomVertexFormat.getV3c4t2() format = GeomVertexFormat.getV3c4() vdata = GeomVertexData("colored_quad", format, Geom.UHStatic) vdata.setNumRows(4) vertexPosWriter = GeomVertexWriter(vdata, "vertex") if center_it == False: vertexPosWriter.addData3f(0, 0, 0) vertexPosWriter.addData3f(1, 0, 0) vertexPosWriter.addData3f(1, 0, 1) vertexPosWriter.addData3f(0, 0, 1) else: vertexPosWriter.addData3f(0 - 0.5, 0, 0 - 0.5) vertexPosWriter.addData3f(1 - 0.5, 0, 0 - 0.5) vertexPosWriter.addData3f(1 - 0.5, 0, 1 - 0.5) vertexPosWriter.addData3f(0 - 0.5, 0, 1 - 0.5) # let's also add color to each vertex colorWriter = GeomVertexWriter(vdata, "color") colorWriter.addData4f(color_vec4) colorWriter.addData4f(color_vec4) colorWriter.addData4f(color_vec4) colorWriter.addData4f(color_vec4) # make primitives and assign vertices to them (primitives and primitive # groups can be made independently from vdata, and are later assigned # to vdata) tris = GeomTriangles(Geom.UHStatic) # 1st triangle tris.addVertices(0, 1, 3) tris.closePrimitive() # the 1st primitive is finished # 2nd triangle tris.addVertices(1, 2, 3) tris.closePrimitive() # make a Geom object to hold the primitives quadGeom = Geom(vdata) quadGeom.addPrimitive(tris) # now put quadGeom in a GeomNode. You can now position your geometry # in the scene graph. quadGeomNode = GeomNode("colored_quad_node") quadGeomNode.addGeom(quadGeom) return quadGeomNode
def __init__(self, nrects: int = 100, wheel_width=0.2, zpos=-0.38): """ Initalizes one track for a skid mark. The mark will be placed parallel to X-Y in world coordinates. They will be placed at the given height given by 'zpos'. The 'nrects' argument specifies how long the skid can be in input point locations. You can make the skid mark longer by increasing nrects, or spreading out the distance between input points. Once nrect points have been input, the earlier points will disappear. The 'wheel-width' parameter is the thickness of the wheel that is producing the marks. """ self.wheel_width = wheel_width # One unit = about 10 inches in current world. self.wheel_state = [ ] # Tupels of: X, Y position of wheel, While Direction in degrees, and force self.nrects = nrects self.zpos = zpos self.vdata = GeomVertexData("skid", GeomVertexFormat.getV3c4(), Geom.UHStatic) self.vdata.setNumRows(4 * nrects) vtx = GeomVertexWriter(self.vdata, "vertex") cx = GeomVertexWriter(self.vdata, "color") prim = GeomTriangles(Geom.UHStatic) c = self.forceToColor(0.0) # Here we set up all the vertexts and the primatives that use them. For each set of four vertices, a # flat rectangle is defined. Then 4 primatives are defined by splitting up the rectangle into pair of 2 # triangles -- 4 triangles per rectangle. Each pair of triangles are identical, except that their normals # are opposite, so that we don't have to worry about back culling later. Although it would be possible # to share vertics between the rectangles we don't do that here so that the chain can be cut at any point # by only operating on the vertics and not visiting the primatives. Finally, to "disable" a rectangle, # we set all the vertics to the same point in 3D space (and therefore create a zero-area rectangle). for i in range(nrects): vtx.addData3f(0.0, 0.0, 0.0) vtx.addData3f(0.0, 0.0, 0.0) vtx.addData3f(0.0, 0.0, 0.0) vtx.addData3f(0.0, 0.0, 0.0) cx.addData4f(*c) cx.addData4f(*c) cx.addData4f(*c) cx.addData4f(*c) j = i * 4 j0, j1, j2, j3 = j + 0, j + 1, j + 2, j + 3 prim.addVertices(j0, j1, j3) prim.addVertices(j3, j1, j0) prim.addVertices(j0, j3, j2) prim.addVertices(j2, j3, j0) prim.closePrimitive() geom = Geom(self.vdata) geom.addPrimitive(prim) node = GeomNode('Skid') node.addGeom(geom) self.nodepath = render.attachNewNode(node) self.nextrect = 0 self.havelast = False self.lastpoints = (0, 0, 0, 0)
def primitives_lines(self, vdata): vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') n = len(self.points) # Points for p in self.points: vertex.addData3f(p.x, p.y, p.z) color.addData4f(0.2, 0.2, 0.2, 0.0) # Triangles for a, b, c in self.vertices: lines = GeomLinestrips(Geom.UHStatic) lines.addVertices(a, b, c, a) lines.closePrimitive() yield lines
def create_colored_rect(x, z, width, height, colors=None): _format = GeomVertexFormat.getV3c4() vdata = GeomVertexData('square', _format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') vertex.addData3(x, 0, z) vertex.addData3(x + width, 0, z) vertex.addData3(x + width, 0, z + height) vertex.addData3(x, 0, z + height) if colors: if len(colors) < 4: colors = (1.0, 1.0, 1.0, 1.0) color.addData4f(colors) color.addData4f(colors) color.addData4f(colors) color.addData4f(colors) else: 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, 1.0, 1.0, 1.0) tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 2) tris.addVertices(2, 3, 0) square = Geom(vdata) square.addPrimitive(tris) return square
def makeAxis(): #FIXME make this scale based on zoom??? """ x y z r g b """ colors = ( (1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), (1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1), ) points = ( (0, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), ) fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color #fmt = GeomVertexFormat.getV3() #3 component vertex, w/ 4 comp color vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for p, c in zip(points, colors): verts.addData3f(*p) color.addData4f(*c) axisX = GeomLinestrips(Geom.UHStatic) axisX.addVertices(0, 3) axisX.closePrimitive() axisY = GeomLinestrips(Geom.UHStatic) axisY.addVertices(1, 4) axisY.closePrimitive() axisZ = GeomLinestrips(Geom.UHStatic) axisZ.addVertices(2, 5) axisZ.closePrimitive() axis = Geom(vertexData) axis.addPrimitive(axisX) axis.addPrimitive(axisY) axis.addPrimitive(axisZ) return axis
class ModelBuilder: def __init__(self): self.vformat = GeomVertexFormat.getV3n3c4() self.vdata = GeomVertexData("Data", self.vformat, Geom.UHStatic) self.vdata.setNumRows(3) self.vertex = GeomVertexWriter(self.vdata, 'vertex') self.normal = GeomVertexWriter(self.vdata, 'normal') self.color = GeomVertexWriter(self.vdata, 'color') self.prim = GeomTriangles(Geom.UHStatic) self.idx = 0 def _add_point(self, v, n, c): self.vertex.addData3f(v[0], v[1], v[2]) self.normal.addData3f(n[0], n[1], n[2]) self.color.addData4f(c[0], c[1], c[2], c[3]) self.idx += 1 return self.idx - 1 def add_triangle(self, v1, v2, v3, n, c): i0 = self._add_point(v1, n, c) i1 = self._add_point(v2, n, c) i2 = self._add_point(v3, n, c) self.prim.addVertices(i0, i1, i2) def add_rect(self, org, w, h, n, c): i0 = self._add_point(org, n, c) i1 = self._add_point(org + w, n, c) i2 = self._add_point(org + w + h, n, c) i3 = self._add_point(org + h, n, c) self.prim.addVertices(i0, i1, i2) self.prim.addVertices(i2, i3, i0) def add_box(self, org, w, h, l, c): self.add_rect(org, w, h, -norm(l), c) self.add_rect(org, w, l, -norm(h), c) self.add_rect(org, h, l, -norm(w), c) self.add_rect(org + w, l, h, norm(w), c) self.add_rect(org + h, l, w, norm(h), c) self.add_rect(org + l, h, w, norm(l), c) def build(self, name): self.prim.closePrimitive() geom = Geom(self.vdata) geom.addPrimitive(self.prim) node = GeomNode(name) node.addGeom(geom) return node
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 makePoint(point=(0,0,0)): clr4 = [1,1,1,1] fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') verts.addData3f(*point) color = GeomVertexWriter(vertexData, 'color') color.addData4f(*clr4) pointCloud = GeomPoints(Geom.UHStatic) pointCloud.addVertex(0) pointCloud.closePrimitive() cloud = Geom(vertexData) cloud.addPrimitive(pointCloud) cloudNode = GeomNode('point') cloudNode.addGeom(cloud) return cloudNode
def makeGeom(index_counter, array,ctup,i,pipe, geomType=GeomPoints): """ multiprocessing capable geometery maker """ #man = indexMan(('127.0.0.1',5000), authkey='none') #man.connect() #index = man.index() index = {} fmt = GeomVertexFormat.getV3c4() vertexData = GeomVertexData('points', fmt, Geom.UHDynamic) #FIXME use the index for these too? with setPythonTag, will have to 'reserve' some #vertexData.setPythonTag('uid',index.reserve()) #maybe we don't need this? the geom should have it all? cloudGeom = Geom(vertexData) #cloudGeom.setPythonTag('uid',index.reserve()) cloudNode = GeomNode('bin %s selectable'%(i)) uid = next(index_counter) index[uid] = None cloudNode.setPythonTag('uid',uid) #FIXME we return cloudnode elsewhere... maybe on the other end we can set the uid in the index properly? points = array verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for point in points: index[next(index_counter)]=[point,cloudNode.getPythonTag('uid'),None] #FIXME we're gonna need a decode on the other end? verts.addData3f(*point) color.addData4f(*ctup) points = geomType(Geom.UHDynamic) points.addConsecutiveVertices(0,len(array)) points.closePrimitive() cloudGeom.addPrimitive(points) cloudNode.addGeom(cloudGeom) #TODO figure out if it is faster to add and subtract Geoms from geom nodes... #output[i] = cloudNode #print('ping',{i:cloudNode}) #pipe.send((i,)) #out = q.get() #print('pong',out) #q.put(out) if pipe == None: return cloudNode, index pipe.send(cloudNode.encodeToBamStream()) #FIXME make this return a pointer NOPE pipe.send(index) #FIXME make this return a pointer NOPE
def makeCameraTarget(): colors = ( (0,0,1,1), (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,0,0,1), (0,1,0,1), ) points = ( (0,0,1), (-1,0,0), (0,-1,0), (0,0,-1), (1,0,0), (0,1,0), (0,0,1), ) fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color #fmt = GeomVertexFormat.getV3() #3 component vertex, w/ 4 comp color vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for p,c in zip(points,colors): verts.addData3f(*p) color.addData4f(*c) targetTris = GeomTristrips(Geom.UHStatic) targetTris.addConsecutiveVertices(0,6) targetTris.addVertex(0) targetTris.addVertex(1) targetTris.addVertex(3) targetTris.addVertex(5) targetTris.addVertex(2) targetTris.addVertex(4) targetTris.addVertex(0) targetTris.closePrimitive() target = Geom(vertexData) target.addPrimitive(targetTris) return target
def draw_vert(self,v, v_color): format = GeomVertexFormat.getV3cp() vdata=GeomVertexData('vert', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') color=GeomVertexWriter(vdata, 'color') vertex.addData3f(0.0,0.0,0.0) color.addData4f(v_color) mesh = Geom(vdata) point = GeomPoints(Geom.UHDynamic) point.addVertex(0) point.closePrimitive() mesh.addPrimitive(point) vert_node = GeomNode(self.mesh.name+'_vert_'+str(v.ID)) vert_node.addGeom(mesh) vert_node.setTag('ID',str(v.ID)) rendered_vert = self.render_root.attachNewNode(vert_node) rendered_vert.setPos(v.pos.x,v.pos.y,v.pos.z) rendered_vert.setRenderModeThickness(5.0) self.render_nodes['vert_'+str(v.ID)] = rendered_vert
def makeCircle(vdata, numVertices=40,offset=Vec3(0,0,0), direction=1): circleGeom=Geom(vdata) vertWriter=GeomVertexWriter(vdata, "vertex") normalWriter=GeomVertexWriter(vdata, "normal") colorWriter=GeomVertexWriter(vdata, "color") uvWriter=GeomVertexWriter(vdata, "texcoord") drawWriter=GeomVertexWriter(vdata, "drawFlag") #make sure we start at the end of the GeomVertexData so we dont overwrite anything #that might be there already startRow=vdata.getNumRows() vertWriter.setRow(startRow) colorWriter.setRow(startRow) uvWriter.setRow(startRow) normalWriter.setRow(startRow) drawWriter.setRow(startRow) angle=2*math.pi/numVertices currAngle=angle for i in range(numVertices): position=Vec3(math.cos(currAngle)+offset.getX(), math.sin(currAngle)+offset.getY(),offset.getZ()) vertWriter.addData3f(position) uvWriter.addData2f(position.getX()/2.0+0.5,position.getY()/2.0+0.5) colorWriter.addData4f(1.0, 1.0, 1.0, 1.0) position.setZ(position.getZ()*direction) position.normalize() normalWriter.addData3f(position) #at default Opengl only draws "front faces" (all shapes whose vertices are arranged CCW). We #need direction so we can specify which side we want to be the front face currAngle+=angle*direction circle=GeomTrifans(Geom.UHStatic) circle.addConsecutiveVertices(startRow, numVertices) circle.closePrimitive() circleGeom.addPrimitive(circle) return circleGeom
def msgVec(self, ctup, x, y, z): vertexData = GeomVertexData('points', self.fmt, Geom.UHDynamic) cloudGeom = Geom(vertexData) cloudNode = GeomNode('just some points') verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for x_, y_, z_, c in zip(x, y, z, ctup): verts.addData3f(x_, y_, z_) color.addData4f(*c) points = self.geomType(Geom.UHDynamic) # FIXME static? points.addConsecutiveVertices(0,len(x)) points.closePrimitive() cloudGeom.addPrimitive(points) cloudNode.addGeom(cloudGeom) #TODO figure out if it is faster to add and subtract Geoms from geom nodes... print(cloudNode) return cloudNode
def make_cube(x, y, z): # FIXME make prism """ make x, y, z sized cube (ints pls) """ colors = [[1,1,1,0] for i in range(8)] #colors[0] = np.array((1,1,1,1)) #colors[1] = np.array((1,0,0,0)) #colors[2] = np.array((0,1,0,0)) #colors[5] = np.array((0,0,1,0)) points = ( (0,0,0), (0,0,z), (0,y,0), (0,y,z), (x,0,0), (x,0,z), (x,y,0), (x,y,z), ) order = [0, 5, 1, 7, 3, 2, 1, 0, 5, 4, 7, 6, 2, 4, 0] # perfect for clockwise #order = [2, 6, 3, 7, 5, 6, 4, 2, 0, 3, 1, 4, 0, 4] #order.reverse() #order = [4, 3, 7, 8, 5, 3, 1, 4, 2, 7, 6, 5, 2, 1] fmt = GeomVertexFormat.getV3c4() vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for p,c in zip(points,colors): verts.addData3f(*p) color.addData4f(*c) targetTris = GeomTristrips(Geom.UHStatic) targetTris.addConsecutiveVertices(0,8) for i in order: targetTris.addVertex(i)#-1) targetTris.closePrimitive() target = Geom(vertexData) target.addPrimitive(targetTris) return target
def create_model(self): # Set up the vertex arrays vformat = GeomVertexFormat.get_v3c4() vdata = GeomVertexData("Data", vformat, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') geom = Geom(vdata) # Vertex data vertex.addData3f(1.5, 0, -1) color.addData4f(1, 0, 0, 1) vertex.addData3f(-1.5, 0, -1) color.addData4f(0, 1, 0, 1) vertex.addData3f(0, 0, 1) color.addData4f(0, 0, 1, 1) # Primitive tri = GeomTriangles(Geom.UHStatic) tri.add_vertex(2) tri.add_vertex(1) tri.add_vertex(0) tri.close_primitive() geom.addPrimitive(tri) # Create the actual node node = GeomNode('geom_node') node.addGeom(geom) np = NodePath(node) # Shader and initial shader vars np.set_shader(Shader.load(Shader.SL_GLSL, "shader/shader.vert", "shader/shader.frag")) np.set_shader_input("time", 0.0) # No instancing necessary #np.set_instance_count(27) # return np np.reparent_to(base.render) self.model = np
def draw_face(self,f,f_color): #add normal format = GeomVertexFormat.getV3n3cp() vdata=GeomVertexData('vert', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') color=GeomVertexWriter(vdata, 'color') normal=GeomVertexWriter(vdata, 'normal') vertex.addData3f(f.v1.pos) normal.addData3f(f.v1.norm.x, f.v1.norm.y, f.v1.norm.z) color.addData4f(f_color) vertex.addData3f(f.v2.pos) normal.addData3f(f.v2.norm.x, f.v2.norm.y, f.v2.norm.z) color.addData4f(f_color) vertex.addData3f(f.v3.pos) normal.addData3f(f.v3.norm.x, f.v3.norm.y, f.v3.norm.z) color.addData4f(f_color) mesh = Geom(vdata) tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(0) tri.addVertex(1) tri.addVertex(2) tri.closePrimitive() mesh.addPrimitive(tri) face_node = GeomNode(self.mesh.name+'_face_'+str(f.ID)) face_node.addGeom(mesh) face_node.setTag('ID',str(f.ID)) rendered_face = self.render_root.attachNewNode(face_node) rendered_face.setTwoSided(True) self.render_nodes['face_'+str(f.ID)] = rendered_face
def makeSimpleGeom(array, ctup, geomType = GeomPoints, fix = False): fmt = GeomVertexFormat.getV3c4() vertexData = GeomVertexData('points', fmt, Geom.UHDynamic) #FIXME use the index for these too? with setPythonTag, will have to 'reserve' some cloudGeom = Geom(vertexData) cloudNode = GeomNode('just some points') verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') if fix: if len(ctup) == len(array): for point,c in zip(array, ctup): verts.addData3f(*point) color.addData4f(*c) else: for point in array: verts.addData3f(*point) color.addData4f(*ctup) else: for point in array: verts.addData3f(*point) color.addData4f(*ctup) points = geomType(Geom.UHDynamic) points.addConsecutiveVertices(0,len(array)) points.closePrimitive() cloudGeom.addPrimitive(points) cloudNode.addGeom(cloudGeom) #TODO figure out if it is faster to add and subtract Geoms from geom nodes... if fix: return cloudNode.__reduce__() else: return cloudNode # decoding fails becuase ForkingPickler is called for reasons beyond comprehension
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, mp): vdata = GeomVertexData("name_me", GeomVertexFormat.getV3c4(), Geom.UHStatic) vertex = GeomVertexWriter(vdata, "vertex") color = GeomVertexWriter(vdata, "color") primitive = GeomTristrips(Geom.UHStatic) film_size = base.cam.node().getLens().getFilmSize() x = film_size.getX() / 2.0 z = x * 256.0 / 240.0 vertex.addData3f(x, 90, z) vertex.addData3f(-x, 90, z) vertex.addData3f(x, 90, -z) vertex.addData3f(-x, 90, -z) color.addData4f(VBase4(*mp["backgroundcolor1"])) color.addData4f(VBase4(*mp["backgroundcolor1"])) color.addData4f(VBase4(*mp["backgroundcolor2"])) color.addData4f(VBase4(*mp["backgroundcolor2"])) primitive.addNextVertices(4) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) self.node = GeomNode("sky") self.node.addGeom(geom) base.camera.attachNewNode(self.node)
def makeGrid(rng = 1000, spacing = 10): #FIXME make this scale based on zoom??? ctup = (.3,.3,.3,1) xs = range(-rng,rng+1,spacing) ys = xs fmt = GeomVertexFormat.getV3c4() #3 component vertex, w/ 4 comp color #fmt = GeomVertexFormat.getV3() #3 component vertex, w/ 4 comp color vertexData = GeomVertexData('points', fmt, Geom.UHStatic) verts = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') for i,d in enumerate(xs): switch1 = (-1) ** i * rng switch2 = (-1) ** i * -rng #print(d,switch1,0) verts.addData3f(d, switch1, 0) verts.addData3f(d, switch2, 0) color.addData4f(*ctup) color.addData4f(*ctup) for i,d in enumerate(ys): switch1 = (-1) ** i * rng switch2 = (-1) ** i * -rng verts.addData3f(switch1, d, 0) verts.addData3f(switch2, d, 0) color.addData4f(*ctup) color.addData4f(*ctup) gridLines = GeomLinestrips(Geom.UHStatic) gridLines.addConsecutiveVertices(0, vertexData.getNumRows()) gridLines.closePrimitive() grid = Geom(vertexData) grid.addPrimitive(gridLines) return grid
def create_model(self): # Set up the vertex arrays vformatArray = GeomVertexArrayFormat() # Panda3D implicitly generates a bounding volume from a # column named "vertex", so you either # * have a column of that name, or # * add a bounding volume yourself. vformatArray.addColumn(InternalName.make("vertex"), 3, Geom.NTFloat32, Geom.CPoint) vformatArray.addColumn(InternalName.make("color"), 4, Geom.NTFloat32, Geom.CColor) vformat = GeomVertexFormat() vformat.addArray(vformatArray) vformat = GeomVertexFormat.registerFormat(vformat) vdata = GeomVertexData("Data", vformat, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') geom = Geom(vdata) # Vertex data vertex.addData3f(1.5, 0, -1) color.addData4f(1, 0, 0, 1) vertex.addData3f(-1.5, 0, -1) color.addData4f(0, 1, 0, 1) vertex.addData3f(0, 0, 1) color.addData4f(0, 0, 1, 1) # Primitive tri = GeomPatches(3, Geom.UHStatic) tri.add_vertex(2) tri.add_vertex(1) tri.add_vertex(0) tri.close_primitive() geom.addPrimitive(tri) # Create the actual node node = GeomNode('geom_node') node.addGeom(geom) np = NodePath(node) # Shader, initial shader vars, number of instances np.set_shader(Shader.load(Shader.SL_GLSL, vertex = "shader.vert", tess_control = "shader.tesc", tess_evaluation = "shader.tese", geometry = "shader.geom", fragment = "shader.frag")) np.set_shader_input("time", 0.0) np.set_shader_input("tess_level", 32.0) np.set_instance_count(num_instances) np.set_shader_input("numInstances", num_instances) return np
def renderCharts(facegraph, verts, vert_indices, lineset=None): from meshtool.filters.panda_filters.pandacore import getVertexData, attachLights, ensureCameraAt from meshtool.filters.panda_filters.pandacontrols import KeyboardMovement, MouseDrag, MouseScaleZoom, ButtonUtils from panda3d.core import GeomTriangles, Geom, GeomNode, GeomVertexFormat, GeomVertexData, GeomVertexWriter, LineSegs from direct.showbase.ShowBase import ShowBase vformat = GeomVertexFormat.getV3c4() vdata=GeomVertexData('tris', vformat, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') color=GeomVertexWriter(vdata, 'color') colors = gen_color3(len(facegraph)) numtris = 0 for chart, data in facegraph.nodes_iter(data=True): curcolor = next(colors) for tri in data['tris']: triv = verts[vert_indices[tri]] vertex.addData3f(triv[0][0], triv[0][1], triv[0][2]) vertex.addData3f(triv[1][0], triv[1][1], triv[1][2]) vertex.addData3f(triv[2][0], triv[2][1], triv[2][2]) color.addData4f(curcolor[0],curcolor[1], curcolor[2], 1) color.addData4f(curcolor[0],curcolor[1], curcolor[2], 1) color.addData4f(curcolor[0],curcolor[1], curcolor[2], 1) numtris += 1 tris=GeomTriangles(Geom.UHDynamic) tris.addConsecutiveVertices(0, 3*numtris) tris.closePrimitive() linenodes = [] if lineset: for lines in lineset: ls = LineSegs() ls.setThickness(4) curcolor = next(colors) ls.setColor(curcolor[0]/256.0, curcolor[1]/256.0, curcolor[2]/256.0, 1) tuples = False for blah in lines: if isinstance(blah, tuple): tuples = True break if tuples: for i, j in lines: frompt = verts[i] topt = verts[j] ls.moveTo(frompt[0], frompt[1], frompt[2]) ls.drawTo(topt[0], topt[1], topt[2]) else: for i in range(len(lines)-1): frompt = verts[lines[i]] topt = verts[lines[i+1]] ls.moveTo(frompt[0], frompt[1], frompt[2]) ls.drawTo(topt[0], topt[1], topt[2]) linenodes.append(ls.create()) pgeom = Geom(vdata) pgeom.addPrimitive(tris) node = GeomNode("primitive") node.addGeom(pgeom) p3dApp = ShowBase() #attachLights(render) geomPath = render.attachNewNode(node) for linenode in linenodes: geomPath.attachNewNode(linenode) #geomPath.setRenderModeWireframe() ensureCameraAt(geomPath, base.cam) boundingSphere = geomPath.getBounds() base.cam.setPos(boundingSphere.getCenter() + boundingSphere.getRadius()) base.cam.lookAt(boundingSphere.getCenter()) KeyboardMovement() ButtonUtils(geomPath) MouseDrag(geomPath) MouseScaleZoom(geomPath) #render.setShaderAuto() p3dApp.run()
class MeshGenerator(): def __init__(self, name = 'Mesh'): self.name = name self.finished = False self.format = GeomVertexFormat.getV3c4t2() #print self.format self.vertexData = GeomVertexData(name, self.format, Geom.UHStatic) self.vertexData.setNumRows(26136) self.mesh = Geom(self.vertexData) self.triangles = GeomTriangles(Geom.UHStatic) self.triangleData = self.triangles.modifyVertices() self.triangleData.setNumRows(26136) self.vertex = GeomVertexWriter(self.vertexData, 'vertex') self.normal = GeomVertexWriter(self.vertexData, 'normal') self.color = GeomVertexWriter(self.vertexData, 'color') self.texcoord = GeomVertexWriter(self.vertexData, 'texcoord') self.faceCount = 0 def makeFace(self, x1, y1, z1, x2, y2, z2, id, color): if x1 != x2: self.vertex.addData3f(x1, y1, z1) self.vertex.addData3f(x2, y1, z1) self.vertex.addData3f(x2, y2, z2) self.vertex.addData3f(x1, y2, z2) else: self.vertex.addData3f(x1, y1, z1) self.vertex.addData3f(x2, y2, z1) self.vertex.addData3f(x2, y2, z2) self.vertex.addData3f(x1, y1, z2) self.color.addData4f(color, color, color, 1.0) self.color.addData4f(color, color, color, 1.0) self.color.addData4f(color, color, color, 1.0) self.color.addData4f(color, color, color, 1.0) if id == 1: self.texcoord.addData2f(0.0, 0.5) self.texcoord.addData2f(0.0, 0.0) self.texcoord.addData2f(0.25, 0.0) self.texcoord.addData2f(0.25, 0.5) elif id == 2: self.texcoord.addData2f(0.25, 0.5) self.texcoord.addData2f(0.25, 0.0) self.texcoord.addData2f(0.5, 0.0) self.texcoord.addData2f(0.5, 0.5) elif id == 3: self.texcoord.addData2f(0.5, 0.5) self.texcoord.addData2f(0.5, 0.0) self.texcoord.addData2f(0.75, 0.0) self.texcoord.addData2f(0.75, 0.5) elif id == 4: self.texcoord.addData2f(0.75, 0.5) self.texcoord.addData2f(0.75, 0.0) self.texcoord.addData2f(1.0, 0.0) self.texcoord.addData2f(1.0, 0.5) elif id == 5: self.texcoord.addData2f(0.0, 1.0) self.texcoord.addData2f(0.0, 0.5) self.texcoord.addData2f(0.25, 0.5) self.texcoord.addData2f(0.25, 1.0) elif id == 6: self.texcoord.addData2f(0.25, 1.0) self.texcoord.addData2f(0.25, 0.5) self.texcoord.addData2f(0.5, 0.5) self.texcoord.addData2f(0.5, 1.0) vertexId = self.faceCount * 4 self.triangles.addVertices(vertexId, vertexId + 1, vertexId + 3) self.triangles.addVertices(vertexId + 1, vertexId + 2, vertexId + 3) self.faceCount += 1 def makeFrontFace(self, x, y, z, id): self.makeFace(x + 1, y + 1, z - 1, x, y + 1, z, id, 0.6) def makeBackFace(self, x, y, z, id): self.makeFace(x, y, z - 1, x + 1, y, z, id, 0.85) def makeRightFace(self, x, y, z, id): self.makeFace(x + 1, y, z - 1, x + 1, y + 1, z, id, 1.0) def makeLeftFace(self, x, y, z, id): self.makeFace(x, y + 1, z - 1, x, y, z, id, 0.9) def makeTopFace(self, x, y, z, id): self.makeFace(x + 1, y + 1, z, x, y, z, id, 1.0) def makeBottomFace(self, x, y, z, id): self.makeFace(x, y + 1, z - 1, x + 1, y, z - 1, id, 0.70) def getMesh(self): return self.mesh def getGeomNode(self): if self.finished == False: self.triangles.closePrimitive() self.mesh.addPrimitive(self.triangles) self.finished = True geomNode = GeomNode(self.name) geomNode.addGeom(self.mesh) return geomNode
def __init__(self, point, vertices, map_width=1024, map_height=1024, base=0.0): self.point = point # TODO: Sort these clockwise (they still are 2D points) # http://stackoverflow.com/questions/6989100/sort-points-in-clockwise-order # Might just need to reverse if the wrong way. I think they're already # ordered. Should be able to tell by checking the normal calculation. self.vertices = vertices # Ensure that stuff is working correctly if [-10.101, -10.101] in vertices: raise ValueError("Can't create this region.") # TODO: Constrain the bounds like this... though it's somewhat busted. # for i, v in enumerate(vertices): # x, y = v # x = max(min(x, map_width/2), -map_width/2) # y = max(min(y, map_height/2), -map_height/2) # vertices[i] = (x, y) for v in vertices: if not (-map_width/2 <= v[0] <= map_width/2 and -map_height/2 <= v[1] <= map_height/2): raise ValueError("Can't create this region.") # Prepare the vertex format writers v_fmt = GeomVertexFormat.getV3n3c4() v_data = GeomVertexData('TerrainData', v_fmt, Geom.UHStatic) vertex = GeomVertexWriter(v_data, 'vertex') normal = GeomVertexWriter(v_data, 'normal') color = GeomVertexWriter(v_data, 'color') #texcoord = GeomVertexWriter(v_data, 'texcoord') # Create a primitive # TODO: Migrate to Tristrips. # TODO: Make walls, floors (and ceilings?) prim = GeomTrifans(Geom.UHStatic) DETAIL = 128 HEIGHT_SCALE = 20 vertices = [ (v[0], v[1], snoise2( v[0]/DETAIL, v[1]/DETAIL, base=base, persistence=0.5, lacunarity=2.0, repeatx=map_width/4, repeaty=map_height/4, octaves=5) * HEIGHT_SCALE) for v in vertices] num_high = len(list(filter(lambda x: x[2] > HEIGHT_SCALE * 0.4, vertices))) num_above = len(list(filter(lambda x: HEIGHT_SCALE * 0.4 > x[2] > 0, vertices))) num_below = len(list(filter(lambda x: x[2] <= 0, vertices))) num_deep = len(list(filter(lambda x: x[2] < -HEIGHT_SCALE * 0.2, vertices))) # Calculate the color: # * White if high altitude # * Green if land # * Tan if coast # * Blue if water # * Dark Blue if deep water # poly_color = (uniform(0, 1), uniform(0, 1), uniform(0, 1), 1, ) if num_high: white = uniform(0.7, 1.0) poly_color = (white, white, white, 1.0) elif num_below and num_above: yellow = uniform(0.5, 1.0) poly_color = (yellow, yellow, 0.0, 1.0) elif num_deep: poly_color = (0.0, 0.0, uniform(0.5, 1.0), 1.0) elif num_below: poly_color = (0.3, 0.3, uniform(0.5, 1.0), 1.0) else: poly_color = (0.0, uniform(0.5, 1.0), 0.0, 1.0) for i, point in enumerate(vertices): vertex.addData3f(point[0], point[1], max(point[2], 0)) normal.addData3f(0, 0, 1) color.addData4f(*poly_color) #texcoord.addData2f(1, 0) prim.addVertex(i) prim.addVertex(0) prim.closePrimitive() # Add to the scene graph geom = Geom(v_data) geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) nodePath = render.attachNewNode(node) nodePath.setTwoSided(True)
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