【问题标题】:How to calculate texture coords?如何计算纹理坐标?
【发布时间】:2014-05-12 10:17:43
【问题描述】:

我有一个程序,用户可以在其中创建不同宽度和高度的框。

我还有一个纹理(512 x 512 像素),应该放在每个盒子上。

现在我遇到的问题是在 90x90 Box (Mesh.Box) 上我可以将纹理放置 4x4 次。

所以我认为我可以使用 4/90 的比率来对盒子进行纹理处理。但这不起作用

框的宽度为 28.723,高度为 56.43661

我使用以下代码计算纹理坐标:

float tex_coords_w = 4.0f / 90;
float tex_coords_h = 4.0f / 90;

mesh = Mesh.Box(d3dDevice, width, height, 0);

        Mesh tempMesh = mesh.Clone(mesh.Options.Value, VertexFormats.PositionNormal | VertexFormats.Texture1, d3dDevice);
        CustomVertex.PositionNormalTextured[] vertData = (CustomVertex.PositionNormalTextured[])tempMesh.VertexBuffer.Lock(0, typeof(CustomVertex.PositionNormalTextured), LockFlags.None, tempMesh.NumberVertices);
        for (int i = vertData.Length / 2; i < vertData.Length; i += 4)
        {
            vertData[i + 1].Tu = Convert.ToSingle(i) + 0.0f;
            vertData[i + 1].Tv = Convert.ToSingle(i) + 0.0f;
            vertData[i + 2].Tu = Convert.ToSingle(i) + tex_coords_w * width;
            vertData[i + 2].Tv = Convert.ToSingle(i) + 0.0f;
            vertData[i + 3].Tu = Convert.ToSingle(i) + tex_coords_w * width;
            vertData[i + 3].Tv = Convert.ToSingle(i) + tex_coords_h * height;
            vertData[i + 0].Tu = Convert.ToSingle(i) + 0.0f;
            vertData[i + 0].Tv = Convert.ToSingle(i) + tex_coords_h * height;
        }
        tempMesh.VertexBuffer.Unlock();



        mesh.Dispose();
        mesh = tempMesh;

有人可以帮帮我吗?!

【问题讨论】:

    标签: c# directx texture-mapping


    【解决方案1】:

    如果你想在每个面上重复纹理 n 次。

    1. 将纹理寻址模式设置为WRAP模式

    2. 为盒子的每个面设置如下纹理坐标。

          A(0, 0)                    B(n, 0)
                 -------------------
                 |                 |
                 |                 |
                 |                 |
                 |                 |
                 |                 |
                 |                 |
                 |                 |
          D(0, n) ------------------- C(n, n)
      

    这里是代码示例,我使用的是原生DirectX,以顶点B为例,它的位置是(5.0, 5.0, 0),纹理坐标是(0, 4.0f),所以纹理重复4次在你的方向。

    // Vertex layout
    struct Vertex
    {
        float x, y, z ; // Position
        float u, v ;    // Texture coordinates
    };
    
    Vertex FrontFace[] = 
    {
        {-5.0f,  5.0f, 0,    0,    0},  // Vertex A
        { 5.0f,  5.0f, 0, 4.0f,    0},  // B
        { 5.0f, -5.0f, 0, 4.0f, 4.0f},  // C
        { 5.0f, -5.0f, 0, 4.0f,    0},  // D
    } ;
    

    通常,分配给顶点的 u 和 v 纹理坐标在 0.0 到 1.0 的范围内(包括 0.0 到 1.0)。但是,通过在该范围之外分配纹理坐标,您可以创建某些特殊的纹理效果。

    详情请参阅Texture Addressing Mode。这是针对原生 DirectX 的,但您可以简单地在 C# 中应用它。

    【讨论】:

    • 您好,感谢您的回答。这对我的 for 循环意味着什么?你能给我举个例子吗?谢谢
    • 假设vertData[0]从人脸的左上角开始,vertData[123]按照顺时针顺序,可以试试下面的代码vertData[0].Tu = 0; vertData[0].Tv = 0; vertData[1].Tu = 1; vertData[1].Tv = 0; vertData[2].Tu = 1; vertData[2].Tv = 1; vertData[3].Tu = 0; vertData[3].Tv = 1;
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多