@babylonjs-toolkit/agent 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/package.json +1 -1
- package/skills/bt-atlas/SKILL.md +22 -5
- package/skills/bt-design/references/3d-hero-docs.md +10 -3
- package/skills/bt-design/references/3d-hero-scroll.md +30 -5
- package/skills/bt-gauntlet/SKILL.md +4 -4
- package/skills/bt-gauntlet/references/spec-composition.md +4 -2
- package/skills/bt-gauntlet/references/target-images.md +28 -4
- package/skills/bt-gauntlet/references/unity-pipeline.md +30 -8
- package/skills/bt-hero/SKILL.md +3 -3
- package/skills/bt-landing/SKILL.md +1 -1
- package/skills/bt-prototype/SKILL.md +3 -3
- package/skills/bt-unwrap/SKILL.md +396 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/bt_unwrap.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/image_utils.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/mesh_tools.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/model_io.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/previews.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/rasterizer.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/reference_views.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/reference_warp.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/texture_bake.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/texture_compose.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/uv_layout.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/bt_unwrap.py +678 -0
- package/skills/bt-unwrap/scripts/blender/image_utils.py +264 -0
- package/skills/bt-unwrap/scripts/blender/mesh_tools.py +508 -0
- package/skills/bt-unwrap/scripts/blender/model_io.py +135 -0
- package/skills/bt-unwrap/scripts/blender/previews.py +205 -0
- package/skills/bt-unwrap/scripts/blender/rasterizer.py +217 -0
- package/skills/bt-unwrap/scripts/blender/reference_views.py +945 -0
- package/skills/bt-unwrap/scripts/blender/reference_warp.py +571 -0
- package/skills/bt-unwrap/scripts/blender/texture_bake.py +390 -0
- package/skills/bt-unwrap/scripts/blender/texture_compose.py +596 -0
- package/skills/bt-unwrap/scripts/blender/uv_layout.py +605 -0
- package/skills/bt-unwrap/scripts/bt-unwrap.mjs +165 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Image helpers for bt-unwrap: read and write images as numpy arrays through Blender, sample them, and
|
|
3
|
+
grow island colors into the padding around them.
|
|
4
|
+
|
|
5
|
+
Every array here is float32 RGBA in Blender's pixel order: row 0 is the BOTTOM of the image, so a texture
|
|
6
|
+
coordinate (u, v) maps to pixel (u * width, v * height) with no flip.
|
|
7
|
+
"""
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
import bpy
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
# Blender file formats by output extension.
|
|
14
|
+
FILE_FORMAT_BY_EXTENSION = {
|
|
15
|
+
".png": "PNG",
|
|
16
|
+
".jpg": "JPEG",
|
|
17
|
+
".jpeg": "JPEG",
|
|
18
|
+
".webp": "WEBP",
|
|
19
|
+
".tga": "TARGA",
|
|
20
|
+
".tif": "TIFF",
|
|
21
|
+
".tiff": "TIFF",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
# 8-neighbourhood offsets used by the padding dilation, as (row, column) shifts.
|
|
25
|
+
NEIGHBOUR_OFFSETS = ((-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def load_image_array(path: str) -> np.ndarray:
|
|
29
|
+
"""
|
|
30
|
+
Loads an image file as raw stored values, with no color management applied.
|
|
31
|
+
|
|
32
|
+
:param path: Any image format Blender reads (PNG, JPEG, WebP, TGA, EXR, ...).
|
|
33
|
+
:returns: float32 array of shape (height, width, 4), row 0 at the bottom of the image.
|
|
34
|
+
:raises RuntimeError: when the file cannot be read as an image.
|
|
35
|
+
"""
|
|
36
|
+
try:
|
|
37
|
+
image = bpy.data.images.load(path, check_existing=False)
|
|
38
|
+
except RuntimeError as error:
|
|
39
|
+
raise RuntimeError(f"cannot read image '{path}': {error}") from error
|
|
40
|
+
image.colorspace_settings.name = "Non-Color"
|
|
41
|
+
width, height = image.size
|
|
42
|
+
if width == 0 or height == 0:
|
|
43
|
+
bpy.data.images.remove(image)
|
|
44
|
+
raise RuntimeError(f"image '{path}' is empty")
|
|
45
|
+
pixels = np.empty(width * height * 4, dtype=np.float32)
|
|
46
|
+
image.pixels.foreach_get(pixels)
|
|
47
|
+
bpy.data.images.remove(image)
|
|
48
|
+
return pixels.reshape(height, width, 4)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def read_blender_image(image: bpy.types.Image) -> np.ndarray:
|
|
52
|
+
"""
|
|
53
|
+
Copies an image datablock that is already in memory (for example a fresh bake target) into numpy.
|
|
54
|
+
|
|
55
|
+
:param image: The Blender image to read.
|
|
56
|
+
:returns: float32 array of shape (height, width, 4), row 0 at the bottom.
|
|
57
|
+
"""
|
|
58
|
+
width, height = image.size
|
|
59
|
+
pixels = np.empty(width * height * 4, dtype=np.float32)
|
|
60
|
+
image.pixels.foreach_get(pixels)
|
|
61
|
+
return pixels.reshape(height, width, 4)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def save_image_array(array: np.ndarray, path: str, quality: int = 92) -> str:
|
|
65
|
+
"""
|
|
66
|
+
Writes an RGB or RGBA array to disk as an 8-bit image. Values are stored as they are, so pass sRGB
|
|
67
|
+
values for color maps and raw data for normal / metallic-roughness maps.
|
|
68
|
+
|
|
69
|
+
:param array: float array (height, width, 3 or 4) in the 0..1 range, row 0 at the bottom.
|
|
70
|
+
:param path: Output path; the extension picks the format (png, jpg, webp, tga, tif).
|
|
71
|
+
:param quality: JPEG / WebP quality, 0..100.
|
|
72
|
+
:returns: The path written.
|
|
73
|
+
:raises ValueError: when the extension is not a supported image format.
|
|
74
|
+
"""
|
|
75
|
+
extension = os.path.splitext(path)[1].lower()
|
|
76
|
+
file_format = FILE_FORMAT_BY_EXTENSION.get(extension)
|
|
77
|
+
if file_format is None:
|
|
78
|
+
raise ValueError(f"unsupported image extension '{extension}' for '{path}'")
|
|
79
|
+
height, width = array.shape[:2]
|
|
80
|
+
rgba = np.ones((height, width, 4), dtype=np.float32)
|
|
81
|
+
rgba[:, :, : min(4, array.shape[2])] = np.clip(array[:, :, :4], 0.0, 1.0)
|
|
82
|
+
image = bpy.data.images.new(os.path.basename(path), width, height, alpha=True, float_buffer=False)
|
|
83
|
+
image.colorspace_settings.name = "Non-Color"
|
|
84
|
+
image.pixels.foreach_set(rgba.ravel())
|
|
85
|
+
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
|
|
86
|
+
image.filepath_raw = path
|
|
87
|
+
image.file_format = file_format
|
|
88
|
+
image.save(quality=quality)
|
|
89
|
+
bpy.data.images.remove(image)
|
|
90
|
+
return path
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def sample_bilinear(image: np.ndarray, pixel_x: np.ndarray, pixel_y: np.ndarray) -> np.ndarray:
|
|
94
|
+
"""
|
|
95
|
+
Bilinearly samples an image at continuous pixel coordinates, clamping at the borders.
|
|
96
|
+
|
|
97
|
+
:param image: Array of shape (height, width, channels).
|
|
98
|
+
:param pixel_x: Horizontal coordinates in pixels; pixel centers sit at i + 0.5.
|
|
99
|
+
:param pixel_y: Vertical coordinates in pixels, measured from the bottom row.
|
|
100
|
+
:returns: Array of shape (count, channels).
|
|
101
|
+
"""
|
|
102
|
+
height, width = image.shape[:2]
|
|
103
|
+
x = np.clip(pixel_x - 0.5, 0.0, width - 1.0)
|
|
104
|
+
y = np.clip(pixel_y - 0.5, 0.0, height - 1.0)
|
|
105
|
+
left = np.floor(x).astype(np.int32)
|
|
106
|
+
bottom = np.floor(y).astype(np.int32)
|
|
107
|
+
right = np.minimum(left + 1, width - 1)
|
|
108
|
+
top = np.minimum(bottom + 1, height - 1)
|
|
109
|
+
blend_x = (x - left)[:, None]
|
|
110
|
+
blend_y = (y - bottom)[:, None]
|
|
111
|
+
lower_row = image[bottom, left] * (1.0 - blend_x) + image[bottom, right] * blend_x
|
|
112
|
+
upper_row = image[top, left] * (1.0 - blend_x) + image[top, right] * blend_x
|
|
113
|
+
return lower_row * (1.0 - blend_y) + upper_row * blend_y
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def sample_nearest(image: np.ndarray, pixel_x: np.ndarray, pixel_y: np.ndarray) -> np.ndarray:
|
|
117
|
+
"""
|
|
118
|
+
Samples the nearest pixel at continuous pixel coordinates, clamping at the borders.
|
|
119
|
+
|
|
120
|
+
:param image: Array of shape (height, width) or (height, width, channels).
|
|
121
|
+
:param pixel_x: Horizontal coordinates in pixels.
|
|
122
|
+
:param pixel_y: Vertical coordinates in pixels, measured from the bottom row.
|
|
123
|
+
:returns: The sampled values, one per coordinate.
|
|
124
|
+
"""
|
|
125
|
+
height, width = image.shape[:2]
|
|
126
|
+
column = np.clip(np.floor(pixel_x).astype(np.int64), 0, width - 1)
|
|
127
|
+
row = np.clip(np.floor(pixel_y).astype(np.int64), 0, height - 1)
|
|
128
|
+
return image[row, column]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def resize_image(image: np.ndarray, new_width: int, new_height: int) -> np.ndarray:
|
|
132
|
+
"""
|
|
133
|
+
Resizes an image. Shrinking by a whole factor averages pixel blocks so thin details survive; any
|
|
134
|
+
remaining change is bilinear.
|
|
135
|
+
|
|
136
|
+
:param image: Array of shape (height, width, channels) or (height, width).
|
|
137
|
+
:param new_width: Target width in pixels.
|
|
138
|
+
:param new_height: Target height in pixels.
|
|
139
|
+
:returns: The resized array, same channel layout and dtype float32.
|
|
140
|
+
"""
|
|
141
|
+
is_single_channel = image.ndim == 2
|
|
142
|
+
working = image[:, :, None] if is_single_channel else image
|
|
143
|
+
working = working.astype(np.float32, copy=False)
|
|
144
|
+
height, width = working.shape[:2]
|
|
145
|
+
block_factor = min(width // max(new_width, 1), height // max(new_height, 1))
|
|
146
|
+
if block_factor >= 2:
|
|
147
|
+
trimmed_height = (height // block_factor) * block_factor
|
|
148
|
+
trimmed_width = (width // block_factor) * block_factor
|
|
149
|
+
working = working[:trimmed_height, :trimmed_width]
|
|
150
|
+
working = working.reshape(
|
|
151
|
+
trimmed_height // block_factor, block_factor, trimmed_width // block_factor, block_factor, -1
|
|
152
|
+
).mean(axis=(1, 3))
|
|
153
|
+
height, width = working.shape[:2]
|
|
154
|
+
if (width, height) != (new_width, new_height):
|
|
155
|
+
grid_x, grid_y = np.meshgrid(
|
|
156
|
+
(np.arange(new_width) + 0.5) * width / new_width,
|
|
157
|
+
(np.arange(new_height) + 0.5) * height / new_height,
|
|
158
|
+
)
|
|
159
|
+
working = sample_bilinear(working, grid_x.ravel(), grid_y.ravel()).reshape(new_height, new_width, -1)
|
|
160
|
+
return working[:, :, 0] if is_single_channel else working
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def downsample_map(image: np.ndarray, size: int, is_normal_map: bool = False) -> np.ndarray:
|
|
164
|
+
"""
|
|
165
|
+
Filters a square texture down to its output size (block averaging, so thin lines and text stay clean).
|
|
166
|
+
Normal maps are averaged as vectors and re-normalised.
|
|
167
|
+
|
|
168
|
+
:param image: float32 (height, width, channels) or (height, width), values 0..1.
|
|
169
|
+
:param size: Output side length in pixels; the input is returned unchanged when already this size.
|
|
170
|
+
:param is_normal_map: Treat RGB as an encoded tangent-space normal.
|
|
171
|
+
:returns: float32 array (size, size[, channels]).
|
|
172
|
+
"""
|
|
173
|
+
if image.shape[0] == size and image.shape[1] == size:
|
|
174
|
+
return image
|
|
175
|
+
resized = resize_image(image, size, size)
|
|
176
|
+
if is_normal_map:
|
|
177
|
+
vectors = resized[:, :, :3] * 2.0 - 1.0
|
|
178
|
+
vectors /= np.maximum(np.linalg.norm(vectors, axis=2, keepdims=True), 1e-6)
|
|
179
|
+
resized[:, :, :3] = vectors * 0.5 + 0.5
|
|
180
|
+
return resized
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def erode_mask(mask: np.ndarray, iterations: int) -> np.ndarray:
|
|
184
|
+
"""
|
|
185
|
+
Shrinks a boolean mask by a number of pixels (4-neighbourhood).
|
|
186
|
+
|
|
187
|
+
:param mask: Boolean array (height, width).
|
|
188
|
+
:param iterations: How many pixels to remove from every edge.
|
|
189
|
+
:returns: The eroded mask.
|
|
190
|
+
"""
|
|
191
|
+
eroded = mask.copy()
|
|
192
|
+
for _ in range(max(0, iterations)):
|
|
193
|
+
shrunk = eroded.copy()
|
|
194
|
+
shrunk[1:, :] &= eroded[:-1, :]
|
|
195
|
+
shrunk[:-1, :] &= eroded[1:, :]
|
|
196
|
+
shrunk[:, 1:] &= eroded[:, :-1]
|
|
197
|
+
shrunk[:, :-1] &= eroded[:, 1:]
|
|
198
|
+
eroded = shrunk
|
|
199
|
+
return eroded
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def dilate_mask(mask: np.ndarray, iterations: int) -> np.ndarray:
|
|
203
|
+
"""
|
|
204
|
+
Grows a boolean mask by a number of pixels (4-neighbourhood).
|
|
205
|
+
|
|
206
|
+
:param mask: Boolean array (height, width).
|
|
207
|
+
:param iterations: How many pixels to add around every edge.
|
|
208
|
+
:returns: The dilated mask.
|
|
209
|
+
"""
|
|
210
|
+
return ~erode_mask(~mask, iterations)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def dilate_colors(colors: np.ndarray, covered: np.ndarray, iterations: int) -> np.ndarray:
|
|
214
|
+
"""
|
|
215
|
+
Pads texture islands: every uncovered pixel next to covered ones takes the average of its covered
|
|
216
|
+
neighbours, repeated outward. This stops mipmapping and bilinear filtering from pulling background
|
|
217
|
+
color into the edges of UV islands.
|
|
218
|
+
|
|
219
|
+
:param colors: float32 array (height, width, channels); modified copy is returned.
|
|
220
|
+
:param covered: Boolean array (height, width) of pixels that hold real surface color.
|
|
221
|
+
:param iterations: Padding width in pixels.
|
|
222
|
+
:returns: The padded color array.
|
|
223
|
+
"""
|
|
224
|
+
padded = colors.copy()
|
|
225
|
+
filled = covered.copy()
|
|
226
|
+
height, width = filled.shape
|
|
227
|
+
for _ in range(max(0, iterations)):
|
|
228
|
+
color_sum = np.zeros_like(padded)
|
|
229
|
+
neighbour_count = np.zeros((height, width), dtype=np.float32)
|
|
230
|
+
for row_shift, column_shift in NEIGHBOUR_OFFSETS:
|
|
231
|
+
source_rows = slice(max(0, -row_shift), height - max(0, row_shift))
|
|
232
|
+
target_rows = slice(max(0, row_shift), height - max(0, -row_shift))
|
|
233
|
+
source_columns = slice(max(0, -column_shift), width - max(0, column_shift))
|
|
234
|
+
target_columns = slice(max(0, column_shift), width - max(0, -column_shift))
|
|
235
|
+
neighbour_filled = filled[source_rows, source_columns]
|
|
236
|
+
color_sum[target_rows, target_columns] += padded[source_rows, source_columns] * neighbour_filled[:, :, None]
|
|
237
|
+
neighbour_count[target_rows, target_columns] += neighbour_filled
|
|
238
|
+
frontier = (~filled) & (neighbour_count > 0)
|
|
239
|
+
if not frontier.any():
|
|
240
|
+
break
|
|
241
|
+
padded[frontier] = color_sum[frontier] / neighbour_count[frontier][:, None]
|
|
242
|
+
filled |= frontier
|
|
243
|
+
return padded
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def srgb_to_linear(values: np.ndarray) -> np.ndarray:
|
|
247
|
+
"""
|
|
248
|
+
Converts sRGB-encoded values to linear light.
|
|
249
|
+
|
|
250
|
+
:param values: Array of sRGB values in 0..1.
|
|
251
|
+
:returns: Linear values in 0..1.
|
|
252
|
+
"""
|
|
253
|
+
return np.where(values <= 0.04045, values / 12.92, ((values + 0.055) / 1.055) ** 2.4)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def linear_to_srgb(values: np.ndarray) -> np.ndarray:
|
|
257
|
+
"""
|
|
258
|
+
Converts linear-light values to sRGB encoding.
|
|
259
|
+
|
|
260
|
+
:param values: Array of linear values in 0..1.
|
|
261
|
+
:returns: sRGB values in 0..1.
|
|
262
|
+
"""
|
|
263
|
+
clipped = np.clip(values, 0.0, 1.0)
|
|
264
|
+
return np.where(clipped <= 0.0031308, clipped * 12.92, 1.055 * np.power(clipped, 1.0 / 2.4) - 0.055)
|