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 – 用于场景中实体的缩放因子
- 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,
- render_plane(
- name,
- pos,
- rot,
- name,
- pos,
- rot,
- width,
- length,
parent_body=None
- is_template (bool) – 平面是否为模板 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,
radius (float) – 球体的半径
- render_capsule(
- name,
- pos,
- rot,
- render_ground(size=100.0, plane=None)[source]#,
- 用于可视化的调试辅助工具,添加一个球体,
- width,
- length,
- rot,
pos
- 用于可视化的调试辅助工具,添加一个胶囊体
- name,
- pos,
- rot,
- render_ground(size=100.0, plane=None)[source]#,
- 用于可视化的调试辅助工具,添加一个球体,
- width,
- length,
- rot,
radius (float) – 胶囊体的半径
- pos
- name,
- pos,
- rot,
- radius,
- width,
- length,
- rot,
color=None
- radius (float) – 圆柱体的半径
- color (tuple) – 圆柱体的颜色
- name,
- render_cone(,
- name,
- pos,
- radius,
- half_height,
- scale=(1.0, 1.0, 1.0),
- color=None,
- width,
- length,
- 参数:
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,
- 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) – 顶部半径,
up_axis (int) – 向上轴
- 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) – 顶部半径,
- 参数:
time
radius=0.01
用于可视化的调试辅助工具,将线列表添加为一组胶囊体
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) – 半径
name (str) – 名称
points (tuple) – 点
radius (float) – 半径
colors (tuple) – 颜色
body_q – 物体四元数
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_ncols 和 tile_nrows,则按照指定的列数和行数排列成网格。
tile_sizes (list[tuple[int]] | None): 一个包含 (width, height) 元组的列表,指定每个块的像素大小。如果为 None,这些块将具有 tile_width 和 tile_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: 要更新的块的索引。
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,
根据视场、宽高比、近平面和远平面计算投影矩阵。
- add_shape_instance(
- name,
- shape,
- body,
- pos,
- rot,
- scale=(1.0, 1.0, 1.0),
- color1=None,
- color2=None,
- custom_index=-1,
- visible=True,
- update_shape_instance(
- name,
- pos=None,
- rot=None,
- color1=None,
- color2=None,
- visible=None,
更新形状的实例变换
- 参数:
name: 形状的名称
pos: 形状的位置
rot: 形状的旋转
color1: 棋盘格图案的第一种颜色
color2: 棋盘格图案的第二种颜色
visible: 形状是否可见
- 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_width
和tile_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]。
- 返回:
像素是否成功读取。
- 返回类型:
- 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,
添加一个平面用于可视化
- is_template (bool) – 平面是否为模板 )[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,
添加用于可视化的胶囊体
- render_capsule(
- name,
- pos,
- rot,
- render_ground(size=100.0, plane=None)[source]#,
- 用于可视化的调试辅助工具,添加一个球体,
- width,
- length,
- render_box(,
- rot,
添加用于可视化的圆柱体
- 用于可视化的调试辅助工具,添加一个胶囊体
- name,
- pos,
- rot,
- render_ground(size=100.0, plane=None)[source]#,
- 用于可视化的调试辅助工具,添加一个球体,
- width,
- length,
- render_box(,
- rot,
添加用于可视化的圆锥体
- color (tuple) – 圆柱体的颜色
- name,
- render_cone(,
- name,
- pos,
- radius,
- half_height,
- scale=(1.0, 1.0, 1.0),
- color=None,
- width,
- length,
- smooth_shading=True,
添加用于可视化的网格
- 参数:
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,
添加用于可视化的箭头
- render_points(name, points, radius, colors=None)[source]#
添加一组点
- 参数:
points – 要渲染的点
radius – 点的半径(标量或列表)
colors – 点的颜色
radius
模拟渲染器#
基于 warp.render
中的这些渲染器,派生出了 warp.sim.render
中的 SimRendererUsd
(等同于 SimRenderer
)和 SimRendererOpenGL
类,用于直接从 warp.sim.ModelBuilder
场景填充渲染器,并从 warp.sim.State
对象更新它们。
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,
- 参数:
up_axis='Y'
fallback_to_copy
选项(如果 CUDA 图形互操作性功能不可用,则使用复制操作)需要 pyglet 2.0 或更高版本。通过pip install pyglet==2.*
安装。