85
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L11 – Shaders Part 2

XNA L11–Shaders Part 2

Embed Size (px)

Citation preview

Page 1: XNA L11–Shaders Part 2

Mohammad Shakermohammadshaker.com

@ZGTRShaker2011, 2012, 2013, 2014

XNA Game DevelopmentL11 – Shaders Part 2

Page 2: XNA L11–Shaders Part 2

Experimenting With Shaders

Page 3: XNA L11–Shaders Part 2

Experimenting With Shaders

Page 4: XNA L11–Shaders Part 2

Notice the rasterizer between the vertex and the pixel shader

Page 5: XNA L11–Shaders Part 2

This rasterizer determines which pixels on the screen are occupied by our triangle, and makes sure these pixels are also sent to the pixel shader.

Page 6: XNA L11–Shaders Part 2

Without this rasterizer, only the 3 points corresponding to the vertices would be sent to the pixel shader!!

Page 7: XNA L11–Shaders Part 2

The interpolator next to the rasterizer calculates this value, by interpolating the color value of the corner points.

Page 8: XNA L11–Shaders Part 2

This means that a pixel exactly in the middle between a blue and a red corner point, will get the color that is exactly in the middle of these colors.

Page 9: XNA L11–Shaders Part 2

It will look like?!

Page 10: XNA L11–Shaders Part 2

This!

Page 11: XNA L11–Shaders Part 2

Nice!

Page 12: XNA L11–Shaders Part 2

With HLSL,you could change the position or color of each vertex by all means!

Page 13: XNA L11–Shaders Part 2

You could also do this in your XNA app, but then your CPU would have to perform those calculations, which would lower your framerate!

Page 14: XNA L11–Shaders Part 2

Now you can have these calculations done by the GPU, which is A LOT faster at it, leaving your CPU free to perform more important calculations

Page 15: XNA L11–Shaders Part 2

As you have seen,Using the vertex shader, you could also adjust the color, which we’ve done before (we made our whole triangle white)

Page 16: XNA L11–Shaders Part 2

So for this example, we will throw away the color information provided to us by the vertex stream, and define our own colors. Say, we want our vertex shader make the red color component indicate the X coordinate of each vertex, the green component the Y coordinate, and the blue color component indicate the z coordinate.

Page 17: XNA L11–Shaders Part 2

Which is this output!

Page 18: XNA L11–Shaders Part 2

Let’s go!

Page 19: XNA L11–Shaders Part 2

Experimenting With Shaders

• Go to our last HLSL file “.fx”– Replace the following line

Output.Color = inColor;

Page 20: XNA L11–Shaders Part 2

Experimenting With Shaders

• Go to our last HLSL file “.fx”– Replace the following line

– With this one

Output.Color = inColor;

Output.Color.rgb = inPos.xyz;

Page 21: XNA L11–Shaders Part 2

Experimenting With Shaders

• Go to our last HLSL file “.fx”– Replace the following line

– With this one

Output.Color = inColor;

Output.Color.rgb = inPos.xyz;

Page 22: XNA L11–Shaders Part 2

Experimenting With Shaders

• Go to our last HLSL file “.fx”– Replace the following line

– With this one

Output.Color = inColor;

Output.Color.rgb = inPos.xyz;

Page 23: XNA L11–Shaders Part 2

Experimenting With Shaders

• Go to our last HLSL file “.fx”– Replace the following line

– With this one

Output.Color = inColor;

Output.Color.rgb = inPos.xyz;

Page 24: XNA L11–Shaders Part 2

Experimenting With Shaders

• Go to our last HLSL file “.fx”– Replace the following line

– With this one

Output.Color = inColor;

Output.Color.rgb = inPos.xyz;

Page 25: XNA L11–Shaders Part 2

Experimenting With Shaders

• Now, Compile and run to see the following

• “App4-NotGoodInterpolation”

Page 26: XNA L11–Shaders Part 2

Experimenting With Shaders

• Now, Compile and run to see the following

Page 27: XNA L11–Shaders Part 2

Experimenting With Shaders

• Now, Compile and run to see the following

Page 28: XNA L11–Shaders Part 2

Experimenting With Shaders

• Now, Compile and run to see the following

Page 29: XNA L11–Shaders Part 2

Experimenting With Shaders

• So what is happening?

• Before the colors are passed to the interpolator, the 3 color values of the 3 vertices are being clipped to the [0,1] region. For example, the (-2,-2,2) vertex should have -2, -2 and 2 as rgb color values, but it gets 0, 0 and 1 as color values

Page 30: XNA L11–Shaders Part 2

Experimenting With Shaders

• So what is happening?

• The (0,0,0) point gets a color value that is an interpolation of color values between the [0,1] region, and thus will never be completely 0,0,0 (=black).

Page 31: XNA L11–Shaders Part 2

Experimenting With ShadersVertexToPixel SimplestVertexShader( float4 inPos : POSITION,

float4 inColor : COLOR0)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color.rgb = inPos.xyz;

return Output;

}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)

{

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;

}

technique Simplest

{

pass Pass0

{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}

}

float4x4 xViewProjection;

struct VertexToPixel

{

float4 Position : POSITION;

float4 Color : COLOR0;

};

struct PixelToFrame

{

float4 Color : COLOR0;

};

Page 32: XNA L11–Shaders Part 2

Experimenting With ShadersVertexToPixel SimplestVertexShader( float4 inPos : POSITION,

float4 inColor : COLOR0)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color.rgb = inPos.xyz;

return Output;

}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)

{

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;

}

technique Simplest

{

pass Pass0

{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}

}

float4x4 xViewProjection;

struct VertexToPixel

{

float4 Position : POSITION;

float4 Color : COLOR0;

};

struct PixelToFrame

{

float4 Color : COLOR0;

};

Page 33: XNA L11–Shaders Part 2

Experimenting With Shaders

Page 34: XNA L11–Shaders Part 2

Experimenting With Shaders

Page 35: XNA L11–Shaders Part 2

Experimenting With ShadersVertexToPixel SimplestVertexShader( float4 inPos : POSITION,

float4 inColor : COLOR0)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color.rgb = inPos.xyz;

return Output;

}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)

{

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

return Output;

}

technique Simplest

{

pass Pass0

{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}

}

float4x4 xViewProjection;

struct VertexToPixel

{

float4 Position : POSITION;

float4 Color : COLOR0;

};

struct PixelToFrame

{

float4 Color : COLOR0;

};

Page 36: XNA L11–Shaders Part 2

Experimenting With ShadersVertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color = inColor;

Output.Position3D = inPos;

return Output;

}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)

{

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

Output.Color.rgb = PSIn.Position3D.xyz;

return Output;

}

technique Simplest

{

pass Pass0

{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}

}

float4x4 xViewProjection;

struct VertexToPixel

{

float4 Position : POSITION;

float4 Color : COLOR0;

float3 Position3D : TEXCOORD0;

};

struct PixelToFrame

{

float4 Color : COLOR0;

};

Page 37: XNA L11–Shaders Part 2

Experimenting With ShadersVertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color = inColor;

Output.Position3D = inPos;

return Output;

}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)

{

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

Output.Color.rgb = PSIn.Position3D.xyz;

return Output;

}

technique Simplest

{

pass Pass0

{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}

}

float4x4 xViewProjection;

struct VertexToPixel

{

float4 Position : POSITION;

float4 Color : COLOR0;

float3 Position3D : TEXCOORD0;

};

struct PixelToFrame

{

float4 Color : COLOR0;

};

Page 38: XNA L11–Shaders Part 2

Experimenting With ShadersVertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color = inColor;

Output.Position3D = inPos;

return Output;

}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)

{

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

Output.Color.rgb = PSIn.Position3D.xyz;

return Output;

}

technique Simplest

{

pass Pass0

{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}

}

float4x4 xViewProjection;

struct VertexToPixel

{

float4 Position : POSITION;

float4 Color : COLOR0;

float3 Position3D : TEXCOORD0;

};

struct PixelToFrame

{

float4 Color : COLOR0;

};

Page 39: XNA L11–Shaders Part 2

Experimenting With ShadersVertexToPixel SimplestVertexShader( float4 inPos : POSITION, float4 inColor : COLOR0)

{ VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xViewProjection);

Output.Color = inColor;

Output.Position3D = inPos;

return Output;

}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)

{

PixelToFrame Output = (PixelToFrame)0;

Output.Color = PSIn.Color;

Output.Color.rgb = PSIn.Position3D.xyz;

return Output;

}

technique Simplest

{

pass Pass0

{

VertexShader = compile vs_2_0 SimplestVertexShader();

PixelShader = compile ps_2_0 OurFirstPixelShader();

}

}

float4x4 xViewProjection;

struct VertexToPixel

{

float4 Position : POSITION;

float4 Color : COLOR0;

float3 Position3D : TEXCOORD0;

};

struct PixelToFrame

