FingerBlend.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class FingerBlend : MonoBehaviour
  6. {
  7. public void Awake()
  8. {
  9. if (FingerBlend.open_dic == null)
  10. {
  11. FingerBlend.open_dic = FingerBlend.ReadFingerBoneDataFromrResource("ScenePhotoMode/binary/finger_template_open");
  12. FingerBlend.close_dic = FingerBlend.ReadFingerBoneDataFromrResource("ScenePhotoMode/binary/finger_template_close");
  13. FingerBlend.fist_dic = FingerBlend.ReadFingerBoneDataFromrResource("ScenePhotoMode/binary/finger_template_fist1");
  14. }
  15. }
  16. public void Start()
  17. {
  18. }
  19. public void SetIKManager(IKManager setting_ik_mgr)
  20. {
  21. if (this.ik_mgr != null)
  22. {
  23. return;
  24. }
  25. this.ik_mgr = setting_ik_mgr;
  26. this.right_arm_finger = new FingerBlend.ArmFinger(this, true);
  27. this.left_arm_finger = new FingerBlend.ArmFinger(this, false);
  28. this.right_leg_finger = new FingerBlend.LegFinger(this, true);
  29. this.left_leg_finger = new FingerBlend.LegFinger(this, false);
  30. this.right_arm_finger.enabled = true;
  31. }
  32. public static Dictionary<IKManager.BoneType, Quaternion> ReadFingerBoneDataFromrResource(string path)
  33. {
  34. TextAsset textAsset = Resources.Load(path) as TextAsset;
  35. if (textAsset == null)
  36. {
  37. return null;
  38. }
  39. return FingerBlend.ReadFingerBoneData(textAsset.bytes);
  40. }
  41. public static Dictionary<IKManager.BoneType, Quaternion> ReadFingerBoneData(byte[] data_byte)
  42. {
  43. if (data_byte == null)
  44. {
  45. return null;
  46. }
  47. Dictionary<IKManager.BoneType, Quaternion> dictionary = new Dictionary<IKManager.BoneType, Quaternion>();
  48. using (MemoryStream memoryStream = new MemoryStream(data_byte))
  49. {
  50. using (BinaryReader binaryReader = new BinaryReader(memoryStream))
  51. {
  52. string a = binaryReader.ReadString();
  53. if (a != "CM3D2_IK_FingerBlend")
  54. {
  55. return null;
  56. }
  57. int num = binaryReader.ReadInt32();
  58. int num2 = binaryReader.ReadInt32();
  59. for (int i = 0; i < num2; i++)
  60. {
  61. int num3 = binaryReader.ReadInt32();
  62. if (num == 1000)
  63. {
  64. num3 += 5;
  65. }
  66. Vector4 vector;
  67. vector.x = binaryReader.ReadSingle();
  68. vector.y = binaryReader.ReadSingle();
  69. vector.z = binaryReader.ReadSingle();
  70. vector.w = binaryReader.ReadSingle();
  71. dictionary.Add((IKManager.BoneType)num3, new Quaternion(vector.x, vector.y, vector.z, vector.w));
  72. }
  73. }
  74. }
  75. return dictionary;
  76. }
  77. public static byte[] GetFingerBoneData(IKManager ik_mgr)
  78. {
  79. Dictionary<int, Quaternion> dictionary = new Dictionary<int, Quaternion>();
  80. for (int i = 42; i <= 56; i++)
  81. {
  82. dictionary.Add(i, ik_mgr.GetBone((IKManager.BoneType)i).transform.localRotation);
  83. }
  84. for (int j = 27; j <= 41; j++)
  85. {
  86. dictionary.Add(j, ik_mgr.GetBone((IKManager.BoneType)j).transform.localRotation);
  87. }
  88. for (int k = 63; k <= 68; k++)
  89. {
  90. dictionary.Add(k, ik_mgr.GetBone((IKManager.BoneType)k).transform.localRotation);
  91. }
  92. for (int l = 57; l <= 62; l++)
  93. {
  94. dictionary.Add(l, ik_mgr.GetBone((IKManager.BoneType)l).transform.localRotation);
  95. }
  96. MemoryStream memoryStream = new MemoryStream();
  97. BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
  98. binaryWriter.Write("CM3D2_IK_FingerBlend");
  99. binaryWriter.Write(1000);
  100. binaryWriter.Write(dictionary.Count);
  101. foreach (KeyValuePair<int, Quaternion> keyValuePair in dictionary)
  102. {
  103. binaryWriter.Write(keyValuePair.Key);
  104. Quaternion value = keyValuePair.Value;
  105. binaryWriter.Write(value.x);
  106. binaryWriter.Write(value.y);
  107. binaryWriter.Write(value.z);
  108. binaryWriter.Write(value.w);
  109. }
  110. binaryWriter.Close();
  111. memoryStream.Close();
  112. byte[] result = memoryStream.ToArray();
  113. memoryStream.Dispose();
  114. return result;
  115. }
  116. public Maid maid
  117. {
  118. get
  119. {
  120. return (!(this.ik_mgr != null)) ? null : this.ik_mgr.maid;
  121. }
  122. }
  123. public IKManager ik_mgr { get; private set; }
  124. public FingerBlend.ArmFinger right_arm_finger { get; private set; }
  125. public FingerBlend.ArmFinger left_arm_finger { get; private set; }
  126. public FingerBlend.LegFinger right_leg_finger { get; private set; }
  127. public FingerBlend.LegFinger left_leg_finger { get; private set; }
  128. public static Dictionary<IKManager.BoneType, Quaternion> open_dic;
  129. public static Dictionary<IKManager.BoneType, Quaternion> close_dic;
  130. public static Dictionary<IKManager.BoneType, Quaternion> fist_dic;
  131. public class BaseFinger
  132. {
  133. public BaseFinger(FingerBlend finger_blend)
  134. {
  135. this.finger_blend_ = finger_blend;
  136. }
  137. public virtual void Apply()
  138. {
  139. if (!this.enabled)
  140. {
  141. return;
  142. }
  143. for (int i = 0; i < this.value_array_.Length; i++)
  144. {
  145. float lock_value_open;
  146. float lock_value_fist;
  147. if (!this.value_array_[i].is_lock)
  148. {
  149. lock_value_open = this.value_open_;
  150. lock_value_fist = this.value_fist_;
  151. }
  152. else
  153. {
  154. lock_value_open = this.value_array_[i].lock_value_open;
  155. lock_value_fist = this.value_array_[i].lock_value_fist;
  156. }
  157. foreach (IKManager.BoneType boneType in this.value_array_[i].bone)
  158. {
  159. this.finger_blend_.ik_mgr.GetBone(boneType).transform.localRotation = Quaternion.Lerp(Quaternion.Lerp(FingerBlend.close_dic[boneType], FingerBlend.open_dic[boneType], lock_value_open), FingerBlend.fist_dic[boneType], lock_value_fist);
  160. }
  161. }
  162. }
  163. public void SetBinary(byte[] data_byte, bool mirroring = false)
  164. {
  165. using (MemoryStream memoryStream = new MemoryStream(data_byte))
  166. {
  167. using (BinaryReader binaryReader = new BinaryReader(memoryStream))
  168. {
  169. for (int i = 0; i < this.value_array_.Length; i++)
  170. {
  171. IKManager.BoneType[] bone = this.value_array_[i].bone;
  172. for (int j = 0; j < bone.Length; j++)
  173. {
  174. Vector4 vector;
  175. vector.x = binaryReader.ReadSingle();
  176. vector.y = binaryReader.ReadSingle();
  177. vector.z = binaryReader.ReadSingle();
  178. vector.w = binaryReader.ReadSingle();
  179. if (mirroring)
  180. {
  181. vector.x *= -1f;
  182. vector.y *= -1f;
  183. }
  184. this.finger_blend_.ik_mgr.GetBone(bone[j]).transform.localRotation = new Quaternion(vector.x, vector.y, vector.z, vector.w);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. public byte[] GetBinary()
  191. {
  192. byte[] result = null;
  193. using (MemoryStream memoryStream = new MemoryStream())
  194. {
  195. using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
  196. {
  197. for (int i = 0; i < this.value_array_.Length; i++)
  198. {
  199. IKManager.BoneType[] bone = this.value_array_[i].bone;
  200. for (int j = 0; j < bone.Length; j++)
  201. {
  202. Quaternion localRotation = this.finger_blend_.ik_mgr.GetBone(bone[j]).transform.localRotation;
  203. binaryWriter.Write(localRotation.x);
  204. binaryWriter.Write(localRotation.y);
  205. binaryWriter.Write(localRotation.z);
  206. binaryWriter.Write(localRotation.w);
  207. }
  208. }
  209. result = memoryStream.ToArray();
  210. }
  211. }
  212. return result;
  213. }
  214. public float value_open
  215. {
  216. get
  217. {
  218. return this.value_open_;
  219. }
  220. set
  221. {
  222. this.value_open_ = value;
  223. this.Apply();
  224. }
  225. }
  226. public float value_fist
  227. {
  228. get
  229. {
  230. return this.value_fist_;
  231. }
  232. set
  233. {
  234. this.value_fist_ = value;
  235. this.Apply();
  236. }
  237. }
  238. public bool enabled
  239. {
  240. get
  241. {
  242. return this.enabled_;
  243. }
  244. set
  245. {
  246. this.enabled_ = value;
  247. this.Apply();
  248. }
  249. }
  250. public void LockSingleItemValue(bool set_lock, int value_no, Vector2 value)
  251. {
  252. this.value_array_[value_no].is_lock = set_lock;
  253. this.value_array_[value_no].lock_value_open = value.x;
  254. this.value_array_[value_no].lock_value_fist = value.y;
  255. }
  256. public void LockSingleItem(bool set_lock, int value_no)
  257. {
  258. this.value_array_[value_no].is_lock = set_lock;
  259. this.value_array_[value_no].lock_value_open = this.value_open_;
  260. this.value_array_[value_no].lock_value_fist = this.value_fist_;
  261. }
  262. protected float value_open_;
  263. protected float value_fist_;
  264. protected bool enabled_;
  265. protected FingerBlend.BaseFinger.value_set[] value_array_;
  266. protected FingerBlend finger_blend_;
  267. protected struct value_set
  268. {
  269. public bool is_lock;
  270. public float lock_value_open;
  271. public float lock_value_fist;
  272. public IKManager.BoneType[] bone;
  273. }
  274. }
  275. public class ArmFinger : FingerBlend.BaseFinger
  276. {
  277. public ArmFinger(FingerBlend finger_blend, bool is_right) : base(finger_blend)
  278. {
  279. this.value_array_ = new FingerBlend.BaseFinger.value_set[5];
  280. for (int i = 0; i < this.value_array_.Length; i++)
  281. {
  282. this.value_array_[i].bone = new IKManager.BoneType[3];
  283. for (int j = 0; j < this.value_array_[i].bone.Length; j++)
  284. {
  285. if (is_right)
  286. {
  287. this.value_array_[i].bone[j] = i * this.value_array_[i].bone.Length + IKManager.BoneType.Finger0_Root_R + j;
  288. }
  289. else
  290. {
  291. this.value_array_[i].bone[j] = i * this.value_array_[i].bone.Length + IKManager.BoneType.Finger0_Root_L + j;
  292. }
  293. }
  294. }
  295. }
  296. public bool lock_enabled0
  297. {
  298. get
  299. {
  300. return this.value_array_[0].is_lock;
  301. }
  302. set
  303. {
  304. base.LockSingleItem(value, 0);
  305. }
  306. }
  307. public Vector2 lock_value0
  308. {
  309. get
  310. {
  311. return new Vector2(this.value_array_[0].lock_value_open, this.value_array_[0].lock_value_fist);
  312. }
  313. set
  314. {
  315. if (this.lock_enabled0)
  316. {
  317. base.LockSingleItemValue(true, 0, value);
  318. }
  319. }
  320. }
  321. public bool lock_enabled1
  322. {
  323. get
  324. {
  325. return this.value_array_[1].is_lock;
  326. }
  327. set
  328. {
  329. base.LockSingleItem(value, 1);
  330. }
  331. }
  332. public Vector2 lock_value1
  333. {
  334. get
  335. {
  336. return new Vector2(this.value_array_[1].lock_value_open, this.value_array_[1].lock_value_fist);
  337. }
  338. set
  339. {
  340. if (this.lock_enabled1)
  341. {
  342. base.LockSingleItemValue(true, 1, value);
  343. }
  344. }
  345. }
  346. public bool lock_enabled2
  347. {
  348. get
  349. {
  350. return this.value_array_[2].is_lock;
  351. }
  352. set
  353. {
  354. base.LockSingleItem(value, 2);
  355. }
  356. }
  357. public Vector2 lock_value2
  358. {
  359. get
  360. {
  361. return new Vector2(this.value_array_[2].lock_value_open, this.value_array_[2].lock_value_fist);
  362. }
  363. set
  364. {
  365. if (this.lock_enabled2)
  366. {
  367. base.LockSingleItemValue(true, 2, value);
  368. }
  369. }
  370. }
  371. public bool lock_enabled3
  372. {
  373. get
  374. {
  375. return this.value_array_[3].is_lock;
  376. }
  377. set
  378. {
  379. base.LockSingleItem(value, 3);
  380. }
  381. }
  382. public Vector2 lock_value3
  383. {
  384. get
  385. {
  386. return new Vector2(this.value_array_[3].lock_value_open, this.value_array_[3].lock_value_fist);
  387. }
  388. set
  389. {
  390. if (this.lock_enabled3)
  391. {
  392. base.LockSingleItemValue(true, 3, value);
  393. }
  394. }
  395. }
  396. public bool lock_enabled4
  397. {
  398. get
  399. {
  400. return this.value_array_[4].is_lock;
  401. }
  402. set
  403. {
  404. base.LockSingleItem(value, 4);
  405. }
  406. }
  407. public Vector2 lock_value4
  408. {
  409. get
  410. {
  411. return new Vector2(this.value_array_[4].lock_value_open, this.value_array_[4].lock_value_fist);
  412. }
  413. set
  414. {
  415. if (this.lock_enabled4)
  416. {
  417. base.LockSingleItemValue(true, 4, value);
  418. }
  419. }
  420. }
  421. }
  422. public class LegFinger : FingerBlend.BaseFinger
  423. {
  424. public LegFinger(FingerBlend finger_blend, bool is_right) : base(finger_blend)
  425. {
  426. this.value_array_ = new FingerBlend.BaseFinger.value_set[3];
  427. for (int i = 0; i < this.value_array_.Length; i++)
  428. {
  429. this.value_array_[i].bone = new IKManager.BoneType[2];
  430. for (int j = 0; j < this.value_array_[i].bone.Length; j++)
  431. {
  432. if (is_right)
  433. {
  434. this.value_array_[i].bone[j] = i * this.value_array_[i].bone.Length + IKManager.BoneType.Toe0_Root_R + j;
  435. }
  436. else
  437. {
  438. this.value_array_[i].bone[j] = i * this.value_array_[i].bone.Length + IKManager.BoneType.Toe0_Root_L + j;
  439. }
  440. }
  441. }
  442. }
  443. public bool lock_enabled0
  444. {
  445. get
  446. {
  447. return this.value_array_[0].is_lock;
  448. }
  449. set
  450. {
  451. base.LockSingleItem(value, 0);
  452. }
  453. }
  454. public Vector2 lock_value0
  455. {
  456. get
  457. {
  458. return new Vector2(this.value_array_[0].lock_value_open, this.value_array_[0].lock_value_fist);
  459. }
  460. set
  461. {
  462. if (this.lock_enabled0)
  463. {
  464. base.LockSingleItemValue(true, 0, value);
  465. }
  466. }
  467. }
  468. public bool lock_enabled1
  469. {
  470. get
  471. {
  472. return this.value_array_[1].is_lock;
  473. }
  474. set
  475. {
  476. base.LockSingleItem(value, 1);
  477. }
  478. }
  479. public Vector2 lock_value1
  480. {
  481. get
  482. {
  483. return new Vector2(this.value_array_[1].lock_value_open, this.value_array_[1].lock_value_fist);
  484. }
  485. set
  486. {
  487. if (this.lock_enabled1)
  488. {
  489. base.LockSingleItemValue(true, 1, value);
  490. }
  491. }
  492. }
  493. public bool lock_enabled2
  494. {
  495. get
  496. {
  497. return this.value_array_[2].is_lock;
  498. }
  499. set
  500. {
  501. base.LockSingleItem(value, 2);
  502. }
  503. }
  504. public Vector2 lock_value2
  505. {
  506. get
  507. {
  508. return new Vector2(this.value_array_[2].lock_value_open, this.value_array_[2].lock_value_fist);
  509. }
  510. set
  511. {
  512. if (this.lock_enabled2)
  513. {
  514. base.LockSingleItemValue(true, 2, value);
  515. }
  516. }
  517. }
  518. }
  519. }