Renderer

Mesh Renderer

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.

Usage

In the editor Hierarchy Panel, you can quickly create a node with a cuboid mesh renderer ( Hierarchy Panel -> Right Click -> 3D Object -> Cuboid ).

image-20231007153753006

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 ).

image-20231007153753006

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));

Properties

In the editor, you can easily set the properties of the mesh renderer.

image-20231007153753006
SettingExplanation
materialMaterial information of the object to be rendered
meshMesh information of the object to be rendered
receiveShadowsWhether to receive shadows
castShadowsWhether to cast shadows
priorityRendering 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).

PropertyExplanation
enableVertexColorWhether to support vertex colors
const meshRenderer = entity.getComponent(MeshRenderer);
// Enable vertex colors
meshRenderer.enableVertexColor = true;

Methods

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.

Was this page helpful?