-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathActorIKTarget.cs
304 lines (264 loc) · 7.97 KB
/
ActorIKTarget.cs
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.IO;
using Nima.Math2D;
namespace Nima
{
public class ActorIKTarget : ActorNode
{
private InfluencedBone[] m_InfluencedBones;
private bool m_InvertDirection;
public float m_Strength;
private ActorIKConstraint m_Constraint;
public InfluencedBone[] InfluencedBones
{
get
{
return m_InfluencedBones;
}
}
public bool InvertDirection
{
get
{
return m_InvertDirection;
}
}
public static ActorIKTarget Read(Actor actor, BinaryReader reader, ActorIKTarget node = null)
{
if(node == null)
{
node = new ActorIKTarget();
}
ActorNode.Read(actor, reader, node);
// discard old value
int order = reader.ReadUInt16();
node.m_Strength = reader.ReadSingle();
node.m_InvertDirection = reader.ReadByte() == 1;
int numInfluencedBones = (int)reader.ReadByte();
if(numInfluencedBones > 0)
{
node.m_InfluencedBones = new InfluencedBone[numInfluencedBones];
for(int i = 0; i < numInfluencedBones; i++)
{
InfluencedBone ib = new InfluencedBone();
ib.m_BoneIdx = reader.ReadUInt16();
node.m_InfluencedBones[i] = ib;
}
}
return node;
}
public override void ResolveComponentIndices(ActorComponent[] components)
{
base.ResolveComponentIndices(components);
m_Constraint = new ActorIKConstraint(this);
m_Constraint.ResolveComponentIndices(components);
// if(m_InfluencedBones != null)
// {
// for(int i = 0; i < m_InfluencedBones.Length; i++)
// {
// InfluencedBone ib = m_InfluencedBones[i];
// ib.m_Bone = components[ib.m_BoneIdx] as ActorBone;
// if(ib.m_Bone != null)
// {
// ib.m_Bone.AddDependent(this);
// }
// }
// if(m_InfluencedBones.Length > 0)
// {
// m_Bone1 = m_InfluencedBones[0].m_Bone;
// m_Bone2 = m_InfluencedBones[m_InfluencedBones.Length-1].m_Bone;
// ActorBone b1c = m_Bone2;
// ActorBone b1 = m_Bone1;
// if(m_InfluencedBones.Length > 1)
// {
// while(b1c != null && b1c.Parent != b1)
// {
// b1c = b1c.Parent as ActorBone;
// }
// }
// m_Bone1Child = b1c;
// int chainCount = 0;
// ActorBone end = m_Bone2;
// while(end != null && end != b1.Parent)
// {
// chainCount++;
// end = end.Parent as ActorBone;
// }
// m_Chain = new BoneChain[chainCount];
// end = m_Bone2;
// int chainIdx = 0;
// while(end != null && end != b1.Parent)
// {
// BoneChain bc = new BoneChain();
// bc.m_Bone = end;
// bc.m_Angle = 0.0f;
// bc.m_Included = DoesInfluence(end) || DoesInfluence(end.Parent as ActorBone);
// m_Chain[chainIdx++] = bc;
// end = end.Parent as ActorBone;
// }
// }
// }
}
public override void CompleteResolve()
{
base.CompleteResolve();
m_Constraint.CompleteResolve();
}
public override ActorComponent MakeInstance(Actor resetActor)
{
ActorIKTarget instanceNode = new ActorIKTarget();
instanceNode.Copy(this, resetActor);
return instanceNode;
}
public void Copy(ActorIKTarget node, Actor resetActor)
{
base.Copy(node, resetActor);
m_InvertDirection = node.m_InvertDirection;
m_Strength = node.m_Strength;
m_InfluencedBones = new InfluencedBone[node.m_InfluencedBones.Length];
for(int i = 0; i < m_InfluencedBones.Length; i++)
{
InfluencedBone ib = new InfluencedBone();
ib.m_BoneIdx = node.m_InfluencedBones[i].m_BoneIdx;
m_InfluencedBones[i] = ib;
}
}
public float Strength
{
get
{
return m_Constraint.Strength;
}
set
{
m_Constraint.Strength = value;
}
}
// public void SolveStart()
// {
// if(m_Bone1 == null || m_Bone1Child == null)
// {
// return;
// }
// // Reset all rotation overrides to FK ones,
// if(m_Bone1Child != m_Bone2)
// {
// m_Bone1Child.SetRotationOverride(m_Bone1Child.Rotation);
// }
// foreach(InfluencedBone b in m_InfluencedBones)
// {
// b.m_Bone.SetRotationOverride(b.m_Bone.Rotation);
// }
// }
// private void Solve2(ActorBone b1, ActorBone b2, Vec2D worldTargetTranslation, bool invert)
// {
// Mat2D world = b1.Parent.WorldTransform;
// ActorBone b1c = b2;
// while(b1c != null && b1c.Parent != b1)
// {
// b1c = b1c.Parent as ActorBone;
// }
// // Transform to root bone space
// ActorBone b1p = b1.Parent as ActorBone;
// // If it's an ActorRootBone, it has no length.
// if(b1p != null && b1p.Length > 0.0f)
// {
// Mat2D t = new Mat2D();
// t[4] = b1p.Length;
// Mat2D.Multiply(t, world, t);
// world = t;
// }
// Mat2D iworld = new Mat2D();
// Mat2D.Invert(iworld, world);
// Vec2D pA = b1.GetWorldTranslation(new Vec2D());
// Vec2D pC = b1.GetTipWorldTranslation(new Vec2D());
// Vec2D pB = b2.GetTipWorldTranslation(new Vec2D());
// Vec2D pBT = new Vec2D(worldTargetTranslation);
// Vec2D.TransformMat2D(pA, pA, iworld);
// Vec2D.TransformMat2D(pC, pC, iworld);
// Vec2D.TransformMat2D(pB, pB, iworld);
// Vec2D.TransformMat2D(pBT, pBT, iworld);
// // http://mathworld.wolfram.com/LawofCosines.html
// Vec2D av = Vec2D.Subtract(new Vec2D(), pB, pC);
// float a = Vec2D.Length(av);
// Vec2D bv = Vec2D.Subtract(new Vec2D(), pC, pA);
// float b = Vec2D.Length(bv);
// Vec2D cv = Vec2D.Subtract(new Vec2D(), pBT, pA);
// float c = Vec2D.Length(cv);
// float A = (float)Math.Acos((double)Math.Max(-1.0f,Math.Min(1.0f,(-a*a+b*b+c*c)/(2.0f*b*c))));
// float C = (float)Math.Acos((double)Math.Max(-1.0f, Math.Min(1.0f,(a*a+b*b-c*c)/(2.0f*a*b))));
// float angleCorrection = 0;
// if(b1c != b2)
// {
// Mat2D iworld2 = new Mat2D();
// Mat2D.Invert(iworld2, b1c.WorldTransform);
// Vec2D pa2 = b2.GetTipWorldTranslation(new Vec2D());
// Vec2D tipBone2Local = Vec2D.TransformMat2D(pa2, pa2, iworld2);
// angleCorrection = -(float)Math.Atan2((double)tipBone2Local[1], (double)tipBone2Local[0]);
// }
// if(invert)
// {
// b1.SetRotationOverride((float)Math.Atan2((double)pBT[1],(double)pBT[0]) - A);
// b1c.SetRotationOverride(-C+(float)Math.PI+angleCorrection);
// }
// else
// {
// b1.SetRotationOverride(A+(float)Math.Atan2((double)pBT[1],(double)pBT[0]));
// b1c.SetRotationOverride(C-(float)Math.PI+angleCorrection);
// }
// }
// private void Solve1(ActorBone b1, Vec2D worldTargetTranslation)
// {
// Mat2D iworld2 = new Mat2D();
// Mat2D.Invert(iworld2, b1.WorldTransform);
// Vec2D targetLocal = Vec2D.TransformMat2D(new Vec2D(), worldTargetTranslation, iworld2);
// float a = (float)Math.Atan2((double)targetLocal[1], (double)targetLocal[0]);
// b1.SetRotationOverride(b1.OverrideRotationValue+a);
// }
// public void Solve()
// {
// if(m_Chain == null)
// {
// return;
// }
// Vec2D worldTargetTranslation = new Vec2D();
// Mat2D wt = WorldTransform;
// worldTargetTranslation[0] = wt[4];
// worldTargetTranslation[1] = wt[5];
// for(int i = 0; i < m_Chain.Length; i++)
// {
// BoneChain fk = m_Chain[i];
// fk.m_Angle = fk.m_Bone.OverrideRotationValue;
// }
// if(m_InfluencedBones.Length == 1)
// {
// Solve1(m_InfluencedBones[0].m_Bone, worldTargetTranslation);
// }
// else if(m_InfluencedBones.Length == 2)
// {
// Solve2(m_InfluencedBones[0].m_Bone, m_InfluencedBones[1].m_Bone, worldTargetTranslation, m_InvertDirection);
// }
// else
// {
// for(int i = 0; i < m_InfluencedBones.Length-1; i++)
// {
// Solve2(m_InfluencedBones[i].m_Bone, m_Bone2, worldTargetTranslation, m_InvertDirection);
// }
// }
// // At the end, mix the FK angle with the IK angle by strength
// float m = m_Strength;
// if(m != 1.0f)
// {
// float im = 1.0f-m_Strength;
// for(int i = 0; i < m_Chain.Length; i++)
// {
// BoneChain fk = m_Chain[i];
// if(fk.m_Included)
// {
// fk.m_Bone.SetRotationOverride(fk.m_Bone.OverrideRotationValue * m + fk.m_Angle * im);
// }
// }
// }
// }
}
}