What’s the purpose of this line: grayScaleEffect.CurrentTechnique.Passes[0].Apply(); ?
This line tells the shader to set the targeted pass for drawing. Shaders can have more then one pass in a technique. In this case…
The effect is grayScaleEffect
The technique to apply changes to is the current one
The particular pass is set to [0]. // safe if you only have one pass.
Some shaders can have more then one pass.
For example the one im working on right now is a two pass shader the apply call is used from game one to make sure both passes execute when triangles are drawn.
This is on the shader.
technique Create360DegreeShadowDepth
{
pass Pass0
{
VertexShader = compile VS_SHADERMODEL
Create360DegreeShadowDepthVertexShaderPass0();
PixelShader = compile PS_SHADERMODEL
Create360DegreeShadowDepthPixelShader();
}
pass Pass1
{
VertexShader = compile VS_SHADERMODEL
Create360DegreeShadowDepthVertexShaderPass1();
PixelShader = compile PS_SHADERMODEL
Create360DegreeShadowDepthPixelShader();
}
}
When i draw it i make sure both passes execute like so by looping thru all the passes.
This is in game1 or the class file that i call to it from.
public void DrawAllParts(GraphicsDevice gd, Effect effect)
{
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
gd.DrawUserPrimitives(PrimitiveType.TriangleList, vertexArray, 0, vertexArray.Length /3, VertexPositionPositionLightIndexNormal.VertexDeclaration);
}
}
While this doesn’t use spritebatch what spritebatch does is basically the same thing with extra sauce.
And is there a difference between applying shaders to a screen or objects?
No however when you are drawing sprites the area affected on screen is limited to the destination area you are drawing to so then too is the effect.
It is possible to draw a screen sized rectangle that will allow the pixel shader to affect everything on screen.
Absolutely and this is done often.
This is the primary reason RenderTargets are useful. You can draw everything to them then use them as if they are a big texture for a final draw with a pixel shader at the end. Which is drawing the RenderTarget to the backbuffer in one draw.