warp.render#

warp.render 模块提供了一组渲染器,可用于可视化涉及各种类型形状的场景。

在这些独立渲染器之上,warp.sim.render 模块提供了可用于直接从 warp.sim.ModelBuilder 对象可视化场景并从 warp.sim.State 对象更新它们的渲染器。

独立渲染器#

OpenGLRenderer 提供了一个交互式渲染器,可以实时回放动画,主要用于调试;而更复杂的渲染则可以通过 UsdRenderer 实现,它允许将场景导出到 USD 文件,然后可以在您选择的外部 3D 应用程序或渲染器中进行渲染。

class warp.render.UsdRenderer(stage, up_axis='Y', fps=60, scaling=1.0)[source]#

一个 USD 渲染器

__init__(stage, up_axis='Y', fps=60, scaling=1.0)[source]#

构造一个 UsdRenderer 对象

参数:
  • model – 一个仿真模型

  • stage (str/Usd.Stage) – 一个 USD 阶段(内存中或磁盘上)

  • up_axis (str) – 阶段的向上轴

  • fps – 在 USD 文件中使用的每秒帧数

  • scaling – 用于场景中实体的缩放因子

begin_frame(time)[source]#
end_frame()[source]#
register_body(body_name)[source]#
add_shape_instance(
name,
shape,
body,
pos,
rot,
scale=(1.0, 1.0, 1.0),
color=(1.0, 1.0, 1.0),
custom_index=-1,
visible=True,
)[source]#
参数:
  • name (str) – 名称

  • pos (tuple) – 位置

  • rot (tuple) – 旋转

  • scale (tuple) – 缩放

  • color (tuple) – 颜色

  • custom_index (int) – 自定义索引

  • visible (bool) – 可见性

render_plane(
name,
pos,
rot,
name,
pos,
rot,
width,
length,
color=None

parent_body=None

参数:
  • is_template=False

  • )[source]#

  • 使用给定尺寸渲染一个平面。

  • name (str) – 平面的名称

  • pos (tuple) – 平面的位置

  • rot (tuple) – 平面的旋转

  • width (float) – 平面的宽度

  • length (float) – 平面的长度

color (tuple) – 平面的颜色
参数:

parent_body (str) – 父物体的名称

is_template (bool) – 平面是否为模板
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
width,
length,
rot,
size (float) – 尺寸

render_sphere(

参数:
  • name

  • pos

  • radius

  • color=None

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

)[source]#
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
用于可视化的调试辅助工具,添加一个球体,
width,
length,
rot,
pos (tuple) – 球体的位置

radius (float) – 球体的半径

参数:
  • name (str) – 在阶段上的 USD prim 的名称

  • color (tuple) – 球体的颜色

  • parent_body (str) – 父物体

  • radius

  • is_template (bool) – 是否为模板

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

render_capsule(
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
用于可视化的调试辅助工具,添加一个球体,
width,
length,
rot,
name

pos

参数:
  • radius

  • half_height

  • color=None

  • radius

  • )[source]#

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

用于可视化的调试辅助工具,添加一个胶囊体
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
用于可视化的调试辅助工具,添加一个球体,
width,
length,
rot,
pos (tuple) – 胶囊体的位置

radius (float) – 胶囊体的半径

参数:
  • half_height (float) – 胶囊体的半高

  • color (tuple) – 胶囊体的颜色

  • render_cylinder(

  • radius

  • name

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

pos
name,
pos,
rot,
radius,
width,
length,
rot,
half_height

color=None

参数:
  • )[source]#

  • 用于可视化的调试辅助工具,添加一个圆柱体

  • radius

  • pos (tuple) – 圆柱体的位置

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

radius (float) – 圆柱体的半径
参数:
  • name (str) – 名称

  • half_height (float) – 圆柱体的半高

  • pos (tuple) – 位置

  • rot (tuple) – 旋转

  • scale (tuple) – 缩放

  • color (tuple) – 颜色

