MeshRenderer is a mesh renderer component that uses mesh objects (such as cubes) as the data source for geometric outlines. When an entity is mounted with a mesh renderer component, you only need to 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 ( Hierarchy Panel -> Right Click -> 3D Object -> Cuboid ).
Of course, you can also mount a mesh renderer to an existing node in the scene and set any mesh and material. ( Select Node -> Inspector Panel -> Add Component -> Mesh Renderer ).
The corresponding usage in the script is as follows:
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 easily set the properties of the mesh renderer.
Setting | Explanation |
---|---|
material | Material information of the object to be rendered |
mesh | Mesh information of the object to be rendered |
receiveShadows | Whether to receive shadows |
castShadows | Whether to cast shadows |
priority | Rendering priority of the renderer, the smaller the value, the higher the priority, default is 0 |
Compared to the basic Renderer, the mesh renderer can also set whether to support vertex colors (vertex color data is included in the vertex information of the mesh).
Property | Explanation |
---|---|
enableVertexColor | Whether to support vertex colors |
const meshRenderer = entity.getComponent(MeshRenderer);
// Enable vertex colors
meshRenderer.enableVertexColor = true;
The mesh renderer does not add any new methods, but it is important to note that in many cases, the mesh renderer's mesh contains several sub-meshes. If you want each sub-mesh to correspond to different materials, you can specify the corresponding mesh index when setting it, otherwise, the same material will be used by default.