Nick Gravelyn

I just finished a basic billboard class that is capable of reading in multiple textures and animating. However, I would like to read in a single texture file and be able to simply change texture coordinates to change the appearance of the billboard. Now, I could recreate my VertexBuffer each frame, but that would be a lot of wasted processing. Is there a way to dynamically alter the texture coordinates for VertexBuffer objects each frame

Edit: As a bonus question, how do I enable the alpha channels in my textures Right now it shows up white. I am using the BasicEffect. Would I have to write my own effect just for that Seems rather silly but I will if I have to.

Edit 2: As bonus question 2, how can I disable antialiasing of my textures I'm loading in 32x32 images and scaling them up. I would LIKE to keep the pixelated look, but XNA seems to prefer not letting me.


Re: XNA Game Studio Express Animating Billboards Using A Single Texture

leclerc9

VertexBuffer.SetData allows you to dynamically alter vertex buffers.

I'm afraid I can't answer the bonus questions without some hacking, but:
Question 1: Alpha blending has to be enabled in the render state and you have to set a blend function. You can probably find details of this on the forums.

Question 2: You might want to try setting Texture2D.LevelOfDetail to 1 and then calling Texture2D.GenerateMipMaps(TextureFilter.None).




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

Thanks. I'll see if the .SetData method will work fast enough to handle lots of animated billboards.

As for the "bonus questions", I've tried both of those as solutions and neither worked. I am using PNG files with transparency for my textures. But thanks for suggesting something.




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Shawn Hargreaves - MSFT

SetData will work, but probably not very quickly if you have a lot of billboards. Graphics cards don't really like it when the CPU changes resources on the fly: they are much happier if big things like vertex buffers and textures are just created once at load time and then treated as read-only during the actual rendering.

A faster approach would be to animate the texture coordinates in your vertex shader. Store the coordinates for frame #0 of your animation in the vertex buffer, then each time you want to render, set an offset value as an effect parameter, and in your vertex shader, add this offset to the input coordinate. That way you can animate any number of billboards while only having to upload a single dynamic constant to the GPU.

If you don't want all the billboards to animate in sync, you could randomly give each one a different set of starting texture coordinates. That way when you add the offset, it will affect each billboard differently. You can get even fancier if you do things like storing a random animation speed value as part of each vertex, which you could use in the shader to modify the offset parameter so each billboard would update at a different rate.

Bonus 1: alpha blending will work with BasicEffect if you enable it in the GraphicsDevice renderstates.

Bonus 2: by "antialiasing" I think you actually mean texture filtering. You can set this to point sampling (using GraphicsDevice.SamplerState[0].MagFilter), but you'll need to do that after you begin your effect pass to avoid your settings being overwritten by BasicEffect.






Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

The texture filtering worked perfectly. And I'll look into the vertex shader animation. But the alpha blending still doesn't work. I enable it with GraphicsDevice.RenderState.AlphaBlendEnable in the Draw method, but I still see white boxes. Are there any settings in BasicEffect I have to use for this I've used PNGs for sprites with transparent backgrounds, but when applying it as a texture it doesn't seem to work as well.

Thanks for the help so far.




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

ProfEclipse

When setting RenderState.AlphaBlendEnable to true, you also need to set RenderState.SouceBlend and RenderState.DestinationBlend. Typically, you would set SouceBlend to Blend.SourceAlpha and DestinationBlend to Blend.InverseSourceAlpha.





Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

I tried setting those:

GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.InverseSourceAlpha;

but still white boxes. I am doing it just prior to effect.Begin(). Should it be after that Is it possible the content loader isn't getting the transparency I couldn't see why as it's the same content loader I used for my 2D sprite batches where transparency worked great. I've also tried setting the DrawOrder for my billboards very high to make sure everything else was drawn first. Would it matter that I'm using DrawableGameComponents for my billboards I figured with the DrawOrder, I'd be ok as far as transparency and sorting.




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

ProfEclipse

Not AlphaSourceBlend, but SourceBlend.

Not AlphaDestinationBlend, but DestinationBlend.

The "Alpha" versions are for a different purpose.





Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

Thanks. Missed that one.




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

Shawn Hargreaves - MSFT wrote:

A faster approach would be to animate the texture coordinates in your vertex shader. Store the coordinates for frame #0 of your animation in the vertex buffer, then each time you want to render, set an offset value as an effect parameter, and in your vertex shader, add this offset to the input coordinate. That way you can animate any number of billboards while only having to upload a single dynamic constant to the GPU.


Does anyone have an example of how to do this I've used custom shaders a few times back with DirectX, but I'm pretty lost on them now.




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Derek Nedelman

A little off topic...have you tried running your billboard code on a 360 I have a billboards demo which you can find at http://www.threesixbox.com/project/ id=85875c2304 The demo looks fine on Windows but has strange rendering bugs when run on a 360.

Back on topic...my demo also implements (but doesn't demonstrate) animation with a single texture. It achieves this by precalculating all the billboard quads, each of which has different texture coordinates, and setting them all into a vertex buffer. The correct start vertex is then chosen when drawing based on the current texture index.





Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

Hm. I just got RenderMonkey and I think I've got the shader animation thing down, but if not I might look into your method. A little less dynamic than I'd like, but the textures shouldn't change so I think that's fine.

As for running it on 360, I don't yet own one or have the Creator's Club. And I'm a little way off from when I'll care about putting it on 360. My game is still in the first few days of development. Heck, my billboards aren't actually billboards yet. They don't stay facing the camera yet :).




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Derek Nedelman

Well if you're having trouble with your billboards you definitely should take a look at my demo since it actually has billboards that face the camera :)





Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

Oh, I'm not having trouble, I haven't even gotten there yet. I wanted to get the animation part down and also a few things with my scene manager before I worried about that. I don't figure it'll be too hard, but I'll look at the demo to make sure I'm doing it efficiently.




Re: XNA Game Studio Express Animating Billboards Using A Single Texture

Nick Gravelyn

Derek Nedelman wrote:
Well if you're having trouble with your billboards you definitely should take a look at my demo since it actually has billboards that face the camera :)


So I took a look at the demo today. I tried implementing it. I brought over your MathUtilities file (which is very nice) so that I didn't have to worry about that aspect of it. But when I run, not only do I not see properly billboarded billboards, I don't see anything. Any ideas Here's the code I'm using to compute it:


Matrix world;
Matrix view = SceneManagerService.ViewMatrix;
MathUtilities.InvertRotationTranslationMatrix(ref view, out world);
Matrix billboardMatrix = Matrix.CreateBillboard(
this.Position,
world.Translation,
world.Up,
world.Forward
);
Matrix scale = ScaleMatrix;
Matrix.Multiply(ref scale, ref billboardMatrix, out world);
animationEffect.Parameters["matWorld"].SetValue(world);