DynamicMuneYureBone.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DynamicMuneYureBone : MonoBehaviour, IDynamicBone
  5. {
  6. public void Init(DynamicMuneBone mune_bone)
  7. {
  8. this.m_Exclusions = new List<Transform>();
  9. this.m_Root = base.transform;
  10. this.MuneBone = mune_bone;
  11. Transform transform = null;
  12. for (int i = 0; i < base.transform.childCount; i++)
  13. {
  14. Transform child = base.transform.GetChild(i);
  15. if (!child.name.Contains("Mune_") || !child.name.Contains("_yure_"))
  16. {
  17. this.m_Exclusions.Add(child);
  18. }
  19. else
  20. {
  21. for (int j = 0; j < child.childCount; j++)
  22. {
  23. this.m_Exclusions.Add(child.GetChild(j));
  24. }
  25. float value = Mathf.Cos((float)(this.BoneYureDataDic.Count * 45) * 0.017453292f);
  26. if (this.BoneYureDataDic.Count == 0)
  27. {
  28. transform = child;
  29. }
  30. this.BoneYureDataDic[child] = new DynamicMuneYureBone.YureBoneData();
  31. this.BoneYureDataDic[child].baseWeight = Mathf.InverseLerp(-1f, 1f, value);
  32. this.BoneYureDataDic[child].muneSizeWeight = this.MuneBone.muneSizeNormalized;
  33. }
  34. }
  35. this.CurCenterPos = this.m_Root.position + this.m_Root.rotation * Vector3.left * Vector3.Dot(transform.position - this.m_Root.position, this.m_Root.rotation * Vector3.left);
  36. this.LocalCenterPos = this.m_Root.InverseTransformPoint(this.CurCenterPos);
  37. this.NotDynamicCenter = (this.LastCenterPos = this.CurCenterPos);
  38. this.LastMuneBonePos = this.MuneBone.transform.position;
  39. this.SetupParticles();
  40. base.enabled = false;
  41. }
  42. public void UnInit()
  43. {
  44. }
  45. private void SetupParticles()
  46. {
  47. this.m_Particles.Clear();
  48. if (this.m_Root == null)
  49. {
  50. return;
  51. }
  52. this.m_LocalGravity = this.m_Root.InverseTransformDirection(this.m_Gravity);
  53. this.m_ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  54. this.m_ObjectPrevPosition = base.transform.position;
  55. this.m_ObjectMove = Vector3.zero;
  56. this.m_BoneTotalLength = 0f;
  57. this.AppendParticles(this.m_Root, -1, 0f);
  58. this.UpdateParameters();
  59. }
  60. private void AppendParticles(Transform b, int parentIndex, float boneLength)
  61. {
  62. DynamicMuneYureBone.Particle particle = new DynamicMuneYureBone.Particle();
  63. particle.m_Transform = b;
  64. particle.m_ParentIndex = parentIndex;
  65. if (b != null)
  66. {
  67. particle.m_Position = (particle.m_PrevPosition = b.position);
  68. particle.m_InitLocalPosition = b.localPosition;
  69. particle.m_InitLocalRotation = b.localRotation;
  70. }
  71. else
  72. {
  73. Transform transform = this.m_Particles[parentIndex].m_Transform;
  74. if (this.m_EndLength > 0f)
  75. {
  76. Transform parent = transform.parent;
  77. if (parent != null)
  78. {
  79. particle.m_EndOffset = transform.InverseTransformPoint(transform.position * 2f - parent.position) * this.m_EndLength;
  80. }
  81. else
  82. {
  83. particle.m_EndOffset = new Vector3(this.m_EndLength, 0f, 0f);
  84. }
  85. }
  86. else
  87. {
  88. particle.m_EndOffset = transform.InverseTransformPoint(base.transform.TransformDirection(this.m_EndOffset) + transform.position);
  89. }
  90. particle.m_Position = (particle.m_PrevPosition = transform.TransformPoint(particle.m_EndOffset));
  91. }
  92. if (parentIndex >= 0)
  93. {
  94. boneLength += (this.m_Particles[parentIndex].m_Transform.position - particle.m_Position).magnitude;
  95. particle.m_BoneLength = boneLength;
  96. this.m_BoneTotalLength = Mathf.Max(this.m_BoneTotalLength, boneLength);
  97. }
  98. int count = this.m_Particles.Count;
  99. this.m_Particles.Add(particle);
  100. if (particle.m_Transform && this.BoneYureDataDic.ContainsKey(particle.m_Transform))
  101. {
  102. this.BoneYureDataDic[particle.m_Transform].particle = particle;
  103. }
  104. if (b != null)
  105. {
  106. for (int i = 0; i < b.childCount; i++)
  107. {
  108. bool flag = false;
  109. if (this.m_Exclusions != null)
  110. {
  111. for (int j = 0; j < this.m_Exclusions.Count; j++)
  112. {
  113. Transform x = this.m_Exclusions[j];
  114. if (x == b.GetChild(i))
  115. {
  116. flag = true;
  117. break;
  118. }
  119. }
  120. }
  121. if (!flag)
  122. {
  123. this.AppendParticles(b.GetChild(i), count, boneLength);
  124. }
  125. else if (this.m_EndLength > 0f || this.m_EndOffset != Vector3.zero)
  126. {
  127. this.AppendParticles(null, count, boneLength);
  128. }
  129. }
  130. if (b.childCount == 0 && (this.m_EndLength > 0f || this.m_EndOffset != Vector3.zero))
  131. {
  132. this.AppendParticles(null, count, boneLength);
  133. }
  134. }
  135. }
  136. public void UpdateParameters()
  137. {
  138. if (this.m_Root == null)
  139. {
  140. return;
  141. }
  142. this.m_LocalGravity = this.m_Root.InverseTransformDirection(this.m_Gravity);
  143. for (int i = 0; i < this.m_Particles.Count; i++)
  144. {
  145. DynamicMuneYureBone.Particle particle = this.m_Particles[i];
  146. particle.m_Damping = ((!this.BoneYureDataDic.ContainsKey(particle.m_Transform)) ? this.m_Damping : this.BoneYureDataDic[particle.m_Transform].damping);
  147. particle.m_Elasticity = this.m_Elasticity;
  148. particle.m_Stiffness = ((!this.BoneYureDataDic.ContainsKey(particle.m_Transform)) ? this.m_Stiffness : this.BoneYureDataDic[particle.m_Transform].stiffness);
  149. particle.m_Inert = ((!this.BoneYureDataDic.ContainsKey(particle.m_Transform)) ? this.m_Inert : this.BoneYureDataDic[particle.m_Transform].inert);
  150. particle.m_Radius = this.m_Radius;
  151. if (this.m_BoneTotalLength > 0f)
  152. {
  153. float time = particle.m_BoneLength / this.m_BoneTotalLength;
  154. if (this.m_DampingDistrib != null && this.m_DampingDistrib.keys.Length > 0)
  155. {
  156. particle.m_Damping *= this.m_DampingDistrib.Evaluate(time);
  157. }
  158. if (this.m_ElasticityDistrib != null && this.m_ElasticityDistrib.keys.Length > 0)
  159. {
  160. particle.m_Elasticity *= this.m_ElasticityDistrib.Evaluate(time);
  161. }
  162. if (this.m_StiffnessDistrib != null && this.m_StiffnessDistrib.keys.Length > 0)
  163. {
  164. particle.m_Stiffness *= this.m_StiffnessDistrib.Evaluate(time);
  165. }
  166. if (this.m_InertDistrib != null && this.m_InertDistrib.keys.Length > 0)
  167. {
  168. particle.m_Inert *= this.m_InertDistrib.Evaluate(time);
  169. }
  170. if (this.m_RadiusDistrib != null && this.m_RadiusDistrib.keys.Length > 0)
  171. {
  172. particle.m_Radius *= this.m_RadiusDistrib.Evaluate(time);
  173. }
  174. }
  175. particle.m_Damping = Mathf.Clamp01(particle.m_Damping);
  176. particle.m_Elasticity = Mathf.Clamp01(particle.m_Elasticity);
  177. particle.m_Stiffness = Mathf.Clamp01(particle.m_Stiffness);
  178. particle.m_Inert = Mathf.Clamp01(particle.m_Inert);
  179. particle.m_Radius = Mathf.Max(particle.m_Radius, 0f);
  180. }
  181. }
  182. public void PhysicsReset()
  183. {
  184. this.ResetParticlesPosition();
  185. }
  186. private void ResetParticlesPosition()
  187. {
  188. for (int i = 0; i < this.m_Particles.Count; i++)
  189. {
  190. DynamicMuneYureBone.Particle particle = this.m_Particles[i];
  191. if (particle.m_Transform != null)
  192. {
  193. particle.m_Position = (particle.m_PrevPosition = particle.m_Transform.position);
  194. }
  195. else
  196. {
  197. Transform transform = this.m_Particles[particle.m_ParentIndex].m_Transform;
  198. particle.m_Position = (particle.m_PrevPosition = transform.TransformPoint(particle.m_EndOffset));
  199. }
  200. }
  201. this.m_ObjectPrevPosition = base.transform.position;
  202. }
  203. public void PreUpdate()
  204. {
  205. if (this.m_Weight > 0f && (!this.m_DistantDisable || !this.m_DistantDisabled))
  206. {
  207. this.InitTransforms();
  208. }
  209. }
  210. private void InitTransforms()
  211. {
  212. for (int i = 0; i < this.m_Particles.Count; i++)
  213. {
  214. DynamicMuneYureBone.Particle particle = this.m_Particles[i];
  215. if (particle.m_Transform != null)
  216. {
  217. particle.m_Transform.localPosition = particle.m_InitLocalPosition;
  218. particle.m_Transform.localRotation = particle.m_InitLocalRotation;
  219. }
  220. }
  221. if (this.m_Root)
  222. {
  223. this.NotDynamicCenter = this.GetCenterPos();
  224. }
  225. }
  226. public void CalcMuneEditMove()
  227. {
  228. Vector3 centerPos = this.GetCenterPos();
  229. this.MuneEditMove = this.m_Root.InverseTransformVector(this.NotDynamicCenter - centerPos);
  230. }
  231. public void DynamicUpdate()
  232. {
  233. this.CurCenterPos = this.GetCenterPos();
  234. if (this.isDynamicOn)
  235. {
  236. if (!this.MuneBone.isDynamicReset)
  237. {
  238. this.m_Force = this.LastCenterPos - this.CurCenterPos;
  239. this.m_Force -= this.LastMuneBonePos - this.MuneBone.transform.position;
  240. }
  241. else
  242. {
  243. this.m_Force = Vector3.zero;
  244. }
  245. foreach (DynamicMuneYureBone.YureBoneData yureBoneData in this.BoneYureDataDic.Values)
  246. {
  247. yureBoneData.muneSizeWeight = this.MuneBone.muneSizeNormalized;
  248. }
  249. if (this.m_DistantDisable)
  250. {
  251. this.CheckDistance();
  252. }
  253. if (this.m_Weight > 0f && (!this.m_DistantDisable || !this.m_DistantDisabled))
  254. {
  255. float t = (this.m_UpdateMode != DynamicMuneYureBone.UpdateMode.UnscaledTime) ? Time.deltaTime : Time.unscaledDeltaTime;
  256. this.UpdateDynamicBones(t);
  257. }
  258. }
  259. this.LastMuneBonePos = this.MuneBone.transform.position;
  260. this.LastCenterPos = this.CurCenterPos;
  261. }
  262. private void CheckDistance()
  263. {
  264. Transform transform = this.m_ReferenceObject;
  265. if (transform == null && Camera.main != null)
  266. {
  267. transform = Camera.main.transform;
  268. }
  269. if (transform != null)
  270. {
  271. float sqrMagnitude = (transform.position - base.transform.position).sqrMagnitude;
  272. bool flag = sqrMagnitude > this.m_DistanceToObject * this.m_DistanceToObject;
  273. if (flag != this.m_DistantDisabled)
  274. {
  275. if (!flag)
  276. {
  277. this.ResetParticlesPosition();
  278. }
  279. this.m_DistantDisabled = flag;
  280. }
  281. }
  282. }
  283. private void UpdateDynamicBones(float t)
  284. {
  285. if (this.m_Root == null)
  286. {
  287. return;
  288. }
  289. this.m_ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  290. this.m_ObjectMove = base.transform.position - this.m_ObjectPrevPosition;
  291. this.m_ObjectPrevPosition = base.transform.position;
  292. int num = 1;
  293. if (this.m_UpdateRate > 0f)
  294. {
  295. float num2 = 1f / this.m_UpdateRate;
  296. this.m_Time += t;
  297. num = 0;
  298. while (this.m_Time >= num2)
  299. {
  300. this.m_Time -= num2;
  301. if (++num >= 3)
  302. {
  303. this.m_Time = 0f;
  304. break;
  305. }
  306. }
  307. }
  308. if (num > 0)
  309. {
  310. for (int i = 0; i < num; i++)
  311. {
  312. this.UpdateParticles1();
  313. this.UpdateParticles2();
  314. this.m_ObjectMove = Vector3.zero;
  315. }
  316. }
  317. else
  318. {
  319. this.SkipUpdateParticles();
  320. }
  321. this.ApplyParticlesToTransforms();
  322. }
  323. private void UpdateParticles1()
  324. {
  325. Vector3 a = this.m_Gravity;
  326. Vector3 normalized = this.m_Gravity.normalized;
  327. Vector3 lhs = this.m_Root.TransformDirection(this.m_LocalGravity);
  328. Vector3 b = normalized * Mathf.Max(Vector3.Dot(lhs, normalized), 0f);
  329. a -= b;
  330. Transform parent = this.m_Root.parent;
  331. Vector3 normalized2 = this.m_Force.normalized;
  332. Vector3 b2 = this.m_Root.TransformVector(this.MuneEditMove);
  333. for (int i = 0; i < this.m_Particles.Count; i++)
  334. {
  335. DynamicMuneYureBone.Particle particle = this.m_Particles[i];
  336. if (particle.m_ParentIndex >= 0)
  337. {
  338. Vector3 a2 = particle.m_Position - particle.m_PrevPosition;
  339. Vector3 b3 = this.m_ObjectMove * particle.m_Inert;
  340. particle.m_PrevPosition = particle.m_Position + b3;
  341. float d = (!this.BoneYureDataDic.ContainsKey(particle.m_Transform)) ? 1f : this.BoneYureDataDic[particle.m_Transform].baseWeight;
  342. Vector3 vector = (a + this.m_Force * d) * this.m_ObjectScale;
  343. if (this.BoneYureDataDic.ContainsKey(particle.m_Transform))
  344. {
  345. float f = Vector3.Dot(normalized2, (particle.m_Transform.position - this.CurCenterPos).normalized);
  346. f = Mathf.Abs(f);
  347. float num = Mathf.Acos(f) * 57.29578f;
  348. if (float.IsNaN(num))
  349. {
  350. num = 0f;
  351. }
  352. vector *= Mathf.Clamp01(num / this.RimmitAngle);
  353. vector -= b2;
  354. }
  355. particle.m_Position += a2 * (1f - particle.m_Damping) + vector + b3;
  356. }
  357. else
  358. {
  359. particle.m_PrevPosition = particle.m_Position;
  360. particle.m_Position = particle.m_Transform.position;
  361. }
  362. }
  363. }
  364. private void UpdateParticles2()
  365. {
  366. Plane plane = default(Plane);
  367. for (int i = 1; i < this.m_Particles.Count; i++)
  368. {
  369. DynamicMuneYureBone.Particle particle = this.m_Particles[i];
  370. DynamicMuneYureBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  371. float magnitude;
  372. if (particle.m_Transform != null)
  373. {
  374. magnitude = (particle2.m_Transform.position - particle.m_Transform.position).magnitude;
  375. }
  376. else
  377. {
  378. magnitude = particle2.m_Transform.localToWorldMatrix.MultiplyVector(particle.m_EndOffset).magnitude;
  379. }
  380. float num = Mathf.Lerp(1f, particle.m_Stiffness, this.m_Weight);
  381. if (num > 0f || particle.m_Elasticity > 0f)
  382. {
  383. Matrix4x4 localToWorldMatrix = particle2.m_Transform.localToWorldMatrix;
  384. localToWorldMatrix.SetColumn(3, particle2.m_Position);
  385. Vector3 a;
  386. if (particle.m_Transform != null)
  387. {
  388. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_Transform.localPosition);
  389. }
  390. else
  391. {
  392. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_EndOffset);
  393. }
  394. Vector3 a2 = a - particle.m_Position;
  395. particle.m_Position += a2 * particle.m_Elasticity;
  396. if (num > 0f)
  397. {
  398. a2 = a - particle.m_Position;
  399. float magnitude2 = a2.magnitude;
  400. float num2 = magnitude * (1f - num) * 2f;
  401. if (magnitude2 > num2)
  402. {
  403. particle.m_Position += a2 * ((magnitude2 - num2) / magnitude2);
  404. }
  405. }
  406. }
  407. if (this.m_Colliders != null)
  408. {
  409. float particleRadius = particle.m_Radius * this.m_ObjectScale;
  410. for (int j = 0; j < this.m_Colliders.Count; j++)
  411. {
  412. DynamicBoneColliderBase dynamicBoneColliderBase = this.m_Colliders[j];
  413. if (dynamicBoneColliderBase != null && dynamicBoneColliderBase.enabled)
  414. {
  415. dynamicBoneColliderBase.Collide(ref particle.m_Position, particleRadius);
  416. }
  417. }
  418. }
  419. if (this.m_FreezeAxis != DynamicMuneYureBone.FreezeAxis.None)
  420. {
  421. DynamicMuneYureBone.FreezeAxis freezeAxis = this.m_FreezeAxis;
  422. if (freezeAxis != DynamicMuneYureBone.FreezeAxis.X)
  423. {
  424. if (freezeAxis != DynamicMuneYureBone.FreezeAxis.Y)
  425. {
  426. if (freezeAxis == DynamicMuneYureBone.FreezeAxis.Z)
  427. {
  428. plane.SetNormalAndPosition(particle2.m_Transform.forward, particle2.m_Position);
  429. }
  430. }
  431. else
  432. {
  433. plane.SetNormalAndPosition(particle2.m_Transform.up, particle2.m_Position);
  434. }
  435. }
  436. else
  437. {
  438. plane.SetNormalAndPosition(particle2.m_Transform.right, particle2.m_Position);
  439. }
  440. particle.m_Position -= plane.normal * plane.GetDistanceToPoint(particle.m_Position);
  441. }
  442. if (this.BoneYureDataDic.ContainsKey(particle.m_Transform))
  443. {
  444. Vector3 vector = base.transform.rotation * Vector3.left;
  445. Vector3 b = vector * Vector3.Dot(particle.m_Position - this.CurCenterPos, vector);
  446. particle.m_Position -= b;
  447. float magnitude3 = (particle.m_Transform.position - this.CurCenterPos).magnitude;
  448. float magnitude4 = (particle.m_Position - this.CurCenterPos).magnitude;
  449. float num3 = magnitude4 / magnitude3;
  450. if (num3 > 1f + this.LengthRimmitRange)
  451. {
  452. particle.m_Position = this.CurCenterPos + (particle.m_Position - this.CurCenterPos) / num3 * (1f + this.LengthRimmitRange);
  453. }
  454. else if (num3 < 1f - this.LengthRimmitRange)
  455. {
  456. particle.m_Position = this.CurCenterPos + (particle.m_Position - this.CurCenterPos) / num3 * (1f - this.LengthRimmitRange);
  457. }
  458. }
  459. }
  460. }
  461. private void SkipUpdateParticles()
  462. {
  463. for (int i = 0; i < this.m_Particles.Count; i++)
  464. {
  465. DynamicMuneYureBone.Particle particle = this.m_Particles[i];
  466. if (particle.m_ParentIndex >= 0)
  467. {
  468. particle.m_PrevPosition += this.m_ObjectMove;
  469. particle.m_Position += this.m_ObjectMove;
  470. DynamicMuneYureBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  471. float magnitude;
  472. if (particle.m_Transform != null)
  473. {
  474. magnitude = (particle2.m_Transform.position - particle.m_Transform.position).magnitude;
  475. }
  476. else
  477. {
  478. magnitude = particle2.m_Transform.localToWorldMatrix.MultiplyVector(particle.m_EndOffset).magnitude;
  479. }
  480. float num = Mathf.Lerp(1f, particle.m_Stiffness, this.m_Weight);
  481. if (num > 0f)
  482. {
  483. Matrix4x4 localToWorldMatrix = particle2.m_Transform.localToWorldMatrix;
  484. localToWorldMatrix.SetColumn(3, particle2.m_Position);
  485. Vector3 a;
  486. if (particle.m_Transform != null)
  487. {
  488. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_Transform.localPosition);
  489. }
  490. else
  491. {
  492. a = localToWorldMatrix.MultiplyPoint3x4(particle.m_EndOffset);
  493. }
  494. Vector3 a2 = a - particle.m_Position;
  495. float magnitude2 = a2.magnitude;
  496. float num2 = magnitude * (1f - num) * 2f;
  497. if (magnitude2 > num2)
  498. {
  499. particle.m_Position += a2 * ((magnitude2 - num2) / magnitude2);
  500. }
  501. }
  502. Vector3 a3 = particle2.m_Position - particle.m_Position;
  503. float magnitude3 = a3.magnitude;
  504. if (magnitude3 > 0f)
  505. {
  506. particle.m_Position += a3 * ((magnitude3 - magnitude) / magnitude3);
  507. }
  508. }
  509. else
  510. {
  511. particle.m_PrevPosition = particle.m_Position;
  512. particle.m_Position = particle.m_Transform.position;
  513. }
  514. }
  515. }
  516. private void ApplyParticlesToTransforms()
  517. {
  518. for (int i = 1; i < this.m_Particles.Count; i++)
  519. {
  520. DynamicMuneYureBone.Particle particle = this.m_Particles[i];
  521. DynamicMuneYureBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
  522. if (particle2.m_Transform.childCount <= 1)
  523. {
  524. Vector3 direction;
  525. if (particle.m_Transform != null)
  526. {
  527. direction = particle.m_Transform.localPosition;
  528. }
  529. else
  530. {
  531. direction = particle.m_EndOffset;
  532. }
  533. Vector3 toDirection = particle.m_Position - particle2.m_Position;
  534. Quaternion lhs = Quaternion.FromToRotation(particle2.m_Transform.TransformDirection(direction), toDirection);
  535. particle2.m_Transform.rotation = lhs * particle2.m_Transform.rotation;
  536. }
  537. if (particle.m_Transform != null)
  538. {
  539. particle.m_Transform.position = particle.m_Position;
  540. }
  541. }
  542. }
  543. private static Vector3 MirrorVector(Vector3 v, Vector3 axis)
  544. {
  545. return v - axis * (Vector3.Dot(v, axis) * 2f);
  546. }
  547. public float GetWeight()
  548. {
  549. return this.m_Weight;
  550. }
  551. public void SetWeight(float w)
  552. {
  553. if (this.m_Weight != w)
  554. {
  555. if (w == 0f)
  556. {
  557. this.InitTransforms();
  558. }
  559. else if (this.m_Weight == 0f)
  560. {
  561. this.ResetParticlesPosition();
  562. }
  563. this.m_Weight = w;
  564. }
  565. }
  566. private Vector3 GetCenterPos()
  567. {
  568. return this.m_Root.TransformPoint(this.LocalCenterPos);
  569. }
  570. private const float YURE_PARAM_MIN = 0.1f;
  571. private const float YURE_PARAM_MAX = 0.9f;
  572. [Header("揺れ有効")]
  573. public bool isDynamicOn = true;
  574. [Tooltip("The root of the transform hierarchy to apply physics.")]
  575. public Transform m_Root;
  576. [Tooltip("Internal physics simulation rate.")]
  577. public float m_UpdateRate;
  578. public DynamicMuneYureBone.UpdateMode m_UpdateMode;
  579. [Tooltip("How much the bones slowed down.")]
  580. [Range(0f, 1f)]
  581. public float m_Damping = 0.1f;
  582. public AnimationCurve m_DampingDistrib;
  583. [Tooltip("How much the force applied to return each bone to original orientation.")]
  584. [Range(0f, 1f)]
  585. public float m_Elasticity = 0.1f;
  586. public AnimationCurve m_ElasticityDistrib;
  587. [Tooltip("How much bone's original orientation are preserved.")]
  588. [Range(0f, 1f)]
  589. public float m_Stiffness = 0.1f;
  590. public AnimationCurve m_StiffnessDistrib;
  591. [Tooltip("How much character's position change is ignored in physics simulation.")]
  592. [Range(0f, 1f)]
  593. public float m_Inert;
  594. public AnimationCurve m_InertDistrib;
  595. [Tooltip("Each bone can be a sphere to collide with colliders. Radius describe sphere's size.")]
  596. public float m_Radius;
  597. public AnimationCurve m_RadiusDistrib;
  598. [Tooltip("If End Length is not zero, an extra bone is generated at the end of transform hierarchy.")]
  599. public float m_EndLength;
  600. [Tooltip("If End Offset is not zero, an extra bone is generated at the end of transform hierarchy.")]
  601. public Vector3 m_EndOffset = Vector3.zero;
  602. [Tooltip("The force apply to bones. Partial force apply to character's initial pose is cancelled out.")]
  603. public Vector3 m_Gravity = Vector3.zero;
  604. [Tooltip("The force apply to bones.")]
  605. public Vector3 m_Force = Vector3.zero;
  606. [Tooltip("Collider objects interact with the bones.")]
  607. public List<DynamicBoneColliderBase> m_Colliders;
  608. [Tooltip("Bones exclude from physics simulation.")]
  609. public List<Transform> m_Exclusions;
  610. [Tooltip("Constrain bones to move on specified plane.")]
  611. public DynamicMuneYureBone.FreezeAxis m_FreezeAxis;
  612. [Tooltip("Disable physics simulation automatically if character is far from camera or player.")]
  613. public bool m_DistantDisable;
  614. public Transform m_ReferenceObject;
  615. public float m_DistanceToObject = 20f;
  616. private Vector3 m_LocalGravity = Vector3.zero;
  617. private Vector3 m_ObjectMove = Vector3.zero;
  618. private Vector3 m_ObjectPrevPosition = Vector3.zero;
  619. private float m_BoneTotalLength;
  620. private float m_ObjectScale = 1f;
  621. private float m_Time;
  622. private float m_Weight = 1f;
  623. private bool m_DistantDisabled;
  624. private List<DynamicMuneYureBone.Particle> m_Particles = new List<DynamicMuneYureBone.Particle>();
  625. private Vector3 LocalCenterPos;
  626. private Vector3 CurCenterPos;
  627. private Vector3 LastCenterPos;
  628. private Dictionary<Transform, DynamicMuneYureBone.YureBoneData> BoneYureDataDic = new Dictionary<Transform, DynamicMuneYureBone.YureBoneData>();
  629. [SerializeField]
  630. [Range(1f, 90f)]
  631. private float RimmitAngle = 90f;
  632. private DynamicMuneBone MuneBone;
  633. private Vector3 LastMuneBonePos;
  634. private Vector3 NotDynamicCenter;
  635. [SerializeField]
  636. private Vector3 MuneEditMove;
  637. [SerializeField]
  638. [Range(0f, 1f)]
  639. private float LengthRimmitRange = 0.15f;
  640. public enum UpdateMode
  641. {
  642. Normal,
  643. AnimatePhysics,
  644. UnscaledTime
  645. }
  646. public enum FreezeAxis
  647. {
  648. None,
  649. X,
  650. Y,
  651. Z
  652. }
  653. [Serializable]
  654. private class Particle
  655. {
  656. public Transform m_Transform;
  657. public int m_ParentIndex = -1;
  658. public float m_Damping;
  659. public float m_Elasticity;
  660. public float m_Stiffness;
  661. public float m_Inert;
  662. public float m_Radius;
  663. public float m_BoneLength;
  664. public Vector3 m_Position = Vector3.zero;
  665. public Vector3 m_PrevPosition = Vector3.zero;
  666. public Vector3 m_EndOffset = Vector3.zero;
  667. public Vector3 m_InitLocalPosition = Vector3.zero;
  668. public Quaternion m_InitLocalRotation = Quaternion.identity;
  669. }
  670. private class YureBoneData
  671. {
  672. public DynamicMuneYureBone.Particle particle
  673. {
  674. get
  675. {
  676. return this.Particle;
  677. }
  678. set
  679. {
  680. this.Particle = value;
  681. this.UpdateParticleParam();
  682. }
  683. }
  684. public float baseWeight
  685. {
  686. get
  687. {
  688. return this.BaseWeight;
  689. }
  690. set
  691. {
  692. this.BaseWeight = Mathf.Clamp01(value);
  693. this.UpdateParticleParam();
  694. }
  695. }
  696. public float muneSizeWeight
  697. {
  698. get
  699. {
  700. return this.MuneSizeWeight;
  701. }
  702. set
  703. {
  704. this.MuneSizeWeight = Mathf.Clamp01(value);
  705. this.UpdateParticleParam();
  706. }
  707. }
  708. public float damping
  709. {
  710. get
  711. {
  712. float b = Mathf.Lerp(0.1f, 0.9f, 1f - this.baseWeight);
  713. return Mathf.Lerp(1f, b, this.MuneSizeWeight);
  714. }
  715. }
  716. public float stiffness
  717. {
  718. get
  719. {
  720. float b = Mathf.Lerp(0.1f, 0.9f, 1f - this.baseWeight);
  721. return Mathf.Lerp(1f, b, this.MuneSizeWeight);
  722. }
  723. }
  724. public float inert
  725. {
  726. get
  727. {
  728. float b = Mathf.Lerp(0.1f, 0.9f, 1f - this.baseWeight);
  729. return Mathf.Lerp(1f, b, this.MuneSizeWeight);
  730. }
  731. }
  732. private void UpdateParticleParam()
  733. {
  734. if (this.particle != null)
  735. {
  736. this.particle.m_Stiffness = this.stiffness;
  737. this.particle.m_Damping = this.damping;
  738. this.particle.m_Inert = this.inert;
  739. }
  740. }
  741. private DynamicMuneYureBone.Particle Particle;
  742. private float BaseWeight;
  743. private float MuneSizeWeight;
  744. }
  745. }