SHARE
TWEET

Untitled

a guest Mar 25th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [ExecuteInEditMode]
  6. public class MineCraftCharacter : MonoBehaviour
  7. {
  8.     public Transform[] bonse;
  9.     SkinnedMeshRenderer smr;
  10.     private void Awake()
  11.     {
  12.         CreateCharacter c = new CreateCharacter();
  13.         Mesh m = c.GetCharacterMesh(bonse, transform);
  14.         MeshFilter mf = GetComponent<MeshFilter>();
  15.         mf.mesh = m;
  16.  
  17.         smr = GetComponent<SkinnedMeshRenderer>();
  18.         smr.bones = bonse;
  19.         smr.sharedMesh = m;
  20.  
  21.         smr.quality = SkinQuality.Bone1;
  22.     }
  23. }
  24.  
  25. public class CreateCharacter
  26. {
  27.     int vertexCount = 24;
  28.     int triangleCount = 36;
  29.     int uvCount = 24;
  30.     int cubeCount = 6;
  31.  
  32.     Vector3[] vertices;
  33.     int[] triangles;
  34.     Vector2[] uv;
  35.     Mesh m;
  36.  
  37.     public Mesh GetCharacterMesh(Transform[] bones, Transform tf)
  38.     {
  39.  
  40.         vertices = new Vector3[vertexCount * cubeCount];
  41.         triangles = new int[triangleCount * cubeCount];
  42.         uv = new Vector2[uvCount * cubeCount];
  43.  
  44.         m = new Mesh();
  45.  
  46.         for (int i = 0; i < vertexCount; i++)//버텍스 생성
  47.         {
  48.             vertices[i] = CreateMeshCube(new Vector3(-5, -5, -5), 5f, 5f, 5f)[i];//머리
  49.             vertices[i + vertexCount] = CreateMeshCube(new Vector3(-10f, -20f, -5), 2.5f, 2.5f, 7.5f)[i];//왼팔
  50.             vertices[i + vertexCount * 2] = CreateMeshCube(new Vector3(5f, -20f, -5), 2.5f, 2.5f, 7.5f)[i];//오른팔
  51.             vertices[i + vertexCount * 3] = CreateMeshCube(new Vector3(-5f, -20f, -5), 2.5f, 5f, 7.5f)[i];//몸통
  52.             vertices[i + vertexCount * 4] = CreateMeshCube(new Vector3(-5f, -35f, -5), 2.5f, 2.5f, 7.5f)[i];//오른다리
  53.             vertices[i + vertexCount * 5] = CreateMeshCube(new Vector3(0f, -35f, -5), 2.5f, 2.5f, 7.5f)[i];//왼다리
  54.  
  55.         }
  56.  
  57.         for (int i = 0; i < triangleCount; i++)//두르기
  58.         {
  59.             for (int j = 0; j < cubeCount; j++)
  60.                 triangles[i + triangleCount * j] = GetTriangles(j + 1)[i];
  61.         }
  62.  
  63.         for (int i = 0; i < uvCount; i++)//uv그리기
  64.         {
  65.             uv[i] = DrawUVCube(new Vector2(0f, 6f / 8f), 1f, 1f, 1f)[i];//머리
  66.             uv[i + uvCount] = DrawUVCube(new Vector2(5f / 8f, 4f / 8f), 0.5f, 0.5f, 1.5f)[i];//왼팔
  67.             uv[i + uvCount * 2] = DrawUVCube(new Vector2(4f / 8f, 0f), 0.5f, 0.5f, 1.5f)[i];//오른팔
  68.             uv[i + uvCount * 3] = DrawUVCube(new Vector2(2f / 8f, 4f / 8f), 0.5f, 1f, 1.5f)[i];//몸통
  69.             uv[i + uvCount * 4] = DrawUVCube(new Vector2(0f, 4f / 8f), 0.5f, 0.5f, 1.5f)[i];//오른다리
  70.             uv[i + uvCount * 5] = DrawUVCube(new Vector2(2f / 8f, 0), 0.5f, 0.5f, 1.5f)[i];//왼다리
  71.  
  72.         }
  73.  
  74.         m.vertices = vertices;
  75.         m.triangles = triangles;
  76.         m.uv = uv;
  77.  
  78.         SetBoneWeight(bones,tf);
  79.  
  80.         return m;
  81.     }
  82.  
  83.     //버텍스 생성
  84.     Vector3[] CreateMeshCube(Vector3 startPos, float horiz, float vertical, float height)//시작점, 크기
  85.     {
  86.         Vector3[] reVal;
  87.         reVal = new Vector3[] {
  88.  
  89.            new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),
  90.            new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),
  91.            new Vector3(startPos.x,startPos.y+height*2f,startPos.z),
  92.            new Vector3(startPos.x,startPos.y,startPos.z),//오른쪽
  93.  
  94.            new Vector3(startPos.x,startPos.y,startPos.z),
  95.            new Vector3(startPos.x,startPos.y+height*2f, startPos.z),
  96.            new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
  97.            new Vector3(startPos.x+vertical*2f,startPos.y, startPos.z),//앞
  98.            
  99.            new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z),
  100.            new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
  101.            new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
  102.            new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),//왼
  103.            
  104.            new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),
  105.            new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
  106.            new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),
  107.            new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),//뒤
  108.  
  109.            new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
  110.            new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
  111.            new Vector3(startPos.x,startPos.y+height*2f,startPos.z),
  112.            new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),//위
  113.  
  114.            new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z),
  115.            new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),
  116.            new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),
  117.            new Vector3(startPos.x,startPos.y,startPos.z),//밑
  118.            
  119.         };
  120.  
  121.  
  122.         return reVal;
  123.     }
  124.  
  125.     //두르기 순서
  126.     int[] GetTriangles(int count)
  127.     {
  128.         int[] reVal = new int[36];
  129.         count -= 1;
  130.         count *= 24;
  131.  
  132.         reVal = new int[]
  133.         {
  134.             count+0,count+1,count+2,
  135.             count+0,count+2,count+3,//오
  136.  
  137.             count+4,count+5,count+6,
  138.             count+4,count+6,count+7,//앞
  139.  
  140.             count+8,count+9,count+10,
  141.             count+8,count+10,count+11,//왼
  142.  
  143.             count+12,count+13,count+14,
  144.             count+12,count+14,count+15,//뒤
  145.  
  146.             count+16,count+17,count+18,
  147.             count+16,count+18,count+19,//위
  148.  
  149.             count+20,count+21,count+22,
  150.             count+20,count+22,count+23//아래
  151.         };
  152.         return reVal;
  153.     }
  154.  
  155.     //uv씌우기
  156.     Vector2[] DrawUVCube(Vector2 startPos, float horiz, float vertical, float height)//시작점, 크기
  157.     {
  158.         Vector2[] reVal = new Vector2[]
  159.         {
  160.             new Vector2((0f * vertical/8f) + 0f * (horiz/8f) + startPos.x,startPos.y),
  161.             new Vector2((0f * vertical/8f) + 0f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  162.             new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  163.             new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//오
  164.                    
  165.             new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
  166.             new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  167.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  168.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//앞
  169.                  
  170.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
  171.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  172.             new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  173.             new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//뒤
  174.                    
  175.             new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
  176.             new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  177.             new Vector2((2f * vertical/8f) + 2f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  178.             new Vector2((2f * vertical/8f) + 2f * (horiz/8f) + startPos.x, startPos.y),//왼
  179.            
  180.            
  181.             new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  182.             new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  183.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  184.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),//위
  185.  
  186.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  187.             new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  188.             new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  189.             new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),//아래
  190.         };
  191.         return reVal;
  192.     }
  193.  
  194.     void SetBoneWeight(Transform[] bones, Transform tf)
  195.     {
  196.         Matrix4x4[] transformData;
  197.         BoneWeight[] bwData;
  198.  
  199.         transformData = new Matrix4x4[6];
  200.  
  201.         for (int i = 0; i < 6; i++)
  202.         {
  203.             transformData[i] = bones[i].worldToLocalMatrix * tf.localToWorldMatrix;
  204.         }
  205.         bwData = new BoneWeight[vertexCount * cubeCount];
  206.      
  207.         for(int i=0;i< vertexCount * cubeCount; i++)
  208.         {
  209.             int tmp = i / vertexCount;
  210.             bwData[i] = new BoneWeight() { boneIndex0 = tmp, weight0 = 1.0f };
  211.         }
  212.         m.bindposes = transformData;
  213.         m.boneWeights = bwData;
  214.     }
  215. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top