"""Solids for the 3D views, built in code (no model files: every shape here is ours). Panda3D. Each function returns a NodePath. Flat-shaded: every face carries its own normal.""" from __future__ import annotations import math from panda3d.core import (Geom, GeomNode, GeomTriangles, GeomVertexData, GeomVertexFormat, GeomVertexWriter, NodePath, Point3, Triangulator) def rgba(colour, a=1.0): """'#rrggbb' or an (r, g, b) of 0..1 -> (r, g, b, a).""" if isinstance(colour, str): return tuple(int(colour[i:i + 2], 16) / 255 for i in (1, 3, 5)) + (a,) return tuple(colour[:3]) + (a,) class Mesh: """Collects triangles (each with its own flat normal) and makes one GeomNode.""" def __init__(self, name): self.name = name self.vdata = GeomVertexData(name, GeomVertexFormat.getV3n3c4(), Geom.UHStatic) self.v = GeomVertexWriter(self.vdata, "vertex") self.n = GeomVertexWriter(self.vdata, "normal") self.c = GeomVertexWriter(self.vdata, "color") self.tris = GeomTriangles(Geom.UHStatic) self.count = 0 def tri(self, a, b, c, colour): a, b, c = Point3(*a), Point3(*b), Point3(*c) nrm = (b - a).cross(c - a) if nrm.length() == 0: return nrm.normalize() for p in (a, b, c): self.v.addData3(p) self.n.addData3(nrm) self.c.addData4(*colour) self.tris.addVertices(self.count, self.count + 1, self.count + 2) self.count += 3 def quad(self, a, b, c, d, colour): self.tri(a, b, c, colour) self.tri(a, c, d, colour) def node(self): geom = Geom(self.vdata) geom.addPrimitive(self.tris) gn = GeomNode(self.name) gn.addGeom(geom) return NodePath(gn) def box(name, sx, sy, sz, colour, centre=(0, 0, 0), bottom=True): """A box sx by sy by sz. bottom: centre is the middle of its base (else its middle).""" col = rgba(colour) x0, y0 = centre[0] - sx / 2, centre[1] - sy / 2 z0 = centre[2] if bottom else centre[2] - sz / 2 x1, y1, z1 = x0 + sx, y0 + sy, z0 + sz m = Mesh(name) m.quad((x0, y0, z1), (x1, y0, z1), (x1, y1, z1), (x0, y1, z1), col) # top m.quad((x0, y1, z0), (x1, y1, z0), (x1, y0, z0), (x0, y0, z0), col) # bottom m.quad((x0, y0, z0), (x1, y0, z0), (x1, y0, z1), (x0, y0, z1), col) # front (-y) m.quad((x1, y1, z0), (x0, y1, z0), (x0, y1, z1), (x1, y1, z1), col) # back (+y) m.quad((x0, y1, z0), (x0, y0, z0), (x0, y0, z1), (x0, y1, z1), col) # left m.quad((x1, y0, z0), (x1, y1, z0), (x1, y1, z1), (x1, y0, z1), col) # right return m.node() def cylinder(name, r, h, colour, sides=16, r_top=None, cap=True): """Upright from z=0 to z=h. r_top: a cone or tapered post.""" col = rgba(colour) rt = r if r_top is None else r_top m = Mesh(name) ring = [(math.cos(2 * math.pi * i / sides), math.sin(2 * math.pi * i / sides)) for i in range(sides)] for i in range(sides): (ax, ay), (bx, by) = ring[i], ring[(i + 1) % sides] m.quad((ax * r, ay * r, 0), (bx * r, by * r, 0), (bx * rt, by * rt, h), (ax * rt, ay * rt, h), col) if cap: if rt > 0: m.tri((0, 0, h), (ax * rt, ay * rt, h), (bx * rt, by * rt, h), col) m.tri((0, 0, 0), (bx * r, by * r, 0), (ax * r, ay * r, 0), col) return m.node() def sphere(name, r, colour, rings=6, sides=10): col = rgba(colour) m = Mesh(name) def p(i, j): th, ph = math.pi * i / rings, 2 * math.pi * j / sides return (r * math.sin(th) * math.cos(ph), r * math.sin(th) * math.sin(ph), r * math.cos(th)) for i in range(rings): for j in range(sides): a, b, c, d = p(i, j), p(i + 1, j), p(i + 1, j + 1), p(i, j + 1) if i == 0: m.tri(a, b, c, col) elif i == rings - 1: m.tri(a, b, d, col) else: m.quad(a, b, c, d, col) return m.node() def flat_polygon(name, rings, z, colour): """Flat polygons (lists of (x, y), concave is fine) at height z, facing up. A list of rings is drawn as separate pieces (a county in more than one part).""" col = rgba(colour) m = Mesh(name) for ring in rings: if len(ring) > 2 and ring[0] == ring[-1]: ring = ring[:-1] if len(ring) < 3: continue t = Triangulator() for x, y in ring: t.addPolygonVertex(t.addVertex(x, y)) t.triangulate() for k in range(t.getNumTriangles()): a, b, c = (t.getVertex(t.getTriangleV0(k)), t.getVertex(t.getTriangleV1(k)), t.getVertex(t.getTriangleV2(k))) m.tri((a[0], a[1], z), (b[0], b[1], z), (c[0], c[1], z), col) # Triangulator winds them facing up return m.node()