File size: 6,690 Bytes
e3e73e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Generate example diffuse game textures (with baked-in lighting) for the demo.

These mimic the look of old game assets: a base albedo multiplied by a baked
directional light, ambient-occlusion darkening around details, soft highlight
blobs and weathering. They live in examples/ and are wired into gr.Examples.
"""

import os

import numpy as np
from PIL import Image, ImageFilter

rng = np.random.default_rng(42)
SIZE = 512
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "examples")
os.makedirs(OUT, exist_ok=True)


def blur(arr, radius):
    """Gaussian blur a float array (0..1) via PIL."""
    mono = arr.ndim == 2
    if mono:
        im = Image.fromarray((np.clip(arr, 0, 1) * 255.0).astype(np.uint8))
    else:
        im = Image.fromarray((np.clip(arr, 0, 1) * 255.0).astype(np.uint8), "RGB")
    out = np.asarray(im.filter(ImageFilter.GaussianBlur(radius))).astype(np.float32) / 255.0
    return out[..., 0] if mono else out


def save(name, rgb):
    Image.fromarray((np.clip(rgb, 0, 1) * 255.0).astype(np.uint8), "RGB").save(
        os.path.join(OUT, name)
    )


def light_map(h, w, top=1.25, bottom=0.55, highlight=0.35, ao=None):
    """Baked lighting: directional gradient + soft highlight blob, optional AO mask."""
    yy, xx = np.mgrid[0:h, 0:w]
    t = yy / max(h - 1, 1)
    grad = bottom + (top - bottom) * (1.0 - t) ** 1.4  # bright top, dark bottom
    dx, dy = xx / w - 0.55, yy / h - 0.28
    blob = np.exp(-(dx * dx + dy * dy) / (2 * 0.38**2))
    light = grad * (1.0 + highlight * blob)
    if ao is not None:
        light = light * ao
    return light[..., None]


def scifi_panel():
    h = w = SIZE
    yy, xx = np.mgrid[0:h, 0:w]
    # brushed gunmetal base
    base = np.full((h, w, 3), (0.62, 0.64, 0.70), dtype=np.float32)
    base += rng.normal(0, 0.02, (h, w, 3)).astype(np.float32)
    brush = rng.normal(0, 1, (h, w)).astype(np.float32)
    brush = (blur((brush + 3) / 6, 1.2) - 0.5) * 2 * 0.03  # centered ± grain
    base += brush[..., None]

    # panel grid grooves
    cell = 64
    groove = ((xx % cell) < 5) | ((yy % cell) < 5)

    # AO mask: dark around grooves and rivets, feathered
    ao = np.ones((h, w), np.float32)
    ao[groove] = 0.45
    rivet_pos = [(cx, cy) for cx in range(cell // 2, w, cell) for cy in range(cell // 2, h, cell)]
    rivets = np.zeros((h, w), bool)
    for cx, cy in rivet_pos:
        rivets |= np.hypot(xx - cx, yy - cy) <= 7
    ao[rivets] = 0.35
    ao = blur(ao, 3)

    # render rivets: dark ring + specular dot
    for cx, cy in rivet_pos:
        d = np.hypot(xx - cx, yy - cy)
        ring = 0.25 * np.exp(-((d - 5.0) ** 2) / 2.0)
        dot = 0.30 * np.exp(-(d**2) / (2 * 1.6**2))
        base = base * (1.0 - ring[..., None]) + dot[..., None]

    # scratches
    for _ in range(36):
        cx, cy = rng.uniform(0, w), rng.uniform(0, h)
        ang = rng.uniform(0, np.pi)
        ln = rng.uniform(10, 42)
        tx = np.clip((np.arange(ln) * np.cos(ang) + cx).astype(int), 0, w - 1)
        ty = np.clip((np.arange(ln) * np.sin(ang) + cy).astype(int), 0, h - 1)
        v = rng.choice([-0.14, 0.16])
        base[ty, tx] += v

    light = light_map(h, w, top=1.32, bottom=0.48, highlight=0.42, ao=ao)
    save("scifi_panel.png", base * light)


def stone_wall():
    h = w = SIZE
    yy, xx = np.mgrid[0:h, 0:w]
    n = 7
    cxs = (np.arange(n) + 0.5) * (w / n) + rng.uniform(-0.18, 0.18, n) * (w / n)
    cys = (np.arange(n) + 0.5) * (h / n) + rng.uniform(-0.18, 0.18, n) * (h / n)

    idx_x = np.zeros((h, w), int)
    best = np.full((h, w), np.inf)
    for cx in cxs:
        for cy in cys:
            d = np.hypot(xx - cx, yy - cy)
            m = d < best
            best[m] = d[m]
            idx_x[m] = int(cx)

    cells = np.zeros((h, w, 3), np.float32)
    cell_radius = (w / n) * 0.62
    rn = best / cell_radius
    mortar = rn > 0.82
    for cx in cxs:
        for cy in cys:
            m = idx_x == int(cx)
            tone = rng.uniform(0.52, 0.76, 3).astype(np.float32) * np.array([1.0, 0.93, 0.84], np.float32)
            cells[m] = tone

    # stone grain
    grain = rng.normal(0, 1, (h, w, 3)).astype(np.float32)
    grain = (blur((grain + 2) / 4, 1.4) - 0.5) * 0.35
    cells = np.clip(cells + grain, 0, 1)
    cells[mortar] = (0.30, 0.27, 0.24)

    # AO in crevices
    ao = np.ones((h, w), np.float32)
    ao[mortar] = 0.45
    ao = blur(ao, 4)

    light = light_map(h, w, top=1.45, bottom=0.42, highlight=0.22, ao=ao)
    save("stone_wall.png", cells * light)


def painted_metal():
    h = w = SIZE
    yy, xx = np.mgrid[0:h, 0:w]
    base = np.full((h, w, 3), (0.35, 0.42, 0.38), np.float32)  # faded teal paint

    # blotchy paint variation
    blobs = rng.normal(0, 1, (h, w)).astype(np.float32)
    blobs = (blur((blobs + 2) / 4, 5) - 0.5) * 0.9
    base = base * (1.0 + blobs[..., None])

    # rust clusters (mostly bottom half)
    rust = np.zeros((h, w), np.float32)
    for _ in range(26):
        cx, cy = rng.uniform(0, w), rng.uniform(h * 0.35, h)
        r = rng.uniform(8, 30)
        rust = np.maximum(rust, np.exp(-(np.hypot(xx - cx, yy - cy) ** 2) / (2 * r**2)))
    rust_color = np.array([0.62, 0.26, 0.10], np.float32)
    base = base * (1.0 - 0.75 * rust[..., None]) + rust[..., None] * rust_color

    # chipped paint → bright metal showing through
    chips = np.zeros((h, w), np.float32)
    for _ in range(40):
        cx, cy = rng.uniform(0, w), rng.uniform(0, h)
        r = rng.uniform(1.5, 5)
        chips = np.maximum(chips, np.exp(-(np.hypot(xx - cx, yy - cy) ** 2) / (2 * r**2)))
    metal = np.array([0.72, 0.73, 0.76], np.float32)
    base = base * (1.0 - chips[..., None]) + chips[..., None] * metal

    # scratches
    for _ in range(24):
        cx, cy = rng.uniform(0, w), rng.uniform(0, h)
        ang = rng.uniform(0, np.pi)
        ln = rng.uniform(8, 30)
        tx = np.clip((np.arange(ln) * np.cos(ang) + cx).astype(int), 0, w - 1)
        ty = np.clip((np.arange(ln) * np.sin(ang) + cy).astype(int), 0, h - 1)
        base[ty, tx] = np.array([0.78, 0.79, 0.82], np.float32)

    # grime streaks
    grime = np.zeros((h, w), np.float32)
    for _ in range(8):
        gx = rng.uniform(0, w)
        gw = rng.uniform(3, 12)
        grime += np.exp(-((xx - gx) ** 2) / (2 * gw**2)) * (yy / h) ** 1.5
    grime = np.clip(grime, 0, 1) * 0.28
    base = base * (1.0 - grime[..., None])

    # slight AO under rust
    ao = 1.0 - 0.22 * rust
    ao = blur(ao, 3)
    light = light_map(h, w, top=1.18, bottom=0.68, highlight=0.5, ao=ao)
    save("painted_metal.png", base * light)


if __name__ == "__main__":
    scifi_panel()
    stone_wall()
    painted_metal()
    print("Wrote examples to", OUT)