3

C#.netには、動的な直径と長さのメッシュシリンダーがあり、それにテクスチャをマッピングしようとしています。私はその方法を見つけるために一日の大部分を費やしましたが、Googleで情報を見つけることに成功しませんでした。

円柱のテクスチャにはjpgの上部領域があり、側面にはjpgの残りの部分があります。円柱の上端に沿ってjpg画像の端を配置する必要があります。例えば。1つの画像を使用して、上部が赤、側面が緑。

VertexBufferポイントをテクスチャにマッピングするのを手伝ってくれる人はいますか?

C#.Net 2008 DirectX 9(管理されていない)

私は以下に私の実用的な解決策を投稿しました

4

2 に答える 2

1

わかりました、私はついにそれを理解しました。以前は機能していたコードがいくつかありましたが、 http://channel9.msdn.com/coding4fun/articles/Ask-the-ZMan-Applying-Textures-Part-3から望んでいたものとは正確には 異なります

とにかく、私はそれにいくつかの改造をしました。

参考までに、そして Google から到着した方は、どうぞ。

public static float ComputeBoundingSphere(Mesh mesh, out Microsoft.DirectX.Vector3 center)
    {
        // Lock the vertex buffer
        Microsoft.DirectX.GraphicsStream data = null;
        try
        {
            data = mesh.LockVertexBuffer(LockFlags.ReadOnly);
            // Now compute the bounding sphere
            return Geometry.ComputeBoundingSphere(data, mesh.NumberVertices, 
                mesh.VertexFormat, out center);
        }
        finally
        {
            // Make sure to unlock the vertex buffer
            if (data != null)
                mesh.UnlockVertexBuffer();
        }
    }

    private static Mesh SetSphericalTexture(Mesh mesh)
    {
        Microsoft.DirectX.Vector3 vertexRay;
        Microsoft.DirectX.Vector3 meshCenter;
        double phi;
        float u;


        Microsoft.DirectX.Vector3 north = new Microsoft.DirectX.Vector3(0f, 0f, 1f);
        Microsoft.DirectX.Vector3 equator = new Microsoft.DirectX.Vector3(0f, 1f, 0f);
        Microsoft.DirectX.Vector3 northEquatorCross = Microsoft.DirectX.Vector3.Cross(north, equator);

        ComputeBoundingSphere(mesh, out meshCenter);

        using (VertexBuffer vb = mesh.VertexBuffer)
        {
            CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])vb.Lock(0, typeof(CustomVertex.PositionNormalTextured), LockFlags.None, mesh.NumberVertices);
            try
            {
                for (int i = 0; i < verts.Length; i++)
                {
                    //For each vertex take a ray from the centre of the mesh to the vertex and normalize so the dot products work.
                    vertexRay = Microsoft.DirectX.Vector3.Normalize(verts[i].Position - meshCenter);

                    phi = Math.Acos((double)vertexRay.Z);
                    if (vertexRay.Z > -0.9)
                    {
                        verts[i].Tv = 0.121f; //percentage of the image being the top side
                    }
                    else
                        verts[i].Tv = (float)(phi / Math.PI);

                    if (vertexRay.Z == 1.0f || vertexRay.Z == -1.0f)
                    {
                        verts[i].Tu = 0.5f;
                    }
                    else
                    {
                        u = (float)(Math.Acos(Math.Max(Math.Min((double)vertexRay.Y / Math.Sin(phi), 1.0), -1.0)) / (2.0 * Math.PI));
                        //Since the cross product is just giving us (1,0,0) i.e. the xaxis 
                        //and the dot product was giving us a +ve or -ve angle, we can just compare the x value with 0
                        verts[i].Tu = (vertexRay.X > 0f) ? u : 1 - u;
                    }
                }
            }
            finally
            {
                vb.Unlock();
            }
        }
        return mesh;
    }
于 2011-04-08T01:48:07.300 に答える
1

このチュートリアルは VB ですが、プロセスを明確に説明しています。

テクスチャ座標の計算は、かなりの作業になる可能性があります。そのため、通常、これは 3D モデリング ソフトウェアによって行われるため、マッピングを簡単に、さらに重要なことに、視覚的に調整できます。

ご不明な点がございましたら、お知らせください。

編集

DirecxtX で生成された円柱にテクスチャ座標を追加するには、これを参照してください

于 2011-04-07T06:16:56.147 に答える