color (tuple) – 圆柱体的颜色
name,
render_cone(,
name,
pos,
radius,
half_height,
scale=(1.0, 1.0, 1.0),
color=None,
width,
length,
)[source]#
参数:
  • name (str) – 名称

  • parent_body=None

  • is_template=False

用于可视化的调试辅助工具,添加一个圆锥体
name,
pos,
rot,
pos (tuple) – 圆锥体的位置,
radius (float) – 圆锥体的半径,
half_height (float) – 圆锥体的半高,
color (tuple) – 圆锥体的颜色,
width,
length,
render_box(,
rot,
visible=True,
name
参数:
  • name (str) – 名称

  • pos (tuple) – 位置

  • rot (tuple) – 旋转

  • pos

  • rot

  • extents

  • color=None

  • parent_body=None

  • is_template=False

  • )[source]#

  • 用于可视化的调试辅助工具,添加一个盒子

  • visible (bool) – 可见性

pos (tuple) – 盒子的位置
name,
extents (tuple) – 盒子的范围,
name,
rot,
color (tuple) – 盒子的颜色,
visible=True,
render_ref(name, path, pos, rot, scale, color=None)[source]#

path (str) – 路径

参数:
  • render_mesh(

  • name

  • points

  • name (str) – 名称

  • indices

  • visible (bool) – 可见性

colors=None
参数:
  • name (str) – 名称

  • color (tuple) – 颜色

  • indices

pos=(0.0, 0.0, 0.0)
参数:

name (str) – 名称

rot=(0.0, 0.0, 0.0, 1.0)
time=None
update_topology=False
)[source]#,
name (str) – 名称,
points (tuple) – 点,
indices (tuple) – 索引,
colors (tuple) – 颜色,
pos (tuple) – 位置,
rot (tuple) – 旋转,
time (float) – 时间,
update_topology (bool) – 是否更新拓扑,
render_arrow(,
name,
pos,
rot,
base_radius,
base_height,
cap_radius=None,
cap_height=None,
up_axis=1,
color=(1.0, 1.0, 1.0),
)[source]#,
name (str) – 名称,
pos (tuple) – 位置,
rot (tuple) – 旋转,
base_radius (float) – 底部半径,
base_height (float) – 底部高度,
cap_radius (float) – 顶部半径,
cap_height (float) – 顶部高度

up_axis (int) – 向上轴

color (tuple[float, float, float]) – 颜色
render_line_list(
name
vertices
)[source]#,
name (str) – 名称,
points (tuple) – 点,
indices (tuple) – 索引,
colors (tuple) – 颜色,
pos (tuple) – 位置,
rot (tuple) – 旋转,
time (float) – 时间,
update_topology (bool) – 是否更新拓扑,
render_arrow(,
name,
pos,
rot,
base_radius,
base_height,
cap_radius=None,
cap_height=None,
up_axis=1,
color=(1.0, 1.0, 1.0),
)[source]#,
name (str) – 名称,
pos (tuple) – 位置,
rot (tuple) – 旋转,
base_radius (float) – 底部半径,
base_height (float) – 底部高度,
cap_radius (float) – 顶部半径,
color
参数:
  • time

  • radius=0.01

  • )[source]#

  • 用于可视化的调试辅助工具,将线列表添加为一组胶囊体

  • name (str) – 名称

  • vertices – 线带的顶点

  • color (tuple) – 线的颜色

  • time – 更新的时间

  • radius (float) – 半径

  • render_line_strip(name, vertices, color, radius=0.01)[source]#

  • name (str) – 名称

  • vertices (tuple) – 顶点

  • color (tuple) – 颜色

  • radius (float) – 半径

  • render_points(name, points, radius, colors=None)[source]#

  • name (str) – 名称

  • points (tuple) – 点

  • radius (float) – 半径

  • colors (tuple) – 颜色

  • update_body_transforms(body_q)[source]#

  • body_q – 物体四元数

  • save()[source]#

  • class warp.render.OpenGLRenderer(

  • title='Warp'

  • scaling=1.0

  • fps=60

up_axis='Y'

screen_width=1024

screen_height=768

import pyglet

pyglet.options["headless"] = True

import warp.render

# OpenGLRenderer is instantiated with headless=True by default
renderer = warp.render.OpenGLRenderer()
near_plane=1.0
far_plane=100.0
camera_fov=45.0
camera_pos=(0.0, 2.0, 10.0)
camera_front=(0.0, 0.0, -1.0)
camera_up=(0.0, 1.0, 0.0)
background_color=(0.53, 0.8, 0.92),
rescale_window=False,
tile_width=None,
tile_height=None,
tile_ncols=None,
tile_nrows=None,
tile_positions=None,
tile_sizes=None,
projection_matrices=None,
view_matrices=None,
)[源]#

