Mesh
Unity Mesh 관련 예제이다.
MeshDataPrinter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshDataPrinter : MonoBehaviour
{
[SerializeField]
private MeshFilter _targetMesh;
// Start is called before the first frame update
void Start()
{
PrintMeshData(_targetMesh.mesh);
}
public static void PrintMeshData(Mesh mesh)
{
Debug.Log("vertices");
foreach (var v in mesh.vertices)
{
Debug.Log(v);
}
Debug.Log("triangles");
foreach (var t in mesh.triangles)
{
Debug.Log(t);
}
Debug.Log("normals");
foreach (var n in mesh.normals)
{
Debug.Log(n);
}
Debug.Log("uvs");
foreach (var u in mesh.uv)
{
Debug.Log(u);
}
Debug.Log("colors");
foreach (var u in mesh.colors)
{
Debug.Log(u);
}
Debug.Log("tangents");
foreach (var t in mesh.tangents)
{
Debug.Log(t);
}
}
}
MeshDataGenerator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshGenerator : MonoBehaviour
{
public float width = 5;
public float height = 5;
public Material material;
public void Start()
{
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.material = material;
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[4]
{
new Vector3(width, 0, 0),
new Vector3(width, 0, -height),
new Vector3(0, 0, -height),
new Vector3(0, 0, 0)
};
mesh.vertices = vertices;
int[] tris = new int[6]
{
// lower left triangle
0, 1, 2,
// upper right triangle
0, 2, 3
};
mesh.triangles = tris;
Vector3[] normals = new Vector3[4]
{
new Vector3(0, 1, 0),
new Vector3(0, 1, 0),
new Vector3(0, 1, 0),
new Vector3(0, 1, 0)
};
mesh.normals = normals;
Vector2[] uv = new Vector2[4]
{
new Vector2(0, 0.5f),
new Vector2(0, 0.7f),
new Vector2(0.2f, 0.7f),
new Vector2(0.2f, 0.5f)
};
mesh.uv = uv;
Vector4[] tangents = new Vector4[4]
{
new Vector4(-1.0f, 0, 0, -1.0f),
new Vector4(-1.0f, 0, 0, -1.0f),
new Vector4(-1.0f, 0, 0, -1.0f),
new Vector4(-1.0f, 0, 0, -1.0f)
};
mesh.tangents = tangents;
meshFilter.mesh = mesh;
meshFilter.mesh.Optimize();
meshFilter.mesh.RecalculateNormals();
meshFilter.mesh.RecalculateTangents();
meshFilter.mesh.RecalculateBounds();
meshFilter.mesh.MarkModified();
MeshDataPrinter.PrintMeshData(mesh);
}
}
'게임 엔진 > Unity' 카테고리의 다른 글
[Unity] [Example] 유니티에서 데이터 관리 방법(ScriptableObject) (0) | 2021.07.12 |
---|---|
[Unity] [Example] 평면 연산 방법 (Plane) (0) | 2020.07.28 |
[Unity] [Example] Singleton (0) | 2020.07.13 |
[Unity] Hp Bar 만들기 (0) | 2020.07.13 |
[Unity] 글로벌 이벤트 관리 방법 (1) | 2020.07.06 |