我试图绘制应用了着色器全屏四,但绘图时我不断收到以下错误:在准备提请发生Monogame 3.4 - 效果投掷“InvalidOperationException:准备绘制时发生错误。”
错误。这可能是因为当前顶点声明不包含当前顶点着色器所需的所有元素。当前的顶点声明包含这些元素:SV_Position0,TEXCOORD0。
这是我宣布我的verticies为四:
_vertices = new VertexPositionTexture[4];
_vertices[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0));
_vertices[1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0));
_vertices[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1));
_vertices[3] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1));
这就是我如何绘制四(与联合国需要的东西省略了)
foreach (var pass in _lightEffect1.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _vertices, 0, 2, VertexPositionTexture.VertexDeclaration);
}
这里是适用于四边形的着色器
// Vertex shader input structure
struct VertexShaderInput
{
float4 Pos : SV_Position;
float2 TexCoord : TEXCOORD0;
};
// Vertex shader output structure
struct VertexShaderOutput
{
float4 Pos : SV_Position;
float2 TexCoord : TEXCOORD0;
};
VertexShaderOutput VertexToPixelShader(VertexShaderInput input)
{
VertexShaderOutput output;
output.Pos = input.Pos;
output.TexCoord = input.TexCoord;
return output;
}
float4 PointLightShader(VertexShaderOutput PSIn) : COLOR0
{
//Pixel shader code here....
return float4(shading.r, shading.g, shading.b, 1.0f);
}
technique DeferredPointLight
{
pass Pass1
{
VertexShader = compile vs_4_0_level_9_1 VertexToPixelShader();
PixelShader = compile ps_4_0_level_9_1 PointLightShader();
}
}
我注意到的一件事是,MonoGame提供的VertexPositionTexture中的定义是vec3的位置和texcords的vec2。然而在着色器中它是一个float4的位置和一个float2的texcoords。
我试着将它改为float3,但着色器不能编译。于是我试着创建自己的“VertexPositionTexture”结构,它有我自己定义的vec4,但我最终得到了同样的错误。
我并不是那么擅长DirectX,而且我尝试过遍寻谷歌,但我找不到任何可能导致问题的原因。
我在着色器中做错了什么?我错过了什么吗?