File size: 4,038 Bytes
40c0886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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.