SlideShare a Scribd company logo
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2011, 2012, 2013, 2014
XNA Game Development
L11 – Shaders Part 2
Experimenting With Shaders
Experimenting With Shaders
Notice the rasterizer
between the vertex and
the pixel shader
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.
Without this rasterizer,
only the 3 points
corresponding to the
vertices would be sent to
the pixel shader!!
The interpolator next to
the rasterizer calculates
this value, by interpolating
the color value of the
corner points.
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.
It will look like?!
This!
Nice!
With HLSL,
you could change the
position or color of each
vertex by all means!
You could also do this
in your XNA app, but
then your CPU would
have to perform those
calculations, which
would lower your
framerate!
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
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)
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.
Which is this output!
Let’s go!
Experimenting With Shaders
• Go to our last HLSL file “.fx”
– Replace the following line
Output.Color = inColor;
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;
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;
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;
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;
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;
Experimenting With Shaders
• Now, Compile and run to see the following
• “App4-NotGoodInterpolation”
Experimenting With Shaders
• Now, Compile and run to see the following
Experimenting With Shaders
• Now, Compile and run to see the following
Experimenting With Shaders
• Now, Compile and run to see the following
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
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).
Experimenting With Shaders
VertexToPixel 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;
};
Experimenting With Shaders
VertexToPixel 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;
};
Experimenting With Shaders
Experimenting With Shaders
Experimenting With Shaders
VertexToPixel 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;
};
Experimenting With Shaders
VertexToPixel 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;
};
Experimenting With Shaders
VertexToPixel 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;
};
Experimenting With Shaders
VertexToPixel 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;
};
Experimenting With Shaders
VertexToPixel 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;
};
Experimenting With Shaders
• “App5-GoodInterpolation”
Shaders - Samples
Shaders - Samples
• Texturing our triangle using the Pixel Shader
• Read more
– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Textured_triangle.php
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.
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;
}
}
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;
};
Shaders - Samples
• Higher performance by using Triangle Strips
• Read more
– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Triangle_strip.php
Shaders - Samples
• Acting consciously and sneaky :D
Shaders - Samples
• Acting consciously and sneaky :D
Shaders - Samples
Shaders - Samples
• CLEVER!
Shaders - Samples
• CLEVER!
Shaders - Samples
• CLEVER!
Only 12 vertices are needed to
define 10 triangles!
Shaders - Samples
Shaders - Samples
• Creating your first light
• Read more
– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Per-pixel_lighting.php
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.
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.
Shaders - Samples
float DotProduct(float3 lightPos, float3 pos3D, float3 normal)
{
float3 lightDir = normalize(pos3D - lightPos);
return dot(-lightDir, normal);
}
Shaders - Samples
struct VertexToPixel
{
float4 Position : POSITION;
float2 TexCoords : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 Position3D : TEXCOORD2;
};
Shaders - Samples
Output.Normal = normalize(mul(inNormal, (float3x3)xWorld));
Output.Position3D = mul(inPos, xWorld);
Shaders - Samples
Shaders - Samples
• Shadow Mapping
Shaders - Samples
• Shadow Mapping algorithm
• Read more:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shadow_map.php
Shaders - Samples
• Shadow Mapping algorithm
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);
}
Shaders - Samples
• Transforming vertices to texture space using Projective Texturing
• Reading more
– http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Projective_texturing.php
Shaders - Samples
Shaders - Samples
• Changing the shape of our light
• Reading more
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shaping_the_light.php
Shaders - Samples
Shaders - Samples
• Post-Processing Shaders
• Read more
– http://rbwhitaker.wikidot.com/post-processing-effects
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;
}
Shaders - Samples
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;
}
Shaders - Samples
Shaders - Samples
• Transparency
• Read more
– http://rbwhitaker.wikidot.com/transparency-shader
Shaders - Samples
technique Technique1
{
pass Pass1
{
AlphaBlendEnable = TRUE;
DestBlend = INVSRCALPHA;
SrcBlend = SRCALPHA;
VertexShader = compile vs_1_1 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
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;
}
Shaders - Samples
• Creating a Toon Shader
• Read more
– http://rbwhitaker.wikidot.com/toon-shader
Shaders - Samples
• Intensity
• Light
• Normals
Shaders - Samples
• Intensity
• Light
• Normals
• Then, SHADING!
Shaders - Samples
• Reflection Shader
• Read more
– http://rbwhitaker.wikidot.com/reflection-shader
Shaders - Samples
• Creating a Diffuse Lighting Shader
• Read more
– http://rbwhitaker.wikidot.com/diffuse-lighting-shader
We Are Done For Today!
End of Course!
Take a Look on my other courses
@ http://www.slideshare.net/ZGTRZGTR
Available courses to the date of this slide:
http://www.mohammadshaker.com
mohammadshakergtr@gmail.com
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/
XNA L11–Shaders Part 2

More Related Content

What's hot (17)

PDF
Pythonic Graphics
Kirby Urner
 
PPTX
Python: The Iterator Pattern (Comprehensions)
Damian T. Gordon
 
PDF
Ur Domain Haz Monoids DDDx NYC 2014
Cyrille Martraire
 
PPTX
Type script by Howard
LearningTech
 
PPTX
TypeScript by Howard
LearningTech
 
PDF
Creative Coding 1 - 1 Introduction
Till Nagel
 
PPTX
Intro to Python (High School) Unit #2
Jay Coskey
 
PDF
Functor, Apply, Applicative And Monad
Oliver Daff
 
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Philip Schwarz
 
PDF
Monad Transformers In The Wild
StackMob Inc
 
PDF
First-Class Patterns
John De Goes
 
PDF
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
John De Goes
 
PDF
Introduction to functional programming using Ocaml
pramode_ce
 
PPTX
Kotlin as a Better Java
Garth Gilmour
 
PDF
C++ Windows Forms L08 - GDI P1
Mohammad Shaker
 
PDF
purely_functional_play_framework_application
Naoki Aoyama
 
PPT
Fantom on the JVM Devoxx09 BOF
Dror Bereznitsky
 
Pythonic Graphics
Kirby Urner
 
Python: The Iterator Pattern (Comprehensions)
Damian T. Gordon
 
Ur Domain Haz Monoids DDDx NYC 2014
Cyrille Martraire
 
Type script by Howard
LearningTech
 
TypeScript by Howard
LearningTech
 
Creative Coding 1 - 1 Introduction
Till Nagel
 
Intro to Python (High School) Unit #2
Jay Coskey
 
Functor, Apply, Applicative And Monad
Oliver Daff
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Philip Schwarz
 
Monad Transformers In The Wild
StackMob Inc
 
First-Class Patterns
John De Goes
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
John De Goes
 
Introduction to functional programming using Ocaml
pramode_ce
 
Kotlin as a Better Java
Garth Gilmour
 
C++ Windows Forms L08 - GDI P1
Mohammad Shaker
 
purely_functional_play_framework_application
Naoki Aoyama
 
Fantom on the JVM Devoxx09 BOF
Dror Bereznitsky
 

Viewers also liked (16)

PDF
Delphi L02 Controls P1
Mohammad Shaker
 
PDF
C# Advanced L09-HTML5+ASP
Mohammad Shaker
 
PDF
Indie Series 03: Becoming an Indie
Mohammad Shaker
 
PDF
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
Mohammad Shaker
 
PDF
C# Starter L07-Objects Cloning
Mohammad Shaker
 
PDF
WPF L03-3D Rendering and 3D Animation
Mohammad Shaker
 
PDF
Android L01 - Warm Up
Mohammad Shaker
 
PDF
C# Advanced L10-Workflow Foundation
Mohammad Shaker
 
PDF
WPF L01-Layouts, Controls, Styles and Templates
Mohammad Shaker
 
PDF
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
PDF
C# Advanced L08-Networking+WCF
Mohammad Shaker
 
PDF
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
PDF
C# Starter L06-Delegates, Event Handling and Extension Methods
Mohammad Shaker
 
PDF
Car Dynamics with ABS, ESP and GPS Systems
Mohammad Shaker
 
PDF
OpenGL Starter L02
Mohammad Shaker
 
PDF
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Delphi L02 Controls P1
Mohammad Shaker
 
C# Advanced L09-HTML5+ASP
Mohammad Shaker
 
Indie Series 03: Becoming an Indie
Mohammad Shaker
 
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
Mohammad Shaker
 
C# Starter L07-Objects Cloning
Mohammad Shaker
 
WPF L03-3D Rendering and 3D Animation
Mohammad Shaker
 
Android L01 - Warm Up
Mohammad Shaker
 
C# Advanced L10-Workflow Foundation
Mohammad Shaker
 
WPF L01-Layouts, Controls, Styles and Templates
Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
C# Advanced L08-Networking+WCF
Mohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
C# Starter L06-Delegates, Event Handling and Extension Methods
Mohammad Shaker
 
Car Dynamics with ABS, ESP and GPS Systems
Mohammad Shaker
 
OpenGL Starter L02
Mohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Ad

Similar to XNA L11–Shaders Part 2 (20)

ZIP
Adobe AIR: Stage3D and AGAL
Daniel Freeman
 
PDF
A Novice's Guide to WebGL
Krzysztof Kula
 
PPTX
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
Prabindh Sundareson
 
PPTX
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
JinTaek Seo
 
PDF
iOS Visual F/X Using GLSL
Douglass Turner
 
PDF
GL Shading Language Document by OpenGL.pdf
shaikhshehzad024
 
PDF
Stage3D and AGAL
Daniel Freeman
 
PDF
【Unite 2017 Tokyo】シェーダープログラミング入門!カスタムシェーダー、作るで!
Unity Technologies Japan K.K.
 
PPTX
Cg shaders with Unity3D
Michael Ivanov
 
PPTX
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
JinTaek Seo
 
PPTX
Introduction to open gl in android droidcon - slides
tamillarasan
 
PPTX
Trident International Graphics Workshop 2014 5/5
Takao Wada
 
PDF
Intro to OpenGL ES 2.0
Oleksandr Kozubets
 
PDF
WebGL - 3D in your Browser
Phil Reither
 
PPTX
Demystifying shaders
Bruno Opsenica
 
PPTX
Demystifying shaders
Functional Imperative
 
PDF
"Graphical fun With WebGL shaders", Martin Splitt
Fwdays
 
PDF
Shaders in Unity
Matias Lavik
 
Adobe AIR: Stage3D and AGAL
Daniel Freeman
 
A Novice's Guide to WebGL
Krzysztof Kula
 
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
Prabindh Sundareson
 
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
JinTaek Seo
 
iOS Visual F/X Using GLSL
Douglass Turner
 
GL Shading Language Document by OpenGL.pdf
shaikhshehzad024
 
Stage3D and AGAL
Daniel Freeman
 
【Unite 2017 Tokyo】シェーダープログラミング入門!カスタムシェーダー、作るで!
Unity Technologies Japan K.K.
 
Cg shaders with Unity3D
Michael Ivanov
 
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
JinTaek Seo
 
Introduction to open gl in android droidcon - slides
tamillarasan
 
Trident International Graphics Workshop 2014 5/5
Takao Wada
 
Intro to OpenGL ES 2.0
Oleksandr Kozubets
 
WebGL - 3D in your Browser
Phil Reither
 
Demystifying shaders
Bruno Opsenica
 
Demystifying shaders
Functional Imperative
 
"Graphical fun With WebGL shaders", Martin Splitt
Fwdays
 
Shaders in Unity
Matias Lavik
 
Ad

More from Mohammad Shaker (20)

PDF
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
PDF
Unity L01 - Game Development
Mohammad Shaker
 
PDF
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
PDF
Interaction Design L03 - Color
Mohammad Shaker
 
PDF
Interaction Design L05 - Typography
Mohammad Shaker
 
PDF
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
PDF
Android L05 - Storage
Mohammad Shaker
 
PDF
Android L04 - Notifications and Threading
Mohammad Shaker
 
PDF
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
PDF
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
PDF
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
PDF
Android L10 - Stores and Gaming
Mohammad Shaker
 
PDF
Android L06 - Cloud / Parse
Mohammad Shaker
 
PDF
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
PDF
Android L03 - Styles and Themes
Mohammad Shaker
 
PDF
Android L02 - Activities and Adapters
Mohammad Shaker
 
PDF
Indie Series 01: Intro to Games
Mohammad Shaker
 
PDF
Indie Series 04: The Making of SyncSeven
Mohammad Shaker
 
PDF
Indie Series 02: AI and Recent Advances in Games
Mohammad Shaker
 
PDF
Roboconf DSL Advanced Software Engineering
Mohammad Shaker
 
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L05 - Storage
Mohammad Shaker
 
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Mohammad Shaker
 
Indie Series 01: Intro to Games
Mohammad Shaker
 
Indie Series 04: The Making of SyncSeven
Mohammad Shaker
 
Indie Series 02: AI and Recent Advances in Games
Mohammad Shaker
 
Roboconf DSL Advanced Software Engineering
Mohammad Shaker
 

Recently uploaded (20)

PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
Open Source Milvus Vector Database v 2.6
Zilliz
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PPTX
𝙳𝚘𝚠𝚗𝚕𝚘𝚊𝚍—Wondershare Filmora Crack 14.0.7 + Key Download 2025
sebastian aliya
 
PDF
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Open Source Milvus Vector Database v 2.6
Zilliz
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
𝙳𝚘𝚠𝚗𝚕𝚘𝚊𝚍—Wondershare Filmora Crack 14.0.7 + Key Download 2025
sebastian aliya
 
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 

XNA L11–Shaders Part 2

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L11 – Shaders Part 2
  • 4. Notice the rasterizer between the vertex and the pixel shader
  • 5. 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.
  • 6. Without this rasterizer, only the 3 points corresponding to the vertices would be sent to the pixel shader!!
  • 7. The interpolator next to the rasterizer calculates this value, by interpolating the color value of the corner points.
  • 8. 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.
  • 9. It will look like?!
  • 10. This!
  • 11. Nice!
  • 12. With HLSL, you could change the position or color of each vertex by all means!
  • 13. You could also do this in your XNA app, but then your CPU would have to perform those calculations, which would lower your framerate!
  • 14. 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
  • 15. 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)
  • 16. 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.
  • 17. Which is this output!
  • 19. Experimenting With Shaders • Go to our last HLSL file “.fx” – Replace the following line Output.Color = inColor;
  • 20. 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;
  • 21. 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;
  • 22. 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;
  • 23. 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;
  • 24. 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;
  • 25. Experimenting With Shaders • Now, Compile and run to see the following • “App4-NotGoodInterpolation”
  • 26. Experimenting With Shaders • Now, Compile and run to see the following
  • 27. Experimenting With Shaders • Now, Compile and run to see the following
  • 28. Experimenting With Shaders • Now, Compile and run to see the following
  • 29. 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
  • 30. 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).
  • 31. Experimenting With Shaders VertexToPixel 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; };
  • 32. Experimenting With Shaders VertexToPixel 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; };
  • 35. Experimenting With Shaders VertexToPixel 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; };
  • 36. Experimenting With Shaders VertexToPixel 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; };
  • 37. Experimenting With Shaders VertexToPixel 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; };
  • 38. Experimenting With Shaders VertexToPixel 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; };
  • 39. Experimenting With Shaders VertexToPixel 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; };
  • 40. Experimenting With Shaders • “App5-GoodInterpolation”
  • 42. Shaders - Samples • Texturing our triangle using the Pixel Shader • Read more – http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Textured_triangle.php
  • 43. 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.
  • 44. 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; } }
  • 45. 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; };
  • 46. Shaders - Samples • Higher performance by using Triangle Strips • Read more – http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Triangle_strip.php
  • 47. Shaders - Samples • Acting consciously and sneaky :D
  • 48. Shaders - Samples • Acting consciously and sneaky :D
  • 52. Shaders - Samples • CLEVER! Only 12 vertices are needed to define 10 triangles!
  • 54. Shaders - Samples • Creating your first light • Read more – http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Per-pixel_lighting.php
  • 55. 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.
  • 56. 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.
  • 57. Shaders - Samples float DotProduct(float3 lightPos, float3 pos3D, float3 normal) { float3 lightDir = normalize(pos3D - lightPos); return dot(-lightDir, normal); }
  • 58. Shaders - Samples struct VertexToPixel { float4 Position : POSITION; float2 TexCoords : TEXCOORD0; float3 Normal : TEXCOORD1; float3 Position3D : TEXCOORD2; };
  • 59. Shaders - Samples Output.Normal = normalize(mul(inNormal, (float3x3)xWorld)); Output.Position3D = mul(inPos, xWorld);
  • 61. Shaders - Samples • Shadow Mapping
  • 62. Shaders - Samples • Shadow Mapping algorithm • Read more: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shadow_map.php
  • 63. Shaders - Samples • Shadow Mapping algorithm
  • 64. 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); }
  • 65. Shaders - Samples • Transforming vertices to texture space using Projective Texturing • Reading more – http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Projective_texturing.php
  • 67. Shaders - Samples • Changing the shape of our light • Reading more http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shaping_the_light.php
  • 69. Shaders - Samples • Post-Processing Shaders • Read more – http://rbwhitaker.wikidot.com/post-processing-effects
  • 70. 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; }
  • 72. 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; }
  • 74. Shaders - Samples • Transparency • Read more – http://rbwhitaker.wikidot.com/transparency-shader
  • 75. Shaders - Samples technique Technique1 { pass Pass1 { AlphaBlendEnable = TRUE; DestBlend = INVSRCALPHA; SrcBlend = SRCALPHA; VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }
  • 76. 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; }
  • 77. Shaders - Samples • Creating a Toon Shader • Read more – http://rbwhitaker.wikidot.com/toon-shader
  • 78. Shaders - Samples • Intensity • Light • Normals
  • 79. Shaders - Samples • Intensity • Light • Normals • Then, SHADING!
  • 80. Shaders - Samples • Reflection Shader • Read more – http://rbwhitaker.wikidot.com/reflection-shader
  • 81. Shaders - Samples • Creating a Diffuse Lighting Shader • Read more – http://rbwhitaker.wikidot.com/diffuse-lighting-shader
  • 82. We Are Done For Today! End of Course!
  • 83. Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR Available courses to the date of this slide: