Skip to content

Commit f3003f8

Browse files
committed
tracking 5.4; some highlighting
1 parent b4faa2c commit f3003f8

File tree

8 files changed

+86
-55
lines changed

8 files changed

+86
-55
lines changed

articles/tutorials/advanced/2d_shaders/05_transition_effect/index.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ Add an override to the `LoadContent` method, and load the `sceneTransitionEffect
3939

4040
Make sure to call the `Core`'s version of `LoadContent()` from the `Game1` class. In the previous tutorial, the method was left without calling the base method:
4141

42-
[!code-csharp[](./snippets/snippet-5-03.cs)]
42+
[!code-csharp[](./snippets/snippet-5-03.cs?highlight=4)]
4343

44-
To benefit from hot-reload, we need to update the effect in the `Core`'s `Update()` loop:
44+
And as we develop the effect, enable the debug UI:
4545

46-
[!code-csharp[](./snippets/snippet-5-04.cs)]
46+
[!code-csharp[](./snippets/snippet-5-05.cs?highlight=5)]
4747

48-
And as we develop the effect, enable the debug UI:
48+
To benefit from hot-reload, we need to update the effect in the `Core`'s `Update()` loop:
4949

50-
[!code-csharp[](./snippets/snippet-5-05.cs)]
50+
[!code-csharp[](./snippets/snippet-5-04.cs?highlight=6)]
5151

5252
If you run the game now, you should have a blank debug UI.
5353

@@ -63,19 +63,19 @@ Currently, the shader is compiling and loading into the game, but it isn't being
6363

6464
And initialize it in the `Initialize()` method:
6565

66-
[!code-csharp[](./snippets/snippet-5-07.cs)]
66+
[!code-csharp[](./snippets/snippet-5-07.cs?highlight=7-8)]
6767

6868
> [!tip]
6969
> The `Pixel` property is a texture we can re-use for many effects, and it is helpful to have for debugging purposes.
7070
7171
In the `Core'`s `Draw()` method, use the `SpriteBatch` to draw a full screen sprite using the `Pixel` property. Make sure to put this code after the `s_activeScene` is drawn, because the scene transition effect should _cover_ whatever was rendered from the current scene:
7272

73-
[!code-csharp[](./snippets/snippet-5-08.cs)]
73+
[!code-csharp[](./snippets/snippet-5-08.cs?highlight=10-12)]
7474

7575
If you run the game now, you will see a white background, because the `Pixel` sprite is rendering on top of the entire game scene and GUM UI.
7676

7777
> [!note]
78-
> We can still see the `ImGui.NET` debug UI because the debug UI is rendered in the `Core` class _after_ the current `Scene`'s `.Draw()` method finishes.
78+
> We can still see the `ImGui.NET` debug UI because the debug UI is rendered _after_ the current drawing the `Pixel`.
7979
8080
| ![Figure 5-4: The shader is being used to render a white full screen quad](./images/white.png) |
8181
| :--------------------------------------------------------------------------------------------: |
@@ -85,11 +85,11 @@ If you run the game now, you will see a white background, because the `Pixel` sp
8585

8686
We need to be able to control the progress of the screen transition effect. Add the following parameter to the shader:
8787

88-
[!code-hlsl[](./snippets/snippet-5-09.hlsl)]
88+
[!code-hlsl[](./snippets/snippet-5-09.hlsl?highlight=12)]
8989

90-
And recall that unless the `Progress` parameter is actually _used_ somehow in the calculation of the output of the shader, it will be optimized out of the final compilation. So, for now, lets make the shader return the `Progress` value in the red value of the color:
90+
And recall that unless the `Progress` parameter is actually _used_ somehow in the calculation of the output of the shader, it will be optimized out of the final compilation. So, for now, we will make the shader return the `Progress` value in the red value of the color:
9191

92-
[!code-hlsl[](./snippets/snippet-5-10.hlsl)]
92+
[!code-hlsl[](./snippets/snippet-5-10.hlsl?highlight=9)]
9393

