- 为铁球、陶瓷球和木球制作风格化材质,归档 CC0 原始参考 - 更新方形球洞和实心滑槽,统一 90° 弯槽并兼容旧存档 - 用顺流泡沫表示水流方向,为冰块制作三种交错的裂纹 - 同步 Blender 源资产、Godot 加载、工坊和试玩预览
174 lines
7.5 KiB
Python
174 lines
7.5 KiB
Python
"""Bake and apply the three stylized finishes on the existing UV spheres.
|
|
|
|
Run this file in Blender to bake new maps. The normal asset build reads the
|
|
saved maps, so rebuilding a model does not overwrite an edited texture.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
import struct
|
|
import zlib
|
|
|
|
import bpy
|
|
import numpy as np
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[4]
|
|
REFERENCES = ROOT / 'references/materials/ball-materials'
|
|
BALLS = ROOT / 'assets/balls'
|
|
SIZE = (1024, 512)
|
|
FINISHES = {
|
|
'iron_ball': ('Satin iron', (0.173, 0.226, 0.295), 0.46),
|
|
'ceramic_ball': ('Ivory porcelain glaze with cobalt scallops', (0.78, 0.69, 0.52), 0.0),
|
|
'wood_ball': ('Honey wood with painted grain', (0.49, 0.24, 0.09), 0.0),
|
|
}
|
|
|
|
|
|
def apply_material(obj, kind):
|
|
name, color, metallic = FINISHES[kind]
|
|
material = bpy.data.materials.new(name)
|
|
material.diffuse_color = (*color, 1)
|
|
material.use_nodes = True
|
|
nodes = material.node_tree.nodes
|
|
shader = nodes.get('Principled BSDF')
|
|
shader.inputs['Metallic'].default_value = metallic
|
|
for file, color_space, socket, position in (
|
|
('albedo.png', 'sRGB', 'Base Color', (-480, 230)),
|
|
('roughness.png', 'Non-Color', 'Roughness', (-480, -80)),
|
|
):
|
|
path = BALLS / kind / 'textures' / file
|
|
image = bpy.data.images.load(str(path), check_existing=False)
|
|
image.colorspace_settings.name = color_space
|
|
# Give each ball a reimportable glTF image name, including across a full build.
|
|
image.name = f'{kind}_{path.stem}'
|
|
image.pack()
|
|
image.filepath = str(path)
|
|
texture = nodes.new('ShaderNodeTexImage')
|
|
texture.label = socket
|
|
texture.location = position
|
|
texture.image = image
|
|
texture.extension = 'REPEAT'
|
|
material.node_tree.links.new(texture.outputs['Color'], shader.inputs[socket])
|
|
obj.data.materials.clear()
|
|
obj.data.materials.append(material)
|
|
for face in obj.data.polygons:
|
|
face.material_index = 0
|
|
face.use_smooth = True
|
|
|
|
|
|
def read_reference(path):
|
|
image = bpy.data.images.load(str(REFERENCES / path), check_existing=False)
|
|
width, height = image.size
|
|
values = np.empty(width * height * 4, dtype=np.float32)
|
|
image.pixels.foreach_get(values)
|
|
bpy.data.images.remove(image)
|
|
return values.reshape(height, width, 4)[..., :3]
|
|
|
|
|
|
def lightness(map_rgb):
|
|
return map_rgb @ np.array([0.2126, 0.7152, 0.0722], dtype=np.float32)
|
|
|
|
|
|
def sample(tile, u, v):
|
|
"""Bilinear sample a repeating reference in its local projection."""
|
|
height, width = tile.shape
|
|
u, v = u * width, v * height
|
|
i, j = np.floor(u).astype(int), np.floor(v).astype(int)
|
|
du, dv = u - i, v - j
|
|
low = (1 - du) * tile[j % height, i % width] + du * tile[j % height, (i + 1) % width]
|
|
high = (1 - du) * tile[(j + 1) % height, i % width] + du * tile[(j + 1) % height, (i + 1) % width]
|
|
return low * (1 - dv) + high * dv
|
|
|
|
|
|
def project(tile, x, y, z, scale):
|
|
"""Project from three axes without concentrating image detail at UV poles."""
|
|
weights = np.stack([x**4, y**4, z**4])
|
|
a = sample(tile, y * scale + .31, z * scale + .13)
|
|
b = sample(tile, x * scale + .51, z * scale + .39)
|
|
c = sample(tile, x * scale + .21, y * scale + .77)
|
|
return (weights[0] * a + weights[1] * b + weights[2] * c) / weights.sum(axis=0)
|
|
|
|
|
|
def iron(x, y, z):
|
|
base_tile = lightness(read_reference('iron/textures/metal032/Metal032_1K-JPG_Color.jpg'))
|
|
sheen = lightness(read_reference('iron/textures/metal032/Metal032_1K-JPG_Roughness.jpg'))
|
|
brushed = lightness(read_reference('iron/textures/metal009/Metal009_1K-JPG_Color.jpg'))
|
|
base = project(base_tile - np.mean(base_tile), x, y, z, .58)
|
|
scratches = project(brushed - np.mean(brushed), x, y, z, .85)
|
|
satin = project(sheen - np.mean(sheen), x, y, z, .54)
|
|
# Preserve an even cool silhouette; the fine marks only vary local tone and sheen.
|
|
color = np.array([.45, .515, .58]) + (.24 * base + .18 * scratches)[..., None]
|
|
return color, np.clip(.44 + .21 * satin + .13 * scratches, .38, .50)
|
|
|
|
|
|
def porcelain(x, y, z):
|
|
source = read_reference('ceramic/textures/porcelain-001/Porcelain_001_basecolor.png')
|
|
blue = source[source[..., 2] - source[..., 0] > .17].mean(axis=0)
|
|
pigment = .23 * blue + .77 * np.array([.13, .315, .51])
|
|
# Tilt the decoration so part of it stays readable in the elevated game view.
|
|
latitude = np.arcsin(np.clip(.966 * z + .259 * x, -1, 1))
|
|
longitude = np.arctan2(y, .966 * x - .259 * z)
|
|
phase = 5 * longitude + .44
|
|
band = np.abs(latitude) - .53
|
|
scallop = .145 * (np.cos(phase) + .20 * np.cos(2 * phase))
|
|
stroke = np.abs(band - scallop) - (.080 + .011 * np.sin(phase))
|
|
# The source's little teardrop fan becomes a few soft dabs inside each arc.
|
|
cell = (phase + np.pi) % (2 * np.pi) - np.pi
|
|
for angle in (-.71, 0, .71):
|
|
dab = np.hypot((cell - angle) / .19, (band - .30 + abs(angle) * .09) / .045)
|
|
stroke = np.minimum(stroke, (dab - 1) * .045)
|
|
mask = np.clip(.5 - stroke / .016, 0, 1)
|
|
variation = .008 * np.sin(5 * y + 3 * z) * np.cos(4 * x - 2 * z)
|
|
ivory = np.array([.81, .766, .677]) + variation[..., None]
|
|
color = ivory * (1 - mask[..., None]) + (pigment + variation[..., None]) * mask[..., None]
|
|
# Patches of glaze stay glossy without the constant zero roughness in the source pack.
|
|
return color, .19 + .023 * mask + variation
|
|
|
|
|
|
def wood(x, y, z):
|
|
natural = lightness(read_reference('wood/textures/wood092/Wood092_1K-JPG_Color.jpg'))
|
|
painted = lightness(read_reference('wood/textures/handpainted-wood/oga-wood-painted.png'))
|
|
# Lay large bands over the sphere like grain in a turned wooden blank.
|
|
# A tilted axis puts the grain over the crown as well as down the sides.
|
|
side = .72 * x + .69 * z
|
|
height = .72 * z - .69 * x
|
|
radial = np.hypot(side + .51, y - .40)
|
|
wobble = .059 * np.sin(5.2 * height + 3.4 * side) + .021 * np.sin(12 * height - 2 * y)
|
|
grain = (.5 + .5 * np.sin(37 * (radial + wobble))) ** 3
|
|
painted_tone = project(painted - np.mean(painted), side, y, height, .42)
|
|
fine_tone = project(natural - np.mean(natural), side, y, height, .67)
|
|
color = np.array([.73, .50, .32]) - grain[..., None] * np.array([.105, .113, .090])
|
|
color += (.12 * painted_tone + .10 * fine_tone)[..., None]
|
|
return color, np.clip(.63 + .12 * grain + .12 * fine_tone, .59, .77)
|
|
|
|
|
|
def save_png(path, values):
|
|
data = (np.clip(values, 0, 1) * 255 + .5).astype(np.uint8)
|
|
# UV texture v=0 is the bottom row; PNG stores the top row first.
|
|
pixels = b''.join(b'\0' + row.tobytes() for row in data[::-1])
|
|
|
|
def chunk(kind, payload):
|
|
return (struct.pack('!I', len(payload)) + kind + payload
|
|
+ struct.pack('!I', zlib.crc32(kind + payload)))
|
|
|
|
color_type = 2 if data.ndim == 3 else 0
|
|
header = struct.pack('!IIBBBBB', *SIZE, 8, color_type, 0, 0, 0)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', header)
|
|
+ chunk(b'IDAT', zlib.compress(pixels, 9)) + chunk(b'IEND', b''))
|
|
|
|
|
|
def bake():
|
|
width, height = SIZE
|
|
u, v = np.meshgrid((np.arange(width) + .5) / width, (np.arange(height) + .5) / height)
|
|
theta, phi = u * np.pi * 2, v * np.pi
|
|
x, y, z = np.sin(phi) * np.cos(theta), np.sin(phi) * np.sin(theta), -np.cos(phi)
|
|
for kind, finish in (('iron_ball', iron), ('ceramic_ball', porcelain), ('wood_ball', wood)):
|
|
color, roughness = finish(x, y, z)
|
|
directory = BALLS / kind / 'textures'
|
|
save_png(directory / 'albedo.png', color)
|
|
save_png(directory / 'roughness.png', roughness)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
bake()
|