Skip to content

Instantly share code, notes, and snippets.

@bbagwang
Created March 21, 2019 05:44
Show Gist options
  • Save bbagwang/db94c4c389c7b9c424051a1cd46d491a to your computer and use it in GitHub Desktop.
Save bbagwang/db94c4c389c7b9c424051a1cd46d491a to your computer and use it in GitHub Desktop.
SkinnedQuad
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class SkinnedQuad : MonoBehaviour {
public Transform[] bones;
SkinnedMeshRenderer smr;
static public List<Vector3> vertices = new List<Vector3>();
static public List<Color> colors = new List<Color>();
static public List<int> triangles = new List<int>();
class Triangle
{
public void MakeTriangle(Vector3 center,int width, int height,int count)
{
vertices.Add(new Vector3(center.x - width / 2, center.y + height / 2, 0));
vertices.Add(new Vector3(center.x + width / 2, center.y + height / 2, 0));
vertices.Add(new Vector3(center.x, center.y - height / 2, 0));
colors.Add(new Color(255, 0, 0));
colors.Add(new Color(0, 255, 0));
colors.Add(new Color(0, 0, 255));
triangles.Add(count * 3 + 0);
triangles.Add(count * 3 + 1);
triangles.Add(count * 3 + 2);
}
}
private void Awake()
{
Mesh m = new Mesh();
for(int i=0;i<11;i++)
{
Triangle triangle = new Triangle();
switch (i)
{
case 0: //PELVIS
triangle.MakeTriangle(new Vector3(0.0f, 10.0f, 0.0f),5,5,i);
break;
case 1: //BODY_UP
triangle.MakeTriangle(new Vector3(0.0f, 6.0f, 0.0f), 10, 5, i);
break;
case 2: //BODY_DOWN
triangle.MakeTriangle(new Vector3(0.0f, -1.0f, 0.0f), 10, 10, i);
break;
case 3: //LEFT_ARM_UP
triangle.MakeTriangle(new Vector3(-7.0f, 3.0f, 0.0f), 5, 10, i);
break;
case 4: //RIGHT_ARM_UP
triangle.MakeTriangle(new Vector3(7.0f, 3.0f, 0.0f), 5, 10, i);
break;
case 5: //LEFT_ARM_DOWN
triangle.MakeTriangle(new Vector3(-7.0f, -7.0f, 0.0f), 5, 10, i);
break;
case 6: //RIGHT_ARM_DOWN
triangle.MakeTriangle(new Vector3(7.0f, -7.0f, 0.0f), 5, 10, i);
break;
case 7: //LEFT_LEG_UP
triangle.MakeTriangle(new Vector3(-2.0f, -11.0f, 0.0f), 5, 10, i);
break;
case 8: //RIGHT_LEG_UP
triangle.MakeTriangle(new Vector3(2.0f, -11.0f, 0.0f), 5, 10, i);
break;
case 9: //LEFT_LEG_DOWN
triangle.MakeTriangle(new Vector3(-2.0f, -21.0f, 0.0f), 5, 10, i);
break;
case 10://RIGHT_LEG_DOWN
triangle.MakeTriangle(new Vector3(2.0f, -21.0f, 0.0f), 5, 10, i);
break;
default:
Debug.Log("FUCKED");
break;
}
}
m.vertices = vertices.ToArray();
m.colors = colors.ToArray();
m.triangles = triangles.ToArray();
////본의 갯수만큼 boneweight
m.bindposes = new Matrix4x4[]
{
//pivot으로 부터 얼마나 떨어져있는지
bones[0].worldToLocalMatrix*transform.localToWorldMatrix,
bones[1].worldToLocalMatrix*transform.localToWorldMatrix,
bones[2].worldToLocalMatrix*transform.localToWorldMatrix,
bones[3].worldToLocalMatrix*transform.localToWorldMatrix,
bones[4].worldToLocalMatrix*transform.localToWorldMatrix,
bones[5].worldToLocalMatrix*transform.localToWorldMatrix,
bones[6].worldToLocalMatrix*transform.localToWorldMatrix,
bones[7].worldToLocalMatrix*transform.localToWorldMatrix,
bones[8].worldToLocalMatrix*transform.localToWorldMatrix,
bones[9].worldToLocalMatrix*transform.localToWorldMatrix,
bones[10].worldToLocalMatrix*transform.localToWorldMatrix
};
m.boneWeights = new BoneWeight[]
{
//weight는 최대합이 1
//PELVIS
new BoneWeight() { boneIndex0 = 0, weight0=1 },
new BoneWeight() { boneIndex0 = 0, weight0=1 },
new BoneWeight() { boneIndex0 = 0, weight0=1 },
//CHEST
new BoneWeight() { boneIndex0 = 1, weight0=1 },
new BoneWeight() { boneIndex0 = 1, weight0=1 },
new BoneWeight() { boneIndex0 = 1, weight0=1 },
//NECK
new BoneWeight() { boneIndex0 = 2, weight0=1 },
new BoneWeight() { boneIndex0 = 2, weight0=1 },
new BoneWeight() { boneIndex0 = 2, weight0=1 },
//SHOULDER_LEFT
new BoneWeight() { boneIndex0 = 3, weight0=1 },
new BoneWeight() { boneIndex0 = 3, weight0=1 },
new BoneWeight() { boneIndex0 = 3, weight0=1 },
//ARM_LEFT
new BoneWeight() { boneIndex0 = 4, weight0=1 },
new BoneWeight() { boneIndex0 = 4, weight0=1 },
new BoneWeight() { boneIndex0 = 4, weight0=1 },
//SHOULDER_RIGHT
new BoneWeight() { boneIndex0 = 5, weight0=1 },
new BoneWeight() { boneIndex0 = 5, weight0=1 },
new BoneWeight() { boneIndex0 = 5, weight0=1 },
//ARM_RIGHT
new BoneWeight() { boneIndex0 = 6, weight0=1 },
new BoneWeight() { boneIndex0 = 6, weight0=1 },
new BoneWeight() { boneIndex0 = 6, weight0=1 },
//LEG_RIGHT
new BoneWeight() { boneIndex0 = 7, weight0=1 },
new BoneWeight() { boneIndex0 = 7, weight0=1 },
new BoneWeight() { boneIndex0 = 7, weight0=1 },
//KNEE_RIGHT
new BoneWeight() { boneIndex0 = 8, weight0=1 },
new BoneWeight() { boneIndex0 = 8, weight0=1 },
new BoneWeight() { boneIndex0 = 8, weight0=1 },
//LEG_LEFT
new BoneWeight() { boneIndex0 = 9, weight0=1 },
new BoneWeight() { boneIndex0 = 9, weight0=1 },
new BoneWeight() { boneIndex0 = 9, weight0=1 },
//KNEE_LEFT
new BoneWeight() { boneIndex0 = 10, weight0=1 },
new BoneWeight() { boneIndex0 = 10, weight0=1 },
new BoneWeight() { boneIndex0 = 10, weight0=1 }
};
smr = GetComponent<SkinnedMeshRenderer>();
smr.bones = bones;
smr.sharedMesh = m;
smr.quality = SkinQuality.Bone1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment