개요

  • Unity에서 직접 Mesh를 제작하느 방법에 대해 알아보자.

BuildMesh 예시

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class buildMesh : MonoBehaviour {

    public Vector3 vertLeftTopFront     = new Vector3(-1,+1,+1);
    public Vector3 vertLeftTopBack         = new Vector3(-1,+1,-1);

    public Vector3 vertRightTopFront     = new Vector3(+1,+1,+1);
    public Vector3 vertRightTopBack     = new Vector3(+1,+1,-1);

    public List<Vector3> v3list = new List<Vector3>();


    public bool m_bCreate;

    // Use this for initialization
    void Start () {

        MeshFilter mf = GetComponent<MeshFilter>();
        Mesh mesh = mf.mesh;

        //Vertices//
        Vector3[] vertices = new Vector3[]
        {
            //front face//
            vertLeftTopFront,//left top front, 0
            vertRightTopFront,//right top front, 1

            new Vector3(1,-1,1),//right bottom front, 3
            new Vector3(-1,-1,1),//left bottom front, 2

            /*

            //back face//
            vertRightTopBack,//right top back, 4
            vertLeftTopBack,//left top back, 5
            new Vector3(1,-1,-1),//right bottom back, 6
            new Vector3(-1,-1,-1),//left bottom back, 7

            //left face//
            vertLeftTopBack,//left top back, 8
            vertLeftTopFront,//left top front, 9
            new Vector3(-1,-1,-1),//left bottom back, 10
            new Vector3(-1,-1,1),//left bottom front, 11

            //right face//
            vertRightTopFront,//right top front, 12
            vertRightTopBack,//right top back, 13
            new Vector3(1,-1,1),//right bottom front, 14
            new Vector3(1,-1,-1),//right bottom back, 15

            //top face//
            vertLeftTopBack,//left top back, 16
            vertRightTopBack,//right top back, 17
            vertLeftTopFront,//left top front, 18
            vertRightTopFront,//right top front, 19

            //bottom face//
            new Vector3(-1,-1,1),//left bottom front, 20
            new Vector3(1,-1,1),//right bottom front, 21
            new Vector3(-1,-1,-1),//left bottom back, 22
            new Vector3(1,-1,-1)//right bottom back, 23
            */

        };


        Vector2[] uvs = new Vector2[vertices.Length];

        for (int i=0; i < uvs.Length; i++) {

            // -1. 1
            // 1, 1
            // 1, -1
            // -1.-1



            uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
        }

        //Triangles// 3 points, clockwise determines which side is visible
        int[] triangles = new int[]
        {
            //front face//
            0,1,2,//first triangle
            2,3,0,//second triangle

            /*
            //back face//
            4,6,7,//first triangle
            7,5,4,//second triangle

            //left face//
            8,10,11,//first triangle
            11,9,8,//second triangle

            //right face//
            12,14,15,//first triangle
            15,13,12,//second triangle

            //top face//
            16,18,19,//first triangle
            19,17,16,//second triangle

            //bottom face//
            20,22,23,//first triangle
            23,21,20//second triangle   
            */
        };



        mesh.Clear ();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;
        mesh.RecalculateNormals();

    }

    // Update is called once per frame
    void Update () {

        if (m_bCreate) {
            Mesh mesh = GetComponent<MeshFilter>().mesh;

            // 3 -> 4
            int len = mesh.vertices.Length + 1;

            // 4
            Vector3[] vertices = new Vector3[len];

            // 3
            for (int i = 0; i < len-1; i++) {
                    vertices[i] = mesh.vertices [i];
            }


            vertices [len - 1] = new Vector3 (1, 2, 1);

            mesh.vertices = vertices;
            mesh.RecalculateBounds();

            m_bCreate = false;
        }

    }
}

+ Recent posts