/
IKCONSTRAINED.cs
428 lines (349 loc) · 16.6 KB
/
IKCONSTRAINED.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityInjector.Attributes;
namespace CM3D2.AddBoneSlider.Plugin
{
//制限角度つきIK
public class IKCONSTRAINED
{
private TBody body;
private float defLENroroot;
private float defLENroot;
private float defLENhead;
private Vector3 mid_old;
private Quaternion defRorootlocalRotation;
private Quaternion defRootlocalRotation;
private Quaternion defMidlocalRotation;
private Vector3 vechand;
//制限角度多段配列
//それぞれの添え字は
//[ボーンの先端からの順番,xの下限値→zの上限値]
private float[,] constrait;
public void Init(Transform initRoot, Transform initMid, Transform initHead, TBody b, float[,] initConstrait, Transform initRoroot = null)
{
this.body = b;
this.defLENroot = (initRoot.position - initMid.position).magnitude;
this.defLENhead = (initHead.position - initMid.position).magnitude;
this.mid_old = initMid.position;
this.defRootlocalRotation = initRoot.localRotation;
this.defMidlocalRotation = initMid.localRotation;
this.vechand = Vector3.zero;
if (initRoroot != null)
{
this.defLENroroot = (initRoroot.position - initRoot.position).magnitude;
this.defRorootlocalRotation = initRoroot.localRotation;
}
this.constrait = initConstrait;
}
public void Init(Transform[] boneList, TBody b, float[,] initConstrait, Transform initRoroot = null)
{
this.body = b;
this.defLENroot = (boneList[0].position - boneList[1].position).magnitude;
this.defLENhead = (boneList[2].position - boneList[1].position).magnitude;
this.mid_old = boneList[1].position;
this.defRootlocalRotation = boneList[0].localRotation;
this.defMidlocalRotation = boneList[1].localRotation;
this.vechand = Vector3.zero;
if (initRoroot != null)
{
this.defLENroroot = (initRoroot.position - boneList[0].position).magnitude;
this.defRorootlocalRotation = initRoroot.localRotation;
}
this.constrait = initConstrait;
}
//腕・脚に特化させた肘・膝可動範囲制限付きIK
//本体側と関数名をあわせないためにあえてProcにする
public void Proc(Transform root, Transform mid, Transform head, Vector3 tgt)
{
//tgt += this.vechand;
//先ボーンの曲げる前の回転状態を保持しておく
Quaternion oldHeadRotation = head.transform.rotation;
float LENtgt = (tgt - root.position).magnitude;
//中間のボーンのZ軸角度(肘or膝)を一意に決める
float midAngle;
//ボーンの合計長さ < 根元ボーンからターゲットまでの距離 の場合
if (LENtgt >= defLENroot + defLENhead)
{
midAngle = 0f;//180f;
}
else
{
float s = (float)Math.Sqrt((-LENtgt + defLENroot + defLENhead) * (LENtgt - defLENroot + defLENhead) * (LENtgt + defLENroot - defLENhead) * (LENtgt + defLENroot + defLENhead)) / 4f;
//各ボーンの長さ < 根元ボーンからターゲットまでの距離 の場合
if (LENtgt > ((defLENroot > defLENhead) ? defLENroot : defLENhead))
{
float h = 2 * s / (LENtgt);
//midAngle = 180f - Mathf.Asin(h / defLENhead) * Mathf.Rad2Deg - Mathf.Asin(h / defLENroot) * Mathf.Rad2Deg;
midAngle = Mathf.Asin(h / defLENhead) * Mathf.Rad2Deg + Mathf.Asin(h / defLENroot) * Mathf.Rad2Deg;
}
else //ボーンのどちらか長い方 > 根元ボーンからターゲットまでの距離の場合
{
float h = 2 * s / ((defLENroot > defLENhead) ? defLENroot : defLENhead);
midAngle = 180f - Mathf.Asin(h / ((defLENroot > defLENhead) ? defLENhead : defLENroot)) * Mathf.Rad2Deg;
}
}
//角度制限チェック
//肘も膝もZ軸周りの角度でしか回らないものとして計算する
//他の関節では適用できないので注意
if (this.constrait[1, 4] > midAngle)
{
midAngle = this.constrait[1, 4];
}
if (this.constrait[1, 5] < midAngle)
{
midAngle = this.constrait[1, 5];
}
//中間のボーンのX・Y軸周りの現在の角度を求める
Vector3 pastEuler = calcEulerfromRotation(mid.localRotation);
//計算結果の角度がずれたときのことを考えて一応チェック
//今後の検証次第ではここのチェックをスキップするかも
if (kaiten_ichii(pastEuler, this.constrait[1, 0], this.constrait[1, 1], this.constrait[1, 2], this.constrait[1, 3], this.constrait[1, 4], this.constrait[1, 5]))
{
//pastEuler.z -= 180f;
pastEuler.y -= 180f;
pastEuler.x = 180f - pastEuler.x;
}
//ここで中間のボーンを曲げる
mid.localRotation = Quaternion.identity;
mid.localRotation *= Quaternion.AngleAxis(midAngle, Vector3.forward);
mid.localRotation *= Quaternion.AngleAxis(pastEuler.x, Vector3.right);
mid.localRotation *= Quaternion.AngleAxis(pastEuler.y, Vector3.up);
if (!this.body.boMAN)
{
Vector3 zero = Vector3.zero;
Vector3 mid_old = mid.position;
bool flag = this.body.goSlot[0].bonehair.bodyhit.SphereMove_hair(ref mid_old, ref zero, Vector3.zero);
//if (flag)
//{
// Debug.DrawLine(this.body.Spine0a.position, this.mid_old, Color.white);
//}
//mid.position = mid_old;
}
//中間のボーン(肘or膝)を曲げた状態で
//根元のボーン(肩or股)から先端のボーン(手or足)までのベクトルを
//根元のボーン(肩or股)からターゲットまでのベクトルに向ける回転を求めて
//根元のボーンをターゲットに向けるように回転させる
//root.localRotation = this.defRootlocalRotation;
root.transform.rotation = Quaternion.FromToRotation(head.transform.position - root.transform.position, tgt - root.transform.position) * root.transform.rotation;
//↑のタイミングで根元のボーンが回転する
//ボーンの角度範囲チェック&超えてたら範囲内に収める
root.localRotation = constrainIK(root, 2);
//最後に先ボーンを元の回転になるように曲げる
head.transform.rotation = oldHeadRotation;
//ボーンの角度範囲チェック&超えてたら範囲内に収める
head.localRotation = constrainIK(head, 0, head.name.Contains("Hand"));
//Debug.DrawLine(root.position, this.mid_old, Color.yellow);
//Debug.DrawLine(head.position, this.mid_old, Color.yellow);
//this.vechand = head.rotation * vechand_offset;
}
public Vector3 calcEulerfromRotation(Quaternion tr)
{
float qx = tr.x;
float qy = tr.y;
float qz = tr.z;
float qw = tr.w;
float m02 = 2 * (qx * qz - qw * qy);
float m10 = 2 * (qx * qy - qw * qz);
float m11 = 1 - 2 * (qx * qx + qz * qz);
float m12 = 2 * (qy * qz + qw * qx);
//float m20 = 2 * (qx * qz + qw * qy);
//float m21 = 2 * (qy * qz - qw * qx);
float m22 = 1 - 2 * (qx * qx + qy * qy);
if (m12 > 1.0f) m12 = 1.0f;
if (m12 < -1.0f) m12 = -1.0f;
if (m11 > 1.0f) m11 = 1.0f;
if (m11 < -1.0f) m11 = -1.0f;
if (m10 > 1.0f) m10 = 1.0f;
if (m10 < -1.0f) m10 = -1.0f;
if (m22 > 1.0f) m22 = 1.0f;
if (m22 < -1.0f) m22 = -1.0f;
float rotate_x = Mathf.Asin(m12) * Mathf.Rad2Deg;
float rotate_y = 0f;
float rotate_z = 0f;
if (System.Math.Floor(Mathf.Cos(rotate_x * Mathf.Deg2Rad) * 10000) / 10000 != 0f)
{
rotate_z = Mathf.Atan2(-m10, m11) * Mathf.Rad2Deg;
float before = -m02 / Mathf.Cos(rotate_x * Mathf.Deg2Rad);
if (before > 1.0f) before = 1.0f;
if (before < -1.0f) before = -1.0f;
rotate_y = Mathf.Asin(before) * Mathf.Rad2Deg;
if (m22 < 0)
{
rotate_y = 180 - rotate_y;
}
}
if (System.Math.Floor(Mathf.Cos(rotate_x * Mathf.Deg2Rad) * 10000) / 10000 == 0f || Double.IsNaN(rotate_y))
{
float m00 = 1 - 2 * (qy * qy + qz * qz);
float m01 = 2 * (qx * qy + qw * qz);
if (m01 > 1.0f) m01 = 1.0f;
if (m01 < -1.0f) m01 = -1.0f;
if (m00 > 1.0f) m00 = 1.0f;
if (m00 < -1.0f) m00 = -1.0f;
rotate_y = 0f;
//rotate_x = (tr.x > 0) ? 90f : -90f;
rotate_z = Mathf.Atan2(m01, m00) * Mathf.Rad2Deg;
}
return new Vector3(rotate_x, rotate_y, rotate_z);
}
public Vector3 calcEulerfromRotation_hand(Quaternion tr)
{
float qx = tr.x;
float qy = tr.y;
float qz = tr.z;
float qw = tr.w;
float m02 = 2 * (qx * qz - qw * qy);
if (m02 > 1.0f) m02 = 1.0f;
if (m02 < -1.0f) m02 = -1.0f;
float rotate_x = 0f;
float rotate_y = Mathf.Asin(-m02) * Mathf.Rad2Deg;
float rotate_z = 0f;
if (System.Math.Floor(Mathf.Cos(rotate_y * Mathf.Deg2Rad) * 10000) / 10000 != 0f)
{
float m00 = 1 - 2 * (qy * qy + qz * qz);
float m01 = 2 * (qx * qy + qw * qz);
float m12 = 2 * (qy * qz + qw * qx);
float m22 = 1 - 2 * (qx * qx + qy * qy);
if (m00 > 1.0f) m00 = 1.0f;
if (m00 < -1.0f) m00 = -1.0f;
if (m01 > 1.0f) m01 = 1.0f;
if (m01 < -1.0f) m01 = -1.0f;
if (m12 > 1.0f) m12 = 1.0f;
if (m12 < -1.0f) m12 = -1.0f;
if (m22 > 1.0f) m22 = 1.0f;
if (m22 < -1.0f) m22 = -1.0f;
rotate_z = Mathf.Atan2(m01, m00) * Mathf.Rad2Deg;
float before = m12 / Mathf.Cos(rotate_y * Mathf.Deg2Rad);
if (before > 1.0f) before = 1.0f;
if (before < -1.0f) before = -1.0f;
rotate_x = Mathf.Asin(before) * Mathf.Rad2Deg;
if (m22 < 0)
{
rotate_x = 180 - rotate_x;
}
}
if (System.Math.Floor(Mathf.Cos(rotate_y * Mathf.Deg2Rad) * 10000) / 10000 == 0f || Double.IsNaN(rotate_x))
{
float m10 = 2 * (qx * qy - qw * qz);
float m11 = 1 - 2 * (qx * qx + qz * qz);
if (m10 > 1.0f) m10 = 1.0f;
if (m10 < -1.0f) m10 = -1.0f;
if (m11 > 1.0f) m11 = 1.0f;
if (m11 < -1.0f) m11 = -1.0f;
rotate_x = 0f;
rotate_z = Mathf.Atan2(-m10, m11) * Mathf.Rad2Deg;
}
return new Vector3(rotate_x, rotate_y, rotate_z);
}
//角度が一意に決まらないときの苦肉の策
public bool kaiten_ichii(Vector3 rotate, float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)
{
return ((180f - rotate.x) > xmin && (180f - rotate.x) < xmax
&& (rotate.y - 180f) > ymin && (rotate.y - 180f) < ymax
&& (rotate.z - 180f) > zmin && (rotate.z - 180f) < zmax);
}
public bool kaiten_ichii_hand(Vector3 rotate, float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)
{
return ((rotate.x - 180f) > xmin && (rotate.x - 180f) < xmax
&& (180f - rotate.y) > ymin && (180f - rotate.y) < ymax
&& (rotate.z - 180f) > zmin && (rotate.z - 180f) < zmax);
}
public Quaternion constrainIK(Transform tr, int boneOrder, bool isHand = false)
{
return constrainIK(tr.localRotation, boneOrder, isHand);
}
public Quaternion constrainIK(Quaternion tr, int boneOrder, bool isHand = false)
{
Vector3 rotate;
if (!isHand)
{
rotate = calcEulerfromRotation(tr);
if (kaiten_ichii(rotate, this.constrait[boneOrder, 0], this.constrait[boneOrder, 1], this.constrait[boneOrder, 2], this.constrait[boneOrder, 3], this.constrait[boneOrder, 4], this.constrait[boneOrder, 5]))
{
//rotate.z -= 180f;
//rotate.y -= 180f;
//rotate.x = 180f - rotate.x ;
return tr;
}
}
else
{
rotate = calcEulerfromRotation_hand(tr);
if (kaiten_ichii_hand(rotate, this.constrait[boneOrder, 0], this.constrait[boneOrder, 1], this.constrait[boneOrder, 2], this.constrait[boneOrder, 3], this.constrait[boneOrder, 4], this.constrait[boneOrder, 5]))
{
return tr;
}
}
bool bOver = false;
if (this.constrait[boneOrder, 0] > rotate.x)
{
if (this.constrait[boneOrder, 0] > rotate.x + 360f || this.constrait[boneOrder, 1] < rotate.x + 360f)
{
rotate.x = this.constrait[boneOrder, 0];
bOver = true;
}
}
if (this.constrait[boneOrder, 1] < rotate.x)
{
if (this.constrait[boneOrder, 1] < rotate.x - 360f || this.constrait[boneOrder, 0] > rotate.x - 360f)
{
rotate.x = this.constrait[boneOrder, 1];
bOver = true;
}
}
if (this.constrait[boneOrder, 2] > rotate.y)
{
if (this.constrait[boneOrder, 2] > rotate.y + 360f || this.constrait[boneOrder, 3] < rotate.y + 360f)
{
rotate.y = this.constrait[boneOrder, 2];
bOver = true;
}
}
if (this.constrait[boneOrder, 3] < rotate.y)
{
if (this.constrait[boneOrder, 3] < rotate.y - 360f || this.constrait[boneOrder, 2] > rotate.y - 360f)
{
rotate.y = this.constrait[boneOrder, 3];
bOver = true;
}
}
if (this.constrait[boneOrder, 4] > rotate.z)
{
if (this.constrait[boneOrder, 4] > rotate.z + 360f || this.constrait[boneOrder, 5] < rotate.z + 360f)
{
rotate.z = this.constrait[boneOrder, 4];
bOver = true;
}
}
if (this.constrait[boneOrder, 5] < rotate.z)
{
if (this.constrait[boneOrder, 5] < rotate.z - 360f || this.constrait[boneOrder, 4] > rotate.z - 360f)
{
rotate.z = this.constrait[boneOrder, 5];
bOver = true;
}
}
if (bOver)
{
if (!isHand)
{
tr = Quaternion.identity;
tr *= Quaternion.AngleAxis(rotate.z, Vector3.forward);
tr *= Quaternion.AngleAxis(rotate.x, Vector3.right);
tr *= Quaternion.AngleAxis(rotate.y, Vector3.up);
}
else
{
//苦肉の策のツケ
tr = Quaternion.identity;
tr *= Quaternion.AngleAxis(rotate.z, Vector3.forward);
tr *= Quaternion.AngleAxis(rotate.y, Vector3.up);
tr *= Quaternion.AngleAxis(rotate.x, Vector3.right);
}
}
return tr;
}
}
}