预先变换的纹理多边形

时间:2012-09-12 03:50:45

标签: c# 3d xna texturing

我只是从3D开始,所以我有一个关于在屏幕上绘制预转换内容的问题。

我能够通过使用一些教程弄清楚如何以预转换形式绘制彩色多边形,但我无法理解如何纹理它们......这甚至可能吗?如果是这样的话?

我现在拥有的:

private void TestVertices()
{
 vertices = new VertexPositionColor[3];
 vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
 vertices[0].Color = Color.Red;
 vertices[1].Position = new Vector3(0, 0.5f, 0f);
 vertices[1].Color = Color.Green;
 vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
 vertices[2].Color = Color.Blue;
}

protected override void Draw(GameTime gameTime)
{
     device.Clear(Color.DarkSlateBlue);
     effect.CurrentTechnique = effect.Techniques["Pretransformed"];
     foreach (EffectPass pass in effect.CurrentTechnique.Passes)
     {
         pass.Apply();
         device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1, VertexPositionColor.VertexDeclaration);
     }
     base.Draw(gameTime);
}

1 个答案:

答案 0 :(得分:1)

而不是VertexPositionColor,使用VertexPositionColorTexture作为顶点,并将TextureCoordinate成员设置为每个轴上0到1之间的值。

绘图时,将GraphicsDevice.Texture[0]设置为您要使用的纹理。

确保您的像素着色器执行以下操作:

// in the header:
sampler myTexture : register(s0);

// in your shader function:
float4 outputColor = input.Color * tex2D(myTexture, input.TexCoord);

如果您使用的是BasicEffect,则相当于使用VertexPositionColorTexture,将BasicEffect.Texture设置为纹理,然后设置BasicEffect.TextureEnabled = true