Skip to content

Commit 6154cdb

Browse files
committed
some 07 snippets
1 parent efc1869 commit 6154cdb

File tree

8 files changed

+77
-15
lines changed

8 files changed

+77
-15
lines changed

articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect/index.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ This is the _input_ struct for the standard pixel shaders from previous chapters
9292

9393
### Matrix Transform
9494

95-
The default sprite vertex shader uses this line:
95+
The default sprite vertex shader uses a [`mul()`](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-mul) expression:
9696

97-
[!code-hlsl[](./snippets/snippet-7-07.hlsl)]
97+
[!code-hlsl[](./snippets/snippet-7-07.hlsl?highlight=8)]
9898

9999
The reason this line exists is to convert the vertices from world-space to clip-space.
100100

@@ -133,20 +133,20 @@ Follow along with the steps to set up the effect.
133133

134134
1. We need to add a vertex shader function. To do that, we need a new `struct` that holds all the input semantics passed from `SpriteBatch`:
135135

136+
[!code-hlsl[](./snippets/snippet-7-09.hlsl)]
137+
136138
> [!tip]
137139
> Use a struct for inputs and outputs.
138140
>
139141
> The default vertex shader accepts all 3 inputs (`position`, `color`, and `texCoord`) as direct parameters. However, when you have more than 1 semantic, it is helpful to organize all of the inputs in a `struct`.
140142
141-
[!code-hlsl[](./snippets/snippet-7-09.hlsl)]
142-
143143
2. Now add the stub for the vertex shader function:
144144

145145
[!code-hlsl[](./snippets/snippet-7-10.hlsl)]
146146

147147
3. And finally modify the `technique` to _include_ the vertex shader function. Until now, the `MainVS()` function is just considered as any average function in your shader, and since it wasn't used from the `MainPS` pixel shader, it would be compiled out of the shader. When you specify the `MainVS()` function as the vertex shader function, you are overriding the default `SpriteBatch` vertex shader function:
148148

149-
[!code-hlsl[](./snippets/snippet-7-11.hlsl)]
149+
[!code-hlsl[](./snippets/snippet-7-11.hlsl?highlight=6)]
150150

151151
4. The shader will not compile yet, because the `VertexShaderOutput` has not been completely initialized. We need to replicate the `MatrixTransform` step to convert the vertices from world-space to clip-space.
152152
Add the `MatrixTransform` shader parameter:
@@ -187,7 +187,7 @@ As a quick experiment, we can show that the vertex shader can indeed modify the
187187

188188
And change the vertex shader to add the `DebugOffset` to the `output.Position` after the clip-space conversion:
189189

190-
[!code-hlsl[](./snippets/snippet-7-19.hlsl)]
190+
[!code-hlsl[](./snippets/snippet-7-19.hlsl?highlight=7)]
191191

192192
The sprites now move around as we adjust the shader parameter values.
193193

@@ -197,7 +197,7 @@ The sprites now move around as we adjust the shader parameter values.
197197

198198
It is important to build intuition for the different coordinate systems involved. Instead of adding the `DebugOffset` _after_ the clip-space conversion, if you try to add it _before_, like in the code below:
199199

200-
[!code-hlsl[](./snippets/snippet-7-20.hlsl)]
200+
[!code-hlsl[](./snippets/snippet-7-20.hlsl?highlight=7)]
201201

202202
Then you will not see much movement at all. This is because the `DebugOffset` values only go from `0` to `1`, and in world space, this really only amounts to a single pixel. In fact, exactly how much an addition of _`1`_ happens to make is entirely defined _by_ the conversion to clip-space. The `projection` matrix we created treats world space coordinates with an origin around the screen's center, where 1 unit maps to 1 pixel. Sometimes this is exactly what you want, and sometimes it can be confusing.
203203

@@ -211,13 +211,15 @@ The world-space vertices can have their `x` and `y` values modified in the verte
211211

212212
To check, try modify the shader code to adjust the `z` value based on one of the debug values:
213213

214-
[!code-hlsl[](./snippets/snippet-7-21.hlsl)]
214+
[!code-hlsl[](./snippets/snippet-7-21.hlsl?highlight=7)]
215215

216216
> [!tip]
217217
> Near and Far plane clipping.
218218
>
219219
> Keep in mind that if you modify the `z` value _too_ much, it will likely step outside of the near and far planes of the orthographic projection matrix. If this happens, the sprite will vanish, because it the projection matrix does not handle coordinates outside of the near and far planes. In the example above, they were defined as `0` and `-1`.
220+
>
220221
> [!code-csharp[](./snippets/snippet-7-22.cs)]
222+
>
221223
222224
Nothing happens!
223225

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
output.position = mul(position, MatrixTransform);
1+
// ...
2+
3+
VSOutput SpriteVertexShader( float4 position : POSITION0,
4+
float4 color : COLOR0,
5+
float2 texCoord : TEXCOORD0)
6+
{
7+
VSOutput output;
8+
output.position = mul(position, MatrixTransform);
9+
output.color = color;
10+
output.texCoord = texCoord;
11+
return output;
12+
}
13+
14+
// ...
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
// ...
2+
13
float4x4 MatrixTransform;
4+
5+
// ...

articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect/snippets/snippet-7-13.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ...
2+
13
VertexShaderOutput MainVS(VertexShaderInput input)
24
{
35
VertexShaderOutput output;
@@ -6,3 +8,5 @@ VertexShaderOutput MainVS(VertexShaderInput input)
68
output.TextureCoordinates = input.TexCoord;
79
return output;
810
}
11+
12+
// ...
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
// ...
2+
13
float2 DebugOffset;
4+
5+
// ...
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
output.Position = mul(input.Position, MatrixTransform);
2-
output.Position.xy += DebugOffset;
1+
// ...
2+
3+
VertexShaderOutput MainVS(VertexShaderInput input)
4+
{
5+
VertexShaderOutput output;
6+
output.Position = mul(input.Position, MatrixTransform);
7+
output.Position.xy += DebugOffset;
8+
output.Color = input.Color;
9+
output.TextureCoordinates = input.TexCoord;
10+
return output;
11+
}
12+
13+
// ...
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
float4 pos = input.Position;
2-
pos.xy += DebugOffset;
3-
output.Position = mul(position, MatrixTransform);
1+
// ...
2+
3+
VertexShaderOutput MainVS(VertexShaderInput input)
4+
{
5+
VertexShaderOutput output;
6+
float4 pos = input.Position;
7+
pos.xy += DebugOffset;
8+
output.Position = mul(pos, MatrixTransform);
9+
output.Color = input.Color;
10+
output.TextureCoordinates = input.TexCoord;
11+
return output;
12+
}
13+
14+
// ...
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
pos.z -= DebugOffset.x;
1+
// ...
2+
3+
VertexShaderOutput MainVS(VertexShaderInput input)
4+
{
5+
VertexShaderOutput output;
6+
float4 pos = input.Position;
7+
pos.z -= DebugOffset.x;
8+
output.Position = mul(pos, MatrixTransform);
9+
output.Color = input.Color;
10+
output.TextureCoordinates = input.TexCoord;
11+
return output;
12+
}
13+
14+
// ...

0 commit comments

Comments
 (0)