def _make_fullscreen_tri(self): """ Creates the oversized triangle used for rendering """ vformat = GeomVertexFormat.get_v3() vdata = GeomVertexData("vertices", vformat, Geom.UH_static) vdata.set_num_rows(3) vwriter = GeomVertexWriter(vdata, "vertex") vwriter.add_data3f(-1, 0, -1) vwriter.add_data3f(3, 0, -1) vwriter.add_data3f(-1, 0, 3) gtris = GeomTriangles(Geom.UH_static) gtris.add_next_vertices(3) geom = Geom(vdata) geom.add_primitive(gtris) geom_node = GeomNode("gn") geom_node.add_geom(geom) geom_node.set_final(True) geom_node.set_bounds(OmniBoundingVolume()) tri = NodePath(geom_node) tri.set_depth_test(False) tri.set_depth_write(False) tri.set_attrib(TransparencyAttrib.make(TransparencyAttrib.M_none), 10000) tri.set_color(Vec4(1)) tri.set_bin("unsorted", 10) tri.reparent_to(self._node) self._tri = tri
def _create_geom(self): color = ConfigVariableColor('grid-color', DEFAULT_GRID_COLOR) radius = floor(self.size / (2 * self.spacing)) diameter = (2 * radius + 1) start = -radius * self.spacing vertex_format = GeomVertexFormat.get_v3c4() vertex_data = GeomVertexData('grid', vertex_format, Geom.UH_static) vertex_data.set_num_rows(diameter * 4) vertex_writer = GeomVertexWriter(vertex_data, 'vertex') color_writer = GeomVertexWriter(vertex_data, 'color') for i, j in product(range(diameter), repeat=2): vertex_writer.add_data3f(start + i * self.spacing, start + j * self.spacing, 0.0) alpha = GRID_ALPHA - GRID_ALPHA * ( Vector(i - radius, j - radius).norm() / radius) color_writer.add_data4f(color[0], color[1], color[2], alpha) primitive = GeomLinestrips(Geom.UH_static) for vertex in vertex_indexes(diameter): primitive.add_vertex(vertex) primitive.close_primitive() self.geom = Geom(vertex_data) self.geom.add_primitive(primitive)
def get_p3d_geom_node(self, name='UnnamedGeom'): # type: (Optional[str]) -> GeomNode """Returns a Panda3D GeomNode object""" vertex_data = GeomVertexData(name, GeomVertexFormat.get_v3c4(), Geom.UH_static) total_triangles = len(self.__triangles__) # Every triangle gets its unique set of vertices for flat shading num_rows = total_triangles * 3 vertex_data.set_num_rows(num_rows) vertex_writer = GeomVertexWriter(vertex_data, 'vertex') color_writer = GeomVertexWriter(vertex_data, 'color') for i in range(total_triangles): triangle = self.vertices[self.triangles[i]] vertex_writer.add_data3f(*triangle[0]) vertex_writer.add_data3f(*triangle[1]) vertex_writer.add_data3f(*triangle[2]) triangle_color = self.colors[self.triangles[i]].sum(axis=0) / 3 for _ in [0, 0, 0]: color_writer.add_data4f(*triangle_color) geom = Geom(vertex_data) for i in range(total_triangles): triangle = GeomTriangles(Geom.UH_static) first = i * 3 triangle.add_vertex(first) triangle.add_vertex(first + 1) triangle.add_vertex(first + 2) geom.add_primitive(triangle) node = GeomNode(name) node.add_geom(geom) return node
class VertexDataWriter(object): def __init__(self, vdata): self.count = 0 self.vertex = GeomVertexWriter(vdata, 'vertex') self.normal = GeomVertexWriter(vdata, 'normal') self.color = GeomVertexWriter(vdata, 'color') self.texcoord = GeomVertexWriter(vdata, 'texcoord') def add_vertex(self, point, normal, color, texcoord): self.vertex.add_data3f(point) self.normal.add_data3f(normal) self.color.add_data4f(*color) self.texcoord.add_data2f(*texcoord) self.count += 1
def geom_node(): array = GeomVertexArrayFormat() array.add_column("vertex", 3, Geom.NT_float32, Geom.C_point) array.add_column("normal", 3, Geom.NT_float32, Geom.C_normal) array.add_column("color", 3, Geom.NT_float32, Geom.C_color) array.add_column("texcoord", 3, Geom.NT_float32, Geom.C_texcoord) format = GeomVertexFormat() format.add_array(array) format = GeomVertexFormat.register_format(format) vdata = GeomVertexData("test", format, Geom.UH_static) vdata.set_num_rows(4) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') vertex.add_data3f(1, 0, 0) normal.add_data3f(0, 0, 1) color.add_data4f(0, 0, 1, 1) texcoord.add_data2f(1, 0) vertex.add_data3f(1, 1, 0) normal.add_data3f(0, 1, 1) color.add_data4f(1, 0, 1, 1) texcoord.add_data2f(1, 1) geom = Geom(vdata) node = GeomNode('gnode') node.add_geom(geom) return node
def add_vertices(self, whl_radius, car_h): base_pos = self.last_pos + (0, 0, -whl_radius + .05) rot_mat = Mat4() rot_mat.set_rotate_mat(car_h, (0, 0, 1)) vdata = self.node.modify_geom(0).modify_vertex_data() vwriter = GeomVertexWriter(vdata, 'vertex') vwriter.set_row(vdata.get_num_rows()) vwriter.add_data3f(base_pos + rot_mat.xform_vec((-.12, 0, 0))) vwriter.add_data3f(base_pos + rot_mat.xform_vec((.12, 0, 0))) cnt = self.vtx_cnt prim = self.node.modify_geom(0).modify_primitive(0) if cnt >= 3: prim.add_vertices(cnt - 3, cnt - 2, cnt - 1) prim.add_vertices(cnt - 2, cnt, cnt - 1) self.vtx_cnt += 2
class VertexDataWriter (object): def __init__(self, vdata): self.count = 0 self.vertex = GeomVertexWriter(vdata, 'vertex') self.normal = GeomVertexWriter(vdata, 'normal') self.color = GeomVertexWriter(vdata, 'color') self.texcoord = GeomVertexWriter(vdata, 'texcoord') def add_vertex(self, point, normal, color, texcoord): self.vertex.add_data3f(point) self.normal.add_data3f(normal) self.color.add_data4f(*color) self.texcoord.add_data2f(*texcoord) self.count += 1
def addLine(self, points): """ Adds a line to class GeomNode from a pair of points """ #Creates objects needed to draw a geometry on the HUD #The vertex data which will define the rendered geometry vertex_data = GeomVertexData("graph", GeomVertexFormat.getV3(), Geom.UHStatic) #The object that writes vertexes the vertex data writer = GeomVertexWriter(vertex_data, "vertex") for point in points: writer.add_data3f(point[0], 0, point[1]) #Defines that this geometry represents a polyline primitive = GeomLinestrips(Geom.UHStatic) #Tells geometry how many verticies will be added(?) primitive.add_consecutive_vertices(0, 2) primitive.close_primitive() geometry = Geom(vertex_data) geometry.add_primitive(primitive) #Draws a graph on the HUD self.geom_node.add_geom(geometry)
def _generate_mesh(node: Node) -> Geom: """Generate mesh for a Node.""" size = len(node.geometry.polygon) vertex_data = GeomVertexData(str(node.id), VERTEX_FORMAT, Geom.UH_static) vertex_data.set_num_rows(size) vertex_writer = GeomVertexWriter(vertex_data, 'vertex') normal_writer = GeomVertexWriter(vertex_data, 'normal') texcoord_writer = GeomVertexWriter(vertex_data, 'texcoord') indexes = [range(i - 1, i + 2) for i in range(0, size, 2)] triangles = [[node.geometry.polygon[j] for j in t] for t in indexes] for point in node.geometry.polygon: vertex_writer.add_data3f(point.x, point.y, LEVEL_HEIGHT * node.level) normal_writer.add_data3f(0.0, 0.0, 1.0) texcoord_writer.add_data2f(point.x / LANE_WIDTH, point.y / LANE_WIDTH) indexes = [ i for i, t in zip(indexes, triangles) if not t[0].close_to(t[1]) and not t[1].close_to(t[2]) ] geom = Geom(vertex_data) primitive = GeomTriangles(Geom.UH_static) for index in chain.from_iterable(indexes): primitive.add_vertex(index % size) primitive.close_primitive() geom.add_primitive(primitive) if size > 4: primitive = GeomTristrips(Geom.UH_static) for index in (i if i % 2 else -i - 1 for i in range(size // 2)): primitive.add_vertex(index % size) primitive.close_primitive() geom.add_primitive(primitive) return geom
def _generate_mesh(radius: float, count: int) -> Geom: """Generate mesh for the ground plane.""" vertex_data = GeomVertexData('ground', VERTEX_FORMAT, Geom.UH_static) vertex_data.set_num_rows(count**2) vertex_writer = GeomVertexWriter(vertex_data, 'vertex') normal_writer = GeomVertexWriter(vertex_data, 'normal') texcoord_writer = GeomVertexWriter(vertex_data, 'texcoord') step = 2 * radius / count for i, j in product(range(count + 1), repeat=2): vertex_writer.add_data3f(i * step - radius, j * step - radius, 0.0) normal_writer.add_data3f(0.0, 0.0, 1.0) texcoord_writer.add_data2f(i * 512, j * 512) geom = Geom(vertex_data) primitive = GeomTristrips(Geom.UH_static) for j in range(count): rows = range(count + 1) if j % 2 else reversed(range(count + 1)) for i in rows: primitive.add_vertex((j + (j + 1) % 2) * (count + 1) + i) primitive.add_vertex((j + j % 2) * (count + 1) + i) primitive.close_primitive() geom.add_primitive(primitive) return geom
def _generate_mesh(way: Way) -> Geom: """Generate mesh for a Way.""" geometry = way.geometry rows = 2 * len(geometry.segments) + 2 vertex_data = GeomVertexData(str(way.id), VERTEX_FORMAT, Geom.UH_static) vertex_data.set_num_rows(rows) vertex_writer = GeomVertexWriter(vertex_data, 'vertex') normal_writer = GeomVertexWriter(vertex_data, 'normal') texcoord_writer = GeomVertexWriter(vertex_data, 'texcoord') start_z = way.start.level * LEVEL_HEIGHT end_z = way.end.level * LEVEL_HEIGHT lanes_float = float(way.total_lane_count) segment = geometry.segments[0] for vertex in (segment.start_left, segment.start_right): vertex_writer.add_data3f(vertex.x, vertex.y, start_z) normal_writer.add_data3f(0.0, 0.0, 1.0) texture_v = 0.0 texcoord_writer.add_data2f(0.0, texture_v) texcoord_writer.add_data2f(lanes_float, texture_v) lengths = [s.length() for s in geometry.segments] total_len = sum(lengths) for segment, acc_len in zip(geometry.segments, accumulate(lengths)): height = start_z + (end_z - start_z) * acc_len / total_len for vertex in (segment.end_left, segment.end_right): vertex_writer.add_data3f(vertex.x, vertex.y, height) normal_writer.add_data3f(0.0, 0.0, 1.0) texture_v = acc_len / LANE_WIDTH texcoord_writer.add_data2f(0.0, texture_v) texcoord_writer.add_data2f(lanes_float, texture_v) primitive = GeomTristrips(Geom.UH_static) primitive.add_consecutive_vertices(0, rows) primitive.close_primitive() geom = Geom(vertex_data) geom.add_primitive(primitive) return geom
class Grid(ShowBase): def __init__(self): ShowBase.__init__(self) self.cols = 100 self.rows = 100 base.disableMouse() base.setFrameRateMeter(True) self.cameraHeight = 13 self.camera.set_pos(self.cols / 2, -30, self.cameraHeight) self.camera.look_at(self.cols / 2, 300, 0) plights = [] for i in range(0, int(self.cols / 5), 2): plight = PointLight("plight") plight.setColor(VBase4(1, 1, 1, 1)) plights.append(plight) plights[i] = self.render.attachNewNode(plight) x, y, z = self.camera.get_pos() plights[i].setPos(self.cols / 2 + ((i - int(i / 2)) * 10), y + 20, 5) self.render.set_light(plights[i]) plight = PointLight("plight") plight.setColor(VBase4(1, 1, 1, 1)) plights.append(plight) plights[i + 1] = self.render.attachNewNode(plight) x, y, z = self.camera.get_pos() plights[i + 1].setPos(self.cols / 2 + ((i - int(i / 2)) * 10), y + 20, 10) self.render.set_light(plights[i + 1]) self.plights = plights format = GeomVertexFormat.getV3c4() vdata = GeomVertexData('name', format, Geom.UHStatic) vdata.setNumRows(self.cols * self.rows) self.vertex = GeomVertexWriter(vdata, 'vertex') self.color = GeomVertexWriter(vdata, 'color') pz = [random.uniform(-1, 1)] for i in range(self.rows): pz.append(random.uniform(pz[i - 1] - 1, pz[i] + 1)) for y in range(0, self.rows): for x in range(0, self.cols): nz1 = random.uniform(pz[x] - 1, pz[x] + 1) nz2 = random.uniform(pz[x - 1] - 1, pz[x - 1] + 1) nz3 = random.uniform(pz[x + 1] - 1, pz[x + 1] + 1) nz = (nz1 + nz2 + nz3) / 3 self.vertex.add_data3f((x, y + 1, nz)) self.vertex.add_data3f((x, y, pz[x])) if nz < -5: self.color.add_data4f(0.2, 0.1, 0, 1) elif nz < -3: self.color.add_data4f(0, 0.2, 0.1, 1) elif nz < 0: self.color.add_data4f(0, 0.4, 0.2, 1) elif nz < 2: self.color.add_data4f(0.4, 0.4, 0.4, 1) else: self.color.add_data4f(1, 1, 1, 1) if nz < -5: self.color.add_data4f(0.2, 0.1, 0, 1) elif nz < -3: self.color.add_data4f(0, 0.2, 0.1, 1) elif pz[x] < 0: self.color.add_data4f(0, 0.4, 0.2, 1) elif pz[x] < 2: self.color.add_data4f(0.4, 0.4, 0.4, 1) else: self.color.add_data4f(1, 1, 1, 1) pz[x] = nz #print (nz) self.pz = pz geom = Geom(vdata) for y in range(0, self.rows): prim = GeomTristrips(Geom.UH_static) prim.addVertex(y * self.cols * 2) prim.add_next_vertices((self.cols * 2) - 1) prim.close_primitive() geom.addPrimitive(prim) nodeTris = GeomNode("TriStrips") nodeTris.addGeom(geom) self.nodeTrisPath = self.render.attachNewNode(nodeTris) self.task_mgr.add(self.moveForwardTask, "moveForwardTask") self.vdata = vdata self.newNodePath = [] self.counter = 0 self.rows1 = self.rows skybox = self.loader.loadModel("models/skybox.bam") skybox.reparent_to(self.render) skybox.set_scale(20000) skybox_texture = self.loader.loadTexture("textures/dayfair.jpg") skybox_texture.set_minfilter(SamplerState.FT_linear) skybox_texture.set_magfilter(SamplerState.FT_linear) skybox_texture.set_wrap_u(SamplerState.WM_repeat) skybox_texture.set_wrap_v(SamplerState.WM_mirror) skybox_texture.set_anisotropic_degree(16) skybox.set_texture(skybox_texture) skybox_shader = Shader.load(Shader.SL_GLSL, "skybox.vert.glsl", "skybox.frag.glsl") skybox.set_shader(skybox_shader) def moveForwardTask(self, task): change = 0.7 self.counter = self.counter + change x, y, z = self.camera.get_pos() self.camera.set_pos(x, y + change, z) for i in range(0, len(self.plights)): x, y, z = self.plights[i].get_pos() self.plights[i].set_pos(x, y + change, z) self.render.set_light(self.plights[i]) if y > self.rows1: self.nodeTrisPath.removeNode() if self.counter >= 1: if y > self.rows1: self.newNodePath[0].removeNode() del self.newNodePath[0] self.counter = self.counter - 1 for x in range(0, self.cols): nz1 = random.uniform(self.pz[x] - 1, self.pz[x] + 1) nz2 = random.uniform(self.pz[x - 1] - 1, self.pz[x - 1] + 1) nz3 = random.uniform(self.pz[x + 1] - 1, self.pz[x + 1] + 1) nz = (nz1 + nz2 + nz3) / 3 self.vertex.add_data3f((x, self.rows + 1, nz)) self.vertex.add_data3f((x, self.rows, self.pz[x])) if nz < -5: self.color.add_data4f(0.2, 0.1, 0, 1) elif nz < -3: self.color.add_data4f(0, 0.2, 0.1, 1) elif nz < 0: self.color.add_data4f(0, 0.4, 0.2, 1) elif nz < 4: self.color.add_data4f(0.4, 0.4, 0.4, 1) else: self.color.add_data4f(1, 1, 1, 1) if nz < -5: self.color.add_data4f(0.2, 0.1, 0, 1) elif nz < -3: self.color.add_data4f(0, 0.2, 0.1, 1) elif self.pz[x] < 0: self.color.add_data4f(0, 0.4, 0.2, 1) elif self.pz[x] < 4: self.color.add_data4f(0.4, 0.4, 0.4, 1) else: self.color.add_data4f(1, 1, 1, 1) self.pz[x] = nz #print (nz) geom = Geom(self.vdata) prim = GeomTristrips(Geom.UH_static) prim.addVertex(self.cols * 2 * self.rows) prim.add_next_vertices((self.cols * 2) - 1) prim.close_primitive() geom.addPrimitive(prim) node = GeomNode("TriStrips") node.addGeom(geom) self.newNodePath.append(self.render.attachNewNode(node)) self.rows = self.rows + 1 return Task.cont
class CubeMesh(): name: str geom: Geom __face_count: int __vertex_data_format: GeomVertexFormat __vertex_data: GeomVertexData __triangles: GeomTriangles __triangle_data: GeomVertexArrayData __vertex: GeomVertexWriter __normal: GeomVertexWriter __wireframe_node: GeomNode def __init__(self, name: str = 'cube_mesh', wireframe_thickness: float = 5) -> None: self.name = name self.__vertex_data_format = GeomVertexFormat.getV3n3() self.__vertex_data = GeomVertexData(name, self.__vertex_data_format, Geom.UHStatic) self.geom = Geom(self.__vertex_data) self.__triangles = GeomTriangles(Geom.UHStatic) self.__triangle_data = self.__triangles.modifyVertices() self.__vertex = GeomVertexWriter(self.__vertex_data, 'vertex') self.__normal = GeomVertexWriter(self.__vertex_data, 'normal') self.__face_count = 0 def add_face(face: Face) -> None: self.__make_face(face) self.__make_face(Face.LEFT) self.__make_face(Face.RIGHT) self.__make_face(Face.BACK) self.__make_face(Face.FRONT) self.__make_face(Face.BOTTOM) self.__make_face(Face.TOP) self.__triangles.close_primitive() self.geom.add_primitive(self.__triangles) def is_connected(x, y, z, x1, y1, z1): return (abs(x - x1) == 1 and abs(y - y1) != 1 and abs(z - z1) != 1) or \ (abs(x - x1) != 1 and abs(y - y1) == 1 and abs(z - z1) != 1) or \ (abs(x - x1) != 1 and abs(y - y1) != 1 and abs(z - z1) == 1) ls = LineSegs() ls.set_thickness(wireframe_thickness) arr_x = [0, 0, 0, 0, 1, 1, 1, 1] arr_y = [0, 0, 1, 1, 1, 1, 0, 0] arr_z = [0, -1, -1, 0, 0, -1, -1, 0] for pos1 in range(len(arr_x) - 1): for pos2 in range(pos1, len(arr_x)): x = arr_x[pos1] y = arr_y[pos1] z = arr_z[pos1] x1 = arr_x[pos2] y1 = arr_y[pos2] z1 = arr_z[pos2] if (is_connected(x, y, z, x1, y1, z1)): ls.move_to(x, y, z) ls.draw_to(x1, y1, z1) self.__wireframe_node = ls.create() def __make_face(self, face: Face, pos: Point = Point(0, 0, 0)) -> None: def make(x1, y1, z1, x2, y2, z2) -> None: if x1 == x2: self.__vertex.add_data3f(x1, y1, z1) self.__vertex.add_data3f(x2, y2, z1) self.__vertex.add_data3f(x2, y2, z2) self.__vertex.add_data3f(x1, y1, z2) self.__normal.add_data3( normalise(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) self.__normal.add_data3( normalise(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) self.__normal.add_data3( normalise(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) self.__normal.add_data3( normalise(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) else: self.__vertex.add_data3f(x1, y1, z1) self.__vertex.add_data3f(x2, y1, z1) self.__vertex.add_data3f(x2, y2, z2) self.__vertex.add_data3f(x1, y2, z2) self.__normal.add_data3( normalise(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) self.__normal.add_data3( normalise(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) self.__normal.add_data3( normalise(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) self.__normal.add_data3( normalise(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) vertex_id = self.__face_count * 4 self.__triangles.addVertices(vertex_id, vertex_id + 1, vertex_id + 3) self.__triangles.addVertices(vertex_id + 1, vertex_id + 2, vertex_id + 3) self.__face_count += 1 x, y, z = pos if face == Face.FRONT: make(x + 1, y + 1, z - 1, x, y + 1, z) elif face == Face.BACK: make(x, y, z - 1, x + 1, y, z) elif face == Face.RIGHT: make(x + 1, y, z - 1, x + 1, y + 1, z) elif face == Face.LEFT: make(x, y + 1, z - 1, x, y, z) elif face == Face.TOP: make(x + 1, y + 1, z, x, y, z) elif face == Face.BOTTOM: make(x, y + 1, z - 1, x + 1, y, z - 1) else: raise Exception("unknown face") @property def artificial_lighting(self) -> str: return 'artificial_lighting' @artificial_lighting.getter def artificial_lighting(self) -> bool: return self.__artificial_lighting @property def body_node(self) -> GeomNode: node = GeomNode(self.name) node.addGeom(self.geom) return node @property def wireframe_node(self) -> GeomNode: return self.__wireframe_node
def _generate_mesh(points: List[Point]) -> Geom: """Generate mesh for a Path.""" def calc_color(param): return interpolate_rgb(START_COLOR, END_COLOR, param) if len(points) < 2: return Geom() vertex_data = GeomVertexData('path', VERTEX_FORMAT, Geom.UH_static) vertex_data.set_num_rows(2 * len(points) + 1) vertex_writer = GeomVertexWriter(vertex_data, 'vertex') normal_writer = GeomVertexWriter(vertex_data, 'normal') color_writer = GeomVertexWriter(vertex_data, 'color') length = sum(p1.distance(p2) for p1, p2 in window_iter(points)) vector = points[1] - points[0] distance = vector.norm() position = distance / length vector = vector.normalized() width_vector = LANE_WIDTH * 0.5 * vector.rotated_left() for vertex in (points[0] + width_vector, points[0] - width_vector): vertex_writer.add_data3f(vertex.x, vertex.y, HEIGHT) normal_writer.add_data3f(0.0, 0.0, 1.0) color_writer.add_data4f(*START_COLOR) last_vector = vector for point, next_ in zip(islice(points, 1, None), islice(points, 2, None)): vector = next_ - point distance = vector.norm() vector = vector.normalized() try: bisector = (last_vector + vector).normalized() width_vector = (sec(bisector, vector) * 0.5 * LANE_WIDTH * bisector.rotated_left()) except ZeroDivisionError: width_vector = vector.rotated_right() * 0.5 * LANE_WIDTH color = calc_color(position) for vertex in (point + width_vector, point - width_vector): vertex_writer.add_data3f(vertex.x, vertex.y, HEIGHT) normal_writer.add_data3f(0.0, 0.0, 1.0) color_writer.add_data4f(*color) position = position + distance / length last_vector = vector point = points[-1] width_vector = 0.5 * LANE_WIDTH * last_vector.rotated_left() distance = LANE_WIDTH if distance > LANE_WIDTH else distance / 2 vector = -last_vector * distance for vertex in (point + width_vector + vector, point - width_vector + vector, point): vertex_writer.add_data3f(vertex.x, vertex.y, HEIGHT) normal_writer.add_data3f(0.0, 0.0, 1.0) color_writer.add_data4f(*END_COLOR) primitive = GeomTristrips(Geom.UH_static) primitive.add_consecutive_vertices(0, 2 * len(points) + 1) primitive.close_primitive() geom = Geom(vertex_data) geom.add_primitive(primitive) return geom
class SquareMesh(): name: Final[str] mesh: Final[Geom] depth: Final[float] __vertex_data_format: GeomVertexFormat __vertex_data: GeomVertexData __triangles: GeomTriangles __triangle_data: GeomVertexArrayData __vertex: GeomVertexWriter __normal: GeomVertexWriter __texcoord: GeomVertexWriter def __init__(self, width: int = 1, height: int = 1, depth: Real = 0.1, name: str = 'SquareMesh') -> None: self.name = name self.depth = depth self.__vertex_data_format = GeomVertexFormat. getV3n3t2() self.__vertex_data = GeomVertexData(name, self.__vertex_data_format, Geom.UHStatic) self.__vertex_data.set_num_rows(4) self.mesh = Geom(self.__vertex_data) self.__triangles = GeomTriangles(Geom.UHStatic) self.__triangle_data = self.__triangles.modifyVertices() self.__vertex = GeomVertexWriter(self.__vertex_data, 'vertex') self.__normal = GeomVertexWriter(self.__vertex_data, 'normal') self.__texcoord = GeomVertexWriter(self.__vertex_data, 'texcoord') self.__face_count = 0 def make(x1, y1, z1, x2, y2, z2, tex) -> None: if x1 == x2: self.__vertex.add_data3f(x1, y1, z1) self.__vertex.add_data3f(x2, y2, z1) self.__vertex.add_data3f(x2, y2, z2) self.__vertex.add_data3f(x1, y1, z2) self.__normal.add_data3(normalise(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) self.__normal.add_data3(normalise(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) self.__normal.add_data3(normalise(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) self.__normal.add_data3(normalise(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) else: self.__vertex.add_data3f(x1, y1, z1) self.__vertex.add_data3f(x2, y1, z1) self.__vertex.add_data3f(x2, y2, z2) self.__vertex.add_data3f(x1, y2, z2) self.__normal.add_data3(normalise(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) self.__normal.add_data3(normalise(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) self.__normal.add_data3(normalise(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) self.__normal.add_data3(normalise(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) for data in tex: self.__texcoord.addData2f(*data) vertex_id = self.__face_count * 4 self.__triangles.addVertices(vertex_id, vertex_id + 1, vertex_id + 3) self.__triangles.addVertices(vertex_id + 1, vertex_id + 2, vertex_id + 3) self.__face_count += 1 if self.depth == 0: make(0, 0, 0, width, height, 0, [(1.0, 1.0), (0.0, 1.0), (0.0, 0.0), (1.0, 0.0)]) else: make(0, height, -self.depth, 0, 0, 0, ((0.0, 1.0), (0.0, 0.0), (0.0, 0.0), (0.0, 1.0))) # SIDE make(0, 0, -self.depth, width, 0, 0, ((0.0, 0.0), (1.0, 0.0), (1.0, 0.0), (0.0, 0.0))) # SIDE make(width, height, -self.depth, 0, height, 0, ((1.0, 1.0), (0.0, 1.0), (0.0, 1.0), (1.0, 1.0))) # SIDE make(width, 0, -self.depth, width, height, 0, ((1.0, 0.0), (1.0, 1.0), (1.0, 1.0), (1.0, 0.0))) # SIDE make(width, height, 0, 0, 0, 0, ((1.0, 1.0), (0.0, 1.0), (0.0, 0.0), (1.0, 0.0))) # TOP make(0, height, -self.depth, width, 0, -self.depth, ((0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0))) # BOTTOM self.__triangles.close_primitive() self.mesh.add_primitive(self.__triangles) @property def geom_node(self) -> str: return 'geom_node' @geom_node.getter def geom_node(self) -> GeomNode: node = GeomNode(self.name) node.addGeom(self.mesh) return node