2

こちら(http://unitygametsukuruyo.blogspot.jp/2015/01/unity-csv.html)を参考にして作業を進めていたのですが、「フィールドに落とし込む」という意味がわかりませんでした。
そこでフイールドが「Plane」だと思い進めていくと、数値の数が少ない時は上手くいきました。しかし、数値の数がPlaneの元々の頂点の数を超えると、どのように設定を変更したらいいか分からず、そこで止まっている状態です。
自作Meshを作る場合では三角形を作る事になってしまい、csvファイルの並びをそのまま適応させたいので、三角形の頂点の順番と上手くかみ合いません。
そこで質問なのですが、
[1.]タイトルにあるような事をするのに地面をPlaneで作るのは正しいのか?
[2.]Planeの頂点の数を増やす方法はあるのか?
[3.]Planeではなく、このような場合Meshを自作するのか?
[4.]3の場合csvファイルの並びをそのままMeshに適応(対応)させる方法はあるのか?

[5.]長々と質問を書かせて頂きましたが、要はタイトルにある事をやりたいので他の方法があれば教えて下さればと思います。

どうぞ回答よろしくお願い致します。

1 件の回答 1

0

 Planeを使用するよりも、自分でメッシュを作成したほうが頂点配列を自分で指定できるため使いやすいと思います。
 Meshクラスのverticesプロパティに頂点配列を設定することができるため、CSVファイルの並びをそのまま適応できます。
 
 下記のサンプルを参考にしてみたください。
 サンプルではCSVファイルからではなく、固定配列データから頂点データを取得しています。

public static class FieldData
{
    public static float[,] HeightList = new float[,]
    {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,2,2,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,2,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,2,2,2,1,0,0,0,0,0,0,0,0,5,0,0,0,0},
        {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
        {0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,0,0,0},
        {0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,1,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
        {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    };

    public static int Depth
    {
        get { return HeightList.GetLength( 0 ); }
    }

    public static int Width
    {
        get { return HeightList.GetLength( 1 ); }
    }
}

 
 

public class TestMesh : MonoBehaviour
{
    private MeshFilter  m_meshFilter;

    void Awake()
    {
        gameObject.AddComponent< MeshRenderer >();
        m_meshFilter    = gameObject.AddComponent< MeshFilter >();
    }

    void Start()
    {
        var mesh        = new Mesh();

        var vertices    = MakeVertices();
        var indices     = MakeIndices();

        mesh.vertices   = vertices;
        mesh.SetIndices( indices, MeshTopology.Quads, 0 );
        mesh.name       = "TestMesh";

        m_meshFilter.mesh   = mesh;
    }

    Vector3[] MakeVertices()
    {
        int width       = FieldData.Width;
        int depth       = FieldData.Depth;
        var heightList  = FieldData.HeightList;

        var vertices    = new Vector3[ width * depth ];

        for( int i = 0; i < depth; ++i )
        {
            for( int j = 0; j < width; ++j )
            {
                int index   = i * width + j;
                vertices[ index ]   = new Vector3( i, heightList[i,j], j );
            }
        }

        return vertices;
    }

    int[] MakeIndices()
    {
        int     width       = FieldData.Width;
        int     depth       = FieldData.Depth;

        int     meshSize    = ( width - 1 ) * ( depth - 1 );
        int[]   indices     = new int[ meshSize * 4 ];

        int indicesIndex    = 0;
        for( int i = 0; i < depth - 1; ++i )
        {
            for( int j = 0; j < width - 1; ++j )
            {
                int index = i * width + j;
                indices[ indicesIndex++ ] = index + width;
                indices[ indicesIndex++ ] = index;
                indices[ indicesIndex++ ] = index + 1;
                indices[ indicesIndex++ ] = index + width + 1; 
            }
        }

        return indices;
    }
}

 
 上記のTestMeshコンポーネントをGameObjectに付けると以下のようなメッシュが作成されます。

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。