【问题标题】:Problems with Textured Quad's texture showing up black纹理四边形的纹理显示为黑色的问题
【发布时间】:2016-10-25 18:24:29
【问题描述】:

我正在尝试使用来自 MSDN http://msdn.microsoft.com/en-us/library/bb464051.aspx 的示例代码在 XNA 中绘制带纹理的四边形

但不是在 XY 平面上绘制,而是在 XZ 平面上绘制

我用这个初始化那个四边形 this.quad = new Quad(Vector3.Zero, Vector3.Up, Vector3.Backward, 1, 1);

我已经尝试了法线和左矢量的几乎所有组合,无论在哪里绘制四边形,四边形都是完全黑色的。

我在这里做错了什么?当我传递它时,纹理的信息会丢失吗?我映射纹理位置是否错误?

ps:纹理、光照已启用。

public class Quad
{
    public VertexPositionNormalTexture[] Vertices;
    public Vector3 Origin;
    public Vector3 Up;
    public Vector3 Normal;
    public Vector3 Left;
    public Vector3 UpperLeft;
    public Vector3 UpperRight;
    public Vector3 LowerLeft;
    public Vector3 LowerRight;
    public int[] Indexes;


    public Quad(Vector3 origin, Vector3 normal, Vector3 up,
             float width, float height)
    {
            this.Vertices = new VertexPositionNormalTexture[4];
            this.Indexes = new int[6];
            this.Origin = origin;
            this.Normal = normal;
            this.Up = up;

            // Calculate the quad corners
            this.Left = Vector3.Cross(normal, this.Up);
            Vector3 uppercenter = (this.Up * height / 2) + origin;
            this.UpperLeft = uppercenter + (this.Left * width / 2);
            this.UpperRight = uppercenter - (this.Left * width / 2);
            this.LowerLeft = this.UpperLeft - (this.Up * height);
            this.LowerRight = this.UpperRight - (this.Up * height);

            this.FillVertices();
    }

    private void FillVertices()
    {
            Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f);
            Vector2 textureUpperRight = new Vector2(1.0f, 0.0f);
            Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f);
            Vector2 textureLowerRight = new Vector2(1.0f, 1.0f);

            for (int i = 0; i < this.Vertices.Length; i++)
            {
                    this.Vertices[i].Normal = this.Normal;
            }

            this.Vertices[0].Position = this.LowerLeft;
            this.Vertices[0].TextureCoordinate = textureLowerLeft;
            this.Vertices[1].Position = this.UpperLeft;
            this.Vertices[1].TextureCoordinate = textureUpperLeft;
            this.Vertices[2].Position = this.LowerRight;
            this.Vertices[2].TextureCoordinate = textureLowerRight;
            this.Vertices[3].Position = this.UpperRight;
            this.Vertices[3].TextureCoordinate = textureUpperRight;

            this.Indexes[0] = 0;
            this.Indexes[1] = 1;
            this.Indexes[2] = 2;
            this.Indexes[3] = 2;
            this.Indexes[4] = 1;
            this.Indexes[5] = 3;
    }
}

我的画法:

        graphicsDevice.BlendState = BlendState.AlphaBlend;

        Effect.World = Matrix.Identity;
        Effect.View = Camera.View;
        Effect.Projection = Camera.Projection;
        Effect.TextureEnabled = true;
        Effect.Texture = OilTexture;

        Effect.EnableDefaultLighting();
        Effect.PreferPerPixelLighting = true;


        foreach (EffectPass pass in oilEffect.CurrentTechnique.Passes)
        {
            pass.Apply();

            graphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>
                    (PrimitiveType.TriangleList,
                    Vertices,
                    0,
                    4,
                    Indexes,
                    0,
                    2);
        }

--我解决了我的问题。当我在 3 个地方传递 texture2d 时出了点问题,现在我从主绘图中绘制它,纹理现在显示正常。

【问题讨论】:

  • 您说启用了照明,但您是否设置了一个可以照亮四边形的灯?请发布呈现四边形的代码。
  • 您能否将您的解决方案发布为答案并接受它,而不是编辑您的问题。这会将其从所有人的未答复列表中删除。

标签: c# 3d xna-4.0


【解决方案1】:

四边形可能是黑色的原因有很多,这里有一些我喜欢做的调试:

  • 关闭闪电并设置垂直颜色以确保四边形的方向符合我的预期。
  • 如果可能,请使用示例纹理以确保纹理数据有效(我遇到很多不同版本的管道失败的问题 加载纹理,因为格式不是我所期望的。 DDS 有 如果您自己制作数据可能会很棘手)
  • 检查纹理是否在闪电关闭时可见
  • 打开闪电后,您需要确保为所有顶点设置了闪电值
  • 尝试将系统的环境值调到最大

如果这些都不起作用,请仔细查看将顶点闪电值、顶点法线值和照明向下传递到硬件的区域。

【讨论】:

  • 我在模拟程序中运行了该示例,结果显示良好。我认为我没有传递任何顶点闪电值,我应该在哪里设置?
  • 我只是在谈论 .Normal 值。您使用 设置正确传递它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
相关资源
最近更新 更多