Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XNA 4.0中绘制具有多个边的纹理立方体_C#_Xna - Fatal编程技术网

C# 在XNA 4.0中绘制具有多个边的纹理立方体

C# 在XNA 4.0中绘制具有多个边的纹理立方体,c#,xna,C#,Xna,我已经为这个问题绞尽脑汁好几个小时了。我想做的是画一个立方体,每边都有不同的纹理;或者更具体地说,我希望能够指定我想要的每面纹理。我首先使用了这个示例,然后尝试进一步开发它,这样我就可以拥有多个纹理。但是,无论我做什么,它仍然只使用应用于效果的最后一个纹理,并且不考虑以前的任何指定。这是我的形体课: public class BasicShape { public Vector3 shapeSize; public Vector3 shapePosition; private Verte

我已经为这个问题绞尽脑汁好几个小时了。我想做的是画一个立方体,每边都有不同的纹理;或者更具体地说,我希望能够指定我想要的每面纹理。我首先使用了这个示例,然后尝试进一步开发它,这样我就可以拥有多个纹理。但是,无论我做什么,它仍然只使用应用于效果的最后一个纹理,并且不考虑以前的任何指定。这是我的形体课:

public class BasicShape {

 public Vector3 shapeSize;
 public Vector3 shapePosition;
 private VertexPositionNormalTexture[][] shapeVertices;
 private int shapeTriangles;
 private VertexBuffer shapeBuffer;
 public Texture2D topTexture;
 public Texture2D frontTexture;
 public Texture2D backTexture;
 public Texture2D leftTexture;
 public Texture2D rightTexture;
 public Texture2D bottomTexture;

 public BasicShape(Vector3 size, Vector3 position) {
    shapeSize = size;
    shapePosition = position;
 }

