BlendShapeClip.cs
6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using UnityEngine;
namespace VRM
{
[Serializable]
public struct BlendShapeBinding : IEquatable<BlendShapeBinding>
{
public String RelativePath;
public int Index;
public float Weight;
public override string ToString()
{
return string.Format("{0}[{1}]=>{2}", RelativePath, Index, Weight);
}
public bool Equals(BlendShapeBinding other)
{
return string.Equals(RelativePath, other.RelativePath) && Index == other.Index && Weight.Equals(other.Weight);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is BlendShapeBinding && Equals((BlendShapeBinding)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (RelativePath != null ? RelativePath.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Index;
hashCode = (hashCode * 397) ^ Weight.GetHashCode();
return hashCode;
}
}
}
[Serializable]
public struct MaterialValueBinding : IEquatable<MaterialValueBinding>
{
public String MaterialName;
public String ValueName;
public Vector4 TargetValue;
public Vector4 BaseValue;
public bool Equals(MaterialValueBinding other)
{
return string.Equals(MaterialName, other.MaterialName) && string.Equals(ValueName, other.ValueName) && TargetValue.Equals(other.TargetValue) && BaseValue.Equals(other.BaseValue);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is MaterialValueBinding && Equals((MaterialValueBinding)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (MaterialName != null ? MaterialName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (ValueName != null ? ValueName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ TargetValue.GetHashCode();
hashCode = (hashCode * 397) ^ BaseValue.GetHashCode();
return hashCode;
}
}
}
[CreateAssetMenu(menuName = "VRM/BlendShapeClip")]
public class BlendShapeClip : ScriptableObject
{
#if UNITY_EDITOR
/// <summary>
/// Preview 用のObject参照
/// </summary>
[SerializeField]
GameObject m_prefab;
public GameObject Prefab
{
set { m_prefab = value; }
get
{
if (m_prefab == null)
{
var assetPath = UnityEditor.AssetDatabase.GetAssetPath(this);
if (!string.IsNullOrEmpty(assetPath))
{
// if asset is subasset of prefab
m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
if (m_prefab == null)
{
var parent = UniGLTF.UnityPath.FromAsset(this).Parent;
var prefabPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".prefab");
m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath.Value);
}
}
}
return m_prefab;
}
}
/// <summary>
/// Apply BlendShape for Preview
/// </summary>
/// <param name="root"></param>
/// <param name="value"></param>
public void Apply(Transform root, float value)
{
if (Values != null)
{
foreach (var x in Values)
{
var target = root.Find(x.RelativePath);
if (target != null)
{
var sr = target.GetComponent<SkinnedMeshRenderer>();
if (sr != null)
{
sr.SetBlendShapeWeight(x.Index, x.Weight * value);
}
}
}
}
/*
if (MaterialValues != null)
{
foreach (var x in MaterialValues)
{
var target = root.Find(x.RelativePath);
if (target != null)
{
var sr = target.GetComponent<SkinnedMeshRenderer>();
if (sr != null)
{
var m = sr.sharedMaterials[x.Index];
var color = x.BaseValue + (x.TargetValue - x.BaseValue) * value;
m.SetColor(x.ValueName, color);
}
}
}
}
*/
}
#endif
/// <summary>
/// BlendShapePresetがUnknown場合の識別子
/// </summary>
[SerializeField]
public string BlendShapeName = "";
/// <summary>
/// BlendShapePresetを識別する。Unknownの場合は、BlendShapeNameで識別する
/// </summary>
[SerializeField]
public BlendShapePreset Preset;
/// <summary>
/// BlendShapeに対する参照(index ベース)
/// </summary>
/// <value></value>
[SerializeField]
public BlendShapeBinding[] Values = new BlendShapeBinding[] { };
/// <summary>
/// マテリアルに対する参照(名前ベース)
/// </summary>
/// <value></value>
[SerializeField]
public MaterialValueBinding[] MaterialValues = new MaterialValueBinding[] { };
/// <summary>
/// UniVRM-0.45: trueの場合、このBlendShapeClipは0と1の間の中間値を取らない。四捨五入する
/// </summary>
[SerializeField]
public bool IsBinary;
// [SerializeField]
// public Texture2D Thumbnail;
}
}