Sign in to follow this  
Kuraku

[XNA] My Billboard always behind model problem

Recommended Posts

Hello. I have this problem. Character is in front of the tower on the back. when i move my cam to right a little. The Character goes behind it. This is my codes : Main :
        protected override void Draw(GameTime gameTime)
        { 
            terrain.Draw(Observer.ViewMatrix, Observer.ProjectionMatrix);
            Char.Draw(Observer.ViewMatrix, Observer.ProjectionMatrix, gameTime);      
            base.Draw(gameTime);
        }

terrain :
    foreach (ModelMesh me in Terrain20x10.Meshes) //The background
    {
        foreach (BasicEffect be in me.Effects)
        {           
            be.View = Observer.ViewMatrix;
            be.Projection = Observer.ProjectionMatrix;
        }

        me.Draw();
    }

Char :
            if (chara != null)
            {
                //draw billboards
                expEffect.CurrentTechnique = expEffect.Techniques["Explosion"]; 
                expEffect.Parameters["xWorld"].SetValue(Matrix.Identity);
                expEffect.Parameters["xProjection"].SetValue(Observer.ProjectionMatrix);
                expEffect.Parameters["xView"].SetValue(Observer.ViewMatrix);

                expEffect.Parameters["xCamPos"].SetValue(Observer.Position);
                expEffect.Parameters["xExplosionTexture"].SetValue(tex);

                expEffect.Parameters["xCamUp"].SetValue(Observer.UpVector);
                expEffect.Parameters["xTime"].SetValue((float)gameTime.TotalGameTime.TotalMilliseconds);

                device.RenderState.AlphaBlendEnable = true;
                device.RenderState.SourceBlend = Blend.SourceAlpha;
                device.RenderState.DestinationBlend = Blend.One;
                device.RenderState.DepthBufferWriteEnable = false;

                expEffect.Begin();
                foreach (EffectPass pass in expEffect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    device.VertexDeclaration = myVertexDeclaration;
                    device.DrawUserPrimitives<Chara>(PrimitiveType.TriangleList, chara, 0, chara.Length / 3);
                    pass.End();
                }
                expEffect.End();

                device.RenderState.DepthBufferWriteEnable = true;
            }           

this is my .FX
//------- XNA interface --------
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float3 xCamPos;
float3 xAllowedRotDir;
float3 xCamUp;
float xTime;

//------- Texture Samplers --------
Texture xExplosionTexture;
sampler textureSampler = sampler_state { texture = <xExplosionTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = CLAMP; AddressV = CLAMP;};

struct ExpVertexToPixel
{
	float4 Position : POSITION;
	float2 TexCoord	: TEXCOORD0;
	float4 Color	: COLOR0;
};
struct ExpPixelToFrame
{
    float4 Color 	: COLOR0;
};

//------- Technique: Explosion --------
float3 BillboardVertex(float3 billboardCenter, float2 cornerID, float size)
{
	float3 eyeVector = billboardCenter - xCamPos;		
	
	float3 sideVector = cross(eyeVector,xCamUp);
	sideVector = normalize(sideVector);
	float3 upVector = cross(sideVector,eyeVector);
	upVector = normalize(upVector);
	
	float3 finalPosition = billboardCenter;
	finalPosition += (cornerID.x-0.5f)*sideVector*size;
	finalPosition += (0.5f-cornerID.y)*upVector*size;	
	
	return finalPosition;
}

ExpVertexToPixel ExplosionVS(float3 inPos: POSITION0, float2 inTexCoord: TEXCOORD0)
{
	ExpVertexToPixel Output = (ExpVertexToPixel)0;	

	float3 startingPosition = mul(inPos, xWorld);
	
	float2 texCoords = inTexCoord.xy;
	
	float size = 10;
    	
	float3 billboardCenter = inPos;		
	
	float3 finalPosition = BillboardVertex(billboardCenter, texCoords, size);	
	float4 finalPosition4 = float4(finalPosition, 1);
		
	float4x4 preViewProjection = mul (xView, xProjection);
	Output.Position = mul(finalPosition4, preViewProjection);	
	
	float alpha = 1;
	Output.Color = float4(1,1,1,alpha);		
	
	Output.TexCoord = texCoords;	
	
	return Output;
}

ExpPixelToFrame ExplosionPS(ExpVertexToPixel PSIn) : COLOR0
{
	ExpPixelToFrame Output = (ExpPixelToFrame)0;		
	Output.Color = tex2D(textureSampler, PSIn.TexCoord)*PSIn.Color;
	return Output;
}

technique Explosion
{
	pass Pass0
    {          
    	VertexShader = compile vs_1_1 ExplosionVS();
        PixelShader  = compile ps_1_1 ExplosionPS();        
    }
}


I tried disabling Bacface Culling but my problem didn't solved
device.RenderState.CullMode = CullMode.None;

Share this post


Link to post
Share on other sites
terrain.Draw(Observer.ViewMatrix, Observer.ProjectionMatrix);
Char.Draw(Observer.ViewMatrix, Observer.ProjectionMatrix, gameTime);
base.Draw(gameTime);

You have the draw the billboard after the other stuff. (I guess Char is the billboard, and base is the building mesh)

Or you should enable depth writing for the billboard (if it has depth), and use alpha test instead of blending.

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
You have the draw the billboard after the other stuff. (I guess Char is the billboard, and base is the building mesh)


the building mesh is terrain and the Character is Char. I already draw the Character after the terrain.

Quote:
Original post by szecs
Or you should enable depth writing for the billboard (if it has depth), and use alpha test instead of blending.


Sorry but could you give me a small example to do that? I'm pretty new to XNA.

Share this post


Link to post
Share on other sites


I think i know my problem now. The character, the building meshes and the black background, Their color are blended into one.

What should i do to avoid this problem?

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