 private void BuildShape() {
    shapeTriangles = 12;

    shapeVertices = new VertexPositionNormalTexture[6][];
    for(int i = 0; i < 6; i++) {
        shapeVertices[i] = new VertexPositionNormalTexture[6];
    }

    Vector3 topLeftFront = shapePosition +
     new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
    Vector3 bottomLeftFront = shapePosition +
     new Vector3(0.0f, 0.0f, 0.0f) * shapeSize;
    Vector3 topRightFront = shapePosition +
     new Vector3(1.0f, 1.0f, 0.0f) * shapeSize;
    Vector3 bottomRightFront = shapePosition +
     new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;
    Vector3 topLeftBack = shapePosition +
     new Vector3(0.0f, 1.0f, 1.0f) * shapeSize;
    Vector3 topRightBack = shapePosition +
     new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
    Vector3 bottomLeftBack = shapePosition +
     new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
    Vector3 bottomRightBack = shapePosition +
     new Vector3(1.0f, 0.0f, 1.0f) * shapeSize;

    Vector3 topLeftFront2 = shapePosition +
     new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
    Vector3 bottomLeftFront2 = shapePosition +
     new Vector3(0.0f, 0.0f, 0.0f) * shapeSize;
    Vector3 topRightFront2 = shapePosition +
     new Vector3(1.0f, 1.0f, 0.0f) * shapeSize;
    Vector3 bottomRightFront2 = shapePosition +
     new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;
    Vector3 topLeftBack2 = shapePosition +
     new Vector3(0.0f, 1.0f, 1.0f) * shapeSize;
    Vector3 topRightBack2 = shapePosition +
     new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
    Vector3 bottomLeftBack2 = shapePosition +
     new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
    Vector3 bottomRightBack2 = shapePosition +
     new Vector3(1.0f, 0.0f, 1.0f) * shapeSize;

    Vector3 topLeftFront3 = shapePosition +
     new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
    Vector3 bottomLeftFront3 = shapePosition +
     new Vector3(0.0f, 0.0f, 0.0f) * shapeSize;
    Vector3 topRightFront3 = shapePosition +
     new Vector3(1.0f, 1.0f, 0.0f) * shapeSize;
    Vector3 bottomRightFront3 = shapePosition +
     new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;
    Vector3 topLeftBack3 = shapePosition +
     new Vector3(0.0f, 1.0f, 1.0f) * shapeSize;
    Vector3 topRightBack3 = shapePosition +
     new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
    Vector3 bottomLeftBack3 = shapePosition +
     new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
    Vector3 bottomRightBack3 = shapePosition +
     new Vector3(1.0f, 0.0f, 1.0f) * shapeSize;

    Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
    Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f) * shapeSize;
    Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
    Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f) * shapeSize;
    Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f) * shapeSize;
    Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;

    Vector2 textureTopLeft = new Vector2(1f * shapeSize.X, 0.0f * shapeSize.Y);
    Vector2 textureTopRight = new Vector2(0.0f * shapeSize.X, 0.0f * shapeSize.Y);
    Vector2 textureBottomLeft = new Vector2(1f * shapeSize.X, 1f * shapeSize.Y);
    Vector2 textureBottomRight = new Vector2(0.0f * shapeSize.X, 1f * shapeSize.Y);

    // Front face.
    shapeVertices[0][0] = new VertexPositionNormalTexture(
     topLeftFront, frontNormal, textureTopLeft);
    shapeVertices[0][1] = new VertexPositionNormalTexture(
     bottomLeftFront, frontNormal, textureBottomLeft);
    shapeVertices[0][2] = new VertexPositionNormalTexture(
     topRightFront, frontNormal, textureTopRight);
    shapeVertices[0][3] = new VertexPositionNormalTexture(
     bottomLeftFront, frontNormal, textureBottomLeft);
    shapeVertices[0][4] = new VertexPositionNormalTexture(
     bottomRightFront, frontNormal, textureBottomRight);
    shapeVertices[0][5] = new VertexPositionNormalTexture(
     topRightFront, frontNormal, textureTopRight);

    // Back face.
    shapeVertices[1][0] = new VertexPositionNormalTexture(
     topLeftBack, backNormal, textureTopRight);
    shapeVertices[1][1] = new VertexPositionNormalTexture(
     topRightBack, backNormal, textureTopLeft);
    shapeVertices[1][2] = new VertexPositionNormalTexture(
     bottomLeftBack, backNormal, textureBottomRight);
    shapeVertices[1][3] = new VertexPositionNormalTexture(
     bottomLeftBack, backNormal, textureBottomRight);
    shapeVertices[1][4] = new VertexPositionNormalTexture(
     topRightBack, backNormal, textureTopLeft);
    shapeVertices[1][5] = new VertexPositionNormalTexture(
     bottomRightBack, backNormal, textureBottomLeft);

    // Top face.
    shapeVertices[2][0] = new VertexPositionNormalTexture(
     topLeftFront2, topNormal, textureBottomLeft);
    shapeVertices[2][1] = new VertexPositionNormalTexture(
     topRightBack2, topNormal, textureTopRight);
    shapeVertices[2][2] = new VertexPositionNormalTexture(
     topLeftBack2, topNormal, textureTopLeft);
    shapeVertices[2][3] = new VertexPositionNormalTexture(
     topLeftFront2, topNormal, textureBottomLeft);
    shapeVertices[2][4] = new VertexPositionNormalTexture(
     topRightFront2, topNormal, textureBottomRight);
    shapeVertices[2][5] = new VertexPositionNormalTexture(
     topRightBack2, topNormal, textureTopRight);

    // Bottom face.
    shapeVertices[3][0] = new VertexPositionNormalTexture(
     bottomLeftFront2, bottomNormal, textureTopLeft);
    shapeVertices[3][1] = new VertexPositionNormalTexture(
     bottomLeftBack2, bottomNormal, textureBottomLeft);
    shapeVertices[3][2] = new VertexPositionNormalTexture(
     bottomRightBack2, bottomNormal, textureBottomRight);
    shapeVertices[3][3] = new VertexPositionNormalTexture(
     bottomLeftFront2, bottomNormal, textureTopLeft);
    shapeVertices[3][4] = new VertexPositionNormalTexture(
     bottomRightBack2, bottomNormal, textureBottomRight);
    shapeVertices[3][5] = new VertexPositionNormalTexture(
     bottomRightFront2, bottomNormal, textureTopRight);

    // Left face.
    shapeVertices[4][0] = new VertexPositionNormalTexture(
     topLeftFront3, leftNormal, textureTopRight);
    shapeVertices[4][1] = new VertexPositionNormalTexture(
     bottomLeftBack3, leftNormal, textureBottomLeft);
    shapeVertices[4][2] = new VertexPositionNormalTexture(
     bottomLeftFront3, leftNormal, textureBottomRight);
    shapeVertices[4][3] = new VertexPositionNormalTexture(
     topLeftBack3, leftNormal, textureTopLeft);
    shapeVertices[4][4] = new VertexPositionNormalTexture(
     bottomLeftBack3, leftNormal, textureBottomLeft);
    shapeVertices[4][5] = new VertexPositionNormalTexture(
     topLeftFront3, leftNormal, textureTopRight);

    // Right face.
    shapeVertices[5][0] = new VertexPositionNormalTexture(
     topRightFront3, rightNormal, textureTopLeft);
    shapeVertices[5][1] = new VertexPositionNormalTexture(
     bottomRightFront3, rightNormal, textureBottomLeft);
    shapeVertices[5][2] = new VertexPositionNormalTexture(
     bottomRightBack3, rightNormal, textureBottomRight);
    shapeVertices[5][3] = new VertexPositionNormalTexture(
     topRightBack3, rightNormal, textureTopRight);
    shapeVertices[5][4] = new VertexPositionNormalTexture(
     topRightFront3, rightNormal, textureTopLeft);
    shapeVertices[5][5] = new VertexPositionNormalTexture(
     bottomRightBack3, rightNormal, textureBottomRight);
 }

 public void SetTopTexture(Texture2D tex) {
    topTexture = tex;
 }
 public void SetSideTexture(Texture2D tex) {
    frontTexture = tex;
    backTexture = tex;
    leftTexture = tex;
    rightTexture = tex;
 }
 public void SetBottomTexture(Texture2D tex) {
    bottomTexture = tex;
 }

 public void RenderShape(GraphicsDevice device, Effect effect) {
    BuildShape();


    effect.Parameters["xTexture"].SetValue(topTexture);
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[2], 0, 2);
    effect.Parameters["xTexture"].SetValue(bottomTexture);
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[3], 0, 2);
    effect.Parameters["xTexture"].SetValue(frontTexture);
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[0], 0, 2);
    effect.Parameters["xTexture"].SetValue(backTexture);
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[1], 0, 2);
    effect.Parameters["xTexture"].SetValue(leftTexture);
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[4], 0, 2);
    effect.Parameters["xTexture"].SetValue(rightTexture);
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[5], 0, 2);

 }
