[211] [213] Rendering: physikalisch plausible Bildwirkung, Material je Bauteil, Kamerawahl
Skript: Nishita mit Sonnenstärke 1.0 und automatischer Belichtung nach Sonnenhöhe (gemessene Tabelle), AgX, Albedo höchstens 0.8, Wiese fürs Gelände, Ziegel/Blech/Kies nach Dachschicht, Kontext hellgrau, Glas mit Transmission (für Schattenstrahlen durchsichtig), leichte Umgebungsverdeckung. Export: Dachschichten je Baustoff statt alle als "Dach" (splitRoofMaterials), Rolle und Baustoff je Material in scene.json. Dialog: Auswahl aktuelle 3D-Ansicht oder gespeicherte Kamera samt Sonnenzeit; CLI --camera <Name|id>.
This commit is contained in:
11 files changed
+625
-38
No files matched your search
@@ -18,6 +18,24 @@
|
||||
# Koordinaten: Modell-Koordinaten (x = Ost, y = Nord, z = Hoehe, Meter). Der glTF-Import von Blender dreht die
|
||||
# Y-oben-Achsen des GLB (x, z, -y) genau auf diese zurueck, Kamera und Sonne brauchen keine Umrechnung.
|
||||
#
|
||||
# Materialien (`materials[]`): `name` = glTF-Materialname, `role` (terrain | roof | context | opening | building),
|
||||
# `component` (Baustoff, wenn er vom Namen abweicht: "Dach: Tonziegel"), `color` sRGB, `glass`, `textures`, `sizeM`.
|
||||
# Mehrere Eintraege gleichen Namens (gleicher Name, andere Farbe) ordnet das Skript ueber die Farbe zu.
|
||||
#
|
||||
# Bildwirkung (physikalisch plausibel, wie ein Cycles-Standardbild und nicht "nach Augenmass"):
|
||||
# * Himmel: Nishita (ab Blender 5.0 "Mehrfachstreuung"), Sonnenstaerke 1.0, Sonnenscheibe 0.545 Grad (Winkeldurchmesser der
|
||||
# Sonne), Luft/Staub/Ozon auf 1.0 (Standard). Sonnenstand aus Ort und Zeit der Szene (`sun`).
|
||||
# * Belichtung: Nishita rechnet absolut (weisse besonnte Flaeche ~ 45 bei 60 Grad Sonnenhoehe), die Kamera belichtet wie eine
|
||||
# Automatik (`auto_exposure`, ~ -5 EV mittags) und die Farbumsetzung ist AgX (Filmic, wo AgX fehlt): Lichter laufen weich aus,
|
||||
# Weiss frisst nicht aus. `sun.intensity` und `exposure` (EV) in scene.json uebersteuern (Voreinstellung 1.0 und automatisch).
|
||||
# * Albedo: kein Baustoff reflektiert 100 Prozent. Flaechenfarben und Texturen werden auf 0.80 (linear, = sRGB 0.91, frischer
|
||||
# weisser Putz) begrenzt, sonst frisst Weiss unter der Sonne aus.
|
||||
# * Gelaende: Wiese (Bildkarten der Bibliothek oder ohne diese ein Wiesengruen), Boden darunter erdig-gruen.
|
||||
# * Dach: Baustoff der aeussersten Dachschicht (Textur; ohne Textur Ziegel, Blech, Kies, Schiefer, Schindeln nach dem Namen).
|
||||
# * Kontext (Nachbargebaeude, importierte Koerper): neutral hellgrau (sRGB 0.78), matt.
|
||||
# * Glas: Transmission 1, Rauheit 0, IOR 1.5, leichte Toenung; fuer Schattenstrahlen durchsichtig, damit Sonne ins Innere kommt.
|
||||
# * Umgebungsverdeckung leicht (Cycles-Weltoption `ao_factor`), dazu die globale Beleuchtung mit 8 Abprallern.
|
||||
#
|
||||
# Ausgabe: Blender schreibt auf stdout "Sample x/y" (Cycles), dazu meldet das Skript Marken, die Dossier liest:
|
||||
# DOSSIER_STAGE <name> Phase (import, materials, render, denoise)
|
||||
# DOSSIER_DONE <pfad.png> fertig
|
||||
@@ -111,8 +129,33 @@ def principled(mat):
|
||||
return None
|
||||
|
||||
|
||||
ALBEDO_MAX = 0.80 # linear; frischer weisser Putz ~0.8, nichts in der Natur reflektiert 100 Prozent
|
||||
|
||||
|
||||
def srgb_to_linear(c):
|
||||
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
|
||||
|
||||
|
||||
def lin(rgb):
|
||||
"""sRGB-Tripel (0..1) -> linear."""
|
||||
return tuple(srgb_to_linear(float(c)) for c in rgb[:3])
|
||||
|
||||
|
||||
def clamp_albedo(rgb_linear):
|
||||
return tuple(min(float(c), ALBEDO_MAX) for c in rgb_linear[:3])
|
||||
|
||||
|
||||
def is_unset(color):
|
||||
"""Weiss = vom Modell nicht gesetzt (Baustoff-Farbe #ffffff, der Look kommt aus der Bildkarte)."""
|
||||
return all(float(c) >= 0.97 for c in color[:3])
|
||||
|
||||
|
||||
def make_glass(mat, spec):
|
||||
"""Verglasung: Transmission statt Alpha-Blending, damit Cycles Brechung und Spiegelung rechnet."""
|
||||
"""Verglasung: Transmission statt Alpha-Blending, damit Cycles Brechung und Spiegelung rechnet.
|
||||
|
||||
Physikalisch: klarer Glaskoerper, IOR 1.5, Rauheit 0, hauchduenn getoent. Fuer Schattenstrahlen ist die Scheibe
|
||||
durchsichtig (Mix mit Transparent ueber Light Path > Is Shadow Ray), sonst bliebe das Innere hinter Fenstern
|
||||
dunkel; das ist die uebliche Architektur-Naeherung fuer duenne Verglasung."""
|
||||
bsdf = principled(mat)
|
||||
if bsdf is None:
|
||||
return
|
||||
@@ -120,26 +163,39 @@ def make_glass(mat, spec):
|
||||
if base is not None:
|
||||
c = list(base.default_value)
|
||||
# Leichte Toenung, nie schwarz (Transmission faerbt mit der Basisfarbe).
|
||||
base.default_value = (0.85 + 0.15 * c[0], 0.85 + 0.15 * c[1], 0.85 + 0.15 * c[2], 1.0)
|
||||
base.default_value = (0.90 + 0.10 * c[0], 0.94 + 0.06 * c[1], 0.95 + 0.05 * c[2], 1.0)
|
||||
set_input(bsdf, ("Alpha",), 1.0)
|
||||
set_input(bsdf, ("Transmission Weight", "Transmission"), float(spec.get("transmission", 1.0)))
|
||||
set_input(bsdf, ("Roughness",), 0.03)
|
||||
set_input(bsdf, ("IOR",), float(spec.get("ior", 1.45)))
|
||||
set_input(bsdf, ("Roughness",), 0.0)
|
||||
set_input(bsdf, ("IOR",), float(spec.get("ior", 1.5)))
|
||||
set_input(bsdf, ("Metallic",), 0.0)
|
||||
if hasattr(mat, "blend_method"):
|
||||
mat.blend_method = "OPAQUE"
|
||||
if hasattr(mat, "surface_render_method"):
|
||||
mat.surface_render_method = "DITHERED"
|
||||
nt = mat.node_tree
|
||||
out = next((n for n in nt.nodes if n.type == "OUTPUT_MATERIAL"), None)
|
||||
if out is None:
|
||||
return
|
||||
lp = nt.nodes.new("ShaderNodeLightPath")
|
||||
clear = nt.nodes.new("ShaderNodeBsdfTransparent")
|
||||
mix = nt.nodes.new("ShaderNodeMixShader")
|
||||
nt.links.new(lp.outputs["Is Shadow Ray"], mix.inputs["Fac"])
|
||||
nt.links.new(bsdf.outputs["BSDF"], mix.inputs[1])
|
||||
nt.links.new(clear.outputs["BSDF"], mix.inputs[2])
|
||||
nt.links.new(mix.outputs["Shader"], out.inputs["Surface"])
|
||||
|
||||
|
||||
def add_textures(mat, spec, base_dir):
|
||||
"""Bildtexturen aus dem Material-System (ambientCG): Kachelung ueber Objektkoordinaten mit Box-Projektion.
|
||||
|
||||
Das GLB traegt keine UV-Koordinaten. Die Box-Projektion (Bild je Achse, weiche Uebergaenge) legt die Textur
|
||||
in Metern fest (`sizeM` = Kantenlaenge einer Kachel) und kommt ohne UVs aus."""
|
||||
in Metern fest (`sizeM` = Kantenlaenge einer Kachel) und kommt ohne UVs aus. Gibt zurueck, ob eine Farbkarte
|
||||
angebunden wurde."""
|
||||
bsdf = principled(mat)
|
||||
textures = spec.get("textures") or {}
|
||||
if bsdf is None or not textures:
|
||||
return
|
||||
return False
|
||||
nt = mat.node_tree
|
||||
size = float(spec.get("sizeM") or 1.0)
|
||||
coord = nt.nodes.new("ShaderNodeTexCoord")
|
||||
@@ -161,9 +217,19 @@ def add_textures(mat, spec, base_dir):
|
||||
nt.links.new(mapping.outputs["Vector"], node.inputs["Vector"])
|
||||
return node
|
||||
|
||||
has_color = False
|
||||
n = image_node(textures["color"], "sRGB") if "color" in textures else None
|
||||
if n is not None:
|
||||
nt.links.new(n.outputs["Color"], bsdf.inputs["Base Color"])
|
||||
has_color = True
|
||||
# Albedo begrenzen (ALBEDO_MAX): Textur * Faktor, damit helle Karten (Putz) nicht ausfressen.
|
||||
mul = nt.nodes.new("ShaderNodeMix")
|
||||
mul.data_type = "RGBA"
|
||||
mul.blend_type = "MULTIPLY"
|
||||
mul.inputs[0].default_value = 1.0
|
||||
# Eingaenge nach Platz (Blender 3.4 bis 5): 6 = A (Farbe), 7 = B (Farbe), Ausgang 2 = Ergebnis (Farbe).
|
||||
mul.inputs[7].default_value = (ALBEDO_MAX, ALBEDO_MAX, ALBEDO_MAX, 1.0)
|
||||
nt.links.new(n.outputs["Color"], mul.inputs[6])
|
||||
nt.links.new(mul.outputs[2], bsdf.inputs["Base Color"])
|
||||
n = image_node(textures["roughness"], "Non-Color") if "roughness" in textures else None
|
||||
if n is not None:
|
||||
nt.links.new(n.outputs["Color"], bsdf.inputs["Roughness"])
|
||||
@@ -176,19 +242,106 @@ def add_textures(mat, spec, base_dir):
|
||||
nm.inputs["Strength"].default_value = 0.8
|
||||
nt.links.new(n.outputs["Color"], nm.inputs["Color"])
|
||||
nt.links.new(nm.outputs["Normal"], bsdf.inputs["Normal"])
|
||||
return has_color
|
||||
|
||||
|
||||
# Ersatzlooks ohne Bildkarte: (Schluesselwoerter im Baustoffnamen) -> (Farbe sRGB, Rauheit, Metall).
|
||||
ROOF_LOOKS = (
|
||||
(("ziegel", "biber", "dachstein"), (0.56, 0.25, 0.16), 0.7, 0.0),
|
||||
(("schindel",), (0.40, 0.28, 0.18), 0.85, 0.0),
|
||||
(("blech", "zink", "titan", "kupfer", "alu", "stehfalz"), (0.55, 0.57, 0.58), 0.35, 1.0),
|
||||
(("kies",), (0.46, 0.44, 0.41), 0.95, 0.0),
|
||||
(("schiefer", "faserzement", "eternit"), (0.22, 0.23, 0.25), 0.8, 0.0),
|
||||
(("begr", "substrat", "gras"), (0.30, 0.42, 0.18), 0.95, 0.0),
|
||||
(("bitumen", "abdicht", "folie"), (0.10, 0.10, 0.10), 0.7, 0.0),
|
||||
)
|
||||
ROOF_DEFAULT = ((0.34, 0.30, 0.28), 0.75, 0.0)
|
||||
BUILDING_LOOKS = (
|
||||
(("backstein", "ziegel"), (0.60, 0.31, 0.22), 0.8, 0.0),
|
||||
(("beton",), (0.52, 0.52, 0.50), 0.85, 0.0),
|
||||
(("holz", "parkett", "lattung", "schalung", "bsp", "brettsperr"), (0.56, 0.40, 0.24), 0.65, 0.0),
|
||||
(("stahl", "metall", "alu"), (0.55, 0.57, 0.58), 0.4, 1.0),
|
||||
(("naturstein", "marmor", "granit"), (0.62, 0.60, 0.57), 0.5, 0.0),
|
||||
(("keramik", "fliese", "platte"), (0.75, 0.74, 0.72), 0.3, 0.0),
|
||||
)
|
||||
GRASS_SRGB = (0.33, 0.45, 0.18)
|
||||
CONTEXT_SRGB = (0.78, 0.78, 0.77)
|
||||
|
||||
|
||||
def pick_look(table, text):
|
||||
for words, color, rough, metal in table:
|
||||
if any(w in text for w in words):
|
||||
return color, rough, metal
|
||||
return None
|
||||
|
||||
|
||||
def flat_look(mat, spec):
|
||||
"""Material ohne Bildkarte: Farbe/Rauheit/Metall nach Verwendung (`role`), Baustoffname und gesetzter Farbe."""
|
||||
bsdf = principled(mat)
|
||||
if bsdf is None:
|
||||
return
|
||||
role = spec.get("role")
|
||||
text = (spec.get("component") or spec.get("name") or "").lower()
|
||||
color = tuple(spec.get("color") or (1.0, 1.0, 1.0))
|
||||
rough = float(spec.get("roughness", 0.9))
|
||||
metal = float(spec.get("metallic", 0.0))
|
||||
srgb = None
|
||||
if role == "terrain":
|
||||
srgb, rough, metal = GRASS_SRGB, 0.95, 0.0
|
||||
elif role == "context":
|
||||
srgb, rough, metal = CONTEXT_SRGB, 0.9, 0.0
|
||||
elif role == "roof":
|
||||
hit = pick_look(ROOF_LOOKS, text)
|
||||
if hit:
|
||||
srgb, rough, metal = hit
|
||||
elif is_unset(color):
|
||||
srgb, rough, metal = ROOF_DEFAULT
|
||||
# sonst gesetzte Farbe behalten
|
||||
elif is_unset(color):
|
||||
hit = pick_look(BUILDING_LOOKS, text)
|
||||
if hit:
|
||||
srgb, rough, metal = hit
|
||||
if srgb is not None:
|
||||
color = srgb
|
||||
set_input(bsdf, ("Base Color",), (*clamp_albedo(lin(color)), 1.0))
|
||||
set_input(bsdf, ("Roughness",), rough)
|
||||
set_input(bsdf, ("Metallic",), metal)
|
||||
|
||||
|
||||
def base_color_of(mat):
|
||||
bsdf = principled(mat)
|
||||
s = socket(bsdf, "Base Color") if bsdf is not None else None
|
||||
return tuple(s.default_value)[:3] if s is not None else None
|
||||
|
||||
|
||||
def pick_spec(mat, candidates):
|
||||
"""Mehrere Eintraege gleichen Namens (z. B. zwei "Oeffnungsteil" in verschiedener Farbe): der mit der naechsten Farbe."""
|
||||
if len(candidates) == 1:
|
||||
return candidates[0]
|
||||
have = base_color_of(mat)
|
||||
if have is None:
|
||||
return candidates[0]
|
||||
def dist(spec):
|
||||
want = lin(spec.get("color") or (1.0, 1.0, 1.0))
|
||||
return sum(abs(a - b) for a, b in zip(have, want))
|
||||
return min(candidates, key=dist)
|
||||
|
||||
|
||||
def apply_materials(specs, base_dir):
|
||||
stage("materials")
|
||||
by_name = {s["name"]: s for s in specs}
|
||||
by_name = {}
|
||||
for s in specs:
|
||||
by_name.setdefault(s["name"], []).append(s)
|
||||
for mat in bpy.data.materials:
|
||||
spec = by_name.get(mat.name) or by_name.get(strip_suffix(mat.name))
|
||||
if spec is None:
|
||||
candidates = by_name.get(mat.name) or by_name.get(strip_suffix(mat.name))
|
||||
if not candidates:
|
||||
continue
|
||||
spec = pick_spec(mat, candidates)
|
||||
ensure_nodes(mat)
|
||||
if spec.get("glass"):
|
||||
make_glass(mat, spec)
|
||||
else:
|
||||
add_textures(mat, spec, base_dir)
|
||||
elif not add_textures(mat, spec, base_dir):
|
||||
flat_look(mat, spec)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- Kamera
|
||||
@@ -290,11 +443,17 @@ def make_sky(sun):
|
||||
sky.sun_rotation = math.radians(float(sun.get("azimuth", 180.0)) % 360.0)
|
||||
sky.sun_disc = True
|
||||
sky.sun_size = math.radians(0.545)
|
||||
sky.sun_intensity = float(sun.get("intensity", 0.3))
|
||||
sky.sun_intensity = float(sun.get("intensity", 1.0))
|
||||
# Luft/Staub (Blender 3.x/4.x: air_density, dust_density; 5.x: aerosol_density) — nur setzen, was es gibt.
|
||||
for name in ("air_density", "dust_density", "aerosol_density"):
|
||||
for name in ("air_density", "dust_density", "aerosol_density", "ozone_density"):
|
||||
if hasattr(sky, name):
|
||||
setattr(sky, name, 1.0)
|
||||
# Leichte Umgebungsverdeckung (Cycles-Weltoption, in Blender 5 nur noch ao_factor/distance): dunkelt Ecken und Fugen
|
||||
# sanft ab, ohne die globale Beleuchtung zu ersetzen.
|
||||
ls = getattr(world, "light_settings", None)
|
||||
if ls is not None and hasattr(ls, "ao_factor"):
|
||||
ls.ao_factor = 0.4
|
||||
ls.distance = 1.5
|
||||
bg = nt.nodes.new("ShaderNodeBackground")
|
||||
out = nt.nodes.new("ShaderNodeOutputWorld")
|
||||
nt.links.new(sky.outputs["Color"], bg.inputs["Color"])
|
||||
@@ -315,7 +474,8 @@ def add_ground(objects):
|
||||
bpy.context.scene.collection.objects.link(obj)
|
||||
mat = bpy.data.materials.new("Boden")
|
||||
bsdf = principled(mat)
|
||||
set_input(bsdf, ("Base Color",), (0.30, 0.32, 0.28, 1.0))
|
||||
# Erdig-gruen, matter als die Wiese des Gelaendes (Albedo von Gras/Erde ~0.1 bis 0.2 linear).
|
||||
set_input(bsdf, ("Base Color",), (*lin((0.40, 0.46, 0.28)), 1.0))
|
||||
set_input(bsdf, ("Roughness",), 0.95)
|
||||
obj.data.materials.append(mat)
|
||||
|
||||
@@ -347,6 +507,29 @@ def setup_device(scene, wanted):
|
||||
return "CPU"
|
||||
|
||||
|
||||
# Gemessene Strahldichte einer weissen (Albedo 1), waagrechten, besonnten Flaeche im Nishita-Himmel bei Sonnenstaerke 1.0
|
||||
# (Cycles, Blender 5.3, Mittel ueber Sonne + Himmel, Luminanz) nach Sonnenhoehe in Grad. Die Werte sind weit ueber 1.0: Nishita
|
||||
# rechnet absolut (Mittagssonne ~ 100 000 lx), die Kamera muss das wie in der Fotografie belichten.
|
||||
SUNLIT_WHITE = ((1, 0.65), (3, 1.5), (6, 3.3), (10, 6.4), (15, 10.9), (20, 14.9), (30, 23.7), (45, 34.9), (60, 43.5), (70, 47.5), (90, 50.8))
|
||||
# Gewuenschte Szenenhelligkeit einer weissen besonnten Flaeche (Albedo 1). Weisser Putz (0.8) liegt dann bei ~1.2, noch unter dem
|
||||
# Weisspunkt der AgX-Kurve (Lichter laufen weich aus), Mittelgrau 0.18 bei ~0.27 (mittlere Anzeigehelligkeit).
|
||||
SUNLIT_TARGET = 1.5
|
||||
|
||||
|
||||
def auto_exposure(sun):
|
||||
"""Belichtung in EV wie eine Kamera mit Belichtungsautomatik: besonnte weisse Flaeche -> SUNLIT_TARGET, je nach
|
||||
Sonnenhoehe (tiefe Sonne = weniger Licht = hellere Belichtung) und Sonnenstaerke. Sonst brennt Weiss aus (Nishita ~ 45 bei 60 Grad)."""
|
||||
elevation = max(float(sun.get("elevation", 40.0)), 1.0)
|
||||
strength = max(float(sun.get("intensity", 1.0)), 1e-3)
|
||||
value = SUNLIT_WHITE[-1][1]
|
||||
for (e0, v0), (e1, v1) in zip(SUNLIT_WHITE, SUNLIT_WHITE[1:]):
|
||||
if elevation <= e1:
|
||||
f = max(0.0, (elevation - e0) / (e1 - e0))
|
||||
value = math.exp(math.log(v0) * (1.0 - f) + math.log(v1) * f)
|
||||
break
|
||||
return max(-8.0, min(3.0, -math.log2(value * strength / SUNLIT_TARGET)))
|
||||
|
||||
|
||||
def setup_cycles(spec):
|
||||
scene = bpy.context.scene
|
||||
scene.render.engine = "CYCLES"
|
||||
@@ -362,9 +545,21 @@ def setup_cycles(spec):
|
||||
scene.cycles.denoiser = "OPENIMAGEDENOISE"
|
||||
except Exception:
|
||||
pass
|
||||
# Sonniges Bild ohne ausgebrannte Flaechen: Sonnenstaerke und Belichtung zurueck (mit dem Blender-Standard
|
||||
# sind helle Putzflaechen im Sonnenlicht ausgebrannt). Beides in scene.json uebersteuerbar (`sun.intensity`, `exposure`).
|
||||
scene.view_settings.exposure = float(spec.get("exposure", -1.5))
|
||||
# Standard-Belichtung (0 EV) mit AgX (Filmic, wo es fehlt): die Farbumsetzung komprimiert Lichter, Weiss frisst nicht aus.
|
||||
# In scene.json uebersteuerbar (`exposure` in EV, `sun.intensity`).
|
||||
for transform in ("AgX", "Filmic", "Standard"):
|
||||
try:
|
||||
scene.view_settings.view_transform = transform
|
||||
break
|
||||
except TypeError:
|
||||
continue
|
||||
for look in ("AgX - Medium High Contrast", "Medium High Contrast"):
|
||||
try:
|
||||
scene.view_settings.look = look
|
||||
break
|
||||
except TypeError:
|
||||
continue
|
||||
scene.view_settings.exposure = float(spec["exposure"]) if "exposure" in spec else auto_exposure(spec.get("sun") or {})
|
||||
scene.cycles.max_bounces = 8
|
||||
# Glas-Abpraller heissen je nach Version `glass_bounces` (3.x) oder `transmission_bounces` (4.x+).
|
||||
for name in ("transmission_bounces", "glass_bounces"):
|
||||
|
||||
Reference in new issue
Block a user