Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท 2D and 3D Workflows

3D Workflow - Materials, Shaders and Lighting

Understand Unity 3D art - meshes, materials, PBR textures, shader graph, light types, baked and realtime lighting, probes and post-processing - with runnable lighting math.

A 3D object in Unity is built from three things: a mesh (the shape, made of triangles), a material (how the surface looks) and lights (what makes it visible). Getting a good-looking scene is mostly about understanding how these three interact, and how to keep that cheap enough for your target devices.

This lesson tours the 3D art pipeline: importing models, physically based materials, shaders and Shader Graph, light types, baked versus realtime lighting, reflection and light probes, and post-processing. The editor steps are described in text; the lighting math runs as plain C# stand-ins.

Meshes and importing models

Unity imports .fbx, .obj, .blend (through Blender) and glTF with a package. A model appears as a Mesh Filter (the geometry) plus a Mesh Renderer (which draws it with materials). In the model's Import Settings you control scale (a common surprise: a model that is 100 times too big or small), Generate Colliders, Read/Write (keep off to save memory), normals and tangents, and the Rig type (None, Generic, Humanoid) for animated characters.

Fewer triangles are better for performance. Use LOD Group components to swap a high-detail model for simpler ones with distance, and Occlusion Culling (baked) to skip objects hidden behind walls.

Materials and PBR

A material is an asset with a shader and its settings. The standard approach in modern engines is PBR (physically based rendering) with a few maps:

  • Base Map / Albedo: the surface colour without lighting.
  • Metallic: 0 for non-metals (wood, plastic), 1 for metals; mostly black or white.
  • Smoothness: how sharp the reflection is, from rough to mirror-like.
  • Normal Map: fakes small bumps without extra triangles.
  • Occlusion and Emission: baked shadow in crevices and self-lit areas.

The default shader depends on the render pipeline: URP (Universal Render Pipeline, Lit shader) is the right choice for most mobile, VR and cross-platform games; HDRP targets high-end PCs and consoles; the older Built-in pipeline is legacy. Materials from one pipeline appear magenta in another; convert them in the Render Pipeline Converter.

C#
using UnityEngine;

public class FlashOnHit : MonoBehaviour
{
    static readonly int BaseColor = Shader.PropertyToID("_BaseColor");
    MaterialPropertyBlock block;
    Renderer rend;

    void Awake()
    {
        rend = GetComponent<Renderer>();
        block = new MaterialPropertyBlock();
    }

    public void Flash()
    {
        block.SetColor(BaseColor, Color.red);
        rend.SetPropertyBlock(block);       // per-object change, no new material
    }
}

Reading renderer.material creates a copy of the material for that object (and a leak if you never destroy it); sharedMaterial edits the asset itself. A MaterialPropertyBlock avoids both problems for small changes.

Shader Graph

You rarely need to write shader code. Shader Graph (Assets > Create > Shader Graph > URP) is a node editor: connect nodes such as Sample Texture 2D, Fresnel Effect, Time and Lerp into the Fragment output (Base Color, Emission, Alpha). Expose a node's value as a Property so materials and scripts can change it. Typical effects: dissolve, hologram, water, toon shading, outlines.

How light becomes colour

The simplest lighting model is Lambert diffuse: brightness is the cosine of the angle between the surface normal and the direction to the light, which is a dot product. Specular (shine) adds a highlight that depends on the viewer. PBR shaders are refinements of the same ideas. A point light also fades with distance, following the inverse-square law.

C# Stand-in: Lambert diffuse and light falloff

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The ceiling gets 0 because its normal points away from the light. This is why unlit sides of objects look dark and why ambient light and shadows matter.

Light types and shadows

  • Directional: the sun; direction only, same everywhere. One per scene is typical.
  • Point: a bulb, radiates in all directions with a range.
  • Spot: a cone, such as a torch or stage light.
  • Area (baked only): rectangle or disc, for soft window light.

Each light has a Mode: Realtime (computed every frame, supports moving objects, expensive), Baked (precomputed into lightmaps, nearly free at runtime, but static) or Mixed (baked indirect plus realtime direct). Objects marked Static (Contribute GI) are included in the bake. Shadows cost a lot: limit the shadow distance, use a few shadow cascades and turn shadows off on small lights.

Global illumination and probes

Baked lighting, called Global Illumination (GI), includes bounced light (a red wall tinting the white floor next to it). Open Window > Rendering > Lighting, choose the Progressive GPU lightmapper, and click Generate Lighting. Moving objects do not have lightmaps, so Unity gives them baked light from Light Probes (a cloud of sample points you place) and mirrors the surroundings with Reflection Probes for shiny surfaces. The Skybox (a material on the Lighting settings) provides both background and ambient light.

Post-processing

A Volume with Volume Profile overrides adds screen effects after the scene is drawn: Bloom, Color Adjustments, Tonemapping, Vignette, Depth of Field and Ambient Occlusion (a Renderer Feature in URP). Use them sparingly, and test on the lowest-end device: Bloom and depth of field are expensive on mobile.

C# Stand-in: tonemapping a bright pixel

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Lighting is calculated in linear HDR values that may exceed 1.0; tonemapping squeezes them into the displayable range, so very bright lights do not just clip to white. Keep the project in Linear colour space (Project Settings > Player).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three basic ingredients of a visible 3D object?

    A mesh gives the shape, a material the look, and lights make it visible.
  2. 2. Why prefer sharedMaterial or a MaterialPropertyBlock over renderer.material for small changes?

    Accessing .material duplicates the material; property blocks change per-object values without copies.
  3. 3. Which light mode costs almost nothing at runtime?

    Baked lights are precomputed into lightmaps, but they cannot affect moving objects (probes help).
  4. 4. What gives moving objects bounced baked light?

    Light Probes store baked light at points and are interpolated onto dynamic objects.

Summary

  • A Mesh Filter and Mesh Renderer draw geometry with materials; keep scale, LODs and triangle counts in mind.
  • Use PBR materials (base, metallic, smoothness, normal) in URP, and Shader Graph for custom effects.
  • Diffuse lighting is a dot product with an inverse-square falloff; choose Baked, Mixed or Realtime per light.
  • Light and reflection probes light moving objects; post-processing adds the final look in HDR with tonemapping.

Next: NavMesh and AI Navigation