Skip to content

Commit 6c0c989

Browse files
committed
highlights for 08
1 parent 0517ea0 commit 6c0c989

File tree

8 files changed

+61
-23
lines changed

8 files changed

+61
-23
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,23 +430,23 @@ When each individual light is drawn into the `LightBuffer`, it needs to use the
430430

431431
Create a new struct in the `pointLightEffect.fx` file:
432432

433-
[!code-hlsl[](./snippets/snippet-8-57.hlsl)]
433+
[!code-hlsl[](./snippets/snippet-8-57.hlsl?highlight=6)]
434434

435435
6. Then, create a new vertex function that uses the new `LightVertexShaderOutput`. This function will call to the existing `MainVS` function that does the 3d effect, and add the screen coordinates afterwards:
436436

437437
[!code-hlsl[](./snippets/snippet-8-58.hlsl)]
438438

439439
7. Make sure to update the `technique` to use the new vertex function:
440440

441-
[!code-hlsl[](./snippets/snippet-8-59.hlsl)]
441+
[!code-hlsl[](./snippets/snippet-8-59.hlsl?highlight=5)]
442442

443443
8. In the pixel function, to visualize the screen coordinates, we will short-circuit the existing light code and just render out the screen coordinates. First, modify the input of the pixel function to be the `LightVertexShaderOutput` struct that was returned from the `LightVS` vertex function:
444444

445445
[!code-hlsl[](./snippets/snippet-8-60.hlsl)]
446446

447447
9. And make the function immediately return the screen coordinates in the red and green channel:
448448

449-
[!code-hlsl[](./snippets/snippet-8-61.hlsl)]
449+
[!code-hlsl[](./snippets/snippet-8-61.hlsl?highlight=3)]
450450

451451
10. Be careful, if you run the game now, it will not look right. We need to make sure to send the `MatrixTransform` parameter from C# as well. In the `GameScene`'s `Update()` method, make sure to pass the `MatrixTransform` to _both_ the `_gameMaterial` _and_ the `Core.PointLightMaterial`.
452452

@@ -500,9 +500,12 @@ Now it is time to _use_ the normal data in conjunction with the light direction
500500

501501
[!code-hlsl[](./snippets/snippet-8-65.hlsl)]
502502

503+
> [!note]
504+
> The `normalDir`, `lightDir`, and [`dot`](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-dot) product are a simplified version of the [Blinn-Phong shading model](https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_reflection_model).
505+
503506
2. And then make the final color use the `lightAmount`:
504507

505-
[!code-hlsl[](./snippets/snippet-8-66.hlsl)]
508+
[!code-hlsl[](./snippets/snippet-8-66.hlsl?highlight=7)]
506509

507510
| ![Figure 8-22: The light with the normal](./images/light-with-normal.png) |
508511
| :-----------------------------------------------------------------------: |
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
public static void Draw(SpriteBatch spriteBatch, List<PointLight> pointLights, Texture2D normalBuffer)
2-
{
1+
public static void Draw(SpriteBatch spriteBatch, List<PointLight> pointLights, Texture2D normalBuffer)
2+
{
33
Core.PointLightMaterial.SetParameter("NormalBuffer", normalBuffer);
44
// ...
5+
6+
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
// start rendering the lights
2-
_deferredRenderer.StartLightPhase();
3-
PointLight.Draw(Core.SpriteBatch, _lights, _deferredRenderer.NormalBuffer);
1+
public override void Draw(GameTime gameTime)
2+
{
3+
// ...
4+
5+
// start rendering the lights
6+
_deferredRenderer.StartLightPhase();
7+
PointLight.Draw(Core.SpriteBatch, _lights, _deferredRenderer.NormalBuffer);
8+
9+
// ...
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
float4 MainPS(LightVertexShaderOutput input) : COLOR
2+
{
3+
// ...
4+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
return float4(input.ScreenCoordinates.xy, 0, 1);
1+
float4 MainPS(LightVertexShaderOutput input) : COLOR
2+
{
3+
return float4(input.ScreenCoordinates.xy, 0, 1);
4+
}
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
float4 normal = tex2D(NormalBufferSampler,input.ScreenCoordinates);
2-
3-
// convert from [0,1] to [-1,1]
4-
float3 normalDir = (normal.xyz-.5)*2;
5-
6-
// find the direction the light is travelling at the current pixel
7-
float3 lightDir = float3( normalize(input.TextureCoordinates - .5), 1);
8-
9-
// how much is the normal direction pointing towards the light direction?
10-
float lightAmount = saturate(dot(normalDir, lightDir));
1+
float4 MainPS(LightVertexShaderOutput input) : COLOR
2+
{
3+
// ...
4+
5+
float4 normal = tex2D(NormalBufferSampler,input.ScreenCoordinates);
6+
7+
// convert from [0,1] to [-1,1]
8+
float3 normalDir = (normal.xyz-.5)*2;
9+
10+
// find the direction the light is travelling at the current pixel
11+
float3 lightDir = float3( normalize(input.TextureCoordinates - .5), 1);
12+
13+
// how much is the normal direction pointing towards the light direction?
14+
float lightAmount = saturate(dot(normalDir, lightDir));
15+
16+
// ...
17+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
color.a *= falloff * lightAmount;
1+
2+
float4 MainPS(LightVertexShaderOutput input) : COLOR
3+
{
4+
// ...
5+
6+
float4 color = input.Color;
7+
color.a *= falloff * lightAmount;
8+
return color;
9+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
// Move some lights around for artistic effect
2-
MoveLightsAround(gameTime);
1+
public override void Update(GameTime gameTime)
2+
{
3+
// ...
4+
5+
// Move some lights around for artistic effect
6+
MoveLightsAround(gameTime);
7+
}

0 commit comments

Comments
 (0)