Skip to content

Commit 0517ea0

Browse files
committed
some 08 highlights
1 parent 6478fd7 commit 0517ea0

22 files changed

+201
-87
lines changed

articles/tutorials/advanced/2d_shaders/08_light_effect/index.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The second stage references a new term, called the _Normal_ Map. We will come ba
7070

7171
Right before the `SpriteBatch.Begin()` class, invoke the `StartColorPhase()` method. Here is the `Draw()` method with most of the code left out, but it demonstrates where the `StartColorPhase()` and `Finish()` methods belong:
7272

73-
[!code-csharp[](./snippets/snippet-8-06.cs)]
73+
[!code-csharp[](./snippets/snippet-8-06.cs?highlight=6,17)]
7474

7575
7. If you run the game now, the game will appear blank except for the UI. That is because the game is rendering to an off-screen texture, but nothing is rendering the off-screen texture _back_ to the screen. For now, we will add some diagnostic visualization of the off-screen texture. Add the following function to the `DeferredRenderer` class.
7676

@@ -106,11 +106,11 @@ The next step is to create some lights and render them to a second off-screen te
106106

107107
4. Then, we need to call the new method in the `GameScene`'s `Draw()` method between the current `SpriteBatch.End()` call and the `deferredRenderer.Finish()` call:
108108

109-
[!code-csharp[](./snippets/snippet-8-12.cs)]
109+
[!code-csharp[](./snippets/snippet-8-12.cs?highlight=9)]
110110

111111
5. To finish off with the `DeferredRenderer` changes for now, add the `LightBuffer` to the `DebugDraw()` view as well:
112112

113-
[!code-csharp[](./snippets/snippet-8-13.cs)]
113+
[!code-csharp[](./snippets/snippet-8-13.cs?highlight=41)]
114114

115115
Now when you run the game, you'll see a blank texture in the top-right. It is blank because there are no lights yet.
116116

@@ -134,11 +134,11 @@ Each light will be drawn using a shader so that the fall-off and intensity can b
134134

135135
3. And then load the `Material` in the `LoadContent()` method:
136136

137-
[!code-csharp[](./snippets/snippet-8-15.cs)]
137+
[!code-csharp[](./snippets/snippet-8-15.cs?highlight=7)]
138138

139139
4. And do not forget to enable the hot-reload by adding the `Update()` line in the `Update()` method:
140140

141-
[!code-csharp[](./snippets/snippet-8-16.cs)]
141+
[!code-csharp[](./snippets/snippet-8-16.cs?highlight=5)]
142142

143143
5. In order to handle multiple lights, it will be helpful to have a class to represent each light. Create a new file in the _MonoGameLibrary_'s graphics folder called `PointLight.cs`:
144144

@@ -158,7 +158,7 @@ Each light will be drawn using a shader so that the fall-off and intensity can b
158158

159159
9. And call it from the `GameScene`'s `Draw()` method after the `StartLightPhase()` invocation:
160160

161-
[!code-csharp[](./snippets/snippet-8-21.cs)]
161+
[!code-csharp[](./snippets/snippet-8-21.cs?highlight=7)]
162162

163163
Now when you run the game, you will see a blank white square where the point light is located (at 300,300).
164164

@@ -180,41 +180,41 @@ The next task is to write the `pointLightEffect.fx` shader file so that the whit
180180
| :----------------------------------------------------------------------------------------------------------------: |
181181
| **Figure 8-6: Showing the distance from the center of the light in the red channel** |
182182

183-
2. That starts to look like a light, but in reverse. Create a new variable, `falloff` which inverts the distance. The `saturate` function is shorthand for clamping the value between `0` and `1`:
183+
2. That starts to look like a light, but in reverse. Create a new variable, `falloff` which inverts the distance. The [`saturate`](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-saturate) function is shorthand for clamping the value between `0` and `1`:
184184

185-
[!code-hlsl[](./snippets/snippet-8-23.hlsl)]
185+
[!code-hlsl[](./snippets/snippet-8-23.hlsl?highlight=5)]
186186

187187
| ![Figure 8-7: Invert the distance](./images/point-light-falloff-1.png) |
188188
| :--------------------------------------------------------------------: |
189189
| **Figure 8-7: Invert the distance** |
190190

191191
3. That looks more light-like. Now it is time to add some artistic control parameters to the shader. First, it would be good to be able to increase the brightness of the light. Multiplying the `falloff` by some number larger than 1 would increase the brightness, but leave the unlit sections completely unlit:
192192

193-
[!code-hlsl[](./snippets/snippet-8-24.hlsl)]
193+
[!code-hlsl[](./snippets/snippet-8-24.hlsl?highlight=1,7)]
194194

195195
| ![Figure 8-8: A LightBrightness parameter](./gifs/point-light-brightness.gif) |
196196
| :---------------------------------------------------------------------------: |
197197
| **Figure 8-8: A LightBrightness parameter** |
198198

199199
4. It would also be good to control the sharpness of the fall off. The `pow()` function raises the `falloff` to some exponent value:
200200

201-
[!code-hlsl[](./snippets/snippet-8-25.hlsl)]
201+
[!code-hlsl[](./snippets/snippet-8-25.hlsl?highlight=2,9)]
202202

203203
| ![Figure 8-9: A LightSharpness parameter](./gifs/point-light-sharpness.gif) |
204204
| :-------------------------------------------------------------------------: |
205205
| **Figure 8-9: A LightSharpness parameter** |
206206

207207
5. Finally, the shader parameters from `0` to `1`, but it would be nice to push the brightness and sharpness beyond `1`. Add a `range` multiplier in the shader code:
208208