9494
Now you can use the slider in the debug UI to visualize the `Progress` parameter as the red channel.
9595

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
// Check if the scene transition material needs to be reloaded.
2-
SceneTransitionMaterial.Update();
1+
protected override void Update(GameTime gameTime)
2+
{
3+
// ...
4+
5+
// Check if the scene transition material needs to be reloaded.
6+
SceneTransitionMaterial.Update();
7+
8+
base.Update(gameTime);
9+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
SceneTransitionMaterial.IsDebugVisible = true;
1+
protected override void LoadContent()
2+
{
3+
base.LoadContent();
4+
SceneTransitionMaterial = Content.WatchMaterial("effects/sceneTransitionEffect.fx");
5+
SceneTransitionMaterial.IsDebugVisible = true;
6+
}
7+
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
// Create a 1x1 white pixel texture for drawing quads.
2-
Pixel = new Texture2D(GraphicsDevice, 1, 1);
3-
Pixel.SetData(new Color[]{ Color.White });
1+
protected override void Initialize()
2+
{
3+
base.Initialize();
4+
// ...
5+
6+
// Create a 1x1 white pixel texture for drawing quads.
7+
Pixel = new Texture2D(GraphicsDevice, 1, 1);
8+
Pixel.SetData(new Color[]{ Color.White });
9+
}
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
// Draw the scene transition quad
2-
SpriteBatch.Begin(effect: SceneTransitionMaterial.Effect);
3-
SpriteBatch.Draw(Pixel, GraphicsDevice.Viewport.Bounds, Color.White);
4-
SpriteBatch.End();
1+
protected override void Draw(GameTime gameTime)
2+
{
3+
// If there is an active scene, draw it.
4+
if (s_activeScene != null)
5+
{
6+
s_activeScene.Draw(gameTime);
7+
}
8+
9+
// Draw the scene transition quad
10+
SpriteBatch.Begin(effect: SceneTransitionMaterial.Effect);
11+
SpriteBatch.Draw(Pixel, GraphicsDevice.Viewport.Bounds, Color.White);
12+
SpriteBatch.End();
13+
14+
Material.DrawVisibleDebugUi(gameTime);
15+
16+
base.Draw(gameTime);
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1+
#if OPENGL
2+
#define SV_POSITION POSITION
3+
#define VS_SHADERMODEL vs_3_0
4+
#define PS_SHADERMODEL ps_3_0
5+
#else
6+
#define VS_SHADERMODEL vs_4_0_level_9_1
7+
#define PS_SHADERMODEL ps_4_0_level_9_1
8+
#endif
9+
10+
// ...
11+
112
float Progress;
13+
14+
// ...
15+
16+
float4 MainPS(VertexShaderOutput input) : COLOR
17+
{
18+
// ...
19+
}
Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
1-
#if OPENGL
2-
#define SV_POSITION POSITION
3-
#define VS_SHADERMODEL vs_3_0
4-
#define PS_SHADERMODEL ps_3_0
5-
#else
6-
#define VS_SHADERMODEL vs_4_0_level_9_1
7-
#define PS_SHADERMODEL ps_4_0_level_9_1
8-
#endif
9-
10-
Texture2D SpriteTexture;
11-
1+
// ...
2+
123
float Progress;
13-
14-
sampler2D SpriteTextureSampler = sampler_state
15-
{
16-
Texture = <SpriteTexture>;
17-
};
18-
19-
struct VertexShaderOutput
20-
{
21-
float4 Position : SV_POSITION;
22-
float4 Color : COLOR0;
23-
float2 TextureCoordinates : TEXCOORD0;
24-
};
25-
4+
5+
// ...
6+
267
float4 MainPS(VertexShaderOutput input) : COLOR
278
{
289
return float4(Progress, 0, 0, 1);
2910
}
3011

31-
32-
technique SpriteDrawing
33-
{
34-
pass P0
35-
{
36-
PixelShader = compile PS_SHADERMODEL MainPS();
37-
}
38-
};
12+
// ...
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
float2 uv = input.TextureCoordinates;
2-
return float4(uv.x, 0, 0, 1);
1+
// ...
2+
3+
float4 MainPS(VertexShaderOutput input) : COLOR
4+
{
5+
float2 uv = input.TextureCoordinates;
6+
return float4(uv.x, 0, 0, 1);
7+
}
8+
9+
// ...

0 commit comments

Comments
 (0)