XNA Texture2D Billboarding显示为白色块

时间:2013-06-22 16:20:10

标签: c# windows-phone-7 3d xna texture2d

非常奇怪的问题,当我尝试绘制我的广告牌精灵时,它总是显示为白色块,更改.draw颜色属性仍将其绘制为白色,它也无关紧要我使用jpeg或透明png

[编辑]

所以我现在尝试使用Viewport而不是基本效果来获得x和y屏幕坐标,我稍后会解决任何缩放问题,但现在图像保持完全相同斑点(在屏幕上,它不会根据相机改变位置)并且根据它的距离不会变大或变小

我的新广告牌渲染功能:

public void Draw(Camera camera, GraphicsDevice device, SpriteBatch spriteBatch, Texture2D Texture)
{
    Viewport viewport = new Viewport(new Rectangle(0, 0, 800, 480));
    Vector3 viewSpaceTextPosition = viewport.Project(this.position, camera.Projection, camera.View, camera.World);

    spriteBatch.Begin();
    spriteBatch.Draw(Texture, new Vector2(viewSpaceTextPosition.X, viewSpaceTextPosition.Y), null, Color.White, 0, new Vector2(Texture.Bounds.Center.X, Texture.Bounds.Center.Y), this.Scale, SpriteEffects.None, viewSpaceTextPosition.Z);
    spriteBatch.End();

    device.RasterizerState = RasterizerState.CullCounterClockwise;
    device.BlendState = BlendState.Opaque;
    device.DepthStencilState = DepthStencilState.Default;
}

我对Viewport的使用是错误的还是只需要在spriteBatch.Draw()中以不同方式使用它的信息?

2 个答案:

答案 0 :(得分:1)

我认为这应该是使用视口项目的技巧...获取两个投影点并计算它的距离,你得到一个受深度影响的值...所以如果它更深,那么值会更小。

public void Draw(Camera camera, GraphicsDevice device, SpriteBatch spriteBatch, Texture2D Texture)
{
     Vector3 pos1= device.Viewport.Project(
          this.position, 
          camera.Projection, camera.View, camera.World);
     Vector3 pos2= device.Viewport.Project(
          this.position+ Vactor3.UnitY*10, 
          camera.Projection, camera.View, camera.World);
     Vector2 pos = new Vector2(pos1.X, pos1.Y);
     Vector2 origin = new Vector2(Texture.Bounds.Center.X, Texture.Bounds.Center.Y);
     float Scale = Vector3.Distance(pos1, pos2) * CustomRatio;

     spriteBatch.Begin();
     spriteBatch.Draw(Texture, pos, null, Color.White, 0, 
                      origin, Scale, SpriteEffects.None, 0);
     spriteBatch.End();

    device.RasterizerState = RasterizerState.CullCounterClockwise;
    device.BlendState = BlendState.Opaque;
    device.DepthStencilState = DepthStencilState.Default;
}

另一方面......你以前的代码似乎是从Xna背后的人发表的一篇文章中提取的,该文章解释了如何使用basiceffect用spritebatch在3D中绘制广告牌......

http://blogs.msdn.com/b/shawnhar/archive/2011/01/12/spritebatch-billboards-in-a-3d-world.aspx

我希望它可以帮到你

答案 1 :(得分:0)

想出来,你必须在效果上启用纹理,然后在绘制函数中调用effect.Texture之前将spriteBatch.Begin()设置为你想要使用的Texture2D