• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
vinnyhalts

RenderTargets and Transparency

9 posts in this topic

Hi, I am trying to draw a weapon on screen, using a rendertarget. It works, and it moves smoothly, but there is one problem. My model is see through. I have searched this, and tried many different things, but nothing seems to work. If anyone knows what I am doing wrong, and if they could direct me in the right direction, that would be great.

 

Here is some of my code:

//This is inside LoadContent()
var pp = XNAGame.Instance.GraphicsDevice.PresentationParameters;
            RT = new RenderTarget2D(XNAGame.Instance.GraphicsDevice,
                     pp.BackBufferWidth, //Same width as backbuffer
                     pp.BackBufferHeight, //Same height
                     false, //No mip-mapping
                     pp.BackBufferFormat, //Same colour format
                     pp.DepthStencilFormat);

....
            transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    part.Effect = e;
                }
            }

//My drawing code:
            XNAGame.Instance.GraphicsDevice.SetRenderTarget(RT);
            XNAGame.Instance.GraphicsDevice.Clear(Color.Transparent);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    //part.Effect = e;
                    e.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * Matrix.Identity);
                    e.Parameters["View"].SetValue(Matrix.Identity);
                    e.Parameters["LightDirection"].SetValue(Vector3.Normalize(Skydome.Instance.sunDir));
                    e.Parameters["CameraPosition"].SetValue(XNAGame.Instance.Camera.Position);
                    e.Parameters["Projection"].SetValue(Matrix.Identity);
                    e.Parameters["ColorMap"].SetValue(diff);
                    e.Parameters["NormalMap"].SetValue(norm);
                    e.Parameters["SpecularMap"].SetValue(spec);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                }
                mesh.Draw();
            }
            RtTex = RT;
            XNAGame.Instance.GraphicsDevice.SetRenderTarget(null);

 

 

Here is the shader I am using, I got it off of an opensource project:

