flip normals, simplyfy mesh table

This commit is contained in:
Seth Trowbridge 2026-02-28 21:25:54 -05:00
parent 3987cc9ed5
commit b0c4bf65a1

View File

@ -3,42 +3,24 @@ import bmesh
import math import math
from mathutils import Vector, Matrix from mathutils import Vector, Matrix
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Lookup tables # Canonical primitive builders (Z-up, origin at centre)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _build_unit_circle(segments: int) -> list:
"""(cos, sin) sampled once per segment. Reused by all primitives."""
return [
(math.cos(2 * math.pi * i / segments),
math.sin(2 * math.pi * i / segments))
for i in range(segments)
]
def _build_latitude_stack(segments: int) -> list:
"""(sin_phi, cos_phi) for each latitude ring, poles included."""
rings = max(segments // 2, 2)
return [
(math.sin(math.pi * r / rings),
math.cos(math.pi * r / rings))
for r in range(rings + 1)
]
class _Tables: class _Tables:
"""Precomputed trig tables for a given segment count.""" """Precomputed trig tables for a given segment count."""
def __init__(self, segments: int): def __init__(self, segments: int):
self.segments = segments self.segments = segments
segScalar = 2 * math.pi / segments
self.circle = [
(math.cos(segScalar * i), math.sin(segScalar * i)) for i in range(segments)
]
self.rings = max(segments // 2, 2) self.rings = max(segments // 2, 2)
self.circle = _build_unit_circle(segments) ringScalar = math.pi / self.rings
self.latitudes = _build_latitude_stack(segments) self.latitudes = [
(math.sin(ringScalar * r), math.cos(ringScalar * r)) for r in range(self.rings + 1)
]
# ---------------------------------------------------------------------------
# Canonical primitive builders (Z-up, origin at centre)
# ---------------------------------------------------------------------------
def _sphere_positions(radius: float, tables: _Tables) -> list: def _sphere_positions(radius: float, tables: _Tables) -> list:
out = [] out = []
@ -104,21 +86,21 @@ def add_capped_frustum(bm: bmesh.types.BMesh,
for i in range(segs): for i in range(segs):
nxt = (i + 1) % segs nxt = (i + 1) % segs
try: try:
bm.faces.new((hr[i], hr[nxt], tr[nxt], tr[i])) bm.faces.new((tr[i], tr[nxt], hr[nxt], hr[i]))
except ValueError: except ValueError:
pass pass
hcv = bm.verts.new(head_pos) hcv = bm.verts.new(head_pos)
for i in range(segs): for i in range(segs):
try: try:
bm.faces.new((hcv, hr[(i + 1) % segs], hr[i])) bm.faces.new((hcv, hr[i], hr[(i + 1) % segs]))
except ValueError: except ValueError:
pass pass
tcv = bm.verts.new(tail_pos) tcv = bm.verts.new(tail_pos)
for i in range(segs): for i in range(segs):
try: try:
bm.faces.new((tcv, tr[i], tr[(i + 1) % segs])) bm.faces.new((tcv, tr[(i + 1) % segs], tr[i]))
except ValueError: except ValueError:
pass pass