设置分块渲染,其中渲染缓冲区被分割成多个块,每个块可以使用不同的视图和投影矩阵来可视化场景中不同的形状实例。参阅 get_pixels() 方法,它允许检索每个块的像素。参阅 update_tile() 方法,它允许更新给定块的形状实例、投影矩阵、视图矩阵、块大小或块位置。

参数:
  • instances (list[list[int]]): 形状实例 ID 列表的列表。每个形状实例 ID 列表将渲染到一个单独的块中。

  • rescale_window (bool): 如果为 True,窗口将被调整大小以适应这些块。

  • tile_width (int | None): 每个块的宽度(像素)(可选)。

  • tile_height (int | None): 每个块的高度(像素)(可选)。

  • tile_ncols (int | None): 水平渲染的块数量(可选)。如果设置了 tile_width 以计算块位置,将考虑此参数,除非定义了 tile_positions

  • tile_positions (list[tuple[int]] | None): 一个包含 (x, y) 元组的列表,指定每个块的像素位置。如果为 None,这些块将被排列成一个正方形网格;如果设置了 tile_ncolstile_nrows,则按照指定的列数和行数排列成网格。

  • tile_sizes (list[tuple[int]] | None): 一个包含 (width, height) 元组的列表,指定每个块的像素大小。如果为 None,这些块将具有 tile_widthtile_height 指定的相同大小。

  • projection_matrices (list[List[float] | List[List[float]] | ndarray] | None): 每个块的投影矩阵列表(每个视图矩阵可以是展平的 16 维数组或 4x4 矩阵)。如果整个数组为 None,或者仅为某些实例,则所有这些实例的投影矩阵将分别从当前渲染设置中获取。

  • view_matrices (list[List[float] | List[List[float]] | ndarray] | None): 每个块的视图矩阵列表(每个视图矩阵可以是展平的 16 维数组或 4x4 矩阵)。如果整个数组为 None,或者仅为某些实例,则所有这些实例的视图矩阵将分别从当前相机设置中获取,并在相机移动时更新。

  • tile_nrows (int | None)

update_tile(
tile_id,
instances=None,
projection_matrix=None,
view_matrix=None,
tile_size=None,
tile_position=None,
)[源]#

根据块的索引更新给定块的形状实例、投影矩阵、视图矩阵、块大小或块位置。

参数:
  • tile_id: 要更新的块的索引。

  • instances (list[int] | None): 形状实例 ID 列表(可选)。

  • projection_matrix (List[float] | List[List[float]] | ndarray | None): 投影矩阵(可选)。

  • view_matrix (List[float] | List[List[float]] | ndarray | None): 视图矩阵(可选)。

  • tile_size (tuple[int]] | None): 一个 (width, height) 元组,指定块的像素大小(可选)。

  • tile_position (tuple[int]] | None): 一个 (x, y) 元组,指定块的像素位置(可选)。

static compute_projection_matrix(
fov,
aspect_ratio,
near_plane,
far_plane,
)[源]#

根据视场、宽高比、近平面和远平面计算投影矩阵。

参数:
  • fov (float): 视场(度)。

  • aspect_ratio (float): 宽高比(宽度 / 高度)。

  • near_plane (float): 近平面。

  • far_plane (float): 远平面。

返回:

投影矩阵。

返回类型:

List[float] | List[List[float]] | ndarray