// XNA 4.0 Normal Mapping 
	 
	// Matrix 
	float4x4 World; 
	float4x4 View; 
	float4x4 Projection; 
	 
	// Light related 
	float4 AmbientColor = float4(1, 1, 1, 1); 
	float AmbientIntensity = .4; 
	 
	float3 LightDirection;
	float4 DiffuseColor = float4(1, 1, 1, 1);
	float DiffuseIntensity = .4; 
	 
	float4 SpecularColor = float4(1, 1, 1, 1); 
	float3 CameraPosition; 
	 
	 
	texture2D ColorMap; 
	sampler2D ColorMapSampler = sampler_state 
	{ 
	    Texture = <ColorMap>; 
	    MinFilter = linear; 
	    MagFilter = linear; 
	    MipFilter = linear; 
	}; 
	 
	texture2D NormalMap; 
	sampler2D NormalMapSampler = sampler_state 
	{ 
	    Texture = <NormalMap>; 
	    MinFilter = linear; 
	    MagFilter = linear; 
	    MipFilter = linear; 
	}; 
	 
	texture SpecularMap; 
	sampler SpecMapSampler = sampler_state 
	{ 
	   Texture = <SpecularMap>; 
	   MinFilter = Linear; 
	   MagFilter = Linear; 
	   MipFilter = Linear;    
	   AddressU  = Clamp; 
	   AddressV  = Clamp; 
	}; 
	 
	// The input for the VertexShader 
	struct VertexShaderInput 
	{ 
	    float4 Position : POSITION0; 
	    float2 TexCoord : TEXCOORD0; 
	    float3 Normal : NORMAL0; 
	    float3 Binormal : BINORMAL0; 
	    float3 Tangent : TANGENT0; 
	}; 
	 
	// The output from the vertex shader, used for later processing 
	struct VertexShaderOutput 
	{ 
	    float4 Position : POSITION0; 
	    float2 TexCoord : TEXCOORD0; 
	    float3 View : TEXCOORD1; 
	    float3x3 WorldToTangentSpace : TEXCOORD2; 
	}; 
	 
	// The VertexShader. 
	VertexShaderOutput VertexShaderFunction(VertexShaderInput input) 
	{ 
	    VertexShaderOutput output; 
	 
	    float4 worldPosition = mul(input.Position, World); 
	    float4 viewPosition = mul(worldPosition, View); 
	    output.Position = mul(viewPosition, Projection); 
	    output.TexCoord = input.TexCoord; 
	 
	    output.WorldToTangentSpace[0] = mul(normalize(input.Tangent), World); 
	    output.WorldToTangentSpace[1] = mul(normalize(input.Binormal), World); 
	    output.WorldToTangentSpace[2] = mul(normalize(input.Normal), World); 
	     
	    output.View = normalize(float4(CameraPosition,1.0) - worldPosition); 
	 
	    return output; 
	} 
	 
	// The Pixel Shader 
	float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 
	{ 
	    float4 color = tex2D(ColorMapSampler, input.TexCoord); 
	 
	    float4 SpecMapColor = tex2D(SpecMapSampler, input.TexCoord); 
	 
	    float3 normalMap = 2.0 *(tex2D(NormalMapSampler, input.TexCoord)) - 1.0; 
	    normalMap = normalize(mul(normalMap, input.WorldToTangentSpace)); 
	    float4 normal = float4(normalMap,1.0); 
	 
	    float4 diffuse = saturate(dot(LightDirection,normal)); 
	    float4 reflect = normalize(2*diffuse*normal-float4(LightDirection,1.0)); 
	    float4 specular = pow(saturate(dot(reflect,input.View)),32); 
	 
	    return  color * AmbientColor * AmbientIntensity +  
	            color * DiffuseIntensity * DiffuseColor * diffuse +  
	            color * SpecularColor * specular; 
	} 
	 
	// Our Techinique 
	technique DiffuseSpecularNormal 
	{ 
	    pass Pass0
	    {
		AlphaBlendEnable = FALSE;
	        VertexShader = compile vs_2_0 VertexShaderFunction(); 
	        PixelShader = compile ps_2_0 PixelShaderFunction(); 
	    } 
	} 

 

 

 

 

I thought that maybe it was my SpriteBatch, but I made a screenshot function and commented out all SpriteBatch code, just to check it.

 

 

public void scrn(string name)
        {
            Stream str = File.OpenWrite("Dump - " + name + ".png");
            RtTex.SaveAsPng(str, RtTex.Width, RtTex.Height);
            str.Close();
            str.Dispose();
        }

 

 

So it clearly, as far as I can tell, is not my SpriteBatch. I have attached a photo of how it looks (it is transparent).

 

Thanks for taking the time to read this/help me.

 

0

Share this post


Link to post
Share on other sites
It's not obvious it's transparent (given the smooth background), but it looks possibly like the z-order of the meshes is all off.

Are you making sure to set GraphicsDevice.DepthStencilState to DepthStencilState.Default prior to rendering your model?
0

Share this post


Link to post
Share on other sites

It's not obvious it's transparent (given the smooth background), but it looks possibly like the z-order of the meshes is all off.

Are you making sure to set GraphicsDevice.DepthStencilState to DepthStencilState.Default prior to rendering your model?

I just tried it, it does not seem to change anything.

0

Share this post


Link to post
Share on other sites
Where did you put that line?

Can you post a better screenshot? It's very hard to tell anything from that. We don't know what the object is supposed to look like, so it's hard to tell what's wrong.

How does it look from different angles?
What does the original look like in your modelling program?
Is it really transparent? Can you put something other than a blank background behind it, so it is easy to tell?
0

Share this post


Link to post
Share on other sites

Is your camera positions such that it is inside the camera?  This would make you only see faces that are pointing towards the camera even though it is inside, meaning that there would be lots of faces culled out by back face culling.

0

Share this post


Link to post
Share on other sites

Where did you put that line?

Can you post a better screenshot? It's very hard to tell anything from that. We don't know what the object is supposed to look like, so it's hard to tell what's wrong.

