"""Screen-space passes for simulated effects that are not solid (wave 12), run on the finished 3D image: smoke / steam the particles of composites3.Fx projected through the scene camera and splatted as soft puffs (depth-tested against the render, lit brighter on their upper / sun side, Beer-Lambert opacity) lightning a dielectric-breakdown channel (growth.dbm_lightning) drawn into the sky as a hot core + glow, plus a cold flash on the whole frame """ import math import numpy as np from .. import sdf as S def projector(cam): camd, O, R, f, y0, n = cam["camd"], np.asarray(cam["O"], float), cam["R"], cam["f"], cam["y0"], cam["n"] k = n / R def proj(P): rel = P - O if camd is None: z = np.maximum(rel[:, 2], 1e-6) x = R / 2 + f * rel[:, 0] / z y = y0 - f * rel[:, 1] / z else: fw = rel @ camd.fwd z = np.maximum(fw, 1e-6) fF = R * camd.F a = rel @ camd.right / z b = rel @ camd.up / z x = R / 2 + a * fF y = R / 2 - b * fF - camd.shift * R return x * k, y * k, np.linalg.norm(rel, axis=1), z return proj def _fx_objs(objs): from pydraw.registry import CONCEPTS out = [] for o in objs: spec = CONCEPTS.get(o.name, {}).get("sim") if spec and spec.get("kind") == "fx": out.append((o, spec)) return out def puffs(img, depth, X, r, op, col, proj, f_pix, sun=(-.5, .7, -.4), dens=1.0): """splat particles (world positions X, world radii r, opacities op) onto img (linear-ish 0..1)""" n = img.shape[0] x, y, dist, _ = proj(X) rp = r * f_pix / np.maximum(dist, 1e-6) order = np.argsort(-dist) acc = np.zeros((n, n)) lit = np.zeros((n, n)) rng = np.random.default_rng(0) for i in order: R_ = rp[i] if R_ < .5 or op[i] <= .01: continue x0, x1 = int(max(0, x[i] - 2 * R_)), int(min(n, x[i] + 2 * R_ + 1)) y0, y1 = int(max(0, y[i] - 2 * R_)), int(min(n, y[i] + 2 * R_ + 1)) if x0 >= x1 or y0 >= y1: continue yy, xx = np.mgrid[y0:y1, x0:x1] d2 = ((xx - x[i]) ** 2 + (yy - y[i]) ** 2) / (R_ * R_) # rule: a puff is a soft ball: gaussian density, lumpy edge from noise w = np.exp(-d2 * 2.2) * op[i] * dens vis = depth[y0:y1, x0:x1] > dist[i] - r[i] w = w * vis # rule: light from above / the sun side: the upper part of each puff is brighter sh = np.clip(.55 + .45 * (-(yy - y[i]) / R_ * .8 + (xx - x[i]) / R_ * sun[0] * .4), .25, 1.0) acc[y0:y1, x0:x1] += w lit[y0:y1, x0:x1] += w * sh a = 1 - np.exp(-acc * 1.4) L = np.where(acc > 1e-6, lit / np.maximum(acc, 1e-6), 1) lo_ = .85 if col[0] > 200 else .55 # rule: steam is lit through (bright), smoke absorbs (dark underside) c = (np.asarray(col, float) / 255.0)[None, None, :] * (lo_ + (1.15 - lo_) * L[..., None]) return img * (1 - a[..., None]) + np.clip(c, 0, 1) * a[..., None] def bolt_layer(n, seed=0, x_frac=None, bottom=None): """lightning as an (n, n) intensity image: core + branches, blurred glow""" from PIL import Image, ImageDraw, ImageFilter from .growth import dbm_lightning from .build import cached cells, parent, main = cached(("bolt", seed), lambda: dbm_lightning(seed=seed)) H, W = 150, 90 rng = np.random.default_rng(seed) xf = x_frac if x_frac is not None else rng.uniform(.3, .7) bot = bottom if bottom is not None else n * .62 sx = bot / H ox = xf * n - (W / 2) * sx pts = np.stack([cells[:, 1] * sx + ox, cells[:, 0] * sx], 1) S_ = 2 im = Image.new("L", (n * S_, n * S_), 0) d = ImageDraw.Draw(im) main_set = set(main) from .growth import depth_of for i, p in enumerate(parent): if p < 0: continue a, b = pts[p] * S_, pts[i] * S_ if i in main_set: d.line([tuple(a), tuple(b)], fill=255, width=max(2, int(n / 160 * S_))) else: d.line([tuple(a), tuple(b)], fill=150, width=max(1, int(n / 400 * S_))) core = np.asarray(im.resize((n, n), Image.LANCZOS)).astype(float) / 255 glow = np.asarray(im.resize((n, n), Image.LANCZOS).filter(ImageFilter.GaussianBlur(n / 60))).astype(float) / 255 glow2 = np.asarray(im.resize((n, n), Image.LANCZOS).filter(ImageFilter.GaussianBlur(n / 18))).astype(float) / 255 return core, glow, glow2 def post3d(img, depth, objs, scene, cam): """img (n, n, 3) in 0..1 after grading; returns the image with smoke / steam / lightning""" fx = _fx_objs(objs) want_bolt = "lightning" in (scene.get("sim_fx") or []) if not fx and not want_bolt: return img from .composites import KINDS n = img.shape[0] proj = projector(cam) f_pix = (cam["f"] if cam["camd"] is None else cam["R"] * cam["camd"].F) * n / cam["R"] out = img.copy() for o, spec in fx: res = KINDS["fx"].prepare(spec)["res"] q = res["X"] P = o.base + S.rot_y(q * o.scale, o.yaw) out = puffs(out, depth, P, res["r"] * o.scale, res["op"], res["col"], proj, f_pix, dens=.55 if spec["fx"] == "steam" else 1.8) if want_bolt: core, glow, glow2 = bolt_layer(n, seed=int(scene.get("seed", 0))) sky = ~np.isfinite(depth) | (depth > np.nanpercentile(np.where(np.isfinite(depth), depth, np.nan), 90) if np.isfinite(depth).any() else True) k = sky.astype(float) out = out * .82 + np.array([.03, .04, .08]) # rule: the flash-lit storm: a cold blue cast over everything add = core[..., None] * np.array([1.0, 1.0, 1.0]) + glow[..., None] * np.array([.55, .6, 1.0]) * .9 + \ glow2[..., None] * np.array([.3, .35, .6]) * .5 out = out + add * k[..., None] return np.clip(out, 0, 1)