update_projection_matrix()[源]#
property camera_pos[源]#
property camera_front[源]#
property camera_up[源]#
compute_view_matrix(cam_pos, cam_front, cam_up)[源]#
update_view_matrix(
cam_pos=None,
cam_front=None,
cam_up=None,
stiffness=1.0,
)[源]#
static compute_model_matrix(camera_axis, scaling)[源]#
参数:
update_model_matrix(model_matrix=None)[源]#
参数:

model_matrix (List[float] | List[List[float]] | ndarray | None)

property num_tiles[源]#
property tile_width[源]#
property tile_height[源]#
property num_shapes[源]#
property num_instances[源]#
property scaling[源]#
begin_frame(t=None)[源]#
参数:

t (float)

end_frame()[源]#
update()[源]#
register_input_processor(callback)[源]#
register_key_press_callback(callback)[源]#
register_shape(
geo_hash,
extents (tuple) – 盒子的范围,
name,
color1=None,
color2=None,
)[源]#
deregister_shape(shape)[源]#
add_shape_instance(
name,
shape,
body,
pos,
rot,
scale=(1.0, 1.0, 1.0),
color1=None,
color2=None,
custom_index=-1,
visible=True,
)[源]#
参数:
  • name (str) – 名称

  • shape (int)

  • custom_index (int) – 自定义索引

  • visible (bool) – 可见性

remove_shape_instance(name)[源]#
参数:

name (str) – 名称

update_instance_colors()[源]#
allocate_shape_instances()[源]#
update_shape_instance(
name,
pos=None,
rot=None,
color1=None,
color2=None,
visible=None,
)[源]#

更新形状的实例变换

参数:
  • name: 形状的名称

  • pos: 形状的位置

  • rot: 形状的旋转

  • color1: 棋盘格图案的第一种颜色

  • color2: 棋盘格图案的第二种颜色

  • visible: 形状是否可见

update_shape_instances()[源]#
update_body_transforms(body_tf)[源]#
参数:

body_tf (array)

register_body(name)[源]#
is_running()[源]#
save()[源]#
get_pixels(
target_image,
split_up_tiles=True,
mode='rgb',
use_uint8=False,
)[源]#

将像素从帧缓冲区(支持 RGB 或深度)读取到给定的数组中。

如果 split_up_tiles 为 False,则数组在 RGB 模式下形状必须为 (screen_height, screen_width, 3),在深度模式下形状必须为 (screen_height, screen_width, 1)。如果 split_up_tiles 为 True,像素将被分割成块(尺寸参见 tile_widthtile_height):则数组在 RGB 模式下形状必须为 (num_tiles, tile_height, tile_width, 3),在深度模式下形状必须为 (num_tiles, tile_height, tile_width, 1)。

参数:
  • target_image (array): 用于存储读取像素的数组。必须是 float32 类型的 dtype 并位于 CUDA 设备上。

  • split_up_tiles (bool): 是否将视口分割成块,参阅 setup_tiled_rendering()

  • mode (str): 可以是“rgb”或“depth”

  • use_uint8 (bool): 在 RGB 模式下,target_image 数组是否使用 uint8 作为 dtype 并返回范围 [0, 255] 的值。否则,假定 dtype 为 float32,值为范围 [0, 1]。

返回:

像素是否成功读取。

返回类型:

bool

render_plane(
name,
pos,
rot,
name,
pos,
color=(1.0, 1.0, 1.0),
color2=None,
width,
length,
u_scaling=1.0,
v_scaling=1.0,
)[源]#

添加一个平面用于可视化

参数:
  • name (str): 平面的名称

  • pos (tuple): 平面的位置

  • rot (tuple): 平面的旋转

  • width (float): 平面的宽度

  • length (float): 平面的长度

  • color (tuple): 平面的颜色

  • texture: 平面的纹理(可选)

  • parent_body=None

  • is_template=False

render_ground(size=1000.0, plane=None)[源]#

添加一个地面平面用于可视化

参数:

size (float) – 地面的尺寸

is_template (bool) – 平面是否为模板
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
width,
length,
rot,
)[source]#

添加用于可视化的球体

