DynamicBone.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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(1130);
  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(1130);
  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, 1130);
  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. for (int i = 0; i < this.m_Colliders.Count; i++)
  238. {
  239. if (this.m_Colliders[i] != null)
  240. {
  241. UnityEngine.Object.DestroyImmediate(this.m_Colliders[i].gameObject);
  242. }
  243. }
  244. }
  245. this.m_Colliders.Clear();
  246. }
  247. string a = f_br.ReadString();
  248. if (a != "CM3D21_COL")
  249. {
  250. NDebug.Assert("物理Colliderヘッダー不正", false);
  251. }
  252. int f_nVersion = f_br.ReadInt32();
  253. int num = f_br.ReadInt32();
  254. for (int j = 0; j < num; j++)
  255. {
  256. GameObject gameObject = new GameObject();
  257. string text = f_br.ReadString();
  258. if (text == "dbc")
  259. {
  260. DynamicBoneCollider dynamicBoneCollider = gameObject.AddComponent<DynamicBoneCollider>();
  261. dynamicBoneCollider.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
  262. this.m_Colliders.Add(dynamicBoneCollider);
  263. }
  264. else if (text == "dpc")
  265. {
  266. DynamicBonePlaneCollider dynamicBonePlaneCollider = gameObject.AddComponent<DynamicBonePlaneCollider>();
  267. dynamicBonePlaneCollider.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
  268. this.m_Colliders.Add(dynamicBonePlaneCollider);
  269. }
  270. else if (text == "dbm")
  271. {
  272. DynamicBoneMuneCollider dynamicBoneMuneCollider = gameObject.AddComponent<DynamicBoneMuneCollider>();
  273. dynamicBoneMuneCollider.m_maid = f_maid;
  274. dynamicBoneMuneCollider.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
  275. this.m_Colliders.Add(dynamicBoneMuneCollider);
  276. }
  277. else if (!(text == "missing"))
  278. {
  279. NDebug.Assert("不正なColliderです。" + text, false);
  280. }
  281. }
  282. this.m_ColliderFileName = f_strFileName;
  283. this.m_strNowReadColliderFileName = f_strFileName;
  284. }
  285. private void Start()
  286. {
  287. if (!this.m_bSkipStartSetupParticles)
  288. {
  289. this.SetupParticles();
  290. }
  291. this.m_fRealTimeBefore = Time.realtimeSinceStartup;
  292. }
  293. private void FixedUpdate()
  294. {
  295. if (this.m_UpdateMode == DynamicBone.UpdateMode.AnimatePhysics)
  296. {
  297. this.PreUpdate();
  298. }
  299. }
  300. private void Update()
  301. {
  302. if (this.m_UpdateMode != DynamicBone.UpdateMode.AnimatePhysics)
  303. {
  304. this.PreUpdate();
  305. }
  306. }
  307. private void LateUpdate()
  308. {
  309. if (this.m_DistantDisable)
  310. {
  311. this.CheckDistance();
  312. }
  313. if (this.m_Weight > 0f && (!this.m_DistantDisable || !this.m_DistantDisabled))
  314. {
  315. float t;
  316. if (this.m_UpdateMode == DynamicBone.UpdateMode.UnscaledTime)
  317. {
  318. float realtimeSinceStartup = Time.realtimeSinceStartup;
  319. t = realtimeSinceStartup - this.m_fRealTimeBefore;
  320. this.m_fRealTimeBefore = realtimeSinceStartup;
  321. }
  322. else
  323. {
  324. t = Time.deltaTime;
  325. }
  326. this.UpdateDynamicBones(t);
  327. }
  328. }
  329. private void PreUpdate()
  330. {
  331. if (this.m_Weight > 0f && (!this.m_DistantDisable || !this.m_DistantDisabled))
  332. {
  333. this.InitTransforms();
  334. }
  335. }
  336. private void CheckDistance()
  337. {
  338. Transform transform = this.m_ReferenceObject;
  339. if (transform == null && Camera.main != null)
  340. {
  341. transform = Camera.main.transform;
  342. }
  343. if (transform != null)
  344. {
  345. float sqrMagnitude = (transform.position - base.transform.position).sqrMagnitude;
  346. bool flag = sqrMagnitude > this.m_DistanceToObject * this.m_DistanceToObject;
  347. if (flag != this.m_DistantDisabled)
  348. {
  349. if (!flag)
  350. {
  351. this.ResetParticlesPosition();
  352. }
  353. this.m_DistantDisabled = flag;
  354. }
  355. }
  356. }
  357. private void OnEnable()
  358. {
  359. this.ResetParticlesPosition();
  360. }
  361. private void OnDisable()
  362. {
  363. this.InitTransforms();
  364. }
  365. private void OnValidate()
  366. {
  367. this.m_UpdateRate = Mathf.Max(this.m_UpdateRate, 0f);
  368. this.m_Damping = Mathf.Clamp01(this.m_Damping);
  369. this.m_Elasticity = Mathf.Clamp01(this.m_Elasticity);
  370. this.m_Stiffness = Mathf.Clamp01(this.m_Stiffness);
  371. this.m_Inert = Mathf.Clamp01(this.m_Inert);
  372. this.m_Radius = Mathf.Max(this.m_Radius, 0f);
  373. if (Application.isEditor && Application.isPlaying)
  374. {
  375. this.InitTransforms();
  376. this.SetupParticles();
  377. }
  378. }
  379. public void SetWeight(float w)
  380. {
  381. if (this.m_Weight != w)
  382. {
  383. if (w == 0f)
  384. {
  385. this.InitTransforms();
  386. }
  387. else if (this.m_Weight == 0f)
  388. {
  389. this.ResetParticlesPosition();
  390. }
  391. this.m_Weight = w;
  392. }
  393. }
  394. public float GetWeight()
  395. {
  396. return this.m_Weight;
  397. }
  398. private void UpdateDynamicBones(float t)
  399. {
  400. if (this.m_Root == null)
  401. {
  402. return;
  403. }
  404. this.m_ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  405. this.m_ObjectMove = base.transform.position - this.m_ObjectPrevPosition;
  406. this.m_ObjectPrevPosition = base.transform.position;
  407. int num = 1;
  408. if (this.m_UpdateRate > 0f)
  409. {
  410. float num2 = 1f / this.m_UpdateRate;
  411. this.m_Time += t;
  412. num = 0;
  413. while (this.m_Time >= num2)
  414. {
  415. this.m_Time -= num2;
  416. if (++num >= 3)
  417. {
  418. this.m_Time = 0f;
  419. break;
  420. }
  421. }
  422. }
  423. if (num > 0)
  424. {
  425. for (int i = 0; i < num; i++)
  426. {
  427. this.UpdateParticles1();
  428. this.UpdateParticles2();
  429. this.m_ObjectMove = Vector3.zero;
  430. }
  431. }
  432. else
  433. {
  434. this.SkipUpdateParticles();
  435. }
  436. this.ApplyParticlesToTransforms();
  437. }
  438. private void SetupParticles()
  439. {
  440. this.m_Particles.Clear();
  441. if (this.m_Root == null)
  442. {
  443. return;
  444. }
  445. this.m_LocalGravity = this.m_Root.InverseTransformDirection(this.m_Gravity);
  446. this.m_ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  447. this.m_ObjectPrevPosition = base.transform.position;
  448. this.m_ObjectMove = Vector3.zero;
  449. this.m_BoneTotalLength = 0f;
  450. this.AppendParticles(this.m_Root, -1, 0f);
  451. this.UpdateParameters();
  452. }
  453. private void AppendParticles(Transform b, int parentIndex, float boneLength)
  454. {
  455. DynamicBone.Particle particle = new DynamicBone.Particle();
  456. particle.m_Transform = b;
  457. particle.m_ParentIndex = parentIndex;
  458. if (b != null)
  459. {
  460. particle.m_Position = (particle.m_PrevPosition = b.position);
  461. particle.m_InitLocalPosition = b.localPosition;
  462. particle.m_InitLocalRotation = b.localRotation;
  463. }
  464. else
  465. {
  466. Transform transform = this.m_Particles[parentIndex].m_Transform;
  467. if (this.m_EndLength > 0f)
  468. {
  469. Transform parent = transform.parent;
  470. if (parent != null)
  471. {
  472. particle.m_EndOffset = transform.InverseTransformPoint(transform.position * 2f - parent.position) * this.m_EndLength;
  473. }
  474. else
  475. {
  476. particle.m_EndOffset = new Vector3(this.m_EndLength, 0f, 0f);
  477. }
  478. }
  479. else
  480. {
  481. particle.m_EndOffset = transform.InverseTransformPoint(base.transform.TransformDirection(this.m_EndOffset) + transform.position);
  482. }
  483. particle.m_Position = (particle.m_PrevPosition = transform.TransformPoint(particle.m_EndOffset));
  484. }
  485. if (parentIndex >= 0)
  486. {
  487. boneLength += (this.m_Particles[parentIndex].m_Transform.position - particle.m_Position).magnitude;
  488. particle.m_BoneLength = boneLength;
  489. this.m_BoneTotalLength = Mathf.Max(this.m_BoneTotalLength, boneLength);
  490. }
  491. int count = this.m_Particles.Count;
  492. this.m_Particles.Add(particle);
  493. if (b != null)
  494. {
  495. for (int i = 0; i < b.childCount; i++)
  496. {
  497. bool flag = false;
  498. if (this.m_Exclusions != null)
  499. {
  500. for (int j = 0; j < this.m_Exclusions.Count; j++)
  501. {
  502. Transform x = this.m_Exclusions[j];
  503. if (x == b.GetChild(i))
  504. {
  505. flag = true;
  506. break;
  507. }
  508. }
  509. }
  510. if (!flag)
  511. {
  512. this.AppendParticles(b.GetChild(i), count, boneLength);
  513. }
  514. else if (this.m_EndLength > 0f || this.m_EndOffset != Vector3.zero)
  515. {
  516. this.AppendParticles(null, count, boneLength);
  517. }
  518. }
  519. if (b.childCount == 0 && (this.m_EndLength > 0f || this.m_EndOffset != Vector3.zero))
  520. {
  521. this.AppendParticles(null, count, boneLength);
  522. }
  523. }
  524. }
  525. private void TryGetValueFromBone(DynamicBone.PartialMode f_mode, Dictionary<string, float> f_def, DynamicBone.SaveType f_type, string f_name, ref float f_value)
  526. {
  527. if (f_mode == DynamicBone.PartialMode.Partial)
  528. {
  529. if (!f_def.TryGetValue(f_name, out f_value))
  530. {
  531. float num = 0f;
  532. f_def[f_name] = num;
  533. f_value = num;
  534. }
  535. }
  536. else if (f_mode == DynamicBone.PartialMode.FromBoneName)
  537. {
  538. if (f_name.Contains("_h_"))
  539. {
  540. f_value = this.m_ValueFromBone[(int)f_type, 0];
  541. }
  542. else if (f_name.Contains("_h50_"))
  543. {
  544. f_value = this.m_ValueFromBone[(int)f_type, 1];
  545. }
  546. else
  547. {
  548. f_value = this.m_ValueFromBone[(int)f_type, 2];
  549. }
  550. }
  551. }
  552. public void UpdateParameters()
  553. {
  554. if (this.m_Root == null)
  555. {
  556. return;
  557. }
  558. this.m_LocalGravity = this.m_Root.InverseTransformDirection(this.m_Gravity);
  559. for (int i = 0; i < this.m_Particles.Count; i++)
  560. {
  561. DynamicBone.Particle particle = this.m_Particles[i];
  562. particle.m_Damping = this.m_Damping;
  563. particle.m_Elasticity = this.m_Elasticity;
  564. particle.m_Stiffness = this.m_Stiffness;
  565. particle.m_Inert = this.m_Inert;
  566. particle.m_Radius = this.m_Radius;
  567. if (this.m_BoneTotalLength > 0f)
  568. {
  569. float time = particle.m_BoneLength / this.m_BoneTotalLength;
  570. if (this.m_EnablePartialDamping == DynamicBone.PartialMode.StaticOrCurve && this.m_DampingDistrib != null && this.m_DampingDistrib.keys.Length > 0)
  571. {
  572. particle.m_Damping *= this.m_DampingDistrib.Evaluate(time);
  573. }
  574. if (this.m_EnablePartialElasticity == DynamicBone.PartialMode.StaticOrCurve && this.m_ElasticityDistrib != null && this.m_ElasticityDistrib.keys.Length > 0)
  575. {
  576. particle.m_Elasticity *= this.m_ElasticityDistrib.Evaluate(time);
  577. }
  578. if (this.m_EnablePartialStiffness == DynamicBone.PartialMode.StaticOrCurve && this.m_StiffnessDistrib != null && this.m_StiffnessDistrib.keys.Length > 0)
  579. {
  580. particle.m_Stiffness *= this.m_StiffnessDistrib.Evaluate(time);
  581. }
  582. if (this.m_EnablePartialInert == DynamicBone.PartialMode.StaticOrCurve && this.m_InertDistrib != null && this.m_InertDistrib.keys.Length > 0)
  583. {
  584. particle.m_Inert *= this.m_InertDistrib.Evaluate(time);
  585. }
  586. if (this.m_EnablePartialRadius == DynamicBone.PartialMode.StaticOrCurve && this.m_RadiusDistrib != null && this.m_RadiusDistrib.keys.Length > 0)
  587. {
  588. particle.m_Radius *= this.m_RadiusDistrib.Evaluate(time);
  589. }
  590. }
  591. string f_name = particle.m_Transform.name.ToLower();
  592. this.TryGetValueFromBone(this.m_EnablePartialDamping, this.m_dicPartialDamping, DynamicBone.SaveType.Damping, f_name, ref particle.m_Damping);
  593. this.TryGetValueFromBone(this.m_EnablePartialElasticity, this.m_dicPartialElasticity, DynamicBone.SaveType.Elasticity, f_name, ref particle.m_Elasticity);
  594. this.TryGetValueFromBone(this.m_EnablePartialStiffness, this.m_dicPartialStiffness, DynamicBone.SaveType.Stiffness, f_name, ref particle.m_Stiffness);
  595. this.TryGetValueFromBone(this.m_EnablePartialInert, this.m_dicPartialInert, DynamicBone.SaveType.Inert, f_name, ref particle.m_Inert);
  596. this.TryGetValueFromBone(this.m_EnablePartialRadius, this.m_dicPartialRadius, DynamicBone.SaveType.Radius, f_name, ref particle.m_Radius);
  597. particle.m_Damping = Mathf.Clamp01(particle.m_Damping);
  598. particle.m_Elasticity = Mathf.Clamp01(particle.m_Elasticity);
  599. particle.m_Stiffness = Mathf.Clamp01(particle.m_Stiffness);
  600. particle.m_Inert = Mathf.Clamp01(particle.m_Inert);
  601. particle.m_Radius = Mathf.Max(particle.m_Radius, 0f);
  602. }
  603. }
  604. private void InitTransforms()
  605. {
  606. for (int i = 0; i < this.m_Particles.Count; i++)
  607. {
  608. DynamicBone.Particle particle = this.m_Particles[i];
  609. if (particle.m_Transform != null)
  610. {
  611. particle.m_Transform.localPosition = particle.m_InitLocalPosition;
  612. particle.m_Transform.localRotation = particle.m_InitLocalRotation;
  613. }
  614. }
  615. }
  616. private void ResetParticlesPosition()
  617. {
  618. for (int i = 0; i < this.m_Particles.Count; i++)
  619. {
  620. DynamicBone.Particle particle = this.m_Particles[i];
  621. if (particle.m_Transform != null)
  622. {
  623. particle.m_Position = (particle.m_PrevPosition = particle.m_Transform.position);
  624. }
  625. else
  626. {
  627. Transform transform = this.m_Particles[particle.m_ParentIndex].m_Transform;
  628. particle.m_Position = (particle.m_PrevPosition = transform.TransformPoint(particle.m_EndOffset));
  629. }
  630. }
  631. this.m_ObjectPrevPosition = base.transform.position;
  632. }
  633. private void UpdateParticles1()
  634. {
  635. Vector3 vector = this.m_Gravity;
  636. Vector3 normalized = this.m_Gravity.normalized;
  637. Vector3 lhs = this.m_Root.TransformDirection(this.m_LocalGravity);
  638. Vector3 b = normalized * Mathf.Max(Vector3.Dot(lhs, normalized), 0f);
  639. vector -= b;
  640. vector = (vector + this.m_Force) * this.m_ObjectScale;
  641. for (int i = 0; i < this.m_Particles.Count; i++)
  642. {
  643. DynamicBone.Particle particle = this.m_Particles[i];
  644. if (particle.m_ParentIndex >= 0)
  645. {
  646. Vector3 a = particle.m_Position - particle.m_PrevPosition;
  647. Vector3 b2 = this.m_ObjectMove * particle.m_Inert;
  648. particle.m_PrevPosition = particle.m_Position + b2;
  649. particle.m_Position += a * (1f - particle.m_Damping) + vector + b2;
  650. }
  651. else
  652. {
  653. particle.m_PrevPosition = particle.m_Position;
  654. particle.m_Position = particle.m_Transform.position;
  655. }
  656. }
  657. }
  658. private void UpdateParticles2()
  659. {
  660. Plane plane = default(Plane);
  661. for (int i = 1; i < this.m_Particles.Count; i++)
  662. {
  663. DynamicBone.Particle particle = this.m_Particles[i];
  664. DynamicBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  665. float magnitude;
  666. if (particle.m_Transform != null)
  667. {
  668. magnitude = (particle2.m_Transform.position - particle.m_Transform.position).magnitude;
  669. }
  670. else
  671. {
  672. magnitude = particle2.m_Transform.localToWorldMatrix.MultiplyVector(particle.m_EndOffset).magnitude;
  673. }
  674. float num = Mathf.Lerp(1f, particle.m_Stiffness, this.m_Weight);
  675. if (num > 0f || particle.m_Elasticity > 0f)
  676. {
  677. Matrix4x4 localToWorldMatrix = particle2.m_Transform.localToWorldMatrix;
  678. localToWorldMatrix.SetColumn(3, particle2.m_Position);
  679. Vector3 a;
  680. if (particle.m_Transform != null)
  681. {
  682. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_Transform.localPosition);
  683. }
  684. else
  685. {
  686. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_EndOffset);
  687. }
  688. Vector3 a2 = a - particle.m_Position;
  689. particle.m_Position += a2 * particle.m_Elasticity;
  690. if (num > 0f)
  691. {
  692. a2 = a - particle.m_Position;
  693. float magnitude2 = a2.magnitude;
  694. float num2 = magnitude * (1f - num) * 2f;
  695. if (magnitude2 > num2)
  696. {
  697. particle.m_Position += a2 * ((magnitude2 - num2) / magnitude2);
  698. }
  699. }
  700. }
  701. if (this.m_Colliders != null)
  702. {
  703. float particleRadius = particle.m_Radius * this.m_ObjectScale;
  704. for (int j = 0; j < this.m_Colliders.Count; j++)
  705. {
  706. DynamicBoneColliderBase dynamicBoneColliderBase = this.m_Colliders[j];
  707. if (dynamicBoneColliderBase != null && dynamicBoneColliderBase.enabled)
  708. {
  709. dynamicBoneColliderBase.Collide(ref particle.m_Position, particleRadius);
  710. }
  711. }
  712. if (this.m_hitFloorPlane != null)
  713. {
  714. this.m_hitFloorPlane.Collide(ref particle.m_Position, particleRadius);
  715. }
  716. }
  717. if (this.m_FreezeAxis != DynamicBone.FreezeAxis.None)
  718. {
  719. DynamicBone.FreezeAxis freezeAxis = this.m_FreezeAxis;
  720. if (freezeAxis != DynamicBone.FreezeAxis.X)
  721. {
  722. if (freezeAxis != DynamicBone.FreezeAxis.Y)
  723. {
  724. if (freezeAxis == DynamicBone.FreezeAxis.Z)
  725. {
  726. plane.SetNormalAndPosition(particle2.m_Transform.forward, particle2.m_Position);
  727. }
  728. }
  729. else
  730. {
  731. plane.SetNormalAndPosition(particle2.m_Transform.up, particle2.m_Position);
  732. }
  733. }
  734. else
  735. {
  736. plane.SetNormalAndPosition(particle2.m_Transform.right, particle2.m_Position);
  737. }
  738. particle.m_Position -= plane.normal * plane.GetDistanceToPoint(particle.m_Position);
  739. }
  740. Vector3 a3 = particle2.m_Position - particle.m_Position;
  741. float magnitude3 = a3.magnitude;
  742. if (magnitude3 > 0f)
  743. {
  744. particle.m_Position += a3 * ((magnitude3 - magnitude) / magnitude3);
  745. }
  746. }
  747. }
  748. private void SkipUpdateParticles()
  749. {
  750. for (int i = 0; i < this.m_Particles.Count; i++)
  751. {
  752. DynamicBone.Particle particle = this.m_Particles[i];
  753. if (particle.m_ParentIndex >= 0)
  754. {
  755. particle.m_PrevPosition += this.m_ObjectMove;
  756. particle.m_Position += this.m_ObjectMove;
  757. DynamicBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  758. float magnitude;
  759. if (particle.m_Transform != null)
  760. {
  761. magnitude = (particle2.m_Transform.position - particle.m_Transform.position).magnitude;
  762. }
  763. else
  764. {
  765. magnitude = particle2.m_Transform.localToWorldMatrix.MultiplyVector(particle.m_EndOffset).magnitude;
  766. }
  767. float num = Mathf.Lerp(1f, particle.m_Stiffness, this.m_Weight);
  768. if (num > 0f)
  769. {
  770. Matrix4x4 localToWorldMatrix = particle2.m_Transform.localToWorldMatrix;
  771. localToWorldMatrix.SetColumn(3, particle2.m_Position);
  772. Vector3 a;
  773. if (particle.m_Transform != null)
  774. {
  775. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_Transform.localPosition);
  776. }
  777. else
  778. {
  779. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_EndOffset);
  780. }
  781. Vector3 a2 = a - particle.m_Position;
  782. float magnitude2 = a2.magnitude;
  783. float num2 = magnitude * (1f - num) * 2f;
  784. if (magnitude2 > num2)
  785. {
  786. particle.m_Position += a2 * ((magnitude2 - num2) / magnitude2);
  787. }
  788. }
  789. Vector3 a3 = particle2.m_Position - particle.m_Position;
  790. float magnitude3 = a3.magnitude;
  791. if (magnitude3 > 0f)
  792. {
  793. particle.m_Position += a3 * ((magnitude3 - magnitude) / magnitude3);
  794. }
  795. }
  796. else
  797. {
  798. particle.m_PrevPosition = particle.m_Position;
  799. particle.m_Position = particle.m_Transform.position;
  800. }
  801. }
  802. }
  803. private static Vector3 MirrorVector(Vector3 v, Vector3 axis)
  804. {
  805. return v - axis * (Vector3.Dot(v, axis) * 2f);
  806. }
  807. private void ApplyParticlesToTransforms()
  808. {
  809. for (int i = 1; i < this.m_Particles.Count; i++)
  810. {
  811. DynamicBone.Particle particle = this.m_Particles[i];
  812. DynamicBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  813. if (particle2.m_Transform.childCount <= 1)
  814. {
  815. Vector3 direction;
  816. if (particle.m_Transform != null)
  817. {
  818. direction = particle.m_Transform.localPosition;
  819. }
  820. else
  821. {
  822. direction = particle.m_EndOffset;
  823. }
  824. Vector3 toDirection = particle.m_Position - particle2.m_Position;
  825. Quaternion lhs = Quaternion.FromToRotation(particle2.m_Transform.TransformDirection(direction), toDirection);
  826. particle2.m_Transform.rotation = lhs * particle2.m_Transform.rotation;
  827. }
  828. if (particle.m_Transform != null)
  829. {
  830. particle.m_Transform.position = particle.m_Position;
  831. }
  832. }
  833. }
  834. [ReadOnly]
  835. public string m_strNowReadPhysicsFileName;
  836. [Tooltip("The root of the transform hierarchy to apply physics.")]
  837. public Transform m_Root;
  838. [Tooltip("Internal physics simulation rate.")]
  839. public float m_UpdateRate;
  840. public DynamicBone.UpdateMode m_UpdateMode = DynamicBone.UpdateMode.UnscaledTime;
  841. [Header("■減衰率 設定タイプ")]
  842. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  843. public DynamicBone.PartialMode m_EnablePartialDamping;
  844. public Dictionary<string, float> m_dicPartialDamping = new Dictionary<string, float>();
  845. [Header("減衰率")]
  846. [Tooltip("How much the bones slowed down.")]
  847. [Range(0f, 1f)]
  848. public float m_Damping = 0.5f;
  849. public AnimationCurve m_DampingDistrib;
  850. [Header("■復元率 設定タイプ")]
  851. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  852. public DynamicBone.PartialMode m_EnablePartialElasticity;
  853. public Dictionary<string, float> m_dicPartialElasticity = new Dictionary<string, float>();
  854. [Header("復元率")]
  855. [Tooltip("How much the force applied to return each bone to original orientation.")]
  856. [Range(0f, 1f)]
  857. public float m_Elasticity = 0.1f;
  858. public AnimationCurve m_ElasticityDistrib;
  859. [Header("■元の向きの影響力 設定タイプ")]
  860. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  861. public DynamicBone.PartialMode m_EnablePartialStiffness;
  862. public Dictionary<string, float> m_dicPartialStiffness = new Dictionary<string, float>();
  863. [Header("元の向きの影響力(StaticOrCurve)")]
  864. [Tooltip("How much bone's original orientation are preserved.")]
  865. [Range(0f, 1f)]
  866. public float m_Stiffness = 0.1f;
  867. public AnimationCurve m_StiffnessDistrib;
  868. [Header("■移動距離による物理無視率 設定タイプ")]
  869. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  870. public DynamicBone.PartialMode m_EnablePartialInert;
  871. public Dictionary<string, float> m_dicPartialInert = new Dictionary<string, float>();
  872. [Header("移動距離による物理無視率")]
  873. [Tooltip("How much character's position change is ignored in physics simulation.")]
  874. [Range(0f, 1f)]
  875. public float m_Inert;
  876. public AnimationCurve m_InertDistrib;
  877. [Header("■各ノードの当たり判定半径 設定タイプ")]
  878. [Tooltip("StaticOrCurve=下記の設定を使う。\nPartial=ボーン個別にToolで設定する。\nFromBoneName=旧仕様ボーン名から自動で設定する。")]
  879. public DynamicBone.PartialMode m_EnablePartialRadius;
  880. [SerializeField]
  881. public Dictionary<string, float> m_dicPartialRadius = new Dictionary<string, float>();
  882. [Header("各ノードの当たり判定半径")]
  883. [Tooltip("Each bone can be a sphere to collide with colliders. Radius describe sphere's size.")]
  884. public float m_Radius;
  885. public AnimationCurve m_RadiusDistrib;
  886. [Header("先端に余分ボーンの追加")]
  887. [Tooltip("If End Length is not zero, an extra bone is generated at the end of transform hierarchy.")]
  888. public float m_EndLength;
  889. [Tooltip("If End Offset is not zero, an extra bone is generated at the end of transform hierarchy.")]
  890. [Header("先端に余分ボーンのオフセット座標")]
  891. public Vector3 m_EndOffset = Vector3.zero;
  892. [Tooltip("力は骨に適用されます。キャラクターの初期ポーズに適用される部分的な力は相殺されます。The force apply to bones. Partial force apply to character's initial pose is cancelled out.")]
  893. public Vector3 m_Gravity = new Vector3(0f, 0f, 0f);
  894. [Header("力を加える")]
  895. [Tooltip("The force apply to bones.")]
  896. public Vector3 m_Force = new Vector3(0f, -0.01f, 0f);
  897. [Header("衝突相手ファイル")]
  898. public string m_ColliderFileName;
  899. [ReadOnly]
  900. public string m_strNowReadColliderFileName;
  901. public bool m_ViewColliderGizmo;
  902. [Header("各ノードが衝突する相手(実機では[衝突相手ファイル]により適用される)")]
  903. [Tooltip("Collider objects interact with the bones.")]
  904. public List<DynamicBoneColliderBase> m_Colliders;
  905. [Header("物理計算から特別に除外するノード(子供も動かなくなる)")]
  906. [Tooltip("Bones exclude from physics simulation.")]
  907. public List<Transform> m_Exclusions;
  908. [Tooltip("Constrain bones to move on specified plane.")]
  909. public DynamicBone.FreezeAxis m_FreezeAxis;
  910. [Header("距離による物理自動無効化")]
  911. [Tooltip("キャラクターがカメラやプレーヤーから遠い場合、物理シミュレーションを自動的に無効にします。")]
  912. public bool m_DistantDisable;
  913. public Transform m_ReferenceObject;
  914. public float m_DistanceToObject = 20f;
  915. [HideInInspector]
  916. [NonSerialized]
  917. public DynamicBonePlaneCollider m_hitFloorPlane;
  918. private Vector3 m_LocalGravity = Vector3.zero;
  919. private Vector3 m_ObjectMove = Vector3.zero;
  920. private Vector3 m_ObjectPrevPosition = Vector3.zero;
  921. private float m_BoneTotalLength;
  922. private float m_ObjectScale = 1f;
  923. private float m_Time;
  924. private float m_Weight = 1f;
  925. private bool m_DistantDisabled;
  926. private float m_fRealTimeBefore;
  927. public float[,] m_ValueFromBone = new float[,]
  928. {
  929. {
  930. 0.5f,
  931. 0.5f,
  932. 0.5f
  933. },
  934. {
  935. 0.03f,
  936. 0.03f,
  937. 0.03f
  938. },
  939. {
  940. 0.95f,
  941. 0.8f,
  942. 0.5f
  943. },
  944. {
  945. 0f,
  946. 0f,
  947. 0f
  948. },
  949. {
  950. 0.3f,
  951. 0.3f,
  952. 0.3f
  953. }
  954. };
  955. public List<DynamicBone.Particle> m_Particles = new List<DynamicBone.Particle>();
  956. private bool m_bSkipStartSetupParticles;
  957. public enum UpdateMode
  958. {
  959. Normal,
  960. AnimatePhysics,
  961. UnscaledTime
  962. }
  963. public enum PartialMode
  964. {
  965. StaticOrCurve,
  966. Partial,
  967. FromBoneName
  968. }
  969. public enum FreezeAxis
  970. {
  971. None,
  972. X,
  973. Y,
  974. Z
  975. }
  976. public enum SaveType
  977. {
  978. Damping,
  979. Elasticity,
  980. Stiffness,
  981. Inert,
  982. Radius,
  983. MAX
  984. }
  985. public class Particle
  986. {
  987. public Transform m_Transform;
  988. public int m_ParentIndex = -1;
  989. public float m_Damping;
  990. public float m_Elasticity;
  991. public float m_Stiffness;
  992. public float m_Inert;
  993. public float m_Radius;
  994. public float m_BoneLength;
  995. public Vector3 m_Position = Vector3.zero;
  996. public Vector3 m_PrevPosition = Vector3.zero;
  997. public Vector3 m_EndOffset = Vector3.zero;
  998. public Vector3 m_InitLocalPosition = Vector3.zero;
  999. public Quaternion m_InitLocalRotation = Quaternion.identity;
  1000. }
  1001. }