PhysicsManagerParticleData.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Unity.Jobs;
  7. using Unity.Mathematics;
  8. using UnityEngine;
  9. using UnityEngine.Jobs;
  10. namespace MagicaCloth
  11. {
  12. /// <summary>
  13. /// パーティクルデータ
  14. /// </summary>
  15. public class PhysicsManagerParticleData : PhysicsManagerAccess
  16. {
  17. /// <summary>
  18. /// パーティクルフラグビット
  19. /// </summary>
  20. public const uint Flag_Enable = 0x00000001; // 有効フラグ
  21. public const uint Flag_Kinematic = 0x00000002; // 物理無効
  22. public const uint Flag_Hold = 0x00000004; // ホールド中(これは固定化として扱われる)
  23. //public const uint Flag_Collision = 0x00000008; // コリジョン判定
  24. public const uint Flag_Collider = 0x00000010; // コライダー
  25. public const uint Flag_Plane = 0x00000020; // プレーン
  26. public const uint Flag_CapsuleX = 0x00000040; // カプセルX軸方向
  27. public const uint Flag_CapsuleY = 0x00000080; // カプセルY軸方向
  28. public const uint Flag_CapsuleZ = 0x00000100; // カプセルZ軸方向
  29. public const uint Flag_Box = 0x00000200; // ボックス(予約)
  30. public const uint Flag_TriangleRotation = 0x00000400; // TriangleWorkerによる回転制御
  31. public const uint Flag_Transform_Read_Pos = 0x00001000; // posへトランスフォーム位置を読み込む
  32. public const uint Flag_Transform_Read_Rot = 0x00002000; // rotへトランスフォーム位置を読み込む
  33. public const uint Flag_Transform_Read_Base = 0x00004000; // basePos/baseRotへトランスフォーム位置を読み込む
  34. public const uint Flag_Transform_Read_Local = 0x00008000; // ローカル座標計算(=ワールドスケールが必要)
  35. public const uint Flag_Transform_Read_Scl = 0x00010000; // ワールドスケールが必要
  36. public const uint Flag_Transform_Write = 0x00020000; // トランスフォームへpos/rotを書き戻す
  37. public const uint Flag_Transform_Restore = 0x00040000; // 実行前にトランスフォームlocalPos/localRotを復元
  38. public const uint Flag_Transform_UnityPhysics = 0x00080000; // FixedUpdateでの更新
  39. public const uint Flag_Transform_Parent = 0x00100000; // 親トランスフォームを参照している
  40. public const uint Flag_Step_Update = 0x01000000; // Old -> Base を補間して現在姿勢とする
  41. public const uint Flag_Reset_Position = 0x02000000; // 位置回転速度をBaseにリセットする
  42. //public const uint Flag_Friction = 0x04000000; // 摩擦(コリジョン判定あり)
  43. /// <summary>
  44. /// パーティクルフラグ状態
  45. /// </summary>
  46. public struct ParticleFlag
  47. {
  48. /// <summary>
  49. /// フラグビットデータ
  50. /// </summary>
  51. public uint flag;
  52. /// <summary>
  53. /// コンストラクタ
  54. /// </summary>
  55. /// <param name="flags"></param>
  56. public ParticleFlag(params uint[] flags)
  57. {
  58. flag = 0;
  59. foreach (var f in flags)
  60. flag |= f;
  61. }
  62. /// <summary>
  63. /// フラグ判定
  64. /// </summary>
  65. /// <param name="flag"></param>
  66. /// <returns></returns>
  67. public bool IsFlag(uint flag)
  68. {
  69. return (this.flag & flag) != 0;
  70. }
  71. /// <summary>
  72. /// フラグ設定
  73. /// </summary>
  74. /// <param name="flag"></param>
  75. /// <param name="sw"></param>
  76. public void SetFlag(uint flag, bool sw)
  77. {
  78. if (sw)
  79. this.flag |= flag;
  80. else
  81. this.flag &= ~flag;
  82. }
  83. /// <summary>
  84. /// データが有効か判定する
  85. /// </summary>
  86. /// <returns></returns>
  87. public bool IsValid()
  88. {
  89. return (flag & Flag_Enable) != 0;
  90. }
  91. /// <summary>
  92. /// 有効フラグの設定
  93. /// </summary>
  94. /// <param name="sw"></param>
  95. public void SetEnable(bool sw)
  96. {
  97. if (sw)
  98. flag |= Flag_Enable;
  99. else
  100. flag &= ~Flag_Enable;
  101. }
  102. /// <summary>
  103. /// このパーティクルが固定化されているか判定する
  104. /// </summary>
  105. /// <returns></returns>
  106. public bool IsFixed()
  107. {
  108. return (flag & (Flag_Kinematic | Flag_Hold)) != 0;
  109. }
  110. /// <summary>
  111. /// このパーティクルが移動できるか判定する
  112. /// </summary>
  113. /// <returns></returns>
  114. public bool IsMove()
  115. {
  116. return (flag & (Flag_Kinematic | Flag_Hold)) == 0;
  117. }
  118. /// <summary>
  119. /// このパーティクルが物理無効化されているか判定する
  120. /// </summary>
  121. /// <returns></returns>
  122. public bool IsKinematic()
  123. {
  124. return (flag & Flag_Kinematic) != 0;
  125. }
  126. /// <summary>
  127. /// このパーティクルがコリジョン判定を行うか判定する
  128. /// </summary>
  129. /// <returns></returns>
  130. //public bool IsCollision()
  131. //{
  132. // return (flag & Flag_Collision) != 0;
  133. //}
  134. /// <summary>
  135. /// このパーティクルがホールド中か判定する
  136. /// </summary>
  137. /// <returns></returns>
  138. public bool IsHold()
  139. {
  140. return (flag & Flag_Hold) != 0;
  141. }
  142. /// <summary>
  143. /// このパーティクルがコライダーか判定する
  144. /// </summary>
  145. /// <returns></returns>
  146. public bool IsCollider()
  147. {
  148. return (flag & Flag_Collider) != 0;
  149. }
  150. /// <summary>
  151. /// このパーティクルがトランスフォームの読込みを行うか判定する
  152. /// </summary>
  153. /// <returns></returns>
  154. public bool IsReadTransform()
  155. {
  156. return (flag & (Flag_Transform_Read_Pos | Flag_Transform_Read_Rot | Flag_Transform_Read_Base)) != 0;
  157. }
  158. /// <summary>
  159. /// このパーティクルがトランスフォームの書き込みを行うか判定する
  160. /// </summary>
  161. /// <returns></returns>
  162. public bool IsWriteTransform()
  163. {
  164. return (flag & Flag_Transform_Write) != 0;
  165. }
  166. /// <summary>
  167. /// このパーティクルがトランスフォームの復元を行うか判定する
  168. /// </summary>
  169. /// <returns></returns>
  170. public bool IsRestoreTransform()
  171. {
  172. return (flag & Flag_Transform_Restore) != 0;
  173. //return (flag & (Flag_Transform_Restore_Pos | Flag_Transform_Restore_Rot)) != 0;
  174. }
  175. /// <summary>
  176. /// このパーティクルがFixedUpdateでトランスフォームの更新を行うか判定する
  177. /// </summary>
  178. /// <returns></returns>
  179. public bool IsUnityPhysics()
  180. {
  181. return (flag & Flag_Transform_UnityPhysics) != 0;
  182. }
  183. /// <summary>
  184. /// このパーティクルがトランスフォームのスケール読み込みを行うか判定する
  185. /// </summary>
  186. /// <returns></returns>
  187. public bool IsReadSclTransform()
  188. {
  189. return (flag & Flag_Transform_Read_Scl) != 0;
  190. }
  191. /// <summary>
  192. /// このパーティクルが親トランスフォームの参照を保持しているか判定する
  193. /// </summary>
  194. /// <returns></returns>
  195. public bool IsParentTransform()
  196. {
  197. return (flag & Flag_Transform_Parent) != 0;
  198. }
  199. }
  200. //=========================================================================================
  201. // パーティクルデータ(すべてのインデックスは同期している)
  202. /// <summary>
  203. /// フラグリスト
  204. /// </summary>
  205. public FixedChunkNativeArray<ParticleFlag> flagList;
  206. /// <summary>
  207. /// 所属するチームID(0=グローバル)
  208. /// </summary>
  209. public FixedChunkNativeArray<int> teamIdList;
  210. /// <summary>
  211. /// 現在座標リスト
  212. /// </summary>
  213. public FixedChunkNativeArray<float3> posList;
  214. /// <summary>
  215. /// 現在回転リスト
  216. /// </summary>
  217. public FixedChunkNativeArray<quaternion> rotList;
  218. /// <summary>
  219. /// 1つ前の座標リスト
  220. /// </summary>
  221. public FixedChunkNativeArray<float3> oldPosList;
  222. /// <summary>
  223. /// 1つ前の回転リスト
  224. /// </summary>
  225. public FixedChunkNativeArray<quaternion> oldRotList;
  226. /// <summary>
  227. /// 1つ前の座標リスト(スロー再生用)
  228. /// </summary>
  229. public FixedChunkNativeArray<float3> oldSlowPosList;
  230. /// <summary>
  231. /// 本来のローカル位置リスト
  232. /// </summary>
  233. public FixedChunkNativeArray<float3> localPosList;
  234. /// <summary>
  235. /// 現在の基準位置リスト
  236. /// </summary>
  237. public FixedChunkNativeArray<float3> basePosList;
  238. /// <summary>
  239. /// 現在の基準回転リスト
  240. /// </summary>
  241. public FixedChunkNativeArray<quaternion> baseRotList;
  242. /// <summary>
  243. /// 本来の基準位置リスト
  244. /// </summary>
  245. public FixedChunkNativeArray<float3> snapBasePosList;
  246. /// <summary>
  247. /// 本来の基準回転リスト
  248. /// </summary>
  249. public FixedChunkNativeArray<quaternion> snapBaseRotList;
  250. /// <summary>
  251. /// 1つ前の基準位置リスト
  252. /// </summary>
  253. public FixedChunkNativeArray<float3> oldBasePosList;
  254. /// <summary>
  255. /// 1つ前の基準回転リスト
  256. /// </summary>
  257. public FixedChunkNativeArray<quaternion> oldBaseRotList;
  258. /// <summary>
  259. /// パーティクル深さリスト
  260. /// </summary>
  261. public FixedChunkNativeArray<float> depthList;
  262. /// <summary>
  263. /// 半径リスト
  264. /// </summary>
  265. public FixedChunkNativeArray<float3> radiusList;
  266. /// <summary>
  267. /// 復元トランスフォームリストへのインデックス
  268. /// 不要な場合は(-1)
  269. /// </summary>
  270. public FixedChunkNativeArray<int> restoreTransformIndexList;
  271. /// <summary>
  272. /// 読み込み/書き込みトランスフォームリストへのインデックス
  273. /// 不要な場合は(-1)
  274. /// </summary>
  275. public FixedChunkNativeArray<int> transformIndexList;
  276. /// <summary>
  277. /// 現在の摩擦係数リスト
  278. /// </summary>
  279. public FixedChunkNativeArray<float> frictionList;
  280. /// <summary>
  281. /// 現在の静止摩擦係数リスト
  282. /// </summary>
  283. public FixedChunkNativeArray<float> staticFrictionList;
  284. /// <summary>
  285. /// 現在の速度リスト
  286. /// </summary>
  287. public FixedChunkNativeArray<float3> velocityList;
  288. /// <summary>
  289. /// 接触コライダーID(0=なし)
  290. /// </summary>
  291. public FixedChunkNativeArray<int> collisionLinkIdList;
  292. /// <summary>
  293. /// 接触コライダーの衝突法線
  294. /// </summary>
  295. public FixedChunkNativeArray<float3> collisionNormalList;
  296. /// <summary>
  297. /// 作業用座標リスト0
  298. /// </summary>
  299. FixedChunkNativeArray<float3> nextPos0List;
  300. /// <summary>
  301. /// 作業用座標リスト1
  302. /// </summary>
  303. FixedChunkNativeArray<float3> nextPos1List;
  304. /// <summary>
  305. /// 作業用座標リストの切り替えスイッチ
  306. /// </summary>
  307. int nextPosSwitch = 0;
  308. /// <summary>
  309. /// 作業用回転リスト0
  310. /// </summary>
  311. FixedChunkNativeArray<quaternion> nextRot0List;
  312. /// <summary>
  313. /// 作業用回転リスト1
  314. /// </summary>
  315. FixedChunkNativeArray<quaternion> nextRot1List;
  316. /// <summary>
  317. /// 作業用回転リストの切り替えスイッチ
  318. /// </summary>
  319. int nextRotSwitch = 0;
  320. //=========================================================================================
  321. /// <summary>
  322. /// コライダー数
  323. /// </summary>
  324. private int colliderCount;
  325. //=========================================================================================
  326. /// <summary>
  327. /// 初期設定
  328. /// </summary>
  329. public override void Create()
  330. {
  331. flagList = new FixedChunkNativeArray<ParticleFlag>();
  332. teamIdList = new FixedChunkNativeArray<int>();
  333. posList = new FixedChunkNativeArray<float3>();
  334. rotList = new FixedChunkNativeArray<quaternion>();
  335. oldPosList = new FixedChunkNativeArray<float3>();
  336. oldRotList = new FixedChunkNativeArray<quaternion>();
  337. oldSlowPosList = new FixedChunkNativeArray<float3>();
  338. localPosList = new FixedChunkNativeArray<float3>();
  339. basePosList = new FixedChunkNativeArray<float3>();
  340. baseRotList = new FixedChunkNativeArray<quaternion>();
  341. snapBasePosList = new FixedChunkNativeArray<float3>();
  342. snapBaseRotList = new FixedChunkNativeArray<quaternion>();
  343. oldBasePosList = new FixedChunkNativeArray<float3>();
  344. oldBaseRotList = new FixedChunkNativeArray<quaternion>();
  345. depthList = new FixedChunkNativeArray<float>();
  346. radiusList = new FixedChunkNativeArray<float3>();
  347. restoreTransformIndexList = new FixedChunkNativeArray<int>();
  348. transformIndexList = new FixedChunkNativeArray<int>();
  349. frictionList = new FixedChunkNativeArray<float>();
  350. staticFrictionList = new FixedChunkNativeArray<float>();
  351. velocityList = new FixedChunkNativeArray<float3>();
  352. collisionLinkIdList = new FixedChunkNativeArray<int>();
  353. collisionNormalList = new FixedChunkNativeArray<float3>();
  354. nextPos0List = new FixedChunkNativeArray<float3>();
  355. nextPos1List = new FixedChunkNativeArray<float3>();
  356. nextRot0List = new FixedChunkNativeArray<quaternion>();
  357. nextRot1List = new FixedChunkNativeArray<quaternion>();
  358. // パーティクル[0]番を登録、以降この0番は無効扱いとする
  359. var c = CreateParticle(Flag_Kinematic, 0, 0, quaternion.identity, 0.0f, 1.0f, 0);
  360. SetEnable(c, false, null, null, null);
  361. }
  362. /// <summary>
  363. /// 破棄
  364. /// </summary>
  365. public override void Dispose()
  366. {
  367. if (flagList == null)
  368. return;
  369. flagList.Dispose();
  370. teamIdList.Dispose();
  371. posList.Dispose();
  372. rotList.Dispose();
  373. oldPosList.Dispose();
  374. oldRotList.Dispose();
  375. oldSlowPosList.Dispose();
  376. localPosList.Dispose();
  377. basePosList.Dispose();
  378. baseRotList.Dispose();
  379. snapBasePosList.Dispose();
  380. snapBaseRotList.Dispose();
  381. oldBasePosList.Dispose();
  382. oldBaseRotList.Dispose();
  383. depthList.Dispose();
  384. radiusList.Dispose();
  385. restoreTransformIndexList.Dispose();
  386. transformIndexList.Dispose();
  387. frictionList.Dispose();
  388. staticFrictionList.Dispose();
  389. velocityList.Dispose();
  390. collisionLinkIdList.Dispose();
  391. collisionNormalList.Dispose();
  392. nextPos0List.Dispose();
  393. nextPos1List.Dispose();
  394. nextRot0List.Dispose();
  395. nextRot1List.Dispose();
  396. }
  397. //=========================================================================================
  398. /// <summary>
  399. /// パーティクルを1つ作成
  400. /// </summary>
  401. /// <param name="flag"></param>
  402. /// <param name="wpos"></param>
  403. /// <param name="wrot"></param>
  404. /// <param name="radius"></param>
  405. /// <param name="mass"></param>
  406. /// <param name="gravity"></param>
  407. /// <param name="drag"></param>
  408. /// <param name="depth"></param>
  409. /// <param name="maxVelocity"></param>
  410. /// <param name="target"></param>
  411. /// <returns></returns>
  412. public ChunkData CreateParticle(
  413. uint flag,
  414. int team,
  415. float3 wpos, quaternion wrot,
  416. float depth,
  417. float3 radius,
  418. float3 targetLocalPos
  419. )
  420. {
  421. flag |= Flag_Enable; // 有効フラグは必須
  422. var pf = new ParticleFlag(flag);
  423. var c = flagList.Add(pf);
  424. teamIdList.Add(team);
  425. posList.Add(wpos);
  426. rotList.Add(wrot);
  427. oldPosList.Add(wpos);
  428. oldRotList.Add(wrot);
  429. oldSlowPosList.Add(wpos);
  430. localPosList.Add(targetLocalPos);
  431. basePosList.Add(wpos);
  432. baseRotList.Add(wrot);
  433. snapBasePosList.Add(wpos);
  434. snapBaseRotList.Add(wrot);
  435. oldBasePosList.Add(wpos);
  436. oldBaseRotList.Add(wrot);
  437. depthList.Add(depth);
  438. radiusList.Add(radius);
  439. frictionList.Add(0.0f);
  440. staticFrictionList.Add(0.0f);
  441. velocityList.Add(0);
  442. collisionLinkIdList.Add(0);
  443. collisionNormalList.Add(0);
  444. nextPos0List.Add(0);
  445. nextPos1List.Add(0);
  446. nextRot0List.Add(quaternion.identity);
  447. nextRot1List.Add(quaternion.identity);
  448. // トランスフォームアクセス
  449. int restoreTransformIndex = -1;
  450. int transformIndex = -1;
  451. restoreTransformIndexList.Add(restoreTransformIndex);
  452. transformIndexList.Add(transformIndex);
  453. // コライダーカウント
  454. if (pf.IsCollider())
  455. colliderCount++;
  456. return c;
  457. }
  458. /// <summary>
  459. /// パーティクルをグループで作成
  460. /// </summary>
  461. /// <param name="team"></param>
  462. /// <param name="count"></param>
  463. /// <param name="funcFlag"></param>
  464. /// <param name="funcWpos"></param>
  465. /// <param name="funcWrot"></param>
  466. /// <param name="funcLpos"></param>
  467. /// <param name="funcLrot"></param>
  468. /// <param name="funcRadius"></param>
  469. /// <param name="funcMass"></param>
  470. /// <param name="funcGravity"></param>
  471. /// <param name="funcDrag"></param>
  472. /// <param name="funcDepth"></param>
  473. /// <param name="funcMaxVelocity"></param>
  474. /// <param name="funcTarget"></param>
  475. /// <param name="funcTargetLocalPos"></param>
  476. /// <returns></returns>
  477. public ChunkData CreateParticle(
  478. int team,
  479. int count,
  480. System.Func<int, uint> funcFlag,
  481. System.Func<int, float3> funcWpos,
  482. System.Func<int, quaternion> funcWrot,
  483. System.Func<int, float> funcDepth,
  484. System.Func<int, float3> funcRadius,
  485. System.Func<int, float3> funcTargetLocalPos
  486. )
  487. {
  488. var c = flagList.AddChunk(count);
  489. teamIdList.AddChunk(count);
  490. posList.AddChunk(count);
  491. rotList.AddChunk(count);
  492. oldPosList.AddChunk(count);
  493. oldRotList.AddChunk(count);
  494. oldSlowPosList.AddChunk(count);
  495. localPosList.AddChunk(count);
  496. basePosList.AddChunk(count);
  497. baseRotList.AddChunk(count);
  498. snapBasePosList.AddChunk(count);
  499. snapBaseRotList.AddChunk(count);
  500. oldBasePosList.AddChunk(count);
  501. oldBaseRotList.AddChunk(count);
  502. depthList.AddChunk(count);
  503. radiusList.AddChunk(count);
  504. frictionList.AddChunk(count);
  505. staticFrictionList.AddChunk(count);
  506. velocityList.AddChunk(count);
  507. collisionLinkIdList.AddChunk(count);
  508. collisionNormalList.AddChunk(count);
  509. nextPos0List.AddChunk(count);
  510. nextPos1List.AddChunk(count);
  511. nextRot0List.AddChunk(count);
  512. nextRot1List.AddChunk(count);
  513. restoreTransformIndexList.AddChunk(count);
  514. transformIndexList.AddChunk(count);
  515. teamIdList.Fill(c, team);
  516. nextRot0List.Fill(c, quaternion.identity);
  517. nextRot1List.Fill(c, quaternion.identity);
  518. for (int i = 0; i < count; i++)
  519. {
  520. int pindex = c.startIndex + i;
  521. uint flag = Flag_Enable;
  522. float3 wpos = 0;
  523. quaternion wrot = quaternion.identity;
  524. float3 tlpos = 0;
  525. float depth = 0;
  526. float3 radius = 0;
  527. int restoreTransformIndex = -1;
  528. int transformIndex = -1;
  529. if (funcFlag != null)
  530. flag |= funcFlag(i);
  531. var pf = new ParticleFlag(flag);
  532. if (funcWpos != null)
  533. wpos = funcWpos(i);
  534. if (funcWrot != null)
  535. wrot = funcWrot(i);
  536. if (funcTargetLocalPos != null)
  537. tlpos = funcTargetLocalPos(i);
  538. if (funcDepth != null)
  539. depth = funcDepth(i);
  540. if (funcRadius != null)
  541. radius = funcRadius(i);
  542. flagList[pindex] = pf;
  543. posList[pindex] = wpos;
  544. rotList[pindex] = wrot;
  545. oldPosList[pindex] = wpos;
  546. oldRotList[pindex] = wrot;
  547. oldSlowPosList[pindex] = wpos;
  548. localPosList[pindex] = tlpos;
  549. basePosList[pindex] = wpos;
  550. baseRotList[pindex] = wrot;
  551. snapBasePosList[pindex] = wpos;
  552. snapBaseRotList[pindex] = wrot;
  553. oldBasePosList[pindex] = wpos;
  554. oldBaseRotList[pindex] = wrot;
  555. depthList[pindex] = depth;
  556. radiusList[pindex] = radius;
  557. restoreTransformIndexList[pindex] = restoreTransformIndex;
  558. transformIndexList[pindex] = transformIndex;
  559. // コライダーカウント
  560. if (pf.IsCollider())
  561. colliderCount++;
  562. }
  563. return c;
  564. }
  565. /// <summary>
  566. /// パーティクル削除
  567. /// </summary>
  568. /// <param name="index"></param>
  569. public void RemoveParticle(ChunkData c)
  570. {
  571. for (int i = 0; i < c.dataLength; i++)
  572. {
  573. int pindex = c.startIndex + i;
  574. var pf = flagList[pindex];
  575. // コライダーカウント
  576. if (pf.IsCollider())
  577. colliderCount--;
  578. }
  579. flagList.RemoveChunk(c);
  580. teamIdList.RemoveChunk(c);
  581. posList.RemoveChunk(c);
  582. rotList.RemoveChunk(c);
  583. oldPosList.RemoveChunk(c);
  584. oldRotList.RemoveChunk(c);
  585. oldSlowPosList.RemoveChunk(c);
  586. localPosList.RemoveChunk(c);
  587. basePosList.RemoveChunk(c);
  588. baseRotList.RemoveChunk(c);
  589. snapBasePosList.RemoveChunk(c);
  590. snapBaseRotList.RemoveChunk(c);
  591. oldBasePosList.RemoveChunk(c);
  592. oldBaseRotList.RemoveChunk(c);
  593. depthList.RemoveChunk(c);
  594. radiusList.RemoveChunk(c);
  595. frictionList.RemoveChunk(c);
  596. staticFrictionList.RemoveChunk(c);
  597. velocityList.RemoveChunk(c);
  598. collisionLinkIdList.RemoveChunk(c);
  599. collisionNormalList.RemoveChunk(c);
  600. nextPos0List.RemoveChunk(c);
  601. nextPos1List.RemoveChunk(c);
  602. nextRot0List.RemoveChunk(c);
  603. nextRot1List.RemoveChunk(c);
  604. restoreTransformIndexList.RemoveChunk(c);
  605. transformIndexList.RemoveChunk(c);
  606. }
  607. /// <summary>
  608. /// パーティクルの有効フラグ設定
  609. /// </summary>
  610. /// <param name="c"></param>
  611. /// <param name="sw"></param>
  612. public void SetEnable(
  613. ChunkData c,
  614. bool sw,
  615. System.Func<int, Transform> funcTarget,
  616. System.Func<int, float3> funcLpos,
  617. System.Func<int, quaternion> funcLrot
  618. )
  619. {
  620. for (int i = 0; i < c.dataLength; i++)
  621. {
  622. int index = c.startIndex + i;
  623. var flag = flagList[index];
  624. flag.SetEnable(sw);
  625. if (sw)
  626. {
  627. // 有効化
  628. // 位置リセットフラグも立てる
  629. flag.SetFlag(Flag_Reset_Position, true);
  630. // ボーン登録
  631. if (funcTarget != null)
  632. {
  633. var target = funcTarget(i);
  634. if (target != null)
  635. {
  636. // 読み込みトランスフォーム登録
  637. if (flag.IsReadTransform() && transformIndexList[index] == -1)
  638. {
  639. // パーティクル書き戻し判定
  640. int windex = flag.IsWriteTransform() ? index : -1;
  641. // 親トランスフォームの参照の有無
  642. bool parent = flag.IsParentTransform();
  643. var transformIndex = Bone.AddBone(target, windex, parent);
  644. transformIndexList[index] = transformIndex;
  645. // ボーンのUnityPhysicsカウンタ
  646. if (flag.IsUnityPhysics())
  647. Bone.ChangeUnityPhysicsCount(transformIndex, true);
  648. }
  649. // 復元トランスフォーム登録
  650. if (flag.IsRestoreTransform() && restoreTransformIndexList[index] == -1)
  651. {
  652. float3 lpos = funcLpos != null ? funcLpos(i) : 0;
  653. quaternion lrot = funcLrot != null ? funcLrot(i) : quaternion.identity;
  654. restoreTransformIndexList[index] = Bone.AddRestoreBone(target, lpos, lrot, transformIndexList[index]);
  655. }
  656. }
  657. }
  658. }
  659. else
  660. {
  661. // 無効化
  662. // ボーン登録解除
  663. // 復元トランスフォーム解除
  664. if (flag.IsRestoreTransform())
  665. {
  666. var restoreTransformIndex = restoreTransformIndexList[index];
  667. if (restoreTransformIndex >= 0)
  668. {
  669. Bone.RemoveRestoreBone(restoreTransformIndex);
  670. restoreTransformIndexList[index] = -1;
  671. }
  672. }
  673. // 読み込み/書き込みトランスフォーム解除
  674. if (flag.IsReadTransform())
  675. {
  676. var transformIndex = transformIndexList[index];
  677. if (transformIndex >= 0)
  678. {
  679. // ボーンのUnityPhysicsカウンタ
  680. if (flag.IsUnityPhysics())
  681. Bone.ChangeUnityPhysicsCount(transformIndex, false);
  682. // ボーン解除
  683. int windex = flag.IsWriteTransform() ? index : -1;
  684. Bone.RemoveBone(transformIndex, windex);
  685. transformIndexList[index] = -1;
  686. }
  687. }
  688. }
  689. flagList[index] = flag;
  690. }
  691. }
  692. /// <summary>
  693. /// パーティクルの半径設定
  694. /// </summary>
  695. /// <param name="index"></param>
  696. /// <param name="radius"></param>
  697. public void SetRadius(int index, float3 radius)
  698. {
  699. radiusList[index] = radius;
  700. }
  701. /// <summary>
  702. /// パーティクルのローカル座標設定
  703. /// </summary>
  704. /// <param name="index"></param>
  705. /// <param name="lpos"></param>
  706. public void SetLocalPos(int index, Vector3 lpos)
  707. {
  708. localPosList[index] = lpos;
  709. }
  710. /// <summary>
  711. /// パーティクルのコリジョン判定設定
  712. /// </summary>
  713. /// <param name="index"></param>
  714. /// <param name="sw"></param>
  715. //public void SetCollision(int index, bool sw)
  716. //{
  717. // var flag = flagList[index];
  718. // flag.SetFlag(Flag_Collision, sw);
  719. // flagList[index] = flag;
  720. //}
  721. /// <summary>
  722. /// 実際に利用されているパーティクル数を返す
  723. /// [0]はグローバル無効パーティクルなので-1する
  724. /// </summary>
  725. public int Count
  726. {
  727. get
  728. {
  729. if (flagList == null)
  730. return 0;
  731. return Mathf.Max(flagList.Count - 1, 0);
  732. }
  733. }
  734. /// <summary>
  735. /// パーティクル配列の要素数を返す
  736. /// </summary>
  737. public int Length
  738. {
  739. get
  740. {
  741. if (flagList == null)
  742. return 0;
  743. return flagList.Length;
  744. }
  745. }
  746. /// <summary>
  747. /// コライダーの数を返す
  748. /// </summary>
  749. public int ColliderCount
  750. {
  751. get
  752. {
  753. return colliderCount;
  754. }
  755. }
  756. /// <summary>
  757. /// インデックスのパーティクルが有効か判定する
  758. /// </summary>
  759. /// <param name="index"></param>
  760. /// <returns></returns>
  761. public bool IsValid(int index)
  762. {
  763. if (index < 0 || index >= Length)
  764. return false;
  765. return flagList[index].IsValid();
  766. }
  767. public int GetTeamId(int index)
  768. {
  769. return teamIdList[index];
  770. }
  771. /// <summary>
  772. /// 指定パーティクルに連動するトランスフォームの未来予測をリセットする
  773. /// </summary>
  774. /// <param name="index"></param>
  775. public void ResetFuturePredictionTransform(int index)
  776. {
  777. var transformIndex = transformIndexList[index];
  778. if (transformIndex >= 0)
  779. {
  780. Bone.ResetFuturePrediction(transformIndex);
  781. }
  782. }
  783. /// <summary>
  784. /// 指定パーティクルに連動するトランスフォームの未来予測をリセットする
  785. /// </summary>
  786. /// <param name="c"></param>
  787. public void ResetFuturePredictionTransform(ChunkData c)
  788. {
  789. for (int i = 0, index = c.startIndex; i < c.dataLength; i++, index++)
  790. {
  791. ResetFuturePredictionTransform(index);
  792. }
  793. }
  794. //=========================================================================================
  795. // nextPosのダブルバッファ
  796. /// <summary>
  797. /// 入力用nextPosListを取得
  798. /// </summary>
  799. public FixedChunkNativeArray<float3> InNextPosList
  800. {
  801. get
  802. {
  803. return nextPosSwitch == 0 ? nextPos0List : nextPos1List;
  804. }
  805. }
  806. /// <summary>
  807. /// 出力用nextPosListを取得
  808. /// </summary>
  809. public FixedChunkNativeArray<float3> OutNextPosList
  810. {
  811. get
  812. {
  813. return nextPosSwitch == 0 ? nextPos1List : nextPos0List;
  814. }
  815. }
  816. /// <summary>
  817. /// 作業用nextPosListのIn/Out切り替え
  818. /// </summary>
  819. public void SwitchingNextPosList()
  820. {
  821. nextPosSwitch = (nextPosSwitch + 1) % 2;
  822. }
  823. //=========================================================================================
  824. // nextRotのダブルバッファ
  825. /// <summary>
  826. /// 入力用nextRotListを取得
  827. /// </summary>
  828. public FixedChunkNativeArray<quaternion> InNextRotList
  829. {
  830. get
  831. {
  832. return nextRotSwitch == 0 ? nextRot0List : nextRot1List;
  833. }
  834. }
  835. /// <summary>
  836. /// 出力用nextRotListを取得
  837. /// </summary>
  838. public FixedChunkNativeArray<quaternion> OutNextRotList
  839. {
  840. get
  841. {
  842. return nextRotSwitch == 0 ? nextRot1List : nextRot0List;
  843. }
  844. }
  845. /// <summary>
  846. /// 作業用nextRotListのIn/Out切り替え
  847. /// </summary>
  848. public void SwitchingNextRotList()
  849. {
  850. nextRotSwitch = (nextRotSwitch + 1) % 2;
  851. }
  852. //=========================================================================================
  853. /// <summary>
  854. /// ボーン姿勢をパーティクルにコピーする
  855. /// およびワールド移動影響更新
  856. /// </summary>
  857. public void UpdateBoneToParticle()
  858. {
  859. if (Count == 0)
  860. return;
  861. var job = new CopyBoneToParticleJob()
  862. {
  863. teamData = Team.teamDataList.ToJobArray(),
  864. teamWorldInfluenceList = Team.teamWorldInfluenceList.ToJobArray(),
  865. flagList = flagList.ToJobArray(),
  866. depthList = depthList.ToJobArray(),
  867. transformIndexList = transformIndexList.ToJobArray(),
  868. localPosList = localPosList.ToJobArray(),
  869. teamIdList = teamIdList.ToJobArray(),
  870. velocityList = velocityList.ToJobArray(),
  871. bonePosList = Bone.bonePosList.ToJobArray(),
  872. boneRotList = Bone.boneRotList.ToJobArray(),
  873. boneSclList = Bone.boneSclList.ToJobArray(),
  874. posList = posList.ToJobArray(),
  875. oldPosList = oldPosList.ToJobArray(),
  876. oldRotList = oldRotList.ToJobArray(),
  877. oldSlowPosList = oldSlowPosList.ToJobArray(),
  878. rotList = rotList.ToJobArray(),
  879. //basePosList = basePosList.ToJobArray(),
  880. //baseRotList = baseRotList.ToJobArray(),
  881. snapBasePosList = snapBasePosList.ToJobArray(),
  882. snapBaseRotList = snapBaseRotList.ToJobArray(),
  883. oldBasePosList = oldBasePosList.ToJobArray(),
  884. oldBaseRotList = oldBaseRotList.ToJobArray(),
  885. nextPosList = InNextPosList.ToJobArray(),
  886. };
  887. Compute.MasterJob = job.Schedule(Particle.Length, 64, Compute.MasterJob);
  888. }
  889. [BurstCompile]
  890. struct CopyBoneToParticleJob : IJobParallelFor
  891. {
  892. [Unity.Collections.ReadOnly]
  893. public NativeArray<PhysicsManagerTeamData.TeamData> teamData;
  894. [Unity.Collections.ReadOnly]
  895. public NativeArray<PhysicsManagerTeamData.WorldInfluence> teamWorldInfluenceList;
  896. // パーティクルごと
  897. [Unity.Collections.ReadOnly]
  898. public NativeArray<ParticleFlag> flagList;
  899. [Unity.Collections.ReadOnly]
  900. public NativeArray<float> depthList;
  901. [Unity.Collections.ReadOnly]
  902. public NativeArray<int> transformIndexList;
  903. [Unity.Collections.ReadOnly]
  904. public NativeArray<float3> localPosList;
  905. [Unity.Collections.ReadOnly]
  906. public NativeArray<int> teamIdList;
  907. public NativeArray<float3> velocityList;
  908. // トランスフォームごと
  909. [Unity.Collections.ReadOnly]
  910. public NativeArray<float3> bonePosList;
  911. [Unity.Collections.ReadOnly]
  912. public NativeArray<quaternion> boneRotList;
  913. [Unity.Collections.ReadOnly]
  914. public NativeArray<float3> boneSclList;
  915. // パーティクルごと
  916. public NativeArray<float3> posList;
  917. public NativeArray<float3> oldPosList;
  918. public NativeArray<quaternion> oldRotList;
  919. public NativeArray<float3> oldSlowPosList;
  920. [Unity.Collections.WriteOnly]
  921. public NativeArray<quaternion> rotList;
  922. //[Unity.Collections.WriteOnly]
  923. //public NativeArray<float3> basePosList;
  924. //[Unity.Collections.WriteOnly]
  925. //public NativeArray<quaternion> baseRotList;
  926. [Unity.Collections.WriteOnly]
  927. public NativeArray<float3> snapBasePosList;
  928. [Unity.Collections.WriteOnly]
  929. public NativeArray<quaternion> snapBaseRotList;
  930. public NativeArray<float3> oldBasePosList;
  931. public NativeArray<quaternion> oldBaseRotList;
  932. [Unity.Collections.WriteOnly]
  933. public NativeArray<float3> nextPosList;
  934. // パーティクルごと
  935. public void Execute(int index)
  936. {
  937. var flag = flagList[index];
  938. if (flag.IsValid() == false)
  939. return;
  940. float depth = depthList[index];
  941. // ワールド移動影響更新
  942. int teamId = teamIdList[index];
  943. var tdata = teamData[teamId];
  944. var wdata = teamWorldInfluenceList[teamId];
  945. float moveInfluence = wdata.moveInfluence.Evaluate(depth);
  946. float rotInfluence = wdata.rotInfluence.Evaluate(depth);
  947. // 維持テレポートなら移動影響は0にする
  948. if (tdata.IsFlag(PhysicsManagerTeamData.Flag_Reset_Keep))
  949. {
  950. moveInfluence = 0;
  951. rotInfluence = 0;
  952. }
  953. var oldpos = oldPosList[index];
  954. float3 offset = 0;
  955. // 最大移動/最大回転影響
  956. {
  957. // 影響値
  958. float moveIgnoreRatio = wdata.moveIgnoreRatio;
  959. float rotationIgnoreRatio = wdata.rotationIgnoreRatio;
  960. // 移動影響
  961. float moveRatio = (1.0f - moveIgnoreRatio) * (1.0f - moveInfluence) + moveIgnoreRatio;
  962. float3 offpos = wdata.moveOffset * moveRatio;
  963. // 回転影響
  964. float rotRatio = (1.0f - rotationIgnoreRatio) * (1.0f - rotInfluence) + rotationIgnoreRatio;
  965. quaternion offrot = math.slerp(quaternion.identity, wdata.rotationOffset, rotRatio);
  966. // 一旦ローカル座標系に戻して計算
  967. var lpos = oldpos - wdata.oldPosition;
  968. lpos = math.mul(offrot, lpos);
  969. lpos += offpos;
  970. var npos = wdata.oldPosition + lpos;
  971. offset = npos - oldpos;
  972. // 速度に回転影響を加える
  973. var vel = velocityList[index];
  974. vel = math.mul(offrot, vel);
  975. velocityList[index] = vel;
  976. // 回転影響を回転に加える
  977. var oldrot = oldRotList[index];
  978. oldrot = math.mul(offrot, oldrot);
  979. oldRotList[index] = oldrot;
  980. // 基準姿勢にも適用(v1.11.1)
  981. oldBasePosList[index] = oldBasePosList[index] + offset;
  982. var oldBaseRot = oldBaseRotList[index];
  983. oldBaseRot = math.mul(offrot, oldBaseRot);
  984. oldBaseRotList[index] = oldBaseRot;
  985. }
  986. oldPosList[index] = oldpos + offset;
  987. oldSlowPosList[index] = oldSlowPosList[index] + offset;
  988. if (flag.IsFixed())
  989. {
  990. // 固定パーティクルは前回位置をnextPosから計算するのでここにもオフセットを掛けておく(1.8.3)
  991. nextPosList[index] = oldpos + offset;
  992. }
  993. // ここからトランスフォーム読み込み
  994. if (flag.IsReadTransform() == false)
  995. return;
  996. // トランスフォームの最新の姿勢を読み込む
  997. var tindex = transformIndexList[index];
  998. if (tindex < 0)
  999. {
  1000. // この状況はDisable状態で配置されたコンポーネントなどで発生する可能性あり!(v1.12.0)
  1001. return;
  1002. }
  1003. var bpos = bonePosList[tindex];
  1004. var brot = boneRotList[tindex];
  1005. // ローカル姿勢を考慮
  1006. if (flag.IsFlag(Flag_Transform_Read_Local))
  1007. {
  1008. var bscl = boneSclList[tindex];
  1009. bpos = bpos + math.mul(brot, localPosList[index] * bscl);
  1010. }
  1011. // 原点として書き込み
  1012. if (flag.IsFlag(Flag_Transform_Read_Base))
  1013. {
  1014. //basePosList[index] = bpos;
  1015. //baseRotList[index] = brot;
  1016. snapBasePosList[index] = bpos;
  1017. snapBaseRotList[index] = brot;
  1018. }
  1019. // 現在値として書き込み
  1020. if (flag.IsFlag(Flag_Transform_Read_Pos))
  1021. {
  1022. posList[index] = bpos;
  1023. }
  1024. if (flag.IsFlag(Flag_Transform_Read_Rot))
  1025. {
  1026. rotList[index] = brot;
  1027. }
  1028. }
  1029. }
  1030. //=========================================================================================
  1031. /// <summary>
  1032. /// パーティクルの姿勢リセット更新
  1033. /// </summary>
  1034. public void UpdateResetParticle()
  1035. {
  1036. if (Count == 0)
  1037. return;
  1038. var job = new ResetParticleJob()
  1039. {
  1040. teamData = Team.teamDataList.ToJobArray(),
  1041. flagList = flagList.ToJobArray(),
  1042. teamIdList = teamIdList.ToJobArray(),
  1043. snapBasePosList = snapBasePosList.ToJobArray(),
  1044. snapBaseRotList = snapBaseRotList.ToJobArray(),
  1045. basePosList = basePosList.ToJobArray(),
  1046. baseRotList = baseRotList.ToJobArray(),
  1047. oldBasePosList = oldBasePosList.ToJobArray(),
  1048. oldBaseRotList = oldBaseRotList.ToJobArray(),
  1049. posList = posList.ToJobArray(),
  1050. rotList = rotList.ToJobArray(),
  1051. oldPosList = oldPosList.ToJobArray(),
  1052. oldRotList = oldRotList.ToJobArray(),
  1053. oldSlowPosList = oldSlowPosList.ToJobArray(),
  1054. velocityList = velocityList.ToJobArray(),
  1055. nextPosList = InNextPosList.ToJobArray(),
  1056. nextRotList = InNextRotList.ToJobArray(),
  1057. localPosList = localPosList.ToJobArray(),
  1058. };
  1059. Compute.MasterJob = job.Schedule(Particle.Length, 64, Compute.MasterJob);
  1060. }
  1061. [BurstCompile]
  1062. struct ResetParticleJob : IJobParallelFor
  1063. {
  1064. [Unity.Collections.ReadOnly]
  1065. public NativeArray<PhysicsManagerTeamData.TeamData> teamData;
  1066. // パーティクルごと
  1067. public NativeArray<ParticleFlag> flagList;
  1068. [Unity.Collections.ReadOnly]
  1069. public NativeArray<int> teamIdList;
  1070. [Unity.Collections.ReadOnly]
  1071. public NativeArray<float3> snapBasePosList;
  1072. [Unity.Collections.ReadOnly]
  1073. public NativeArray<quaternion> snapBaseRotList;
  1074. [Unity.Collections.WriteOnly]
  1075. public NativeArray<float3> basePosList;
  1076. [Unity.Collections.WriteOnly]
  1077. public NativeArray<quaternion> baseRotList;
  1078. [Unity.Collections.WriteOnly]
  1079. public NativeArray<float3> oldBasePosList;
  1080. [Unity.Collections.WriteOnly]
  1081. public NativeArray<quaternion> oldBaseRotList;
  1082. [Unity.Collections.WriteOnly]
  1083. public NativeArray<float3> posList;
  1084. [Unity.Collections.WriteOnly]
  1085. public NativeArray<quaternion> rotList;
  1086. [Unity.Collections.WriteOnly]
  1087. public NativeArray<float3> oldPosList;
  1088. [Unity.Collections.WriteOnly]
  1089. public NativeArray<quaternion> oldRotList;
  1090. [Unity.Collections.WriteOnly]
  1091. public NativeArray<float3> oldSlowPosList;
  1092. [Unity.Collections.WriteOnly]
  1093. public NativeArray<float3> velocityList;
  1094. [Unity.Collections.WriteOnly]
  1095. public NativeArray<float3> nextPosList;
  1096. [Unity.Collections.WriteOnly]
  1097. public NativeArray<quaternion> nextRotList;
  1098. [Unity.Collections.WriteOnly]
  1099. public NativeArray<float3> localPosList;
  1100. // パーティクルごと
  1101. public void Execute(int index)
  1102. {
  1103. var flag = flagList[index];
  1104. if (flag.IsValid() == false)
  1105. return;
  1106. // ワールド移動影響更新
  1107. int teamId = teamIdList[index];
  1108. var tdata = teamData[teamId];
  1109. if (tdata.IsPause())
  1110. return;
  1111. // 姿勢リセット
  1112. if (tdata.IsFlag(PhysicsManagerTeamData.Flag_Reset_Position) || flag.IsFlag(Flag_Reset_Position))
  1113. {
  1114. //var basePos = basePosList[index];
  1115. //var baseRot = baseRotList[index];
  1116. var basePos = snapBasePosList[index];
  1117. var baseRot = snapBaseRotList[index];
  1118. basePosList[index] = basePos;
  1119. baseRotList[index] = baseRot;
  1120. oldBasePosList[index] = basePos;
  1121. oldBaseRotList[index] = baseRot;
  1122. posList[index] = basePos;
  1123. rotList[index] = baseRot;
  1124. oldPosList[index] = basePos;
  1125. oldRotList[index] = baseRot;
  1126. oldSlowPosList[index] = basePos;
  1127. velocityList[index] = 0;
  1128. nextPosList[index] = basePos;
  1129. nextRotList[index] = baseRot;
  1130. // 移動パーティクルはlocalPosに実際の移動速度が格納されている
  1131. if (flag.IsKinematic() == false)
  1132. {
  1133. localPosList[index] = 0;
  1134. }
  1135. // フラグクリア
  1136. flag.SetFlag(Flag_Reset_Position, false);
  1137. flagList[index] = flag;
  1138. }
  1139. }
  1140. }
  1141. //=========================================================================================
  1142. /// <summary>
  1143. /// パーティクル姿勢をボーン姿勢に書き出す
  1144. /// </summary>
  1145. public bool UpdateParticleToBone()
  1146. {
  1147. if (Count > 0 && Bone.WriteBoneCount > 0)
  1148. {
  1149. var job = new CopyParticleToBoneJob()
  1150. {
  1151. flagList = flagList.ToJobArray(),
  1152. posList = posList.ToJobArray(),
  1153. rotList = rotList.ToJobArray(),
  1154. transformParticleIndexMap = Bone.writeBoneParticleIndexMap.Map,
  1155. writeBoneIndexList = Bone.writeBoneIndexList.ToJobArray(),
  1156. bonePosList = Bone.bonePosList.ToJobArray(),
  1157. boneRotList = Bone.boneRotList.ToJobArray(),
  1158. };
  1159. Compute.MasterJob = job.Schedule(Bone.writeBoneList.Length, 64, Compute.MasterJob);
  1160. return true;
  1161. }
  1162. return false;
  1163. }
  1164. [BurstCompile]
  1165. struct CopyParticleToBoneJob : IJobParallelFor
  1166. {
  1167. // パーティクルごと
  1168. [Unity.Collections.ReadOnly]
  1169. public NativeArray<ParticleFlag> flagList;
  1170. [Unity.Collections.ReadOnly]
  1171. public NativeArray<float3> posList;
  1172. [Unity.Collections.ReadOnly]
  1173. public NativeArray<quaternion> rotList;
  1174. // トランスフォームごと
  1175. [Unity.Collections.ReadOnly]
  1176. public NativeMultiHashMap<int, int> transformParticleIndexMap;
  1177. [Unity.Collections.ReadOnly]
  1178. public NativeArray<int> writeBoneIndexList;
  1179. [NativeDisableParallelForRestriction]
  1180. [Unity.Collections.WriteOnly]
  1181. public NativeArray<float3> bonePosList;
  1182. [NativeDisableParallelForRestriction]
  1183. [Unity.Collections.WriteOnly]
  1184. public NativeArray<quaternion> boneRotList;
  1185. // 書き込みトランスフォームごと
  1186. public void Execute(int index)
  1187. {
  1188. int pindex;
  1189. NativeMultiHashMapIterator<int> iterator;
  1190. if (transformParticleIndexMap.TryGetFirstValue(index, out pindex, out iterator))
  1191. {
  1192. // パーティクルは登録されている中から最初にヒットしたものを採用する
  1193. var flag = flagList[pindex];
  1194. if (flag.IsValid() == false)
  1195. return;
  1196. var pos = posList[pindex];
  1197. var rot = rotList[pindex];
  1198. // ボーン姿勢に書き戻す
  1199. int bindex = writeBoneIndexList[index] - 1; // +1が入っているので-1する
  1200. bonePosList[bindex] = pos;
  1201. boneRotList[bindex] = rot;
  1202. }
  1203. }
  1204. }
  1205. }
  1206. }