DynamicBone.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. [AddComponentMenu("Dynamic Bone/Dynamic Bone")]
  6. public class DynamicBone : MonoBehaviour
  7. {
  8. public bool SerializeWrite(BinaryWriter f_bw)
  9. {
  10. Func<DynamicBone.PartialMode, Dictionary<string, float>, bool> func = delegate(DynamicBone.PartialMode f_pmode, Dictionary<string, float> f_dicValueFromBone)
  11. {
  12. f_bw.Write((int)f_pmode);
  13. if (f_pmode != DynamicBone.PartialMode.Partial)
  14. {
  15. return false;
  16. }
  17. if (this.m_Particles != null)
  18. {
  19. f_bw.Write(this.m_Particles.Count);
  20. for (int j = 0; j < this.m_Particles.Count; j++)
  21. {
  22. string text = this.m_Particles[j].m_Transform.name.ToLower();
  23. f_bw.Write(text);
  24. f_bw.Write(f_dicValueFromBone[text]);
  25. }
  26. return true;
  27. }
  28. NDebug.MessageBox("エラー", "PartialMode.Partialの場合は実行中でないと各ボーン名が解らずボーン個別値指定が保存できません。");
  29. return false;
  30. };
  31. f_bw.Write("CM3D21_PHY");
  32. f_bw.Write(1310);
  33. if (this.m_Root == null)
  34. {
  35. NDebug.MessageBox("エラー", "×物理ルートボーンが設定されていません。");
  36. return false;
  37. }
  38. f_bw.Write(this.m_Root.name);
  39. func(this.m_EnablePartialDamping, this.m_dicPartialDamping);
  40. f_bw.Write(this.m_Damping);
  41. this.SerializeWriteAnimationCurve(f_bw, this.m_DampingDistrib);
  42. func(this.m_EnablePartialElasticity, this.m_dicPartialElasticity);
  43. f_bw.Write(this.m_Elasticity);
  44. this.SerializeWriteAnimationCurve(f_bw, this.m_ElasticityDistrib);
  45. func(this.m_EnablePartialStiffness, this.m_dicPartialStiffness);
  46. f_bw.Write(this.m_Stiffness);
  47. this.SerializeWriteAnimationCurve(f_bw, this.m_StiffnessDistrib);
  48. func(this.m_EnablePartialInert, this.m_dicPartialInert);
  49. f_bw.Write(this.m_Inert);
  50. this.SerializeWriteAnimationCurve(f_bw, this.m_InertDistrib);
  51. func(this.m_EnablePartialRadius, this.m_dicPartialRadius);
  52. f_bw.Write(this.m_Radius);
  53. this.SerializeWriteAnimationCurve(f_bw, this.m_RadiusDistrib);
  54. f_bw.Write(this.m_EndLength);
  55. f_bw.Write(this.m_EndOffset.x);
  56. f_bw.Write(this.m_EndOffset.y);
  57. f_bw.Write(this.m_EndOffset.z);
  58. f_bw.Write(this.m_Gravity.x);
  59. f_bw.Write(this.m_Gravity.y);
  60. f_bw.Write(this.m_Gravity.z);
  61. f_bw.Write(this.m_Force.x);
  62. f_bw.Write(this.m_Force.y);
  63. f_bw.Write(this.m_Force.z);
  64. f_bw.Write(Path.GetFileNameWithoutExtension(this.m_ColliderFileName).ToLower());
  65. f_bw.Write((this.m_Colliders == null) ? 0 : this.m_Colliders.Count);
  66. if (this.m_Colliders != null)
  67. {
  68. }
  69. f_bw.Write((this.m_Exclusions == null) ? 0 : this.m_Exclusions.Count);
  70. if (this.m_Exclusions != null)
  71. {
  72. for (int i = 0; i < this.m_Exclusions.Count; i++)
  73. {
  74. }
  75. }
  76. f_bw.Write((int)this.m_FreezeAxis);
  77. return true;
  78. }
  79. private void SerializeWriteAnimationCurve(BinaryWriter f_bw, AnimationCurve f_ac)
  80. {
  81. Keyframe[] keys = f_ac.keys;
  82. f_bw.Write(keys.Length);
  83. for (int i = 0; i < keys.Length; i++)
  84. {
  85. f_bw.Write(keys[i].time);
  86. f_bw.Write(keys[i].value);
  87. f_bw.Write(keys[i].inTangent);
  88. f_bw.Write(keys[i].outTangent);
  89. }
  90. }
  91. public bool SerializeWriteCollider(BinaryWriter f_bw, string f_strFileName)
  92. {
  93. f_bw.Write("CM3D21_COL");
  94. f_bw.Write(1310);
  95. if (this.m_Colliders == null || this.m_Colliders.Count == 0)
  96. {
  97. NDebug.MessageBox("エラー", "×物理Collidersは空です。");
  98. return false;
  99. }
  100. f_bw.Write(this.m_Colliders.Count);
  101. for (int i = 0; i < this.m_Colliders.Count; i++)
  102. {
  103. if (this.m_Colliders[i] == null)
  104. {
  105. f_bw.Write("missing");
  106. }
  107. else
  108. {
  109. f_bw.Write(this.m_Colliders[i].TypeName);
  110. this.m_Colliders[i].Serialize(f_bw, 1310);
  111. }
  112. }
  113. this.m_ColliderFileName = f_strFileName;
  114. this.m_strNowReadColliderFileName = f_strFileName;
  115. return true;
  116. }
  117. public void SerializeRead(BinaryReader f_br)
  118. {
  119. string a;
  120. Func<Dictionary<string, float>, DynamicBone.PartialMode> func = delegate(Dictionary<string, float> f_dicValueFromBone)
  121. {
  122. DynamicBone.PartialMode partialMode = (DynamicBone.PartialMode)f_br.ReadInt32();
  123. if (partialMode != DynamicBone.PartialMode.Partial)
  124. {
  125. return partialMode;
  126. }
  127. int num4 = f_br.ReadInt32();
  128. if (this.m_Particles != null && this.m_Particles.Count != 0)
  129. {
  130. if (num4 != this.m_Particles.Count)
  131. {
  132. NDebug.Assert("ボーン数が一致していません。", false);
  133. }
  134. for (int k = 0; k < num4; k++)
  135. {
  136. string strName = f_br.ReadString();
  137. if (this.m_Particles.Find((DynamicBone.Particle a) => a.m_Transform.name.ToLower() == strName) == null)
  138. {
  139. NDebug.Assert("ボーンがありません。", false);
  140. }
  141. f_dicValueFromBone[strName] = f_br.ReadSingle();
  142. }
  143. }
  144. else
  145. {
  146. for (int l = 0; l < num4; l++)
  147. {
  148. string key = f_br.ReadString();
  149. f_dicValueFromBone[key] = f_br.ReadSingle();
  150. }
  151. }
  152. return partialMode;
  153. };
  154. if (this.m_Exclusions != null)
  155. {
  156. this.m_Exclusions.Clear();
  157. }
  158. this.m_dicPartialStiffness.Clear();
  159. a = f_br.ReadString();
  160. if (a != "CM3D21_PHY")
  161. {
  162. NDebug.Assert("物理定義ヘッダー不正", false);
  163. }
  164. int num = f_br.ReadInt32();
  165. string text = f_br.ReadString();
  166. this.m_EnablePartialDamping = func(this.m_dicPartialDamping);
  167. this.m_Damping = f_br.ReadSingle();
  168. this.SerializeReadAnimationCurve(f_br, out this.m_DampingDistrib);
  169. this.m_EnablePartialElasticity = func(this.m_dicPartialElasticity);
  170. this.m_Elasticity = f_br.ReadSingle();
  171. this.SerializeReadAnimationCurve(f_br, out this.m_ElasticityDistrib);
  172. this.m_EnablePartialStiffness = func(this.m_dicPartialStiffness);
  173. this.m_Stiffness = f_br.ReadSingle();
  174. this.SerializeReadAnimationCurve(f_br, out this.m_StiffnessDistrib);
  175. this.m_EnablePartialInert = func(this.m_dicPartialInert);
  176. this.m_Inert = f_br.ReadSingle();
  177. this.SerializeReadAnimationCurve(f_br, out this.m_InertDistrib);
  178. this.m_EnablePartialRadius = func(this.m_dicPartialRadius);
  179. this.m_Radius = f_br.ReadSingle();
  180. this.SerializeReadAnimationCurve(f_br, out this.m_RadiusDistrib);
  181. this.m_EndLength = f_br.ReadSingle();
  182. this.m_EndOffset.Set(f_br.ReadSingle(), f_br.ReadSingle(), f_br.ReadSingle());
  183. this.m_Gravity.Set(f_br.ReadSingle(), f_br.ReadSingle(), f_br.ReadSingle());
  184. this.m_Force.Set(f_br.ReadSingle(), f_br.ReadSingle(), f_br.ReadSingle());
  185. this.m_ColliderFileName = f_br.ReadString();
  186. int num2 = f_br.ReadInt32();
  187. for (int i = 0; i < num2; i++)
  188. {
  189. if (this.m_Colliders == null)
  190. {
  191. this.m_Colliders = new List<DynamicBoneColliderBase>();
  192. }
  193. }
  194. int num3 = f_br.ReadInt32();
  195. for (int j = 0; j < num3; j++)
  196. {
  197. if (this.m_Exclusions == null)
  198. {
  199. this.m_Exclusions = new List<Transform>();
  200. }
  201. }
  202. this.m_FreezeAxis = (DynamicBone.FreezeAxis)f_br.ReadInt32();
  203. this.InitTransforms();
  204. this.SetupParticles();
  205. this.m_bSkipStartSetupParticles = true;
  206. }
  207. private void SerializeReadAnimationCurve(BinaryReader f_br, out AnimationCurve f_ac)
  208. {
  209. int num = f_br.ReadInt32();
  210. f_ac = null;
  211. if (num != 0)
  212. {
  213. Keyframe[] array = new Keyframe[num];
  214. for (int i = 0; i < num; i++)
  215. {
  216. array[i] = new Keyframe
  217. {
  218. time = f_br.ReadSingle(),
  219. value = f_br.ReadSingle(),
  220. inTangent = f_br.ReadSingle(),
  221. outTangent = f_br.ReadSingle()
  222. };
  223. }
  224. f_ac = new AnimationCurve(array);
  225. }
  226. }
  227. public void SerializeReadCollider(BinaryReader f_br, Transform f_trBodyBoneRoot, string f_strFileName, Maid f_maid)
  228. {
  229. if (this.m_Colliders == null)
  230. {
  231. this.m_Colliders = new List<DynamicBoneColliderBase>();
  232. }
  233. if (this.m_Colliders != null)
  234. {
  235. if (Application.isPlaying)
  236. {
  237. this.Uninit();
  238. }
  239. this.m_Colliders.Clear();
  240. }
  241. string a = f_br.ReadString();
  242. if (a != "CM3D21_COL")
  243. {
  244. NDebug.Assert("物理Colliderヘッダー不正", false);
  245. }
  246. int f_nVersion = f_br.ReadInt32();
  247. int num = f_br.ReadInt32();
  248. for (int i = 0; i < num; i++)
  249. {
  250. GameObject gameObject = new GameObject();
  251. string text = f_br.ReadString();
  252. if (text == "dbc")
  253. {
  254. DynamicBoneCollider dynamicBoneCollider = gameObject.AddComponent<DynamicBoneCollider>();
  255. dynamicBoneCollider.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
  256. this.m_Colliders.Add(dynamicBoneCollider);
  257. }
  258. else if (text == "dpc")
  259. {
  260. DynamicBonePlaneCollider dynamicBonePlaneCollider = gameObject.AddComponent<DynamicBonePlaneCollider>();
  261. dynamicBonePlaneCollider.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
  262. this.m_Colliders.Add(dynamicBonePlaneCollider);
  263. }
  264. else if (text == "dbm")
  265. {
  266. DynamicBoneMuneCollider dynamicBoneMuneCollider = gameObject.AddComponent<DynamicBoneMuneCollider>();
  267. dynamicBoneMuneCollider.m_maid = f_maid;
  268. dynamicBoneMuneCollider.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
  269. this.m_Colliders.Add(dynamicBoneMuneCollider);
  270. }
  271. else if (!(text == "missing"))
  272. {
  273. NDebug.Assert("不正なColliderです。" + text, false);
  274. }
  275. }
  276. this.m_ColliderFileName = f_strFileName;
  277. this.m_strNowReadColliderFileName = f_strFileName;
  278. if (GameMain.Instance.CMSystem.SConfig.OvrHandHitToHair && GameMain.Instance.VRMode && GameMain.Instance.VRFamily != GameMain.VRFamilyType.NON)
  279. {
  280. Transform vrcontrollerTransform = GameMain.Instance.OvrMgr.GetVRControllerTransform(true);
  281. if (vrcontrollerTransform != null)
  282. {
  283. Transform transform = vrcontrollerTransform.Find(DynamicBone.m_vrHandHitObjName);
  284. if (transform == null)
  285. {
  286. GameObject gameObject2 = new GameObject(DynamicBone.m_vrHandHitObjName);
  287. transform = gameObject2.transform;
  288. transform.transform.SetParent(vrcontrollerTransform, false);
  289. transform.localPosition = new Vector3(0.03f, -0.03f, -0.03f);
  290. }
  291. DynamicBoneCollider orAddComponent = transform.GetOrAddComponent<DynamicBoneCollider>();
  292. orAddComponent.m_Radius = 0.07f;
  293. this.m_Colliders.Add(orAddComponent);
  294. }
  295. Transform vrcontrollerTransform2 = GameMain.Instance.OvrMgr.GetVRControllerTransform(false);
  296. if (vrcontrollerTransform2 != null)
  297. {
  298. Transform transform2 = vrcontrollerTransform2.Find(DynamicBone.m_vrHandHitObjName);
  299. if (transform2 == null)
  300. {
  301. GameObject gameObject3 = new GameObject(DynamicBone.m_vrHandHitObjName);
  302. transform2 = gameObject3.transform;
  303. transform2.transform.SetParent(vrcontrollerTransform2, false);
  304. transform2.localPosition = new Vector3(-0.03f, -0.03f, -0.03f);
  305. }
  306. DynamicBoneCollider orAddComponent2 = transform2.GetOrAddComponent<DynamicBoneCollider>();
  307. orAddComponent2.m_Radius = 0.07f;
  308. this.m_Colliders.Add(orAddComponent2);
  309. }
  310. }
  311. }
  312. public void Uninit()
  313. {
  314. if (this.m_Colliders != null)
  315. {
  316. for (int i = 0; i < this.m_Colliders.Count; i++)
  317. {
  318. if (this.m_Colliders[i] != null && this.m_Colliders[i].gameObject.name != DynamicBone.m_vrHandHitObjName)
  319. {
  320. UnityEngine.Object.DestroyImmediate(this.m_Colliders[i].gameObject);
  321. }
  322. }
  323. this.m_Colliders.Clear();
  324. }
  325. }
  326. private void Start()
  327. {
  328. if (!this.m_bSkipStartSetupParticles)
  329. {
  330. this.SetupParticles();
  331. }
  332. this.m_fRealTimeBefore = Time.realtimeSinceStartup;
  333. }
  334. private void FixedUpdate()
  335. {
  336. if (this.m_UpdateMode == DynamicBone.UpdateMode.AnimatePhysics)
  337. {
  338. this.PreUpdate();
  339. }
  340. }
  341. private void Update()
  342. {
  343. if (this.m_UpdateMode != DynamicBone.UpdateMode.AnimatePhysics)
  344. {
  345. this.PreUpdate();
  346. }
  347. }
  348. private void LateUpdate()
  349. {
  350. if (this.m_DistantDisable)
  351. {
  352. this.CheckDistance();
  353. }
  354. if (this.m_Weight > 0f && (!this.m_DistantDisable || !this.m_DistantDisabled))
  355. {
  356. float t;
  357. if (this.m_UpdateMode == DynamicBone.UpdateMode.UnscaledTime)
  358. {
  359. float realtimeSinceStartup = Time.realtimeSinceStartup;
  360. t = realtimeSinceStartup - this.m_fRealTimeBefore;
  361. this.m_fRealTimeBefore = realtimeSinceStartup;
  362. }
  363. else
  364. {
  365. t = Time.deltaTime;
  366. }
  367. this.UpdateDynamicBones(t);
  368. }
  369. }
  370. private void PreUpdate()
  371. {
  372. if (this.m_Weight > 0f && (!this.m_DistantDisable || !this.m_DistantDisabled))
  373. {
  374. this.InitTransforms();
  375. }
  376. }
  377. private void CheckDistance()
  378. {
  379. Transform transform = this.m_ReferenceObject;
  380. if (transform == null && Camera.main != null)
  381. {
  382. transform = Camera.main.transform;
  383. }
  384. if (transform != null)
  385. {
  386. float sqrMagnitude = (transform.position - base.transform.position).sqrMagnitude;
  387. bool flag = sqrMagnitude > this.m_DistanceToObject * this.m_DistanceToObject;
  388. if (flag != this.m_DistantDisabled)
  389. {
  390. if (!flag)
  391. {
  392. this.ResetParticlesPosition();
  393. }
  394. this.m_DistantDisabled = flag;
  395. }
  396. }
  397. }
  398. private void OnEnable()
  399. {
  400. this.ResetParticlesPosition();
  401. }
  402. private void OnDisable()
  403. {
  404. this.InitTransforms();
  405. }
  406. private void OnValidate()
  407. {
  408. this.m_UpdateRate = Mathf.Max(this.m_UpdateRate, 0f);
  409. this.m_Damping = Mathf.Clamp01(this.m_Damping);
  410. this.m_Elasticity = Mathf.Clamp01(this.m_Elasticity);
  411. this.m_Stiffness = Mathf.Clamp01(this.m_Stiffness);
  412. this.m_Inert = Mathf.Clamp01(this.m_Inert);
  413. this.m_Radius = Mathf.Max(this.m_Radius, 0f);
  414. if (Application.isEditor && Application.isPlaying)
  415. {
  416. this.InitTransforms();
  417. this.SetupParticles();
  418. }
  419. }
  420. public void SetWeight(float w)
  421. {
  422. if (this.m_Weight != w)
  423. {
  424. if (w == 0f)
  425. {
  426. this.InitTransforms();
  427. }
  428. else if (this.m_Weight == 0f)
  429. {
  430. this.ResetParticlesPosition();
  431. }
  432. this.m_Weight = w;
  433. }
  434. }
  435. public float GetWeight()
  436. {
  437. return this.m_Weight;
  438. }
  439. private void UpdateDynamicBones(float t)
  440. {
  441. if (this.m_Root == null)
  442. {
  443. return;
  444. }
  445. this.m_ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  446. this.m_ObjectMove = base.transform.position - this.m_ObjectPrevPosition;
  447. this.m_ObjectPrevPosition = base.transform.position;
  448. int num = 1;
  449. if (this.m_UpdateRate > 0f)
  450. {
  451. float num2 = 1f / this.m_UpdateRate;
  452. this.m_Time += t;
  453. num = 0;
  454. while (this.m_Time >= num2)
  455. {
  456. this.m_Time -= num2;
  457. if (++num >= 3)
  458. {
  459. this.m_Time = 0f;
  460. break;
  461. }
  462. }
  463. }
  464. if (num > 0)
  465. {
  466. for (int i = 0; i < num; i++)
  467. {
  468. this.UpdateParticles1();
  469. this.UpdateParticles2();
  470. this.m_ObjectMove = Vector3.zero;
  471. }
  472. }
  473. else
  474. {
  475. this.SkipUpdateParticles();
  476. }
  477. this.ApplyParticlesToTransforms();
  478. }
  479. private void SetupParticles()
  480. {
  481. this.m_Particles.Clear();
  482. if (this.m_Root == null)
  483. {
  484. return;
  485. }
  486. this.m_LocalGravity = this.m_Root.InverseTransformDirection(this.m_Gravity);
  487. this.m_ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  488. this.m_ObjectPrevPosition = base.transform.position;
  489. this.m_ObjectMove = Vector3.zero;
  490. this.m_BoneTotalLength = 0f;
  491. this.AppendParticles(this.m_Root, -1, 0f);
  492. this.UpdateParameters();
  493. }
  494. private void AppendParticles(Transform b, int parentIndex, float boneLength)
  495. {
  496. DynamicBone.Particle particle = new DynamicBone.Particle();
  497. particle.m_Transform = b;
  498. particle.m_ParentIndex = parentIndex;
  499. if (b != null)
  500. {
  501. particle.m_Position = (particle.m_PrevPosition = b.position);
  502. particle.m_InitLocalPosition = b.localPosition;
  503. particle.m_InitLocalRotation = b.localRotation;
  504. }
  505. else
  506. {
  507. Transform transform = this.m_Particles[parentIndex].m_Transform;
  508. if (this.m_EndLength > 0f)
  509. {
  510. Transform parent = transform.parent;
  511. if (parent != null)
  512. {
  513. particle.m_EndOffset = transform.InverseTransformPoint(transform.position * 2f - parent.position) * this.m_EndLength;
  514. }
  515. else
  516. {
  517. particle.m_EndOffset = new Vector3(this.m_EndLength, 0f, 0f);
  518. }
  519. }
  520. else
  521. {
  522. particle.m_EndOffset = transform.InverseTransformPoint(base.transform.TransformDirection(this.m_EndOffset) + transform.position);
  523. }
  524. particle.m_Position = (particle.m_PrevPosition = transform.TransformPoint(particle.m_EndOffset));
  525. }
  526. if (parentIndex >= 0)
  527. {
  528. boneLength += (this.m_Particles[parentIndex].m_Transform.position - particle.m_Position).magnitude;
  529. particle.m_BoneLength = boneLength;
  530. this.m_BoneTotalLength = Mathf.Max(this.m_BoneTotalLength, boneLength);
  531. }
  532. int count = this.m_Particles.Count;
  533. this.m_Particles.Add(particle);
  534. if (b != null)
  535. {
  536. for (int i = 0; i < b.childCount; i++)
  537. {
  538. bool flag = false;
  539. if (this.m_Exclusions != null)
  540. {
  541. for (int j = 0; j < this.m_Exclusions.Count; j++)
  542. {
  543. Transform x = this.m_Exclusions[j];
  544. if (x == b.GetChild(i))
  545. {
  546. flag = true;
  547. break;
  548. }
  549. }
  550. }
  551. if (!flag)
  552. {
  553. this.AppendParticles(b.GetChild(i), count, boneLength);
  554. }
  555. else if (this.m_EndLength > 0f || this.m_EndOffset != Vector3.zero)
  556. {
  557. this.AppendParticles(null, count, boneLength);
  558. }
  559. }
  560. if (b.childCount == 0 && (this.m_EndLength > 0f || this.m_EndOffset != Vector3.zero))
  561. {
  562. this.AppendParticles(null, count, boneLength);
  563. }
  564. }
  565. }
  566. private void TryGetValueFromBone(DynamicBone.PartialMode f_mode, Dictionary<string, float> f_def, DynamicBone.SaveType f_type, string f_name, ref float f_value)
  567. {
  568. if (f_mode == DynamicBone.PartialMode.Partial)
  569. {
  570. if (!f_def.TryGetValue(f_name, out f_value))
  571. {
  572. float num = 0f;
  573. f_def[f_name] = num;
  574. f_value = num;
  575. }
  576. }
  577. else if (f_mode == DynamicBone.PartialMode.FromBoneName)
  578. {
  579. if (f_name.Contains("_h_"))
  580. {
  581. f_value = this.m_ValueFromBone[(int)f_type, 0];
  582. }
  583. else if (f_name.Contains("_h50_"))
  584. {
  585. f_value = this.m_ValueFromBone[(int)f_type, 1];
  586. }
  587. else
  588. {
  589. f_value = this.m_ValueFromBone[(int)f_type, 2];
  590. }
  591. }
  592. }
  593. public void UpdateParameters()
  594. {
  595. if (this.m_Root == null)
  596. {
  597. return;
  598. }
  599. this.m_LocalGravity = this.m_Root.InverseTransformDirection(this.m_Gravity);
  600. for (int i = 0; i < this.m_Particles.Count; i++)
  601. {
  602. DynamicBone.Particle particle = this.m_Particles[i];
  603. particle.m_Damping = this.m_Damping;
  604. particle.m_Elasticity = this.m_Elasticity;
  605. particle.m_Stiffness = this.m_Stiffness;
  606. particle.m_Inert = this.m_Inert;
  607. particle.m_Radius = this.m_Radius;
  608. if (this.m_BoneTotalLength > 0f)
  609. {
  610. float time = particle.m_BoneLength / this.m_BoneTotalLength;
  611. if (this.m_EnablePartialDamping == DynamicBone.PartialMode.StaticOrCurve && this.m_DampingDistrib != null && this.m_DampingDistrib.keys.Length > 0)
  612. {
  613. particle.m_Damping *= this.m_DampingDistrib.Evaluate(time);
  614. }
  615. if (this.m_EnablePartialElasticity == DynamicBone.PartialMode.StaticOrCurve && this.m_ElasticityDistrib != null && this.m_ElasticityDistrib.keys.Length > 0)
  616. {
  617. particle.m_Elasticity *= this.m_ElasticityDistrib.Evaluate(time);
  618. }
  619. if (this.m_EnablePartialStiffness == DynamicBone.PartialMode.StaticOrCurve && this.m_StiffnessDistrib != null && this.m_StiffnessDistrib.keys.Length > 0)
  620. {
  621. particle.m_Stiffness *= this.m_StiffnessDistrib.Evaluate(time);
  622. }
  623. if (this.m_EnablePartialInert == DynamicBone.PartialMode.StaticOrCurve && this.m_InertDistrib != null && this.m_InertDistrib.keys.Length > 0)
  624. {
  625. particle.m_Inert *= this.m_InertDistrib.Evaluate(time);
  626. }
  627. if (this.m_EnablePartialRadius == DynamicBone.PartialMode.StaticOrCurve && this.m_RadiusDistrib != null && this.m_RadiusDistrib.keys.Length > 0)
  628. {
  629. particle.m_Radius *= this.m_RadiusDistrib.Evaluate(time);
  630. }
  631. }
  632. string f_name = particle.m_Transform.name.ToLower();
  633. this.TryGetValueFromBone(this.m_EnablePartialDamping, this.m_dicPartialDamping, DynamicBone.SaveType.Damping, f_name, ref particle.m_Damping);
  634. this.TryGetValueFromBone(this.m_EnablePartialElasticity, this.m_dicPartialElasticity, DynamicBone.SaveType.Elasticity, f_name, ref particle.m_Elasticity);
  635. this.TryGetValueFromBone(this.m_EnablePartialStiffness, this.m_dicPartialStiffness, DynamicBone.SaveType.Stiffness, f_name, ref particle.m_Stiffness);
  636. this.TryGetValueFromBone(this.m_EnablePartialInert, this.m_dicPartialInert, DynamicBone.SaveType.Inert, f_name, ref particle.m_Inert);
  637. this.TryGetValueFromBone(this.m_EnablePartialRadius, this.m_dicPartialRadius, DynamicBone.SaveType.Radius, f_name, ref particle.m_Radius);
  638. particle.m_Damping = Mathf.Clamp01(particle.m_Damping);
  639. particle.m_Elasticity = Mathf.Clamp01(particle.m_Elasticity);
  640. particle.m_Stiffness = Mathf.Clamp01(particle.m_Stiffness);
  641. particle.m_Inert = Mathf.Clamp01(particle.m_Inert);
  642. particle.m_Radius = Mathf.Max(particle.m_Radius, 0f);
  643. }
  644. }
  645. private void InitTransforms()
  646. {
  647. for (int i = 0; i < this.m_Particles.Count; i++)
  648. {
  649. DynamicBone.Particle particle = this.m_Particles[i];
  650. if (particle.m_Transform != null)
  651. {
  652. particle.m_Transform.localPosition = particle.m_InitLocalPosition;
  653. particle.m_Transform.localRotation = particle.m_InitLocalRotation;
  654. }
  655. }
  656. }
  657. private void ResetParticlesPosition()
  658. {
  659. for (int i = 0; i < this.m_Particles.Count; i++)
  660. {
  661. DynamicBone.Particle particle = this.m_Particles[i];
  662. if (particle.m_Transform != null)
  663. {
  664. particle.m_Position = (particle.m_PrevPosition = particle.m_Transform.position);
  665. }
  666. else
  667. {
  668. Transform transform = this.m_Particles[particle.m_ParentIndex].m_Transform;
  669. particle.m_Position = (particle.m_PrevPosition = transform.TransformPoint(particle.m_EndOffset));
  670. }
  671. }
  672. this.m_ObjectPrevPosition = base.transform.position;
  673. }
  674. private void UpdateParticles1()
  675. {
  676. Vector3 vector = this.m_Gravity;
  677. Vector3 normalized = this.m_Gravity.normalized;
  678. Vector3 lhs = this.m_Root.TransformDirection(this.m_LocalGravity);
  679. Vector3 b = normalized * Mathf.Max(Vector3.Dot(lhs, normalized), 0f);
  680. vector -= b;
  681. vector = (vector + this.m_Force) * this.m_ObjectScale;
  682. for (int i = 0; i < this.m_Particles.Count; i++)
  683. {
  684. DynamicBone.Particle particle = this.m_Particles[i];
  685. if (particle.m_ParentIndex >= 0)
  686. {
  687. Vector3 a = particle.m_Position - particle.m_PrevPosition;
  688. Vector3 b2 = this.m_ObjectMove * particle.m_Inert;
  689. particle.m_PrevPosition = particle.m_Position + b2;
  690. particle.m_Position += a * (1f - particle.m_Damping) + vector + b2;
  691. }
  692. else
  693. {
  694. particle.m_PrevPosition = particle.m_Position;
  695. particle.m_Position = particle.m_Transform.position;
  696. }
  697. }
  698. }
  699. private void UpdateParticles2()
  700. {
  701. Plane plane = default(Plane);
  702. for (int i = 1; i < this.m_Particles.Count; i++)
  703. {
  704. DynamicBone.Particle particle = this.m_Particles[i];
  705. DynamicBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  706. float magnitude;
  707. if (particle.m_Transform != null)
  708. {
  709. magnitude = (particle2.m_Transform.position - particle.m_Transform.position).magnitude;
  710. }
  711. else
  712. {
  713. magnitude = particle2.m_Transform.localToWorldMatrix.MultiplyVector(particle.m_EndOffset).magnitude;
  714. }
  715. float num = Mathf.Lerp(1f, particle.m_Stiffness, this.m_Weight);
  716. if (num > 0f || particle.m_Elasticity > 0f)
  717. {
  718. Matrix4x4 localToWorldMatrix = particle2.m_Transform.localToWorldMatrix;
  719. localToWorldMatrix.SetColumn(3, particle2.m_Position);
  720. Vector3 a;
  721. if (particle.m_Transform != null)
  722. {
  723. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_Transform.localPosition);
  724. }
  725. else
  726. {
  727. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_EndOffset);
  728. }
  729. Vector3 a2 = a - particle.m_Position;
  730. particle.m_Position += a2 * particle.m_Elasticity;
  731. if (num > 0f)
  732. {
  733. a2 = a - particle.m_Position;
  734. float magnitude2 = a2.magnitude;
  735. float num2 = magnitude * (1f - num) * 2f;
  736. if (magnitude2 > num2)
  737. {
  738. particle.m_Position += a2 * ((magnitude2 - num2) / magnitude2);
  739. }
  740. }
  741. }
  742. if (this.m_Colliders != null)
  743. {
  744. float particleRadius = particle.m_Radius * this.m_ObjectScale;
  745. for (int j = 0; j < this.m_Colliders.Count; j++)
  746. {
  747. DynamicBoneColliderBase dynamicBoneColliderBase = this.m_Colliders[j];
  748. if (dynamicBoneColliderBase != null && dynamicBoneColliderBase.enabled)
  749. {
  750. dynamicBoneColliderBase.Collide(ref particle.m_Position, particleRadius);
  751. }
  752. }
  753. if (this.m_hitFloorPlane != null)
  754. {
  755. this.m_hitFloorPlane.Collide(ref particle.m_Position, particleRadius);
  756. }
  757. }
  758. if (this.m_FreezeAxis != DynamicBone.FreezeAxis.None)
  759. {
  760. DynamicBone.FreezeAxis freezeAxis = this.m_FreezeAxis;
  761. if (freezeAxis != DynamicBone.FreezeAxis.X)
  762. {
  763. if (freezeAxis != DynamicBone.FreezeAxis.Y)
  764. {
  765. if (freezeAxis == DynamicBone.FreezeAxis.Z)
  766. {
  767. plane.SetNormalAndPosition(particle2.m_Transform.forward, particle2.m_Position);
  768. }
  769. }
  770. else
  771. {
  772. plane.SetNormalAndPosition(particle2.m_Transform.up, particle2.m_Position);
  773. }
  774. }
  775. else
  776. {
  777. plane.SetNormalAndPosition(particle2.m_Transform.right, particle2.m_Position);
  778. }
  779. particle.m_Position -= plane.normal * plane.GetDistanceToPoint(particle.m_Position);
  780. }
  781. Vector3 a3 = particle2.m_Position - particle.m_Position;
  782. float magnitude3 = a3.magnitude;
  783. if (magnitude3 > 0f)
  784. {
  785. particle.m_Position += a3 * ((magnitude3 - magnitude) / magnitude3);
  786. }
  787. }
  788. }
  789. private void SkipUpdateParticles()
  790. {
  791. for (int i = 0; i < this.m_Particles.Count; i++)
  792. {
  793. DynamicBone.Particle particle = this.m_Particles[i];
  794. if (particle.m_ParentIndex >= 0)
  795. {
  796. particle.m_PrevPosition += this.m_ObjectMove;
  797. particle.m_Position += this.m_ObjectMove;
  798. DynamicBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  799. float magnitude;
  800. if (particle.m_Transform != null)
  801. {
  802. magnitude = (particle2.m_Transform.position - particle.m_Transform.position).magnitude;
  803. }
  804. else
  805. {
  806. magnitude = particle2.m_Transform.localToWorldMatrix.MultiplyVector(particle.m_EndOffset).magnitude;
  807. }
  808. float num = Mathf.Lerp(1f, particle.m_Stiffness, this.m_Weight);
  809. if (num > 0f)
  810. {
  811. Matrix4x4 localToWorldMatrix = particle2.m_Transform.localToWorldMatrix;
  812. localToWorldMatrix.SetColumn(3, particle2.m_Position);
  813. Vector3 a;
  814. if (particle.m_Transform != null)
  815. {
  816. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_Transform.localPosition);
  817. }
  818. else
  819. {
  820. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_EndOffset);
  821. }
  822. Vector3 a2 = a - particle.m_Position;
  823. float magnitude2 = a2.magnitude;
  824. float num2 = magnitude * (1f - num) * 2f;
  825. if (magnitude2 > num2)
  826. {
  827. particle.m_Position += a2 * ((magnitude2 - num2) / magnitude2);
  828. }
  829. }
  830. Vector3 a3 = particle2.m_Position - particle.m_Position;
  831. float magnitude3 = a3.magnitude;
  832. if (magnitude3 > 0f)
  833. {
  834. particle.m_Position += a3 * ((magnitude3 - magnitude) / magnitude3);
  835. }
  836. }
  837. else
  838. {
  839. particle.m_PrevPosition = particle.m_Position;
  840. particle.m_Position = particle.m_Transform.position;
  841. }
  842. }
  843. }
  844. private static Vector3 MirrorVector(Vector3 v, Vector3 axis)
  845. {
  846. return v - axis * (Vector3.Dot(v, axis) * 2f);
  847. }
  848. private void ApplyParticlesToTransforms()
  849. {
  850. for (int i = 1; i < this.m_Particles.Count; i++)
  851. {
  852. DynamicBone.Particle particle = this.m_Particles[i];
  853. DynamicBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  854. if (particle2.m_Transform.childCount <= 1)
  855. {
  856. Vector3 direction;
  857. if (particle.m_Transform != null)
  858. {
  859. direction = particle.m_Transform.localPosition;
  860. }
  861. else
  862. {
  863. direction = particle.m_EndOffset;
  864. }
  865. Vector3 toDirection = particle.m_Position - particle2.m_Position;
  866. Quaternion lhs = Quaternion.FromToRotation(particle2.m_Transform.TransformDirection(direction), toDirection);
  867. particle2.m_Transform.rotation = lhs * particle2.m_Transform.rotation;
  868. }
  869. if (particle.m_Transform != null)
  870. {
  871. particle.m_Transform.position = particle.m_Position;
  872. }
  873. }
  874. }
  875. [ReadOnly]
  876. public string m_strNowReadPhysicsFileName;
  877. [Tooltip("The root of the transform hierarchy to apply physics.")]
  878. public Transform m_Root;
  879. [Tooltip("Internal physics simulation rate.")]
  880. public float m_UpdateRate;
  881. public DynamicBone.UpdateMode m_UpdateMode = DynamicBone.UpdateMode.UnscaledTime;
  882. [Header("■減衰率 設定タイプ")]
  883. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  884. public DynamicBone.PartialMode m_EnablePartialDamping;
  885. public Dictionary<string, float> m_dicPartialDamping = new Dictionary<string, float>();
  886. [Header("減衰率")]
  887. [Tooltip("How much the bones slowed down.")]
  888. [Range(0f, 1f)]
  889. public float m_Damping = 0.5f;
  890. public AnimationCurve m_DampingDistrib;
  891. [Header("■復元率 設定タイプ")]
  892. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  893. public DynamicBone.PartialMode m_EnablePartialElasticity;
  894. public Dictionary<string, float> m_dicPartialElasticity = new Dictionary<string, float>();
  895. [Header("復元率")]
  896. [Tooltip("How much the force applied to return each bone to original orientation.")]
  897. [Range(0f, 1f)]
  898. public float m_Elasticity = 0.1f;
  899. public AnimationCurve m_ElasticityDistrib;
  900. [Header("■元の向きの影響力 設定タイプ")]
  901. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  902. public DynamicBone.PartialMode m_EnablePartialStiffness;
  903. public Dictionary<string, float> m_dicPartialStiffness = new Dictionary<string, float>();
  904. [Header("元の向きの影響力(StaticOrCurve)")]
  905. [Tooltip("How much bone's original orientation are preserved.")]
  906. [Range(0f, 1f)]
  907. public float m_Stiffness = 0.1f;
  908. public AnimationCurve m_StiffnessDistrib;
  909. [Header("■移動距離による物理無視率 設定タイプ")]
  910. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  911. public DynamicBone.PartialMode m_EnablePartialInert;
  912. public Dictionary<string, float> m_dicPartialInert = new Dictionary<string, float>();
  913. [Header("移動距離による物理無視率")]
  914. [Tooltip("How much character's position change is ignored in physics simulation.")]
  915. [Range(0f, 1f)]
  916. public float m_Inert;
  917. public AnimationCurve m_InertDistrib;
  918. [Header("■各ノードの当たり判定半径 設定タイプ")]
  919. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  920. public DynamicBone.PartialMode m_EnablePartialRadius;
  921. [SerializeField]
  922. public Dictionary<string, float> m_dicPartialRadius = new Dictionary<string, float>();
  923. [Header("各ノードの当たり判定半径")]
  924. [Tooltip("Each bone can be a sphere to collide with colliders. Radius describe sphere's size.")]
  925. public float m_Radius;
  926. public AnimationCurve m_RadiusDistrib;
  927. [Header("先端に余分ボーンの追加")]
  928. [Tooltip("If End Length is not zero, an extra bone is generated at the end of transform hierarchy.")]
  929. public float m_EndLength;
  930. [Tooltip("If End Offset is not zero, an extra bone is generated at the end of transform hierarchy.")]
  931. [Header("先端に余分ボーンのオフセット座標")]
  932. public Vector3 m_EndOffset = Vector3.zero;
  933. [Tooltip("力は骨に適用されます。キャラクターの初期ポーズに適用される部分的な力は相殺されます。The force apply to bones. Partial force apply to character's initial pose is cancelled out.")]
  934. public Vector3 m_Gravity = new Vector3(0f, 0f, 0f);
  935. [Header("力を加える")]
  936. [Tooltip("The force apply to bones.")]
  937. public Vector3 m_Force = new Vector3(0f, -0.01f, 0f);
  938. [Header("衝突相手ファイル")]
  939. public string m_ColliderFileName;
  940. [ReadOnly]
  941. public string m_strNowReadColliderFileName;
  942. public bool m_ViewColliderGizmo;
  943. [Header("各ノードが衝突する相手(実機では[衝突相手ファイル]により適用される)")]
  944. [Tooltip("Collider objects interact with the bones.")]
  945. public List<DynamicBoneColliderBase> m_Colliders;
  946. [Header("物理計算から特別に除外するノード(子供も動かなくなる)")]
  947. [Tooltip("Bones exclude from physics simulation.")]
  948. public List<Transform> m_Exclusions;
  949. [Tooltip("Constrain bones to move on specified plane.")]
  950. public DynamicBone.FreezeAxis m_FreezeAxis;
  951. [Header("距離による物理自動無効化")]
  952. [Tooltip("キャラクターがカメラやプレーヤーから遠い場合、物理シミュレーションを自動的に無効にします。")]
  953. public bool m_DistantDisable;
  954. public Transform m_ReferenceObject;
  955. public float m_DistanceToObject = 20f;
  956. [HideInInspector]
  957. [NonSerialized]
  958. public DynamicBonePlaneCollider m_hitFloorPlane;
  959. private Vector3 m_LocalGravity = Vector3.zero;
  960. private Vector3 m_ObjectMove = Vector3.zero;
  961. private Vector3 m_ObjectPrevPosition = Vector3.zero;
  962. private float m_BoneTotalLength;
  963. private float m_ObjectScale = 1f;
  964. private float m_Time;
  965. private float m_Weight = 1f;
  966. private bool m_DistantDisabled;
  967. private float m_fRealTimeBefore;
  968. public float[,] m_ValueFromBone = new float[,]
  969. {
  970. {
  971. 0.5f,
  972. 0.5f,
  973. 0.5f
  974. },
  975. {
  976. 0.03f,
  977. 0.03f,
  978. 0.03f
  979. },
  980. {
  981. 0.95f,
  982. 0.8f,
  983. 0.5f
  984. },
  985. {
  986. 0f,
  987. 0f,
  988. 0f
  989. },
  990. {
  991. 0.3f,
  992. 0.3f,
  993. 0.3f
  994. }
  995. };
  996. public List<DynamicBone.Particle> m_Particles = new List<DynamicBone.Particle>();
  997. private bool m_bSkipStartSetupParticles;
  998. private static string m_vrHandHitObjName = "VR Hand Hair Hit";
  999. private bool m_bHandHitEnableBefore;
  1000. public enum UpdateMode
  1001. {
  1002. Normal,
  1003. AnimatePhysics,
  1004. UnscaledTime
  1005. }
  1006. public enum PartialMode
  1007. {
  1008. StaticOrCurve,
  1009. Partial,
  1010. FromBoneName
  1011. }
  1012. public enum FreezeAxis
  1013. {
  1014. None,
  1015. X,
  1016. Y,
  1017. Z
  1018. }
  1019. public enum SaveType
  1020. {
  1021. Damping,
  1022. Elasticity,
  1023. Stiffness,
  1024. Inert,
  1025. Radius,
  1026. MAX
  1027. }
  1028. public class Particle
  1029. {
  1030. public Transform m_Transform;
  1031. public int m_ParentIndex = -1;
  1032. public float m_Damping;
  1033. public float m_Elasticity;
  1034. public float m_Stiffness;
  1035. public float m_Inert;
  1036. public float m_Radius;
  1037. public float m_BoneLength;
  1038. public Vector3 m_Position = Vector3.zero;
  1039. public Vector3 m_PrevPosition = Vector3.zero;
  1040. public Vector3 m_EndOffset = Vector3.zero;
  1041. public Vector3 m_InitLocalPosition = Vector3.zero;
  1042. public Quaternion m_InitLocalRotation = Quaternion.identity;
  1043. }
  1044. }