Files
Bedrock/brand/ribbon-study/shell.py
T

94 lines
3.8 KiB
Python

"""Procedural ribbon shells. Run with --turn 20 to rotate every shell in depth."""
import argparse
import json
from math import asin, atan2, cos, pi, radians, sin, sqrt
from pathlib import Path
import struct
import zlib
BG = (68, 23, 68)
STYLE_FILE = Path(__file__).with_name("shell-styles.json")
def make_shader(style, turn):
roll, pitch, linear, quadratic = style["angles"]
cr, sr = cos(radians(roll)), sin(radians(roll))
cp, sp = cos(radians(pitch)), sin(radians(pitch))
ct, st = cos(radians(turn)), sin(radians(turn))
bands = style["bands"]
palette = {tuple(map(int, pair.split("/"))): tuple(bytes.fromhex(ink[1:]))
for pair, ink in style["inks"].items()}
mode = style["mode"]
def band_at(u, v, w):
if mode == "petals":
for index, (a, b) in enumerate(((0, 1), (.8660254, -.5), (-.8660254, -.5)), 1):
radial, tangent = a*u+b*v, b*u-a*v
if radial + .72*w*tangent > .725:
return index
return 0
if mode == "spiral":
phase, width = bands[0]
cycle = 3*(.5+asin(max(-1, min(1, v)))/pi) - atan2(w,u)/(2*pi) + phase
return 1+round(cycle)%3 if abs(cycle-round(cycle)) < width/2 else 0
angle = radians(linear*u + quadratic*(2*u*u-1))
height = v*cos(angle) + w*sin(angle)
for index, (centre, width) in enumerate(bands, 1):
if abs(height-centre) <= width/2:
return index
return 0
def face(x, y, z):
depth = st*x+ct*z
return band_at(ct*x-st*z, cp*y+sp*depth, cp*depth-sp*y)
def pixel(x, y):
d = 1-x*x-y*y
if d <= 0:
return BG
z = sqrt(d)
u, v = cr*x+sr*y, cr*y-sr*x
front, back = face(u,v,z), face(u,v,-z)
if not (front or back):
return BG
# Each pair of visible faces defines one ink mixture. New angles can
# reveal pairs absent from the reference view.
color = palette.get((front, back))
if color is None:
ink = palette.get((front, 0), palette.get((front, front), BG))
rear = palette.get((0, back), palette.get((back, back), BG))
color = tuple((f+r)/2 for f,r in zip(ink,rear)) if front and back else (ink if front else rear)
light = 1+.045*(-x+y)/2
return tuple(min(255,round(k*light)) for k in color)
return pixel
def sheet(turn, size=118, samples=3):
gap = size//3
width, height = 4*(size+gap)+gap, 3*(size+gap)+gap
pixels = bytearray(bytes(BG)*width*height)
for i, style in enumerate(json.loads(STYLE_FILE.read_text())):
shade = make_shader(style, turn)
left, top = gap+(i%4)*(size+gap), gap+(i//4)*(size+gap)
for row in range(size):
for col in range(size):
colors = [shade(2*(col+(a+.5)/samples)/size-1,
1-2*(row+(b+.5)/samples)/size)
for a in range(samples) for b in range(samples)]
start = 3*((top+row)*width+left+col)
pixels[start:start+3] = bytes(round(sum(c)/len(colors)) for c in zip(*colors))
def chunk(kind, data):
return (struct.pack(">I", len(data))+kind+data+
struct.pack(">I", zlib.crc32(kind+data)))
scanlines = b"".join(b"\0"+pixels[i:i+3*width] for i in range(0,len(pixels),3*width))
return (b"\x89PNG\r\n\x1a\n"+chunk(b"IHDR", struct.pack(">2I5B",width,height,8,2,0,0,0))+
chunk(b"IDAT",zlib.compress(scanlines,9))+chunk(b"IEND",b""))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--turn", type=float, default=0)
parser.add_argument("--output", type=Path, default=Path(__file__).with_suffix(".png"))
options = parser.parse_args()
options.output.write_bytes(sheet(options.turn))