Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MineCraftCharacter : MonoBehaviour
- {
- private void Awake()
- {
- int vertexCount = 24;
- int triangleCount = 36;
- int uvCount = 24;
- int cubeCount = 6;
- Vector3[] vertices = new Vector3[vertexCount* cubeCount];
- int[] triangles = new int[triangleCount * cubeCount];
- Vector2[] uv = new Vector2[uvCount * cubeCount];
- MeshFilter mf = GetComponent<MeshFilter>();
- Mesh m = new Mesh();
- for (int i = 0; i < vertexCount; i++)//버텍스 생성
- {
- vertices[i] = CreateMeshCube(new Vector3(-5, -5, -5), 5f, 5f, 5f)[i];//머리
- vertices[i + vertexCount] = CreateMeshCube(new Vector3(-10f, -20f, -5), 2.5f, 2.5f, 7.5f)[i];//왼팔
- vertices[i + vertexCount * 2] = CreateMeshCube(new Vector3(5f, -20f, -5), 2.5f, 2.5f, 7.5f)[i];//오른팔
- vertices[i + vertexCount * 3] = CreateMeshCube(new Vector3(-5f, -20f, -5), 2.5f, 5f, 7.5f)[i];//몸통
- vertices[i + vertexCount * 4] = CreateMeshCube(new Vector3(-5f, -35f, -5), 2.5f, 2.5f, 7.5f)[i];//오른다리
- vertices[i + vertexCount * 5] = CreateMeshCube(new Vector3(0f, -35f, -5), 2.5f, 2.5f, 7.5f)[i];//왼다리
- }
- for (int i = 0; i < triangleCount; i++)//두르기
- {
- for (int j = 0; j < cubeCount; j++)
- triangles[i + triangleCount * j] = GetTriangles(j + 1)[i];
- }
- for (int i = 0; i < uvCount; i++)//uv그리기
- {
- uv[i] = DrawUVCube(new Vector2(0f, 6f / 8f), 1f, 1f, 1f)[i];//머리
- uv[i + uvCount] = DrawUVCube(new Vector2(5f / 8f, 4f / 8f), 0.5f, 0.5f, 1.5f)[i];//왼팔
- uv[i + uvCount * 2] = DrawUVCube(new Vector2(4f / 8f, 0f), 0.5f, 0.5f, 1.5f)[i];//오른팔
- uv[i + uvCount * 3] = DrawUVCube(new Vector2(2f / 8f, 4f / 8f), 0.5f, 1f, 1.5f)[i];//몸통
- uv[i + uvCount * 4] = DrawUVCube(new Vector2(0f , 4f / 8f), 0.5f, 0.5f, 1.5f)[i];//오른다리
- uv[i + uvCount * 5] = DrawUVCube(new Vector2(2f / 8f, 0), 0.5f, 0.5f, 1.5f)[i];//왼다리
- }
- m.vertices = vertices;
- m.triangles = triangles;
- m.uv = uv;
- mf.mesh = m;
- }
- //버텍스 생성
- Vector3[] CreateMeshCube(Vector3 startPos, float horiz, float vertical, float height)//시작점, 크기
- {
- Vector3[] reVal;
- reVal = new Vector3[] {
- new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),
- new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),
- new Vector3(startPos.x,startPos.y+height*2f,startPos.z),
- new Vector3(startPos.x,startPos.y,startPos.z),//오른쪽
- new Vector3(startPos.x,startPos.y,startPos.z),
- new Vector3(startPos.x,startPos.y+height*2f, startPos.z),
- new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
- new Vector3(startPos.x+vertical*2f,startPos.y, startPos.z),//앞
- new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z),
- new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
- new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
- new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),//왼
- new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),
- new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
- new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),
- new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),//뒤
- new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
- new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
- new Vector3(startPos.x,startPos.y+height*2f,startPos.z),
- new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),//위
- new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z),
- new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),
- new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),
- new Vector3(startPos.x,startPos.y,startPos.z),//밑
- };
- return reVal;
- }
- //두르기 순서
- int[] GetTriangles(int count)
- {
- int[] reVal = new int[36];
- count -= 1;
- count *= 24;
- reVal = new int[]
- {
- count+0,count+1,count+2,
- count+0,count+2,count+3,//오
- count+4,count+5,count+6,
- count+4,count+6,count+7,//앞
- count+8,count+9,count+10,
- count+8,count+10,count+11,//왼
- count+12,count+13,count+14,
- count+12,count+14,count+15,//뒤
- count+16,count+17,count+18,
- count+16,count+18,count+19,//위
- count+20,count+21,count+22,
- count+20,count+22,count+23//아래
- };
- return reVal;
- }
- //uv씌우기
- Vector2[] DrawUVCube(Vector2 startPos, float horiz, float vertical, float height)//시작점, 크기
- {
- Vector2[] reVal = new Vector2[]
- {
- new Vector2((0f * vertical/8f) + 0f * (horiz/8f) + startPos.x,startPos.y),
- new Vector2((0f * vertical/8f) + 0f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//오
- new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
- new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//앞
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//뒤
- new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
- new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((2f * vertical/8f) + 2f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((2f * vertical/8f) + 2f * (horiz/8f) + startPos.x, startPos.y),//왼
- new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),//위
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
- new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
- new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
- new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),//아래
- };
- return reVal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement