Created
March 19, 2019 17:15
-
-
Save bbagwang/0443985b594c23f0ad10320472393195 to your computer and use it in GitHub Desktop.
MineCraftCharacter
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 | |
{ | |
enum UV_Type { HEAD, BODY, LEGARM }; | |
static float p4 = 0.0625f; | |
static float p8 = 0.125f; | |
static float p12 = 0.1875f; | |
static float p16 = 0.25f; | |
[SerializeField] | |
private Vector3[] vertices = new Vector3[4 * 6 * 6]; | |
[SerializeField] | |
private Vector2[] uv = new Vector2[4 * 6 * 6]; | |
[SerializeField] | |
private int[] triangles = new int[6 * 6 * 6]; | |
class Cube | |
{ | |
public Vector3[] vertices; | |
public Vector2[] uv; | |
public int[] triangles; | |
public Cube CreateCube(Vector3 center, float width, float height, Vector2 origin, UV_Type type, int count) | |
{ | |
vertices = new Vector3[]{ | |
//RIGHT | |
new Vector3(center.x + width / 2, center.y - height / 2, center.z - width / 2), //3 //0 | |
new Vector3(center.x + width / 2, center.y + height / 2, center.z - width / 2), //2 //1 | |
new Vector3(center.x + width / 2, center.y + height / 2, center.z + width / 2), //6 //2 | |
new Vector3(center.x + width / 2, center.y - height / 2, center.z + width / 2), //7 //3 | |
//FRONT | |
new Vector3(center.x - width / 2, center.y - height / 2, center.z - width / 2), //0 //4 | |
new Vector3(center.x - width / 2, center.y + height / 2, center.z - width / 2), //1 //5 | |
new Vector3(center.x + width / 2, center.y + height / 2, center.z - width / 2), //2 //6 | |
new Vector3(center.x + width / 2, center.y - height / 2, center.z - width / 2), //3 //7 | |
//TOP | |
new Vector3(center.x - width / 2, center.y + height / 2, center.z - width / 2), //1 //8 | |
new Vector3(center.x - width / 2, center.y + height / 2, center.z + width / 2), //5 //9 | |
new Vector3(center.x + width / 2, center.y + height / 2, center.z + width / 2), //6 //10 | |
new Vector3(center.x + width / 2, center.y + height / 2, center.z - width / 2), //2 //11 | |
//LEFT | |
new Vector3(center.x - width / 2, center.y - height / 2, center.z + width / 2), //4 //12 | |
new Vector3(center.x - width / 2, center.y + height / 2, center.z + width / 2), //5 //13 | |
new Vector3(center.x - width / 2, center.y + height / 2, center.z - width / 2), //1 //14 | |
new Vector3(center.x - width / 2, center.y - height / 2, center.z - width / 2), //0 //15 | |
//BOTTOM | |
new Vector3(center.x - width / 2, center.y - height / 2, center.z - width / 2), //0 //16 | |
new Vector3(center.x + width / 2, center.y - height / 2, center.z - width / 2), //3 //17 | |
new Vector3(center.x + width / 2, center.y - height / 2, center.z + width / 2), //7 //18 | |
new Vector3(center.x - width / 2, center.y - height / 2, center.z + width / 2), //4 //19 | |
//BACK | |
new Vector3(center.x + width / 2, center.y - height / 2, center.z + width / 2), //7 //20 | |
new Vector3(center.x + width / 2, center.y + height / 2, center.z + width / 2), //6 //21 | |
new Vector3(center.x - width / 2, center.y + height / 2, center.z + width / 2), //5 //22 | |
new Vector3(center.x - width / 2, center.y - height / 2, center.z + width / 2), //4 //23 | |
}; | |
switch (type) | |
{ | |
case UV_Type.HEAD: | |
uv = new Vector2[] | |
{ | |
//RIGHT | |
new Vector2(origin.x,origin.y), | |
new Vector2(origin.x,origin.y+p8), | |
new Vector2(origin.x+p8,origin.y+p8), | |
new Vector2(origin.x+p8,origin.y), | |
//FRONT | |
new Vector2(origin.x+p8,origin.y), | |
new Vector2(origin.x+p8,origin.y+p8), | |
new Vector2(origin.x+p16,origin.y+p8), | |
new Vector2(origin.x+p16,origin.y), | |
//TOP | |
new Vector2(origin.x+p8,origin.y+p8), | |
new Vector2(origin.x+p8,origin.y+p16), | |
new Vector2(origin.x+p16,origin.y+p16), | |
new Vector2(origin.x+p16,origin.y+p8), | |
//LEFT | |
new Vector2(origin.x+p16,origin.y), | |
new Vector2(origin.x+p16,origin.y+p8), | |
new Vector2(origin.x+p16+p8,origin.y+p8), | |
new Vector2(origin.x+p16+p8,origin.y), | |
//BOTTOM | |
new Vector2(origin.x+p16,origin.y+p8), | |
new Vector2(origin.x+p16,origin.y+p16), | |
new Vector2(origin.x+p16+p8,origin.y+p16), | |
new Vector2(origin.x+p16+p8,origin.y+p8), | |
//BACK | |
new Vector2(origin.x+p16+p8,origin.y), | |
new Vector2(origin.x+p16+p8,origin.y+p8), | |
new Vector2(origin.x+p16+p16,origin.y+p8), | |
new Vector2(origin.x+p16+p16,origin.y) | |
}; | |
break; | |
case UV_Type.BODY: | |
uv = new Vector2[] | |
{ | |
//RIGHT | |
new Vector2(origin.x,origin.y), | |
new Vector2(origin.x,origin.y+p12), | |
new Vector2(origin.x+p4,origin.y+p12), | |
new Vector2(origin.x+p4,origin.y), | |
//FRONT | |
new Vector2(origin.x+p4,origin.y), | |
new Vector2(origin.x+p4,origin.y+p12), | |
new Vector2(origin.x+p12,origin.y+p12), | |
new Vector2(origin.x+p12,origin.y), | |
//TOP | |
new Vector2(origin.x+p4,origin.y+p12), | |
new Vector2(origin.x+p4,origin.y+p16), | |
new Vector2(origin.x+p12,origin.y+p16), | |
new Vector2(origin.x+p12,origin.y+p12), | |
//LEFT | |
new Vector2(origin.x+p16+p4,origin.y), | |
new Vector2(origin.x+p16+p4,origin.y+p12), | |
new Vector2(origin.x+p16+p8,origin.y+p12), | |
new Vector2(origin.x+p16+p8,origin.y), | |
//BOTTOM | |
new Vector2(origin.x+p12,origin.y+p12), | |
new Vector2(origin.x+p12,origin.y+p16), | |
new Vector2(origin.x+p16+p4,origin.y+p16), | |
new Vector2(origin.x+p16+p4,origin.y+p12), | |
//BACK | |
new Vector2(origin.x+p12,origin.y), | |
new Vector2(origin.x+p12,origin.y+p12), | |
new Vector2(origin.x+p16+p4,origin.y+p12), | |
new Vector2(origin.x+p16+p4,origin.y) | |
}; | |
break; | |
case UV_Type.LEGARM: | |
uv = new Vector2[] | |
{ | |
//RIGHT | |
new Vector2(origin.x,origin.y), | |
new Vector2(origin.x,origin.y+p12), | |
new Vector2(origin.x+p4,origin.y+p12), | |
new Vector2(origin.x+p4,origin.y), | |
//FRONT | |
new Vector2(origin.x+p4,origin.y), | |
new Vector2(origin.x+p4,origin.y+p12), | |
new Vector2(origin.x+p8,origin.y+p12), | |
new Vector2(origin.x+p8,origin.y), | |
//TOP | |
new Vector2(origin.x+p4,origin.y+p12), | |
new Vector2(origin.x+p4,origin.y+p16), | |
new Vector2(origin.x+p8,origin.y+p16), | |
new Vector2(origin.x+p8,origin.y+p12), | |
//LEFT | |
new Vector2(origin.x+p8,origin.y), | |
new Vector2(origin.x+p8,origin.y+p12), | |
new Vector2(origin.x+p12,origin.y+p12), | |
new Vector2(origin.x+p12,origin.y), | |
//BOTTOM | |
new Vector2(origin.x+p8,origin.y+p12), | |
new Vector2(origin.x+p8,origin.y+p16), | |
new Vector2(origin.x+p12,origin.y+p16), | |
new Vector2(origin.x+p12,origin.y+p12), | |
//BACK | |
new Vector2(origin.x+p12,origin.y), | |
new Vector2(origin.x+p12,origin.y+p12), | |
new Vector2(origin.x+p16,origin.y+p12), | |
new Vector2(origin.x+p16,origin.y) | |
}; | |
break; | |
} | |
triangles = new int[] | |
{ | |
//RIGHT | |
count*24+0,count*24+1,count*24+2, | |
count*24+2,count*24+3,count*24+0, | |
//FRONT | |
count*24+4,count*24+5,count*24+6, | |
count*24+6,count*24+7,count*24+4, | |
//TOP | |
count*24+8,count*24+9,count*24+10, | |
count*24+10,count*24+11,count*24+8, | |
//LEFT | |
count*24+12,count*24+13,count*24+14, | |
count*24+14,count*24+15,count*24+12, | |
//BOTTOM | |
count*24+16,count*24+17,count*24+18, | |
count*24+18,count*24+19,count*24+16, | |
//BACK | |
count*24+20,count*24+21,count*24+22, | |
count*24+22,count*24+23,count*24+20 | |
}; | |
return this; | |
} | |
} | |
void Awake() | |
{ | |
MeshFilter mf = GetComponent<MeshFilter>(); | |
Mesh m = new Mesh(); | |
for (int i = 0; i < 6; i++) | |
{ | |
Cube cube = new Cube(); | |
switch (i) | |
{ | |
case 0: //HEAD | |
cube = cube.CreateCube(Vector3.zero, 10.0f, 10.0f, new Vector2(0, 1 - p16), UV_Type.HEAD, i); | |
break; | |
case 1: //BODY | |
cube = cube.CreateCube(new Vector3(0, -12.5f), 10.0f, 15.0f, new Vector2(p16, 1 - p16 * 2), UV_Type.BODY, i); | |
break; | |
case 2: //LEFT ARM | |
cube = cube.CreateCube(new Vector3(-7.0f, -12.5f), 4.0f, 15.0f, new Vector2(p16 * 2, 0), UV_Type.LEGARM, i); | |
break; | |
case 3: //RIGHT ARM | |
cube = cube.CreateCube(new Vector3(7.0f, -12.5f), 4.0f, 15.0f, new Vector2(1 - p16 - p8, p16 * 2), UV_Type.LEGARM, i); | |
break; | |
case 4: //LEFT LEG | |
cube = cube.CreateCube(new Vector3(-2.5f, -25.5f), 6.0f, 22.5f, new Vector2(p16, 0), UV_Type.LEGARM, i); | |
break; | |
case 5: //RIGHT LEG | |
cube = cube.CreateCube(new Vector3(2.5f, -25.5f), 6.0f, 22.5f, new Vector2(0, p16 * 2), UV_Type.LEGARM, i); | |
break; | |
default: | |
break; | |
} | |
for (int x = 0; x < 4 * 6; x++) | |
{ | |
vertices[i * 4 * 6 + x] = cube.vertices[x]; | |
uv[i * 4 * 6 + x] = cube.uv[x]; | |
} | |
for (int x = 0; x < 6 * 6; x++) | |
{ | |
triangles[i * 6 * 6 + x] = cube.triangles[x]; | |
} | |
} | |
m.vertices = this.vertices; | |
m.uv = this.uv; | |
m.triangles = this.triangles; | |
mf.mesh = m; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment