Created
December 19, 2016 10:17
-
-
Save Indp-Dustin/a4de8c0cacb8ab860912072b41559e55 to your computer and use it in GitHub Desktop.
Unity Code Rigging
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 UnityEngine; | |
using System.Collections; | |
public class CustomSkinnedMesh : MonoBehaviour { | |
public Transform[] bones; | |
public Material mat; | |
public AnimationClip clip; | |
void Awake() | |
{ | |
clip.wrapMode = WrapMode.PingPong; | |
} | |
void Start () | |
{ | |
Mesh m = new Mesh(); | |
m.vertices = new Vector3[] | |
{ | |
new Vector3(-0.5f, -0.5f, 0f), | |
new Vector3(-0.5f, 0.5f, 0f), | |
new Vector3(0.5f, 0.5f, 0f), | |
new Vector3(0.5f, -0.5f, 0f) | |
}; | |
m.triangles = new int[] { 0, 1, 2, 0, 2, 3 }; | |
m.uv = new Vector2[] | |
{ | |
new Vector2(0f, 0f), | |
new Vector2(0f, 1f), | |
new Vector2(1f, 1f), | |
new Vector2(1f, 0f), | |
}; | |
m.boneWeights = new BoneWeight[] | |
{ | |
new BoneWeight() { boneIndex0 = 0, weight0 = 1f}, | |
new BoneWeight() { boneIndex0 = 0, weight0 = 1f}, | |
new BoneWeight() { boneIndex0 = 1, weight0 = 1f}, | |
new BoneWeight() { boneIndex0 = 1, weight0 = 1f} | |
}; | |
m.bindposes = new Matrix4x4[] | |
{ | |
bones[0].worldToLocalMatrix * transform.localToWorldMatrix, | |
bones[1].worldToLocalMatrix * transform.localToWorldMatrix | |
}; | |
m.RecalculateBounds(); | |
m.RecalculateNormals(); | |
SkinnedMeshRenderer smr = GetComponent<SkinnedMeshRenderer>(); | |
smr.sharedMesh = m; | |
smr.bones = bones; | |
smr.rootBone = transform; | |
smr.material = mat; | |
smr.quality = SkinQuality.Bone1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment