59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
public class BlendShapeExample : MonoBehaviour
|
|
{
|
|
private int blendShapeCount;
|
|
private SkinnedMeshRenderer skinnedMeshRenderer;
|
|
private Mesh skinnedMesh;
|
|
private Dictionary<int, string> shapekeyIndex = new Dictionary<int, string>();
|
|
private Dictionary<string, int> shapekeyIndexR = new Dictionary<string, int>();
|
|
private Dictionary<int, List<int>> correctiveIndex = new Dictionary<int, List<int>>();
|
|
|
|
void Awake()
|
|
{
|
|
this.skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
|
|
this.skinnedMesh = GetComponent<SkinnedMeshRenderer>().sharedMesh;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
this.blendShapeCount = skinnedMesh.blendShapeCount;
|
|
for (int i = 0; i < this.blendShapeCount; i++)
|
|
{
|
|
string s = this.skinnedMesh.GetBlendShapeName(i);
|
|
this.shapekeyIndex.Add(i, s);
|
|
this.shapekeyIndexR.Add(s, i);
|
|
}
|
|
for (int i = 0; i < this.blendShapeCount; i++)
|
|
{
|
|
string s = this.skinnedMesh.GetBlendShapeName(i);
|
|
if (s.Contains('_'))
|
|
{
|
|
var ctrls = new List<int>();
|
|
foreach (var sub in s.Split('_', StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
ctrls.Add(shapekeyIndexR[sub]);
|
|
}
|
|
this.correctiveIndex.Add(i, ctrls);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
foreach (var item in this.correctiveIndex)
|
|
{
|
|
float weight = 1.0f;
|
|
foreach (var index in item.Value)
|
|
{
|
|
weight *= this.skinnedMeshRenderer.GetBlendShapeWeight(index) * 0.01f;
|
|
}
|
|
this.skinnedMeshRenderer.SetBlendShapeWeight(item.Key, weight * 100);
|
|
}
|
|
}
|
|
}
|