![]() |
|
XNA Effect on Sprites |
blargmob
Member #8,356
February 2007
![]() |
I've been playing around with some pre-written .fx HLSL files in XNA lately. Cursed Tyrant turned me toward FX Composer for creating shaders in realtime. It's pretty cool, but there are some questions I have: 1. Is it at all possible to use 3D shaders on a 2D texture? 2. I've tried using the .fx files from FX Composer in XNA, but to no avail. I'm seems like I'm doing everything right, but when I try to draw a sprite with the effect, nothing appears...
--- |
X-G
Member #856
December 2000
![]() |
blargmob said: Is it at all possible to use 3D shaders on a 2D texture? There is no real difference between 3D or 2D aside from the projection matrix used. -- |
CursedTyrant
Member #7,080
April 2006
![]() |
Post code? --------- |
Neil Walker
Member #210
April 2000
![]() |
Have you tried without using the studio with something simple, e.g. http://digierr.spaces.live.com/blog/cns!2B7007E9EC2AE37B!424.entry Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
blargmob
Member #8,356
February 2007
![]() |
Pardon my lack of knowledge, we are just starting our unit on Matrices this trimester. Anyway, X-G said: There is no real difference between 3D or 2D aside from the projection matrix used. What if I need to provide a World Matrix as well? And what about vertex shaders? I don't think sprites have vertexes. And it is unclear to me, what parameters I need to set in order start using a shader. I have tried looking through the .fx HLSL file, but I can't seem to find exactly what needs to be set before I start calling the shaders passes.
--- |
CursedTyrant
Member #7,080
April 2006
![]() |
Sample code: 1Effect effect = Content.Load<Effect>(path_to_effect_file);
2
3//Render
4
5spriteBatch.Begin();
6
7effect.Begin();
8foreach (EffectPass ep in effect.CurrentTechnique.Passes)
9{
10 ep.Begin();
11
12 //draw stuff here
13
14 ep.End();
15}
16effect.End();
17
18spriteBatch.End();
If you use the vertex shader, you'll need to set up World, View and Projection Matrices (usually). E.g. use effect.Parameters["World"].SetValue(WorldMatrix); --------- |
blargmob
Member #8,356
February 2007
![]() |
CursedTyrant said: Sample code: That's exactly what I am doing. Quote: If you use the vertex shader, you'll need to set up World, View and Projection Matrices (usually). E.g. use effect.Parameters["World"].SetValue(WorldMatrix); What am I to pass if my game is 2D and only using sprites? (i.e. no models, not a 3d game) --- |
CursedTyrant
Member #7,080
April 2006
![]() |
Quote:
Matrix World, View, Projection; World = Matrix.Identity;
And then you have to figure out how to use those in HLSL Also, if what you're looking for is post processing, then you'll most likely want to use the shader on the entire screen at once, and that's where you use a RenderTarget (or multiple RenderTargets, depending on what you're trying to accomplish). --------- |
blargmob
Member #8,356
February 2007
![]() |
I'm trying to draw a simple sprite (Texture2D) with a pixel shader for brightness and contrast that I downloaded from the Nvidia Shader Library. However, when I try to draw the image with the shader, nothing appears but a gray rectangle. I think it may have something to do with this: In the effect file: Quote:
DECLARE_QUAD_TEX(gSceneTexture,gSceneSampler,"A8R8G8B8") Should I be "setting" those? If so, how? --- |
CursedTyrant
Member #7,080
April 2006
![]() |
EDIT: RenderTarget2D renderTarget = //constructor here, too lazy to look it up, use values from PresentationParameters and you'll be fine //Render GraphicsDevice.SetRenderTarget(0, renderTarget); GraphicsDevice.Clear(color); //draw stuff here GraphicsDevice.SetRenderTarget(0, null); //effect.Begin() etc. spriteBatch.Draw(renderTarget.GetTexture(), Vector2.Zero, Color.White); //effect.End() etc. And in the .fx file: sampler2D texSampler;
Then you can use TEXCOORD and tex2D(sampler_here, uv_coords_here) to get a float4, which is the current pixel color. Also, take a look at this: http://www.riemers.net/eng/Tutorials/XNA/Csharp/series3.php Also, note that for most 2D effects that affect the whole screen (like bloom, greyscale, etc.), you don't really need a vertex shader. Just pass float2 uv : TEXCOORD0 to the PS arguments. --------- |
blargmob
Member #8,356
February 2007
![]() |
Okay this is what I've got in my draw method now: 1
2graphics.GraphicsDevice.SetRenderTarget(0, renderTarget);
3 graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
4
5 spriteBatch.Begin();
6 spriteBatch.Draw(image, Vector2.Zero, Color.White);
7 spriteBatch.End();
8
9 graphics.GraphicsDevice.SetRenderTarget(0, null);
10 graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
11
12 spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
13
14 effect.Begin();
15 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
16 {
17 pass.Begin();
18 spriteBatch.Draw(renderTarget.GetTexture(), Vector2.Zero, Color.White);
19 pass.End();
20 }
21 effect.End();
22
23 spriteBatch.End();
24
25 base.Draw(gameTime);
but it still refuses to work, and I only get a gray texture drawn --- |
CursedTyrant
Member #7,080
April 2006
![]() |
That code has to work. It's the .fx file then. Also, you can use GraphicsDevice directly, there's no need to access it through graphics. --------- |
blargmob
Member #8,356
February 2007
![]() |
This happens with any shader I try to use. The problem may be that there are parameters or something that I am not specifying, but it seems that everything is accounted for. Maybe it has something to do with the "gDepthBuffer"?
--- |
CursedTyrant
Member #7,080
April 2006
![]() |
To answer your question - I doubt it. Try creating a new shader, throw out all the vertex shader related stuff, and run that. And change the return color, because I'm uncertain if it isn't set to 0 alpha. If it doesn't work, then either your gfx card is older than time itself and you somehow managed to run XNA on it, or it's got something to do with your XNA init. --------- |
X-G
Member #856
December 2000
![]() |
blargmob said: I don't think sprites have vertexes.
They have one in each corner. -- |
|