Last active
March 13, 2019 11:13
-
-
Save LJH960101/cf0591fa632f5dea3e2ed977a010f251 to your computer and use it in GitHub Desktop.
Engine Programming first homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MinecraftCharacter : MonoBehaviour | |
{ | |
class Quad | |
{ | |
public Vector3[] vertices; | |
public Vector2[] uvs; | |
public int[] triangles; | |
public static int SizeOfVertex() | |
{ | |
return 4; | |
} | |
public static int SizeOfIndex() | |
{ | |
return 6; | |
} | |
public static Quad CreateQuad(Vector3 center, Vector3 rotation, Vector2 uvLeftDown, Vector2 uvRightTop, float size = 5f, bool isReverse = false) | |
{ | |
Quad quad = new Quad(); | |
// 중심이 0,0,0인 벡터 만들기 | |
quad.vertices = new Vector3[] { | |
new Vector3(- size/2, - size/2, 0f), | |
new Vector3(- size/2, size/2, 0f), | |
new Vector3(size/2, size/2, 0f), | |
new Vector3( size/2, - size/2, 0f) | |
}; | |
// uv 만들기 | |
quad.uvs = new Vector2[] | |
{ | |
new Vector2(uvLeftDown.x, uvLeftDown.y), | |
new Vector2(uvLeftDown.x, uvRightTop.y), | |
new Vector2(uvRightTop.x, uvRightTop.y), | |
new Vector2(uvRightTop.x, uvLeftDown.y) | |
}; | |
// 벡터 회전하기 | |
Matrix4x4 rotateMatrix = Matrix4x4.Rotate(Quaternion.Euler(rotation)); | |
for (int i = 0; i < 4; ++i) | |
{ | |
quad.vertices[i] = rotateMatrix * quad.vertices[i]; | |
} | |
// 회전한 벡터 움직이기 | |
for (int i = 0; i < 4; ++i) | |
{ | |
quad.vertices[i] = center + quad.vertices[i]; | |
} | |
if (isReverse) | |
{ | |
quad.triangles = new int[] | |
{ | |
0, 1, 2, | |
2, 3, 0 | |
}; | |
} | |
else | |
{ | |
quad.triangles = new int[] | |
{ | |
0, 2, 1, | |
2, 0, 3 | |
}; | |
} | |
return quad; | |
} | |
}; | |
class Cube | |
{ | |
public Vector3[] vertices; | |
public Vector2[] uvs; | |
public int[] triangles; | |
public static int SizeOfVertex() | |
{ | |
return Quad.SizeOfVertex() * 6; | |
} | |
public static int SizeOfIndex() | |
{ | |
return Quad.SizeOfIndex() * 6; | |
} | |
/* Parameters : | |
V3: Center, front, back, left, right, top, bottom | |
V2,V2: front, back, left, right, top, bottom | |
Vector3: scale | |
*/ | |
public static Cube CreateCube(Vector3 center, | |
Vector2 uvLeftDown_front, Vector2 uvRightTop_front, | |
Vector2 uvLeftDown_back, Vector2 uvRightTop_back, | |
Vector2 uvLeftDown_left, Vector2 uvRightTop_left, | |
Vector2 uvLeftDown_right, Vector2 uvRightTop_right, | |
Vector2 uvLeftDown_top, Vector2 uvRightTop_top, | |
Vector2 uvLeftDown_bottom, Vector2 uvRightTop_bottom, | |
Vector3 scale) | |
{ | |
Cube cube = new Cube(); | |
cube.vertices = new Vector3[24]; | |
cube.uvs = new Vector2[24]; | |
cube.triangles = new int[36]; | |
int triangleCount = 0, indexCount = 0, quadCount = 0; | |
for (int k = 1; k <= 6; ++k) | |
{ | |
Quad quad; | |
switch (k) | |
{ | |
case 1: // 정면 | |
quad = Quad.CreateQuad(new Vector3(0f, 0f, -0.5f), Vector3.zero, uvLeftDown_front, uvRightTop_front, 1f, true); | |
break; | |
case 2: // 후면 | |
quad = Quad.CreateQuad(new Vector3(0f, 0f, 0.5f), Vector3.zero, uvLeftDown_back, uvRightTop_back, 1f); | |
break; | |
case 3: // 좌 | |
quad = Quad.CreateQuad(new Vector3(-0.5f, 0f, 0f), new Vector3(0.0f, -90.0f, 0.0f), uvLeftDown_left, uvRightTop_left, 1f); | |
break; | |
case 4: // 우 | |
quad = Quad.CreateQuad(new Vector3(0.5f, 0f, 0f), new Vector3(0.0f, 90.0f, 0.0f), uvLeftDown_right, uvRightTop_right, 1f); | |
break; | |
case 5: // 상단 | |
quad = Quad.CreateQuad(new Vector3(0f, 0.5f, 0f), new Vector3(-90.0f, 0.0f, 0.0f), uvLeftDown_top, uvRightTop_top, 1f); | |
break; | |
case 6: // 하단 | |
quad = Quad.CreateQuad(new Vector3(0f, -0.5f, 0f), new Vector3(90.0f, 0.0f, 0.0f), uvLeftDown_bottom, uvRightTop_bottom, 1f); | |
break; | |
default: | |
Debug.Log("잘못된 K"); | |
throw new System.Exception(); | |
} | |
// 위치 * 이동 변환 행렬 | |
Matrix4x4 scaleAndTranslateMatrix = Matrix4x4.Scale(scale) * Matrix4x4.Translate(center); | |
for (int i = 0; i < quad.vertices.Length; ++i) | |
{ | |
// 버텍스 위치를 스케일, 센터에 맞춰준다. | |
quad.vertices[i] = scaleAndTranslateMatrix.MultiplyPoint3x4(quad.vertices[i]); | |
cube.vertices[triangleCount] = quad.vertices[i]; | |
cube.uvs[triangleCount] = quad.uvs[i]; | |
++triangleCount; | |
} | |
for (int i = 0; i < quad.triangles.Length; ++i) | |
{ | |
cube.triangles[indexCount] = quad.triangles[i] + quadCount * 4; | |
++indexCount; | |
} | |
++quadCount; | |
} | |
return cube; | |
} | |
}; | |
private void Awake() | |
{ | |
MeshFilter mf = GetComponent<MeshFilter>(); | |
Mesh m = new Mesh(); | |
Vector3[] vertices = new Vector3[Cube.SizeOfVertex() * 6]; | |
Vector2[] uvs = new Vector2[Cube.SizeOfVertex() * 6]; | |
int[] triangles = new int[Cube.SizeOfIndex() * 6]; | |
int vertexCount = 0, indexCount = 0, quadCount = 0; | |
const float pixel4 = 0.0625f; | |
const float pixel8 = 0.125f; | |
const float pixel12 = 0.1875f; | |
const float pixel16 = 0.25f; | |
for (int i_part=1; i_part <= 6; ++i_part) | |
{ | |
Cube cube = new Cube(); | |
switch (i_part) | |
{ | |
case 1: // head | |
cube = Cube.CreateCube(new Vector3(0.0f, 1.25f, 0.0f), | |
new Vector2(0.125f, 0.75f), new Vector2(0.25f, 0.875f), | |
new Vector2(0.375f, 0.75f), new Vector2(0.5f, 0.875f), | |
new Vector2(0.25f, 0.75f), new Vector2(0.375f, 0.875f), | |
new Vector2(0f, 0.75f), new Vector2(0.125f, 0.875f), | |
new Vector2(0.125f, 0.875f), new Vector2(0.25f, 1f), | |
new Vector2(0.25f, 0.875f), new Vector2(0.375f, 1f), | |
new Vector3(1f, 1f, 0.5f)); | |
break; | |
case 2: // body | |
cube = Cube.CreateCube(new Vector3(0.0f, 0.0f, 0.0f), | |
new Vector2(0.3125f, 0.5f), new Vector2(0.3125f + pixel8, 0.5f + pixel12), | |
new Vector2(0.3125f + pixel8, 0.5f), new Vector2(0.5f + pixel8, 0.6875f), | |
new Vector2(0.5625f, 0.5f), new Vector2(0.5625f + pixel4, 0.5f + pixel12), | |
new Vector2(pixel16, 0.5f), new Vector2(pixel16 + pixel4, 0.5f + pixel12), | |
new Vector2(0.3125f, 0.6875f), new Vector2(0.3125f + 0.125f, 0.75f), | |
new Vector2(0.3125f + pixel8, 0.6875f), new Vector2(0.3125f + pixel8 + 0.125f, 0.75f), | |
new Vector3(1.0f, 1.5f, 0.5f)); | |
break; | |
case 3: // left arm | |
cube = Cube.CreateCube(new Vector3(-1.5f, 0.0f, 0.0f), | |
new Vector2(0.5f + pixel4, 0f), new Vector2(0.5f + pixel4 * 2, pixel12), | |
new Vector2(0.5f + pixel4 * 3, 0f), new Vector2(0.5f + pixel4 * 4, pixel12), | |
new Vector2(0.5f + pixel4 * 2, 0f), new Vector2(0.5f + pixel4 * 3, pixel12), | |
new Vector2(0.5f, 0f), new Vector2(0.5f + pixel4, pixel12), | |
new Vector2(0.5625f + pixel4, pixel12), new Vector2(0.5625f + pixel4 * 2, pixel12 + pixel4), | |
new Vector2(0.625f, pixel12), new Vector2(0.625f + pixel4, pixel12 + pixel4), | |
new Vector3(0.5f, 1.5f, 0.5f)); | |
break; | |
case 4: // right arm | |
cube = Cube.CreateCube(new Vector3(1.5f, 0.0f, 0.0f), | |
new Vector2(0.6875f, 0.5f), new Vector2(0.6875f + pixel4, 0.5f + pixel12), | |
new Vector2(0.6875f + pixel4 * 2, 0.5f), new Vector2(0.6875f + pixel4 * 3, 0.5f + pixel12), | |
new Vector2(0.6875f + pixel4, 0.5f), new Vector2(0.6875f + pixel4 * 2, 0.5f + pixel12), | |
new Vector2(0.6875f - pixel4, 0.5f), new Vector2(0.6875f, 0.5f + pixel12), | |
new Vector2(0.6875f + pixel4, 0.6875f), new Vector2(0.6875f + pixel4 * 2, 0.6875f + pixel4), | |
new Vector2(0.6875f + pixel4, 0.5f + pixel12), new Vector2(0.6875f + pixel4 * 2, 0.5f + pixel12 + pixel4), | |
new Vector3(0.5f, 1.5f, 0.5f)); | |
break; | |
case 5: // left leg | |
cube = Cube.CreateCube(new Vector3(-0.5f, -1.0f, 0.0f), | |
new Vector2(0.25f + pixel4, 0f), new Vector2(0.25f + pixel4 * 2, 0f + pixel12), | |
new Vector2(0.25f + pixel4 * 3, 0f), new Vector2(0.25f + pixel4 * 4, 0f + pixel12), | |
new Vector2(0.25f + pixel4 * 2, 0f), new Vector2(0.25f + +pixel4 * 3, 0f + pixel12), | |
new Vector2(0.25f, 0f), new Vector2(0.25f + pixel4, 0f + pixel12), | |
new Vector2(0.3125f, pixel12), new Vector2(0.3125f + pixel4, pixel12 + pixel4), | |
new Vector2(0.3125f + pixel4, pixel12), new Vector2(0.3125f + pixel4 * 2, pixel12 + pixel4), | |
new Vector3(0.5f, 1.5f, 0.5f)); | |
break; | |
case 6: // right leg | |
cube = Cube.CreateCube(new Vector3(0.5f, -1.0f, 0.0f), | |
new Vector2(pixel4, 0.5f), new Vector2(pixel4 * 2, 0.5f + pixel12), | |
new Vector2(pixel4 * 3, 0.5f), new Vector2(pixel4 * 4, 0.5f + pixel12), | |
new Vector2(pixel4 * 2, 0.5f), new Vector2(pixel4 * 3, 0.5f + pixel12), | |
new Vector2(0.0f, 0.5f), new Vector2(pixel4, 0.5f + pixel12), | |
new Vector2(pixel4, 0.6875f), new Vector2(pixel4 * 2, 0.6875f + pixel4), | |
new Vector2(pixel4 * 2, 0.6875f), new Vector2(pixel4 * 3, 0.6875f + pixel4), | |
new Vector3(0.5f, 1.5f, 0.5f)); | |
break; | |
default: | |
Debug.Log("Bad parts value!!!"); | |
throw new System.Exception(); | |
} | |
for (int j_vertex = 0; j_vertex < cube.vertices.Length; ++j_vertex) | |
{ | |
vertices[vertexCount] = cube.vertices[j_vertex]; | |
uvs[vertexCount++] = cube.uvs[j_vertex]; | |
} | |
for (int j_index = 0; j_index < cube.triangles.Length; ++j_index) | |
{ | |
triangles[indexCount] = cube.triangles[j_index] + quadCount * Cube.SizeOfVertex(); | |
++indexCount; | |
} | |
++quadCount; | |
} | |
m.vertices = vertices; | |
m.triangles = triangles; | |
m.uv = uvs; | |
mf.mesh = m; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment