PhysicsManagerTeamData.cs 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using Unity.Burst;
  7. using Unity.Collections;
  8. using Unity.Jobs;
  9. using Unity.Mathematics;
  10. using UnityEngine;
  11. namespace MagicaCloth
  12. {
  13. /// <summary>
  14. /// チームデータ
  15. /// チーム0はグローバルとして扱う
  16. /// </summary>
  17. public class PhysicsManagerTeamData : PhysicsManagerAccess
  18. {
  19. /// <summary>
  20. /// チームフラグビット
  21. /// </summary>
  22. public const uint Flag_Enable = 0x00000001; // 有効フラグ
  23. public const uint Flag_Interpolate = 0x00000002; // 補間処理適用
  24. public const uint Flag_FixedNonRotation = 0x00000004; // 固定パーティクルは回転させない
  25. public const uint Flag_AnimatedPose = 0x00000008; // アニメーションされた姿勢を使用する
  26. public const uint Flag_IgnoreClampPositionVelocity = 0x00000010; // ClampPositionの最大移動速度制限を無視する(Spring系)
  27. public const uint Flag_Collision = 0x00000020;
  28. public const uint Flag_AfterCollision = 0x00000040;
  29. public const uint Flag_UpdatePhysics = 0x00000080; // 更新をUnityPhysicsに合わせる
  30. public const uint Flag_Pause = 0x00000100; // 停止中
  31. //public const uint Flag_Update = 0x00000004; // 更新フラグ
  32. public const uint Flag_Reset_WorldInfluence = 0x00010000; // ワールド影響をリセットする
  33. public const uint Flag_Reset_Position = 0x00020000; // クロスパーティクル姿勢をリセット
  34. //public const uint Flag_Collision_KeepShape = 0x00040000; // 当たり判定時の初期姿勢をキープ(v1.8.8で廃止)
  35. public const uint Flag_Reset_Keep = 0x00080000; // ワールド影響を最大にしてリセットする
  36. public const uint Flag_Wind = 0x00100000; // 風の計算あり
  37. /// <summary>
  38. /// 速度変更モード
  39. /// </summary>
  40. public enum ForceMode
  41. {
  42. None,
  43. VelocityAdd, // 速度に加算(質量の影響を受ける)
  44. VelocityChange, // 速度を変更(質量の影響を受ける)
  45. VelocityAddWithoutMass = 10, // 速度に加算(質量無視)
  46. VelocityChangeWithoutMass, // 速度を変更(質量無視)
  47. }
  48. /// <summary>
  49. /// チーム状態
  50. /// </summary>
  51. public struct TeamData
  52. {
  53. /// <summary>
  54. /// チームが生成したパーティクル(コライダーパーティクルは除くので注意)
  55. /// </summary>
  56. public ChunkData particleChunk;
  57. /// <summary>
  58. /// チームが生成したコライダーパーティクルリスト
  59. /// </summary>
  60. public ChunkData colliderChunk;
  61. public ChunkData skinningBoneChunk;
  62. /// <summary>
  63. /// フラグビットデータ
  64. /// </summary>
  65. public uint flag;
  66. /// <summary>
  67. /// 動摩擦係数(0.0-1.0)
  68. /// </summary>
  69. public float dynamicFriction;
  70. /// <summary>
  71. /// 静止摩擦係数(0.0-1.0)
  72. /// </summary>
  73. public float staticFriction;
  74. /// <summary>
  75. /// セルフコリジョンの影響範囲
  76. /// </summary>
  77. public float selfCollisionRange;
  78. /// <summary>
  79. /// 自身のボーンインデックス
  80. /// </summary>
  81. public int boneIndex;
  82. /// <summary>
  83. /// 現在のチームスケール倍率
  84. /// </summary>
  85. public float3 initScale; // 自身のボーンのクロスデータ作成時のスケール長
  86. public float scaleRatio; // スケール倍率
  87. public float3 scaleDirection; // スケール値方向(xyz):(1/-1)のみ
  88. public float4 quaternionScale; // 回転フリップ用スケール
  89. /// <summary>
  90. /// チーム固有の現在時間
  91. /// </summary>
  92. public float time;
  93. /// <summary>
  94. /// チームが最後に更新処理を行った時間
  95. /// </summary>
  96. public float oldTime;
  97. /// <summary>
  98. /// チーム固有の更新時間(deltaTime)
  99. /// </summary>
  100. public float addTime;
  101. /// <summary>
  102. /// チーム固有のタイムスケール(0.0-1.0)
  103. /// </summary>
  104. public float timeScale;
  105. /// <summary>
  106. /// チーム内更新時間
  107. /// </summary>
  108. public float nowTime;
  109. /// <summary>
  110. /// チームでの最初の物理更新が実行される時間
  111. /// </summary>
  112. public float startTime;
  113. /// <summary>
  114. /// チーム更新回数
  115. /// </summary>
  116. public int updateCount;
  117. /// <summary>
  118. /// ブレンド率(0.0-1.0)
  119. /// </summary>
  120. public float blendRatio;
  121. /// <summary>
  122. /// 外力(計算結果)
  123. /// </summary>
  124. public float3 externalForce;
  125. /// <summary>
  126. /// 外力の重力影響率
  127. /// </summary>
  128. public float forceMassInfluence;
  129. /// <summary>
  130. /// 風の影響率
  131. /// </summary>
  132. public float forceWindInfluence;
  133. /// <summary>
  134. /// 風のランダム率
  135. /// </summary>
  136. public float forceWindRandomScale;
  137. /// <summary>
  138. /// 風の同期率
  139. /// </summary>
  140. public float forceWindSynchronization;
  141. /// <summary>
  142. /// 現在の速度ウエイト
  143. /// </summary>
  144. public float velocityWeight;
  145. /// <summary>
  146. /// 速度ウエイトの回復速度(s)
  147. /// </summary>
  148. public float velocityRecoverySpeed;
  149. /// <summary>
  150. /// 重力方向(単位ベクトルかつ天井方向)
  151. /// </summary>
  152. public float3 gravityDirection;
  153. public ForceMode forceMode;
  154. public float3 impactForce;
  155. /// <summary>
  156. /// 計算実行カウント
  157. /// </summary>
  158. public int calcCount;
  159. /// <summary>
  160. /// 距離拘束データへのインデックス
  161. /// </summary>
  162. public short restoreDistanceGroupIndex;
  163. public short triangleBendGroupIndex;
  164. public short clampDistanceGroupIndex;
  165. public short clampDistance2GroupIndex;
  166. public short clampPositionGroupIndex;
  167. public short clampRotationGroupIndex; // Algorithm 1
  168. public short restoreRotationGroupIndex; // Algorithm 1
  169. public short adjustRotationGroupIndex;
  170. public short springGroupIndex;
  171. public short volumeGroupIndex;
  172. public short airLineGroupIndex;
  173. public short lineWorkerGroupIndex;
  174. public short triangleWorkerGroupIndex;
  175. public short selfCollisionGroupIndex;
  176. public short edgeCollisionGroupIndex;
  177. public short penetrationGroupIndex;
  178. public short baseSkinningGroupIndex;
  179. public short twistGroupIndex;
  180. public short compositeRotationGroupIndex; // Algorithm 2
  181. /// <summary>
  182. /// データが有効か判定する
  183. /// </summary>
  184. /// <returns></returns>
  185. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  186. public bool IsActive()
  187. {
  188. return (flag & Flag_Enable) != 0;
  189. }
  190. /// <summary>
  191. /// 今回のフレームで更新があるか判定する
  192. /// </summary>
  193. /// <returns></returns>
  194. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  195. public bool IsRunning()
  196. {
  197. return updateCount > 0;
  198. }
  199. /// <summary>
  200. /// このステップを更新すべきか判定する
  201. /// </summary>
  202. /// <returns></returns>
  203. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  204. public bool IsUpdate(int runCount)
  205. {
  206. return runCount < updateCount;
  207. }
  208. /// <summary>
  209. /// 補間を行うか判定する
  210. /// </summary>
  211. /// <returns></returns>
  212. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  213. public bool IsInterpolate()
  214. {
  215. return (flag & Flag_Interpolate) != 0;
  216. }
  217. /// <summary>
  218. /// UnityPhysicsのタイミングで更新するか判定する
  219. /// </summary>
  220. /// <returns></returns>
  221. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  222. public bool IsPhysicsUpdate()
  223. {
  224. return (flag & Flag_UpdatePhysics) != 0;
  225. }
  226. /// <summary>
  227. /// フラグ判定
  228. /// </summary>
  229. /// <param name="flag"></param>
  230. /// <returns></returns>
  231. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  232. public bool IsFlag(uint flag)
  233. {
  234. return (this.flag & flag) != 0;
  235. }
  236. /// <summary>
  237. /// フラグ設定
  238. /// </summary>
  239. /// <param name="flag"></param>
  240. /// <param name="sw"></param>
  241. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  242. public void SetFlag(uint flag, bool sw)
  243. {
  244. if (sw)
  245. this.flag |= flag;
  246. else
  247. this.flag &= ~flag;
  248. }
  249. /// <summary>
  250. /// パーティクル座標リセットが必要か判定する
  251. /// </summary>
  252. /// <returns></returns>
  253. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  254. public bool IsReset()
  255. {
  256. return (flag & (Flag_Reset_WorldInfluence | Flag_Reset_Position)) != 0;
  257. }
  258. /// <summary>
  259. /// 計算停止中判定
  260. /// </summary>
  261. /// <returns></returns>
  262. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  263. public bool IsPause()
  264. {
  265. return (flag & Flag_Pause) != 0;
  266. }
  267. }
  268. /// <summary>
  269. /// チームデータリスト
  270. /// </summary>
  271. public FixedNativeList<TeamData> teamDataList;
  272. public FixedNativeList<CurveParam> teamMassList;
  273. public FixedNativeList<CurveParam> teamGravityList;
  274. public FixedNativeList<CurveParam> teamDragList;
  275. public FixedNativeList<CurveParam> teamMaxVelocityList;
  276. public FixedNativeList<CurveParam> teamDepthInfluenceList;
  277. //public FixedNativeList<CurveParam> teamDirectionalDampingList;
  278. /// <summary>
  279. /// チームのワールド移動回転影響
  280. /// </summary>
  281. public struct WorldInfluence
  282. {
  283. /// <summary>
  284. /// 影響力(0.0-1.0)
  285. /// </summary>
  286. public CurveParam moveInfluence;
  287. public CurveParam rotInfluence;
  288. public float maxMoveSpeed; // (m/s)
  289. public float maxRotationSpeed; // (deg/s)
  290. /// <summary>
  291. /// ワールド移動量
  292. /// </summary>
  293. public float3 nowPosition;
  294. public float3 oldPosition;
  295. public float3 moveOffset; // 今回の移動
  296. public float moveIgnoreRatio; // 移動を無視する割合
  297. /// <summary>
  298. /// ワールド回転量
  299. /// </summary>
  300. public quaternion nowRotation;
  301. public quaternion oldRotation;
  302. public quaternion rotationOffset; // 今回の回転
  303. public float rotationIgnoreRatio; // 回転を無視する割合
  304. /// <summary>
  305. /// テレポート
  306. /// </summary>
  307. public int resetTeleport;
  308. public float teleportDistance;
  309. public float teleportRotation;
  310. public ClothParams.TeleportMode teleportMode;
  311. /// <summary>
  312. /// リセット
  313. /// </summary>
  314. public float stabilizationTime; // 安定化時間(s)
  315. /// <summary>
  316. /// ClampRotationの設定
  317. /// 速度制限を行うため必要
  318. /// </summary>
  319. public float clampRotationLimit; // (0.0-1.0)
  320. }
  321. public FixedNativeList<WorldInfluence> teamWorldInfluenceList;
  322. /// <summary>
  323. /// 風の影響情報
  324. /// </summary>
  325. public struct WindInfo
  326. {
  327. public int windCount;
  328. public int4 windDataIndexList;
  329. public float3x4 windDirectionList;
  330. public float4 windMainList;
  331. }
  332. public FixedNativeList<WindInfo> teamWindInfoList;
  333. /// <summary>
  334. /// チームごとの判定コライダー
  335. /// </summary>
  336. public FixedMultiNativeList<int> colliderList;
  337. /// <summary>
  338. /// チームごとのスキニング用ボーンリスト
  339. /// </summary>
  340. public FixedMultiNativeList<int> skinningBoneList;
  341. /// <summary>
  342. /// チームごとのチームコンポーネント参照への辞書(キー:チームID)
  343. /// nullはグローバルチーム
  344. /// </summary>
  345. private Dictionary<int, PhysicsTeam> teamComponentDict = new Dictionary<int, PhysicsTeam>();
  346. /// <summary>
  347. /// 稼働中のチーム数
  348. /// </summary>
  349. int activeTeamCount;
  350. /// <summary>
  351. /// 各更新モードのチーム数
  352. /// </summary>
  353. int normalUpdateCount = 0;
  354. int physicsUpdateCount = 0;
  355. //=========================================================================================
  356. /// <summary>
  357. /// 初期設定
  358. /// </summary>
  359. public override void Create()
  360. {
  361. teamDataList = new FixedNativeList<TeamData>();
  362. teamMassList = new FixedNativeList<CurveParam>();
  363. teamGravityList = new FixedNativeList<CurveParam>();
  364. teamDragList = new FixedNativeList<CurveParam>();
  365. teamMaxVelocityList = new FixedNativeList<CurveParam>();
  366. teamDepthInfluenceList = new FixedNativeList<CurveParam>();
  367. teamWorldInfluenceList = new FixedNativeList<WorldInfluence>();
  368. teamWindInfoList = new FixedNativeList<WindInfo>();
  369. //teamDirectionalDampingList = new FixedNativeList<CurveParam>();
  370. colliderList = new FixedMultiNativeList<int>();
  371. skinningBoneList = new FixedMultiNativeList<int>();
  372. // グローバルチーム[0]を作成し常に有効にしておく
  373. CreateTeam(null, Flag_Enable);
  374. }
  375. /// <summary>
  376. /// 破棄
  377. /// </summary>
  378. public override void Dispose()
  379. {
  380. if (teamDataList == null)
  381. return;
  382. skinningBoneList.Dispose();
  383. colliderList.Dispose();
  384. teamMassList.Dispose();
  385. teamGravityList.Dispose();
  386. teamDragList.Dispose();
  387. teamMaxVelocityList.Dispose();
  388. teamDepthInfluenceList.Dispose();
  389. teamWorldInfluenceList.Dispose();
  390. teamWindInfoList.Dispose();
  391. //teamDirectionalDampingList.Dispose();
  392. teamDataList.Dispose();
  393. teamComponentDict.Clear();
  394. }
  395. //=========================================================================================
  396. /// <summary>
  397. /// 登録チーム数を返す
  398. /// [0]はグローバルチームなので-1する
  399. /// </summary>
  400. public int TeamCount
  401. {
  402. get
  403. {
  404. return teamDataList.Count - 1;
  405. }
  406. }
  407. /// <summary>
  408. /// チーム配列数を返す
  409. /// </summary>
  410. public int TeamLength
  411. {
  412. get
  413. {
  414. return teamDataList.Length;
  415. }
  416. }
  417. /// <summary>
  418. /// 現在活動中のチーム数を返す
  419. /// これが0の場合はチームが無いか、すべて停止中となっている
  420. /// </summary>
  421. public int ActiveTeamCount
  422. {
  423. get
  424. {
  425. return activeTeamCount;
  426. }
  427. }
  428. /// <summary>
  429. /// コライダーの数を返す
  430. /// </summary>
  431. public int ColliderCount
  432. {
  433. get
  434. {
  435. if (colliderList == null)
  436. return 0;
  437. return colliderList.Count;
  438. }
  439. }
  440. public int NormalUpdateCount
  441. {
  442. get
  443. {
  444. return normalUpdateCount;
  445. }
  446. }
  447. public int PhysicsUpdateCount
  448. {
  449. get
  450. {
  451. return physicsUpdateCount;
  452. }
  453. }
  454. public int PauseCount
  455. {
  456. get
  457. {
  458. int cnt = 0;
  459. for (int i = 0; i < teamDataList.Length; i++)
  460. {
  461. var team = teamDataList[i];
  462. if (team.IsActive() && team.IsPause())
  463. cnt++;
  464. }
  465. return cnt;
  466. }
  467. }
  468. //=========================================================================================
  469. /// <summary>
  470. /// チームを作成する
  471. /// </summary>
  472. /// <returns></returns>
  473. public int CreateTeam(PhysicsTeam team, uint flag)
  474. {
  475. var data = new TeamData();
  476. //flag |= Flag_Enable; // 作成時はActiveにしない(v1.12.0)
  477. flag |= Flag_Reset_WorldInfluence; // 移動影響リセット
  478. if (team != null)
  479. {
  480. // 更新モード設定
  481. switch (team.UpdateMode)
  482. {
  483. case PhysicsTeam.TeamUpdateMode.UnityPhysics:
  484. flag |= Flag_UpdatePhysics;
  485. break;
  486. }
  487. }
  488. data.flag = flag;
  489. data.dynamicFriction = 0;
  490. data.boneIndex = team != null ? 0 : -1; // グローバルチームはボーン無し
  491. data.initScale = 0;
  492. data.scaleDirection = 1;
  493. data.scaleRatio = 1;
  494. data.quaternionScale = 1;
  495. //data.directionalDampingBoneIndex = team != null ? 0 : -1; // グローバルチームはボーン無し
  496. //data.directionalDampingLocalDir = new float3(0, 1, 0);
  497. data.timeScale = 1.0f;
  498. data.blendRatio = 1.0f;
  499. data.forceMassInfluence = 1.0f;
  500. data.forceWindInfluence = 1.0f;
  501. data.forceWindRandomScale = 0.0f;
  502. data.gravityDirection = new float3(0, 1, 0);
  503. // 拘束チームインデックス
  504. data.restoreDistanceGroupIndex = -1;
  505. data.triangleBendGroupIndex = -1;
  506. data.clampDistanceGroupIndex = -1;
  507. data.clampDistance2GroupIndex = -1;
  508. data.clampPositionGroupIndex = -1;
  509. data.clampRotationGroupIndex = -1;
  510. data.restoreRotationGroupIndex = -1;
  511. data.adjustRotationGroupIndex = -1;
  512. data.springGroupIndex = -1;
  513. data.volumeGroupIndex = -1;
  514. data.airLineGroupIndex = -1;
  515. data.lineWorkerGroupIndex = -1;
  516. data.triangleWorkerGroupIndex = -1;
  517. data.selfCollisionGroupIndex = -1;
  518. data.edgeCollisionGroupIndex = -1;
  519. data.penetrationGroupIndex = -1;
  520. data.baseSkinningGroupIndex = -1;
  521. data.twistGroupIndex = -1;
  522. data.compositeRotationGroupIndex = -1;
  523. int teamId = teamDataList.Add(data);
  524. teamMassList.Add(new CurveParam(1.0f));
  525. teamGravityList.Add(new CurveParam());
  526. teamDragList.Add(new CurveParam());
  527. teamMaxVelocityList.Add(new CurveParam());
  528. teamDepthInfluenceList.Add(new CurveParam());
  529. //teamDirectionalDampingList.Add(new CurveParam());
  530. var worldInfluence = new WorldInfluence();
  531. if (team == null)
  532. {
  533. // グローバルチーム設定
  534. worldInfluence.moveInfluence = new CurveParam(1.0f);
  535. worldInfluence.rotInfluence = new CurveParam(1.0f);
  536. worldInfluence.maxMoveSpeed = 10000.0f;
  537. worldInfluence.maxRotationSpeed = 10000.0f;
  538. }
  539. teamWorldInfluenceList.Add(worldInfluence);
  540. teamWindInfoList.Add(new WindInfo());
  541. teamComponentDict.Add(teamId, team);
  542. //if (team != null)
  543. // activeTeamCount++;
  544. return teamId;
  545. }
  546. /// <summary>
  547. /// チームを削除する
  548. /// </summary>
  549. /// <param name="teamId"></param>
  550. public void RemoveTeam(int teamId)
  551. {
  552. if (teamId >= 0)
  553. {
  554. teamDataList.Remove(teamId);
  555. teamMassList.Remove(teamId);
  556. teamGravityList.Remove(teamId);
  557. teamDragList.Remove(teamId);
  558. teamMaxVelocityList.Remove(teamId);
  559. teamDepthInfluenceList.Remove(teamId);
  560. teamWorldInfluenceList.Remove(teamId);
  561. teamWindInfoList.Remove(teamId);
  562. //teamDirectionalDampingList.Remove(teamId);
  563. teamComponentDict.Remove(teamId);
  564. }
  565. }
  566. /// <summary>
  567. /// チームの有効フラグ切り替え
  568. /// </summary>
  569. /// <param name="teamId"></param>
  570. /// <param name="sw"></param>
  571. public void SetEnable(int teamId, bool sw)
  572. {
  573. if (teamId >= 0)
  574. {
  575. // 有効化 | 位置リセット | 移動影響リセット
  576. uint flag = Flag_Enable | Flag_Reset_Position | Flag_Reset_WorldInfluence;
  577. SetFlag(teamId, flag, sw);
  578. //SetFlag(teamId, Flag_Enable, sw);
  579. //SetFlag(teamId, Flag_Reset_Position, sw); // 位置リセット
  580. //SetFlag(teamId, Flag_Reset_WorldInfluence, sw); // 移動影響リセット
  581. }
  582. }
  583. /// <summary>
  584. /// チームが存在するか判定する
  585. /// </summary>
  586. /// <param name="teamId"></param>
  587. /// <returns></returns>
  588. public bool IsValid(int teamId)
  589. {
  590. return teamId >= 0;
  591. }
  592. /// <summary>
  593. /// チームデータが存在するか判定する
  594. /// </summary>
  595. /// <param name="teamId"></param>
  596. /// <returns></returns>
  597. public bool IsValidData(int teamId)
  598. {
  599. return teamId >= 0 && teamComponentDict.ContainsKey(teamId);
  600. }
  601. /// <summary>
  602. /// チームが有効状態か判定する
  603. /// </summary>
  604. /// <param name="teamId"></param>
  605. /// <returns></returns>
  606. public bool IsActive(int teamId)
  607. {
  608. if (teamId >= 0)
  609. return teamDataList[teamId].IsActive();
  610. else
  611. return false;
  612. }
  613. /// <summary>
  614. /// チームの状態フラグ設定
  615. /// </summary>
  616. /// <param name="teamId"></param>
  617. /// <param name="flag"></param>
  618. /// <param name="sw"></param>
  619. public void SetFlag(int teamId, uint flag, bool sw)
  620. {
  621. if (teamId < 0)
  622. return;
  623. TeamData data = teamDataList[teamId];
  624. bool oldvalid = data.IsActive();
  625. data.SetFlag(flag, sw);
  626. bool newvalid = data.IsActive();
  627. if (oldvalid != newvalid)
  628. {
  629. // アクティブチーム数カウント
  630. activeTeamCount += newvalid ? 1 : -1;
  631. }
  632. teamDataList[teamId] = data;
  633. }
  634. public bool IsFlag(int teamId, uint flag)
  635. {
  636. if (teamId < 0)
  637. return false;
  638. TeamData data = teamDataList[teamId];
  639. return data.IsFlag(flag);
  640. }
  641. public void SetParticleChunk(int teamId, ChunkData chunk)
  642. {
  643. TeamData data = teamDataList[teamId];
  644. data.particleChunk = chunk;
  645. teamDataList[teamId] = data;
  646. }
  647. /// <summary>
  648. /// チームの摩擦係数設定
  649. /// </summary>
  650. /// <param name="teamId"></param>
  651. /// <param name="dynamicFriction"></param>
  652. public void SetFriction(int teamId, float dynamicFriction, float staticFriction)
  653. {
  654. TeamData data = teamDataList[teamId];
  655. data.dynamicFriction = dynamicFriction;
  656. data.staticFriction = staticFriction;
  657. teamDataList[teamId] = data;
  658. }
  659. public void SetMass(int teamId, BezierParam mass)
  660. {
  661. teamMassList[teamId] = new CurveParam(mass);
  662. }
  663. public void SetGravity(int teamId, BezierParam gravity)
  664. {
  665. teamGravityList[teamId] = new CurveParam(gravity);
  666. }
  667. /// <summary>
  668. /// 重力(天井)方向を設定する
  669. /// </summary>
  670. /// <param name="teamId"></param>
  671. /// <param name="dir">天井方向を指定</param>
  672. public void SetGravityDirection(int teamId, float3 dir)
  673. {
  674. TeamData data = teamDataList[teamId];
  675. if (math.lengthsq(dir) >= Define.Compute.Epsilon)
  676. dir = math.normalize(dir);
  677. data.gravityDirection = dir;
  678. teamDataList[teamId] = data;
  679. }
  680. public void SetDrag(int teamId, BezierParam drag)
  681. {
  682. teamDragList[teamId] = new CurveParam(drag);
  683. }
  684. public void SetMaxVelocity(int teamId, BezierParam maxVelocity)
  685. {
  686. teamMaxVelocityList[teamId] = new CurveParam(maxVelocity);
  687. }
  688. public void SetExternalForce(int teamId, float massInfluence, float windInfluence, float windRandomScale, float windSynchronization)
  689. {
  690. TeamData data = teamDataList[teamId];
  691. data.forceMassInfluence = massInfluence;
  692. data.forceWindInfluence = windInfluence;
  693. data.forceWindRandomScale = windRandomScale;
  694. data.forceWindSynchronization = windSynchronization;
  695. teamDataList[teamId] = data;
  696. }
  697. public void SetDepthInfluence(int teamId, BezierParam depthInfluence)
  698. {
  699. teamDepthInfluenceList[teamId] = new CurveParam(depthInfluence);
  700. }
  701. /// <summary>
  702. /// ワールド移動影響設定
  703. /// </summary>
  704. /// <param name="teamId"></param>
  705. /// <param name="worldMoveInfluence"></param>
  706. public void SetWorldInfluence(int teamId, float maxSpeed, float maxRotatinSpeed, BezierParam moveInfluence, BezierParam rotInfluence,
  707. bool resetTeleport, float teleportDistance, float teleportRotation, float resetStabilizationTime, ClothParams.TeleportMode teleportMode,
  708. bool useClampRotation, BezierParam clampRotation
  709. )
  710. {
  711. var data = teamWorldInfluenceList[teamId];
  712. data.maxMoveSpeed = maxSpeed;
  713. data.maxRotationSpeed = maxRotatinSpeed;
  714. data.moveInfluence = new CurveParam(moveInfluence);
  715. data.rotInfluence = new CurveParam(rotInfluence);
  716. data.resetTeleport = resetTeleport ? 1 : 0;
  717. data.teleportDistance = teleportDistance;
  718. data.teleportRotation = teleportRotation;
  719. data.stabilizationTime = resetStabilizationTime;
  720. data.teleportMode = teleportMode;
  721. // ClampRotationによる最大移動制限値
  722. data.clampRotationLimit = CalcClampRotationLimit(useClampRotation, clampRotation);
  723. teamWorldInfluenceList[teamId] = data;
  724. }
  725. /// <summary>
  726. /// ClampRotationによる最大移動制限値を計算する
  727. /// </summary>
  728. /// <param name="useClampRotation"></param>
  729. /// <param name="clampRotation"></param>
  730. /// <returns></returns>
  731. float CalcClampRotationLimit(bool useClampRotation, BezierParam clampRotation)
  732. {
  733. var averageAngle = useClampRotation ? clampRotation.Evaluate(0.5f) : 180.0f;
  734. var clampLimit = math.pow(math.saturate(averageAngle / 90.0f), 0.5f);
  735. //Debug.Log($"ClampLimit:{clampLimit}");
  736. return clampLimit;
  737. }
  738. public void SetWorldInfluence(int teamId, float maxSpeed, float maxRotationSpeed, BezierParam moveInfluence, BezierParam rotInfluence)
  739. {
  740. var data = teamWorldInfluenceList[teamId];
  741. data.maxMoveSpeed = maxSpeed;
  742. data.maxRotationSpeed = maxRotationSpeed;
  743. data.moveInfluence = new CurveParam(moveInfluence);
  744. data.rotInfluence = new CurveParam(rotInfluence);
  745. teamWorldInfluenceList[teamId] = data;
  746. }
  747. public void SetAfterTeleport(int teamId, bool resetTeleport, float teleportDistance, float teleportRotation, ClothParams.TeleportMode teleportMode)
  748. {
  749. var data = teamWorldInfluenceList[teamId];
  750. data.resetTeleport = resetTeleport ? 1 : 0;
  751. data.teleportDistance = teleportDistance;
  752. data.teleportRotation = teleportRotation;
  753. data.teleportMode = teleportMode;
  754. teamWorldInfluenceList[teamId] = data;
  755. }
  756. public void SetStabilizationTime(int teamId, float resetStabilizationTime)
  757. {
  758. var data = teamWorldInfluenceList[teamId];
  759. data.stabilizationTime = resetStabilizationTime;
  760. teamWorldInfluenceList[teamId] = data;
  761. }
  762. public void ResetWorldInfluenceTarget(int teamId, Transform target)
  763. {
  764. float3 pos = target.position;
  765. quaternion rot = target.rotation;
  766. var data = teamWorldInfluenceList[teamId];
  767. data.nowPosition = pos;
  768. data.oldPosition = pos;
  769. data.nowRotation = rot;
  770. data.oldRotation = rot;
  771. teamWorldInfluenceList[teamId] = data;
  772. }
  773. public void SetClampRotation(int teamId, bool useClampRotation, BezierParam clampRotation)
  774. {
  775. var data = teamWorldInfluenceList[teamId];
  776. // ClampRotationによる最大移動制限値
  777. data.clampRotationLimit = CalcClampRotationLimit(useClampRotation, clampRotation);
  778. teamWorldInfluenceList[teamId] = data;
  779. }
  780. /// <summary>
  781. /// セルフコリジョンの影響範囲設定
  782. /// </summary>
  783. /// <param name="teamId"></param>
  784. /// <param name="range"></param>
  785. public void SetSelfCollisionRange(int teamId, float range)
  786. {
  787. TeamData data = teamDataList[teamId];
  788. data.selfCollisionRange = range;
  789. teamDataList[teamId] = data;
  790. }
  791. /// <summary>
  792. /// チームのボーンインデックスを設定
  793. /// </summary>
  794. /// <param name="teamId"></param>
  795. /// <param name="boneIndex"></param>
  796. public void SetBoneIndex(int teamId, int boneIndex, Vector3 initScale)
  797. {
  798. TeamData data = teamDataList[teamId];
  799. data.boneIndex = boneIndex;
  800. data.initScale = initScale;
  801. teamDataList[teamId] = data;
  802. }
  803. /// <summary>
  804. /// チームにコライダーを追加
  805. /// </summary>
  806. /// <param name="teamId"></param>
  807. /// <param name="particleIndex"></param>
  808. public void AddCollider(int teamId, int particleIndex)
  809. {
  810. //Develop.Log($"AddCollider team:{teamId} pindex:{particleIndex}");
  811. TeamData data = teamDataList[teamId];
  812. var c = data.colliderChunk;
  813. if (c.IsValid() == false)
  814. {
  815. // 新規
  816. c = colliderList.AddChunk(4);
  817. }
  818. // 追加
  819. c = colliderList.AddData(c, particleIndex);
  820. data.colliderChunk = c;
  821. teamDataList[teamId] = data;
  822. }
  823. /// <summary>
  824. /// チームからコライダーを削除
  825. /// </summary>
  826. /// <param name="teamId"></param>
  827. /// <param name="particleIndex"></param>
  828. public void RemoveCollider(int teamId, int particleIndex)
  829. {
  830. //Develop.Log($"RemoveCollider team:{teamId} pindex:{particleIndex}");
  831. TeamData data = teamDataList[teamId];
  832. var c = data.colliderChunk;
  833. if (c.IsValid())
  834. {
  835. c = colliderList.RemoveData(c, particleIndex);
  836. data.colliderChunk = c;
  837. teamDataList[teamId] = data;
  838. }
  839. }
  840. /// <summary>
  841. /// チームのコライダーをすべて削除
  842. /// </summary>
  843. /// <param name="teamId"></param>
  844. public void RemoveCollider(int teamId)
  845. {
  846. //Develop.Log($"RemoveAllCollider team:{teamId}");
  847. TeamData data = teamDataList[teamId];
  848. var c = data.colliderChunk;
  849. if (c.IsValid())
  850. {
  851. colliderList.RemoveChunk(c);
  852. data.colliderChunk = new ChunkData();
  853. teamDataList[teamId] = data;
  854. }
  855. }
  856. /// <summary>
  857. /// チームのコライダートランスフォームの未来予測をリセットする
  858. /// </summary>
  859. /// <param name="teamId"></param>
  860. public void ResetFuturePredictionCollidere(int teamId)
  861. {
  862. TeamData data = teamDataList[teamId];
  863. var c = data.colliderChunk;
  864. if (c.IsValid())
  865. {
  866. colliderList.Process(c, (pindex) =>
  867. {
  868. MagicaPhysicsManager.Instance.Particle.ResetFuturePredictionTransform(pindex);
  869. });
  870. }
  871. }
  872. /// <summary>
  873. /// チームにスキニングボーンインデックスを追加
  874. /// </summary>
  875. /// <param name="teamId"></param>
  876. /// <param name="particleIndex"></param>
  877. public void AddSkinningBoneIndex(int teamId, List<Transform> boneList)
  878. {
  879. if (boneList.Count == 0)
  880. return;
  881. TeamData data = teamDataList[teamId];
  882. var c = data.skinningBoneChunk;
  883. if (c.IsValid() == false)
  884. {
  885. // 新規
  886. c = skinningBoneList.AddChunk(boneList.Count);
  887. }
  888. // 追加
  889. foreach (var bone in boneList)
  890. {
  891. if (bone)
  892. {
  893. int boneIndex = Bone.AddBone(bone);
  894. c = skinningBoneList.AddData(c, boneIndex);
  895. }
  896. }
  897. data.skinningBoneChunk = c;
  898. teamDataList[teamId] = data;
  899. }
  900. /// <summary>
  901. /// チームのスキニングボーンインデックスをすべて削除
  902. /// </summary>
  903. /// <param name="teamId"></param>
  904. public void RemoveSkinningBoneIndex(int teamId)
  905. {
  906. TeamData data = teamDataList[teamId];
  907. var c = data.skinningBoneChunk;
  908. if (c.IsValid())
  909. {
  910. for (int i = 0, index = c.startIndex; i < c.dataLength; i++, index++)
  911. {
  912. int boneIndex = skinningBoneList[index];
  913. Bone.RemoveBone(boneIndex);
  914. }
  915. skinningBoneList.RemoveChunk(c);
  916. data.skinningBoneChunk = new ChunkData();
  917. teamDataList[teamId] = data;
  918. }
  919. }
  920. /// <summary>
  921. /// チームのタイムスケールを設定する
  922. /// </summary>
  923. /// <param name="teamId"></param>
  924. /// <param name="timeScale">0.0-1.0</param>
  925. public void SetTimeScale(int teamId, float timeScale)
  926. {
  927. TeamData data = teamDataList[teamId];
  928. data.timeScale = Mathf.Clamp01(timeScale);
  929. teamDataList[teamId] = data;
  930. }
  931. /// <summary>
  932. /// チームのタイムスケールを取得する
  933. /// </summary>
  934. /// <param name="teamId"></param>
  935. /// <returns></returns>
  936. public float GetTimeScale(int teamId)
  937. {
  938. return teamDataList[teamId].timeScale;
  939. }
  940. /// <summary>
  941. /// チームのブレンド率を設定する
  942. /// </summary>
  943. /// <param name="teamId"></param>
  944. /// <param name="blendRatio"></param>
  945. public void SetBlendRatio(int teamId, float blendRatio)
  946. {
  947. TeamData data = teamDataList[teamId];
  948. data.blendRatio = Mathf.Clamp01(blendRatio);
  949. teamDataList[teamId] = data;
  950. }
  951. /// <summary>
  952. /// チームのブレンド率を取得する
  953. /// </summary>
  954. /// <param name="teamId"></param>
  955. /// <returns></returns>
  956. public float GetBlendRatio(int teamId)
  957. {
  958. return teamDataList[teamId].blendRatio;
  959. }
  960. /// <summary>
  961. /// 外力を与える
  962. /// </summary>
  963. /// <param name="teamId"></param>
  964. /// <param name="force">1秒あたりの外力</param>
  965. public void SetImpactForce(int teamId, float3 force, ForceMode mode)
  966. {
  967. TeamData data = teamDataList[teamId];
  968. data.impactForce = force;
  969. data.forceMode = mode;
  970. teamDataList[teamId] = data;
  971. }
  972. /// <summary>
  973. /// チームのリセット後の安定化時間を設定する
  974. /// </summary>
  975. /// <param name="teamId"></param>
  976. /// <param name="resetStabilizationTime">安定時間(s).マイナスの場合はインスペクタで設定された時間</param>
  977. public void ResetStabilizationTime(int teamId, float resetStabilizationTime = -1.0f)
  978. {
  979. TeamData data = teamDataList[teamId];
  980. data.velocityWeight = 0;
  981. if (resetStabilizationTime >= 0.0f)
  982. {
  983. data.velocityRecoverySpeed = resetStabilizationTime;
  984. }
  985. else
  986. {
  987. // インスペクタで設定された値
  988. var wdata = teamWorldInfluenceList[teamId];
  989. data.velocityRecoverySpeed = wdata.stabilizationTime;
  990. }
  991. teamDataList[teamId] = data;
  992. }
  993. /// <summary>
  994. /// チームの更新モードを変更する
  995. /// </summary>
  996. /// <param name="teamId"></param>
  997. /// <param name="updateMode"></param>
  998. public void SetUpdateMode(int teamId, PhysicsTeam.TeamUpdateMode updateMode)
  999. {
  1000. //Debug.Log($"SetUpdateMode:{updateMode}");
  1001. TeamData data = teamDataList[teamId];
  1002. switch (updateMode)
  1003. {
  1004. case PhysicsTeam.TeamUpdateMode.Normal:
  1005. data.SetFlag(Flag_UpdatePhysics, false);
  1006. break;
  1007. case PhysicsTeam.TeamUpdateMode.UnityPhysics:
  1008. data.SetFlag(Flag_UpdatePhysics, true);
  1009. break;
  1010. }
  1011. teamDataList[teamId] = data;
  1012. }
  1013. /// <summary>
  1014. /// チームが参照するすべてのボーンに対してUnityPhysicsでの更新状態を変更する
  1015. /// </summary>
  1016. /// <param name="teamId"></param>
  1017. /// <param name="sw"></param>
  1018. public void ChangeUseUnityPhysics(int teamId, bool sw)
  1019. {
  1020. TeamData data = teamDataList[teamId];
  1021. // チームのボーンインデックス
  1022. if (data.boneIndex >= 0)
  1023. {
  1024. Bone.ChangeUnityPhysicsCount(data.boneIndex, sw);
  1025. }
  1026. // パーティクルと連動するボーンのUnityPhysicsフラグを切り替える
  1027. // ただしMeshClothは除く(パフォーマンスに配慮)
  1028. var team = teamComponentDict[teamId];
  1029. if (team.GetComponentType() != ComponentType.MeshCloth)
  1030. {
  1031. var chunk = data.particleChunk;
  1032. for (int i = 0; i < chunk.dataLength; i++)
  1033. {
  1034. int pindex = chunk.startIndex + i;
  1035. ChangeParticleUseUnityPhysics(pindex, sw);
  1036. }
  1037. }
  1038. // コライダーパーティクルと連動するボーンのUnityPhysicsフラグを切り替える
  1039. var chunk2 = data.colliderChunk;
  1040. for (int i = 0; i < chunk2.useLength; i++) // FixedMultiNativeListはuseLengthなので注意!
  1041. {
  1042. int pindex = colliderList[chunk2.startIndex + i];
  1043. ChangeParticleUseUnityPhysics(pindex, sw);
  1044. }
  1045. }
  1046. private void ChangeParticleUseUnityPhysics(int pindex, bool unityPhysics)
  1047. {
  1048. var flag = Particle.flagList[pindex];
  1049. if (flag.IsReadTransform()) // ※ここはIsValid()フラグを見ない(Disable中のコライダーなどを考慮)
  1050. {
  1051. // particle flag
  1052. flag.SetFlag(PhysicsManagerParticleData.Flag_Transform_UnityPhysics, unityPhysics);
  1053. Particle.flagList[pindex] = flag;
  1054. // bone UnityPhysics count
  1055. int transformIndex = Particle.transformIndexList[pindex];
  1056. if (transformIndex >= 0)
  1057. {
  1058. Bone.ChangeUnityPhysicsCount(transformIndex, unityPhysics);
  1059. }
  1060. }
  1061. }
  1062. /// <summary>
  1063. /// カリングモードに応じてボーンの計算フラグを変更する
  1064. /// </summary>
  1065. /// <param name="teamId"></param>
  1066. /// <param name="cullingMode"></param>
  1067. /// <param name="isCalculation"></param>
  1068. internal void ChangeBoneFlag(int teamId, PhysicsTeam.TeamCullingMode cullingMode, bool isCalculation)
  1069. {
  1070. TeamData data = teamDataList[teamId];
  1071. var boneFlagList = Bone.boneFlagList.ToJobArray();
  1072. var boneFlagList1 = Bone.boneFlagList.ToJobArray(1);
  1073. // 遅延実行判定
  1074. var delayed = UpdateTime.IsDelay && boneFlagList1.IsCreated && boneFlagList1.Length == boneFlagList.Length;
  1075. // 設定フラグ構築
  1076. byte flag = 0;
  1077. switch (cullingMode)
  1078. {
  1079. case PhysicsTeam.TeamCullingMode.Off:
  1080. flag |= PhysicsManagerBoneData.Flag_Restore;
  1081. flag |= PhysicsManagerBoneData.Flag_Write;
  1082. delayed = false;
  1083. break;
  1084. case PhysicsTeam.TeamCullingMode.Reset:
  1085. flag |= PhysicsManagerBoneData.Flag_Restore;
  1086. if (isCalculation)
  1087. flag |= PhysicsManagerBoneData.Flag_Write;
  1088. delayed = false;
  1089. break;
  1090. case PhysicsTeam.TeamCullingMode.Pause:
  1091. if (isCalculation)
  1092. {
  1093. flag |= PhysicsManagerBoneData.Flag_Restore;
  1094. flag |= PhysicsManagerBoneData.Flag_Write;
  1095. }
  1096. if (data.calcCount < 4) // ※エディタ環境の問題なのか4程度必要!
  1097. {
  1098. // 計算されていない場合はダブルバッファへの書き込みをスキップする
  1099. delayed = false;
  1100. }
  1101. break;
  1102. }
  1103. var c = data.particleChunk;
  1104. for (int i = 0, pindex = c.startIndex; i < c.dataLength; i++, pindex++)
  1105. {
  1106. var boneIndex = Particle.transformIndexList[pindex];
  1107. if (boneIndex >= 0)
  1108. {
  1109. var bflag = boneFlagList[boneIndex];
  1110. bflag = (byte)(bflag & 0xf); // 上位4bitをクリア
  1111. bflag |= flag;
  1112. boneFlagList[boneIndex] = bflag;
  1113. // 遅延実行用ダブルバッファにも書き込み
  1114. if (delayed)
  1115. boneFlagList1[boneIndex] = bflag;
  1116. }
  1117. }
  1118. }
  1119. //=========================================================================================
  1120. /// <summary>
  1121. /// フレーム開始時に行うチーム更新
  1122. /// アクティブ状態に限らず行う(メインスレッド)
  1123. /// </summary>
  1124. internal void EarlyUpdateTeamAlways()
  1125. {
  1126. foreach (var team in teamComponentDict.Values)
  1127. {
  1128. // BoneCloth/BoneSpringの表示判定
  1129. if (team != null && team.isActiveAndEnabled && team.CullingMode != PhysicsTeam.TeamCullingMode.Off)
  1130. {
  1131. switch (team.GetComponentType())
  1132. {
  1133. case ComponentType.BoneCloth:
  1134. case ComponentType.BoneSpring:
  1135. team.UpdateCullingMode(team);
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. //=========================================================================================
  1142. /// <summary>
  1143. /// シミュレーション計算前に行うチーム更新
  1144. /// アクティブ状態に限らず行う(メインスレッド)
  1145. /// </summary>
  1146. internal void PreUpdateTeamAlways()
  1147. {
  1148. normalUpdateCount = 0;
  1149. physicsUpdateCount = 0;
  1150. // マネージャー停止中ならばこれ以降は処理しない(v1.12.0)
  1151. if (manager.IsActive == false)
  1152. return;
  1153. var mainCamera = Camera.main != null ? Camera.main.transform : manager.transform;
  1154. foreach (var team in teamComponentDict.Values)
  1155. {
  1156. var baseCloth = team as BaseCloth;
  1157. if (baseCloth == null)
  1158. continue;
  1159. // 距離ブレンド率更新
  1160. float blend = 1.0f;
  1161. if (baseCloth.Params.UseDistanceDisable)
  1162. {
  1163. var refObject = baseCloth.Params.DisableReferenceObject;
  1164. if (refObject == null)
  1165. refObject = mainCamera;
  1166. float dist = Vector3.Distance(team.transform.position, refObject.position);
  1167. float disableDist = baseCloth.Params.DisableDistance;
  1168. float fadeDist = Mathf.Max(disableDist - (baseCloth.Params.DisableFadeDistance + Define.Compute.Epsilon/*FadeDistance=0の対処*/), 0.0f);
  1169. blend = Mathf.InverseLerp(disableDist, fadeDist, dist);
  1170. }
  1171. baseCloth.Setup.DistanceBlendRatio = blend;
  1172. // ブレンド率更新
  1173. baseCloth.UpdateBlend();
  1174. // 各更新モード別のチーム数
  1175. if (baseCloth.Status.IsActive)
  1176. {
  1177. switch (baseCloth.UpdateMode)
  1178. {
  1179. case PhysicsTeam.TeamUpdateMode.Normal:
  1180. normalUpdateCount++;
  1181. break;
  1182. case PhysicsTeam.TeamUpdateMode.UnityPhysics:
  1183. physicsUpdateCount++;
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. }
  1189. /// <summary>
  1190. /// 今回フレームの最大実効回数を求める
  1191. /// </summary>
  1192. /// <param name="ups"></param>
  1193. /// <returns></returns>
  1194. internal int CalcMaxUpdateCount(int ups, float deltaTime, float physicsDeltaTime, float updateDeltaTime)
  1195. {
  1196. // マネージャの固定更新判定
  1197. bool once = manager.UpdateTime.GetUpdateMode() == UpdateTimeManager.UpdateMode.OncePerFrame;
  1198. // 全チームの最大実行回数を求める
  1199. float globalTimeScale = manager.GetGlobalTimeScale();
  1200. int maxcnt = 0;
  1201. foreach (var kv in teamComponentDict)
  1202. {
  1203. if (kv.Value == null)
  1204. continue;
  1205. int tid = kv.Key;
  1206. if (tid <= 0)
  1207. continue;
  1208. var tdata = teamDataList[tid];
  1209. bool updatePhysics = tdata.IsPhysicsUpdate();
  1210. int cnt = 0;
  1211. if (once && updatePhysics == false)
  1212. {
  1213. // 固定更新は1回(ただしUnityPhysics更新は除く)
  1214. cnt = 1;
  1215. }
  1216. else
  1217. {
  1218. float timeScale = tdata.timeScale * globalTimeScale;
  1219. timeScale = tdata.IsPause() ? 0.0f : timeScale; // 一時停止
  1220. float addTime = (updatePhysics ? physicsDeltaTime : deltaTime) * timeScale;
  1221. float nowTime = tdata.nowTime + addTime;
  1222. cnt = (int)(nowTime / updateDeltaTime);
  1223. }
  1224. // リセットフラグがONならば最低1回更新とする(ただしUnityPhysics更新は除く)
  1225. if (tdata.IsReset() && updatePhysics == false)
  1226. cnt = Mathf.Max(cnt, 1);
  1227. maxcnt = Mathf.Max(maxcnt, cnt);
  1228. }
  1229. // upsに関係なく1フレームの最大回数は4回
  1230. maxcnt = Mathf.Min(maxcnt, 4);
  1231. return maxcnt;
  1232. }
  1233. //=========================================================================================
  1234. internal void PreUpdateTeamData(float deltaTime, float physicsDeltaTime, float updateDeltaTime, int ups, int maxUpdateCount)
  1235. {
  1236. bool unscaledUpdate = manager.UpdateTime.IsUnscaledUpdate;
  1237. float globalTimeScale = manager.GetGlobalTimeScale();
  1238. // 固定更新では1回の更新時間をupdateDeltaTimeに設定する
  1239. if (unscaledUpdate == false)
  1240. deltaTime = updateDeltaTime;
  1241. // チームデータ前処理
  1242. var job = new PreProcessTeamDataJob()
  1243. {
  1244. //time = Time.time,
  1245. deltaTime = deltaTime,
  1246. physicsDeltaTime = physicsDeltaTime,
  1247. updateDeltaTime = updateDeltaTime,
  1248. globalTimeScale = globalTimeScale,
  1249. //unscaledUpdate = unscaledUpdate,
  1250. //ups = ups,
  1251. maxUpdateCount = maxUpdateCount,
  1252. unityTimeScale = Time.timeScale,
  1253. elapsedTime = Time.time,
  1254. teamData = teamDataList.ToJobArray(),
  1255. teamWorldInfluenceList = teamWorldInfluenceList.ToJobArray(),
  1256. teamWindInfoList = teamWindInfoList.ToJobArray(),
  1257. bonePosList = Bone.bonePosList.ToJobArray(),
  1258. boneRotList = Bone.boneRotList.ToJobArray(),
  1259. boneSclList = Bone.boneSclList.ToJobArray(),
  1260. windCount = Wind.windDataList.Count,
  1261. windData = Wind.windDataList.ToJobArray(),
  1262. //directionalWindId = Wind.DirectionalWindId,
  1263. };
  1264. Compute.MasterJob = job.Schedule(teamDataList.Length, 2, Compute.MasterJob);
  1265. }
  1266. [BurstCompile]
  1267. struct PreProcessTeamDataJob : IJobParallelFor
  1268. {
  1269. //public float time;
  1270. public float deltaTime;
  1271. public float physicsDeltaTime;
  1272. public float updateDeltaTime;
  1273. public float globalTimeScale;
  1274. //public bool unscaledUpdate;
  1275. //public int ups;
  1276. public int maxUpdateCount;
  1277. public float unityTimeScale;
  1278. public float elapsedTime;
  1279. // team
  1280. public NativeArray<TeamData> teamData;
  1281. public NativeArray<WorldInfluence> teamWorldInfluenceList;
  1282. public NativeArray<WindInfo> teamWindInfoList;
  1283. [Unity.Collections.ReadOnly]
  1284. public NativeArray<float3> bonePosList;
  1285. [Unity.Collections.ReadOnly]
  1286. public NativeArray<quaternion> boneRotList;
  1287. [Unity.Collections.ReadOnly]
  1288. public NativeArray<float3> boneSclList;
  1289. // wind
  1290. public int windCount;
  1291. [Unity.Collections.ReadOnly]
  1292. public NativeArray<PhysicsManagerWindData.WindData> windData;
  1293. //[Unity.Collections.ReadOnly]
  1294. //public int directionalWindId;
  1295. // チームデータごと
  1296. public void Execute(int teamId)
  1297. {
  1298. var tdata = teamData[teamId];
  1299. // グローバルチーム判定
  1300. bool isGlobal = teamId == 0;
  1301. if (tdata.IsActive() == false || (isGlobal == false && tdata.boneIndex < 0))
  1302. {
  1303. tdata.updateCount = 0;
  1304. teamData[teamId] = tdata;
  1305. return;
  1306. }
  1307. // このチームの今回のdeltaTime
  1308. float dtime = tdata.IsPhysicsUpdate() ? physicsDeltaTime : deltaTime;
  1309. if (isGlobal)
  1310. {
  1311. // グローバルチーム
  1312. // 時間更新(タイムスケール対応)
  1313. UpdateTime(ref tdata, false, dtime);
  1314. }
  1315. else
  1316. {
  1317. // チームボーン情報
  1318. var bpos = bonePosList[tdata.boneIndex];
  1319. var brot = boneRotList[tdata.boneIndex];
  1320. var bscl = boneSclList[tdata.boneIndex];
  1321. // チームスケール倍率算出
  1322. if (tdata.initScale.x > 0.0f)
  1323. {
  1324. tdata.scaleRatio = math.length(bscl) / math.length(tdata.initScale);
  1325. }
  1326. // マイナススケール対応
  1327. tdata.scaleDirection = math.sign(bscl);
  1328. if (bscl.x < 0 || bscl.y < 0 || bscl.z < 0)
  1329. tdata.quaternionScale = new float4(-math.sign(bscl), 1);
  1330. else
  1331. tdata.quaternionScale = 1;
  1332. // ワールド移動影響
  1333. WorldInfluence wdata = teamWorldInfluenceList[teamId];
  1334. // 移動量算出
  1335. float3 moveVector = bpos - wdata.oldPosition;
  1336. quaternion moveRot = MathUtility.FromToRotation(wdata.oldRotation, brot);
  1337. var moveLen = math.length(moveVector); // 移動距離
  1338. var moveAng = math.degrees(MathUtility.Angle(wdata.oldRotation, brot)); // 移動角度
  1339. var maxMoveSpeed = wdata.maxMoveSpeed; // 最大移動速度(m/s)
  1340. var maxRotationSpeed = wdata.maxRotationSpeed; // 最大回転角度(deg/s)
  1341. // ClampRotationによるワールド移動影響補正(v1.11.2)
  1342. // あまり強い移動影響が掛かると制限を超えて曲がってしまうため、それを抑え安定化させる
  1343. // 角度制限がきついほど移動影響も低く抑えられる
  1344. {
  1345. maxMoveSpeed *= wdata.clampRotationLimit;
  1346. //maxRotationSpeed *= wdata.clampRotationLimit;
  1347. maxRotationSpeed *= math.lerp(0.5f, 1.0f, wdata.clampRotationLimit);
  1348. }
  1349. // テレポート判定
  1350. if (wdata.resetTeleport == 1)
  1351. {
  1352. // テレポート距離はチームスケールを乗算する
  1353. if (moveLen >= wdata.teleportDistance * tdata.scaleRatio || moveAng >= wdata.teleportRotation)
  1354. {
  1355. // テレポートモードにより処理を分岐
  1356. switch (wdata.teleportMode)
  1357. {
  1358. case ClothParams.TeleportMode.Reset:
  1359. tdata.SetFlag(Flag_Reset_WorldInfluence, true);
  1360. tdata.SetFlag(Flag_Reset_Position, true);
  1361. break;
  1362. case ClothParams.TeleportMode.Keep:
  1363. tdata.SetFlag(Flag_Reset_Keep, true);
  1364. break;
  1365. }
  1366. }
  1367. }
  1368. // 移動影響
  1369. if (tdata.IsFlag(Flag_Reset_Keep))
  1370. {
  1371. // 移動影響を最大にするテレポート
  1372. wdata.moveOffset = moveVector;
  1373. wdata.moveIgnoreRatio = 0;
  1374. }
  1375. else if (moveLen > 1e-06f && dtime > 1e-06f)
  1376. {
  1377. // 最大移動
  1378. float speed = moveLen / dtime;
  1379. float ratio = math.max(speed - maxMoveSpeed, 0.0f) / speed;
  1380. wdata.moveIgnoreRatio = ratio;
  1381. wdata.moveOffset = moveVector;
  1382. }
  1383. else
  1384. {
  1385. wdata.moveOffset = 0;
  1386. wdata.moveIgnoreRatio = 0;
  1387. }
  1388. // 回転影響
  1389. if (tdata.IsFlag(Flag_Reset_Keep))
  1390. {
  1391. wdata.rotationOffset = moveRot;
  1392. wdata.rotationIgnoreRatio = 0;
  1393. }
  1394. if (moveAng > 1e-06f && dtime > 1e-06f)
  1395. {
  1396. // 最大回転
  1397. float speed = moveAng / dtime;
  1398. float ratio = math.max(speed - maxRotationSpeed, 0.0f) / speed;
  1399. wdata.rotationOffset = moveRot;
  1400. wdata.rotationIgnoreRatio = ratio;
  1401. }
  1402. else
  1403. {
  1404. wdata.rotationOffset = quaternion.identity;
  1405. wdata.rotationIgnoreRatio = 0;
  1406. }
  1407. // 速度ウエイト更新
  1408. if (tdata.velocityWeight < 1.0f)
  1409. {
  1410. float addw = tdata.velocityRecoverySpeed > 1e-6f ? dtime / tdata.velocityRecoverySpeed : 1.0f;
  1411. tdata.velocityWeight = math.saturate(tdata.velocityWeight + addw);
  1412. }
  1413. bool reset = false;
  1414. if (tdata.IsFlag(Flag_Reset_WorldInfluence) || tdata.IsFlag(Flag_Reset_Position))
  1415. {
  1416. // リセット
  1417. wdata.moveOffset = 0;
  1418. wdata.moveIgnoreRatio = 0;
  1419. wdata.rotationOffset = quaternion.identity;
  1420. wdata.rotationIgnoreRatio = 0;
  1421. wdata.oldPosition = bpos;
  1422. wdata.oldRotation = brot;
  1423. // チームタイムリセット(強制更新)
  1424. tdata.nowTime = updateDeltaTime;
  1425. // 速度ウエイト
  1426. tdata.velocityWeight = wdata.stabilizationTime > 1e-6f ? 0.0f : 1.0f;
  1427. tdata.velocityRecoverySpeed = wdata.stabilizationTime;
  1428. reset = true;
  1429. }
  1430. wdata.nowPosition = bpos;
  1431. wdata.nowRotation = brot;
  1432. // 書き戻し
  1433. teamWorldInfluenceList[teamId] = wdata;
  1434. // 時間更新(タイムスケール対応)
  1435. UpdateTime(ref tdata, reset, dtime);
  1436. // リセットフラグOFF
  1437. tdata.SetFlag(Flag_Reset_WorldInfluence, false);
  1438. // 風の影響を計算
  1439. Wind(ref tdata, bpos, teamId);
  1440. }
  1441. // 書き戻し
  1442. teamData[teamId] = tdata;
  1443. }
  1444. /// <summary>
  1445. /// チーム時間更新
  1446. /// </summary>
  1447. /// <param name="tdata"></param>
  1448. /// <param name="reset"></param>
  1449. void UpdateTime(ref TeamData tdata, bool reset, float dtime)
  1450. {
  1451. // 時間更新(タイムスケール対応)
  1452. // チームごとに固有の時間で動作する
  1453. tdata.updateCount = 0;
  1454. float timeScale = tdata.timeScale * globalTimeScale;
  1455. timeScale = tdata.IsPause() ? 0.0f : timeScale; // 一時停止
  1456. float addTime = dtime * timeScale;
  1457. tdata.time += addTime;
  1458. tdata.addTime = addTime;
  1459. // 時間ステップ
  1460. float nowTime = tdata.nowTime + addTime;
  1461. while (nowTime >= updateDeltaTime)
  1462. {
  1463. nowTime -= updateDeltaTime;
  1464. tdata.updateCount++;
  1465. }
  1466. // リセットフラグがONならば最低1回更新とする
  1467. if (reset)
  1468. {
  1469. // ただしUnityPhysics更新は除く
  1470. if (tdata.IsPhysicsUpdate() == false)
  1471. tdata.updateCount = Mathf.Max(tdata.updateCount, 1);
  1472. // 最終更新時間もリセット
  1473. tdata.oldTime = tdata.time;
  1474. }
  1475. // 最大実行回数
  1476. //tdata.updateCount = math.min(tdata.updateCount, ups / 30);
  1477. tdata.updateCount = math.min(tdata.updateCount, maxUpdateCount);
  1478. tdata.nowTime = nowTime;
  1479. // スタート時間(最初の物理更新が実行される時間)
  1480. tdata.startTime = tdata.time - nowTime - updateDeltaTime * (math.max(tdata.updateCount - 1, 0));
  1481. // 補間再生判定
  1482. if (timeScale < 0.99f || unityTimeScale < 0.99f)
  1483. {
  1484. tdata.SetFlag(Flag_Interpolate, true);
  1485. }
  1486. else
  1487. {
  1488. tdata.SetFlag(Flag_Interpolate, false);
  1489. }
  1490. }
  1491. /// <summary>
  1492. /// コンポーネントの風影響を事前計算する
  1493. /// 実際の詳細はパーティクルごとに行う
  1494. /// </summary>
  1495. /// <param name="tdata"></param>
  1496. /// <param name="pos"></param>
  1497. /// <param name="teamId"></param>
  1498. void Wind(ref TeamData tdata, float3 pos, int teamId)
  1499. {
  1500. var windInfo = new WindInfo();
  1501. windInfo.windDataIndexList = -1;
  1502. if (windCount > 0 && tdata.forceWindInfluence >= 0.01f)
  1503. {
  1504. float minVolume = float.MaxValue;
  1505. int areaWindCount = 0;
  1506. int addWindCount = 0;
  1507. for (int i = 0; i < windData.Length; i++)
  1508. {
  1509. var wdata = windData[i];
  1510. if (wdata.IsActive())
  1511. {
  1512. // このチームが風エリアに入っているか判定する
  1513. // 加算風は最大3つまで
  1514. bool isAddition = wdata.IsFlag(PhysicsManagerWindData.Flag_Addition);
  1515. if (isAddition && addWindCount >= 3)
  1516. continue;
  1517. // 風コンポーネントの姿勢
  1518. var bpos = bonePosList[wdata.transformIndex];
  1519. var brot = boneRotList[wdata.transformIndex];
  1520. // ローカル座標
  1521. float3 v = pos - bpos;
  1522. float3 lpos = math.mul(math.inverse(brot), v);
  1523. float len = math.length(v);
  1524. // エリア判定
  1525. float3 areaSize = wdata.areaSize;
  1526. switch (wdata.shapeType)
  1527. {
  1528. case PhysicsManagerWindData.ShapeType.Box:
  1529. var lv = math.abs(lpos);
  1530. if (lv.x > areaSize.x || lv.y > areaSize.y || lv.z > areaSize.z)
  1531. continue;
  1532. break;
  1533. case PhysicsManagerWindData.ShapeType.Sphere:
  1534. if (math.length(v) > wdata.areaSize.x)
  1535. continue;
  1536. break;
  1537. default:
  1538. continue;
  1539. }
  1540. // エリア風の場合はボリューム判定(体積が小さいものが優先)
  1541. if (isAddition == false && wdata.areaVolume > minVolume)
  1542. continue;
  1543. // 風の方向
  1544. float3 mainDirection; // world
  1545. switch (wdata.directionType)
  1546. {
  1547. case PhysicsManagerWindData.DirectionType.OneDirection:
  1548. mainDirection = math.mul(brot, wdata.direction);
  1549. break;
  1550. case PhysicsManagerWindData.DirectionType.Radial:
  1551. if (len < 1e-06f)
  1552. continue;
  1553. mainDirection = math.normalize(v);
  1554. break;
  1555. default:
  1556. continue;
  1557. }
  1558. // 距離による風力減衰
  1559. float windMain = wdata.main;
  1560. if (wdata.windType == PhysicsManagerWindData.WindType.Area && wdata.directionType == PhysicsManagerWindData.DirectionType.Radial && wdata.areaLength > 0.01f)
  1561. {
  1562. float depth = math.saturate(len / wdata.areaLength);
  1563. float attenuation = wdata.attenuation.Evaluate(depth);
  1564. windMain *= attenuation;
  1565. //Debug.Log($"len:{len}, areaLength:{wdata.areaLength}, depth:{depth}, atten:{attenuation}");
  1566. }
  1567. if (windMain < 0.01f)
  1568. continue;
  1569. // 実行する風として登録する
  1570. int index = isAddition ? 1 + addWindCount : 0;
  1571. windInfo.windDataIndexList[index] = i;
  1572. windInfo.windDirectionList[index] = mainDirection;
  1573. windInfo.windMainList[index] = windMain;
  1574. if (isAddition)
  1575. addWindCount++;
  1576. else
  1577. {
  1578. areaWindCount = 1;
  1579. minVolume = wdata.areaVolume;
  1580. }
  1581. }
  1582. }
  1583. windInfo.windCount = areaWindCount + addWindCount;
  1584. }
  1585. teamWindInfoList[teamId] = windInfo;
  1586. tdata.SetFlag(Flag_Wind, windInfo.windCount > 0);
  1587. }
  1588. }
  1589. //=========================================================================================
  1590. internal void PostUpdateTeamData()
  1591. {
  1592. // チームデータ後処理
  1593. var job = new PostProcessTeamDataJob()
  1594. {
  1595. fixedUpdateCount = UpdateTime.FixedUpdateCount,
  1596. teamData = teamDataList.ToJobArray(),
  1597. teamWorldInfluenceList = teamWorldInfluenceList.ToJobArray(),
  1598. };
  1599. Compute.MasterJob = job.Schedule(teamDataList.Length, 8, Compute.MasterJob);
  1600. }
  1601. [BurstCompile]
  1602. struct PostProcessTeamDataJob : IJobParallelFor
  1603. {
  1604. public int fixedUpdateCount;
  1605. public NativeArray<TeamData> teamData;
  1606. public NativeArray<WorldInfluence> teamWorldInfluenceList;
  1607. // チームデータごと
  1608. public void Execute(int index)
  1609. {
  1610. var tdata = teamData[index];
  1611. if (tdata.IsActive() == false)
  1612. return;
  1613. var wdata = teamWorldInfluenceList[index];
  1614. wdata.oldPosition = wdata.nowPosition;
  1615. wdata.oldRotation = wdata.nowRotation;
  1616. if (tdata.IsRunning())
  1617. {
  1618. // 外部フォースをリセット
  1619. tdata.impactForce = 0;
  1620. tdata.forceMode = ForceMode.None;
  1621. // 最終更新時間
  1622. tdata.oldTime = tdata.time;
  1623. // 姿勢リセットフラグクリア(チーム更新時のみ)
  1624. tdata.SetFlag(Flag_Reset_Position, false);
  1625. tdata.SetFlag(Flag_Reset_Keep, false);
  1626. // 計算実行カウント
  1627. tdata.calcCount++;
  1628. }
  1629. // 書き戻し
  1630. teamData[index] = tdata;
  1631. teamWorldInfluenceList[index] = wdata;
  1632. }
  1633. }
  1634. //=========================================================================================
  1635. /*
  1636. public void UpdateTeamUpdateCount()
  1637. {
  1638. // チームデータ後処理
  1639. var job = new UpdateTeamUpdateCountJob()
  1640. {
  1641. teamData = Team.teamDataList.ToJobArray(),
  1642. };
  1643. Compute.MasterJob = job.Schedule(Team.teamDataList.Length, 8, Compute.MasterJob);
  1644. }
  1645. [BurstCompile]
  1646. struct UpdateTeamUpdateCountJob : IJobParallelFor
  1647. {
  1648. public NativeArray<TeamData> teamData;
  1649. // チームデータごと
  1650. public void Execute(int index)
  1651. {
  1652. var tdata = teamData[index];
  1653. if (tdata.IsActive() == false)
  1654. return;
  1655. tdata.runCount++;
  1656. // 書き戻し
  1657. teamData[index] = tdata;
  1658. }
  1659. }
  1660. */
  1661. }
  1662. }