{

float4 Color : COLOR0;

};

Page 40: XNA L11–Shaders Part 2

Experimenting With Shaders

• “App5-GoodInterpolation”

Page 41: XNA L11–Shaders Part 2

Shaders - Samples

Page 42: XNA L11–Shaders Part 2

Shaders - Samples

• Texturing our triangle using the Pixel Shader

• Read more– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Textured_triangle.php

Page 43: XNA L11–Shaders Part 2

Shaders - Samples

• A logical next step would be to load a texture from within our XNA app, and have our pixel shader sample the correct color for each pixel.

Page 44: XNA L11–Shaders Part 2

Shaders - Samples

• In Game1 class

struct MyOwnVertexFormat{

private Vector3 position;private Vector2 texCoord;

public MyOwnVertexFormat(Vector3 position, Vector2 texCoord){

this.position = position;this.texCoord = texCoord;

}}

Page 45: XNA L11–Shaders Part 2

Shaders - Samples

• At the top of our effects - HLSL file

sampler TextureSampler = sampler_state

{

texture = <xTexture>;

magfilter = LINEAR;

minfilter = LINEAR;

mipfilter=LINEAR;

AddressU = mirror;

AddressV = mirror;

};

Page 46: XNA L11–Shaders Part 2

Shaders - Samples

• Higher performance by using Triangle Strips

• Read more– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Triangle_strip.php

Page 47: XNA L11–Shaders Part 2

Shaders - Samples

• Acting consciously and sneaky :D

Page 48: XNA L11–Shaders Part 2

Shaders - Samples

• Acting consciously and sneaky :D

Page 49: XNA L11–Shaders Part 2

Shaders - Samples

Page 50: XNA L11–Shaders Part 2

Shaders - Samples

• CLEVER!

Page 51: XNA L11–Shaders Part 2

Shaders - Samples

• CLEVER!

Page 52: XNA L11–Shaders Part 2

Shaders - Samples

• CLEVER!

Only 12 vertices are needed to define 10 triangles!

Page 53: XNA L11–Shaders Part 2

Shaders - Samples

Page 54: XNA L11–Shaders Part 2

Shaders - Samples

• Creating your first light

• Read more– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Per-pixel_lighting.php

Page 55: XNA L11–Shaders Part 2

Shaders - Samples

• Every object is lit by an amount of light, which depends on the angle between the normal and the direction of the light.

Page 56: XNA L11–Shaders Part 2

Shaders - Samples

• Every object is lit by an amount of light, which depends on the angle between the normal and the direction of the light.

• This is found by taking the dot product between that object’s normal and the direction of the incoming light.

Page 57: XNA L11–Shaders Part 2

Shaders - Samples

float DotProduct(float3 lightPos, float3 pos3D, float3 normal){

float3 lightDir = normalize(pos3D - lightPos);return dot(-lightDir, normal);

}

Page 58: XNA L11–Shaders Part 2

Shaders - Samples

struct VertexToPixel{

float4 Position : POSITION;float2 TexCoords : TEXCOORD0;float3 Normal : TEXCOORD1;float3 Position3D : TEXCOORD2;

};

Page 59: XNA L11–Shaders Part 2

Shaders - Samples

Output.Normal = normalize(mul(inNormal, (float3x3)xWorld));Output.Position3D = mul(inPos, xWorld);

Page 60: XNA L11–Shaders Part 2

Shaders - Samples

Page 61: XNA L11–Shaders Part 2

Shaders - Samples

• Shadow Mapping

Page 62: XNA L11–Shaders Part 2

Shaders - Samples

• Shadow Mapping algorithm

• Read more:http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shadow_map.php

Page 63: XNA L11–Shaders Part 2

Shaders - Samples

• Shadow Mapping algorithm

Page 64: XNA L11–Shaders Part 2

Shaders - Samples

private void DrawScene(string technique){

effect.CurrentTechnique = effect.Techniques[technique];effect.Parameters["xWorldViewProjection"].SetValue(Matrix.Identity * viewMatrix * projectionMatrix);effect.Parameters["xTexture"].SetValue(streetTexture);effect.Parameters["xWorld"].SetValue(Matrix.Identity);effect.Parameters["xLightPos"].SetValue(lightPos);effect.Parameters["xLightPower"].SetValue(lightPower);effect.Parameters["xAmbient"].SetValue(ambientPower);effect.Parameters["xLightsWorldViewProjection"].SetValue(Matrix.Identity *

lightsViewProjectionMatrix);

foreach (EffectPass pass in effect.CurrentTechnique.Passes){

pass.Apply();

device.SetVertexBuffer(vertexBuffer);device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 16);

}