209-
[!code-hlsl[](./snippets/snippet-8-26.hlsl)]
209+
[!code-hlsl[](./snippets/snippet-8-26.hlsl?highlight=8)]
210210

211211
| ![Figure 8-10: Increase the range of the artistic parameters](./gifs/point-light-range.gif) |
212212
| :----------------------------------------------------------------------------------------: |
213213
| **Figure 8-10: Increase the range of the artistic parameters** |
214214

215215
6. The final touch is to return the `Color` of the light, instead of the red debug value. The `input.Color` carries the `Color` passed through the `SpriteBatch`, so we can use that. Multiply the alpha channel of the color by the `falloff` to _fade_ the light out without changing the light color itself:
216216

217-
[!code-hlsl[](./snippets/snippet-8-27.hlsl)]
217+
[!code-hlsl[](./snippets/snippet-8-27.hlsl?highlight=14)]
218218

219219
7. Change the light color in C# to `CornflowerBlue`:
220220

@@ -349,7 +349,7 @@ Now that we have the art assets, it is time to work the normal maps into the cod
349349

350350
5. And do not forget to update the `technique` to reference the new `MainPS` function:
351351

352-
[!code-hlsl[](./snippets/snippet-8-45.hlsl)]
352+
[!code-hlsl[](./snippets/snippet-8-45.hlsl?highlight=6)]
353353

354354
6. In C#, when the `GraphcisDevice.SetRenderTarget()` function is called, it sets the texture that the `COLOR0` semantic will be sent to. However, there is an overload called `SetRenderTargets()` that accepts _multiple_ `RenderTarget2D`s, and each additional texture will be assigned to the next `COLOR` semantic.
355355

@@ -386,7 +386,7 @@ To start rendering the normal values themselves, we need to load the normal text
386386

387387
3. And finally, pass it to the `_gameEffect` material as a parameter:
388388

389-
[!code-csharp[](./snippets/snippet-8-50.cs)]
389+
[!code-csharp[](./snippets/snippet-8-50.cs?highlight=10)]
390390

391391
4. The shader itself needs to expose a `Texture2D` and `Sampler` state for the new normal texture:
392392

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
// Render the debug view for the game
2-
_deferredRenderer.DebugDraw();
1+
public override void Draw(GameTime gameTime)
2+
{
3+
// ...
4+
5+
// Render the debug view for the game
6+
_deferredRenderer.DebugDraw();
7+
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
LightBuffer = new RenderTarget2D(
2-
graphicsDevice: Core.GraphicsDevice,
3-
width: viewport.Width,
4-
height: viewport.Height,
5-
mipMap: false,
6-
preferredFormat: SurfaceFormat.Color,
7-
preferredDepthFormat: DepthFormat.None);
1+
public DeferredRenderer()
2+
{
3+
// ...
4+
5+
LightBuffer = new RenderTarget2D(
6+
graphicsDevice: Core.GraphicsDevice,
7+
width: viewport.Width,
8+
height: viewport.Height,
9+
mipMap: false,
10+
preferredFormat: SurfaceFormat.Color,
11+
preferredDepthFormat: DepthFormat.None);
12+
}
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
// Always end the sprite batch when finished.
2-
Core.SpriteBatch.End();
3-
4-
// start rendering the lights
5-
_deferredRenderer.StartLightPhase();
6-
7-
// TODO: draw lights
8-
9-
// finish the deferred rendering
10-
_deferredRenderer.Finish();
1+
public override void Draw(GameTime gameTime)
2+
{
3+
// ...
4+
5+
// Always end the sprite batch when finished.
6+
Core.SpriteBatch.End();
7+
8+
// start rendering the lights
9+
_deferredRenderer.StartLightPhase();
10+
11+
// TODO: draw lights
12+
13+
// finish the deferred rendering
14+
_deferredRenderer.Finish();
15+
16+
// ...
17+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
PointLightMaterial = SharedContent.WatchMaterial("effects/pointLightEffect");
1+
protected override void LoadContent()
2+
{
3+
base.LoadContent();
4+
5+
// ...
6+
7+
PointLightMaterial = SharedContent.WatchMaterial("effects/pointLightEffect");
8+
9+
// ...
10+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
PointLightMaterial.Update();
1+
protected override void Update(GameTime gameTime)
2+
{
3+
// ...
4+
5+
PointLightMaterial.Update();
6+
7+
// ...
8+
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
_lights.Add(new PointLight
2-
{
3-
Position = new Vector2(300,300)
4-
});
1+
public override void Initialize()
2+
{
3+
// ...
4+
5+
_lights.Add(new PointLight
6+
{
7+
Position = new Vector2(300, 300)
8+
});
9+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
PointLight.Draw(Core.SpriteBatch, _lights);
1+
public override void Draw(GameTime gameTime)
2+
{
3+
// ...
4+
5+
// start rendering the lights
6+
_deferredRenderer.StartLightPhase();
7+
PointLight.Draw(Core.SpriteBatch, _lights);
8+
9+
// ...
10+
}

articles/tutorials/advanced/2d_shaders/08_light_effect/snippets/snippet-8-23.hlsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ float4 MainPS(VertexShaderOutput input) : COLOR
55
float falloff = saturate(.5 - dist);
66

77
return float4(falloff, 0, 0, 1);
8-
9-
}
8+
}

articles/tutorials/advanced/2d_shaders/08_light_effect/snippets/snippet-8-24.hlsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ float4 MainPS(VertexShaderOutput input) : COLOR
77
float falloff = saturate(.5 - dist) * (LightBrightness + 1);
88

99
return float4(falloff, 0, 0, 1);
10-
1110
}

0 commit comments

Comments
 (0)