【问题标题】:How to add a texture to dynamically generated terrain vertices in xna如何在 xna 中为动态生成的地形顶点添加纹理
【发布时间】:2013-02-14 04:29:19
【问题描述】:

我正在尝试将纹理坐标添加到每个顶点,以便将草纹理添加到每个三角形。我的代码将纹理延伸到整个区域,这可以工作但不能很好地放大。如何正确地将 (0,0)、(0,1)、(1,1) 等添加到顶点? 目前它们是在 SetUpVertices() 方法中添加的,如果代码可以区分它是左上角、左下角、右下角等,是否应该在 SetUpIndices() 方法中添加它们。任何帮助将不胜感激。相关方法如下,完整的Game1.cs代码在这里http://pastebin.com/REd8QDZA

    private void SetUpVertices()
    {
        vertices = new VertexPositionNormalTexture[terrainWidth * terrainHeight];
        for (int x = 0; x < terrainWidth; x++)
        {
            for (int y = 0; y < terrainHeight; y++)
            {
                vertices[x + y * terrainWidth].Position = new Vector3(x, -y, heightData[x, y]);
                vertices[x + y * terrainWidth].TextureCoordinate.X = x / (terrainWidth - 1.0);
                vertices[x + y * terrainWidth].TextureCoordinate.Y = y / (terrainHeight - 1.0);
            }
        }
    }
    private void SetUpIndices()
    {
        indices = new short[(terrainWidth - 1) * (terrainHeight - 1) * 6];
        int counter = 0;
        for (int y = 0; y < terrainHeight - 1; y++)
        {
            for (int x = 0; x < terrainWidth - 1; x++)
            {
                int lowerLeft = x + y * terrainWidth;
                int lowerRight = (x + 1) + y * terrainWidth;
                int topLeft = x + (y + 1) * terrainWidth;
                int topRight = (x + 1) + (y + 1) * terrainWidth;

                indices[counter++] = (short)topLeft;
                indices[counter++] = (short)lowerRight;
                indices[counter++] = (short)lowerLeft;

                indices[counter++] = (short)topLeft;
                indices[counter++] = (short)topRight;
                indices[counter++] = (short)lowerRight;
            }
        }
    }

【问题讨论】:

    标签: c# xna coordinates textures


    【解决方案1】:

    只要指定

    vertices[x + y * terrainWidth].TextureCoordinate.X = x;
    vertices[x + y * terrainWidth].TextureCoordinate.Y = y;
    

    默认情况下,大于1的纹理坐标会被包裹,纹理会重复。

    【讨论】:

    • 我在这家已经好几天了,这就是答案? FFS!!!!谢谢 Nico,你是明星。
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多