Page 65: XNA L11–Shaders Part 2

Shaders - Samples

• Transforming vertices to texture space using Projective Texturing

• Reading more– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Projective_texturing.php

Page 66: XNA L11–Shaders Part 2

Shaders - Samples

Page 67: XNA L11–Shaders Part 2

Shaders - Samples

• Changing the shape of our light

• Reading morehttp://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shaping_the_light.php

Page 68: XNA L11–Shaders Part 2

Shaders - Samples

Page 69: XNA L11–Shaders Part 2

Shaders - Samples

• Post-Processing Shaders

• Read more– http://rbwhitaker.wikidot.com/post-processing-effects

Page 70: XNA L11–Shaders Part 2

Shaders - Samples

• Post-Processing Shaders

float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0

{

float4 color = tex2D(TextureSampler, TextureCoordinate);

float value = (color.r + color.g + color.b) / 3;

color.r = value;

color.g = value;

color.b = value;

return color;

}

Page 71: XNA L11–Shaders Part 2

Shaders - Samples

Page 72: XNA L11–Shaders Part 2

Shaders - Samples

• Post-Processing Shaders

float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0

{

float4 color = tex2D(TextureSampler, TextureCoordinate);

float4 outputColor = color;

outputColor.r = (color.r * 0.393) + (color.g * 0.769) + (color.b * 0.189);

outputColor.g = (color.r * 0.349) + (color.g * 0.686) + (color.b * 0.168);

outputColor.b = (color.r * 0.272) + (color.g * 0.534) + (color.b * 0.131);

return outputColor;

}

Page 73: XNA L11–Shaders Part 2

Shaders - Samples

Page 74: XNA L11–Shaders Part 2

Shaders - Samples

• Transparency

• Read more– http://rbwhitaker.wikidot.com/transparency-shader

Page 75: XNA L11–Shaders Part 2

Shaders - Samples

technique Technique1

{

pass Pass1

{

AlphaBlendEnable = TRUE;

DestBlend = INVSRCALPHA;

SrcBlend = SRCALPHA;

VertexShader = compile vs_1_1 VertexShaderFunction();

PixelShader = compile ps_2_0 PixelShaderFunction();

}

}

Page 76: XNA L11–Shaders Part 2

Shaders - Samples

VertexShaderOutput VertexShaderFunction(VertexShaderInput input){

VertexShaderOutput output;

float4 worldPosition = mul(input.Position, World);float4 viewPosition = mul(worldPosition, View);output.Position = mul(viewPosition, Projection);

float4 normal = normalize(mul(input.Normal, WorldInverseTranspose));float lightIntensity = dot(normal, DiffuseLightDirection);output.Color = saturate(DiffuseColor * DiffuseIntensity * lightIntensity);

output.Normal = normal;

output.TextureCoordinate = input.TextureCoordinate;return output;

}

Page 77: XNA L11–Shaders Part 2

Shaders - Samples

• Creating a Toon Shader

• Read more– http://rbwhitaker.wikidot.com/toon-shader

Page 78: XNA L11–Shaders Part 2

Shaders - Samples

• Intensity

• Light

• Normals

Page 79: XNA L11–Shaders Part 2

Shaders - Samples

• Intensity

• Light

• Normals

• Then, SHADING!

Page 80: XNA L11–Shaders Part 2

Shaders - Samples

• Reflection Shader

• Read more– http://rbwhitaker.wikidot.com/reflection-shader

Page 81: XNA L11–Shaders Part 2

Shaders - Samples

• Creating a Diffuse Lighting Shader

• Read more– http://rbwhitaker.wikidot.com/diffuse-lighting-shader

Page 82: XNA L11–Shaders Part 2

We Are Done For Today!End of Course!

Page 83: XNA L11–Shaders Part 2

Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR

Available courses to the date of this slide:

Page 84: XNA L11–Shaders Part 2

http://www.mohammadshaker.com

[email protected]

https://twitter.com/ZGTRShaker @ZGTRShaker

https://de.linkedin.com/pub/mohammad-shaker/30/122/128/

http://www.slideshare.net/ZGTRZGTR

https://www.goodreads.com/user/show/11193121-mohammad-shaker

https://plus.google.com/u/0/+MohammadShaker/

https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA

http://mohammadshakergtr.wordpress.com/

Page 85: XNA L11–Shaders Part 2