- 为铁球、陶瓷球和木球制作风格化材质,归档 CC0 原始参考 - 更新方形球洞和实心滑槽,统一 90° 弯槽并兼容旧存档 - 用顺流泡沫表示水流方向,为冰块制作三种交错的裂纹 - 同步 Blender 源资产、Godot 加载、工坊和试玩预览
144 lines
6.1 KiB
Python
144 lines
6.1 KiB
Python
"""Bake the storybook ice color maps and bind them to the existing cube."""
|
|
|
|
import struct
|
|
import zlib
|
|
from pathlib import Path
|
|
|
|
import bpy
|
|
import numpy as np
|
|
|
|
|
|
ASSET = Path(__file__).resolve().parents[1]
|
|
REFERENCES = ASSET.parents[2] / 'references/materials/ice/textures'
|
|
SIZE = 512
|
|
CRACK_PATTERNS = {
|
|
'top_albedo.png': (
|
|
([(0.0, .34), (.26, .42), (.40, .60), (.68, .54), (.92, .73)], .019),
|
|
([(.40, .60), (.44, .77), (.35, .94)], .011),
|
|
([(.68, .54), (.74, .32), (.89, .21)], .012),
|
|
),
|
|
'top_albedo_diagonal.png': (
|
|
([(.10, .86), (.28, .68), (.34, .49), (.57, .36), (.64, .16), (.79, .08)], .018),
|
|
([(.34, .49), (.21, .40), (.08, .39)], .011),
|
|
([(.57, .36), (.73, .48), (.92, .46)], .012),
|
|
),
|
|
'top_albedo_forked.png': (
|
|
([(.63, .04), (.57, .23), (.65, .39), (.49, .57), (.54, .76), (.44, .93)], .018),
|
|
([(.57, .23), (.39, .28), (.29, .18)], .011),
|
|
([(.65, .39), (.80, .40), (.92, .52)], .012),
|
|
([(.49, .57), (.30, .61), (.23, .79)], .010),
|
|
),
|
|
}
|
|
|
|
|
|
def mix(image, color, weight):
|
|
return image + (np.asarray(color) - image) * np.expand_dims(weight, -1)
|
|
|
|
|
|
def ramp(low, high, value):
|
|
t = np.clip((value - low) / (high - low), 0, 1)
|
|
return t * t * (3 - 2 * t)
|
|
|
|
|
|
def stroke(u, v, points, width):
|
|
distance = np.full_like(u, np.inf)
|
|
for (ax, ay), (bx, by) in zip(points, points[1:]):
|
|
dx, dy = bx - ax, by - ay
|
|
along = np.clip(((u - ax) * dx + (v - ay) * dy) / (dx * dx + dy * dy), 0, 1)
|
|
distance = np.minimum(distance, np.hypot(u - ax - along * dx, v - ay - along * dy))
|
|
return 1 - ramp(width * 0.48, width, distance)
|
|
|
|
|
|
def reference_tone(path):
|
|
resolution = 16
|
|
image = bpy.data.images.load(str(REFERENCES / path), check_existing=False)
|
|
image.scale(resolution, resolution)
|
|
pixels = np.empty(resolution * resolution * 4, dtype=np.float32)
|
|
image.pixels.foreach_get(pixels)
|
|
bpy.data.images.remove(image)
|
|
tone = pixels.reshape(resolution, resolution, 4)[:, :, :3].mean(axis=2)
|
|
tone = (tone - tone.mean()) / max(tone.std(), 0.001)
|
|
grid = np.arange(resolution)
|
|
coordinates = np.linspace(0, resolution - 1, SIZE)
|
|
rows = [np.interp(coordinates, grid, row) for row in tone]
|
|
return np.stack([np.interp(coordinates, grid, column) for column in np.asarray(rows).T], axis=1)
|
|
|
|
|
|
def save_color(name, color):
|
|
path = ASSET / 'textures' / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
# PNG scanlines run top-down, whereas UV V starts at the bottom.
|
|
rgb = np.clip(np.rint(color[::-1] * 255), 0, 255).astype(np.uint8)
|
|
scanlines = b''.join(b'\x00' + row.tobytes() for row in rgb)
|
|
|
|
def chunk(kind, payload):
|
|
return (struct.pack('!I', len(payload)) + kind + payload
|
|
+ struct.pack('!I', zlib.crc32(kind + payload)))
|
|
|
|
header = struct.pack('!IIBBBBB', SIZE, SIZE, 8, 2, 0, 0, 0)
|
|
path.write_bytes(b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', header)
|
|
+ chunk(b'IDAT', zlib.compress(scanlines, 9)) + chunk(b'IEND', b''))
|
|
|
|
|
|
def bake_color_maps():
|
|
samples = (np.arange(SIZE) + 0.5) / SIZE
|
|
u, v = np.meshgrid(samples, samples)
|
|
lake = reference_tone('ambientcg-ice003/Ice003_1K-JPG_Color.jpg')
|
|
frost = reference_tone('ambientcg-ice002/Ice002_1K-JPG_Color.jpg')
|
|
blue = reference_tone('oga-y2k/y2k_ice_texture.png')
|
|
|
|
top = mix((.44, .65, .75), (.54, .73, .81), ramp(-1.5, 1.6, lake))
|
|
facet = ramp(.42, .45, u * .55 + v * .82) * (1 - ramp(.76, .80, u * .55 + v * .82))
|
|
top = mix(top, (.67, .81, .87), facet * .33)
|
|
edge = np.minimum(np.minimum(u, 1 - u), np.minimum(v, 1 - v))
|
|
top = mix(top, (.86, .95, .94), (1 - ramp(.025, .14, edge + frost * .022)) * .73)
|
|
# Broken, angular highlight strokes hint at cracks just below the surface.
|
|
for filename, pattern in CRACK_PATTERNS.items():
|
|
painted_top = top
|
|
for points, width in pattern:
|
|
dark_lip = [(x, y - .019) for x, y in points]
|
|
painted_top = mix(painted_top, (.27, .55, .69), stroke(u, v, dark_lip, width) * .37)
|
|
painted_top = mix(painted_top, (.85, .94, .97), stroke(u, v, points, width) * .72)
|
|
save_color(filename, painted_top)
|
|
|
|
side = mix((.31, .49, .64), (.51, .69, .78), ramp(.05, 1.05, v + blue * .10))
|
|
icy_band = np.exp(-((v - (.82 + .05 * np.sin(7 * u))) / .064) ** 2)
|
|
side = mix(side, (.79, .92, .94), icy_band * .72)
|
|
streak = stroke(u, v, [(.07, .42), (.32, .59), (.58, .51), (.94, .67)], .019)
|
|
side = mix(side, (.74, .90, .94), streak * .64)
|
|
for x, y, size in [(.18, .65, .029), (.37, .31, .016), (.72, .42, .024), (.81, .25, .013)]:
|
|
bubble = np.hypot(u - x, (v - y) * .82) / size
|
|
edge = ramp(.49, .77, bubble) * (1 - ramp(.81, 1.1, bubble))
|
|
side = mix(side, (.77, .92, .95), edge * .62)
|
|
save_color('side_albedo.png', side)
|
|
|
|
|
|
def apply_ice_surface(obj, side, top):
|
|
uv = obj.data.uv_layers.new(name='Ice faces')
|
|
for polygon in obj.data.polygons:
|
|
for loop_index in polygon.loop_indices:
|
|
point = obj.data.vertices[obj.data.loops[loop_index].vertex_index].co
|
|
if abs(polygon.normal.z) > 0.5:
|
|
uv.uv[loop_index].vector = (point.x + 0.5, point.y + 0.5)
|
|
elif abs(polygon.normal.x) > 0.5:
|
|
uv.uv[loop_index].vector = (0.5 + point.y * polygon.normal.x, point.z)
|
|
else:
|
|
uv.uv[loop_index].vector = (0.5 - point.x * polygon.normal.y, point.z)
|
|
|
|
for material, file, roughness in ((side, 'side_albedo.png', .27), (top, 'top_albedo.png', .28)):
|
|
image = bpy.data.images.load(str(ASSET / 'textures' / file), check_existing=False)
|
|
image.colorspace_settings.name = 'sRGB'
|
|
image.pack()
|
|
texture = material.node_tree.nodes.new('ShaderNodeTexImage')
|
|
texture.image = image
|
|
texture.label = "Baked ice color"
|
|
texture.extension = 'EXTEND'
|
|
texture.location = (-420, 240)
|
|
shader = material.node_tree.nodes.get('Principled BSDF')
|
|
shader.inputs['Roughness'].default_value = roughness
|
|
material.node_tree.links.new(texture.outputs['Color'], shader.inputs['Base Color'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
bake_color_maps()
|