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 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 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 _makeGeom(array,ctup,i,pipe, geomType=GeomPoints): #XXX testing multiple Geom version ... for perf seems like it will be super slow #SUUUPER slow TONS of draw calls #wwwayyy better to make a bunch of geoms ahead of time... """ multiprocessing capable geometery maker """ fmt = GeomVertexFormat.getV3c4() cloudNode = GeomNode('bin %s selectable'%(i)) for point in array: vertexData = GeomVertexData('poitn', fmt, Geom.UHStatic) GeomVertexWriter(vertexData, 'vertex').addData3f(*point) GeomVertexWriter(vertexData, 'color').addData4f(*ctup) #verts.addData3f(*point) #color.addData4f(*ctup) points = geomType(Geom.UHStatic) points.addVertex(0) points.closePrimitive() cloudGeom = Geom(vertexData) 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) pipe.send(cloudNode.encodeToBamStream()) #FIXME make this return a pointer NOPE
def makeSimpleGeomBuffer(array, color, geomType=GeomPoints): """ massively faster than the nonbuffer version """ full = [tuple(d) for d in np.hstack((array,color))] 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') vertexData.setNumRows(len(array)) mem_array = vertexData.modifyArray(0) view = memoryview(mem_array) arr = np.asarray(view) arr[:] = full points = geomType(Geom.UHDynamic) points.addConsecutiveVertices(0,len(array)) points.closePrimitive() cloudGeom.addPrimitive(points) cloudNode.addGeom(cloudGeom) return cloudNode
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 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 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 makeSimpleGeomBuffer(array, color, geomType=GeomPoints): """ massively faster than the nonbuffer version """ full = [tuple(d) for d in np.hstack((array, color))] 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') vertexData.setNumRows(len(array)) mem_array = vertexData.modifyArray(0) view = memoryview(mem_array) arr = np.asarray(view) arr[:] = full points = geomType(Geom.UHDynamic) points.addConsecutiveVertices(0, len(array)) points.closePrimitive() cloudGeom.addPrimitive(points) cloudNode.addGeom(cloudGeom) return cloudNode
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 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 __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 __init__(self, diameter, density): self.fmt = GeomVertexFormat.getV3c4() self.diameter = diameter r = diameter / 2 # Sample random points in a 2d plane points_2d = [] for i in range(int(density * pi * r**2)): x, y = random_in_circle() points_2d.append((x * r, y * r)) # Compute triangulations triangulation = DelaunayTri(points_2d) # Save points in 3d space self.points = [] for x, y in triangulation.points: p = Vec3(x, y, elevation(x, y)) self.points.append(p) # Save triangles self.vertices = triangulation.vertices # Save a graph with the triangulation self.nodes = [set() for i in range(len(self.points))] self.edges = [] for a, b, c in triangulation.vertices: self.nodes[a] |= {b, c} self.nodes[b] |= {a, c} self.nodes[c] |= {a, b} self.edges.append((a, b)) self.edges.append((b, c)) self.edges.append((c, a))
def newVertexData(self): fmt = GeomVertexFormat.getV3c4() # fmt = GeomVertexFormat.getV3n3c4() self.vertexData = GeomVertexData("path", fmt, Geom.UHStatic) self.vertexWriter = GeomVertexWriter(self.vertexData, 'vertex') # self.normalWriter = GeomVertexWriter(self.vertexData, 'normal') self.colorWriter = GeomVertexWriter(self.vertexData, 'color')
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 newVertexData(self): fmt = GeomVertexFormat.getV3c4() # fmt = GeomVertexFormat.getV3n3c4() self.vertexData = GeomVertexData("path", fmt, Geom.UHStatic) self.vertexWriter = GeomVertexWriter(self.vertexData, "vertex") # self.normalWriter = GeomVertexWriter(self.vertexData, 'normal') self.colorWriter = GeomVertexWriter(self.vertexData, "color")
def __init__(self, length=1., tickness=3.): GeomNode.__init__(self, "Basis") self.vertexData = GeomVertexData("Basis", GeomVertexFormat.getV3c4(), Geom.UHStatic) self.vertex = GeomVertexWriter(self.vertexData, 'vertex') self.color = GeomVertexWriter(self.vertexData, 'color') self.mesh = Geom(self.vertexData) self.lines = GeomLines(Geom.UHStatic) self.vertex.addData3f(0.0, 0.0, 0.0) self.color.addData4f(1.0, 0.0, 0.0, 1.0) self.vertex.addData3f(length, 0.0, 0.0) self.color.addData4f(1.0, 0.0, 0.0, 1.0) self.lines.add_vertices(0, 1) self.vertex.addData3f(0.0, 0.0, 0.0) self.color.addData4f(0.0, 1.0, 0.0, 1.0) self.vertex.addData3f(0.0, length, 0.0) self.color.addData4f(0.0, 1.0, 0.0, 1.0) self.lines.add_vertices(2, 3) self.vertex.addData3f(0.0, 0.0, 0.0) self.color.addData4f(0.0, 0.0, 1.0, 1.0) self.vertex.addData3f(0.0, 0.0, length) self.color.addData4f(0.0, 0.0, 1.0, 1.0) self.lines.add_vertices(4, 5) self.lines.closePrimitive() self.mesh.addPrimitive(self.lines) self.addGeom(self.mesh) NodePath(self).setRenderModeThickness(tickness) NodePath(self).setLightOff() NodePath(self).setColorOff() NodePath(self).set_bin('fixed', 9)
def create_instance(self): self.vertexData = GeomVertexData('vertexData', GeomVertexFormat.getV3c4(), Geom.UHStatic) self.vertexWriter = GeomVertexWriter(self.vertexData, 'vertex') self.colorwriter = GeomVertexWriter(self.vertexData, 'color') for point in self.points: position = point.project(0, self.context.observer.camera_global_pos, self.context.observer.infinity) self.vertexWriter.addData3f(*position) self.colorwriter.addData4f(*self.color) self.lines = GeomLines(Geom.UHStatic) index = 0 for i in range(len(self.points) - 1): self.lines.addVertex(index) self.lines.addVertex(index + 1) self.lines.closePrimitive() index += 1 self.geom = Geom(self.vertexData) self.geom.addPrimitive(self.lines) self.node = GeomNode("boundary") self.node.addGeom(self.geom) self.instance = NodePath(self.node) self.instance.setRenderModeThickness(settings.boundary_thickness) self.instance.reparentTo(self.context.annotation) self.instance.setBin('background', settings.boundaries_depth) self.instance.set_depth_write(False)
def create_instance(self): self.vertexData = GeomVertexData('vertexData', GeomVertexFormat.getV3c4(), Geom.UHStatic) self.vertexWriter = GeomVertexWriter(self.vertexData, 'vertex') self.colorwriter = GeomVertexWriter(self.vertexData, 'color') for r in range(1, self.nbOfRings + 1): for i in range(self.nbOfPoints): angle = 2 * pi / self.nbOfPoints * i x = cos(angle) * sin( pi * r / (self.nbOfRings + 1) ) y = sin(angle) * sin( pi * r / (self.nbOfRings + 1) ) z = sin( -pi / 2 + pi * r / (self.nbOfRings + 1) ) self.vertexWriter.addData3f((self.context.observer.infinity * x, self.context.observer.infinity * y, self.context.observer.infinity * z)) if r == self.nbOfRings / 2 + 1: self.colorwriter.addData4f(self.color.x * 1.5, 0, 0, 1) else: self.colorwriter.addData4f(*self.color) for s in range(self.nbOfSectors): for i in range(self.points_to_remove, self.nbOfPoints // 2 - self.points_to_remove + 1): angle = 2 * pi / self.nbOfPoints * i x = cos(2*pi * s / self.nbOfSectors) * sin(angle) y = sin(2*pi * s / self.nbOfSectors) * sin(angle) z = cos(angle) self.vertexWriter.addData3f((self.context.observer.infinity * x , self.context.observer.infinity * y, self.context.observer.infinity * z)) if s == 0: self.colorwriter.addData4f(self.color.x * 1.5, 0, 0, 1) else: self.colorwriter.addData4f(*self.color) self.lines = GeomLines(Geom.UHStatic) index = 0 for r in range(self.nbOfRings): for i in range(self.nbOfPoints-1): self.lines.addVertex(index) self.lines.addVertex(index+1) self.lines.closePrimitive() index += 1 self.lines.addVertex(index) self.lines.addVertex(index - self.nbOfPoints + 1) self.lines.closePrimitive() index += 1 for r in range(self.nbOfSectors): for i in range(self.nbOfPoints // 2 - self.points_to_remove * 2): self.lines.addVertex(index) self.lines.addVertex(index+1) self.lines.closePrimitive() index += 1 index += 1 self.geom = Geom(self.vertexData) self.geom.addPrimitive(self.lines) self.node = GeomNode("grid") self.node.addGeom(self.geom) self.instance = NodePath(self.node) self.instance.setRenderModeThickness(settings.grid_thickness) #myMaterial = Material() #myMaterial.setEmission((1.0, 1.0, 1.0, 1)) #self.instance.setMaterial(myMaterial) self.instance.reparentTo(self.context.annotation) self.instance.setQuat(LQuaternion(*self.orientation))
def __init__(self, size, translation, rotation, color, text): self._visible = False wy, wx, wz = size[0], size[1], size[2] format = GeomVertexFormat.getV3c4() vdata = GeomVertexData('cu_points', format, Geom.UHStatic) vdata.setNumRows(8) self._pos_writer = GeomVertexWriter(vdata, 'vertex') self._color_writer = GeomVertexWriter(vdata, 'color') self._pos_writer.set_row(0) self._color_writer.set_row(0) self._pos_writer.addData3f(-0.5 * wx, -0.5 * wy, 0.) self._pos_writer.addData3f(-0.5 * wx, -0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, -0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, -0.5 * wy, 0.) self._pos_writer.addData3f(-0.5 * wx, 0.5 * wy, 0.) self._pos_writer.addData3f(-0.5 * wx, 0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, 0.5 * wy, wz) self._pos_writer.addData3f(0.5 * wx, 0.5 * wy, 0.) for i in range(8): self._color_writer.addData4f(color[0], color[1], color[2], color[3]) lines = GeomLines(Geom.UHStatic) lines.addVertices(0, 1) lines.addVertices(1, 2) lines.addVertices(2, 3) lines.addVertices(3, 0) lines.addVertices(4, 5) lines.addVertices(5, 6) lines.addVertices(6, 7) lines.addVertices(7, 4) lines.addVertices(0, 4) lines.addVertices(1, 5) lines.addVertices(2, 6) lines.addVertices(3, 7) cuboid = Geom(vdata) cuboid.addPrimitive(lines) node = GeomNode('cuboid') node.addGeom(cuboid) self._node_path = NodePath(node) # self.title = OnscreenText(text=text, style=1, fg=(1, 1, 1, 1), pos=(-0.1, 0.1), scale=.05, # parent=self._node_path, align=TextNode.ARight) self._txt_node = TextNode('id') self._txt_node.setText(text) self._txt_node.setTextScale(0.2) self._txt_node.setCardColor(0, 0, 1, 1) self._txt_node.setCardAsMargin(0, 0, 0, 0) self._txt_node.setCardDecal(True) self._txt_node.set_align(2) text_geom = GeomNode('text') text_geom.addChild(self._txt_node) self._txt_np = NodePath(text_geom) self._txt_np.reparentTo(self._node_path) self.show() self.update_values(size, translation, rotation, color, text)
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 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 _build_sticker_geom(sticker, sticker_color, scalar=1): """Builds and returns a Geom composed of the specified points in sticker, colored the specified color. Parameters ---------- sticker: tuple A size-4 tuple consisting of the middle, edge, corner, and edge points, respectively. sticker_color: tuple The RGB values for the sticker color. scalar: float, optional The scale of the sticker from 0 to 1, where 1 has the sticker cover the entier cubie. Defaults to 1. Returns ------- Geom The just-built Geom of the sticker. """ sticker = CubeModel._scale_sticker(sticker, scalar) vertex_format = GeomVertexFormat.getV3c4() vertex_data = GeomVertexData("vertices", vertex_format, Geom.UHStatic) vertex_data.setNumRows(4) vertex_writer = GeomVertexWriter(vertex_data, 'vertex') color_writer = GeomVertexWriter(vertex_data, 'color') vertex_writer.addData3f(sticker[0]) color_writer.addData3f(sticker_color) vertex_writer.addData3f(sticker[1]) color_writer.addData3f(sticker_color) vertex_writer.addData3f(sticker[2]) color_writer.addData3f(sticker_color) vertex_writer.addData3f(sticker[3]) color_writer.addData3f(sticker_color) color_writer.addData3f(sticker_color) triangles = GeomTriangles(Geom.UH_static) triangles.addVertices(0, 1, 2) triangles.addVertices(0, 3, 2) triangles.closePrimitive() geom = Geom(vertex_data) geom.addPrimitive(triangles) return geom
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 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 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 __init__(self, handleType): Geometry.__init__(self, "handles", GeomVertexFormat.getV3c4()) self.radius = 1.0 self.zoom = 1.0 self.handleOrigins = [ Point3(0), Point3(0), Point3(0), Point3(0), Point3(0), Point3(0), Point3(0), Point3(0) ] self.handleType = handleType
def __init__(self): ShowBase.__init__(self) vertexFormat = GeomVertexFormat.getV3c4() self.vertexData = GeomVertexData("data", vertexFormat, Geom.UHDynamic) geometry = Geom(self.vertexData) self.primitive = GeomPoints(Geom.UHDynamic) geometry.addPrimitive(self.primitive) node = GeomNode("node") node.addGeom(geometry) self.nodePath = NodePath(node) self.nodePath.setPos(0, 0, 0) self.nodePath.setRenderModeThickness(10.0) self.camera.setPos(0.0, -10.0, 0.0) self.disableMouse() self.nodePath.reparentTo(self.render)
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 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 make_response_pipe(pipe, request): """ returns the request hash and a compressed bam stream """ np.random.seed() # looky here! rh = request.hash_ n = 9999 positions = np.cumsum(np.random.randint(-1, 2, (n, 3)), axis=0) uuids = np.array(['%s' % uuid4() for _ in range(n)]) bounds = np.ones(n) * .5 example_coll = pickle.dumps( (positions, uuids, bounds)) # FIXME putting pickles last can bollox the STOP print('making example bam') #example_bam = makeSimpleGeom(positions, np.random.rand(4)).__reduce__()[1][-1] # the ONE way we can get this to work atm; GeomNode iirc; FIXME make sure -1 works every time #print('done making bam',example_bam) # XXX if you want this use repr() ffs array, ctup, geomType = positions, np.random.rand(4), GeomPoints 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') 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... example_bam = cloudNode.__reduce__()[1][-1] data_tuple = (example_bam, example_coll, b'this is a UI data I swear') data_stream = ResponseByteStream.makeResponseStream(rh, data_tuple) pipe.send_bytes(data_stream) pipe.close()
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 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 create_Triangle_Mesh_From_Vertices_and_Indices(vertices, indices, color_vec4=Vec4(1., 1., 1., 1.)): """ create a mesh out of flat arrays of vertices and indices, which were prepared to create a mesh made of triangles. """ # Own Geometry # format = GeomVertexFormat.getV3c4t2() format = GeomVertexFormat.getV3c4() vdata = GeomVertexData("colored_mesh_out_of_triangles", format, Geom.UHStatic) vdata.setNumRows(4) # let's also add color to each vertex colorWriter = GeomVertexWriter(vdata, "color") vertexPosWriter = GeomVertexWriter(vdata, "vertex") vertices = np.reshape(vertices, (-1, 3)) for v in vertices: vertexPosWriter.addData3f(v[0], v[1], v[2]) 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) indices = np.reshape(indices, (-1, 3)) for index_triple in indices: tris.addVertices(index_triple[0], index_triple[1], index_triple[2]) tris.closePrimitive() # make a Geom object to hold the primitives geom = Geom(vdata) # vdata contains the vertex position/color/... buffers geom.addPrimitive(tris) # tris contains the index buffer # now put geom in a GeomNode geom_node = GeomNode("colored_polygon_node") geom_node.addGeom(geom) return geom_node
def createColoredArrowGeomNode(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 is False: vertexPosWriter.addData3f( 0, 0, 0) vertexPosWriter.addData3f( 0, 0, 1) vertexPosWriter.addData3f(cos(pi / 6.), 0., .5) else: vertexPosWriter.addData3f( 0, 0, 0 - 0.5) vertexPosWriter.addData3f( 0, 0, 1 - 0.5) vertexPosWriter.addData3f(cos(pi / 6.), 0., .5 - 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) # 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, 2, 1) tris.closePrimitive() # the 1st primitive is finished # make a Geom object to hold the primitives quadGeom = Geom(vdata) quadGeom.addPrimitive(tris) # add the Geom object to a GeomNode geomNode = GeomNode("colored_arrowhead_node") geomNode.addGeom(quadGeom) return geomNode
def create_instance(self): self.vertexData = GeomVertexData('vertexData', GeomVertexFormat.getV3c4(), Geom.UHStatic) self.vertexWriter = GeomVertexWriter(self.vertexData, 'vertex') self.colorwriter = GeomVertexWriter(self.vertexData, 'color') #TODO: Ugly hack to calculate star position from the sun... old_cam_pos = self.context.observer.camera_global_pos self.context.observer.camera_global_pos = LPoint3d() center = LPoint3d() for segment in self.segments: if len(segment) < 2: continue for star in segment: #TODO: Temporary workaround to have star pos star.update(0) star.update_obs(self.context.observer) position, distance, scale_factor = self.get_real_pos_rel( star.rel_position, star.distance_to_obs, star.vector_to_obs) self.vertexWriter.addData3f(*position) self.colorwriter.addData4f(*self.color) self.context.observer.camera_global_pos = old_cam_pos self.lines = GeomLines(Geom.UHStatic) index = 0 for segment in self.segments: if len(segment) < 2: continue for i in range(len(segment) - 1): self.lines.addVertex(index) self.lines.addVertex(index + 1) self.lines.closePrimitive() index += 1 index += 1 self.geom = Geom(self.vertexData) self.geom.addPrimitive(self.lines) self.node = GeomNode("asterism") self.node.addGeom(self.geom) self.instance = NodePath(self.node) self.instance.setRenderModeThickness(settings.asterism_thickness) self.instance.reparentTo(self.context.annotation) self.instance.setBin('background', settings.asterisms_depth) self.instance.set_depth_write(False)
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 __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
import numpy as np from panda3d.core import Geom, GeomVertexFormat, GeomVertexData from .util.ipython import embed size = 1000 data = np.random.randint(0,1000,(size,3)) #color = np.random.randint(0,255,(size,4)) color = np.repeat(np.random.randint(0,255,(1,4)), size, 0) #full = np.hstack((data,color)) full = [tuple(d) for d in np.hstack((data,color))] #full = [tuple(*d,*color) for d in data] geom = GeomVertexData('points', GeomVertexFormat.getV3c4(), Geom.UHDynamic) geom.setNumRows(len(full)) array = geom.modifyArray(0) # need a writeable version handle = array.modifyHandle() #options are then the following: view = memoryview(array) arr = np.asarray(view) arr[:] = full embed() #OR #handle.copyDataFrom('some other handle to a GVDA') #handle.copySubataFrom(to_start, to_size, buffer, from_start, from_size)
def visualize(centers, corners, edges): from meshtool.filters.panda_filters.pandacore import getVertexData, attachLights, ensureCameraAt from meshtool.filters.panda_filters.pandacontrols import KeyboardMovement, MouseDrag, MouseScaleZoom, MouseCamera from panda3d.core import GeomPoints, GeomTriangles, Geom, GeomNode, GeomVertexFormat, GeomVertexData, GeomVertexWriter, LineSegs, VBase3 from direct.showbase.ShowBase import ShowBase format = GeomVertexFormat.getV3c4() vdata = GeomVertexData('pts', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') vertex_index = 0 center_vertex_indices = {} corner_vertex_indices = {} for key, center in centers.iteritems(): vertex.addData3f(center.x * X_SCALE, center.y * Y_SCALE, center.elevation * Z_SCALE) curcolor = hex2rgb(COLORS[center.biome]) color.addData4f(curcolor[0], curcolor[1], curcolor[2], 1) center_vertex_indices[key] = vertex_index vertex_index += 1 for corner in center.corners: vertex.addData3f(corner.x * X_SCALE, corner.y * Y_SCALE, corner.elevation * Z_SCALE) color.addData4f(curcolor[0], curcolor[1], curcolor[2], 1) corner_vertex_indices[corner.id] = vertex_index vertex_index += 1 tris = GeomTriangles(Geom.UHDynamic) for edge in edges.itervalues(): corner0 = edge.corner0 corner1 = edge.corner1 center0 = edge.center0 center1 = edge.center1 if corner0 is None or corner1 is None: continue tris.addVertices(corner_vertex_indices[corner1.id], corner_vertex_indices[corner0.id], center_vertex_indices[center0.id]) tris.addVertices(center_vertex_indices[center1.id], corner_vertex_indices[corner0.id], corner_vertex_indices[corner1.id]) tris.closePrimitive() pgeom = Geom(vdata) pgeom.addPrimitive(tris) node = GeomNode("primitive") node.addGeom(pgeom) p3dApp = ShowBase() attachLights(render) geomPath = render.attachNewNode(node) #geomPath.setRenderModeThickness(6.0) #geomPath.setRenderModeWireframe() ensureCameraAt(geomPath, base.cam) boundingSphere = geomPath.getBounds() base.cam.setPos(boundingSphere.getCenter() + boundingSphere.getRadius()) base.cam.lookAt(boundingSphere.getCenter()) geomPath.setScale(VBase3(50,50,50)) KeyboardMovement() #MouseDrag(geomPath) MouseCamera() MouseScaleZoom(geomPath) #render.setShaderAuto() p3dApp.run()
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()
def makeMountains(): format=GeomVertexFormat.getV3t2() vdata=GeomVertexData('mountains', format, Geom.UHStatic) vertex=GeomVertexWriter(vdata, 'vertex') texcoord=GeomVertexWriter(vdata, 'texcoord') format2=GeomVertexFormat.getV3c4() vdata2=GeomVertexData('sky', format2, Geom.UHStatic) vertex2=GeomVertexWriter(vdata2, 'vertex') color2=GeomVertexWriter(vdata2, 'color') numQuads=32 angle=0 textureX=0 angleAdd=math.pi*2/numQuads textureXAdd=1.0/numQuads currentQuad=0 numVertices=0 while currentQuad<numQuads: if currentQuad==0: vertexX=math.sin(angle)*3000 vertexY=math.cos(angle)*3000 vertex.addData3f(vertexX,vertexY,0.0) vertex.addData3f(vertexX,vertexY,360.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) numVertices=numVertices+2 vertexX=math.sin(angle)*3000 vertexY=math.cos(angle)*3000 vertex.addData3f(vertexX,vertexY,0.0) vertex.addData3f(vertexX,vertexY,360.0) vertex2.addData3f(vertexX,vertexY,360.0) color2.addData4f(45.0/255.0,112.0/255.0,255.0/255.0,1.0) #color2.addData4f(1.0,1.0,1.0,1.0) #print('created vertex at '+str(vertexX)+','+str(vertexY)+',2') #print('created vertex at '+str(vertexX)+','+str(vertexY)+',0') texcoord.addData2f(textureX, 0.0) texcoord.addData2f(textureX, 1.0) #print('texturex is '+str(textureX)) #print('creating vertices v'+str(numVertices)+' and v'+str(numVertices+1)) numVertices=numVertices+2 currentQuad=currentQuad+1 textureX=textureX+textureXAdd angle=angle+angleAdd vertex2.addData3f(0.0,0.0,360.0) #color2.addData4f(1.0,1.0,1.0,1.0) color2.addData4f(45.0/255.0,112.0/255.0,255.0/255.0,1.0) currentQuad=0 currentOffset=2 tris=GeomTriangles(Geom.UHDynamic) #print('creating tris - numVertices is '+str(numVertices)) while currentQuad<numQuads: vertexOffsets=[] for innerIndex in range(4): offset=currentOffset+innerIndex #print('comparing '+str(offset)+' with '+str(numVertices-1)) if offset>numVertices-1: offset=offset-numVertices vertexOffsets.append(offset) #print('adding tri connecting v'+str(vertexOffsets[0])+', v'+str(vertexOffsets[1])+', v'+str(vertexOffsets[3])) #print('adding tri connecting v'+str(vertexOffsets[1])+', v'+str(vertexOffsets[3])+', v'+str(vertexOffsets[2])) tris.addVertex(vertexOffsets[0]) tris.addVertex(vertexOffsets[2]) tris.addVertex(vertexOffsets[1]) tris.closePrimitive() tris.addVertex(vertexOffsets[1]) tris.addVertex(vertexOffsets[2]) tris.addVertex(vertexOffsets[3]) tris.closePrimitive() currentOffset=currentOffset+2 currentQuad=currentQuad+1 tris2=GeomTriangles(Geom.UHDynamic) currentOffset=1 numTris=numQuads currentTri=0 while currentTri<numTris: tris2.addVertex(numTris) tris2.addVertex(currentTri) if currentTri==numTris-1: tris2.addVertex(0) else: tris2.addVertex(currentTri+1) tris2.closePrimitive() currentTri=currentTri+1 mountains=Geom(vdata) mountains.addPrimitive(tris) sky=Geom(vdata2) sky.addPrimitive(tris2) snode=GeomNode('mountains') snode.addGeom(mountains) snode2=GeomNode('sky') snode2.addGeom(sky) returnValues={"mountains":snode,"sky":snode2} return returnValues