DynamicYureBone.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using kt.Physics;
  6. using kt.Serialization;
  7. using kt.Utility;
  8. using UnityEngine;
  9. public class DynamicYureBone : MonoBehaviour, IDynamicBone
  10. {
  11. public DynamicBoneStatus status
  12. {
  13. get
  14. {
  15. return this.Status;
  16. }
  17. }
  18. private string ColliderRootName
  19. {
  20. get
  21. {
  22. return base.name.Replace("_SM_", string.Empty) + " Collider";
  23. }
  24. }
  25. public void Init(TBody body, bool particle_setup, params Transform[] rootbones)
  26. {
  27. this.isEventExecAuto = false;
  28. foreach (Transform root_bone in rootbones)
  29. {
  30. this.AddRootBone(root_bone);
  31. }
  32. this.Body = body;
  33. this.ColliderSearchRoot = this.Body.m_trBones;
  34. this.InitLimbColiders();
  35. if (particle_setup)
  36. {
  37. this.SetupParticles();
  38. }
  39. }
  40. private void InitLimbColiders()
  41. {
  42. foreach (LimbColliderMgr.LimbType limbType in (LimbColliderMgr.LimbType[])Enum.GetValues(typeof(LimbColliderMgr.LimbType)))
  43. {
  44. DynamicYureBone.LimbColliderInfo limbColliderInfo = new DynamicYureBone.LimbColliderInfo();
  45. limbColliderInfo.isEnable = true;
  46. limbColliderInfo.limbType = limbType;
  47. limbColliderInfo.collider = this.Body.limbColliderMgr.GetCollider(limbType);
  48. this.LimbColliderInfoList.Add(limbColliderInfo);
  49. }
  50. }
  51. private void Start()
  52. {
  53. if (this.NodeDataList.Count == 0)
  54. {
  55. this.AddRootBone(base.transform);
  56. }
  57. if (!this.IsParticleSetUped && this.isEventExecAuto)
  58. {
  59. this.SetupParticles();
  60. }
  61. }
  62. public void Save(StreamWriter writer)
  63. {
  64. string value = JsonUtility.ToJson(this.Status, true);
  65. writer.Write(value);
  66. }
  67. public void SaveCollider(StreamWriter writer)
  68. {
  69. DynamicBoneColliderData dynamicBoneColliderData = new DynamicBoneColliderData();
  70. dynamicBoneColliderData.colliderStatusList = (from col in this.colliderList
  71. where col != null
  72. select col.status).ToList<NativeColliderStatus>();
  73. dynamicBoneColliderData.limbEnableList = this.LimbColliderInfoList;
  74. Dictionary<Transform, Transform> dictionary = new Dictionary<Transform, Transform>();
  75. foreach (ANativeColliderBase anativeColliderBase in this.colliderList)
  76. {
  77. Transform parent = anativeColliderBase.transform.parent;
  78. Transform parent2 = parent.parent;
  79. dictionary[parent2] = parent;
  80. anativeColliderBase.transform.SetParent(parent2, true);
  81. }
  82. string value = JsonUtility.ToJson(dynamicBoneColliderData, true);
  83. writer.Write(value);
  84. foreach (ANativeColliderBase anativeColliderBase2 in this.colliderList)
  85. {
  86. Transform parent3 = anativeColliderBase2.transform.parent;
  87. anativeColliderBase2.transform.SetParent(dictionary[parent3], true);
  88. }
  89. }
  90. public void Load(StreamReader reader)
  91. {
  92. string json = reader.ReadToEnd();
  93. this.Status = JsonUtility.FromJson<DynamicBoneStatus>(json);
  94. this.UpdateParameters();
  95. }
  96. public void LoadCollider(StreamReader reader)
  97. {
  98. if (!this.ColliderSearchRoot)
  99. {
  100. DebugUtility.Assert.Call("DynamicYureBone:コライダーの親が設定されてません");
  101. }
  102. string json = reader.ReadToEnd();
  103. DynamicBoneColliderData dynamicBoneColliderData = JsonUtility.FromJson<DynamicBoneColliderData>(json);
  104. if (this.colliderList == null)
  105. {
  106. this.colliderList = new List<ANativeColliderBase>();
  107. }
  108. else
  109. {
  110. for (int i = 0; i < this.colliderList.Count; i++)
  111. {
  112. UnityEngine.Object.DestroyImmediate(this.colliderList[i].gameObject);
  113. }
  114. this.colliderList.Clear();
  115. }
  116. List<NativeColliderStatus> colliderStatusList = dynamicBoneColliderData.colliderStatusList;
  117. for (int j = 0; j < colliderStatusList.Count; j++)
  118. {
  119. GameObject gameObject = new GameObject("Collider");
  120. NativeColliderStatus nativeColliderStatus = colliderStatusList[j];
  121. ANativeColliderBase anativeColliderBase = null;
  122. switch (nativeColliderStatus.colliderType)
  123. {
  124. case NativeColliderStatus.ColliderType.Plane:
  125. anativeColliderBase = gameObject.AddComponent<NativePlaneCollider>();
  126. break;
  127. case NativeColliderStatus.ColliderType.Capsule:
  128. anativeColliderBase = gameObject.AddComponent<NativeCapsuleCollider>();
  129. break;
  130. case NativeColliderStatus.ColliderType.Sphere:
  131. anativeColliderBase = gameObject.AddComponent<NativeSphereCollider>();
  132. break;
  133. case NativeColliderStatus.ColliderType.MaidPropCol:
  134. {
  135. NativeMaidPropCollider nativeMaidPropCollider = gameObject.AddComponent<NativeMaidPropCollider>();
  136. if (this.Body)
  137. {
  138. nativeMaidPropCollider.SetChara(this.Body.maid);
  139. }
  140. anativeColliderBase = nativeMaidPropCollider;
  141. break;
  142. }
  143. }
  144. if (anativeColliderBase)
  145. {
  146. anativeColliderBase.SetStatus(nativeColliderStatus, this.Body);
  147. this.colliderList.Add(anativeColliderBase);
  148. this.CreateColliderRoot(anativeColliderBase);
  149. }
  150. }
  151. this.LimbColliderInfoList = dynamicBoneColliderData.limbEnableList;
  152. for (int k = 0; k < this.LimbColliderInfoList.Count; k++)
  153. {
  154. LimbColliderMgr.LimbType limbType = this.LimbColliderInfoList[k].limbType;
  155. this.LimbColliderInfoList[k].collider = this.Body.limbColliderMgr.GetCollider(limbType);
  156. }
  157. }
  158. private void CreateColliderRoot(ANativeColliderBase collider)
  159. {
  160. Transform parent = collider.transform.parent;
  161. Transform transform = parent.Find(this.ColliderRootName);
  162. if (!transform)
  163. {
  164. transform = new GameObject(this.ColliderRootName).transform;
  165. transform.SetParent(parent, false);
  166. this.ColliderRoots.Add(transform);
  167. }
  168. collider.transform.SetParent(transform, true);
  169. }
  170. private void AddRootBone(Transform root_bone)
  171. {
  172. if (!this.RootBones.Contains(root_bone))
  173. {
  174. this.NodeDataList.Add(new DynamicYureBone.NodeData(root_bone));
  175. this.RootBones.Add(root_bone);
  176. }
  177. }
  178. private void SetupParticles()
  179. {
  180. this.ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  181. this.ObjectPrevPosition = base.transform.position;
  182. this.ObjectMove = Vector3.zero;
  183. this.MaxParticleCount = 0;
  184. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  185. {
  186. nodeData.particles.Clear();
  187. nodeData.defaultRotation = nodeData.rootBone.rotation;
  188. nodeData.localGravity = nodeData.rootBone.InverseTransformDirection(this.status.gravity);
  189. this.AppendParticles(nodeData, nodeData.rootBone, -1, 0f);
  190. }
  191. this.UpdateParameters();
  192. this.IsParticleSetUped = true;
  193. }
  194. private void AppendParticles(DynamicYureBone.NodeData data, Transform b, int parentIndex, float boneLength)
  195. {
  196. DynamicYureBone.Particle particle = new DynamicYureBone.Particle();
  197. particle.transform = b;
  198. particle.parentIndex = parentIndex;
  199. List<DynamicYureBone.Particle> particles = data.particles;
  200. if (b != null)
  201. {
  202. particle.position = (particle.prevPosition = b.position);
  203. particle.initLocalPosition = b.localPosition;
  204. particle.initLocalRotation = b.localRotation;
  205. }
  206. else
  207. {
  208. Transform transform = particles[parentIndex].transform;
  209. if (this.status.endLength > 0f)
  210. {
  211. Transform parent = transform.parent;
  212. if (parent != null)
  213. {
  214. particle.endOffset = transform.InverseTransformPoint(transform.position * 2f - parent.position) * this.status.endLength;
  215. }
  216. else
  217. {
  218. particle.endOffset = new Vector3(this.status.endLength, 0f, 0f);
  219. }
  220. }
  221. else
  222. {
  223. particle.endOffset = transform.InverseTransformPoint(base.transform.TransformDirection(this.status.endOffset) + transform.position);
  224. }
  225. particle.position = (particle.prevPosition = transform.TransformPoint(particle.endOffset));
  226. }
  227. if (parentIndex >= 0)
  228. {
  229. boneLength += (particles[parentIndex].transform.position - particle.position).magnitude;
  230. particle.boneLength = boneLength;
  231. data.boneTotalLength = Mathf.Max(data.boneTotalLength, boneLength);
  232. }
  233. int count = particles.Count;
  234. particles.Add(particle);
  235. this.MaxParticleCount = Mathf.Max(this.MaxParticleCount, particles.Count);
  236. if (b != null)
  237. {
  238. for (int i = 0; i < b.childCount; i++)
  239. {
  240. bool flag = false;
  241. if (this.exclusions != null)
  242. {
  243. for (int j = 0; j < this.exclusions.Count; j++)
  244. {
  245. Transform x = this.exclusions[j];
  246. if (x == b.GetChild(i))
  247. {
  248. flag = true;
  249. break;
  250. }
  251. }
  252. }
  253. if (!flag)
  254. {
  255. this.AppendParticles(data, b.GetChild(i), count, boneLength);
  256. }
  257. else if (this.status.endLength > 0f || this.status.endOffset != Vector3.zero)
  258. {
  259. this.AppendParticles(data, null, count, boneLength);
  260. }
  261. }
  262. if (b.childCount == 0 && (this.status.endLength > 0f || this.status.endOffset != Vector3.zero))
  263. {
  264. this.AppendParticles(data, null, count, boneLength);
  265. }
  266. }
  267. }
  268. public void UpdateParameters()
  269. {
  270. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  271. {
  272. nodeData.localGravity = Quaternion.Inverse(nodeData.defaultRotation) * this.status.gravity;
  273. for (int i = 0; i < nodeData.particles.Count; i++)
  274. {
  275. DynamicYureBone.Particle particle = nodeData.particles[i];
  276. particle.damping = this.status.damping;
  277. particle.elasticity = this.status.elasticity;
  278. particle.stiffness = this.status.stiffness;
  279. particle.inert = this.status.inert;
  280. particle.radius = this.status.radius;
  281. if (nodeData.boneTotalLength > 0f)
  282. {
  283. float time = particle.boneLength / nodeData.boneTotalLength;
  284. if (this.status.dampingCurve != null && this.status.dampingCurve.keys.Length > 0)
  285. {
  286. particle.damping *= this.status.dampingCurve.Evaluate(time);
  287. }
  288. if (this.status.elasticityCurve != null && this.status.elasticityCurve.keys.Length > 0)
  289. {
  290. particle.elasticity *= this.status.elasticityCurve.Evaluate(time);
  291. }
  292. if (this.status.stiffnessCurve != null && this.status.stiffnessCurve.keys.Length > 0)
  293. {
  294. particle.stiffness *= this.status.stiffnessCurve.Evaluate(time);
  295. }
  296. if (this.status.inertCurve != null && this.status.inertCurve.keys.Length > 0)
  297. {
  298. particle.inert *= this.status.inertCurve.Evaluate(time);
  299. }
  300. if (this.status.radiusCurve != null && this.status.radiusCurve.keys.Length > 0)
  301. {
  302. particle.radius *= this.status.radiusCurve.Evaluate(time);
  303. }
  304. }
  305. particle.damping = Mathf.Clamp01(particle.damping);
  306. particle.elasticity = Mathf.Clamp01(particle.elasticity);
  307. particle.stiffness = Mathf.Clamp01(particle.stiffness);
  308. particle.inert = Mathf.Clamp01(particle.inert);
  309. particle.radius = Mathf.Max(particle.radius, 0f);
  310. }
  311. }
  312. }
  313. private void OnDestroy()
  314. {
  315. this.UnInit();
  316. }
  317. public void UnInit()
  318. {
  319. for (int i = 0; i < this.ColliderRoots.Count; i++)
  320. {
  321. if (this.ColliderRoots[i])
  322. {
  323. UnityEngine.Object.DestroyImmediate(this.ColliderRoots[i].gameObject);
  324. }
  325. }
  326. this.ColliderRoots.Clear();
  327. this.colliderList.Clear();
  328. this.LimbColliderInfoList.Clear();
  329. }
  330. private void OnEnable()
  331. {
  332. this.ResetParticlesPosition();
  333. }
  334. private void OnDisable()
  335. {
  336. this.InitTransforms();
  337. }
  338. public void PhysicsReset()
  339. {
  340. this.ResetParticlesPosition();
  341. }
  342. private void ResetParticlesPosition()
  343. {
  344. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  345. {
  346. List<DynamicYureBone.Particle> particles = nodeData.particles;
  347. for (int i = 0; i < particles.Count; i++)
  348. {
  349. DynamicYureBone.Particle particle = particles[i];
  350. if (particle.transform != null)
  351. {
  352. particle.position = (particle.prevPosition = particle.transform.position);
  353. }
  354. else
  355. {
  356. Transform transform = particles[particle.parentIndex].transform;
  357. particle.position = (particle.prevPosition = transform.TransformPoint(particle.endOffset));
  358. }
  359. }
  360. }
  361. this.ObjectPrevPosition = base.transform.position;
  362. }
  363. private void FixedUpdate()
  364. {
  365. if (this.isEventExecAuto && this.updateMode == DynamicYureBone.UpdateMode.AnimatePhysics)
  366. {
  367. this.PreUpdate();
  368. }
  369. }
  370. private void Update()
  371. {
  372. if (this.isEventExecAuto && this.updateMode != DynamicYureBone.UpdateMode.AnimatePhysics)
  373. {
  374. this.PreUpdate();
  375. }
  376. }
  377. public void PreUpdate()
  378. {
  379. if (this.Weight > 0f && (!this.distantDisable || !this.DistantDisabled))
  380. {
  381. this.InitTransforms();
  382. }
  383. }
  384. private void InitTransforms()
  385. {
  386. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  387. {
  388. for (int i = 0; i < nodeData.particles.Count; i++)
  389. {
  390. DynamicYureBone.Particle particle = nodeData.particles[i];
  391. if (particle.transform != null)
  392. {
  393. particle.transform.localPosition = particle.initLocalPosition;
  394. particle.transform.localRotation = particle.initLocalRotation;
  395. }
  396. }
  397. }
  398. }
  399. private void LateUpdate()
  400. {
  401. if (!this.isEventExecAuto)
  402. {
  403. return;
  404. }
  405. this.DynamicUpdate();
  406. }
  407. public void DynamicUpdate()
  408. {
  409. if (!base.enabled)
  410. {
  411. return;
  412. }
  413. if (!this.IsParticleSetUped)
  414. {
  415. this.SetupParticles();
  416. }
  417. if (!this.isEventExecAuto)
  418. {
  419. this.PreUpdate();
  420. }
  421. if (this.distantDisable)
  422. {
  423. this.CheckDistance();
  424. }
  425. if (this.Weight > 0f && (!this.distantDisable || !this.DistantDisabled))
  426. {
  427. float t = (this.updateMode != DynamicYureBone.UpdateMode.UnscaledTime) ? Time.deltaTime : Time.unscaledDeltaTime;
  428. this.UpdateDynamicBones(t);
  429. }
  430. }
  431. private void CheckDistance()
  432. {
  433. Transform transform = this.referenceObject;
  434. if (transform == null && Camera.main != null)
  435. {
  436. transform = Camera.main.transform;
  437. }
  438. if (transform != null)
  439. {
  440. float sqrMagnitude = (transform.position - base.transform.position).sqrMagnitude;
  441. bool flag = sqrMagnitude > this.distanceToObject * this.distanceToObject;
  442. if (flag != this.DistantDisabled)
  443. {
  444. if (!flag)
  445. {
  446. this.ResetParticlesPosition();
  447. }
  448. this.DistantDisabled = flag;
  449. }
  450. }
  451. }
  452. private void UpdateDynamicBones(float t)
  453. {
  454. this.ObjectScale = Mathf.Abs(base.transform.lossyScale.x);
  455. this.ObjectMove = base.transform.position - this.ObjectPrevPosition;
  456. this.ObjectPrevPosition = base.transform.position;
  457. int num = 1;
  458. if (this.updateRate > 0f)
  459. {
  460. float num2 = 1f / this.updateRate;
  461. this.Timer += t;
  462. num = 0;
  463. while (this.Timer >= num2)
  464. {
  465. this.Timer -= num2;
  466. if (++num >= 3)
  467. {
  468. this.Timer = 0f;
  469. break;
  470. }
  471. }
  472. }
  473. for (int i = 0; i < this.MaxParticleCount; i++)
  474. {
  475. if (num > 0)
  476. {
  477. this.UpdateParticles1(i);
  478. this.UpdateParticles2(i);
  479. }
  480. else
  481. {
  482. this.SkipUpdateParticles(i);
  483. }
  484. }
  485. this.ObjectMove = Vector3.zero;
  486. this.ApplyParticlesToTransforms();
  487. }
  488. private void UpdateParticles1(int index)
  489. {
  490. Vector3 normalized = this.status.gravity.normalized;
  491. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  492. {
  493. List<DynamicYureBone.Particle> particles = nodeData.particles;
  494. if (index < particles.Count)
  495. {
  496. DynamicYureBone.Particle particle = particles[index];
  497. Vector3 vector = this.status.gravity;
  498. Vector3 lhs = nodeData.rootBone.TransformDirection(nodeData.localGravity);
  499. Vector3 b = normalized * Mathf.Max(Vector3.Dot(lhs, normalized), 0f);
  500. vector -= b;
  501. vector = (vector + this.status.force) * this.ObjectScale;
  502. if (particle.parentIndex >= 0)
  503. {
  504. Vector3 a = particle.position - particle.prevPosition;
  505. Vector3 b2 = this.ObjectMove * particle.inert;
  506. particle.prevPosition = particle.position + b2;
  507. particle.position += a * (1f - particle.damping) + vector + b2;
  508. }
  509. else
  510. {
  511. particle.prevPosition = particle.position;
  512. particle.position = particle.transform.position;
  513. }
  514. }
  515. }
  516. }
  517. private void UpdateParticles2(int index)
  518. {
  519. if (index == 0)
  520. {
  521. return;
  522. }
  523. Plane plane = default(Plane);
  524. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  525. {
  526. List<DynamicYureBone.Particle> particles = nodeData.particles;
  527. if (index < particles.Count)
  528. {
  529. DynamicYureBone.Particle particle = particles[index];
  530. DynamicYureBone.Particle particle2 = particles[particle.parentIndex];
  531. float magnitude;
  532. if (particle.transform != null)
  533. {
  534. magnitude = (particle2.transform.position - particle.transform.position).magnitude;
  535. }
  536. else
  537. {
  538. magnitude = particle2.transform.localToWorldMatrix.MultiplyVector(particle.endOffset).magnitude;
  539. }
  540. float num = Mathf.Lerp(1f, particle.stiffness, this.Weight);
  541. if (num > 0f || particle.elasticity > 0f)
  542. {
  543. Matrix4x4 localToWorldMatrix = particle2.transform.localToWorldMatrix;
  544. localToWorldMatrix.SetColumn(3, particle2.position);
  545. Vector3 a;
  546. if (particle.transform != null)
  547. {
  548. a = localToWorldMatrix.MultiplyPoint3x4(particle.transform.localPosition);
  549. }
  550. else
  551. {
  552. a = localToWorldMatrix.MultiplyPoint3x4(particle.endOffset);
  553. }
  554. Vector3 a2 = a - particle.position;
  555. particle.position += a2 * particle.elasticity;
  556. if (num > 0f)
  557. {
  558. a2 = a - particle.position;
  559. float magnitude2 = a2.magnitude;
  560. float num2 = magnitude * (1f - num) * 2f;
  561. if (magnitude2 > num2)
  562. {
  563. particle.position += a2 * ((magnitude2 - num2) / magnitude2);
  564. }
  565. }
  566. }
  567. float radius = particle.radius * this.ObjectScale;
  568. if (this.colliderList != null)
  569. {
  570. for (int i = 0; i < this.colliderList.Count; i++)
  571. {
  572. ANativeColliderBase anativeColliderBase = this.colliderList[i];
  573. if (anativeColliderBase != null && anativeColliderBase.enabled)
  574. {
  575. anativeColliderBase.Collide(ref particle.position, radius);
  576. }
  577. }
  578. }
  579. if (this.floorPlaneColider)
  580. {
  581. this.floorPlaneColider.Collide(ref particle.position, radius);
  582. }
  583. for (int j = 0; j < this.LimbColliderInfoList.Count; j++)
  584. {
  585. if (this.LimbColliderInfoList[j].isEnable)
  586. {
  587. this.LimbColliderInfoList[j].collider.Collide(ref particle.position, radius);
  588. }
  589. }
  590. if (this.status.freezeAxis != DynamicBoneStatus.FreezeAxis.None)
  591. {
  592. DynamicBoneStatus.FreezeAxis freezeAxis = this.status.freezeAxis;
  593. if (freezeAxis != DynamicBoneStatus.FreezeAxis.X)
  594. {
  595. if (freezeAxis != DynamicBoneStatus.FreezeAxis.Y)
  596. {
  597. if (freezeAxis == DynamicBoneStatus.FreezeAxis.Z)
  598. {
  599. plane.SetNormalAndPosition(particle2.transform.forward, particle2.position);
  600. }
  601. }
  602. else
  603. {
  604. plane.SetNormalAndPosition(particle2.transform.up, particle2.position);
  605. }
  606. }
  607. else
  608. {
  609. plane.SetNormalAndPosition(particle2.transform.right, particle2.position);
  610. }
  611. particle.position -= plane.normal * plane.GetDistanceToPoint(particle.position);
  612. }
  613. Vector3 a3 = particle2.position - particle.position;
  614. float magnitude3 = a3.magnitude;
  615. if (magnitude3 > 0f)
  616. {
  617. particle.position += a3 * ((magnitude3 - magnitude) / magnitude3);
  618. }
  619. }
  620. }
  621. }
  622. private static Vector3 MirrorVector(Vector3 v, Vector3 axis)
  623. {
  624. return v - axis * (Vector3.Dot(v, axis) * 2f);
  625. }
  626. private void SkipUpdateParticles(int index)
  627. {
  628. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  629. {
  630. List<DynamicYureBone.Particle> particles = nodeData.particles;
  631. if (index < particles.Count)
  632. {
  633. DynamicYureBone.Particle particle = particles[index];
  634. if (particle.parentIndex >= 0)
  635. {
  636. particle.prevPosition += this.ObjectMove;
  637. particle.position += this.ObjectMove;
  638. DynamicYureBone.Particle particle2 = particles[particle.parentIndex];
  639. float magnitude;
  640. if (particle.transform != null)
  641. {
  642. magnitude = (particle2.transform.position - particle.transform.position).magnitude;
  643. }
  644. else
  645. {
  646. magnitude = particle2.transform.localToWorldMatrix.MultiplyVector(particle.endOffset).magnitude;
  647. }
  648. float num = Mathf.Lerp(1f, particle.stiffness, this.Weight);
  649. if (num > 0f)
  650. {
  651. Matrix4x4 localToWorldMatrix = particle2.transform.localToWorldMatrix;
  652. localToWorldMatrix.SetColumn(3, particle2.position);
  653. Vector3 a;
  654. if (particle.transform != null)
  655. {
  656. a = localToWorldMatrix.MultiplyPoint3x4(particle.transform.localPosition);
  657. }
  658. else
  659. {
  660. a = localToWorldMatrix.MultiplyPoint3x4(particle.endOffset);
  661. }
  662. Vector3 a2 = a - particle.position;
  663. float magnitude2 = a2.magnitude;
  664. float num2 = magnitude * (1f - num) * 2f;
  665. if (magnitude2 > num2)
  666. {
  667. particle.position += a2 * ((magnitude2 - num2) / magnitude2);
  668. }
  669. }
  670. Vector3 a3 = particle2.position - particle.position;
  671. float magnitude3 = a3.magnitude;
  672. if (magnitude3 > 0f)
  673. {
  674. particle.position += a3 * ((magnitude3 - magnitude) / magnitude3);
  675. }
  676. }
  677. else
  678. {
  679. particle.prevPosition = particle.position;
  680. particle.position = particle.transform.position;
  681. }
  682. }
  683. }
  684. }
  685. private void ApplyParticlesToTransforms()
  686. {
  687. foreach (DynamicYureBone.NodeData nodeData in this.NodeDataList)
  688. {
  689. List<DynamicYureBone.Particle> particles = nodeData.particles;
  690. for (int i = 1; i < particles.Count; i++)
  691. {
  692. DynamicYureBone.Particle particle = particles[i];
  693. DynamicYureBone.Particle particle2 = particles[particle.parentIndex];
  694. if (particle2.transform.childCount <= 1)
  695. {
  696. Vector3 direction;
  697. if (particle.transform != null)
  698. {
  699. direction = particle.transform.localPosition;
  700. }
  701. else
  702. {
  703. direction = particle.endOffset;
  704. }
  705. Vector3 toDirection = particle.position - particle2.position;
  706. Quaternion lhs = Quaternion.FromToRotation(particle2.transform.TransformDirection(direction), toDirection);
  707. particle2.transform.rotation = lhs * particle2.transform.rotation;
  708. }
  709. if (particle.transform != null)
  710. {
  711. particle.transform.position = particle.position;
  712. }
  713. }
  714. }
  715. }
  716. public float GetWeight()
  717. {
  718. return this.Weight;
  719. }
  720. public void SetWeight(float w)
  721. {
  722. if (this.Weight != w)
  723. {
  724. if (w == 0f)
  725. {
  726. this.InitTransforms();
  727. }
  728. else if (this.Weight == 0f)
  729. {
  730. this.ResetParticlesPosition();
  731. }
  732. this.Weight = w;
  733. }
  734. }
  735. public const string STATUS_FILE_EXTENTION = "dbconf";
  736. public const string COLLIDER_FILE_EXTENTION = "dbcol";
  737. [SerializeField]
  738. private TBody Body;
  739. [SerializeField]
  740. private Transform ColliderSearchRoot;
  741. [SerializeField]
  742. [Header("揺れボーンリスト")]
  743. [ReadOnly]
  744. [Space]
  745. private List<Transform> RootBones = new List<Transform>();
  746. [Header("更新頻度。0だと現在のFPSに依存する")]
  747. [Tooltip("Internal physics simulation rate.")]
  748. public float updateRate;
  749. public DynamicYureBone.UpdateMode updateMode;
  750. [Header("適用してるパラメータ設定ファイル")]
  751. [ReadOnly]
  752. public string loadConfigFile = string.Empty;
  753. [SerializeField]
  754. private DynamicBoneStatus Status = new DynamicBoneStatus();
  755. [Header("適用してる当たり判定設定ファイル")]
  756. [ReadOnly]
  757. public string loadColliderFile = string.Empty;
  758. [SerializeField]
  759. private List<DynamicYureBone.LimbColliderInfo> LimbColliderInfoList = new List<DynamicYureBone.LimbColliderInfo>();
  760. [Tooltip("Collider objects interact with the bones.")]
  761. public List<ANativeColliderBase> colliderList;
  762. [Header("床コライダー")]
  763. public NativePlaneCollider floorPlaneColider;
  764. private List<Transform> ColliderRoots = new List<Transform>();
  765. [Header("物理計算から特別に除外するノード(子供も動かなくなる)")]
  766. [Tooltip("Bones exclude from physics simulation.")]
  767. public List<Transform> exclusions;
  768. [Header("距離による物理自動無効化")]
  769. [Tooltip("Disable physics simulation automatically if character is far from camera or player.")]
  770. public bool distantDisable;
  771. public Transform referenceObject;
  772. public float distanceToObject = 20f;
  773. private Vector3 ObjectMove = Vector3.zero;
  774. private Vector3 ObjectPrevPosition = Vector3.zero;
  775. private float ObjectScale = 1f;
  776. private float Timer;
  777. private float Weight = 1f;
  778. private bool DistantDisabled;
  779. private List<DynamicYureBone.NodeData> NodeDataList = new List<DynamicYureBone.NodeData>();
  780. [Header("自動で物理処理を更新するか")]
  781. public bool isEventExecAuto = true;
  782. private int MaxParticleCount;
  783. private bool IsParticleSetUped;
  784. [Serializable]
  785. public class LimbColliderInfo : ASerializationVersionControl
  786. {
  787. public override int FixVersion
  788. {
  789. get
  790. {
  791. return 1000;
  792. }
  793. }
  794. [ReadOnly]
  795. public LimbColliderMgr.LimbType limbType;
  796. public bool isEnable;
  797. [NonSerialized]
  798. public NativeCapsuleCollider collider;
  799. }
  800. public enum UpdateMode
  801. {
  802. Normal,
  803. AnimatePhysics,
  804. UnscaledTime
  805. }
  806. public class Particle
  807. {
  808. public Transform transform;
  809. public int parentIndex = -1;
  810. public float damping;
  811. public float elasticity;
  812. public float stiffness;
  813. public float inert;
  814. public float radius;
  815. public float boneLength;
  816. public Vector3 position = Vector3.zero;
  817. public Vector3 prevPosition = Vector3.zero;
  818. public Vector3 endOffset = Vector3.zero;
  819. public Vector3 initLocalPosition = Vector3.zero;
  820. public Quaternion initLocalRotation = Quaternion.identity;
  821. }
  822. private class NodeData
  823. {
  824. public NodeData(Transform root)
  825. {
  826. this.rootBone = root;
  827. this.defaultRotation = root.rotation;
  828. }
  829. public DynamicYureBone.Particle GetParticle(int index)
  830. {
  831. return this.particles[index];
  832. }
  833. public readonly Transform rootBone;
  834. public List<DynamicYureBone.Particle> particles = new List<DynamicYureBone.Particle>();
  835. public Vector3 localGravity;
  836. public float boneTotalLength;
  837. public Quaternion defaultRotation;
  838. }
  839. }