DynamicMuneBone.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DynamicMuneBone : MonoBehaviour, IDynamicBone
  5. {
  6. public bool isLeft { get; private set; }
  7. private float MuneSize
  8. {
  9. get
  10. {
  11. return this.GetMaidPropRatio(MPN.MuneL);
  12. }
  13. }
  14. public float muneSizeNormalized
  15. {
  16. get
  17. {
  18. return this.GetMaidPropNormalized(MPN.MuneL);
  19. }
  20. }
  21. private float MuneUpDown
  22. {
  23. get
  24. {
  25. return Mathf.Lerp(30f, -30f, this.GetMaidPropNormalized(MPN.MuneUpDown)) * this.MuneSize;
  26. }
  27. }
  28. private float MuneYori
  29. {
  30. get
  31. {
  32. float num = Mathf.Lerp(12.5f, -12.5f, this.GetMaidPropNormalized(MPN.MuneYori)) * (float)((!this.isLeft) ? -1 : 1);
  33. return num * this.MuneSize;
  34. }
  35. }
  36. private float MuneDir
  37. {
  38. get
  39. {
  40. return Mathf.Lerp(-25f, 25f, this.GetMaidPropNormalized(MPN.MuneDir));
  41. }
  42. }
  43. private float MuneYawarakaNormalized
  44. {
  45. get
  46. {
  47. return this.GetMaidPropNormalized(MPN.MuneYawaraka);
  48. }
  49. }
  50. private Transform Spine
  51. {
  52. get
  53. {
  54. return base.transform.parent;
  55. }
  56. }
  57. public DynamicMuneYureBone muneYureBone
  58. {
  59. get
  60. {
  61. return this.MuneYureBone;
  62. }
  63. }
  64. public bool isDynamicReset { get; private set; }
  65. public float dynamicWeight
  66. {
  67. get
  68. {
  69. return this.DynamicWeight;
  70. }
  71. set
  72. {
  73. this.DynamicWeight = Mathf.Clamp01(value);
  74. }
  75. }
  76. private bool IsSubYureEnable
  77. {
  78. get
  79. {
  80. return this.Maid.body0.boVisible_NIP;
  81. }
  82. }
  83. public void Init(Maid maid, bool is_left)
  84. {
  85. this.Maid = maid;
  86. this.isLeft = is_left;
  87. this.SetUpMunePropValueDic(MPN.MuneL);
  88. this.SetUpMunePropValueDic(MPN.MuneYori);
  89. this.SetUpMunePropValueDic(MPN.MuneUpDown);
  90. this.SetUpMunePropValueDic(MPN.MuneDir);
  91. this.SetUpMunePropValueDic(MPN.MuneYawaraka);
  92. this.SetUpMunePropValueDic(MPN.MuneLong);
  93. this.NotDynamicRot = (this.DefaultLocalRot = base.transform.localRotation);
  94. this.MuneSub = base.transform.Find(base.transform.name + "_sub");
  95. this.NotDynamicMuneSubRot = (this.DefaultMuneSubLocalRot = this.MuneSub.localRotation);
  96. this.DynamicPos = this.GetTargetPos();
  97. this.LastMuneYawarakaVal = this.MuneYawarakaNormalized;
  98. Transform transform = this.MuneSub.Find(base.transform.name + "_scale");
  99. this.MuneYureBone = transform.gameObject.AddComponent<DynamicMuneYureBone>();
  100. this.MuneYureBone.Init(this);
  101. base.enabled = false;
  102. this.isDynamicReset = false;
  103. }
  104. private void SetUpMunePropValueDic(MPN mpn)
  105. {
  106. this.MunePropValueDic[mpn] = this.Maid.GetProp(mpn).value;
  107. }
  108. private void OnDestroy()
  109. {
  110. this.UnInit();
  111. }
  112. public void UnInit()
  113. {
  114. if (this.Maid && this.Maid.body0)
  115. {
  116. this.Maid.body0.RemoveDynamicBone(this);
  117. }
  118. this.MuneYureBone.UnInit();
  119. }
  120. public void ApplyMuneProp(MPN mpn, float val)
  121. {
  122. if (!this.MunePropValueDic.ContainsKey(mpn))
  123. {
  124. return;
  125. }
  126. this.MunePropValueDic[mpn] = Mathf.FloorToInt(val * 100f);
  127. }
  128. public void PreUpdate()
  129. {
  130. if (!base.enabled)
  131. {
  132. return;
  133. }
  134. base.transform.localRotation = this.NotDynamicRot;
  135. this.MuneSub.localRotation = this.NotDynamicMuneSubRot;
  136. Transform transform = base.transform;
  137. Vector3 one = Vector3.one;
  138. this.MuneSub.localScale = one;
  139. transform.localScale = one;
  140. this.MuneYureBone.PreUpdate();
  141. base.transform.localRotation = this.DefaultLocalRot;
  142. base.transform.Rotate(0f, this.MuneYori, this.MuneUpDown);
  143. this.NotDynamicRot = base.transform.localRotation;
  144. if (this.isDynamicOn)
  145. {
  146. float a = Mathf.Lerp(180f, 120f, this.muneSizeNormalized);
  147. float b = Mathf.Lerp(180f, 160f, Mathf.Clamp01(this.muneSizeNormalized / 0.5f));
  148. float num = Mathf.Lerp(a, b, this.GetMaidPropNormalized(MPN.MuneUpDown));
  149. num += 40f * Mathf.Abs(Vector3.Dot(this.Spine.up, Vector3.down)) * this.muneSizeNormalized;
  150. this.MuneSub.localRotation = Quaternion.Euler(Vector3.forward * (num + this.MuneDir));
  151. }
  152. else
  153. {
  154. this.MuneSub.localRotation = this.DefaultMuneSubLocalRot;
  155. }
  156. this.NotDynamicMuneSubRot = this.MuneSub.localRotation;
  157. this.MuneYureBone.CalcMuneEditMove();
  158. this.MuneYureBone.SetWeight((float)((!this.IsSubYureEnable) ? 0 : 1));
  159. }
  160. public void PhysicsReset()
  161. {
  162. this.isDynamicReset = true;
  163. this.MuneYureBone.PhysicsReset();
  164. }
  165. public void DynamicUpdate()
  166. {
  167. if (!base.enabled)
  168. {
  169. return;
  170. }
  171. if (this.isDynamicOn)
  172. {
  173. this.Stiffness = this.GetStiffness();
  174. this.Mass = this.MassLerp.GetValue(Mathf.Clamp01((this.MuneYawarakaNormalized - 0.5f) / 0.5f));
  175. this.Mass = Mathf.Lerp(1f, this.Mass, Mathf.Clamp01(this.muneSizeNormalized / 0.5f));
  176. this.Damping = this.DampingLerp.GetValue(Mathf.Clamp01((this.MuneYawarakaNormalized - 0.5f) / 0.5f));
  177. this.Damping = Mathf.Lerp(1f, this.Damping, Mathf.Clamp01(this.muneSizeNormalized / 0.5f));
  178. if (this.IsGravityVariable)
  179. {
  180. float value = Vector3.Dot(this.Spine.right, Vector3.down);
  181. float num = Vector3.Dot(this.Spine.up, Vector3.down);
  182. bool flag = num < 0f;
  183. float d = Mathf.Lerp(this.GravityYLandScape, this.GravityYFaceUpDown, Mathf.Abs(num));
  184. float a = Mathf.Sin(180f * this.GetMaidPropNormalized(MPN.MuneYori) * 0.017453292f);
  185. float num2;
  186. if (flag)
  187. {
  188. num2 = Mathf.Clamp01(this.GetMaidPropNormalized(MPN.MuneYori) / 0.5f);
  189. }
  190. else
  191. {
  192. num2 = 1f - Mathf.Clamp01(this.GetMaidPropNormalized(MPN.MuneYori) / 0.5f);
  193. }
  194. float num3 = Mathf.Lerp(a, num2, Mathf.Abs(num));
  195. this.Gravity = Vector3.Lerp(Vector3.down * this.GravityYMin, Vector3.down * d, (1f - Mathf.Clamp01(value)) * num3);
  196. if (flag)
  197. {
  198. Vector3 vector = Vector3.zero;
  199. if (this.isLeft)
  200. {
  201. vector = this.Spine.rotation * Vector3.forward;
  202. }
  203. else
  204. {
  205. vector = this.Spine.rotation * Vector3.back;
  206. }
  207. vector.y = 0f;
  208. vector *= this.GravityXSupine * Mathf.Abs(num) * num2;
  209. this.Gravity += vector;
  210. }
  211. }
  212. else
  213. {
  214. this.Gravity = Vector3.down * 0.01f;
  215. }
  216. Vector3 targetPos = this.GetTargetPos();
  217. Vector3 vector2 = (targetPos - this.DynamicPos) * this.Stiffness;
  218. vector2 += this.Gravity;
  219. Vector3 a2 = vector2 / this.Mass;
  220. this.Velocity += a2 * (1f - this.Damping);
  221. if (this.MuneYawarakaNormalized != this.LastMuneYawarakaVal || this.dynamicWeight != this.LastDynamicWeight || this.isDynamicReset)
  222. {
  223. vector2 = Vector3.zero;
  224. a2 = Vector3.zero;
  225. this.Velocity = Vector3.zero;
  226. if (this.isDynamicReset)
  227. {
  228. this.DynamicPos = targetPos;
  229. }
  230. this.isDynamicReset = true;
  231. }
  232. Vector3 a3 = this.DynamicPos + vector2 + this.Velocity;
  233. if (Vector3.Dot((a3 - base.transform.position).normalized, base.transform.rotation * Vector3.left) < Mathf.Cos(this.RimmitAngle * 0.017453292f))
  234. {
  235. vector2 *= this.RimmitOverBias;
  236. this.Velocity *= this.RimmitOverBias;
  237. }
  238. this.DynamicPos += vector2;
  239. this.DynamicPos += this.Velocity;
  240. float num4 = (this.MuneYawarakaNormalized < 0.5f) ? (this.MuneYawarakaNormalized / 0.5f) : 1f;
  241. this.DynamicPos = Vector3.Lerp(targetPos, this.DynamicPos, num4 * this.dynamicWeight * Mathf.Clamp01(this.muneSizeNormalized / 0.5f));
  242. base.transform.rotation = Quaternion.FromToRotation(base.transform.rotation * Vector3.left, this.DynamicPos - base.transform.position) * base.transform.rotation;
  243. float magnitude = (this.DynamicPos - targetPos).magnitude;
  244. float num5 = -0.2f;
  245. float num6 = 0.3f;
  246. Vector3 zero = Vector3.zero;
  247. zero.x = 1f + magnitude * num6;
  248. zero.y = 1f + -magnitude * num5;
  249. zero.z = 1f + -magnitude * num5;
  250. base.transform.localScale = zero;
  251. if (this.MuneXScaling)
  252. {
  253. Vector3 localScale = base.transform.localScale;
  254. Vector3 localScale2 = this.MuneSub.localScale;
  255. float num7 = Vector3.Dot(this.Spine.rotation * Vector3.down, Vector3.down) * this.muneSizeNormalized;
  256. float num8;
  257. float num9;
  258. float b;
  259. if (num7 > 0f)
  260. {
  261. num8 = num7 * Vector3.Dot(base.transform.rotation * Vector3.right, Vector3.down);
  262. num9 = num7 * Vector3.Dot(this.MuneSub.rotation * Vector3.left, Vector3.down);
  263. b = this.GravityXScalingSupine;
  264. }
  265. else
  266. {
  267. num7 = Mathf.Abs(num7);
  268. num8 = num7 * Vector3.Dot(base.transform.rotation * Vector3.left, Vector3.down);
  269. num9 = num7 * Vector3.Dot(this.MuneSub.rotation * Vector3.right, Vector3.down);
  270. b = this.GravityXScalingProne;
  271. }
  272. float num10 = Mathf.Sin(180f * this.GetMaidPropNormalized(MPN.MuneLong) * 0.017453292f);
  273. localScale.x *= Mathf.Lerp(1f, b, num8 * num10);
  274. localScale2.x *= Mathf.Lerp(1f, b, num9 * num10);
  275. base.transform.localScale = localScale;
  276. this.MuneSub.localScale = localScale2;
  277. }
  278. if (this.isDynamicReset)
  279. {
  280. this.isDynamicReset = false;
  281. }
  282. }
  283. else
  284. {
  285. this.isDynamicReset = true;
  286. }
  287. this.MuneYureBone.DynamicUpdate();
  288. this.LastDynamicWeight = this.dynamicWeight;
  289. this.LastMuneYawarakaVal = this.MuneYawarakaNormalized;
  290. }
  291. private float GetStiffness()
  292. {
  293. DynamicMuneBone.MathLerpPair mathLerpPair = (!this.IsSubYureEnable) ? this.StiffnessBraLerp : this.StiffnessLerp;
  294. float t = Mathf.Pow(this.muneSizeNormalized, (!this.IsSubYureEnable) ? 0.7f : 0.5f);
  295. return mathLerpPair.GetValue(t);
  296. }
  297. private Vector3 GetTargetPos()
  298. {
  299. return base.transform.position + base.transform.rotation * Vector3.left * this.TargetDistance;
  300. }
  301. private float GetMaidPropNormalized(MPN mpn)
  302. {
  303. if (!this.MunePropValueDic.ContainsKey(mpn))
  304. {
  305. Debug.LogErrorFormat("MPN:{0}は不正です。胸に関するMPNのみ指定してください", new object[]
  306. {
  307. mpn
  308. });
  309. return 0f;
  310. }
  311. int min = this.Maid.GetProp(mpn).min;
  312. int max = this.Maid.GetProp(mpn).max;
  313. return Mathf.InverseLerp((float)min, (float)max, (float)this.MunePropValueDic[mpn]);
  314. }
  315. private float GetMaidPropRatio(MPN mpn)
  316. {
  317. if (!this.MunePropValueDic.ContainsKey(mpn))
  318. {
  319. Debug.LogErrorFormat("MPN:{0}は不正です。胸に関するMPNのみ指定してください", new object[]
  320. {
  321. mpn
  322. });
  323. return 0f;
  324. }
  325. return (float)this.MunePropValueDic[mpn] / 100f;
  326. }
  327. private const float MUNE_YORI_VAL = 12.5f;
  328. private const float MUNE_UPDAWN_VAL = 30f;
  329. private const float MUNE_DIR_VAL = 25f;
  330. private const float MUNESUB_FALLDOWN_ROT = 40f;
  331. private Dictionary<MPN, int> MunePropValueDic = new Dictionary<MPN, int>();
  332. private Maid Maid;
  333. private float LastMuneYawarakaVal;
  334. private Quaternion DefaultLocalRot;
  335. private Transform MuneSub;
  336. private Quaternion DefaultMuneSubLocalRot;
  337. private Quaternion NotDynamicRot;
  338. private Quaternion NotDynamicMuneSubRot;
  339. [SerializeField]
  340. [Range(0f, 1f)]
  341. private float TargetDistance = 0.7f;
  342. private Vector3 DynamicPos;
  343. private Vector3 Velocity = Vector3.zero;
  344. [Header("剛性(揺れ幅)に関するパラメータ")]
  345. [SerializeField]
  346. private DynamicMuneBone.MathLerpPair StiffnessLerp = new DynamicMuneBone.MathLerpPair(0.22f, 0.055f);
  347. [SerializeField]
  348. private DynamicMuneBone.MathLerpPair StiffnessBraLerp = new DynamicMuneBone.MathLerpPair(0.27f, 0.07f);
  349. [SerializeField]
  350. [Range(0f, 1f)]
  351. private float Stiffness;
  352. [Header("振り子運動の質量に関するパラメータ")]
  353. [SerializeField]
  354. private DynamicMuneBone.MathLerpPair MassLerp = new DynamicMuneBone.MathLerpPair(0.4f, 1.5f);
  355. [SerializeField]
  356. [Range(0.4f, 1.5f)]
  357. private float Mass = 0.4f;
  358. [Header("減衰力(揺れを抑制する力)に関するパラメータ")]
  359. [SerializeField]
  360. private DynamicMuneBone.MathLerpPair DampingLerp = new DynamicMuneBone.MathLerpPair(0.3f, 0.01f);
  361. [SerializeField]
  362. [Range(0.01f, 1f)]
  363. private float Damping = 0.3f;
  364. [SerializeField]
  365. [Header("重量可変か")]
  366. private bool IsGravityVariable = true;
  367. [SerializeField]
  368. [Header("下にかかる重力の最小値")]
  369. [Range(0f, 0.1f)]
  370. private float GravityYMin = 0.01f;
  371. [SerializeField]
  372. [Header("体を横にしたときに下にかかる重力")]
  373. [Range(0f, 0.1f)]
  374. private float GravityYLandScape = 0.03f;
  375. [SerializeField]
  376. [Header("体を仰向けorうつ伏せにしたときに下にかかる重力")]
  377. [Range(0f, 0.1f)]
  378. private float GravityYFaceUpDown = 0.02f;
  379. [SerializeField]
  380. [Header("仰向けになったとき横方向にかかる重力の値")]
  381. [Range(0f, 0.1f)]
  382. private float GravityXSupine = 0.02f;
  383. [SerializeField]
  384. [Space]
  385. private Vector3 Gravity = Vector3.down * 0.01f;
  386. [Space]
  387. [SerializeField]
  388. [Range(0f, 90f)]
  389. private float RimmitAngle = 72f;
  390. [SerializeField]
  391. [Range(0f, 1f)]
  392. private float RimmitOverBias = 0.8f;
  393. private DynamicMuneYureBone MuneYureBone;
  394. [SerializeField]
  395. [Header("物理が有効か")]
  396. public bool isDynamicOn = true;
  397. [SerializeField]
  398. [Header("物理の適用度")]
  399. [Range(0f, 1f)]
  400. private float DynamicWeight = 1f;
  401. private float LastDynamicWeight = 1f;
  402. [Header("重力による胸潰れのパラメータ")]
  403. [SerializeField]
  404. private bool MuneXScaling = true;
  405. [SerializeField]
  406. [Range(0f, 1f)]
  407. private float GravityXScalingSupine = 0.75f;
  408. [SerializeField]
  409. [Range(1f, 2f)]
  410. private float GravityXScalingProne = 1.15f;
  411. [Serializable]
  412. private struct MathLerpPair
  413. {
  414. public MathLerpPair(float t0, float t1)
  415. {
  416. this.t0Value = t0;
  417. this.t1Value = t1;
  418. }
  419. public float GetValue(float t)
  420. {
  421. return Mathf.Lerp(this.t0Value, this.t1Value, t);
  422. }
  423. public float t0Value;
  424. public float t1Value;
  425. }
  426. }