CodeBlanks Logo Icon CodeBlanks

MeshFilter and MeshRenderer

Learn the division of labor between MeshFilter and MeshRenderer.

The MeshFilter Component

To give a GameObject a 3D shape, the MeshFilter component is used. MeshFilter holds the reference to which Mesh data the object contains, but does not draw anything on screen itself. It provides this data via the sharedMesh or mesh property.

MeshFilter filter = GetComponent<MeshFilter>();
Mesh modelMesh = filter.sharedMesh;

The MeshRenderer Component

The component that takes geometry from MeshFilter and renders it into pixels on screen is MeshRenderer. This component manages material (Material) settings and lighting/shadow behavior. For a 3D model to be visible in a scene, MeshFilter and MeshRenderer work together.

MeshRenderer renderer = GetComponent<MeshRenderer>();
renderer.enabled = true;

Practice Check Renderer State

If the renderer.enabled property is true, print Debug.Log("Model ciziliyor");.

Starter code

Player.cs
using System;

public class MeshRenderer
{
    public bool enabled = true;
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        MeshRenderer renderer = new MeshRenderer();
        // cizici aktifse ekrana cizim mesajini yazdir
    }
}

Sample solution

Player.cs
using System;

public class MeshRenderer
{
    public bool enabled = true;
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        MeshRenderer renderer = new MeshRenderer();
        if (renderer.enabled)
        {
            Debug.Log("Model ciziliyor");
        }
    }
}

Expected output

Model ciziliyor

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the primary role of the MeshFilter component on a GameObject?

  • To change the texture of the material
  • To hold the 3D geometry data (Mesh) reference Correct answer
  • To convert geometry into pixels and render on screen
  • To add physical gravity to the object

Explanation: MeshFilter holds the geometry data in memory; rendering it to the screen is performed by MeshRenderer.

2 Get MeshRenderer Component

Question
MeshRenderer cizici = GetComponent<____>();
Answer
MeshRenderer cizici = GetComponent<MeshRenderer>();

Explanation: To control rendering and material operations, the MeshRenderer component is retrieved.

3 Model Components Division of Labor

Term Meaning
MeshFilter Holds reference to geometry data
MeshRenderer Draws geometry to screen with material
sharedMesh Shared original mesh reference in memory
renderer.enabled Whether rendering is enabled or not

All lessons in Intermediate