参数:
  • name

  • pos

  • radius

  • color – 球体的颜色

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

)[source]#
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
用于可视化的调试辅助工具,添加一个球体,
width,
length,
render_box(,
rot,
)[source]#

添加用于可视化的胶囊体

参数:
  • name (str) – 在阶段上的 USD prim 的名称

  • color (tuple) – 球体的颜色

  • parent_body (str) – 父物体

  • radius

  • up_axis (int) – 胶囊体指向上方的轴 (0: x, 1: y, 2: z)

  • is_template (bool) – 是否为模板

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

render_capsule(
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
用于可视化的调试辅助工具,添加一个球体,
width,
length,
render_box(,
rot,
)[source]#

添加用于可视化的圆柱体

参数:
  • radius

  • half_height

  • color=None

  • radius

  • up_axis (int) – 圆柱体指向上方的轴 (0: x, 1: y, 2: z)

  • is_template (bool) – 是否为模板

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

用于可视化的调试辅助工具,添加一个胶囊体
name,
pos,
rot,
render_ground(size=100.0, plane=None)[source]#,
用于可视化的调试辅助工具,添加一个球体,
width,
length,
render_box(,
rot,
)[source]#

添加用于可视化的圆锥体

参数:
  • half_height (float) – 胶囊体的半高

  • color (tuple) – 胶囊体的颜色

  • render_cylinder(

  • radius

  • up_axis (int) – 圆锥体指向上方的轴 (0: x, 1: y, 2: z)

  • name

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

pos
name,
pos,
rot,
radius,
width,
length,
rot,
)[source]#

添加用于可视化的盒子

参数:
  • )[source]#

  • extents (tuple) – 盒子的范围

  • radius

  • pos (tuple) – 圆柱体的位置

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

color (tuple) – 圆柱体的颜色
name,
render_cone(,
name,
pos,
radius,
half_height,
scale=(1.0, 1.0, 1.0),
color=None,
width,
length,
smooth_shading=True,
)[source]#

添加用于可视化的网格

参数:
  • points – 网格的点

  • indices – 网格的索引

  • colors – 网格的颜色

  • pos – 网格的位置

  • rot – 网格的旋转

  • scale – 网格的缩放

  • radius

  • smooth_shading (bool) – 是否在每个顶点处平均面法线,或为每个面引入额外的顶点

  • parent_body=None

  • is_template=False

用于可视化的调试辅助工具,添加一个圆锥体
name,
pos,
rot,
pos (tuple) – 圆锥体的位置,
radius (float) – 圆锥体的半径,
half_height (float) – 圆锥体的半高,
color (tuple) – 圆锥体的颜色,
width,
length,
render_box(,
rot,
)[source]#

添加用于可视化的箭头

参数:
  • pos (tuple) – 箭头的位置

  • base_radius (float) – 箭头圆柱底部的半径

  • base_height (float) – 箭头圆柱底部的高度

  • cap_radius (float) – 箭头圆锥头部(帽)的半径

  • cap_height (float) – 箭头圆锥头部(帽)的高度

  • radius

  • up_axis (int) – 箭头指向上方的轴 (0: x, 1: y, 2: z)

  • rot (tuple) – 旋转

  • parent_body=None

  • is_template=False

  • 用于可视化的调试辅助工具,添加一个盒子

render_ref(name, path, pos, rot, scale, color=None)[source]#

使用给定名称创建对给定路径的引用(实例)。

参数:
  • name (str) – 名称

  • half_height (float) – 圆柱体的半高

  • pos (tuple) – 位置

  • rot (tuple) – 旋转

  • scale (tuple) – 缩放

  • color (tuple) – 颜色

render_points(name, points, radius, colors=None)[source]#

添加一组点

参数:
  • points – 要渲染的点

  • radius – 点的半径(标量或列表)

  • colors – 点的颜色

  • radius

pos (tuple) – 盒子的位置
name,
extents (tuple) – 盒子的范围,
name,
rot,
color (tuple) – 盒子的颜色,
)[source]#

添加线列表作为一组胶囊体

