The MeshRenderer is a mesh renderer component that uses mesh objects (e.g., cuboids) as the geometry data source. When an entity is equipped with a mesh renderer component, simply set its mesh
and material
to render the object.
In the editor Hierarchy Panel, you can quickly create a node with a cuboid mesh renderer attached ( Hierarchy Panel -> Right-click -> 3D Object -> Cuboid ).
Alternatively, you can manually attach a mesh renderer to an existing node in the scene and assign any mesh and material ( Select Node -> Inspector Panel -> Add Component -> Mesh Renderer ).
Here's how to use it in code:
const cubeEntity = rootEntity.createChild("cube");
const cube = cubeEntity.addComponent(MeshRenderer);
cube.mesh = PrimitiveMesh.createCuboid(engine, 2, 2, 2);
cube.setMaterial(new BlinnPhongMaterial(engine));
In the editor, you can conveniently configure properties of the mesh renderer.
Property | Description |
---|---|
material | Material information for the object to be rendered |
mesh | Mesh information for the object to be rendered |
receiveShadows | Whether to receive shadows |
castShadows | Whether to cast shadows |
priority | Render priority. Lower values indicate higher priority. Default is 0 |
Compared to the basic Renderer, the mesh renderer also supports vertex color settings (vertex color data is included in the mesh's vertex information).
Property | Description |
---|---|
enableVertexColor | Whether to support vertex colors |
The mesh renderer does not add any new methods. However, note that a mesh often contains multiple sub-meshes. If you want each sub-mesh to use different materials, specify the corresponding mesh index during setup. Otherwise, the same material will be applied to all sub-meshes by default.