How does it look from different angles?
What does the original look like in your modelling program?
Is it really transparent? Can you put something other than a blank background behind it, so it is easy to tell?

 

I put the line right after I set my new render target.

 

        public override void Draw(GameTime gameTime)
        {
            XNAGame.Instance.GraphicsDevice.SetRenderTarget(RT);
            XNAGame.Instance.GraphicsDevice.Clear(Color.Transparent);
            XNAGame.Instance.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    //part.Effect = e;
                    e.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * Matrix.Identity);
                    e.Parameters["View"].SetValue(Matrix.Identity);
                    e.Parameters["LightDirection"].SetValue(Vector3.Normalize(Skydome.Instance.sunDir));
                    e.Parameters["CameraPosition"].SetValue(XNAGame.Instance.Camera.Position);
                    e.Parameters["Projection"].SetValue(Matrix.Identity);
                    e.Parameters["ColorMap"].SetValue(diff);
                    e.Parameters["NormalMap"].SetValue(norm);
                    e.Parameters["SpecularMap"].SetValue(spec);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                    //e.Parameters["Projection"].SetValue(XNAGame.Instance.Camera.ProjectionMatrix);
                }
                mesh.Draw();
            }
            RtTex = RT;
            XNAGame.Instance.GraphicsDevice.SetRenderTarget(null);
            
            //Stream str = File.OpenWrite("Dump.png") ;
            //RtTex.SaveAsPng(str, RtTex.Width, RtTex.Height);
            //str.Close();
        }
    }

 

 

Here is a photo of the gun in game, without using a different render target.

 

2l7ic9H.jpg

Is your camera positions such that it is inside the camera?  This would make you only see faces that are pointing towards the camera even though it is inside, meaning that there would be lots of faces culled out by back face culling.

 

I've tested it with other models and tried moving the model further away. Nothing seems to work.

 

 

 

 

Edit: I added more photos to show the problem.

 

HP58uoM.png

Edited by vinnyhalts
0

Share this post


Link to post
Share on other sites
Hmm... well, alpha-blending should be disabled given that you have AlphaBlendEnable = FALSE in your shader.
But just for kicks, can you add this next to the line where you set the DepthStencilState:

XNAGame.Instance.GraphicsDevice.BlendState = BlendState.Opaque;
0

Share this post


Link to post
Share on other sites

Hmm... well, alpha-blending should be disabled given that you have AlphaBlendEnable = FALSE in your shader.
But just for kicks, can you add this next to the line where you set the DepthStencilState:

XNAGame.Instance.GraphicsDevice.BlendState = BlendState.Opaque;

Nothing changed.

gYQxbbB.png

 

 

 

Edit, it has to be something in the Render Target. As I disabled it, and just drew the image as I would any other model, and it worked. But this does not solve my problem, as it still jitters when I move in game. I still need to use a Render Target.

 

 

Any ideas why?

Edited by vinnyhalts
0

Share this post


Link to post
Share on other sites
The only thing I can thing of is that the alpha in your gun source textures must be partially transparent. That would explain the difference you're seeing between render target and no render target.

As a quick test, in your shader try replacing this:

return color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;

with

float4 temp = color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;
temp.a = 1; // Force alpha to be 1
return temp;
1

Share this post


Link to post
Share on other sites

The only thing I can thing of is that the alpha in your gun source textures must be partially transparent. That would explain the difference you're seeing between render target and no render target.

As a quick test, in your shader try replacing this:

return color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;

with

float4 temp = color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;
temp.a = 1; // Force alpha to be 1
return temp;

 

Yeah, that did it. It seems to be working now. It's a bit jagged, but that's another problem for another time. Thank you very much. I would not have known it was the textures, as I am using public domain models and textures.

rgRTcwI.png

 

Edit: I fixed the Anti aliasing, I just turned up my multisampling. Just in the event anyone should read this and need help:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.rendertarget2d_members.aspx

 

 

 

NbjMOK2.png

Edited by vinnyhalts
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0