参数:
  • vertices – 线列表的顶点

  • indices – 线列表的索引

  • name

  • radius (float) – 线的半径

  • name (str) – 名称

render_line_strip(
name,
extents (tuple) – 盒子的范围,
rot,
color (tuple) – 盒子的颜色,
)[source]#

添加线带作为一组胶囊体

参数:
  • render_mesh(

  • name

  • radius (float) – 线的半径

  • name (str) – 名称

update_shape_vertices(shape, points, scale)[source]#

模拟渲染器#

基于 warp.render 中的这些渲染器,派生出了 warp.sim.render 中的 SimRendererUsd(等同于 SimRenderer)和 SimRendererOpenGL 类,用于直接从 warp.sim.ModelBuilder 场景填充渲染器,并从 warp.sim.State 对象更新它们。

warp.sim.render.SimRendererUsd[source]#

别名 SimRenderer

warp.sim.render.SimRendererOpenGL[source]#

别名 SimRenderer

CUDA 图形接口#

Warp 提供了一个 CUDA 图形接口,允许您从 CUDA 内核访问 OpenGL 缓冲区。这对于操作 OpenGL 数组缓冲区非常有用,而无需在 CPU 和 GPU 之间来回复制数据。

有关更多信息,请参阅 CUDA 文档中关于 OpenGL 互操作性的部分

class warp.context.RegisteredGLBuffer(*args, **kwargs)[source]#

辅助类,用于向 CUDA 注册 GL 缓冲区,以便将其映射到 Warp 数组。

示例用法

import warp as wp
import numpy as np
from pyglet.gl import *

wp.init()

# create a GL buffer
gl_buffer_id = GLuint()
glGenBuffers(1, gl_buffer_id)

# copy some data to the GL buffer
glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id)
gl_data = np.arange(1024, dtype=np.float32)
glBufferData(GL_ARRAY_BUFFER, gl_data.nbytes, gl_data.ctypes.data, GL_DYNAMIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)

# register the GL buffer with CUDA
cuda_gl_buffer = wp.RegisteredGLBuffer(gl_buffer_id)

# map the GL buffer to a Warp array
arr = cuda_gl_buffer.map(dtype=wp.float32, shape=(1024,))
# launch a Warp kernel to manipulate or read the array
wp.launch(my_kernel, dim=1024, inputs=[arr])
# unmap the GL buffer
cuda_gl_buffer.unmap()
NONE = 0#

标志,指定了关于如何使用此资源的无任何提示。因此假定此资源将由 CUDA 进行读取和写入。这是默认值。

READ_ONLY = 1#

标志,指定 CUDA 不会写入此资源。

WRITE_DISCARD = 2#

标志,指定 CUDA 不会从此资源读取,并且会覆盖资源的全部内容,因此资源中之前存储的数据将不会保留。

vertices
gl_buffer_id,
cap_radius (float) – 顶部半径,
flags=NONE,
fallback_to_copy=True,
)[source]#
参数:
  • gl_buffer_id (int) – OpenGL 缓冲区 ID (GLuint)。

  • device (Device | str | None) – 用于注册缓冲区的设备。如果为 None,则使用当前设备。

  • flags (int) – 标志常量 NONEREAD_ONLYWRITE_DISCARD 的组合。

  • fallback_to_copy (bool) – 如果为 True 且 CUDA/OpenGL 互操作性不可用,则回退到在 Warp 数组和 OpenGL 缓冲区之间进行复制操作。否则,将引发 RuntimeError

up_axis='Y'

fallback_to_copy 选项(如果 CUDA 图形互操作性功能不可用,则使用复制操作)需要 pyglet 2.0 或更高版本。通过 pip install pyglet==2.* 安装。

map(dtype, shape)[source]#

将 OpenGL 缓冲区映射到 Warp 数组。

参数:
  • dtype – 数组中每个元素的类型。

  • shape – 数组的形状。

返回:

表示已映射 OpenGL 缓冲区的 Warp 数组对象。

返回类型:

array

unmap()[source]#

取消映射 OpenGL 缓冲区。