OpenMotionLab/MotionGPT
118
1.. _model_guide:2 3Loading and Configuring Models4==============================5The first step to any rendering application is loading your models.6Pyrender implements the GLTF 2.0 specification, which means that all7models are composed of a hierarchy of objects.8 9At the top level, we have a :class:`.Mesh`. The :class:`.Mesh` is10basically a wrapper of any number of :class:`.Primitive` types,11which actually represent geometry that can be drawn to the screen.12 13Primitives are composed of a variety of parameters, including14vertex positions, vertex normals, color and texture information,15and triangle indices if smooth rendering is desired.16They can implement point clouds, triangular meshes, or lines17depending on how you configure their data and set their18:attr:`.Primitive.mode` parameter.19 20Although you can create primitives yourself if you want to,21it's probably easier to just use the utility functions provided22in the :class:`.Mesh` class.23 24Creating Triangular Meshes25--------------------------26 27Simple Construction28~~~~~~~~~~~~~~~~~~~29Pyrender allows you to create a :class:`.Mesh` containing a30triangular mesh model directly from a :class:`~trimesh.base.Trimesh` object31using the :meth:`.Mesh.from_trimesh` static method.32 33>>> import trimesh34>>> import pyrender35>>> import numpy as np36>>> tm = trimesh.load('examples/models/fuze.obj')37>>> m = pyrender.Mesh.from_trimesh(tm)38>>> m.primitives39[<pyrender.primitive.Primitive at 0x7fbb0af60e50>]40 41You can also create a single :class:`.Mesh` from a list of42:class:`~trimesh.base.Trimesh` objects:43 44>>> tms = [trimesh.creation.icosahedron(), trimesh.creation.cylinder()]45>>> m = pyrender.Mesh.from_trimesh(tms)46[<pyrender.primitive.Primitive at 0x7fbb0c2b74d0>,47 <pyrender.primitive.Primitive at 0x7fbb0c2b7550>]48 49Vertex Smoothing50~~~~~~~~~~~~~~~~51 52The :meth:`.Mesh.from_trimesh` method has a few additional optional parameters.53If you want to render the mesh without interpolating face normals, which can54be useful for meshes that are supposed to be angular (e.g. a cube), you55can specify ``smooth=False``.56 57>>> m = pyrender.Mesh.from_trimesh(tm, smooth=False)58 59Per-Face or Per-Vertex Coloration60~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~61 62If you have an untextured trimesh, you can color it in with per-face or63per-vertex colors:64 65>>> tm.visual.vertex_colors = np.random.uniform(size=tm.vertices.shape)66>>> tm.visual.face_colors = np.random.uniform(size=tm.faces.shape)67>>> m = pyrender.Mesh.from_trimesh(tm)68 69Instancing70~~~~~~~~~~71 72If you want to render many copies of the same mesh at different poses,73you can statically create a vast array of them in an efficient manner.74Simply specify the ``poses`` parameter to be a list of ``N`` 4x4 homogenous75transformation matrics that position the meshes relative to their common76base frame:77 78>>> tfs = np.tile(np.eye(4), (3,1,1))79>>> tfs[1,:3,3] = [0.1, 0.0, 0.0]80>>> tfs[2,:3,3] = [0.2, 0.0, 0.0]81>>> tfs82array([[[1. , 0. , 0. , 0. ],83 [0. , 1. , 0. , 0. ],84 [0. , 0. , 1. , 0. ],85 [0. , 0. , 0. , 1. ]],86 [[1. , 0. , 0. , 0.1],87 [0. , 1. , 0. , 0. ],88 [0. , 0. , 1. , 0. ],89 [0. , 0. , 0. , 1. ]],90 [[1. , 0. , 0. , 0.2],91 [0. , 1. , 0. , 0. ],92 [0. , 0. , 1. , 0. ],93 [0. , 0. , 0. , 1. ]]])94 95>>> m = pyrender.Mesh.from_trimesh(tm, poses=tfs)96 97Custom Materials98~~~~~~~~~~~~~~~~99 100You can also specify a custom material for any triangular mesh you create101in the ``material`` parameter of :meth:`.Mesh.from_trimesh`.102The main material supported by Pyrender is the103:class:`.MetallicRoughnessMaterial`.104The metallic-roughness model supports rendering highly-realistic objects across105a wide gamut of materials.106 107For more information, see the documentation of the108:class:`.MetallicRoughnessMaterial` constructor or look at the Khronos_109documentation for more information.110 111.. _Khronos: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#materials112 113Creating Point Clouds114---------------------115 116Point Sprites117~~~~~~~~~~~~~118Pyrender also allows you to create a :class:`.Mesh` containing a119point cloud directly from :class:`numpy.ndarray` instances120using the :meth:`.Mesh.from_points` static method.121 122Simply provide a list of points and optional per-point colors and normals.123 124>>> pts = tm.vertices.copy()125>>> colors = np.random.uniform(size=pts.shape)126>>> m = pyrender.Mesh.from_points(pts, colors=colors)127 128Point clouds created in this way will be rendered as square point sprites.129 130.. image:: /_static/points.png131 132Point Spheres133~~~~~~~~~~~~~134If you have a monochromatic point cloud and would like to render it with135spheres, you can render it by instancing a spherical trimesh:136 137>>> sm = trimesh.creation.uv_sphere(radius=0.1)138>>> sm.visual.vertex_colors = [1.0, 0.0, 0.0]139>>> tfs = np.tile(np.eye(4), (len(pts), 1, 1))140>>> tfs[:,:3,3] = pts141>>> m = pyrender.Mesh.from_trimesh(sm, poses=tfs)142 143.. image:: /_static/points2.png144 