def make_axes(): """Make an axes geometry. Returns: Geom -- p3d geometry """ vformat = GeomVertexFormat.get_v3c4() vdata = GeomVertexData('vdata', vformat, Geom.UHStatic) vdata.uncleanSetNumRows(6) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') for x, y, z in np.eye(3): vertex.addData3(0, 0, 0) color.addData4(x, y, z, 1) vertex.addData3(x, y, z) color.addData4(x, y, z, 1) prim = GeomLines(Geom.UHStatic) prim.addNextVertices(6) geom = Geom(vdata) geom.addPrimitive(prim) return geom
def make_box(): """Make a uniform box geometry. Returns: Geom -- p3d geometry """ vformat = GeomVertexFormat.get_v3n3t2() vdata = GeomVertexData('vdata', vformat, Geom.UHStatic) vdata.uncleanSetNumRows(24) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') tcoord = GeomVertexWriter(vdata, 'texcoord') axes = itertools.permutations(np.eye(3), r=2) quad = ((0, 0), (1, 0), (0, 1), (1, 1)) for x, y in axes: z = np.cross(x, y) for u, v in quad: vertex.addData3(*(x * (u - 0.5) + y * (v - 0.5) + z * 0.5)) normal.addData3(*z) tcoord.addData2(u, v) prim = GeomTriangles(Geom.UHStatic) for i in range(0, 24, 4): prim.addVertices(i + 0, i + 1, i + 2) prim.addVertices(i + 2, i + 1, i + 3) geom = Geom(vdata) geom.addPrimitive(prim) return geom
def make_node_from_mesh(points: np.ndarray, faces: np.ndarray, normals: np.ndarray, rgba: np.ndarray = np.asarray([1, 1, 1, 1])): vertex_normal_format = GeomVertexFormat.get_v3n3c4() v_data = GeomVertexData('sphere', vertex_normal_format, Geom.UHStatic) num_rows = np.max([points.shape[0], faces.shape[0]]) v_data.setNumRows(int(num_rows)) vertex_data = GeomVertexWriter(v_data, 'vertex') normal_data = GeomVertexWriter(v_data, 'normal') color_data = GeomVertexWriter(v_data, 'color') for point, normal in zip(points, normals): vertex_data.addData3(point[0], point[1], point[2]) normal_data.addData3(normal[0], normal[1], normal[2]) color_data.addData4(rgba[0], rgba[1], rgba[2], rgba[3]) geom = Geom(v_data) for face in faces: tri = GeomTriangles(Geom.UHStatic) p_1 = points[face[0], :] p_2 = points[face[1], :] p_3 = points[face[2], :] norm = normals[face[0], :] if np.dot(np.cross(p_2 - p_1, p_3 - p_2), norm) < 0: tri.add_vertices(face[2], face[1], face[0]) else: tri.add_vertices(face[0], face[1], face[2]) geom.addPrimitive(tri) node = GeomNode('gnode') node.addGeom(geom) return node
def make_plane(size=(1.0, 1.0)): """Make a plane geometry. Arguments: size {tuple} -- plane size x,y Returns: Geom -- p3d geometry """ vformat = GeomVertexFormat.get_v3n3t2() vdata = GeomVertexData('vdata', vformat, Geom.UHStatic) vdata.uncleanSetNumRows(4) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') tcoord = GeomVertexWriter(vdata, 'texcoord') quad = ((0, 0), (1, 0), (0, 1), (1, 1)) for u, v in quad: vertex.addData3((u - 0.5) * size[0], (v - 0.5) * size[1], 0) normal.addData3(0, 0, 1) tcoord.addData2(u, v) prim = GeomTriangles(Geom.UHStatic) prim.addVertices(0, 1, 2) prim.addVertices(2, 1, 3) geom = Geom(vdata) geom.addPrimitive(prim) return geom
def create_model(self): vdata = GeomVertexData('name', Diagram.gformat, Geom.UHStatic) vdata.setNumRows(len(self.values)*2) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') prim = GeomTristrips(Geom.UHStatic) i = 0 for x, z in self.values: vertex.addData3(0, x, 0) color.addData4(0, 0, 1, 1) prim.addVertex(i) vertex.addData3(0, x, z/100) color.addData4(0, 0, 1, 1) prim.addVertex(i+1) i += 2 prim.closePrimitive() diagram_geom = Geom(vdata) diagram_geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(diagram_geom) node.setTag('entity_type', self.__class__.__name__) node.setTag('entity_id', self.entity_id) model_parent = self.parent.geom[0] parent_scale = model_parent.getScale() nodePath = render.attachNewNode(node) nodePath.reparentTo(model_parent) nodePath.set_two_sided(True) nodePath.setLightOff() node_parent = self.parent.start.geom[0] L = self.parent.longitude() h = 1 nodePath.setScale(h / parent_scale[2], 1 / parent_scale[1], 1 / parent_scale[0]) nodePath.wrtReparentTo(node_parent) """ model = app.base.loader.loadModel("data/geom/plate") model.set_two_sided(True) model.setTag('entity_type', self.__class__.__name__) model.setTag('entity_id', self.entity_id) self.geom = [model]""" self.update_model()
def make_grid(num_ticks=10, step=1.0): """Make a grid geometry. Keyword Arguments: step {float} -- step in meters (default: {1.0}) num_ticks {int} -- ticks number per axis (default: {5}) Returns: Geom -- p3d geometry """ ticks = np.arange(-num_ticks // 2, num_ticks // 2 + 1) * step vformat = GeomVertexFormat.get_v3() vdata = GeomVertexData('vdata', vformat, Geom.UHStatic) vdata.uncleanSetNumRows(len(ticks) * 4) vertex = GeomVertexWriter(vdata, 'vertex') for t in ticks: vertex.addData3(t, ticks[0], 0) vertex.addData3(t, ticks[-1], 0) vertex.addData3(ticks[0], t, 0) vertex.addData3(ticks[-1], t, 0) prim = GeomLines(Geom.UHStatic) prim.addNextVertices(len(ticks) * 4) geom = Geom(vdata) geom.addPrimitive(prim) return geom
def make_square(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, tex_len=None, tex_width=None): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x3, y3, z3) vertex.addData3(x4, y4, z4) D = Vec3(x1, y1, z1) C = Vec3(x2, y2, z2) B = Vec3(x3, y3, z3) A = Vec3(x4, y4, z4) normal_vec = (C - A).cross(D - B).normalize() normal.addData3(normal_vec) normal.addData3(normal_vec) normal.addData3(normal_vec) normal.addData3(normal_vec) side_len = math.sqrt((x3 - x1)**2 + (y3 - y1)**2) start_width = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) end_width = math.sqrt((x4 - x3)**2 + (y4 - y3)**2) texcoord.addData2f(0.0, (start_width / tex_width) if tex_width is not None else 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f((side_len / tex_len) if tex_len is not None else 1.0, 0.0) texcoord.addData2f( (side_len / tex_len) if tex_len is not None else 1.0, (end_width / tex_width) if tex_width is not None else 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 2) tris.addVertices(0, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def 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 make_cube_geom(self, pos=(0, 0, 0), geom_color=(1, 1, 1, 0.5), faces_no_draw=(0, 0, 0, 0, 0, 0)): format = GeomVertexFormat.getV3n3cpt2() shift_x, shift_y, shift_z = pos cube_vertex_list = deepcopy(cube_data["vertexPositions"]) for vert in cube_vertex_list: vert[0] += shift_x vert[1] += shift_y vert[2] += shift_z vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') test_textcoords = [] # define available vertexes here [vertex.addData3(*v) for v in cube_vertex_list] [normal.addData3(*n) for n in cube_data["vertexNormals"]] [color.addData4f(*geom_color) for _ in cube_vertex_list] [texcoord.addData2f(*t) for t in texture_mapping] """ CREATE A NEW PRIMITIVE """ prim = GeomTriangles(Geom.UHStatic) # convert from numpy arr mapping to normal faces mapping... excluded_normals = [ normal_face_mapping[self.dict_mapping[i]] for i in range(len(faces_no_draw)) if faces_no_draw[i] == 1 ] # only use acceptable indices indices = [ ind for ind in cube_data['indices'] if cube_data['vertexNormals'][ind] not in excluded_normals ] [prim.addVertex(v) for v in indices] # N.B: this must correspond to a vertex defined in vdata geom = Geom(vdata) # create a new geometry geom.addPrimitive(prim) # add the primitive to the geometry return geom
class VChunkGeom: def __init__(self, voxel_unit_size): self.voxel_unit_size = voxel_unit_size self.triangles = GeomTriangles(Geom.UHStatic) self.format = GeomVertexFormat.getV3n3c4() self.v_data = GeomVertexData('square', self.format, Geom.UHStatic) self.vertex = GeomVertexWriter(self.v_data, 'vertex') self.normal = GeomVertexWriter(self.v_data, 'normal') self.color = GeomVertexWriter(self.v_data, 'color') def add_face(self, face, cube_x, cube_y, cube_z, color: LVector4): start_index = self.vertex.getWriteRow() vector = LVector3(cube_x * self.voxel_unit_size.x, cube_y * self.voxel_unit_size.z, cube_z * self.voxel_unit_size.y) for offset in face.offsets: scaled_offset = LVector3(self.voxel_unit_size.x * offset[0], self.voxel_unit_size.z * offset[1], self.voxel_unit_size.y * offset[2]) self.vertex.addData3(vector + scaled_offset) self.normal.addData3(face.normal) self.color.addData4(color) self.add_triangles(start_index) def add_triangles(self, start_index): self.triangles.addVertices(start_index, start_index + 1, start_index + 2) self.triangles.closePrimitive() self.triangles.addVertices(start_index, start_index + 2, start_index + 3) self.triangles.closePrimitive() def get_geom(self): geom = Geom(self.v_data) geom.addPrimitive(self.triangles) return geom
def make_capsule(radius, length, num_segments=16, num_rings=16): """Make capsule geometry. Arguments: radius {float} -- capsule radius length {float} -- capsule length Keyword Arguments: num_segments {int} -- segments number (default: {16}) num_rings {int} -- rings number (default: {16}) Returns: Geom -- p3d geometry """ vformat = GeomVertexFormat.get_v3n3t2() vdata = GeomVertexData('vdata', vformat, Geom.UHStatic) vdata.uncleanSetNumRows(num_segments * num_rings) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') tcoord = GeomVertexWriter(vdata, 'texcoord') for u in np.linspace(0, np.pi, num_rings): for v in np.linspace(0, 2 * np.pi, num_segments): x, y, z = np.cos(v) * np.sin(u), np.sin(v) * np.sin(u), np.cos(u) offset = np.sign(z) * 0.5 * length vertex.addData3(x * radius, y * radius, z * radius + offset) normal.addData3(x, y, z) tcoord.addData2(u / np.pi, v / (2 * np.pi)) prim = GeomTriangles(Geom.UHStatic) for i in range(num_rings - 1): for j in range(num_segments - 1): r0 = i * num_segments + j r1 = r0 + num_segments if i < num_rings - 2: prim.addVertices(r0, r1, r1 + 1) if i > 0: prim.addVertices(r0, r1 + 1, r0 + 1) geom = Geom(vdata) geom.addPrimitive(prim) return geom
def Square(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('cube', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') ################################ # # FACE 1 # ################################ vert1 = LVector3(self.pos.getX(), self.pos.getY(), self.pos.getZ()) vert2 = LVector3(vert1.getX() + self.len, vert1.getY(), vert1.getZ()) vert3 = LVector3(vert2.getX(), vert2.getY() + self.wid, vert2.getZ()) vert4 = LVector3(vert1.getX(), vert1.getY() + self.wid, vert1.getZ()) vertex.addData3(vert1) vertex.addData3(vert2) vertex.addData3(vert3) vertex.addData3(vert4) normal = GeomVertexWriter(vdata, 'normal') norm = (vert4 - vert1).cross(vert2 - vert1) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color = GeomVertexWriter(vdata, 'color') color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord = GeomVertexWriter(vdata, 'texcoord') texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 3, 1) tris.addVertices(1, 3, 2) square = Geom(vdata) square.addPrimitive(tris) self.prim = square self.model = GeomNode(self.name) self.model.addGeom(self.prim)
def make_cloud_node(pts, col=LColorf(1.0, 0.0, 0.0, 1.0)): ptCloudData = GeomVertexData("PointCloudData", GeomVertexFormat.getV3c4(), GeomEnums.UH_static) vertexWriter = GeomVertexWriter(ptCloudData, Thread.getCurrentThread()) vertexWriter.setColumn("vertex") colorWriter = GeomVertexWriter(ptCloudData, Thread.getCurrentThread()) colorWriter.setColumn("color") for (x, y, z) in pts: vertexWriter.addData3(x, y, z) colorWriter.addData4(col) geomPts = GeomPoints(GeomEnums.UH_static) geomPts.addConsecutiveVertices(0, len(pts)) geomPts.closePrimitive() geom = Geom(ptCloudData) geom.addPrimitive(geomPts) node = GeomNode("PointCloudNode") node.addGeom(geom, RenderState.makeEmpty()) return node
def make_cube_geom(pos=(0, 0, 0), geom_color=(1, 1, 1, 1)): format = GeomVertexFormat.getV3n3cpt2() shift_x, shift_y, shift_z = pos cube_vertex_list = deepcopy(cube_data["vertexPositions"]) for vert in cube_vertex_list: # vert[0] *= scale # vert[1] *= scale # vert[2] *= scale vert[0] += shift_x vert[1] += shift_y vert[2] += shift_z vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # define available vertexes here [normal.addData3(*n) for n in cube_data["vertexNormals"]] [vertex.addData3(*v) for v in cube_vertex_list] [color.addData4f(*geom_color) for _ in cube_vertex_list] [texcoord.addData2f(1, 1) for _ in cube_vertex_list] """ CREATE A NEW PRIMITIVE """ prim = GeomTriangles(Geom.UHStatic) # [prim.addVertex(v) for v in vertexes] indices = [ind for ind in cube_data['indices']] [prim.addVertex(v) for v in indices] # N.B: this must correspond to a vertex defined in vdata geom = Geom(vdata) # create a new geometry geom.addPrimitive(prim) # add the primitive to the geometry return geom
def make_patch_node(pts, col=LColorf(0.0, 1.0, 0.0, 1.0)): splinePatchData = GeomVertexData("SplinePatchData", GeomVertexFormat.getV3c4(), GeomEnums.UH_static) vertexWriter = GeomVertexWriter(splinePatchData, Thread.getCurrentThread()) vertexWriter.setColumn("vertex") colorWriter = GeomVertexWriter(splinePatchData, Thread.getCurrentThread()) colorWriter.setColumn("color") for (x, y, z) in pts: vertexWriter.addData3(x, y, z) colorWriter.addData4(col) geomLines = GeomLines(GeomEnums.UH_static) geomLines.addConsecutiveVertices(0, len(pts)) geomLines.addVertex(0) geomLines.closePrimitive() geom = Geom(splinePatchData) geom.addPrimitive(geomLines) node = GeomNode("SplinePatchNode") node.addGeom(geom, RenderState.makeEmpty()) return node
def __init__(self): ShowBase.__init__(self) # 初期化 vs = open('vs_water.glsl', "r", encoding="UTF-8").read() fs = open('fs_water.glsl', "r", encoding="UTF-8").read() props = WindowProperties() props.setTitle('Fragment') Win_size = (1920, 1080) # ウィンドウサイズ props.setSize(Win_size) # ウィンドウのサイズを設定 props.setCursorHidden(True) # マウスカーソルを隠す base.win.requestProperties(props) self.AspectRaito = float(Win_size[0] / Win_size[1]) # アスペクト比を計算 self.accept("escape", sys.exit) # 終了 self.disableMouse() # マウスカーソルを非表示 self.setFrameRateMeter(True) # FPSを表示 camera.setPos(0, -2, 0) # カメラを配置 format = GeomVertexFormat.getV3() mesh = GeomVertexData('', format, Geom.UHDynamic) # メッシュを用意 vertex = GeomVertexWriter(mesh, 'vertex') # 頂点オブジェクト vertex.addData3(-1, 0, -1) # 頂点を追加 vertex.addData3(-1, 0, 1) vertex.addData3(1, 0, 1) vertex.addData3(1, 0, -1) tri = GeomTriangles(Geom.UHDynamic) # 面オブジェクト tri.addVertices(0, 2, 1) # 面のインデックスを追加 tri.addVertices(0, 3, 2) node = GeomNode('') geo = Geom(mesh) # ジオメトリオブジェクト geo.addPrimitive(tri) # プリミティブにする node.addGeom(geo) self.panel = render.attachNewNode(node) # レンダーオブジェクトとして追加 shader = Shader.make(Shader.SL_GLSL, vs, fs) self.panel.setShader(shader) resolution = self.win.getXSize(), self.win.getYSize() self.panel.setShaderInput('resolution', resolution) taskMgr.add(self.Loop, 'update') # メインループ
def create_textured_rect(x, z, width, height): _format = GeomVertexFormat.getV3t2() vdata = GeomVertexData('square', _format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') texcoord = GeomVertexWriter(vdata, 'texcoord') vertex.addData3(x, 0, z) vertex.addData3(x + width, 0, z) vertex.addData3(x + width, 0, z + height) vertex.addData3(x, 0, z + height) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) texcoord.addData2f(0.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 makeSquare(x1, y1, z1, x2, y2, z2): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility color.addData4f(1.0, 0.0, 0.0, 1.0) color.addData4f(0.0, 1.0, 0.0, 1.0) color.addData4f(0.0, 0.0, 1.0, 1.0) color.addData4f(1.0, 0.0, 1.0, 1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def Ramp(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') #The corner at Quadrant 2 on face 1 is the starting point of our rectangle ################################ # # FACE 1 # ################################ vert1 = LVector3(self.pos.getX(), self.pos.getY(), self.pos.getZ()) vert2 = LVector3(vert1.getX() + self.len, vert1.getY(), vert1.getZ()) vert3 = LVector3(vert2.getX(), vert2.getY() + self.wid, vert2.getZ()) vert4 = LVector3(vert1.getX(), vert1.getY() + self.wid, vert1.getZ()) vertex.addData3(vert1) vertex.addData3(vert2) vertex.addData3(vert3) vertex.addData3(vert4) normal = GeomVertexWriter(vdata, 'normal') norm = (vert4 - vert1).cross(vert2 - vert1) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color = GeomVertexWriter(vdata, 'color') color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord = GeomVertexWriter(vdata, 'texcoord') texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 3, 1) tris.addVertices(1, 3, 2) ##################################### # # FACE 2 # ##################################### vert5 = vert1 vert6 = vert2 vert7 = LVector3(vert3.getX(), vert3.getY(), vert3.getZ() + self.dep) vert8 = LVector3(vert4.getX(), vert4.getY(), vert4.getZ() + self.dep) vertex.addData3(vert5) vertex.addData3(vert6) vertex.addData3(vert7) vertex.addData3(vert8) norm = (vert8 - vert5).cross(vert6 - vert5) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(4, 7, 5) tris.addVertices(5, 7, 6) ######################################## # # FACE 3 # ######################################## vert9 = vert1 vert10 = vert2 vert11 = vert6 vert12 = vert5 vertex.addData3(vert9) vertex.addData3(vert10) vertex.addData3(vert11) vertex.addData3(vert12) norm = (vert12 - vert9).cross(vert10 - vert9) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(8, 11, 9) tris.addVertices(9, 11, 10) ############################################### # # FACE 4 # ############################################### vert13 = vert1 vert14 = vert4 vert15 = vert8 vert16 = vert5 vertex.addData3(vert13) vertex.addData3(vert14) vertex.addData3(vert15) vertex.addData3(vert16) norm = (vert16 - vert13).cross(vert14 - vert13) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(12, 15, 13) tris.addVertices(13, 15, 14) ############################################ # # FACE 5 # ############################################ vert17 = vert2 vert18 = vert3 vert19 = vert7 vert20 = vert6 vertex.addData3(vert17) vertex.addData3(vert18) vertex.addData3(vert19) vertex.addData3(vert20) norm = (vert20 - vert17).cross(vert18 - vert17) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(16, 19, 17) tris.addVertices(17, 19, 18) ramp = Geom(vdata) ramp.addPrimitive(tris) self.prim = ramp self.model = GeomNode(self.name) self.model.addGeom(self.prim)
def Prism(self): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('cube', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') #################################### #First, we need to create the first face, and then base #the other faces on it. #################################### # # DIAGRAM OF A RECTANGULAR PRISM # |--------| # | Face 6 |<-Depth # |--------|--------|--------|--------| # | Face 4 | Face 2 | Face 3 | Face 1 | <-Width # | | | | | # |--------|--------|--------|--------| # | Face 5 | ^Length # |--------| #################################### #The corner at Quadrant 2 on face 1 is the starting point of our rectangle ################################ # # FACE 1 # ################################ vert1 = LVector3(self.pos.getX(), self.pos.getY(), self.pos.getZ()) vert2 = LVector3(vert1.getX() + self.len, vert1.getY(), vert1.getZ()) vert3 = LVector3(vert2.getX(), vert2.getY() + self.wid, vert2.getZ()) vert4 = LVector3(vert1.getX(), vert1.getY() + self.wid, vert1.getZ()) vertex.addData3(vert1) vertex.addData3(vert2) vertex.addData3(vert3) vertex.addData3(vert4) normal = GeomVertexWriter(vdata, 'normal') norm = (vert4 - vert1).cross(vert2 - vert1) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color = GeomVertexWriter(vdata, 'color') color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord = GeomVertexWriter(vdata, 'texcoord') texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 3, 1) tris.addVertices(1, 3, 2) ##################################### # # FACE 2 # ##################################### vert5 = LVector3(vert1.getX(), vert1.getY(), vert1.getZ() - self.dep) vert6 = LVector3(vert5.getX() + self.len, vert5.getY(), vert5.getZ()) vert7 = LVector3(vert6.getX(), vert6.getY() + self.wid, vert6.getZ()) vert8 = LVector3(vert5.getX(), vert5.getY() + self.wid, vert5.getZ()) vertex.addData3(vert5) vertex.addData3(vert6) vertex.addData3(vert7) vertex.addData3(vert8) norm = (vert8 - vert5).cross(vert6 - vert5) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(4, 7, 5) tris.addVertices(5, 7, 6) ######################################## # # FACE 3 # ######################################## vert9 = vert1 vert10 = vert2 vert11 = vert6 vert12 = vert5 vertex.addData3(vert9) vertex.addData3(vert10) vertex.addData3(vert11) vertex.addData3(vert12) norm = (vert12 - vert9).cross(vert10 - vert9) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(8, 11, 9) tris.addVertices(9, 11, 10) ########################################## # # FACE 4 # ########################################### vert13 = vert4 vert14 = vert3 vert15 = vert7 vert16 = vert8 vertex.addData3(vert13) vertex.addData3(vert14) vertex.addData3(vert15) vertex.addData3(vert16) norm = (vert16 - vert13).cross(vert14 - vert13) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(12, 15, 13) tris.addVertices(13, 15, 14) square = Geom(vdata) square.addPrimitive(tris) ############################################### # # FACE 5 # ############################################### vert17 = vert1 vert18 = vert4 vert19 = vert8 vert20 = vert5 vertex.addData3(vert17) vertex.addData3(vert18) vertex.addData3(vert19) vertex.addData3(vert20) norm = (vert20 - vert17).cross(vert18 - vert17) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(16, 19, 17) tris.addVertices(17, 19, 18) ############################################ # # FACE 6 # ############################################ vert21 = vert2 vert22 = vert3 vert23 = vert7 vert24 = vert6 vertex.addData3(vert21) vertex.addData3(vert22) vertex.addData3(vert23) vertex.addData3(vert24) norm = (vert21 - vert24).cross(vert21 - vert22) norm.normalize() normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) normal.addData3(norm) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) color.addData4f(self.color) texcoord.addData2f(1, 0) texcoord.addData2f(1, 1) texcoord.addData2f(0, 1) texcoord.addData2f(0, 0) tris.addVertices(20, 23, 21) tris.addVertices(21, 23, 22) square = Geom(vdata) square.addPrimitive(tris) self.prim = square self.model = GeomNode(self.name) self.model.addGeom(self.prim)
def makeSquare(x1, y1, z1, x2, y2, z2): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') # color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility # color.addData4f(1.0, 0.0, 0.0, 1.0) # color.addData4f(0.0, 1.0, 0.0, 1.0) # color.addData4f(0.0, 0.0, 1.0, 1.0) # color.addData4f(1.0, 0.0, 1.0, 1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def make_square(sq_color): # sq_color is a list of tuples, describing each vertex: # (r, g, b, a) for [bl, br, tr, tl] x1 = -1 y1 = -1 z1 = -1 x2 = 1 y2 = -1 z2 = 1 v_format = GeomVertexFormat.getV3n3cpt2() v_data = GeomVertexData('square', v_format, Geom.UHDynamic) vertex = GeomVertexWriter(v_data, 'vertex') normal = GeomVertexWriter(v_data, 'normal') color = GeomVertexWriter(v_data, 'color') tex_coord = GeomVertexWriter(v_data, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility # color.addData4f(1.0, 0.0, 0.0, 1.0) # color.addData4f(0.0, 1.0, 0.0, 1.0) # color.addData4f(0.0, 0.0, 1.0, 1.0) # color.addData4f(1.0, 0.0, 1.0, 1.0) color.addData4f(sq_color[0]) # (0, 0) bottom left color.addData4f(sq_color[1]) # (0.5, 0) bottom right color.addData4f(sq_color[2]) # (0.5, 0.5) top right color.addData4f(sq_color[3]) # (0, 0.5) top left tex_coord.addData2f(0.0, 1.0) tex_coord.addData2f(0.0, 0.0) tex_coord.addData2f(1.0, 0.0) tex_coord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(v_data) square.addPrimitive(tris) return square
class RoadBuilder(): def __init__(self): self.debugSphere = render.find("scene.dae").find("Scene").find( "debug_sphere") self.debugVector = render.find("scene.dae").find("Scene").find( "debug_vector") self.vdata = GeomVertexData('road', GeomVertexFormat.getV3t2(), Geom.UHStatic) # TODO: call this with right number of rows when it is known # self.vdata.setNumRows() self.vertex = GeomVertexWriter(self.vdata, 'vertex') self.texcoord = GeomVertexWriter(self.vdata, 'texcoord') self.prim = None self.prims = [] self.lastv = 0 self.vertices = [] self.lru_vertices = [] def addPrim(self): if self.prim is not None: for vertex in self.vertices: self.prim.addVertex(vertex) self.prims.append(self.prim) self.prim = GeomTriangles(Geom.UHStatic) def snapToExisting(self, point, tolerance=0.1): CACHE_SIZE = 1024 for i in self.lru_vertices: if (point - i).length() < tolerance: return i self.lru_vertices.append(point) if len(self.lru_vertices) > CACHE_SIZE: self.lru_vertices = self.lru_vertices[0:CACHE_SIZE] return point def updateTask(self, dt): self.nodepath.setShaderInput("time", (globalClock.getFrameTime())) return Task.cont def addSegment(self, p0, p1): width = HALF_ROAD_WIDTH side = -(p1 - p0).cross(Vec3().up()).normalized() * width length = (p1 - p0) h = (p1 - p0).length() v1 = side v2 = side + length v3 = -side + length v4 = -side v1 += p0 v2 += p0 v3 += p0 v4 += p0 v1 = self.snapToExisting(v1) v2 = self.snapToExisting(v2) v3 = self.snapToExisting(v3) v4 = self.snapToExisting(v4) self.vertex.addData3(v1) self.texcoord.addData2(1, 0) self.vertex.addData3(v2) self.texcoord.addData2(1, 1) self.vertex.addData3(v3) self.texcoord.addData2(0, 1) self.vertex.addData3(v4) self.texcoord.addData2(0, 0) lastv = self.lastv self.vertices.append(0 + lastv) self.vertices.append(1 + lastv) self.vertices.append(2 + lastv) self.vertices.append(3 + lastv) self.vertices.append(2 + lastv) self.vertices.append(0 + lastv) self.lastv += 4 def finish(self): self.geom = Geom(self.vdata) for prim in self.prims: self.geom.addPrimitive(prim) self.road_visual_node = GeomNode('road_node') visnode = self.road_visual_node visnode.addGeom(self.geom) self.road_visual_nodePath = render.attachNewNode(visnode) nodepath = self.road_visual_nodePath nodepath.setTwoSided(True) nodepath.setShader( Shader.load(Shader.SL_GLSL, vertex="shaders/road.vert", fragment="shaders/road.frag")) pleaseMakeTransparent(nodepath) self.nodepath = nodepath taskMgr.add(self.updateTask, "updateTask") self.lru_vertices = []
def __init__(self, base): # Load texture tex = Loader(base).loadTexture((Path(path.realpath(__file__)).parent.parent.parent / "res/images/checkerboard.png").absolute()) tex.setMagfilter(SamplerState.FT_nearest) tex.setMinfilter(SamplerState.FT_nearest) # Set up vertex data vdata = GeomVertexData("floor_data", GeomVertexFormat.get_v3t2(), Geom.UHStatic) vdata.setNumRows(6) vertex = GeomVertexWriter(vdata, "vertex") texcoord = GeomVertexWriter(vdata, "texcoord") vertex.addData3(-5, -5, 0) texcoord.addData3(0, 0, 0) vertex.addData3(-5, 5, 0) texcoord.addData3(0, 10, 0) vertex.addData3(5, 5, 0) texcoord.addData3(10, 10, 0) vertex.addData3(5, 5, 0) texcoord.addData3(10, 10, 0) vertex.addData3(5, -5, 0) texcoord.addData3(10, 0, 0) vertex.addData3(-5, -5, 0) texcoord.addData3(0, 0, 0) # Create primitive prim = GeomTriangles(Geom.UHStatic) prim.addVertices(0, 1, 2) prim.addVertices(3, 4, 5) geom = Geom(vdata) geom.add_primitive(prim) # Initialize geometry node GeomNode.__init__(self, "floor") attrib = TextureAttrib.make(tex) state = RenderState.make(attrib) self.addGeom(geom, state)
def make_circle(x1, y1, z1, x2, y2, z2, inner_radius, end_inner_radius, arc, new_z1, new_z2, tex_len=None, tex_width=None, end_radius=None, rev=False): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('circle', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') ccw = arc < 0 arc = -arc if ccw else arc num_parts = 50 * (arc / 360)**-1 num_draw = round(num_parts * (arc / 360)) x3 = x1 - x2 y3 = y1 - y2 z3 = z1 - z2 x3, y3, z3 = normalized(x3, y3, z3) theta_offset = math.atan2(y1 - y2, x1 - x2) orig_radius = math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2) cur_x1 = x1 cur_y1 = y1 cur_z1 = z1 cur_x2 = x2 cur_y2 = y2 cur_z2 = z2 z1_diff = new_z1 - z1 z2_diff = new_z2 - z2 if end_radius is None: end_radius = orig_radius inner_radius_diff = end_inner_radius - inner_radius radius_diff = end_radius - orig_radius for i in range(num_draw + 1): theta = (math.pi - 2 * math.pi * (i / num_parts)) % (2 * math.pi) interp = (i / num_draw) inner_radius_interp = inner_radius + (inner_radius_diff * (i / num_parts)) radius = (orig_radius + (radius_diff * interp)) + inner_radius_interp radius_offset = 0 if rev: radius_offset = radius_diff * interp x1_interp = x1 + ((inner_radius + radius_offset) * x3) y1_interp = y1 + ((inner_radius + radius_offset) * y3) z1_interp = z1 + ((inner_radius + radius_offset) * z3) x_outer = (math.cos((-theta if ccw else theta) + theta_offset) * radius) + x1_interp y_outer = (math.sin((-theta if ccw else theta) + theta_offset) * radius) + y1_interp x_inner = (math.cos((-theta if ccw else theta) + theta_offset) * inner_radius_interp) + x1_interp y_inner = (math.sin((-theta if ccw else theta) + theta_offset) * inner_radius_interp) + y1_interp D = Vec3(cur_x1, cur_y1, cur_z1) C = Vec3(cur_x2, cur_y2, cur_z2) B = Vec3(x_inner, y_inner, z1_interp + (z1_diff * interp)) A = Vec3(x_outer, y_outer, z1_interp + (z2_diff * interp)) normal_vec = (C - A).cross(D - B).normalize() vertex.addData3(x_inner, y_inner, z1_interp + (z1_diff * interp)) cur_x1, cur_y1, cur_z1 = x_inner, y_inner, z1_interp + (z1_diff * interp) normal.addData3(normal_vec) texcoord.addData2f( (((i / num_parts) * 2 * math.pi * inner_radius_interp) / tex_len) if tex_len is not None else 1.0, (orig_radius / tex_width) if tex_width is not None else 1.0) vertex.addData3(x_outer, y_outer, z1_interp + (z2_diff * interp)) cur_x2, cur_y2, cur_z2 = x_outer, y_outer, z1_interp + (z2_diff * interp) normal.addData3(normal_vec) texcoord.addData2f((((i / num_parts) * 2 * math.pi * radius) / tex_len) if tex_len is not None else 1.0, 0) tris = GeomTriangles(Geom.UHDynamic) for i in range(num_draw * 2): tris.addVertices(0 + i, 1 + i, 2 + i) circle = Geom(vdata) circle.addPrimitive(tris) return circle, cur_x1, cur_y1, cur_z1, cur_x2, cur_y2, cur_z2
class RenderApp(ShowBase): def __init__(self, data, timescale, scale, record, dark_matter, no_ordinary_matter): self.data = data self.scale = scale self.timescale = timescale self.dark_matter = dark_matter self.no_ordinary_matter = no_ordinary_matter self.n_particles = self.data.shape[1] ShowBase.__init__(self) vdata = GeomVertexData('galaxies', GeomVertexFormat.get_v3c4(), Geom.UHStatic) vdata.setNumRows(self.n_particles) self.vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') for i in range(self.n_particles): if (self.data[0][i].dark_matter and not self.dark_matter) or (not self.data[0][i].dark_matter and self.no_ordinary_matter): continue pos = self.data[0][i].pos / self.scale self.vertex.addData3(*pos) color.addData4(1, 1, 1, 1) prim = GeomPoints(Geom.UHStatic) prim.add_consecutive_vertices(0, self.n_particles - 1) geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) nodePath = self.render.attach_new_node(node) nodePath.setRenderModeThickness(2) self.disableMouse() self.useTrackball() self.trackball.node().set_pos(0, 100, 0) self.trackball.node().set_hpr(90, 0, 90) self.setBackgroundColor(0, 0, 0) self.taskMgr.add(self.update_task, "VertexUpdateTask") self.record(record) def load_data(self): self.data = cs.Result.load(self.file).numpy() def record(self, time): if time > 0: self.movie("screenshot", time, fps=60) def update_vertex(self, i): for j in range(self.n_particles): if (self.data[0][j].dark_matter and not self.dark_matter) or (not self.data[0][j].dark_matter and self.no_ordinary_matter): continue pos = self.data[i][j].pos / self.scale self.vertex.setRow(j) self.vertex.setData3(*pos) def update_task(self, task): index = int((task.time * self.timescale)) % self.data.shape[0] self.update_vertex(index) return Task.cont
def get_render_mesh(self): array = GeomVertexArrayFormat() array.addColumn("vertex", 3, Geom.NTFloat32, Geom.CPoint) array.addColumn("barycenter", 3, Geom.NTFloat32, Geom.CPoint) vertex_format = GeomVertexFormat() vertex_format.addArray(array) vertex_format = GeomVertexFormat.registerFormat(vertex_format) v_data = GeomVertexData('mesh', vertex_format, Geom.UHStatic) v_data.setNumRows(len(self.nodes)) vertex = GeomVertexWriter(v_data, 'vertex') barycenter = GeomVertexWriter(v_data, 'barycenter') prim = GeomTriangles(Geom.UHStatic) vertex_id = 0 for elem in self.elements: def node_coords(e, i): return self.nodes[e.nodes[i]].coordinate vertex.addData3(node_coords(elem, 0).x, node_coords(elem, 0).y, 0) vertex.addData3(node_coords(elem, 1).x, node_coords(elem, 1).y, 0) vertex.addData3(node_coords(elem, 2).x, node_coords(elem, 2).y, 0) barycenter.addData3(1, 0, 0) barycenter.addData3(0, 1, 0) barycenter.addData3(0, 0, 1) prim.addVertices(vertex_id, vertex_id + 1, vertex_id + 2) vertex_id += 3 vertex.addData3(node_coords(elem, 2).x, node_coords(elem, 2).y, 0) vertex.addData3(node_coords(elem, 3).x, node_coords(elem, 3).y, 0) vertex.addData3(node_coords(elem, 0).x, node_coords(elem, 0).y, 0) barycenter.addData3(1, 0, 0) barycenter.addData3(0, 1, 0) barycenter.addData3(0, 0, 1) prim.addVertices(vertex_id, vertex_id + 1, vertex_id + 2) vertex_id += 3 geom = Geom(v_data) geom.addPrimitive(prim) return geom
def makeSquare(x1, y1, z1, x2, y2, z2, color): # This is straight copied off the Panda3d 'procedural cube' example https://github.com/panda3d/panda3d/blob/master/samples/procedural-cube/main.py#L11 format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color_setter = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) # adding different colors to the vertex for visibility if np.sum(color) > 0: a = 1 # Alpha transparency else: a = 0 # Handling channel size 1 or 3 if np.size(color) == 1: for i in range(4): # Have to run this 4 times cuz each vertex on the square has a color setting color_setter.addData2f(color,a) elif np.size(color) == 3: r, g, b = color[0], color[1], color[2] for i in range(4): color_setter.addData4f(r,g,b,a) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) # Quads aren't directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) square = Geom(vdata) square.addPrimitive(tris) return square
def gen_geom(self, mesh_json): # Create vertex format geom_array = GeomVertexArrayFormat() geom_array.add_column("vertex", 3, Geom.NTFloat32, Geom.CPoint) has_normals = False has_texcoords = False has_weights = False if "normals" in mesh_json: geom_array.add_column("normal", 3, Geom.NTFloat32, Geom.CNormal) has_normals = True if "texcoords" in mesh_json: geom_array.add_column("texcoord", 3, Geom.NTFloat32, Geom.CTexcoord) has_texcoords = True if "weights" in mesh_json: geom_array.add_column("joint", 4, Geom.NTUint8, Geom.CIndex) geom_array.add_column("weight", 4, Geom.NTFloat32, Geom.COther) has_weights = True geom_format = GeomVertexFormat() geom_format.add_array(geom_array) geom_format = GeomVertexFormat.register_format(geom_format) # Set up vertex data vdata = GeomVertexData( str(random.randint(0, 255)) + "_vdata", geom_format, Geom.UHStatic) vcount = len(mesh_json["vertices"]) // 3 vdata.setNumRows(vcount) vertex = GeomVertexWriter(vdata, "vertex") for i in range(vcount): vertex.addData3(mesh_json["vertices"][3 * i], mesh_json["vertices"][3 * i + 1], mesh_json["vertices"][3 * i + 2]) if has_normals: normal = GeomVertexWriter(vdata, "normal") for i in range(vcount): normal.addData3(mesh_json["normals"][3 * i], mesh_json["normals"][3 * i + 1], mesh_json["normals"][3 * i + 2]) if has_texcoords: texcoord = GeomVertexWriter(vdata, "texcoord") for i in range(vcount): texcoord.addData2(mesh_json["texcoords"][2 * i], mesh_json["texcoords"][2 * i + 1]) if has_weights: joint = GeomVertexWriter(vdata, "joint") weight = GeomVertexWriter(vdata, "weight") for i in range(vcount): joint_count = len(mesh_json["joints"][i]) joint.addData4( 0 if joint_count < 1 else mesh_json["joints"][i][0], 0 if joint_count < 2 else mesh_json["joints"][i][1], 0 if joint_count < 3 else mesh_json["joints"][i][2], 0 if joint_count < 4 else mesh_json["joints"][i][3]) weight.addData4( 0 if joint_count < 1 else mesh_json["weights"][i][0], 0 if joint_count < 2 else mesh_json["weights"][i][1], 0 if joint_count < 3 else mesh_json["weights"][i][2], 0 if joint_count < 4 else mesh_json["weights"][i][3]) # Create primitive prim = GeomTriangles(Geom.UHStatic) for i in range(vcount // 3): prim.addVertices(3 * i, 3 * i + 1, 3 * i + 2) geom = Geom(vdata) geom.add_primitive(prim) # Load texture tex = None if "texture" in mesh_json: tex = Loader(EComponent.base).loadTexture( (Path("resources") / mesh_json["texture"]).absolute()) tex.setMagfilter(SamplerState.FT_nearest) tex.setMinfilter(SamplerState.FT_nearest) # Create new geometry node geom_node = GeomNode(str(random.randint(0, 255)) + "_node") if tex is None: geom_node.addGeom(geom) else: attrib = TextureAttrib.make(tex) state = RenderState.make(attrib) geom_node.addGeom(geom, state) if EComponent.panda_root_node is not None: self.geom_path = EComponent.panda_root_node.attach_new_node( geom_node) self.geom_path.set_shader_input("object_id", self.object_id) # Set shader if has_weights and self.geom_path is not None: self.geom_path.setTag("shader type", "skinned") bone_mats = PTA_LMatrix4f() for _ in range(100): bone_mats.push_back(helper.np_mat4_to_panda(np.identity(4))) self.geom_path.set_shader_input(f"boneMats", bone_mats) # Disable culling self.geom_path.node().setBounds(OmniBoundingVolume()) self.geom_path.node().setFinal(True)
def makeSquare(x1, y1, z1, x2, y2, z2): format = GeomVertexFormat.getV3n3cpt2() vdata = GeomVertexData('square', format, Geom.UHDynamic) #initializing variables vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') color = GeomVertexWriter(vdata, 'color') texcoord = GeomVertexWriter(vdata, 'texcoord') #gotta draw the sqaure in the right plane #aligning vertexes to the right planes if x1 != x2: vertex.addData3(x1, y1, z1) vertex.addData3(x2, y1, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y2, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y2 - 1, 2 * z2 - 1)) else: #adding vertexes vertex.addData3(x1, y1, z1) vertex.addData3(x2, y2, z1) vertex.addData3(x2, y2, z2) vertex.addData3(x1, y1, z2) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1)) normal.addData3(normalized(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1)) normal.addData3(normalized(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1)) #adding different colors to the vertex for visibility color.addData4f(1.0, 0.0, 0.0, 1.0) color.addData4f(0.0, 1.0, 0.0, 1.0) color.addData4f(0.0, 0.0, 1.0, 1.0) color.addData4f(1.0, 0.0, 1.0, 1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) #quads aren't directly supported by the geom interface tris = GeomTriangles(Geom.UHDynamic) tris.addVertices(0, 1, 3) tris.addVertices(1, 2, 3) #initialize surface square = Geom(vdata) square.addPrimitive(tris) return square
def make_cylinder(num_segments=16, closed=True): """Make a uniform cylinder geometry. Keyword Arguments: num_segments {int} -- segments number (default: {16}) closed {bool} -- add caps (default: {True}) Returns: Geom -- p3d geometry """ vformat = GeomVertexFormat.get_v3n3t2() vdata = GeomVertexData('vdata', vformat, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') tcoord = GeomVertexWriter(vdata, 'texcoord') cyl_rows = num_segments * 2 cap_rows = num_segments + 1 if closed: vdata.uncleanSetNumRows(cyl_rows + 2 * cap_rows) else: vdata.uncleanSetNumRows(cyl_rows) for phi in np.linspace(0, 2 * np.pi, num_segments): x, y = np.cos(phi), np.sin(phi) for z in (-1, 1): vertex.addData3(x, y, z * 0.5) normal.addData3(x, y, 0) tcoord.addData2(phi / (2 * np.pi), (z + 1) / 2) prim = GeomTriangles(Geom.UHStatic) for i in range(num_segments - 1): prim.addVertices(i * 2, i * 2 + 3, i * 2 + 1) prim.addVertices(i * 2, i * 2 + 2, i * 2 + 3) if closed: for z in (-1, 1): vertex.addData3(0, 0, z * 0.5) normal.addData3(0, 0, z) tcoord.addData2(0, 0) for phi in np.linspace(0, 2 * np.pi, num_segments): x, y = np.cos(phi), np.sin(phi) vertex.addData3(x, y, z * 0.5) normal.addData3(0, 0, z) tcoord.addData2(x, y) for i in range(num_segments): r0 = cyl_rows r1 = r0 + cap_rows prim.addVertices(r0, r0 + i + 1, r0 + i) prim.addVertices(r1, r1 + i, r1 + i + 1) geom = Geom(vdata) geom.addPrimitive(prim) return geom