Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » XNA Effect on Sprites

This thread is locked; no one can reply to it. rss feed Print
XNA Effect on Sprites
blargmob
Member #8,356
February 2007
avatar

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...

???

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

X-G
Member #856
December 2000
avatar

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.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

CursedTyrant
Member #7,080
April 2006
avatar

Post code?

---------
Signature.
----
[My Website] | [My YouTube Channel]

Neil Walker
Member #210
April 2000
avatar

Have you tried without using the studio with something simple, e.g.

http://digierr.spaces.live.com/blog/cns!2B7007E9EC2AE37B!424.entry

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

blargmob
Member #8,356
February 2007
avatar

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.

???

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

Sample code:

#SelectExpand
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);

---------
Signature.
----
[My Website] | [My YouTube Channel]

blargmob
Member #8,356
February 2007
avatar

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)

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

Quote:

Matrix World, View, Projection;
PresentationParameters pp = GraphicsDevice.PresentationParameters;

World = Matrix.Identity;
View = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, Vector3.Up);
Projection = Matrix.CreateOrthographicOffCenter(0, pp.BackBufferWidth, pp.BackBufferHeight, 0, 1, 100);

And then you have to figure out how to use those in HLSL :P Typically you'd probably want to replace XNA's default WorldViewProjection (in the .fx file) by separate variables (at least that seems like a good idea, if only to better understand stuff).

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).

---------
Signature.
----
[My Website] | [My YouTube Channel]

blargmob
Member #8,356
February 2007
avatar

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")
DECLARE_QUAD_DEPTH_BUFFER(gDepthBuffer,"D24S8")

Should I be "setting" those? If so, how?

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

EDIT:
You can't really set that in HLSL I think, you'd need to look into GraphicsDevice.RenderState. But to pass the screen as a texture use a RenderTarget2D, use something like this:

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.

---------
Signature.
----
[My Website] | [My YouTube Channel]

blargmob
Member #8,356
February 2007
avatar

Okay this is what I've got in my draw method now:

#SelectExpand
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 :'(

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

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.

---------
Signature.
----
[My Website] | [My YouTube Channel]

blargmob
Member #8,356
February 2007
avatar

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"?

???

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

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.

---------
Signature.
----
[My Website] | [My YouTube Channel]

X-G
Member #856
December 2000
avatar

blargmob said:

I don't think sprites have vertexes.

They have one in each corner. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Go to: