| # Unity Rendering |
|
|
| ## Materials and Shaders |
|
|
| A **Material** is an asset that defines how a surface is rendered. It |
| references a **Shader** (the GPU program) and sets its properties (colors, |
| textures, numbers). |
|
|
| Unity 2022 ships the following built-in shaders worth knowing: |
|
|
| | Shader | Use case | |
| |-------------------|---------------------------------------------------------| |
| | `Standard` | The default PBR shader -- metal/smoothness workflow | |
| | `Universal Render Pipeline/Lit` | URP equivalent of Standard | |
| | `Unlit/Texture` | Pure textured quad, no lighting | |
| | `Sprites/Default` | 2D sprites | |
| | `UI/Default` | Canvas UI Image | |
|
|
| The generated project uses the Built-in Render Pipeline (not URP) so that |
| the agent can rely on `Shader.Find("Standard")` without any package setup. |
|
|
| ### Procedural materials |
|
|
| ```csharp |
| var mat = new Material(Shader.Find("Standard")); |
| mat.name = "ProceduralWater"; |
| mat.color = new Color(0.2f, 0.6f, 0.8f, 0.7f); |
| mat.SetFloat("_Glossiness", 0.85f); |
| mat.SetFloat("_Metallic", 0.1f); |
| mat.EnableKeyword("_EMISSION"); |
| mat.SetColor("_EmissionColor", new Color(1f, 0.85f, 0.4f)); |
| mat.renderQueue = 3000; // transparent |
| GetComponent<MeshRenderer>().sharedMaterial = mat; |
| ``` |
|
|
| * `_Color`, `_MainTex`, `_Glossiness`, `_Metallic`, `_EmissionColor` are the |
| Standard shader's main properties. |
| * Call `EnableKeyword("_EMISSION")` before setting `_EmissionColor`. |
| * For transparent materials set `_Mode` to 3 (Transparent) and configure |
| blend modes as in the `WaterMaterial.cs` generator. |
|
|
| ## Lighting |
|
|
| * **Directional light** -- simulates the sun. Rotating it changes time of |
| day. The `DayNightCycle.cs` generator does exactly this. |
| * **Point light** -- radiates in all directions; cost is proportional to |
| the number of pixels it touches. Use sparingly on mobile. |
| * **Spot light** -- cone shape; useful for flashlights, street lamps. |
| * **Area light** -- only baked; good for indoor ceilings. |
|
|
| **Realtime vs Baked:** static geometry should use **baked** lighting |
| (Lightmap) for performance; dynamic objects use realtime lighting. |
|
|
| ```csharp |
| Light l = gameObject.AddComponent<Light>(); |
| l.type = LightType.Point; |
| l.color = Color.white; |
| l.intensity = 1.5f; |
| l.range = 15f; |
| l.shadows = LightShadows.Soft; // softest, but most expensive |
| ``` |
|
|
| ## Skybox and ambient |
|
|
| * The skybox is set via `RenderSettings.skybox` or `Window > Rendering > Lighting > Environment`. |
| * `RenderSettings.ambientLight` controls flat ambient color when |
| `RenderSettings.ambientMode = AmbientMode.Flat`. |
| * The `DayNightCycle` generator lerps both `ambientLight` and |
| `Camera.main.backgroundColor` between day/night colors. |
|
|
| ## Post-processing (Built-in Pipeline) |
|
|
| Post-processing is provided by the `com.unity.postprocessing` package. Add a |
| `Post-process Layer` to the Main Camera and a `Post-process Volume` to the |
| scene to drive effects such as Bloom, Color Grading, Depth of Field, |
| Vignette and Motion Blur. |
|
|
| ```csharp |
| // You usually configure post-processing in the inspector rather than code. |
| // Make sure the camera has a PostProcessLayer and a global PostProcessVolume. |
| ``` |
|
|
| ## LOD (Level of Detail) |
|
|
| Use `LODGroup` to swap meshes based on distance. Unity picks the LOD level |
| based on the screen-space size of the bounding box. |
|
|
| ``` |
| LOD 0 -- 60%+ screen size -- high-poly mesh |
| LOD 1 -- 30%-60% -- medium |
| LOD 2 -- 10%-30% -- low |
| Cull -- <10% -- not rendered |
| ``` |
|
|
| For procedural cities, prefer instancing (`Graphics.DrawMeshInstanced`) or |
| `LODGroup` over spawning 50+ full-detail buildings. |
|
|
| ## Fog |
|
|
| ```csharp |
| RenderSettings.fog = true; |
| RenderSettings.fogMode = FogMode.Linear; |
| RenderSettings.fogColor = new Color(0.5f, 0.5f, 0.5f, 1f); |
| RenderSettings.fogStartDistance = 50f; |
| RenderSettings.fogEndDistance = 300f; |
| ``` |
|
|
| Fog hides the far clip plane pop-in and is essential for large open worlds. |
|
|