Skip to content

Instantly share code, notes, and snippets.

@onotchi
Created May 11, 2017 13:56
Show Gist options
  • Save onotchi/58411b57e0c72f3df17a68a39d0ae250 to your computer and use it in GitHub Desktop.
Save onotchi/58411b57e0c72f3df17a68a39d0ae250 to your computer and use it in GitHub Desktop.
高崎柚乃ちゃんの表情を変化させる
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Onoty3D
{
public class BlendShapeValueChangerYuno : MonoBehaviour
{
/// <summary>
/// 表示するShapeの種類
/// </summary>
private enum DisplayShapeKind
{
笑い,
笑い2,
はにかみ,
困惑,
怒り,
目閉じ,
凝視,
口下,
口上,
口右,
口左,
あ,
い,
う,
え,
お,
ウインク右,
ウインク左,
}
/// <summary>
/// DisplayShapeのWeight
/// </summary>
private class DisplaySliderValue
{
public float Value { get; set; }
}
/// <summary>
/// 各BlendShapeがどのDisplayShapeKindを対象にするか設定するクラス
/// </summary>
private class BlendShapeDetail
{
public SkinnedMeshRenderer Mesh { get; set; }
public int Index { get; set; }
public DisplaySliderValue[] TargetSliderValues { get; set; }
public BlendShapeDetail(SkinnedMeshRenderer mesh, int index, DisplaySliderValue[] targetSliderValues)
{
this.Mesh = mesh;
this.Index = index;
this.TargetSliderValues = targetSliderValues;
}
}
public SkinnedMeshRenderer Face;
public SkinnedMeshRenderer EyeBrow;
public SkinnedMeshRenderer EyeLashes;
public SkinnedMeshRenderer Tooth;
public SkinnedMeshRenderer Tongue;
//各BlendShape
private BlendShapeDetail[] _shapeDetails;
//各スライダーの現在値を保持する辞書
private Dictionary<DisplayShapeKind, DisplaySliderValue> _sliderValues;
//BlendShape名の表示スタイル
private GUIStyle _labelTitleStyle;
//スライダー値の表示スタイル
private GUIStyle _labelValueStyle;
private void Start()
{
//SkinnedMeshRendererの取得
if (this.Face == null) this.Face = this.GetComponentsInChildren<SkinnedMeshRenderer>().First(x => x.name == "face");
if (this.EyeBrow == null) this.EyeBrow = this.GetComponentsInChildren<SkinnedMeshRenderer>().First(x => x.name == "eyeBrow");
if (this.EyeLashes == null) this.EyeLashes = this.GetComponentsInChildren<SkinnedMeshRenderer>().First(x => x.name == "eyeLashes");
if (this.Tooth == null) this.Tooth = this.GetComponentsInChildren<SkinnedMeshRenderer>().First(x => x.name == "Tooth1");
if (this.Tongue == null) this.Tongue = this.GetComponentsInChildren<SkinnedMeshRenderer>().First(x => x.name == "Tongue");
//各スライダーの現在値を保持する辞書の生成
this._sliderValues = Enum.GetValues(typeof(DisplayShapeKind)).Cast<DisplayShapeKind>().ToDictionary(x => x, _ => new DisplaySliderValue());
//BlendShape毎、どのスライダーの値を反映するか設定
this._shapeDetails = new BlendShapeDetail[]
{
new BlendShapeDetail(this.Face, 0, new [] { this._sliderValues[DisplayShapeKind.笑い],}),
new BlendShapeDetail(this.Face, 1, new [] { this._sliderValues[DisplayShapeKind.はにかみ],}),
new BlendShapeDetail(this.Face, 2, new [] { this._sliderValues[DisplayShapeKind.困惑],}),
new BlendShapeDetail(this.Face, 3, new [] { this._sliderValues[DisplayShapeKind.怒り],}),
new BlendShapeDetail(this.Face, 4, new [] { this._sliderValues[DisplayShapeKind.目閉じ],}),
new BlendShapeDetail(this.Face, 5, new [] { this._sliderValues[DisplayShapeKind.凝視],}),
new BlendShapeDetail(this.Face, 6, new [] { this._sliderValues[DisplayShapeKind.笑い2],}),
new BlendShapeDetail(this.Face, 7, new [] { this._sliderValues[DisplayShapeKind.口下],}),
new BlendShapeDetail(this.Face, 8, new [] { this._sliderValues[DisplayShapeKind.口上],}),
new BlendShapeDetail(this.Face, 9, new [] { this._sliderValues[DisplayShapeKind.口右],}),
new BlendShapeDetail(this.Face, 10, new [] { this._sliderValues[DisplayShapeKind.口左],}),
new BlendShapeDetail(this.Face, 11, new [] { this._sliderValues[DisplayShapeKind.お],}),
new BlendShapeDetail(this.Face, 12, new [] { this._sliderValues[DisplayShapeKind.え],}),
new BlendShapeDetail(this.Face, 13, new [] { this._sliderValues[DisplayShapeKind.う],}),
new BlendShapeDetail(this.Face, 14, new [] { this._sliderValues[DisplayShapeKind.い],}),
new BlendShapeDetail(this.Face, 15, new [] { this._sliderValues[DisplayShapeKind.あ],}),
new BlendShapeDetail(this.Face, 16, new [] { this._sliderValues[DisplayShapeKind.ウインク右],}),
new BlendShapeDetail(this.Face, 17, new [] { this._sliderValues[DisplayShapeKind.ウインク左],}),
new BlendShapeDetail(this.EyeBrow, 0, new [] { this._sliderValues[DisplayShapeKind.笑い], this._sliderValues[DisplayShapeKind.笑い2],}),
new BlendShapeDetail(this.EyeBrow, 1, new [] { this._sliderValues[DisplayShapeKind.はにかみ],}),
new BlendShapeDetail(this.EyeBrow, 2, new [] { this._sliderValues[DisplayShapeKind.困惑],}),
new BlendShapeDetail(this.EyeBrow, 3, new [] { this._sliderValues[DisplayShapeKind.怒り],}),
new BlendShapeDetail(this.EyeBrow, 4, new [] { this._sliderValues[DisplayShapeKind.凝視],}),
new BlendShapeDetail(this.EyeLashes, 0, new [] { this._sliderValues[DisplayShapeKind.笑い],}),
new BlendShapeDetail(this.EyeLashes, 1, new [] { this._sliderValues[DisplayShapeKind.はにかみ],}),
new BlendShapeDetail(this.EyeLashes, 2, new [] { this._sliderValues[DisplayShapeKind.目閉じ],}),
new BlendShapeDetail(this.EyeLashes, 3, new [] { this._sliderValues[DisplayShapeKind.凝視],}),
new BlendShapeDetail(this.EyeLashes, 4, new [] { this._sliderValues[DisplayShapeKind.笑い2],}),
new BlendShapeDetail(this.EyeLashes, 5, new [] { this._sliderValues[DisplayShapeKind.ウインク右],}),
new BlendShapeDetail(this.EyeLashes, 6, new [] { this._sliderValues[DisplayShapeKind.ウインク左],}),
new BlendShapeDetail(this.Tooth, 0, new [] { this._sliderValues[DisplayShapeKind.笑い], this._sliderValues[DisplayShapeKind.あ], this._sliderValues[DisplayShapeKind.う], this._sliderValues[DisplayShapeKind.え], this._sliderValues[DisplayShapeKind.お],}),
new BlendShapeDetail(this.Tongue, 0, new [] { this._sliderValues[DisplayShapeKind.笑い], this._sliderValues[DisplayShapeKind.あ], this._sliderValues[DisplayShapeKind.う], this._sliderValues[DisplayShapeKind.え], this._sliderValues[DisplayShapeKind.お],}),
};
this._labelTitleStyle = new GUIStyle { fixedWidth = 80f, alignment = TextAnchor.MiddleRight, normal = new GUIStyleState { textColor = Color.white } };
this._labelValueStyle = new GUIStyle { fixedWidth = 20f, alignment = TextAnchor.MiddleRight, normal = new GUIStyleState { textColor = Color.white } };
}
private void OnGUI()
{
GUILayout.Box("", GUILayout.Width(220), GUILayout.Height(15 * (this._sliderValues.Count + 1)));
var screenRect = new Rect(10, 10, 190, 15 * (this._sliderValues.Count + 1));
GUILayout.BeginArea(screenRect);
foreach (var sliderValue in this._sliderValues)
{
GUILayout.BeginHorizontal();
GUILayout.Label(sliderValue.Key.ToString(), _labelTitleStyle);
//スライダー
sliderValue.Value.Value = GUILayout.HorizontalSlider(sliderValue.Value.Value, 0f, 100f);
//スライダーの値
GUILayout.Label(sliderValue.Value.Value.ToString("#0"), this._labelValueStyle);
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
foreach (var shapeDetail in this._shapeDetails)
{
//スライダーの値をWeightに反映。複数のスライダーが対象の場合は一番大きな値。
shapeDetail.Mesh.SetBlendShapeWeight(shapeDetail.Index, shapeDetail.TargetSliderValues.Max(x => x.Value));
}
}
private void Update()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment