Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have created a terrain using VertexPositionNormalTexture in XNA 4.0, but now I would like to allow the user changing the opacity of the terrain (from 0%, totally opaque, to 100%, totally transparent), so that user can see through it. Is there a simple way to do that?

I would also like to know how to do that if I use VertexPositionColor instead of VertexPositionNormalTexture (i.e., creating a colored terrain)

Thank you very much in advance.

share|improve this question
    
Im assuming this is 3D, but to make transparent 2D textures, in the spriteBatch.Draw() method, change the color value to Color.White *= 0.5f. that'll give you half transparency. –  Battle_Pasture Mar 6 '13 at 13:01
    
Yes, it's 3D. I knew that I could change it with SpriteBatch, but I am using DrawIndexedPrimitives of GraphicsDevice... –  DarthRoman Mar 6 '13 at 13:44
    
Do you want to change the entire terrain's opacity or only sections of it? If it's the latter then you can easily accomplish this using an alpha map. –  Aaron Kabashi Mar 6 '13 at 15:31
add comment

1 Answer 1

up vote 3 down vote accepted

Changing the colours of a vertex buffer is relatively expensive. You'd have to use something like VertexPositionColor and then modify every vertex every time you want to change transparency!

It would be much easier, and faster, to add transparency using a shader (pixel or vertex). Fortunately BasicEffect does exactly what you need - it even has an appropriate Alpha member. Here's the code:

GraphicsDevice.BlendState = BlendState.AlphaBlend;
basicEffect.Alpha = 0.5f; // <- however opaque you want it

The other built-in effects also have Alpha members.

If you're writing your own shaders - just pass in a colour (with appropriate transparency) as a shader parameter. Then somewhere in your vertex or pixel shader, multiply by that colour before output.

share|improve this answer
    
Perfect, that was I was looking for. Previously, I was using Riemer's effects file, but BasicEffect has the Alpha variable, which is perfect for me... Sadly, now I am having lightning and colour problems with BasicEffect, but I imagine it's something related to the effect's configuration. Thanks!! –  DarthRoman Mar 6 '13 at 16:24
    
Just adding EnableDefaultLighting() should get some lighting working, so you can test with it. It seems to work correctly with Alpha. Perhaps there's a problem with your model's normals? –  Andrew Russell Mar 6 '13 at 18:12
    
I don't think so... please, take a look at this question I have posted today: gamedev.stackexchange.com/questions/50592/… I still don't have enough reputation to post more than 2 links, so if you want a more complete question, look at stackoverflow.com/questions/15269216/xna-4-0-weird-color-error –  DarthRoman Mar 7 '13 at 10:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.