Modify world MJCF model to make it compatible with MJWarp's GPU batch rendering.
This may reduce texture and lighting realism.
Modification happens in place. Returns True if any modifications were made, False
otherwise.
Note for developers: Check if anything here can be dropped upon new MJWarp releases.
Source code in src/flygym/warp/rendering.py
| def modify_world_for_batch_rendering(world: BaseWorld) -> bool:
"""Modify world MJCF model to make it compatible with MJWarp's GPU batch rendering.
This may reduce texture and lighting realism.
Modification happens in place. Returns True if any modifications were made, False
otherwise.
Note for developers: Check if anything here can be dropped upon new MJWarp releases.
"""
is_modified = False
# Strip textures from fly body materials
# (rendering textures on complex meshes causes MJWarp memory corruption)
for material in world.mjcf_root.asset.find_all("material"):
if material.full_identifier.split("/")[0] not in world.fly_lookup:
continue # not a fly body material - leave it alone
if material.texture is None:
continue # material doesn't have texture - nothing to strip
texture_element = world.mjcf_root.asset.find(
"texture", material.texture.full_identifier
)
primary_color_rgb = texture_element.rgb1
material.texture = None
material.rgba[:3] = primary_color_rgb
is_modified = True
# Adjust scale of checker materials (e.g., ground): texrepeat needs to be scaled
# down by 1000x to get the same pattern - unclear why
for material in world.mjcf_root.asset.find_all("material"):
if material.texrepeat is not None:
material.texrepeat = tuple(tr / 1000 for tr in material.texrepeat)
is_modified = True
# Add light above each fly explicitly
for body in world.mjcf_root.find_all("body"):
if hasattr(body, "name") and body.name == "c_thorax":
warnings.warn(f"Adding overhead light for body {body.full_identifier}")
body.add(
"light",
name=body.full_identifier.replace("/", "-") + "-overheadlight",
mode="track",
target="c_thorax",
pos=(0, 0, 30),
dir=(0, 0, -1),
directional=True,
ambient=(10, 10, 10),
diffuse=(10, 10, 10),
specular=(0.3, 0.3, 0.3),
)
is_modified = True
return is_modified
|