如您所见,我正在加载不同的纹理,但结果如下:


我确信这些纹理是不同的,但是在所有的侧面都绘制了相同的纹理。我需要为每一个面分别设置一个效果吗?这看起来确实有些过分。

在调用EffectPass.Apply()之前,效果上设置的任何参数都不会应用。这是因为将更改应用于效果非常昂贵,并且您可能希望一次性执行多个更改

您的RenderShape函数应该如下所示:

public void RenderShape(GraphicsDevice device, Effect effect)
{
    BuildShape();

    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    {
        effect.Parameters["xTexture"].SetValue(topTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[2], 0, 2);

        effect.Parameters["xTexture"].SetValue(bottomTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[3], 0, 2);

        effect.Parameters["xTexture"].SetValue(frontTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[0], 0, 2);

        effect.Parameters["xTexture"].SetValue(backTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[1], 0, 2);

        effect.Parameters["xTexture"].SetValue(leftTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[4], 0, 2);

        effect.Parameters["xTexture"].SetValue(rightTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[5], 0, 2);
    }
}

您是否验证了
TextureLoader.GetTexture(35)
正在返回您希望它返回的内容?@ChrisF-嗯,纹理35是唯一显示的纹理,因此我假设它是。另外,如果我在RenderShape中获取绘制代码的前两行并将其放在末尾,它将渲染纹理4。因此,它只是渲染最后应用的纹理。这似乎是您应该在问题中包含的有用信息。不过我还是忍不住要回答——我对xna不太熟悉。@Empyrean——那么,你是说这不是推荐的方法吗?什么是更快的方法?我需要画很多这样的立方体,所以我需要尽可能地优化它。在遇到性能问题之前,通常只能进行几百次状态更改(称为批处理)。在代码中,每个多维数据集的呈现由6个批次组成。这意味着在帧速率开始变得不可接受之前,您可能能够渲染少于100个立方体。您需要尽可能少的绘制调用,因此在一个绘制调用中绘制具有相同纹理的所有立方体面。最好将六个纹理组合成一个纹理(纹理贴图集)。此外,PC上的顶点缓冲区比DrawUserPrimitive调用快得多。最快的方法是使用纹理贴图,根据立方体是否移动,使用静态或动态顶点缓冲区在一次调用中渲染所有立方体。如果其中一些不移动,则对它们使用静态顶点缓冲区,对移动的则使用动态顶点缓冲区。还应使用索引以避免使用重复的顶点。重复的顶点必须同时进行变换,并且它们的变换方式可能会略有不同,从而在网格中形成接缝。此外,重复的顶点必须进行变换,通过总线发送,并且会占用缓存中的空间,所有这些都会降低性能。@Empyrean-我将如何处理贴图集?我是否使用顶点的纹理坐标?所以我需要在一个大的纹理贴图上找到每个纹理的坐标?
public void RenderShape(GraphicsDevice device, Effect effect)
{
    BuildShape();

    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    {
        effect.Parameters["xTexture"].SetValue(topTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[2], 0, 2);

        effect.Parameters["xTexture"].SetValue(bottomTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[3], 0, 2);

        effect.Parameters["xTexture"].SetValue(frontTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[0], 0, 2);

        effect.Parameters["xTexture"].SetValue(backTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[1], 0, 2);

        effect.Parameters["xTexture"].SetValue(leftTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[4], 0, 2);

        effect.Parameters["xTexture"].SetValue(rightTexture);
        pass.Apply();
        device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[5], 0, 2);
    }
}