DrawUserPrimitives抱怨顶点着色器缺少 Color0
首先,我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的着色器需要顶点流中的颜色......因此您必须使用 VertexPositionColorTexture 或更改您的着色器。
看来您没有使用任何着色器。如果活动着色器是与 spritebatch 一起使用的着色器,您将无法正确绘制它。
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.
如果要绘制多边形,请使用
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 useSpriteBatch
for sprite drawing (ie: using itsDraw
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
), setBasicEffect.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 asSpriteBatch
, see this answer or this blog post.