def init_node_path(self): if self.node_path: self.node_path.remove() vdata = GeomVertexData('name_me', self.format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') primitive = GeomLines(Geom.UHStatic) vertex.addData3f(*coords_to_panda(0, 0, 0)) vertex.addData3f(*coords_to_panda(1000, 0, 0)) vertex.addData3f(*coords_to_panda(0, 0, 0)) vertex.addData3f(*coords_to_panda(0, -1000, 0)) vertex.addData3f(*coords_to_panda(0, 0, 0)) vertex.addData3f(*coords_to_panda(0, 0, 1000)) color.addData4f(1.0, 0.0, 0.0, 1.0) color.addData4f(1.0, 0.0, 0.0, 1.0) color.addData4f(0.0, 1.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(0.0, 0.0, 1.0, 1.0) primitive.addNextVertices(6) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) node = GeomNode('gnode') node.addGeom(geom) self.node_path = self.parent.node_path_ui.attachNewNode(node)
def create_hexagon(radius): """ Creates a hexagon shape that is centered at (0,0,0) with the corners having a distance of radius to the center and the normals pointing in direction (0,-1,0). Returns the tuple (PandaNode, GeomVertexData). """ format = GeomVertexFormat.getV3n3c4t2() vdata=GeomVertexData('hexagon', format, Geom.UHStatic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') # create the vertices vertex.addData3f(0,0,0) normal.addData3f(0,-1,0) # add the other vertices for phi in range(0,360,60): # right-hand-rule (with middle finger pointing upwards): the y-axis points towards the screen, # therefore the hexagon will be created in the x,z plane, with x-axis pointing to the right # and the z-axis pointing up # get the next vertex coordinates by rotating the point (0,0,radius) in the x,z plane x,z = rotate_phi_degrees_counter_clockwise(phi, (0,radius)) #print (x,z) vertex.addData3f(x,0,z) normal.addData3f(0,-1,0) # the normal vector points away from the screen # add the vertices to a geometry primitives prim = GeomTrifans(Geom.UHStatic) for i in range(7): prim.addVertex(i) prim.addVertex(1) prim.closePrimitive() geom = Geom(vdata) geom.addPrimitive(prim) hex_node = GeomNode('') hex_node.addGeom(geom) return hex_node, vdata
def generate(self): format = GeomVertexFormat.getV3() data = GeomVertexData("Data", format, Geom.UHStatic) vertices = GeomVertexWriter(data, "vertex") vertices.addData3f(-self.w, -self.h, -self.d) vertices.addData3f(+self.w, -self.h, -self.d) vertices.addData3f(-self.w, +self.h, -self.d) vertices.addData3f(+self.w, +self.h, -self.d) vertices.addData3f(-self.w, -self.h, +self.d) vertices.addData3f(+self.w, -self.h, +self.d) vertices.addData3f(-self.w, +self.h, +self.d) vertices.addData3f(+self.w, +self.h, +self.d) triangles = GeomTriangles(Geom.UHStatic) def addQuad(v0, v1, v2, v3): triangles.addVertices(v0, v1, v2) triangles.addVertices(v0, v2, v3) triangles.closePrimitive() addQuad(4, 5, 7, 6) # Z+ addQuad(0, 2, 3, 1) # Z- addQuad(3, 7, 5, 1) # X+ addQuad(4, 6, 2, 0) # X- addQuad(2, 6, 7, 3) # Y+ addQuad(0, 1, 5, 4) # Y+ geom = Geom(data) geom.addPrimitive(triangles) node = GeomNode("BoxMaker") node.addGeom(geom) return NodePath(node)
def create_side(x_z_top_left, x_z_bottom_right, static=True): x1, z1 = x_z_top_left x2, z2 = x_z_bottom_right format = GeomVertexFormat.getV3n3c4t2() vdata = GeomVertexData('', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') vertex.addData3f(x1, 0, z1) # top left vertex.addData3f(x2, 0, z1) # top right vertex.addData3f(x2, 0, z2) # bottom right vertex.addData3f(x1, 0, z2) # bottom left for _i in range(4): normal.addData3f(0, - 1, 0) if static: prim_hint = Geom.UHStatic else: prim_hint = Geom.UHDynamic prim = GeomTristrips(prim_hint) prim.addVertices(1, 0, 2, 3) prim.closePrimitive() geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('') node.addGeom(geom) return (node, vdata)
def create_triangle(x_z_left, x_z_top, x_z_right, static=True): x1,z1 = x_z_left x2,z2 = x_z_top x3,z3 = x_z_right format = GeomVertexFormat.getV3n3c4t2() vdata=GeomVertexData('', format, Geom.UHStatic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') vertex.addData3f(x1, 0, z1) # left vertex.addData3f(x2, 0, z2) # top vertex.addData3f(x3, 0, z3) # right for _i in range(3): normal.addData3f(0,-1,0) if static: prim_hint = Geom.UHStatic else: prim_hint = Geom.UHDynamic prim = GeomTriangles(prim_hint) prim.addVertices(0,2,1) prim.closePrimitive() geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('') node.addGeom(geom) return node
def init_node_path(self): if self.node_path: self.node_path.remove() vdata = GeomVertexData('name_me', self.format, 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 * 0.75 vertex.addData3f(-x, 10000, z) vertex.addData3f(x, 10000, z) vertex.addData3f(-x, 10000, -z) vertex.addData3f(x, 10000, -z) color.addData4f(*[x / 255.0 for x in self.color1] + [1.0]) color.addData4f(*[x / 255.0 for x in self.color1] + [1.0]) color.addData4f(*[x / 255.0 for x in self.color2] + [1.0]) color.addData4f(*[x / 255.0 for x in self.color2] + [1.0]) primitive.addNextVertices(4) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) node = GeomNode('gnode') node.addGeom(geom) self.node_path = base.camera.attachNewNode(node)
def drawCircle(self, radius, axis, offset): # since we're doing line segments, just vertices in our geom format = GeomVertexFormat.getV3() # build our data structure and get a handle to the vertex column vdata = GeomVertexData('', format, Geom.UHStatic) vertices = GeomVertexWriter(vdata, 'vertex') # build a linestrip vertex buffer lines = GeomLinestrips(Geom.UHStatic) for i in range(0, self.subdiv): angle = i / float(self.subdiv) * 2.0 * math.pi ca = math.cos(angle) sa = math.sin(angle) if axis == "x": vertices.addData3f(0, radius * ca, radius * sa + offset) if axis == "y": vertices.addData3f(radius * ca, 0, radius * sa + offset) if axis == "z": vertices.addData3f(radius * ca, radius * sa, offset) for i in range(1, self.subdiv): lines.addVertices(i - 1, i) lines.addVertices(self.subdiv - 1, 0) lines.closePrimitive() geom = Geom(vdata) geom.addPrimitive(lines) # Add our primitive to the geomnode self.gnode.addGeom(geom)
def init_node_path_line(self, light_number): if self.node_path_line: self.node_path_line.remove() vdata = GeomVertexData('name_me', self.format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') primitive = GeomLines(Geom.UHStatic) vertex.addData3f(*coords_to_panda(0, 0, 0)) vertex.addData3f(*coords_to_panda(*self.direction.coords)) if light_number == 0: line_color = (0.0, 1.0, 1.0, 1.0) elif light_number == 1: line_color = (1.0, 0.0, 1.0, 1.0) elif light_number == 2: line_color = (1.0, 1.0, 0.0, 1.0) color.addData4f(*line_color) color.addData4f(*line_color) primitive.addNextVertices(2) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) node = GeomNode('gnode') node.addGeom(geom) self.node_path_line = self.parent.node_path_ui.attachNewNode(node) self.node_path_line.setScale(1000) self.node_path_line.setPos(100, 100, 0) self.show_line(self.parent.parent.lights_edit_window.IsShown())
def init_node_path(self): if self.node_path: self.node_path.remove() polygon = self.source vdata = GeomVertexData('name_me', self.format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') primitive = GeomTristrips(Geom.UHStatic) vertex.addData3f(*coords_to_panda(*polygon.A.point.coords)) vertex.addData3f(*coords_to_panda(*polygon.B.point.coords)) vertex.addData3f(*coords_to_panda(*polygon.C.point.coords)) if hasattr(polygon, 'D'): vertex.addData3f(*coords_to_panda(*polygon.D.point.coords)) else: vertex.addData3f(*coords_to_panda(*polygon.C.point.coords)) if polygon.A.normal: normal.addData3f(*coords_to_panda(*polygon.A.normal.coords)) normal.addData3f(*coords_to_panda(*polygon.B.normal.coords)) normal.addData3f(*coords_to_panda(*polygon.C.normal.coords)) if hasattr(polygon, 'D'): normal.addData3f(*coords_to_panda(*polygon.D.normal.coords)) if polygon.A.normal: gray = 1.0 else: gray = 0.0 self.old_color = (gray, gray, gray, 1.0) color.addData4f(gray, gray, gray, 1.0) color.addData4f(gray, gray, gray, 1.0) color.addData4f(gray, gray, gray, 1.0) if hasattr(polygon, 'D'): color.addData4f(gray, gray, gray, 1.0) if polygon.A.texcoord: pal = ((polygon.texture_palette + 1) * 256) texcoord_A = uv_to_panda2(polygon, pal, *polygon.A.texcoord.coords) texcoord_B = uv_to_panda2(polygon, pal, *polygon.B.texcoord.coords) texcoord_C = uv_to_panda2(polygon, pal, *polygon.C.texcoord.coords) texcoord.addData2f(*texcoord_A) texcoord.addData2f(*texcoord_B) texcoord.addData2f(*texcoord_C) if hasattr(polygon, 'D'): texcoord_D = uv_to_panda2(polygon, pal, *polygon.D.texcoord.coords) texcoord.addData2f(*texcoord_D) primitive.addNextVertices(4) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) node = GeomNode('gnode') node.addGeom(geom) self.node_path = self.parent.node_path_mesh.attachNewNode(node)
def __init__(self, subdivides=3, scale=1.0): super(SphereNode, self).__init__('sphere') uniform = True # see if scale is a tuple try: xs, ys, zs = scale uniform = False except TypeError: # no, it's a scalar xs, ys, zs = scale, scale, scale north = (0.0, 1.0, 0.0) g = Octahedron() for i in range(subdivides): g.UniformSubdivide(midpointdisplace=NormalizeVert) #print "%d faces per sphere"%len(g.faces) # okay, we're gonna use setShaderInput to set constants for # surface coverages and planetary seed, so all we need per # vertex is position and normal # and we kind of don't need normal for a unit sphere, but # we want to use the same shader for other thangs format = GeomVertexFormat.getV3n3() vdata = GeomVertexData('sphere', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') for (x, y, z) in g.verts: vertex.addData3f(x * xs, y * ys, z * zs) if uniform: normal.addData3f(x, y, z) else: n = NormalizeVert( (x / (xs * xs), y / (ys * ys), z / (zs * zs))) normal.addData3f(n[0], n[1], n[2]) trilist = GeomTriangles(Geom.UHDynamic) for (a, b, c) in g.faces: trilist.addVertex(a) trilist.addVertex(b) trilist.addVertex(c) trilist.closePrimitive() self.geom = Geom(vdata) self.geom.addPrimitive(trilist) self.addGeom(self.geom)
def init_node_path(self): if self.node_path: self.node_path.remove() vdata = GeomVertexData('name_me', self.format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') primitive = GeomTristrips(Geom.UHStatic) y = self.height * 12 + self.depth * 12 + 1 try: (slope, rotation) = slope_types[self.slope_type] except KeyError: print 'Unknown slope type:', self.slope_type (slope, rotation) = (flat, 0) if self.slope_height == 0: (slope, rotation) = (flat, 0) scale_y = self.slope_height * 12 vertex.addData3f(*coords_to_panda(-14.0, -slope['sw'] * scale_y, -14.0)) vertex.addData3f(*coords_to_panda(-14.0, -slope['nw'] * scale_y, 14.0)) vertex.addData3f(*coords_to_panda(14.0, -slope['ne'] * scale_y, 14.0)) vertex.addData3f(*coords_to_panda(14.0, -slope['se'] * scale_y, -14.0)) vertex.addData3f(*coords_to_panda(-14.0, -slope['sw'] * scale_y, -14.0)) tile_color = (0.5, 0.5, 1.0) if self.cant_walk: tile_color = (1.0, 0.5, 0.5) if self.cant_cursor: tile_color = (0.5, 0.0, 0.0) if (self.x + self.z) % 2 == 0: tile_color = tuple([x * 0.8 for x in tile_color]) self.tile_color = tile_color color.addData4f(*tile_color + (1.0,)) color.addData4f(*tile_color + (1.0,)) color.addData4f(*tile_color + (1.0,)) color.addData4f(*tile_color + (1.0,)) color.addData4f(*tile_color + (1.0,)) primitive.addNextVertices(5) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) node = GeomNode('gnode') node.addGeom(geom) self.node_path = self.parent.node_path.attachNewNode(node) self.node_path.setH(rotation) can_stand_height = 0 if not self.cant_cursor: can_stand_height = 1 self.node_path.setPos(*coords_to_panda(self.x * 28 + 14, -((self.height + self.depth) * 12 + 1 + can_stand_height), self.z * 28 + 14)) self.node_path.setTag('terrain_xyz', '%u,%u,%u' % (self.x, self.y, self.z))
def create_line(x1, z1, x2, z2): format = GeomVertexFormat.getV3n3c4t2() vdata = GeomVertexData('', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') vertex.addData3f(x1, 0, z1) vertex.addData3f(x2, 0, z2) for _i in range(2): normal.addData3f(0, - 1, 0) prim_hint = Geom.UHStatic prim = GeomLines(prim_hint) prim.addVertices(0, 1) prim.closePrimitive() geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('') node.addGeom(geom) return (node, vdata)
def __init__(self, scale = 0.125, length = Vec3(4, 4, 7), position = Vec3(0, 0, 0), iterations = 10, num_branches = 4, vector_list = [Vec3(0, 0, 1), Vec3(1, 0, 0), Vec3(0, -1, 0)]): self.set_model(NodePath("SimpleTree")) self.format = SimpleTree.get_format() self.vdata = GeomVertexData("body vertices", self.format, Geom.UHStatic) self.position = position self.length = length self.vector_list = vector_list self.iterations = iterations self.num_branches = num_branches self.scale = scale self.bark_texture = base.loader.loadTexture(APP_PATH + 'media/textures/trees/bark.jpg')
def init_node_path(self): if self.node_path: self.node_path.remove() vdata = GeomVertexData('name_me', self.format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') primitive = GeomTristrips(Geom.UHStatic) vertex.addData3f(-2.0, -2.0, -2.0) vertex.addData3f(2.0, -2.0, -2.0) vertex.addData3f(0, 2.0, -2.0) vertex.addData3f(0, 0, 2.0) vertex.addData3f(-2.0, -2.0, -2.0) vertex.addData3f(0, 0, 2.0) vertex.addData3f(2.0, -2.0, -2.0) vertex.addData3f(0, 0, 2.0) vertex.addData3f(0, 2.0, -2.0) color_tuple = (1.0, 1.0, 1.0) if self.point == 'A': color_tuple = (1.0, 0.0, 0.0) elif self.point == 'B': color_tuple = (0.0, 1.0, 0.0) elif self.point == 'C': color_tuple = (0.0, 0.0, 1.0) elif self.point == 'D': color_tuple = (1.0, 1.0, 0.0) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) color.addData4f(*color_tuple + (1.0,)) primitive.addNextVertices(9) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) node = GeomNode('gnode') node.addGeom(geom) self.node_path = self.parent.parent.node_path_ui.attachNewNode(node) self.node_path.setP(180) self.node_path.setPos(*coords_to_panda(*self.coords))
def init_node_path(self): if self.node_path: self.node_path.remove() vdata = GeomVertexData('name_me', self.format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') primitive = GeomLines(Geom.UHStatic) vertex.addData3f(*coords_to_panda(*self.vector)) vertex.addData3f(*coords_to_panda(*[-x for x in self.vector])) color.addData4f((1.0, 0.0, 0.0, 1.0,)) color.addData4f((0.0, 0.0, 1.0, 1.0,)) primitive.addNextVertices(2) primitive.closePrimitive() geom = Geom(vdata) geom.addPrimitive(primitive) node = GeomNode('gnode') node.addGeom(geom) self.node_path = self.parent.parent.node_path_ui.attachNewNode(node) self.node_path.setScale(20) self.node_path.setPos(*coords_to_panda(*self.coords))
def drawRect(self, width, height, axis): # since we're doing line segments, just vertices in our geom format = GeomVertexFormat.getV3() # build our data structure and get a handle to the vertex column vdata = GeomVertexData('', format, Geom.UHStatic) vertices = GeomVertexWriter(vdata, 'vertex') # build a linestrip vertex buffer lines = GeomLinestrips(Geom.UHStatic) # draw a box if axis == "x": vertices.addData3f(0, -width, -height) vertices.addData3f(0, width, -height) vertices.addData3f(0, width, height) vertices.addData3f(0, -width, height) if axis == "y": vertices.addData3f(-width, 0, -height) vertices.addData3f(width, 0, -height) vertices.addData3f(width, 0, height) vertices.addData3f(-width, 0, height) if axis == "z": vertices.addData3f(-width, -height, 0) vertices.addData3f(width, -height, 0) vertices.addData3f(width, height, 0) vertices.addData3f(-width, height, 0) for i in range(1, 3): lines.addVertices(i - 1, i) lines.addVertices(3, 0) lines.closePrimitive() geom = Geom(vdata) geom.addPrimitive(lines) # Add our primitive to the geomnode self.gnode.addGeom(geom)
def drawLine(self, start, end): # since we're doing line segments, just vertices in our geom format = GeomVertexFormat.getV3() # build our data structure and get a handle to the vertex column vdata = GeomVertexData('', format, Geom.UHStatic) vertices = GeomVertexWriter(vdata, 'vertex') # build a linestrip vertex buffer lines = GeomLinestrips(Geom.UHStatic) vertices.addData3f(start[0], start[1], start[2]) vertices.addData3f(end[0], end[1], end[2]) lines.addVertices(0, 1) lines.closePrimitive() geom = Geom(vdata) geom.addPrimitive(lines) # Add our primitive to the geomnode self.gnode.addGeom(geom)
def draw(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') circle = Geom(vdata) # Create vertices vertex.addData3f(self.pos) color.addData4f(self.color) for v in range(self._EDGES): x = self.pos.getX() + (self.size * math.cos( (2 * math.pi / self._EDGES) * v)) y = self.pos.getY() + (self.size * math.sin( (2 * math.pi / self._EDGES) * v)) z = self.pos.getZ() vertex.addData3f(x, y, z) color.addData4f(self.color) # Create triangles for t in range(self._EDGES): tri = GeomTriangles(Geom.UHDynamic) tri.addVertex(0) tri.addVertex(t + 1) if (t + 2) > self._EDGES: tri.addVertex(1) else: tri.addVertex(t + 2) tri.closePrimitive() circle.addPrimitive(tri) gn = GeomNode('Circle') gn.addGeom(circle) np = NodePath(gn) np.setHpr(0, 90, 0) return np
def pandaRender(self): frameList = [] for node in self.compositeFrames.getiterator('composite-frame'): if node.tag == "composite-frame" and node.attrib.get("id") == str(self.internalFrameIndex): for frameCallNode in node: for frameNode in self.frames.getiterator('frame'): if frameNode.tag == "frame" and frameNode.attrib.get("id") == frameCallNode.attrib.get("id"): offsetX = 0 if frameCallNode.attrib.get("offset-x") == None else float(frameCallNode.attrib.get("offset-x")) offsetY = 0 if frameCallNode.attrib.get("offset-y") == None else float(frameCallNode.attrib.get("offset-y")) tweenId = frameCallNode.attrib.get("tween") frameInTween = 0 if frameCallNode.attrib.get("frame-in-tween") == None else int(frameCallNode.attrib.get("frame-in-tween")) addWidth = 0 if frameNode.attrib.get("w") == None else float(frameNode.attrib.get("w")) addHeight = 0 if frameNode.attrib.get("h") == None else float(frameNode.attrib.get("h")) sInPixels = 0 if frameNode.attrib.get("s") == None else float(frameNode.attrib.get("s")) tInPixels = 0 if frameNode.attrib.get("t") == None else float(frameNode.attrib.get("t")) swInPixels = sInPixels + addWidth thInPixels = tInPixels + addHeight s = (sInPixels / self.baseWidth) t = 1 - (tInPixels / self.baseHeight) # Complemented to deal with loading image upside down. S = (swInPixels / self.baseWidth) T = 1 - (thInPixels / self.baseHeight) # Complemented to deal with loading image upside down. blend = "overwrite" if frameCallNode.attrib.get("blend") == None else frameCallNode.attrib.get("blend") scaleX = 1 if frameCallNode.attrib.get("scale-x") == None else float(frameCallNode.attrib.get("scale-x")) scaleY = 1 if frameCallNode.attrib.get("scale-y") == None else float(frameCallNode.attrib.get("scale-y")) color = Color(1,1,1,1) tweenHasColor = False frameCallHasColor = False frameCallColorName = frameCallNode.attrib.get("color-name") if frameCallColorName != None: # Get color at frame call as first resort. frameCallHasColor = True for colorNode in self.colors.getiterator('color'): if colorNode.tag == 'color' and colorNode.attrib.get("name") == frameCallColorName: R = 1 if colorNode.attrib.get("r") == None else float(colorNode.attrib.get("r")) G = 1 if colorNode.attrib.get("g") == None else float(colorNode.attrib.get("g")) B = 1 if colorNode.attrib.get("b") == None else float(colorNode.attrib.get("b")) A = 1 if colorNode.attrib.get("a") == None else float(colorNode.attrib.get("a")) color = Color(R, G, B, A) break # leave for loop when we find the correct color pass if tweenId != None and tweenId != "0": # Get color at tween frame as second resort. thisTween = None frameLength = 1 advancementFunction = "linear" foundTween = False pointList = [] colorList = [] for tweenNode in self.tweens.getiterator('motion-tween'): if tweenNode.tag == "motion-tween" and tweenNode.attrib.get("id") == tweenId: foundTween = True frameLength = 1 if tweenNode.attrib.get("length-in-frames") == None else tweenNode.attrib.get("length-in-frames") advancementFunction = "linear" if tweenNode.attrib.get("advancement-function") == None else tweenNode.attrib.get("advancement-function") for pointOrColorNode in tweenNode.getiterator(): if pointOrColorNode.tag == "point": pX = 0 if pointOrColorNode.attrib.get("x") == None else float(pointOrColorNode.attrib.get("x")) pY = 0 if pointOrColorNode.attrib.get("y") == None else float(pointOrColorNode.attrib.get("y")) pointList.append(Point(pX, pY, 0)) elif pointOrColorNode.tag == "color-state": colorName = "white" if pointOrColorNode.attrib.get("name") == None else pointOrColorNode.attrib.get("name") for colorNode in self.colors.getiterator('color'): if colorNode.tag == 'color' and colorNode.attrib.get("name") == colorName: R = 1 if colorNode.attrib.get("r") == None else float(colorNode.attrib.get("r")) G = 1 if colorNode.attrib.get("g") == None else float(colorNode.attrib.get("g")) B = 1 if colorNode.attrib.get("b") == None else float(colorNode.attrib.get("b")) A = 1 if colorNode.attrib.get("a") == None else float(colorNode.attrib.get("a")) colorList.append(Color(R, G, B, A)) break # leave for loop when we find the correct color reference pass # Run through all child nodes of selected tween break # Exit after finding correct tween pass if foundTween: thisTween = Tween(frameLength, advancementFunction, pointList, colorList) offset = thisTween.XYFromFrame(frameInTween); offsetFromTweenX = int(offset.X); offsetFromTweenY = int(offset.Y); offsetX += int(offset.X); offsetY += int(offset.Y); if thisTween.hasColorComponent(): tweenHasColor = True; if frameCallHasColor == False: color = thisTween.colorFromFrame(frameInTween); pass if frameNode.attrib.get("color-name") != None and frameCallHasColor == False and tweenHasColor == False: # Get color at frame definition as last resort. for colorNode in colors.getiterator('color'): if colorNode.tag == 'color' and colorNode.attrib.get("name") == frameNode.attrib.get("color-name"): R = 1 if colorNode.attrib.get("r") == None else float(colorNode.attrib.get("r")) G = 1 if colorNode.attrib.get("g") == None else float(colorNode.attrib.get("g")) B = 1 if colorNode.attrib.get("b") == None else float(colorNode.attrib.get("b")) A = 1 if colorNode.attrib.get("a") == None else float(colorNode.attrib.get("a")) color = Color(R, G, B, A) break # leave for loop when we find the correct color pass rotationZ = 0 if frameCallNode.attrib.get("rotation-z") == None else float(frameCallNode.attrib.get("rotation-z")) frameList.append(Frame(Bound(offsetX, offsetY, addWidth, addHeight), s, t, S, T, blend, scaleX, scaleY, color, rotationZ)) pass break # Leave once we've found the appropriate frame # Prepare tracking list of consumed nodes. self.clearNodesForDrawing() # Make an identifier to tack onto primitive names in Panda3d's scene graph. frameIndexForName = 1 # Loop through loaded frames that make up composite frame. for loadedFrame in frameList: # For debugging purposes, print the object. if False: loadedFrame.printAsString() # Set up place to store primitive 3d object; note: requires vertex data made by GeomVertexData squareMadeByTriangleStrips = GeomTristrips(Geom.UHDynamic) # Set up place to hold 3d data and for the following coordinates: # square's points (V3: x, y, z), # the colors at each point of the square (c4: r, g, b, a), and # for the UV texture coordinates at each point of the square (t2: S, T). vertexData = GeomVertexData('square-'+str(frameIndexForName), GeomVertexFormat.getV3c4t2(), Geom.UHDynamic) vertex = GeomVertexWriter(vertexData, 'vertex') color = GeomVertexWriter(vertexData, 'color') texcoord = GeomVertexWriter(vertexData, 'texcoord') # Add the square's data # Upper-Left corner of square vertex.addData3f(-loadedFrame.bound.Width / 2.0, 0, -loadedFrame.bound.Height / 2.0) color.addData4f(loadedFrame.color.R,loadedFrame.color.G,loadedFrame.color.B,loadedFrame.color.A) texcoord.addData2f(loadedFrame.s, loadedFrame.T) # Upper-Right corner of square vertex.addData3f(loadedFrame.bound.Width / 2.0, 0, -loadedFrame.bound.Height / 2.0) color.addData4f(loadedFrame.color.R,loadedFrame.color.G,loadedFrame.color.B,loadedFrame.color.A) texcoord.addData2f(loadedFrame.S, loadedFrame.T) # Lower-Left corner of square vertex.addData3f(-loadedFrame.bound.Width / 2.0, 0, loadedFrame.bound.Height / 2.0) color.addData4f(loadedFrame.color.R,loadedFrame.color.G,loadedFrame.color.B,loadedFrame.color.A) texcoord.addData2f(loadedFrame.s, loadedFrame.t) # Lower-Right corner of square vertex.addData3f(loadedFrame.bound.Width / 2.0, 0, loadedFrame.bound.Height / 2.0) color.addData4f(loadedFrame.color.R,loadedFrame.color.G,loadedFrame.color.B,loadedFrame.color.A) texcoord.addData2f(loadedFrame.S, loadedFrame.t) # Pass data to primitive squareMadeByTriangleStrips.addNextVertices(4) squareMadeByTriangleStrips.closePrimitive() square = Geom(vertexData) square.addPrimitive(squareMadeByTriangleStrips) # Pass primtive to drawing node drawPrimitiveNode=GeomNode('square-'+str(frameIndexForName)) drawPrimitiveNode.addGeom(square) # Pass node to scene (effect camera) nodePath = self.effectCameraNodePath.attachNewNode(drawPrimitiveNode) # Linear dodge: if loadedFrame.blendMode == "darken": nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OOneMinusFbufferColor, ColorBlendAttrib.OOneMinusIncomingColor)) pass elif loadedFrame.blendMode == "multiply": nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OFbufferColor, ColorBlendAttrib.OZero)) pass elif loadedFrame.blendMode == "color-burn": nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OZero, ColorBlendAttrib.OOneMinusIncomingColor)) pass elif loadedFrame.blendMode == "linear-burn": nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OZero, ColorBlendAttrib.OIncomingColor)) pass elif loadedFrame.blendMode == "lighten": nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MMax, ColorBlendAttrib.OIncomingColor, ColorBlendAttrib.OFbufferColor)) pass elif loadedFrame.blendMode == "color-dodge": nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OOne, ColorBlendAttrib.OOne)) pass elif loadedFrame.blendMode == "linear-dodge": nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OOne, ColorBlendAttrib.OOneMinusIncomingColor)) pass else: # Overwrite: nodePath.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OIncomingAlpha, ColorBlendAttrib.OOneMinusIncomingAlpha)) pass nodePath.setDepthTest(False) # Apply texture nodePath.setTexture(self.tex) # Apply translation, then rotation, then scaling to node. nodePath.setPos((loadedFrame.bound.X + loadedFrame.bound.Width / 2.0, 1, -loadedFrame.bound.Y - loadedFrame.bound.Height / 2.0)) nodePath.setR(loadedFrame.rotationZ) nodePath.setScale(loadedFrame.scaleX, 1, loadedFrame.scaleY) nodePath.setTwoSided(True) self.consumedNodesList.append(nodePath) frameIndexForName = frameIndexForName + 1 # Loop continues on through each frame called in the composite frame. pass
def __init__(self, inner, outer, sectors): super(RingNode, self).__init__('ring') self.inner = inner self.outer = outer self.sectors = sectors ringgeo = RawGeometry() # inner and outer radii are the true circular limits, so expand a little bit # for the sectored mesh mesh_inner = self.inner * 0.9 mesh_outer = self.outer * 1.1 angular_width = 2.0 * math.pi / self.sectors for sector in range(self.sectors): start = sector * angular_width end = (sector + 1) * angular_width # add a quad x0 = math.sin(start) * mesh_inner x1 = math.sin(start) * mesh_outer x2 = math.sin(end) * mesh_inner x3 = math.sin(end) * mesh_outer z0 = math.cos(start) * mesh_inner z1 = math.cos(start) * mesh_outer z2 = math.cos(end) * mesh_inner z3 = math.cos(end) * mesh_outer index = len(ringgeo.verts) ringgeo.verts.append((x0, 0, z0)) ringgeo.verts.append((x1, 0, z1)) ringgeo.verts.append((x2, 0, z2)) ringgeo.verts.append((x3, 0, z3)) # double-side the faces so they render from either side # top pair... ringgeo.faces.append((index + 0, index + 1, index + 2)) ringgeo.faces.append((index + 1, index + 3, index + 2)) # bottom pair... ringgeo.faces.append((index + 0, index + 2, index + 1)) ringgeo.faces.append((index + 1, index + 2, index + 3)) format = GeomVertexFormat.getV3n3() vdata = GeomVertexData('ring', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') for (x, y, z) in ringgeo.verts: vertex.addData3f(x, y, z) normal.addData3f(0, 1, 0) trilist = GeomTriangles(Geom.UHDynamic) for (a, b, c) in ringgeo.faces: trilist.addVertex(a) trilist.addVertex(b) trilist.addVertex(c) trilist.closePrimitive() ring = Geom(vdata) ring.addPrimitive(trilist) self.addGeom(ring)
def addGeometry(self, geomData): debugGui = dict() format = GeomVertexFormat.getV3n3t2() vdata = GeomVertexData('name', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') prim = GeomTriangles(Geom.UHStatic) postphonedTriangles = list() vtxTargetId0 = vtxTargetId1 = vtxTargetId2 = None vtxDataCounter = 0 for vtxSourceId0, vtxSourceId1, vtxSourceId2 in geomData.triangles: vx0,vy0,vz0 = v0 = geomData.getVertex(vtxSourceId0) vx1,vy1,vz1 = v1 = geomData.getVertex(vtxSourceId1) vx2,vy2,vz2 = v2 = geomData.getVertex(vtxSourceId2) # prepare the vertices uvx0, uvy0 = uv0 = geomData.getUv(vtxSourceId0) uvx1, uvy1 = uv1 = geomData.getUv(vtxSourceId1) uvx2, uvy2 = uv2 = geomData.getUv(vtxSourceId2) # n0 = geomData.getNormal(vtxSourceId0) n1 = geomData.getNormal(vtxSourceId1) n2 = geomData.getNormal(vtxSourceId2) # make it wrap nicely if min(uvx0,uvx1,uvx2) < .25 and max(uvx0,uvx1,uvx2) > 0.75: if uvx0 < 0.25: uvx0 += 1.0 if uvx1 < 0.25: uvx1 += 1.0 if uvx2 < 0.25: uvx2 += 1.0 vertex.addData3f(*v0) normal.addData3f(*n0) texcoord.addData2f(*uv0) vtxTargetId0 = vtxDataCounter vtxDataCounter += 1 vertex.addData3f(*v1) normal.addData3f(*n1) texcoord.addData2f(*uv1) vtxTargetId1 = vtxDataCounter vtxDataCounter += 1 vertex.addData3f(*v2) normal.addData3f(*n2) texcoord.addData2f(*uv2) vtxTargetId2 = vtxDataCounter vtxDataCounter += 1 prim.addVertex(vtxTargetId0) prim.addVertex(vtxTargetId1) prim.addVertex(vtxTargetId2) prim.closePrimitive() if False: if vtxSourceId0 not in debugGui: i = InfoTextBillaboarded(render) i.setScale(0.05) i.billboardNodePath.setPos(Vec3(x0,y0,z0)*1.1) i.setText('%i: %.1f %.1f %.1f\n%.1f %.1f' % (vtxSourceId0, x0,y0,z0, nx0, ny0)) debugGui[vtxSourceId0] = i if vtxSourceId1 not in debugGui: i = InfoTextBillaboarded(render) i.setScale(0.05) i.billboardNodePath.setPos(Vec3(x1,y1,z1)*1.1) i.setText('%i: %.1f %.1f %.1f\n%.1f %.1f' % (vtxSourceId1, x1,y1,z1, nx1, ny1)) debugGui[vtxSourceId1] = i if vtxSourceId2 not in debugGui: i = InfoTextBillaboarded(render) i.setScale(0.05) i.billboardNodePath.setPos(Vec3(x2,y2,z2)*1.1) i.setText('%i: %.1f %.1f %.1f\n%.1f %.1f' % (vtxSourceId2, x2,y2,z2, nx2, ny2)) debugGui[vtxSourceId2] = i geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) nodePath = self.attachNewNode(node) return nodePath
class SimpleTree(Entity): geom_vertex_format = None def get_format(): if SimpleTree.geom_vertex_format is None: format_array = GeomVertexArrayFormat() format_array.addColumn(InternalName.make("drawFlag"), 1, Geom.NTUint8, Geom.COther) format = GeomVertexFormat(GeomVertexFormat.getV3n3cpt2()) format.addArray(format_array) SimpleTree.geom_vertex_format = GeomVertexFormat.registerFormat(format) return SimpleTree.geom_vertex_format get_format = staticmethod(get_format) def __init__(self, scale = 0.125, length = Vec3(4, 4, 7), position = Vec3(0, 0, 0), iterations = 10, num_branches = 4, vector_list = [Vec3(0, 0, 1), Vec3(1, 0, 0), Vec3(0, -1, 0)]): self.set_model(NodePath("SimpleTree")) self.format = SimpleTree.get_format() self.vdata = GeomVertexData("body vertices", self.format, Geom.UHStatic) self.position = position self.length = length self.vector_list = vector_list self.iterations = iterations self.num_branches = num_branches self.scale = scale self.bark_texture = base.loader.loadTexture(APP_PATH + 'media/textures/trees/bark.jpg') def generate(self): self.recurse(self.length, self.position, self.iterations, self.vector_list) self.get_model().setTexture(self.bark_texture, 1) self.reparentTo(base.render) def recurse(self, length, position, iterations, vector_list): if iterations > 0: self.draw_body(position, vector_list, length.getX()) # move forward along the right axis new_position = position + vector_list[0] * length.length() # Only branch every third level if iterations % 3 == 0: # decrease dimensions when we branch length = Vec3(length.getX() / 2, length.getY() / 2, length.getZ() / 1.1) for i in range(self.num_branches): self.recurse(length, new_position, iterations - 1, ProceduralUtility.random_axis(vector_list)) else: # just make another branch connected to this one with a small # variation in direction self.recurse(length, new_position, iterations - 1, ProceduralUtility.small_random_axis(vector_list)) else: self.draw_body(position, vector_list, length.getX(), False) self.draw_leaf(position, vector_list, self.scale) def draw_body(self, position, vector_list, radius = 1, keep_drawing = True, num_vertices = 8): circle_geom = Geom(self.vdata) vertex_writer = GeomVertexWriter(self.vdata, "vertex") color_writer = GeomVertexWriter(self.vdata, "color") normal_writer = GeomVertexWriter(self.vdata, "normal") draw_rewriter = GeomVertexRewriter(self.vdata, "drawFlag") tex_rewriter = GeomVertexRewriter(self.vdata, "texcoord") start_row = self.vdata.getNumRows() vertex_writer.setRow(start_row) color_writer.setRow(start_row) normal_writer.setRow(start_row) sCoord = 0 if start_row != 0: tex_rewriter.setRow(start_row - num_vertices) sCoord = tex_rewriter.getData2f().getX() + 1 draw_rewriter.setRow(start_row - num_vertices) if draw_rewriter.getData1f() == False: sCoord -= 1 draw_rewriter.setRow(start_row) tex_rewriter.setRow(start_row) angle_slice = 2 * math.pi / num_vertices current_angle = 0 perp1 = vector_list[1] perp2 = vector_list[2] # write vertex information for i in range(num_vertices): adjacent_circle = position + (perp1 * math.cos(current_angle) + perp2 * math.sin(current_angle)) * radius normal = perp1 * math.cos(current_angle) + perp2 * math.sin(current_angle) normal_writer.addData3f(normal) vertex_writer.addData3f(adjacent_circle) tex_rewriter.addData2f(sCoord, (i + 0.001) / (num_vertices - 1)) color_writer.addData4f(0.5, 0.5, 0.5, 1.0) draw_rewriter.addData1f(keep_drawing) current_angle += angle_slice draw_reader = GeomVertexReader(self.vdata, "drawFlag") draw_reader.setRow(start_row - num_vertices) # we can't draw quads directly so use Tristrips if start_row != 0 and draw_reader.getData1f() != False: lines = GeomTristrips(Geom.UHStatic) half = int(num_vertices * 0.5) for i in range(num_vertices): lines.addVertex(i + start_row) if i < half: lines.addVertex(i + start_row - half) else: lines.addVertex(i + start_row - half - num_vertices) lines.addVertex(start_row) lines.addVertex(start_row - half) lines.closePrimitive() lines.decompose() circle_geom.addPrimitive(lines) circle_geom_node = GeomNode("Debug") circle_geom_node.addGeom(circle_geom) circle_geom_node.setAttrib(CullFaceAttrib.makeReverse(), 1) self.get_model().attachNewNode(circle_geom_node) def draw_leaf(self, position, vector_list, scale = 0.125): # use the vectors that describe the direction the branch grows # to make the right rotation matrix new_cs = Mat4(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) new_cs.setRow(0, vector_list[2]) # right new_cs.setRow(1, vector_list[1]) # up new_cs.setRow(2, vector_list[0]) # forward new_cs.setRow(3, Vec3(0, 0, 0)) new_cs.setCol(3, Vec4(0, 0, 0, 1)) axis_adjustment = Mat4.scaleMat(scale) * new_cs * Mat4.translateMat(position) leaf_model = base.loader.loadModelCopy(APP_PATH + 'media/models/shrubbery') leaf_texture = base.loader.loadTexture(APP_PATH + 'media/models/material-10-cl.png') leaf_model.reparentTo(self.get_model()) leaf_model.setTexture(leaf_texture, 1) leaf_model.setTransform(TransformState.makeMat(axis_adjustment))
def visualize(self, parentNodePath, highlightVerts=[], pathVerts=[], visitedVerts=[]): ''' XXX Should move this into a product-specific class. ''' gFormat = GeomVertexFormat.getV3cp() self.visVertexData = GeomVertexData("OMGVERTEXDATA2", gFormat, Geom.UHDynamic) self.visVertexWriter = GeomVertexWriter(self.visVertexData, "vertex") self.visVertexColorWriter = GeomVertexWriter(self.visVertexData, "color") vertToWriterIndex = {} currIndex = 0 for v in self.vertexCoords.keys(): vertToWriterIndex[v] = currIndex x = self.vertexCoords[v][0] y = self.vertexCoords[v][1] z = self.vertexCoords[v][2] self.visVertexWriter.addData3f(x, y, z + 0.5) if v in highlightVerts: self.visVertexColorWriter.addData4f(1.0, 0.0, 0.0, 1.0) elif v in visitedVerts: self.visVertexColorWriter.addData4f(0.0, 0.0, 1.0, 1.0) else: self.visVertexColorWriter.addData4f(1.0, 1.0, 0.0, 1.0) currIndex += 1 pathOffsetIntoIndex = currIndex for v in pathVerts: self.visVertexWriter.addData3f(v[0], v[1], v[2] + 0.5) self.visVertexColorWriter.addData4f(0.0, 1.0, 0.0, 1.0) currIndex += 1 lines = GeomLinestrips(Geom.UHStatic) for p in self.polyToVerts.keys(): for v in self.polyToVerts[p]: lines.addVertex(vertToWriterIndex[v]) lines.addVertex(vertToWriterIndex[self.polyToVerts[p][0]]) lines.closePrimitive() if len(pathVerts) > 0: for i in xrange(len(pathVerts)): lines.addVertex(pathOffsetIntoIndex + i) lines.closePrimitive() self.visGeom = Geom(self.visVertexData) self.visGeom.addPrimitive(lines) self.visGN = GeomNode("NavMeshVis") self.visGN.addGeom(self.visGeom) self.visNodePath = parentNodePath.attachNewNode(self.visGN) self.visNodePath.setTwoSided(True)
def draw(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHStatic) 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.addData3f(self.x1, self.y1, self.z1) vertex.addData3f(self.x2, self.y1, self.z1) vertex.addData3f(self.x2, self.y2, self.z2) vertex.addData3f(self.x1, self.y2, self.z2) normal.addData3f( Vec3(2 * self.x1 - 1, 2 * self.y1 - 1, 2 * self.z1 - 1).normalize()) normal.addData3f( Vec3(2 * self.x2 - 1, 2 * self.y1 - 1, 2 * self.z1 - 1).normalize()) normal.addData3f( Vec3(2 * self.x2 - 1, 2 * self.y2 - 1, 2 * self.z2 - 1).normalize()) normal.addData3f( Vec3(2 * self.x1 - 1, 2 * self.y2 - 1, 2 * self.z2 - 1).normalize()) #adding different colors to the vertex for visibility color.addData4f(self.r, self.g, self.b, self.a) color.addData4f(self.r, self.g, self.b, self.a) color.addData4f(self.r, self.g, self.b, self.a) color.addData4f(self.r, self.g, self.b, self.a) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) #quads arent directly supported by the Geom interface #you might be interested in the CardMaker class if you are #interested in rectangle though tri1 = GeomTriangles(Geom.UHStatic) tri2 = GeomTriangles(Geom.UHStatic) tri1.addVertex(0) tri1.addVertex(1) tri1.addVertex(3) tri2.addConsecutiveVertices(1, 3) tri1.closePrimitive() tri2.closePrimitive() square = Geom(vdata) square.addPrimitive(tri1) square.addPrimitive(tri2) #square.setIntoCollideMask(BitMask32.bit(1)) self.squareNP = NodePath(GeomNode('square gnode')) self.squareNP.node().addGeom(square) self.squareNP.setTransparency(1) self.squareNP.setAlphaScale(.5) self.squareNP.setTwoSided(True) #squareNP.setCollideMask(BitMask32.bit(1)) self.squareNP.reparentTo(self.parent) return self.squareNP