筆記:
改寫 來源此
想要上好骨架的腳色特定部位改色,或者是被攻擊到部位變色,可以使用此方法變色
我上骨架的是用
Mixamo
線上幫忙上骨架,然後在依骨架牽動的vetex作為上色區塊,大約如此。
前提,model materail渲染方式要改成 vetex Color //之後附上渲染模組
之後在上註解,先記錄起來。
using System.Collections;
using UnityEngine;
namespace AssemblyCSharp {
public class NewBoneHighter : MonoBehaviour {
public Color32 highlightColor = Color.red;
public Color32 regularColor = Color.white;
public SkinnedMeshRenderer smr;
// Just for sake of demonstration
public Transform[] boneList; //放進對應的joint 用 GetBoneIndex () 可找到骨架編號
public int[] boneIndex = new int[15];
//joint變的色, 存bone的編號
public static int[, ] jointcolor //0白 1紅 2綠 3黃
= { { 1, 19 }, { 0, 15 }, { 0, 14 }, { 1, 0 }, { 0, 1 }, { 0, 2 }, { 0, 18 }, { 0, 16 }, { 0, 20 }, { 0, 8 }, { 0, 7 }, { 0, 21 }, { 0, 10 }, { 0, 12 } };
//19 15 14 0 1 2 17 16 20 8 7 21 10 12 這些數字是先跑一次,在unity編輯器用boneIndex找到的
// Find bone index given bone transform
void GetBoneIndex () {
var bones = smr.bones;
for (int j = 0; j < boneList.Length; j++) {
for (int i = 0; i < bones.Length; ++i) {
if (bones[i] == boneList[j]) {
boneIndex[j] = i;
}
}
}
}
private void ChangeMesh () {
var mesh = smr.sharedMesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
for(var i = 0; i < vertices.Length ; i++){
vertices[i] *= 3;
}
mesh.vertices = vertices;
}
// Change vertex colors highlighting given bone
public void Highlight () {
var mesh = smr.sharedMesh;
var weights = mesh.boneWeights;
var colors = new Color32[weights.Length];
Vector3[] vertices = mesh.vertices;
for (int i = 0; i < colors.Length; ++i) {
colors[i] = Color.white; //預設全圖白
for (int j = 0; j < 14; j++) { //要變色joint再去檢查
if (jointcolor[j, 0] == 1) {
if (weights[i].boneIndex0 == jointcolor[j, 1] && weights[i].weight0 > 0)
colors[i] = Color.red;
}
if (jointcolor[j, 0] == 2) {
if (weights[i].boneIndex0 == jointcolor[j, 1] && weights[i].weight0 > 0)
colors[i] = Color.green;
}
if (jointcolor[j, 0] == 3) {
if (weights[i].boneIndex0 == jointcolor[j, 1] && weights[i].weight0 > 0)
colors[i] = Color.yellow;
}
}
}
mesh.colors32 = colors;
mesh.vertices = vertices;
}
void Start () {
// If not explicitly specified SkinnedMeshRenderer try to find one
if (smr == null) smr = GetComponent<SkinnedMeshRenderer> ();
// SkinnedMeshRenderer has only shared mesh. We should not modify it.
// So we make a copy on startup, and work with it.
smr.sharedMesh = (Mesh) Instantiate (smr.sharedMesh);
// ChangeMesh ();
Highlight ();
GetBoneIndex ();
}
void Update () {
}
}
}
@copyright MRcodingRoom
觀看更多文章請點MRcoding筆記
觀看更多文章請點MRcoding筆記