Spaces:
Running

pydraw / pydraw3d /models_fire.py
linoyts's picture
linoyts HF Staff
retro 64 renderer (64x64 pixel-art sprites), 3D search option, wave-11 code (worlds, light, poses, imagination, art styles, camera, natural statistics) - pydraw 758e4a7
1dd7af8 verified
Raw History Blame Contribute Delete
2.82 kB
"""Fire: campfire / bonfire as SDF models. The flames are emissive, so at night they become a point light
(pydraw3d/light.py find_lights) that lights the logs, stones, ground and anything nearby."""
import numpy as np
from pydraw3d.models import model, Part, V
from pydraw3d import sdf as S
def _flame(p, base, r, h, seed, wob=.5):
"""a flame tongue: a teardrop (ellipsoid narrowing to a tip) bent and flickered by noise"""
q = p - base
y = np.clip(q[:, 1] / h, 0, 1)
bend = wob * r * (S.noise3(np.stack([q[:, 1] * 6 + seed, np.full(len(q), seed * 1.7), np.zeros(len(q))], -1) * 1.0) - .5) * y * 2
x = q[:, 0] - bend
rad = r * np.sqrt(np.clip(np.sin(np.pi * np.clip(y * .85 + .15, 0, 1)), 0, 1)) * (1 - y) ** .5
d2 = np.sqrt(x * x + q[:, 2] ** 2) - rad
return np.maximum(d2, np.maximum(-q[:, 1], q[:, 1] - h)) * .6
@model("campfire", "bonfire", "fire", "camp fire", aspect=1.25)
def m_campfire(col, inst=None):
flame_c = col or (255, 140, 30)
seed = 3.0
def logs(p):
d = None
for a in (0.3, 1.35, 2.4):
c, s = np.cos(a), np.sin(a)
e = S.capsule(p, (-.42 * c, .07, -.42 * s), (.42 * c, .07, .42 * s), .065)
d = e if d is None else np.minimum(d, e)
# two leaning logs (a teepee)
d = np.minimum(d, S.capsule(p, (-.3, .05, .05), (.02, .42, 0), .05))
d = np.minimum(d, S.capsule(p, (.3, .05, -.05), (-.02, .42, 0), .05))
return d
def stones(p):
d = None
for i in range(9):
a = i / 9 * 2 * np.pi
e = S.ellipsoid(p, V(.58 * np.cos(a), .05, .58 * np.sin(a) * .8), V(.1, .07, .08))
d = e if d is None else np.minimum(d, e)
return d
def outer(p):
d = _flame(p, V(0, .06, 0), .2, .8, seed)
d = np.minimum(d, _flame(p, V(.15, .06, .03), .11, .5, seed + 2))
d = np.minimum(d, _flame(p, V(-.16, .06, -.02), .1, .58, seed + 5))
d = np.minimum(d, _flame(p, V(.05, .06, .1), .09, .66, seed + 7))
return d
def mid(p):
d = _flame(p, V(.01, .06, -.07), .15, .55, seed + 1, .35)
return np.minimum(d, _flame(p, V(-.1, .06, -.08), .08, .38, seed + 4, .4))
def core(p):
return _flame(p, V(0, .06, -.15), .085, .3, seed + 3, .2)
# rule: a flame is layered: deep orange-red outside, orange-yellow inside, a pale core near the wood; the layers
# step toward the camera so all three show (their emission ignores shading)
return [Part(stones, (120, 116, 110), spec=.1, rough=.8, tex="stone"),
Part(logs, (80, 52, 34), spec=.05, rough=.9, tex="bark"),
Part(outer, (230, 70, 15), emit=1.0, spec=0, rough=1),
Part(mid, flame_c, emit=1.0, spec=0, rough=1),
Part(core, (255, 230, 150), emit=1.0, spec=0, rough=1)]