DrawUserPrimitives抱怨顶点着色器缺少 Color0

发布于 2024-12-13 01:01:42 字数 838 浏览 0 评论 0原文

首先,我对 XNA 以及 GPU 的工作方式以及它如何与 XNA(或 DirectX)API 协作感到陌生。

我有一个使用 SpriteBatch 绘制的多边形。我正在对多边形进行三角测量,并创建一个 VertexPositionTexture 数组来保存顶点。我设置了顶点(为了简单起见,将纹理偏移向量设置为零),并尝试绘制图元,但出现此错误:

当前顶点声明不包括当前顶点所需的所有元素着色器。 Color0 丢失。

这是我的代码,我已经仔细检查了三角测量的矢量,它们很好:

        VertexPositionTexture[] vertices = new VertexPositionTexture[triangulationResult.Count * 3];
        int ctr = 0;
        foreach (var item in triangulationResult)
        {
            foreach (var point in item.Vertices)
            {
                vertices[ctr++] = new VertexPositionTexture(new Vector3(point.X, point.Y, 0), Vector2.Zero);
            }
        }

        sb.GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, triangulationResult.Count);

我在这里可能做错了什么?

First of all, I am new to XNA and the way GPU works and how it cooperates with the XNA (or DirectX) API.

I have a polygon to draw using the SpriteBatch. I'm triangulating the polygon, and creating a VertexPositionTexture array to hold the vertices. I set the vertices (and just for simplicity, set the texture offset vector to zero), and try to draw the primitives, but I get this error:

The current vertex declaration does not include all the elements required by the current vertex shader. Color0 is missing.

Here is my code, I've double checked my vectors from triangulation, they are fine:

        VertexPositionTexture[] vertices = new VertexPositionTexture[triangulationResult.Count * 3];
        int ctr = 0;
        foreach (var item in triangulationResult)
        {
            foreach (var point in item.Vertices)
            {
                vertices[ctr++] = new VertexPositionTexture(new Vector3(point.X, point.Y, 0), Vector2.Zero);
            }
        }

        sb.GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, triangulationResult.Count);

What am I possibly doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂柯人 2024-12-20 01:01:42

您的着色器需要顶点流中的颜色......因此您必须使用 VertexPositionColorTexture 或更改您的着色器。

看来您没有使用任何着色器。如果活动着色器是与 spritebatch 一起使用的着色器,您将无法正确绘制它。

    VertexPositionColorTexture[] vertices = new VertexPositionColorTexture[triangulationResult.Count * 3];
    int ctr = 0;
    foreach (var item in triangulationResult)
    {
        foreach (var point in item.Vertices)
        {
            vertices[ctr++] = new VertexPositionColorTexture(new Vector3(point.X, point.Y, 0), Color.White, Vector2.Zero);
        }
    }

    sb.GraphicsDevice.DrawUserPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleList, vertices, 0, triangulationResult.Count);

Your shader is expecting a Color in the vertex stream.... so you have to use VertexPositionColorTexture or change your shader.

It seems that you are not using any shader. If the active shader is the one used with spritebatch you won't be able to draw it right.

    VertexPositionColorTexture[] vertices = new VertexPositionColorTexture[triangulationResult.Count * 3];
    int ctr = 0;
    foreach (var item in triangulationResult)
    {
        foreach (var point in item.Vertices)
        {
            vertices[ctr++] = new VertexPositionColorTexture(new Vector3(point.X, point.Y, 0), Color.White, Vector2.Zero);
        }
    }

    sb.GraphicsDevice.DrawUserPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleList, vertices, 0, triangulationResult.Count);
落叶缤纷 2024-12-20 01:01:42

如果要绘制多边形,请使用 BasicEffect (MSDN 教程< /a>)。您应该只使用 SpriteBatch 进行精灵绘制(即:使用其 Draw 方法)。

BasicEffect 所需的顶点元素类型取决于您对其应用的设置。

要使用不带颜色组件的顶点元素类型(例如 VertexPositionTexture),请将 BasicEffect.VertexColorEnabled 设置为 false。

或者,使用提供颜色的顶点元素类型,例如 VertexPositionColorTexture

如果您想创建与 SpriteBatch 具有相同坐标系的 BasicEffect,请参阅 这个答案这篇博文

Use BasicEffect if you are drawing polygons (MSDN tutorial). You should only use SpriteBatch for sprite drawing (ie: using its Draw methods).

The vertex element type that BasicEffect requires will depend on what settings you apply to it.

To use a vertex element type without a colour component (like VertexPositionTexture), set BasicEffect.VertexColorEnabled to false.

Or alternately, use a vertex element type that supplies a colour, such as VertexPositionColorTexture.

If you want to create a BasicEffect that has the same coordinate system as SpriteBatch, see this answer or this blog post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文