@babylonjs-toolkit/agent 1.2.16 → 1.2.18
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-blender/SKILL.md +386 -0
- package/skills/bt-blender/THIRD_PARTY_NOTICES.md +141 -0
- package/skills/bt-blender/references/art-style-and-genre.md +173 -0
- package/skills/bt-blender/references/characters-and-creatures.md +644 -0
- package/skills/bt-blender/references/environments.md +478 -0
- package/skills/bt-blender/references/game-engine-export.md +284 -0
- package/skills/bt-blender/references/geometry-nodes.md +447 -0
- package/skills/bt-blender/references/hard-surface.md +720 -0
- package/skills/bt-blender/references/procedural-generation.md +358 -0
- package/skills/bt-blender/references/rigging-and-animation.md +1080 -0
- package/skills/bt-blender/references/sculpting-and-retopology.md +544 -0
- package/skills/bt-blender/references/uv-and-pbr-materials.md +381 -0
- package/skills/bt-blender/references/vehicles.md +632 -0
- package/skills/bt-blender/scripts/blender/assembly_check.py +420 -0
- package/skills/bt-blender/scripts/blender/character_blockout.py +462 -0
- package/skills/bt-blender/scripts/blender/construction_kit.py +1710 -0
- package/skills/bt-blender/scripts/blender/environment_kit.py +525 -0
- package/skills/bt-blender/scripts/blender/export_asset.py +623 -0
- package/skills/bt-blender/scripts/blender/geometry_nodes_builders.py +575 -0
- package/skills/bt-blender/scripts/blender/hard_surface_panels.py +374 -0
- package/skills/bt-blender/scripts/blender/hard_surface_trims.py +200 -0
- package/skills/bt-blender/scripts/blender/image_measure.py +568 -0
- package/skills/bt-blender/scripts/blender/materials_pbr.py +439 -0
- package/skills/bt-blender/scripts/blender/measure_stations.py +486 -0
- package/skills/bt-blender/scripts/blender/ortho_views.py +309 -0
- package/skills/bt-blender/scripts/blender/procedural_game_ready.py +258 -0
- package/skills/bt-blender/scripts/blender/procedural_generators.py +842 -0
- package/skills/bt-blender/scripts/blender/qa_check.py +729 -0
- package/skills/bt-blender/scripts/blender/retopology_tools.py +781 -0
- package/skills/bt-blender/scripts/blender/review_render.py +358 -0
- package/skills/bt-blender/scripts/blender/rigging_tools.py +1170 -0
- package/skills/bt-blender/scripts/blender/style_palette.py +190 -0
- package/skills/bt-blender/scripts/blender/tool_common.py +289 -0
- package/skills/bt-blender/scripts/blender/uv_layout.py +555 -0
- package/skills/bt-blender/scripts/blender/vehicle_parts.py +370 -0
- package/skills/bt-blender/scripts/blender/view_consistency.py +133 -0
- package/skills/bt-blender/scripts/blender/world_gate.py +219 -0
- package/skills/bt-blender/scripts/bt-blender.mjs +329 -0
- package/skills/bt-blender/scripts/validate_gltf.mjs +204 -0
- package/skills/bt-gauntlet/SKILL.md +6 -6
- package/skills/bt-gauntlet/references/critic-rubrics.md +71 -4
- package/skills/bt-gauntlet/references/spec-composition.md +13 -0
- package/skills/bt-gauntlet/references/unity-pipeline.md +18 -2
- 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
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
"""
|
|
2
|
+
review_render -- see the asset the way the player will, and read its form at a glance.
|
|
3
|
+
|
|
4
|
+
1. A gameplay render: a perspective camera at the in-game distance and field of view, cropped to the
|
|
5
|
+
asset, so the image is exactly as many pixels tall as the asset is on screen. Detail that does not
|
|
6
|
+
survive at that size is wasted triangles; a silhouette that reads wrong at that size is wrong. A
|
|
7
|
+
4x nearest-neighbour enlargement is written beside it for reading.
|
|
8
|
+
2. A contact sheet of fixed views (Workbench, transparent background) in three modes:
|
|
9
|
+
clay (studio light: shading continuity, faceting, chines), parts (a random colour per object:
|
|
10
|
+
detached or wrongly split parts stand out) and wire (the mesh edges over clay: topology flow, poles,
|
|
11
|
+
density).
|
|
12
|
+
|
|
13
|
+
blender --background --factory-startup --python-exit-code 1 --python review_render.py -- \
|
|
14
|
+
--model model.blend --out review/ --distance 6 --fov 60 --screen-height 1080
|
|
15
|
+
[--pixel-height 140] [--azimuth 35 --elevation 15] [--views front,right,top,front_right]
|
|
16
|
+
[--modes clay,parts,wire] [--tile 512]
|
|
17
|
+
|
|
18
|
+
Azimuth is measured from the front (camera at -Y) towards +X: 0 front, 90 right, 180 back, 270 left.
|
|
19
|
+
Ideas credit: the gameplay-pixel-height render comes from majidmanzarpour/blender-game-skills
|
|
20
|
+
(review_render.py --gameplay-px); the random-colour parts sheet and the wireframe-over-solid overlay from
|
|
21
|
+
EL4CTEO/blender-skills (review_shots.py). Both re-expressed; no code copied.
|
|
22
|
+
"""
|
|
23
|
+
import math
|
|
24
|
+
import os
|
|
25
|
+
import sys
|
|
26
|
+
|
|
27
|
+
sys.dont_write_bytecode = True
|
|
28
|
+
SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
|
|
29
|
+
if SCRIPT_DIRECTORY not in sys.path:
|
|
30
|
+
sys.path.insert(0, SCRIPT_DIRECTORY)
|
|
31
|
+
|
|
32
|
+
import bpy # noqa: E402
|
|
33
|
+
import numpy as np # noqa: E402
|
|
34
|
+
from bpy_extras.object_utils import world_to_camera_view # noqa: E402
|
|
35
|
+
from mathutils import Vector # noqa: E402
|
|
36
|
+
|
|
37
|
+
import construction_kit # noqa: E402
|
|
38
|
+
import ortho_views # noqa: E402
|
|
39
|
+
import tool_common # noqa: E402
|
|
40
|
+
|
|
41
|
+
TOOL_NAME = "review-render"
|
|
42
|
+
|
|
43
|
+
# Named review views: (azimuth degrees from the front towards +X, elevation degrees, orthographic?).
|
|
44
|
+
REVIEW_VIEWS = {
|
|
45
|
+
"front": (0.0, 0.0, True), "right": (90.0, 0.0, True), "back": (180.0, 0.0, True), "left": (270.0, 0.0, True),
|
|
46
|
+
"top": (0.0, 90.0, True), "front_right": (45.0, 20.0, False), "front_left": (315.0, 20.0, False),
|
|
47
|
+
"back_right": (135.0, 20.0, False), "back_left": (225.0, 20.0, False),
|
|
48
|
+
}
|
|
49
|
+
DEFAULT_VIEWS = "front,right,back,top,front_right,back_left"
|
|
50
|
+
MODES = ("clay", "parts", "wire")
|
|
51
|
+
|
|
52
|
+
# Lens of the perspective sheet tiles (mm on a 36 mm sensor) and the margin around the subject.
|
|
53
|
+
SHEET_LENS_MM = 50.0
|
|
54
|
+
FRAME_MARGIN = 1.12
|
|
55
|
+
|
|
56
|
+
# Clay and wire colours.
|
|
57
|
+
CLAY_COLOR = (0.62, 0.62, 0.62, 1.0)
|
|
58
|
+
WIRE_COLOR = (0.02, 0.02, 0.02, 1.0)
|
|
59
|
+
|
|
60
|
+
# Wire thickness in tile pixels.
|
|
61
|
+
WIRE_PIXELS = 0.9
|
|
62
|
+
|
|
63
|
+
# Gameplay crops keep this many pixels around the asset, and the enlargement uses this factor.
|
|
64
|
+
GAMEPLAY_PADDING_PIXELS = 4
|
|
65
|
+
GAMEPLAY_ENLARGEMENT = 4
|
|
66
|
+
|
|
67
|
+
# Gameplay defaults: vertical field of view (degrees) when only --pixel-height is given, the screen's
|
|
68
|
+
# width / height, and the proportional corrections used to hit a requested pixel height.
|
|
69
|
+
DEFAULT_GAMEPLAY_FOV = 60.0
|
|
70
|
+
SCREEN_ASPECT = 16.0 / 9.0
|
|
71
|
+
GAMEPLAY_FIT_STEPS = 4
|
|
72
|
+
|
|
73
|
+
# Background behind transparent tiles in the sheet.
|
|
74
|
+
SHEET_BACKGROUND = (1.0, 1.0, 1.0)
|
|
75
|
+
|
|
76
|
+
# Defaults: sheet tile size and game screen height, in pixels.
|
|
77
|
+
DEFAULT_TILE_PIXELS = 512
|
|
78
|
+
DEFAULT_SCREEN_HEIGHT = 1080
|
|
79
|
+
|
|
80
|
+
# A view direction this close to vertical (|forward.z|) uses world Y as its up vector.
|
|
81
|
+
VERTICAL_VIEW_DOT = 0.999
|
|
82
|
+
|
|
83
|
+
# Nearest clip distance of the review cameras, metres.
|
|
84
|
+
MINIMUM_CLIP_START = 0.001
|
|
85
|
+
|
|
86
|
+
# Sheet camera sensor width (Blender's default), millimetres.
|
|
87
|
+
SENSOR_WIDTH_MM = 36.0
|
|
88
|
+
|
|
89
|
+
# Gameplay camera clipping: near plane in metres, far plane at this many times the distance plus a margin.
|
|
90
|
+
GAMEPLAY_CLIP_START = 0.01
|
|
91
|
+
GAMEPLAY_CLIP_FACTOR = 4.0
|
|
92
|
+
GAMEPLAY_CLIP_MARGIN = 10.0
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def build_parser() -> tool_common.ToolArgumentParser:
|
|
96
|
+
"""The tool's command-line options."""
|
|
97
|
+
parser = tool_common.ToolArgumentParser(prog=TOOL_NAME, description=__doc__.strip().splitlines()[0])
|
|
98
|
+
parser.add_argument("--model", help="model (.blend .glb .gltf .fbx .obj)")
|
|
99
|
+
parser.add_argument("--objects", nargs="*", help="objects to review (default every visible mesh)")
|
|
100
|
+
parser.add_argument("--out", required=True, help="output folder")
|
|
101
|
+
parser.add_argument("--views", default=DEFAULT_VIEWS, help=f"comma list from {', '.join(REVIEW_VIEWS)}")
|
|
102
|
+
parser.add_argument("--modes", default=",".join(MODES), help="comma list from clay, parts, wire")
|
|
103
|
+
parser.add_argument("--tile", type=int, default=DEFAULT_TILE_PIXELS, help="sheet tile size in pixels")
|
|
104
|
+
parser.add_argument("--distance", type=float, help="in-game camera distance to the asset centre, metres")
|
|
105
|
+
parser.add_argument("--fov", type=float, help="in-game vertical field of view, degrees")
|
|
106
|
+
parser.add_argument("--screen-height", type=int, default=DEFAULT_SCREEN_HEIGHT, help="game screen height in pixels")
|
|
107
|
+
parser.add_argument("--pixel-height", type=float,
|
|
108
|
+
help="instead of distance / fov: the asset's on-screen height in pixels")
|
|
109
|
+
parser.add_argument("--azimuth", type=float, default=35.0, help="gameplay camera azimuth, degrees")
|
|
110
|
+
parser.add_argument("--elevation", type=float, default=15.0, help="gameplay camera elevation, degrees")
|
|
111
|
+
return parser
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def view_direction(azimuth_degrees: float, elevation_degrees: float) -> np.ndarray:
|
|
115
|
+
"""
|
|
116
|
+
Unit vector from the subject towards the camera.
|
|
117
|
+
|
|
118
|
+
:param azimuth_degrees: 0 front (-Y), 90 right (+X).
|
|
119
|
+
:param elevation_degrees: Above the horizon.
|
|
120
|
+
:returns: (3,) direction.
|
|
121
|
+
"""
|
|
122
|
+
azimuth, elevation = math.radians(azimuth_degrees), math.radians(elevation_degrees)
|
|
123
|
+
return np.array([math.sin(azimuth) * math.cos(elevation), -math.cos(azimuth) * math.cos(elevation),
|
|
124
|
+
math.sin(elevation)])
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _camera_basis(toward_camera: np.ndarray) -> ortho_views.ViewBasis:
|
|
128
|
+
"""A camera basis looking against `toward_camera` with world Z as up (Y for straight-down views)."""
|
|
129
|
+
forward = -toward_camera
|
|
130
|
+
world_up = np.array([0.0, 1.0, 0.0]) if abs(forward[2]) > VERTICAL_VIEW_DOT else np.array([0.0, 0.0, 1.0])
|
|
131
|
+
right = np.cross(forward, world_up)
|
|
132
|
+
right /= np.linalg.norm(right)
|
|
133
|
+
return ortho_views.ViewBasis(forward=forward, right=right, up=np.cross(right, forward))
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _bounding_sphere(objects: list) -> tuple:
|
|
137
|
+
"""Centre of the world bounding box and the radius of the sphere around it, from world vertices."""
|
|
138
|
+
points = np.concatenate([construction_kit.world_vertices(obj) for obj in objects])
|
|
139
|
+
center = (points.min(axis=0) + points.max(axis=0)) / 2.0
|
|
140
|
+
return center, float(np.linalg.norm(points - center, axis=1).max())
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _configure_workbench(mode: str, width: int, height: int) -> None:
|
|
144
|
+
"""Workbench settings for a sheet mode."""
|
|
145
|
+
scene = bpy.context.scene
|
|
146
|
+
scene.render.engine = "BLENDER_WORKBENCH"
|
|
147
|
+
scene.render.film_transparent = True
|
|
148
|
+
scene.render.image_settings.file_format = "PNG"
|
|
149
|
+
scene.render.image_settings.color_mode = "RGBA"
|
|
150
|
+
scene.render.resolution_x, scene.render.resolution_y = width, height
|
|
151
|
+
scene.render.resolution_percentage = 100
|
|
152
|
+
scene.view_settings.view_transform = "Standard"
|
|
153
|
+
shading = scene.display.shading
|
|
154
|
+
shading.light = "STUDIO"
|
|
155
|
+
shading.show_cavity = False
|
|
156
|
+
shading.show_shadows = False
|
|
157
|
+
shading.show_object_outline = False
|
|
158
|
+
shading.color_type = {"clay": "SINGLE", "parts": "RANDOM", "wire": "OBJECT"}[mode]
|
|
159
|
+
shading.single_color = CLAY_COLOR[:3]
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _render(path: str) -> np.ndarray:
|
|
163
|
+
"""Renders the scene camera to a file and reads it back (row 0 at the top)."""
|
|
164
|
+
bpy.context.scene.render.filepath = os.path.abspath(path)
|
|
165
|
+
outcome = bpy.ops.render.render(write_still=True)
|
|
166
|
+
if "FINISHED" not in outcome:
|
|
167
|
+
raise RuntimeError(f"render did not finish: {outcome}")
|
|
168
|
+
return tool_common.load_image(path)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _wire_overlays(objects: list, thickness: float) -> list:
|
|
172
|
+
"""Linked copies of each object carrying a black wireframe, laid over the clay originals."""
|
|
173
|
+
overlays = []
|
|
174
|
+
for obj in objects:
|
|
175
|
+
obj.color = CLAY_COLOR
|
|
176
|
+
overlay = obj.copy()
|
|
177
|
+
overlay.name = f"_wire_{obj.name}"
|
|
178
|
+
overlay.color = WIRE_COLOR
|
|
179
|
+
bpy.context.scene.collection.objects.link(overlay)
|
|
180
|
+
wire = overlay.modifiers.new("_ReviewWire", "WIREFRAME")
|
|
181
|
+
wire.thickness = thickness
|
|
182
|
+
wire.use_replace = True
|
|
183
|
+
# Even offset divides by the cosine of the angle between a vertex normal and its faces. On a knife
|
|
184
|
+
# edge or a short-edged rim that angle is near 90 degrees, and the wire shoots out as a spike up to
|
|
185
|
+
# metres long. Plain offset keeps every wire within its thickness of the surface.
|
|
186
|
+
wire.use_even_offset = False
|
|
187
|
+
overlays.append(overlay)
|
|
188
|
+
return overlays
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _sheet_tile(objects: list, view: str, mode: str, center: np.ndarray, radius: float, tile: int, path: str) -> np.ndarray:
|
|
192
|
+
"""Renders one sheet tile framing the bounding sphere."""
|
|
193
|
+
azimuth, elevation, orthographic = REVIEW_VIEWS[view]
|
|
194
|
+
basis = _camera_basis(view_direction(azimuth, elevation))
|
|
195
|
+
camera = ortho_views.view_camera()
|
|
196
|
+
camera.data.sensor_fit = "HORIZONTAL"
|
|
197
|
+
camera.data.sensor_width = SENSOR_WIDTH_MM
|
|
198
|
+
if orthographic:
|
|
199
|
+
camera.data.type = "ORTHO"
|
|
200
|
+
camera.data.ortho_scale = 2.0 * radius * FRAME_MARGIN
|
|
201
|
+
distance = radius * 4.0 + 1.0
|
|
202
|
+
else:
|
|
203
|
+
camera.data.type = "PERSP"
|
|
204
|
+
camera.data.lens = SHEET_LENS_MM
|
|
205
|
+
half_angle = math.atan(SENSOR_WIDTH_MM / 2.0 / SHEET_LENS_MM)
|
|
206
|
+
distance = radius * FRAME_MARGIN / math.sin(half_angle)
|
|
207
|
+
camera.data.clip_start = max(MINIMUM_CLIP_START, distance - radius * 2.0)
|
|
208
|
+
camera.data.clip_end = distance + radius * 2.0
|
|
209
|
+
ortho_views.place_camera(camera, basis, center, distance)
|
|
210
|
+
_configure_workbench(mode, tile, tile)
|
|
211
|
+
shown = list(objects)
|
|
212
|
+
overlays = []
|
|
213
|
+
if mode == "wire":
|
|
214
|
+
pixel_size = 2.0 * radius * FRAME_MARGIN / tile
|
|
215
|
+
overlays = _wire_overlays(objects, pixel_size * WIRE_PIXELS)
|
|
216
|
+
shown += overlays
|
|
217
|
+
try:
|
|
218
|
+
with ortho_views.render_only(shown):
|
|
219
|
+
return _render(path)
|
|
220
|
+
finally:
|
|
221
|
+
for overlay in overlays:
|
|
222
|
+
bpy.data.objects.remove(overlay, do_unlink=True)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def contact_sheet(objects: list, views: list, modes: list, tile: int, out_directory: str) -> dict:
|
|
226
|
+
"""
|
|
227
|
+
Renders every view in every mode and lays them out as one sheet: one row per mode.
|
|
228
|
+
|
|
229
|
+
:param objects: Objects to show.
|
|
230
|
+
:param views: Review view names.
|
|
231
|
+
:param modes: Sheet modes.
|
|
232
|
+
:param tile: Tile size in pixels.
|
|
233
|
+
:param out_directory: Output folder.
|
|
234
|
+
:returns: {"sheet": path, "rows": modes, "columns": views}.
|
|
235
|
+
"""
|
|
236
|
+
center, radius = _bounding_sphere(objects)
|
|
237
|
+
rows = []
|
|
238
|
+
for mode in modes:
|
|
239
|
+
tiles = []
|
|
240
|
+
for view in views:
|
|
241
|
+
rgba = _sheet_tile(objects, view, mode, center, radius, tile,
|
|
242
|
+
os.path.join(out_directory, "tiles", f"{mode}_{view}.png"))
|
|
243
|
+
tiles.append(rgba[:, :, :3] * rgba[:, :, 3:4] + np.array(SHEET_BACKGROUND) * (1.0 - rgba[:, :, 3:4]))
|
|
244
|
+
rows.append(np.concatenate(tiles, axis=1))
|
|
245
|
+
sheet = tool_common.save_image(os.path.join(out_directory, "review_sheet.png"), np.concatenate(rows, axis=0))
|
|
246
|
+
return {"sheet": sheet, "rows": modes, "columns": views}
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _projected_bounds(camera: bpy.types.Object, points: np.ndarray) -> tuple:
|
|
250
|
+
"""Normalised (0..1) screen bounds of world points seen by a camera: (left, bottom, right, top)."""
|
|
251
|
+
scene = bpy.context.scene
|
|
252
|
+
projected = np.array([tuple(world_to_camera_view(scene, camera, Vector(point)))[:2] for point in points])
|
|
253
|
+
return (*projected.min(axis=0), *projected.max(axis=0))
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _place_gameplay_camera(camera: bpy.types.Object, basis: ortho_views.ViewBasis, center: np.ndarray,
|
|
257
|
+
distance: float) -> None:
|
|
258
|
+
"""Moves the gameplay camera to a distance and refreshes the scene so projections see it."""
|
|
259
|
+
camera.data.clip_end = distance * GAMEPLAY_CLIP_FACTOR + GAMEPLAY_CLIP_MARGIN
|
|
260
|
+
ortho_views.place_camera(camera, basis, center, distance)
|
|
261
|
+
bpy.context.view_layer.update()
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _fit_distance(camera: bpy.types.Object, basis: ortho_views.ViewBasis, center: np.ndarray, points: np.ndarray,
|
|
265
|
+
pixel_height: float, screen_height: int, start_distance: float) -> float:
|
|
266
|
+
"""
|
|
267
|
+
Finds the camera distance at which the asset's projected height is `pixel_height`. On-screen size
|
|
268
|
+
falls roughly as 1 / distance, so a few proportional corrections converge.
|
|
269
|
+
"""
|
|
270
|
+
distance = start_distance
|
|
271
|
+
for _ in range(GAMEPLAY_FIT_STEPS):
|
|
272
|
+
_place_gameplay_camera(camera, basis, center, distance)
|
|
273
|
+
_, bottom, _, top = _projected_bounds(camera, points)
|
|
274
|
+
distance *= (top - bottom) * screen_height / pixel_height
|
|
275
|
+
_place_gameplay_camera(camera, basis, center, distance)
|
|
276
|
+
return distance
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def gameplay_render(objects: list, options, out_directory: str) -> dict:
|
|
280
|
+
"""
|
|
281
|
+
Renders the asset at its in-game size: a perspective camera at the game distance and vertical field
|
|
282
|
+
of view, cropped to the asset, so the image is as many pixels tall as the asset is on screen. With
|
|
283
|
+
--pixel-height the camera distance is solved to give exactly that on-screen height.
|
|
284
|
+
|
|
285
|
+
:param objects: Objects to show.
|
|
286
|
+
:param options: Parsed options (distance, fov, screen_height, pixel_height, azimuth, elevation).
|
|
287
|
+
:param out_directory: Output folder.
|
|
288
|
+
:returns: Paths, the camera distance, and the projected and rendered on-screen pixel heights.
|
|
289
|
+
"""
|
|
290
|
+
points = np.concatenate([construction_kit.world_vertices(obj) for obj in objects])
|
|
291
|
+
center = (points.min(axis=0) + points.max(axis=0)) / 2.0
|
|
292
|
+
fov_degrees = options.fov or DEFAULT_GAMEPLAY_FOV
|
|
293
|
+
basis = _camera_basis(view_direction(options.azimuth, options.elevation))
|
|
294
|
+
camera = ortho_views.view_camera()
|
|
295
|
+
camera.data.type = "PERSP"
|
|
296
|
+
camera.data.sensor_fit = "VERTICAL"
|
|
297
|
+
camera.data.angle = math.radians(fov_degrees)
|
|
298
|
+
camera.data.clip_start = GAMEPLAY_CLIP_START
|
|
299
|
+
screen_width = int(round(options.screen_height * SCREEN_ASPECT))
|
|
300
|
+
_configure_workbench("clay", screen_width, options.screen_height)
|
|
301
|
+
if options.pixel_height:
|
|
302
|
+
extent = float(np.linalg.norm(points.max(axis=0) - points.min(axis=0)))
|
|
303
|
+
start = extent * options.screen_height / (2.0 * math.tan(math.radians(fov_degrees) / 2.0) * options.pixel_height)
|
|
304
|
+
distance = _fit_distance(camera, basis, center, points, options.pixel_height, options.screen_height, start)
|
|
305
|
+
else:
|
|
306
|
+
distance = options.distance
|
|
307
|
+
_place_gameplay_camera(camera, basis, center, distance)
|
|
308
|
+
left, bottom, right, top = _projected_bounds(camera, points)
|
|
309
|
+
projected_pixels = (top - bottom) * options.screen_height
|
|
310
|
+
padding_x = GAMEPLAY_PADDING_PIXELS / screen_width
|
|
311
|
+
padding_y = GAMEPLAY_PADDING_PIXELS / options.screen_height
|
|
312
|
+
render = bpy.context.scene.render
|
|
313
|
+
render.use_border = True
|
|
314
|
+
render.use_crop_to_border = True
|
|
315
|
+
render.border_min_x, render.border_max_x = max(0.0, left - padding_x), min(1.0, right + padding_x)
|
|
316
|
+
render.border_min_y, render.border_max_y = max(0.0, bottom - padding_y), min(1.0, top + padding_y)
|
|
317
|
+
base = os.path.join(out_directory, f"gameplay_{int(round(projected_pixels))}px")
|
|
318
|
+
try:
|
|
319
|
+
with ortho_views.render_only(objects):
|
|
320
|
+
rgba = _render(f"{base}.png")
|
|
321
|
+
finally:
|
|
322
|
+
render.use_border = False
|
|
323
|
+
rows = np.flatnonzero((rgba[:, :, 3] > 0.5).any(axis=1))
|
|
324
|
+
rendered_pixels = int(rows[-1] - rows[0] + 1) if rows.size else 0
|
|
325
|
+
enlarged = np.repeat(np.repeat(rgba, GAMEPLAY_ENLARGEMENT, axis=0), GAMEPLAY_ENLARGEMENT, axis=1)
|
|
326
|
+
flat = enlarged[:, :, :3] * enlarged[:, :, 3:4] + np.array(SHEET_BACKGROUND) * (1.0 - enlarged[:, :, 3:4])
|
|
327
|
+
return {"image": f"{base}.png", "enlarged": tool_common.save_image(f"{base}_x{GAMEPLAY_ENLARGEMENT}.png", flat),
|
|
328
|
+
"distance_m": round(distance, 3), "fov_degrees": fov_degrees, "screen_height": options.screen_height,
|
|
329
|
+
"azimuth_degrees": options.azimuth, "elevation_degrees": options.elevation,
|
|
330
|
+
"projected_pixel_height": round(projected_pixels, 1), "rendered_pixel_height": rendered_pixels}
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def run(options) -> dict:
|
|
334
|
+
"""
|
|
335
|
+
Renders the contact sheet and, when a distance or pixel height is given, the gameplay render.
|
|
336
|
+
|
|
337
|
+
:param options: Parsed options.
|
|
338
|
+
:returns: Paths and numbers of everything written.
|
|
339
|
+
"""
|
|
340
|
+
views = [view for view in options.views.split(",") if view]
|
|
341
|
+
modes = [mode for mode in options.modes.split(",") if mode]
|
|
342
|
+
unknown = [view for view in views if view not in REVIEW_VIEWS] + [mode for mode in modes if mode not in MODES]
|
|
343
|
+
if unknown:
|
|
344
|
+
raise tool_common.OptionsError(f"unknown views or modes: {unknown}")
|
|
345
|
+
wants_gameplay = options.pixel_height is not None or options.distance is not None
|
|
346
|
+
if options.distance is not None and options.fov is None:
|
|
347
|
+
raise tool_common.OptionsError("--distance needs --fov")
|
|
348
|
+
tool_common.load_model(options.model)
|
|
349
|
+
objects = tool_common.resolve_objects(options.objects)
|
|
350
|
+
out_directory = os.path.abspath(options.out)
|
|
351
|
+
result = {"contact_sheet": contact_sheet(objects, views, modes, options.tile, out_directory)}
|
|
352
|
+
if wants_gameplay:
|
|
353
|
+
result["gameplay"] = gameplay_render(objects, options, out_directory)
|
|
354
|
+
return result
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
if __name__ == "__main__":
|
|
358
|
+
tool_common.run_tool(TOOL_NAME, build_parser(), run)
|