unity练习

18_Mesh

2019-07-28  本文已影响0人  alphonseLin

Procedural Mesh02

  1. Vector3[] vertices, 告诉unity点在哪
  2. int[] Triangles,告诉unity怎么练(3个点相连)


    02.PM
[RequireComponent(typeof(MeshFilter))]//先声明出来
public class Procedural_Mesh : MonoBehaviour {

    Mesh mesh;

    Vector3[] vertices;
    int[] triangles;

    void Awake()
    {
        mesh = GetComponent<MeshFilter>().mesh;
    }


    // Use this for initialization
    void Start () {
        MakeMeshData();
        UpdateMesh();
    }
    
    void MakeMeshData()
    {
        //Create an array of vertices
        vertices = new Vector3[] { new Vector3(0,0,0),new Vector3(0,0,1),new Vector3(1,0,0)};

        //Create an array of int
        triangles = new int[] { 0, 1, 2 };
    }

    void UpdateMesh()
    {
        mesh.Clear();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
    }
}

Procedural Mesh03

03.PM
void MakeMeshData()
    {
        //Create an array of vertices
        vertices = new Vector3[] { new Vector3(0,0,0),new Vector3(0,0,1),new Vector3(1,0,0), new Vector3(1,0,1)};

        //Create an array of int
        triangles = new int[] { 0, 1, 2,2,1,3 };
    }

Procedural Mesh04

  1. vertices of Triangles作用,可包含信息:
    1)Normals, UVs, Tangents, Bone Weights, Vertex Colors
  2. Normals: for Lighting
  3. 六面,与四面的问题,共点问题


    6v&4v

Procedural Grid 05

  1. 移动网格
 void MakeProceduralGrid()
    {
        //set array sizes
        vertices = new Vector3[4];
        triangles = new int[6];

        //set vertex offset
        float vertexOffset = cellSize * 0.5f;

        //populate vertices and triangles sizes
        vertices[0] = new Vector3(-vertexOffset, 0, -vertexOffset)+gridOffset;
        vertices[1] = new Vector3(-vertexOffset, 0, vertexOffset) + gridOffset;
        vertices[2] = new Vector3(vertexOffset, 0, -vertexOffset) + gridOffset;
        vertices[3] = new Vector3(vertexOffset, 0, vertexOffset) + gridOffset;

        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 2;

        triangles[3] = 2;
        triangles[4] = 1;
        triangles[5] = 3;

    }

    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }

Procedural Grid 06 离散图形

  1. 用for语句,把网格变成可自动生成的模式
for (int x = 0; x < gridSize; x++)
        {
            for (int y = 0; y < gridSize; y++)
            {
                Vector3 cellOffset=new Vector3(x*cellSize,0,y*cellSize);

                //populate vertices and triangles sizes
                vertices[v] = new Vector3(-vertexOffset, 0, -vertexOffset) + cellOffset + gridOffset;
                vertices[v + 1] = new Vector3(-vertexOffset, 0, vertexOffset) + cellOffset + gridOffset;
                vertices[v + 2] = new Vector3(vertexOffset, 0, -vertexOffset) + cellOffset + gridOffset;
                vertices[v + 3] = new Vector3(vertexOffset, 0, vertexOffset) + cellOffset + gridOffset;

                triangles[t] = v;
                triangles[t + 1] = triangles[t + 4]=v + 1;
                triangles[t + 2] = triangles[t + 3]=v + 2;
                triangles[t + 5] = v+3;

                v += 4;
                t += 6;
            }
        }
06.PM

Procedural Grid 07 连续型网格

  1. 重写vertex+triangle,以保证共用一条边
void MakeContiguousProceduralGrid()
    {
        //set array sizes
        vertices = new Vector3[(gridSize + 1) * (gridSize + 1)];
        triangles = new int[gridSize * gridSize * 6];

        //set tracker integers
        int v = 0;
        int t = 0;

        //set vertex offset
        float vertexOffset = cellSize * 0.5f;

        //create vertext grid
        for (int x = 0; x <= gridSize; x++)
        {
            for (int y = 0; y <= gridSize; y++)
            {
                vertices[v] = new Vector3((x * cellSize) - vertexOffset, (x+y)*0.2f, (y * cellSize) - vertexOffset);
                v++;
            }
        }

        //reset vertex tracker
        v = 0;

        //setting each cell's triangles
        for (int x = 0; x < gridSize; x++)
        {
            for (int y = 0; y < gridSize; y++)
            {
                triangles[t] = v;
                triangles[t + 1] = triangles[t + 4] = v + 1;
                triangles[t + 2] = triangles[t + 3] = v + (gridSize+1);
                triangles[t + 5] = v +(gridSize+1)+1;
                v++;
                t += 6;
            }
            v++;
        }
    }
07.PM

Cube Basic 08

上一篇 下一篇

猜你喜欢

热点阅读