ClothData.cs 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System.Collections.Generic;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. namespace MagicaCloth
  8. {
  9. /// <summary>
  10. /// クロスデータ
  11. /// データは使用する頂点についてのみ保持する
  12. /// </summary>
  13. [System.Serializable]
  14. public class ClothData : ShareDataObject
  15. {
  16. /// <summary>
  17. /// データバージョン
  18. /// </summary>
  19. private const int DATA_VERSION = 5;
  20. // 頂点フラグ
  21. public const uint VertexFlag_End = 0x00010000; // 末端の頂点
  22. public const uint VertexFlag_TriangleRotation = 0x00020000; // Triangle回転補間頂点
  23. /// <summary>
  24. /// 各アルゴリズムタイプ(v1.11.0より)
  25. /// </summary>
  26. public ClothParams.Algorithm triangleBendAlgorithm;
  27. public ClothParams.Algorithm restoreRotationAlgorithm;
  28. public ClothParams.Algorithm clampRotationAlgorithm;
  29. /// <summary>
  30. /// メッシュの利用する頂点インデックスのリスト
  31. /// これがそのままパーティクルとして作成される
  32. /// クロスデータはこのリストのインデックスをデータとして指すようにする
  33. /// </summary>
  34. public List<int> useVertexList = new List<int>();
  35. /// <summary>
  36. /// 頂点選択データ
  37. /// SelectionDataクラスのInvalid/Move/Fixed/Extend値
  38. /// </summary>
  39. public List<int> selectionData = new List<int>();
  40. /// <summary>
  41. /// 頂点の最大レベル
  42. /// </summary>
  43. public uint maxLevel;
  44. /// <summary>
  45. /// 頂点フラグ(上位16bit)とレベル(下位16bit)リスト
  46. /// </summary>
  47. public List<uint> vertexFlagLevelList = new List<uint>();
  48. /// <summary>
  49. /// 頂点深さデータ(0.0-1.0)
  50. /// </summary>
  51. public List<float> vertexDepthList = new List<float>();
  52. /// <summary>
  53. /// ルート頂点リスト(-1=なし)
  54. /// </summary>
  55. public List<int> rootList = new List<int>();
  56. /// <summary>
  57. /// 親頂点リスト(-1=なし)
  58. /// </summary>
  59. public List<int> parentList = new List<int>();
  60. /// <summary>
  61. /// 重力方向減衰のターゲットボーンの上方向(ローカル)
  62. /// </summary>
  63. //public Vector3 directionalDampingUpDir = Vector3.up;
  64. /// <summary>
  65. /// 距離復元拘束(構造接続)
  66. /// </summary>
  67. public RestoreDistanceConstraint.RestoreDistanceData[] structDistanceDataList;
  68. public ReferenceDataIndex[] structDistanceReferenceList;
  69. /// <summary>
  70. /// 距離復元拘束(ベンド接続)
  71. /// </summary>
  72. public RestoreDistanceConstraint.RestoreDistanceData[] bendDistanceDataList;
  73. public ReferenceDataIndex[] bendDistanceReferenceList;
  74. /// <summary>
  75. /// 距離復元拘束(近接続)
  76. /// </summary>
  77. public RestoreDistanceConstraint.RestoreDistanceData[] nearDistanceDataList;
  78. public ReferenceDataIndex[] nearDistanceReferenceList;
  79. /// <summary>
  80. /// ルート最小最大距離拘束
  81. /// 対象距離リスト
  82. /// </summary>
  83. public ClampDistanceConstraint.ClampDistanceData[] rootDistanceDataList;
  84. public ReferenceDataIndex[] rootDistanceReferenceList;
  85. /// <summary>
  86. /// パーティクル最小最大距離拘束(v1.8.0)
  87. /// 対象距離リスト
  88. /// </summary>
  89. public ClampDistance2Constraint.ClampDistance2Data[] clampDistance2DataList;
  90. public ClampDistance2Constraint.ClampDistance2RootInfo[] clampDistance2RootInfoList;
  91. /// <summary>
  92. /// 回転復元拘束[Algorithm 1]
  93. /// 復元ローカルベクトルリスト
  94. /// </summary>
  95. public RestoreRotationConstraint.RotationData[] restoreRotationDataList;
  96. public ReferenceDataIndex[] restoreRotationReferenceList;
  97. /// <summary>
  98. /// 最大回転拘束[Algorithm 1]
  99. /// </summary>
  100. public ClampRotationConstraint.ClampRotationData[] clampRotationDataList;
  101. public ClampRotationConstraint.ClampRotationRootInfo[] clampRotationRootInfoList;
  102. /// <summary>
  103. /// 複合回転拘束(v1.11.0)
  104. /// </summary>
  105. public CompositeRotationConstraint.RotationData[] compositeRotationDataList;
  106. public CompositeRotationConstraint.RootInfo[] compositeRotationRootInfoList;
  107. /// <summary>
  108. /// ねじれ拘束
  109. /// </summary>
  110. public TwistConstraint.TwistData[] twistDataList;
  111. public ReferenceDataIndex[] twistReferenceList;
  112. /// <summary>
  113. /// 回転調整拘束
  114. /// このデータはモードがRotationLineの場合のみ必要
  115. /// またライン結合に対してのみ生成する
  116. /// </summary>
  117. public AdjustRotationWorker.AdjustRotationData[] adjustRotationDataList;
  118. /// <summary>
  119. /// トライアングルベンド拘束
  120. /// v2 +
  121. /// /|\
  122. /// v0 + | + v1
  123. /// \|/
  124. /// v3 +
  125. /// </summary>
  126. public TriangleBendConstraint.TriangleBendData[] triangleBendDataList;
  127. public ReferenceDataIndex[] triangleBendReferenceList;
  128. public int triangleBendWriteBufferCount;
  129. /// <summary>
  130. /// ボリューム拘束(テトラを形成する4頂点の情報)
  131. /// </summary>
  132. public VolumeConstraint.VolumeData[] volumeDataList;
  133. public ReferenceDataIndex[] volumeReferenceList;
  134. public int volumeWriteBufferCount;
  135. /// <summary>
  136. /// ライン回転調整
  137. /// </summary>
  138. public LineWorker.LineRotationData[] lineRotationDataList;
  139. public LineWorker.LineRotationRootInfo[] lineRotationRootInfoList;
  140. /// <summary>
  141. /// トライアングル回転調整
  142. /// </summary>
  143. public TriangleWorker.TriangleRotationData[] triangleRotationDataList;
  144. public int[] triangleRotationIndexList;
  145. /// <summary>
  146. /// エッジコリジョン拘束
  147. /// </summary>
  148. public EdgeCollisionConstraint.EdgeCollisionData[] edgeCollisionDataList;
  149. public ReferenceDataIndex[] edgeCollisionReferenceList;
  150. public int edgeCollisionWriteBufferCount;
  151. /// <summary>
  152. /// 浸透制限データ
  153. /// </summary>
  154. public PenetrationConstraint.PenetrationData[] penetrationDataList;
  155. public ReferenceDataIndex[] penetrationReferenceList;
  156. public float3[] penetrationDirectionDataList;
  157. public ClothParams.PenetrationMode penetrationMode; // データの型
  158. /// <summary>
  159. /// ベーススキニングデータ
  160. /// </summary>
  161. public BaseSkinningWorker.BaseSkinningData[] baseSkinningDataList;
  162. public float4x4[] baseSkinningBindPoseList;
  163. /// <summary>
  164. /// 設計時のスケール(WorldInfluenceのInfluenceTargetが基準)
  165. /// </summary>
  166. public Vector3 initScale;
  167. //=========================================================================================
  168. /// <summary>
  169. /// データハッシュ計算
  170. /// </summary>
  171. /// <returns></returns>
  172. public override int GetDataHash()
  173. {
  174. int hash = 0;
  175. hash += triangleBendAlgorithm.GetDataHash();
  176. hash += restoreRotationAlgorithm.GetDataHash();
  177. hash += clampRotationAlgorithm.GetDataHash();
  178. hash += VertexUseCount.GetDataHash();
  179. hash += selectionData.Count.GetDataHash();
  180. hash += maxLevel.GetDataHash();
  181. hash += vertexFlagLevelList.Count.GetDataHash();
  182. hash += vertexDepthList.Count.GetDataHash();
  183. hash += rootList.Count.GetDataHash();
  184. hash += parentList.Count.GetDataHash();
  185. hash += StructDistanceConstraintCount.GetDataHash();
  186. hash += BendDistanceConstraintCount.GetDataHash();
  187. hash += NearDistanceConstraintCount.GetDataHash();
  188. hash += ClampDistanceConstraintCount.GetDataHash();
  189. hash += ClampDistance2ConstraintCount.GetDataHash();
  190. hash += RestoreRotationConstraintCount.GetDataHash();
  191. hash += ClampRotationConstraintDataCount.GetDataHash();
  192. hash += AdjustRotationConstraintCount.GetDataHash();
  193. hash += TriangleBendConstraintCount.GetDataHash();
  194. hash += VolumeConstraintCount.GetDataHash();
  195. hash += LineRotationWorkerCount.GetDataHash();
  196. hash += TwistConstraintCount.GetDataHash();
  197. hash += initScale.GetDataHash();
  198. return hash;
  199. }
  200. //=========================================================================================
  201. /// <summary>
  202. /// 使用頂点数
  203. /// </summary>
  204. public int VertexUseCount
  205. {
  206. get
  207. {
  208. return useVertexList.Count;
  209. }
  210. }
  211. /// <summary>
  212. /// 距離復元拘束数(構造接続)
  213. /// </summary>
  214. public int StructDistanceConstraintCount
  215. {
  216. get
  217. {
  218. return structDistanceDataList != null ? structDistanceDataList.Length : 0;
  219. }
  220. }
  221. /// <summary>
  222. /// 距離復元拘束数(ベンド接続)
  223. /// </summary>
  224. public int BendDistanceConstraintCount
  225. {
  226. get
  227. {
  228. return bendDistanceDataList != null ? bendDistanceDataList.Length : 0;
  229. }
  230. }
  231. /// <summary>
  232. /// 距離復元拘束数(近接続)
  233. /// </summary>
  234. public int NearDistanceConstraintCount
  235. {
  236. get
  237. {
  238. return nearDistanceDataList != null ? nearDistanceDataList.Length : 0;
  239. }
  240. }
  241. /// <summary>
  242. /// ルート距離拘束数
  243. /// </summary>
  244. public int ClampDistanceConstraintCount
  245. {
  246. get
  247. {
  248. return rootDistanceDataList != null ? rootDistanceDataList.Length : 0;
  249. }
  250. }
  251. /// <summary>
  252. /// パーティクル最大最小距離拘束数
  253. /// </summary>
  254. public int ClampDistance2ConstraintCount
  255. {
  256. get
  257. {
  258. return clampDistance2DataList != null ? clampDistance2DataList.Length : 0;
  259. }
  260. }
  261. /// <summary>
  262. /// 回転復元拘束数
  263. /// </summary>
  264. public int RestoreRotationConstraintCount
  265. {
  266. get
  267. {
  268. return restoreRotationDataList != null ? restoreRotationDataList.Length : 0;
  269. }
  270. }
  271. /// <summary>
  272. /// 回転制限拘束数
  273. /// </summary>
  274. ///
  275. public int ClampRotationConstraintDataCount
  276. {
  277. get
  278. {
  279. return clampRotationDataList != null ? clampRotationDataList.Length : 0;
  280. }
  281. }
  282. /// <summary>
  283. /// 回転角度拘束数
  284. /// </summary>
  285. public int ClampRotationConstraintRootCount
  286. {
  287. get
  288. {
  289. return clampRotationRootInfoList != null ? clampRotationRootInfoList.Length : 0;
  290. }
  291. }
  292. /// <summary>
  293. /// 回転調整拘束数
  294. /// </summary>
  295. public int AdjustRotationConstraintCount
  296. {
  297. get
  298. {
  299. return adjustRotationDataList != null ? adjustRotationDataList.Length : 0;
  300. }
  301. }
  302. /// <summary>
  303. /// 複合回転拘束数
  304. /// </summary>
  305. public int CompositeRotationCount
  306. {
  307. get
  308. {
  309. return compositeRotationDataList != null ? compositeRotationDataList.Length : 0;
  310. }
  311. }
  312. /// <summary>
  313. /// ねじれ補正拘束数
  314. /// </summary>
  315. public int TwistConstraintCount
  316. {
  317. get
  318. {
  319. return twistDataList != null ? twistDataList.Length : 0;
  320. }
  321. }
  322. /// <summary>
  323. /// トライアングルベンド拘束数
  324. /// </summary>
  325. public int TriangleBendConstraintCount
  326. {
  327. get
  328. {
  329. return triangleBendDataList != null ? triangleBendDataList.Length : 0;
  330. }
  331. }
  332. public int EdgeCollisionConstraintCount
  333. {
  334. get
  335. {
  336. return edgeCollisionDataList != null ? edgeCollisionDataList.Length : 0;
  337. }
  338. }
  339. /// <summary>
  340. /// ボリューム拘束数
  341. /// </summary>
  342. public int VolumeConstraintCount
  343. {
  344. get
  345. {
  346. return volumeDataList != null ? volumeDataList.Length : 0;
  347. }
  348. }
  349. /// <summary>
  350. /// ライン回転調整数
  351. /// </summary>
  352. public int LineRotationWorkerCount
  353. {
  354. get
  355. {
  356. return lineRotationDataList != null ? lineRotationDataList.Length : 0;
  357. }
  358. }
  359. /// <summary>
  360. /// トライアングル回転調整数
  361. /// </summary>
  362. /// <value></value>
  363. public int TriangleRotationWorkerCount
  364. {
  365. get
  366. {
  367. return triangleRotationDataList != null ? triangleRotationDataList.Length : 0;
  368. }
  369. }
  370. /// <summary>
  371. /// 浸透制限データ数
  372. /// </summary>
  373. public int PenetrationCount
  374. {
  375. get
  376. {
  377. if (penetrationDataList != null && penetrationDataList.Length > 0)
  378. return penetrationDataList.Length;
  379. if (penetrationDirectionDataList != null && penetrationDirectionDataList.Length > 0)
  380. return penetrationDirectionDataList.Length;
  381. return 0;
  382. //return penetrationDataList != null ? penetrationDataList.Length : 0;
  383. }
  384. }
  385. /// <summary>
  386. /// ベーススキニングデータ数
  387. /// </summary>
  388. public int BaseSkinningCount
  389. {
  390. get
  391. {
  392. return baseSkinningDataList != null ? baseSkinningDataList.Length : 0;
  393. }
  394. }
  395. /// <summary>
  396. /// 現在使用中アルゴリズムのClampRotationデータ数
  397. /// </summary>
  398. /// <returns></returns>
  399. public int GetClampRotationCount()
  400. {
  401. switch (clampRotationAlgorithm)
  402. {
  403. case ClothParams.Algorithm.Algorithm_1:
  404. return ClampRotationConstraintDataCount;
  405. case ClothParams.Algorithm.Algorithm_2:
  406. return CompositeRotationCount;
  407. default:
  408. return 0;
  409. }
  410. }
  411. /// <summary>
  412. /// 現在使用中アルゴリズムのRestoreRotationデータ数
  413. /// </summary>
  414. /// <returns></returns>
  415. public int GetRestoreRotationCount()
  416. {
  417. switch (restoreRotationAlgorithm)
  418. {
  419. case ClothParams.Algorithm.Algorithm_1:
  420. return RestoreRotationConstraintCount;
  421. case ClothParams.Algorithm.Algorithm_2:
  422. return CompositeRotationCount;
  423. default:
  424. return 0;
  425. }
  426. }
  427. /// <summary>
  428. /// 選択頂点が無効か判定
  429. /// </summary>
  430. /// <param name="vindex"></param>
  431. /// <returns></returns>
  432. public bool IsInvalidVertex(int vindex)
  433. {
  434. Debug.Assert(selectionData != null && vindex < selectionData.Count);
  435. return selectionData[vindex] == SelectionData.Invalid;
  436. }
  437. /// <summary>
  438. /// 選択頂点が固定か判定
  439. /// </summary>
  440. /// <param name="vindex"></param>
  441. /// <returns></returns>
  442. public bool IsFixedVertex(int vindex)
  443. {
  444. Debug.Assert(selectionData != null && vindex < selectionData.Count);
  445. return selectionData[vindex] == SelectionData.Fixed;
  446. }
  447. /// <summary>
  448. /// 選択頂点が移動か判定
  449. /// </summary>
  450. /// <param name="vindex"></param>
  451. /// <returns></returns>
  452. public bool IsMoveVertex(int vindex)
  453. {
  454. Debug.Assert(selectionData != null && vindex < selectionData.Count);
  455. return selectionData[vindex] == SelectionData.Move;
  456. }
  457. /// <summary>
  458. /// 選択頂点が拡張か判定
  459. /// </summary>
  460. /// <param name="vindex"></param>
  461. /// <returns></returns>
  462. public bool IsExtendVertex(int vindex)
  463. {
  464. Debug.Assert(selectionData != null && vindex < selectionData.Count);
  465. return selectionData[vindex] == SelectionData.Extend;
  466. }
  467. /// <summary>
  468. /// 指定頂点が末端か判定
  469. /// </summary>
  470. /// <param name="vindex"></param>
  471. /// <returns></returns>
  472. public bool IsLastLevel(int vindex)
  473. {
  474. return IsFlag(vindex, VertexFlag_End);
  475. }
  476. /// <summary>
  477. /// 頂点フラグ取得
  478. /// </summary>
  479. /// <param name="vindex"></param>
  480. /// <param name="flag"></param>
  481. /// <returns></returns>
  482. public bool IsFlag(int vindex, uint flag)
  483. {
  484. return (vertexFlagLevelList[vindex] & flag) != 0;
  485. }
  486. /// <summary>
  487. /// 頂点フラグ設定
  488. /// </summary>
  489. /// <param name="vindex"></param>
  490. /// <param name="flag"></param>
  491. public void SetFlag(int vindex, uint flag)
  492. {
  493. vertexFlagLevelList[vindex] |= flag;
  494. }
  495. /// <summary>
  496. /// 頂点レベル取得
  497. /// </summary>
  498. /// <param name="vindex"></param>
  499. /// <returns></returns>
  500. public int GetLevel(int vindex)
  501. {
  502. return (int)(vertexFlagLevelList[vindex] & 0xffff);
  503. }
  504. //=========================================================================================
  505. public override int GetVersion()
  506. {
  507. return DATA_VERSION;
  508. }
  509. /// <summary>
  510. /// 現在のデータが正常(実行できる状態)か返す
  511. /// </summary>
  512. /// <returns></returns>
  513. public override Define.Error VerifyData()
  514. {
  515. if (dataHash == 0)
  516. return Define.Error.InvalidDataHash;
  517. //if (dataVersion != GetVersion())
  518. // return Define.Error.DataVersionMismatch;
  519. int vcnt = VertexUseCount;
  520. if (vcnt == 0)
  521. return Define.Error.VertexUseCountZero;
  522. if (selectionData.Count == 0 || selectionData.Count != vcnt)
  523. return Define.Error.SelectionDataCountMismatch;
  524. if (vertexFlagLevelList.Count == 0 || vertexFlagLevelList.Count != vcnt)
  525. return Define.Error.VertexCountMismatch;
  526. if (vertexDepthList.Count == 0 || vertexDepthList.Count != vcnt)
  527. return Define.Error.VertexCountMismatch;
  528. if (rootList.Count == 0 || rootList.Count != vcnt)
  529. return Define.Error.RootListCountMismatch;
  530. return Define.Error.None;
  531. }
  532. //=========================================================================================
  533. // 作業用
  534. private class RestoreDistanceWork
  535. {
  536. public uint vertexPair;
  537. public float dist;
  538. }
  539. private class PenetrationBone
  540. {
  541. public Transform bone;
  542. public Transform childBone;
  543. }
  544. private class PenetrationWork
  545. {
  546. public Transform bone;
  547. public Transform childBone;
  548. public int boneIndex;
  549. public float distance;
  550. public float weight;
  551. }
  552. //=========================================================================================
  553. /// <summary>
  554. /// クロスデータの作成
  555. /// </summary>
  556. /// <param name="teamData"></param>
  557. /// <param name="editMesh"></param>
  558. public void CreateData(
  559. PhysicsTeam team,
  560. ClothParams clothParams,
  561. PhysicsTeamData teamData,
  562. MeshData meshData,
  563. IEditorMesh editMesh,
  564. List<int> selData,
  565. System.Action<List<int>, List<int>, List<Vector3>, List<Vector3>, List<Vector3>, List<int>, List<int>> extensionAction = null
  566. )
  567. {
  568. Debug.Assert(teamData != null);
  569. Debug.Assert(editMesh != null);
  570. // メッシュデータ
  571. int vertexCount = 0;
  572. List<Vector3> wposList;
  573. List<Vector3> wnorList;
  574. List<Vector3> wtanList;
  575. List<int> triangleList;
  576. List<int> lineList;
  577. vertexCount = editMesh.GetEditorPositionNormalTangent(out wposList, out wnorList, out wtanList);
  578. triangleList = editMesh.GetEditorTriangleList();
  579. lineList = editMesh.GetEditorLineList();
  580. // 使用頂点データ作成
  581. useVertexList.Clear();
  582. Debug.Assert(vertexCount == selData.Count);
  583. for (int i = 0; i < vertexCount; i++)
  584. {
  585. if (selData[i] != SelectionData.Invalid)
  586. {
  587. useVertexList.Add(i);
  588. selectionData.Add(selData[i]);
  589. }
  590. }
  591. // 頂点データの拡張(オプション)
  592. if (extensionAction != null)
  593. {
  594. extensionAction(useVertexList, selectionData, wposList, wnorList, wtanList, triangleList, lineList);
  595. vertexCount = wposList.Count;
  596. }
  597. // 使用アルゴリズム
  598. triangleBendAlgorithm = clothParams.AlgorithmType;
  599. restoreRotationAlgorithm = clothParams.AlgorithmType;
  600. clampRotationAlgorithm = clothParams.AlgorithmType;
  601. // 頂点データ作成
  602. CreateVertexData(vertexCount, lineList, triangleList);
  603. // 拘束データ作成
  604. CreateConstraintData(team, clothParams, teamData, vertexCount, wposList, wnorList, wtanList, lineList, triangleList);
  605. // データ検証とハッシュ
  606. CreateVerifyData();
  607. }
  608. /// <summary>
  609. /// 頂点データ作成
  610. /// </summary>
  611. /// <param name="vertexCount"></param>
  612. /// <param name="wposList"></param>
  613. /// <param name="wrotList"></param>
  614. /// <param name="triangleList"></param>
  615. void CreateVertexData(
  616. int vertexCount,
  617. List<int> lineList,
  618. List<int> triangleList
  619. )
  620. {
  621. vertexDepthList.Clear();
  622. vertexFlagLevelList.Clear();
  623. maxLevel = 0;
  624. if (vertexCount == 0)
  625. {
  626. return;
  627. }
  628. float[] depthList = new float[vertexCount]; // 0で初期化
  629. uint[] levelList = new uint[vertexCount];
  630. // ライン/トライアングル情報を分解して各頂点の接続頂点をリスト化
  631. List<HashSet<int>> vlink = MeshUtility.GetTriangleToVertexLinkList(vertexCount, lineList, triangleList);
  632. // 固定頂点を抜粋する
  633. uint lv = 1;
  634. HashSet<int> processedSet = new HashSet<int>();
  635. for (int i = 0; i < vertexCount; i++)
  636. {
  637. int index = useVertexList.IndexOf(i);
  638. if (index < 0)
  639. {
  640. // 無効頂点
  641. processedSet.Add(i);
  642. }
  643. else if (IsInvalidVertex(index) || IsExtendVertex(index))
  644. {
  645. // 無効頂点、拡張頂点
  646. processedSet.Add(i);
  647. }
  648. else if (IsFixedVertex(index))
  649. {
  650. // 固定頂点
  651. processedSet.Add(i);
  652. levelList[i] = lv;
  653. }
  654. }
  655. // 階層
  656. int hitcnt;
  657. do
  658. {
  659. hitcnt = 0;
  660. for (int i = 0; i < vertexCount; i++)
  661. {
  662. if (processedSet.Contains(i))
  663. continue;
  664. var vlist = vlink[i];
  665. foreach (var vindex in vlist)
  666. {
  667. if (levelList[vindex] == lv)
  668. {
  669. // 次のレベルへ設定する
  670. levelList[i] = lv + 1;
  671. processedSet.Add(i);
  672. hitcnt++;
  673. break;
  674. }
  675. }
  676. }
  677. if (hitcnt > 0)
  678. lv++;
  679. }
  680. while (hitcnt > 0);
  681. //Debug.Log("max level->" + lv);
  682. // デプス設定
  683. if (lv > 1)
  684. {
  685. for (int i = 0; i < vertexCount; i++)
  686. {
  687. uint level = levelList[i];
  688. if (level > 0)
  689. {
  690. depthList[i] = Mathf.Clamp01((float)(level - 1) / (float)(lv - 1));
  691. }
  692. }
  693. }
  694. foreach (var vindex in useVertexList)
  695. {
  696. vertexDepthList.Add(depthList[vindex]);
  697. vertexFlagLevelList.Add(levelList[vindex]);
  698. }
  699. maxLevel = lv;
  700. }
  701. /// <summary>
  702. /// 拘束データ作成
  703. /// </summary>
  704. /// <param name="scr"></param>
  705. /// <param name="vertexCount"></param>
  706. /// <param name="wposList"></param>
  707. /// <param name="wrotList"></param>
  708. /// <param name="triangleList"></param>
  709. void CreateConstraintData(
  710. PhysicsTeam team,
  711. ClothParams clothParams,
  712. PhysicsTeamData teamData,
  713. int vertexCount,
  714. List<Vector3> wposList,
  715. List<Vector3> wnorList,
  716. List<Vector3> wtanList,
  717. List<int> lineList,
  718. List<int> triangleList
  719. //List<int> tetraList,
  720. //List<float> tetraSizeList
  721. )
  722. {
  723. parentList.Clear();
  724. rootList.Clear();
  725. structDistanceDataList = null;
  726. structDistanceReferenceList = null;
  727. bendDistanceDataList = null;
  728. bendDistanceReferenceList = null;
  729. nearDistanceDataList = null;
  730. nearDistanceReferenceList = null;
  731. rootDistanceDataList = null;
  732. rootDistanceReferenceList = null;
  733. restoreRotationDataList = null;
  734. restoreRotationReferenceList = null;
  735. adjustRotationDataList = null;
  736. triangleBendDataList = null;
  737. triangleBendReferenceList = null;
  738. triangleBendWriteBufferCount = 0;
  739. volumeDataList = null;
  740. volumeReferenceList = null;
  741. volumeWriteBufferCount = 0;
  742. clampRotationDataList = null;
  743. clampRotationRootInfoList = null;
  744. lineRotationDataList = null;
  745. lineRotationRootInfoList = null;
  746. triangleRotationDataList = null;
  747. triangleRotationIndexList = null;
  748. edgeCollisionDataList = null;
  749. edgeCollisionReferenceList = null;
  750. edgeCollisionWriteBufferCount = 0;
  751. twistDataList = null;
  752. compositeRotationDataList = null;
  753. compositeRotationRootInfoList = null;
  754. penetrationDataList = null;
  755. penetrationReferenceList = null;
  756. penetrationDirectionDataList = null;
  757. //teamSelfCollisionDataList = null;
  758. if (vertexCount == 0)
  759. return;
  760. // 設計時スケール
  761. Transform influenceTarget = clothParams.GetInfluenceTarget() ? clothParams.GetInfluenceTarget() : team.transform;
  762. initScale = influenceTarget.lossyScale;
  763. // ライン/トライアングル情報を分解して各頂点の接続頂点をリスト化
  764. List<HashSet<int>> meshVLink = MeshUtility.GetTriangleToVertexLinkList(vertexCount, lineList, triangleList);
  765. // 使用頂点ベースの接続頂点情報
  766. List<HashSet<int>> useVLink = new List<HashSet<int>>();
  767. for (int i = 0; i < useVertexList.Count; i++)
  768. {
  769. int vindex0 = useVertexList[i];
  770. HashSet<int> hset = new HashSet<int>();
  771. foreach (var vindex1 in meshVLink[vindex0])
  772. {
  773. int ui = useVertexList.IndexOf(vindex1);
  774. if (ui >= 0)
  775. {
  776. hset.Add(ui);
  777. }
  778. }
  779. useVLink.Add(hset);
  780. }
  781. // トライアングルパックデータ
  782. //List<ulong> meshTrianglePackList = MeshUtility.GetTrianglePackList(triangleList);
  783. // 使用頂点の親頂点を計算してリスト化
  784. if (team is MagicaBoneCloth)
  785. {
  786. // BoneClothではLine(Transform)接続情報のみから親情報を計算する
  787. var meshLLink = MeshUtility.GetTriangleToVertexLinkList(vertexCount, lineList, null);
  788. parentList = GetUseParentVertexList(vertexCount, meshLLink, wposList, vertexDepthList);
  789. }
  790. else
  791. {
  792. parentList = GetUseParentVertexList(vertexCount, meshVLink, wposList, vertexDepthList);
  793. //parentList = GetUseParentVertexList_Old(vertexCount, meshVLink, wposList, vertexDepthList);
  794. }
  795. // 末端頂点の判定
  796. for (int i = 0; i < useVertexList.Count; i++)
  797. {
  798. if (parentList.Contains(i) == false && selectionData[i] != SelectionData.Extend)
  799. {
  800. // この頂点は末端
  801. SetFlag(i, VertexFlag_End);
  802. }
  803. }
  804. // 使用頂点のルート頂点を計算してリスト化
  805. rootList = GetUseRootVertexList(parentList);
  806. // メッシュ頂点リストに対応するデプス値
  807. List<float> meshVertexDepthList = GetMeshVertexDepthList(vertexCount, vertexDepthList);
  808. // 使用頂点ベースのルートから続く一連の頂点リスト
  809. List<List<int>> rootLineList = GetUseRootLineList(parentList);
  810. // 重力方向減衰
  811. //var directionalDampingTarget = clothParams.DirectionalDampingObject ? clothParams.DirectionalDampingObject : team.transform;
  812. //directionalDampingUpDir = directionalDampingTarget.InverseTransformDirection(Vector3.up);
  813. // スキニング情報の作成(必要な場合のみ)
  814. // 一旦休眠
  815. //(var baseSkinningData, var bindPoses, var penetrationDirections) = CreateSkinningData(team, teamData, clothParams, wposList, wnorList, wtanList);
  816. // 構造距離拘束リスト作成(構造接続)
  817. var restoreDistanceData = new List<RestoreDistanceConstraint.RestoreDistanceData>();
  818. HashSet<uint> distSet = new HashSet<uint>();
  819. for (int i = 0; i < useVertexList.Count; i++)
  820. {
  821. if (IsInvalidVertex(i) || IsExtendVertex(i))
  822. continue;
  823. int vindex0 = useVertexList[i];
  824. var vlist = meshVLink[vindex0];
  825. foreach (var vindex in vlist)
  826. {
  827. int vi = useVertexList.IndexOf(vindex);
  828. if (vi < 0)
  829. continue;
  830. if (IsInvalidVertex(vi) || IsExtendVertex(vi))
  831. continue;
  832. if ((IsFixedVertex(i) || IsExtendVertex(i)) && (IsFixedVertex(vi) || IsExtendVertex(vi)))
  833. continue;
  834. uint pack = DataUtility.PackPair(i, vi);
  835. if (distSet.Contains(pack))
  836. continue;
  837. // 登録
  838. float dist = Vector3.Distance(wposList[vindex0], wposList[vindex]);
  839. // (1)
  840. var data = new RestoreDistanceConstraint.RestoreDistanceData();
  841. data.vertexIndex = (ushort)i;
  842. data.targetVertexIndex = (ushort)vi;
  843. data.length = dist;
  844. restoreDistanceData.Add(data);
  845. // (2)
  846. data = new RestoreDistanceConstraint.RestoreDistanceData();
  847. data.vertexIndex = (ushort)vi;
  848. data.targetVertexIndex = (ushort)i;
  849. data.length = dist;
  850. restoreDistanceData.Add(data);
  851. distSet.Add(pack);
  852. }
  853. }
  854. if (restoreDistanceData.Count > 0)
  855. {
  856. // ジョブシステム用にデータを加工して登録
  857. var builder = new ReferenceDataBuilder<RestoreDistanceConstraint.RestoreDistanceData>();
  858. builder.Init(useVertexList.Count);
  859. foreach (var data in restoreDistanceData)
  860. {
  861. builder.AddData(data, data.vertexIndex);
  862. }
  863. (var refDataList, var dataList) = builder.GetDirectReferenceData();
  864. this.structDistanceDataList = dataList.ToArray();
  865. this.structDistanceReferenceList = refDataList.ToArray();
  866. }
  867. // 距離拘束リスト作成(ベンド接続)
  868. if (clothParams.UseBendDistance)
  869. {
  870. restoreDistanceData.Clear();
  871. for (int i = 0; i < useVertexList.Count; i++)
  872. {
  873. if (IsInvalidVertex(i) || IsExtendVertex(i))
  874. continue;
  875. int vindex0 = useVertexList[i];
  876. var pos0 = wposList[vindex0];
  877. // vindex0から接続されるすべての頂点を総当りで連結する
  878. List<RestoreDistanceWork> bendList = new List<RestoreDistanceWork>();
  879. var vlist = new List<int>(meshVLink[vindex0]);
  880. for (int j = 0; j < vlist.Count - 1; j++)
  881. {
  882. int vindex1 = vlist[j];
  883. int vi1 = useVertexList.IndexOf(vindex1);
  884. if (vi1 < 0)
  885. continue;
  886. if (IsInvalidVertex(vi1) || IsExtendVertex(vi1))
  887. continue;
  888. for (int k = j + 1; k < vlist.Count; k++)
  889. {
  890. int vindex2 = vlist[k];
  891. int vi2 = useVertexList.IndexOf(vindex2);
  892. if (vi2 < 0)
  893. continue;
  894. if (IsInvalidVertex(vi2) || IsExtendVertex(vi2))
  895. continue;
  896. if ((IsFixedVertex(vi1) || IsExtendVertex(vi1)) && (IsFixedVertex(vi2) || IsExtendVertex(vi2)))
  897. continue;
  898. uint pack = DataUtility.PackPair(vi1, vi2);
  899. var work = new RestoreDistanceWork();
  900. work.vertexPair = pack;
  901. // ラインvindex1 - vindex2 と vindex0 の最近接点距離を求める
  902. var pos1 = wposList[vindex1];
  903. var pos2 = wposList[vindex2];
  904. float3 d = MathUtility.ClosestPtPointSegment(pos0, pos1, pos2);
  905. work.dist = math.distance(pos0, d);
  906. bendList.Add(work);
  907. }
  908. }
  909. // 距離の昇順でソート
  910. bendList.Sort((a, b) => a.dist < b.dist ? -1 : 1);
  911. // 最大BendDistanceMaxCountまでを接続する
  912. for (int j = 0; j < bendList.Count && j < clothParams.BendDistanceMaxCount; j++)
  913. {
  914. var work = bendList[j];
  915. // すでに登録済みならば無視
  916. if (distSet.Contains(work.vertexPair))
  917. continue;
  918. int vi1, vi2;
  919. DataUtility.UnpackPair(work.vertexPair, out vi1, out vi2);
  920. int vindex1 = useVertexList[vi1];
  921. int vindex2 = useVertexList[vi2];
  922. // 登録
  923. float dist = Vector3.Distance(wposList[vindex1], wposList[vindex2]);
  924. // (1)
  925. var data = new RestoreDistanceConstraint.RestoreDistanceData();
  926. data.vertexIndex = (ushort)vi1;
  927. data.targetVertexIndex = (ushort)vi2;
  928. data.length = dist;
  929. restoreDistanceData.Add(data);
  930. // (2)
  931. data = new RestoreDistanceConstraint.RestoreDistanceData();
  932. data.vertexIndex = (ushort)vi2;
  933. data.targetVertexIndex = (ushort)vi1;
  934. data.length = dist;
  935. restoreDistanceData.Add(data);
  936. distSet.Add(work.vertexPair);
  937. }
  938. }
  939. if (restoreDistanceData.Count > 0)
  940. {
  941. // ジョブシステム用にデータを加工して登録
  942. var builder = new ReferenceDataBuilder<RestoreDistanceConstraint.RestoreDistanceData>();
  943. builder.Init(useVertexList.Count);
  944. foreach (var data in restoreDistanceData)
  945. {
  946. builder.AddData(data, data.vertexIndex);
  947. }
  948. (var refDataList, var dataList) = builder.GetDirectReferenceData();
  949. this.bendDistanceDataList = dataList.ToArray();
  950. this.bendDistanceReferenceList = refDataList.ToArray();
  951. }
  952. }
  953. // 距離拘束リスト作成(近接続)
  954. if (clothParams.UseNearDistance)
  955. {
  956. restoreDistanceData.Clear();
  957. for (int i = 0; i < useVertexList.Count; i++)
  958. {
  959. if (IsMoveVertex(i) == false)
  960. continue;
  961. int vindex0 = useVertexList[i];
  962. var wpos0 = wposList[vindex0];
  963. float depth = vertexDepthList[i];
  964. // デプス判定
  965. if (vertexDepthList[i] > clothParams.NearDistanceMaxDepth)
  966. continue;
  967. List<RestoreDistanceWork> linkList = new List<RestoreDistanceWork>();
  968. var useVinfo = useVLink[i];
  969. for (int j = 0; j < useVertexList.Count; j++)
  970. {
  971. if (i == j)
  972. continue;
  973. // すでに接続されている点は無視
  974. if (useVinfo.Contains(j))
  975. continue;
  976. // デプス判定
  977. if (vertexDepthList[j] > clothParams.NearDistanceMaxDepth)
  978. continue;
  979. uint pack = DataUtility.PackPair(i, j);
  980. if (distSet.Contains(pack))
  981. continue;
  982. // 距離判定
  983. int vindex1 = useVertexList[j];
  984. var wpos1 = wposList[vindex1];
  985. float dist = Vector3.Distance(wpos0, wpos1);
  986. if (dist <= clothParams.GetNearDistanceLength().Evaluate(vertexDepthList[i]))
  987. {
  988. var work = new RestoreDistanceWork();
  989. work.vertexPair = pack;
  990. work.dist = dist;
  991. linkList.Add(work);
  992. }
  993. }
  994. // 距離の昇順でソート
  995. linkList.Sort((a, b) => a.dist < b.dist ? -1 : 1);
  996. // 最大NearDistanceMaxCountまでを接続する
  997. for (int j = 0; j < linkList.Count && j < clothParams.NearDistanceMaxCount; j++)
  998. {
  999. var work = linkList[j];
  1000. // すでに登録済みならば無視
  1001. if (distSet.Contains(work.vertexPair))
  1002. continue;
  1003. int i1, i2;
  1004. DataUtility.UnpackPair(work.vertexPair, out i1, out i2);
  1005. // 接続する
  1006. // (1)
  1007. var data = new RestoreDistanceConstraint.RestoreDistanceData();
  1008. data.vertexIndex = (ushort)i1;
  1009. data.targetVertexIndex = (ushort)i2;
  1010. data.length = work.dist;
  1011. restoreDistanceData.Add(data);
  1012. // (2)
  1013. data = new RestoreDistanceConstraint.RestoreDistanceData();
  1014. data.vertexIndex = (ushort)i2;
  1015. data.targetVertexIndex = (ushort)i1;
  1016. data.length = work.dist;
  1017. restoreDistanceData.Add(data);
  1018. distSet.Add(work.vertexPair);
  1019. }
  1020. }
  1021. if (restoreDistanceData.Count > 0)
  1022. {
  1023. // ジョブシステム用にデータを加工して登録
  1024. var builder = new ReferenceDataBuilder<RestoreDistanceConstraint.RestoreDistanceData>();
  1025. builder.Init(useVertexList.Count);
  1026. foreach (var data in restoreDistanceData)
  1027. {
  1028. builder.AddData(data, data.vertexIndex);
  1029. }
  1030. (var refDataList, var dataList) = builder.GetDirectReferenceData();
  1031. this.nearDistanceDataList = dataList.ToArray();
  1032. this.nearDistanceReferenceList = refDataList.ToArray();
  1033. }
  1034. }
  1035. #if true
  1036. // ルート距離拘束
  1037. var rootDistanceData = new List<ClampDistanceConstraint.ClampDistanceData>();
  1038. if (clothParams.UseClampDistanceRatio)
  1039. {
  1040. for (int i = 0; i < useVertexList.Count; i++)
  1041. {
  1042. if (IsInvalidVertex(i))
  1043. continue;
  1044. if (IsMoveVertex(i) == false)
  1045. continue;
  1046. if (rootList[i] >= 0)
  1047. {
  1048. int vindex = useVertexList[i];
  1049. int rvindex = useVertexList[rootList[i]];
  1050. float dist = Vector3.Distance(wposList[vindex], wposList[rvindex]);
  1051. // 登録
  1052. var data = new ClampDistanceConstraint.ClampDistanceData();
  1053. data.vertexIndex = (ushort)i;
  1054. data.targetVertexIndex = (ushort)rootList[i];
  1055. data.length = dist;
  1056. rootDistanceData.Add(data);
  1057. }
  1058. }
  1059. }
  1060. if (rootDistanceData.Count > 0)
  1061. {
  1062. // ジョブシステム用にデータを加工して登録
  1063. var builder = new ReferenceDataBuilder<ClampDistanceConstraint.ClampDistanceData>();
  1064. builder.Init(useVertexList.Count);
  1065. foreach (var data in rootDistanceData)
  1066. {
  1067. builder.AddData(data, data.vertexIndex);
  1068. }
  1069. (var refDataList, var dataList) = builder.GetDirectReferenceData();
  1070. this.rootDistanceDataList = dataList.ToArray();
  1071. this.rootDistanceReferenceList = refDataList.ToArray();
  1072. }
  1073. #endif
  1074. #if false
  1075. // ※実験の結果一長一短のため今は不採用
  1076. // パーティクル最大最小距離拘束(v1.8.0)
  1077. // (ルートラインごと)
  1078. var clampDistance2Data = new List<ClampDistance2Constraint.ClampDistance2Data>();
  1079. var clampDistance2RootInfo = new List<ClampDistance2Constraint.ClampDistance2RootInfo>();
  1080. if (clothParams.UseClampDistanceRatio)
  1081. {
  1082. foreach (var lineIndexList in rootLineList)
  1083. {
  1084. if (lineIndexList.Count <= 1)
  1085. continue;
  1086. var info = new ClampDistance2Constraint.ClampDistance2RootInfo();
  1087. info.startIndex = (ushort)clampDistance2Data.Count;
  1088. int dataCnt = 0;
  1089. for (int i = 0; i < lineIndexList.Count; i++)
  1090. {
  1091. int index = lineIndexList[i];
  1092. int pindex = parentList[index];
  1093. if(pindex < 0)
  1094. continue;
  1095. var data = new ClampDistance2Constraint.ClampDistance2Data();
  1096. data.vertexIndex = index;
  1097. data.parentVertexIndex = pindex;
  1098. int vindex = useVertexList[index];
  1099. int pvindex = useVertexList[pindex];
  1100. Vector3 v = wposList[pvindex] - wposList[vindex];
  1101. data.length = v.magnitude;
  1102. clampDistance2Data.Add(data);
  1103. dataCnt++;
  1104. }
  1105. if (dataCnt > 0)
  1106. {
  1107. info.dataLength = (ushort)dataCnt;
  1108. clampDistance2RootInfo.Add(info);
  1109. }
  1110. }
  1111. }
  1112. if (clampDistance2Data.Count > 0)
  1113. {
  1114. this.clampDistance2DataList = clampDistance2Data.ToArray();
  1115. this.clampDistance2RootInfoList = clampDistance2RootInfo.ToArray();
  1116. }
  1117. #endif
  1118. // 回転復元拘束[Algorithm 1]
  1119. // (親ラインに対して)
  1120. if (restoreRotationAlgorithm == ClothParams.Algorithm.Algorithm_1)
  1121. {
  1122. var restoreRotationData = new List<RestoreRotationConstraint.RotationData>();
  1123. if (clothParams.UseRestoreRotation)
  1124. {
  1125. for (int i = 0; i < useVertexList.Count; i++)
  1126. {
  1127. if (IsMoveVertex(i) == false)
  1128. continue;
  1129. int pindex = parentList[i];
  1130. if (pindex >= 0)
  1131. {
  1132. int vindex = useVertexList[i];
  1133. int pvindex = useVertexList[pindex];
  1134. Vector3 v = wposList[vindex] - wposList[pvindex];
  1135. var q = Quaternion.LookRotation(wnorList[pvindex], wtanList[pvindex]);
  1136. var iq = Quaternion.Inverse(q);
  1137. var lpos = iq * v;
  1138. // 登録
  1139. var data = new RestoreRotationConstraint.RotationData();
  1140. data.vertexIndex = (ushort)i;
  1141. data.targetVertexIndex = (ushort)pindex;
  1142. data.localPos = lpos;
  1143. restoreRotationData.Add(data);
  1144. }
  1145. }
  1146. }
  1147. if (restoreRotationData.Count > 0)
  1148. {
  1149. // ジョブシステム用にデータを加工して登録
  1150. var builder = new ReferenceDataBuilder<RestoreRotationConstraint.RotationData>();
  1151. builder.Init(useVertexList.Count);
  1152. foreach (var data in restoreRotationData)
  1153. {
  1154. builder.AddData(data, data.vertexIndex);
  1155. }
  1156. (var refDataList, var dataList) = builder.GetDirectReferenceData();
  1157. this.restoreRotationDataList = dataList.ToArray();
  1158. this.restoreRotationReferenceList = refDataList.ToArray();
  1159. }
  1160. }
  1161. // 回転角度拘束[Algorithm 1]
  1162. // (ルートラインごと)
  1163. if (clothParams.AlgorithmType == ClothParams.Algorithm.Algorithm_1)
  1164. {
  1165. var clampRotationData = new List<ClampRotationConstraint.ClampRotationData>();
  1166. var clampRotationRootInfo = new List<ClampRotationConstraint.ClampRotationRootInfo>();
  1167. if (clothParams.UseClampRotation)
  1168. {
  1169. // Algorithm 1 (old style)
  1170. foreach (var lineIndexList in rootLineList)
  1171. {
  1172. if (lineIndexList.Count <= 1)
  1173. continue;
  1174. var info = new ClampRotationConstraint.ClampRotationRootInfo();
  1175. info.startIndex = (ushort)clampRotationData.Count;
  1176. info.dataLength = (ushort)lineIndexList.Count;
  1177. for (int i = 0; i < lineIndexList.Count; i++)
  1178. {
  1179. int index = lineIndexList[i];
  1180. int pindex = parentList[index];
  1181. var data = new ClampRotationConstraint.ClampRotationData();
  1182. data.vertexIndex = index;
  1183. data.parentVertexIndex = pindex;
  1184. if (pindex >= 0)
  1185. {
  1186. int vindex = useVertexList[index];
  1187. int pvindex = useVertexList[pindex];
  1188. Vector3 v = wposList[vindex] - wposList[pvindex];
  1189. v.Normalize();
  1190. var pq = Quaternion.LookRotation(wnorList[pvindex], wtanList[pvindex]);
  1191. var ipq = Quaternion.Inverse(pq);
  1192. Vector3 lpos = ipq * v;
  1193. var q = Quaternion.LookRotation(wnorList[vindex], wtanList[vindex]);
  1194. Quaternion lrot = ipq * q;
  1195. data.localPos = lpos;
  1196. data.localRot = lrot;
  1197. }
  1198. clampRotationData.Add(data);
  1199. }
  1200. clampRotationRootInfo.Add(info);
  1201. }
  1202. }
  1203. if (clampRotationData.Count > 0)
  1204. {
  1205. this.clampRotationDataList = clampRotationData.ToArray();
  1206. this.clampRotationRootInfoList = clampRotationRootInfo.ToArray();
  1207. }
  1208. }
  1209. // 複合回転拘束[Algorithm 2]
  1210. // ClampRotation + RestoreRotationの複合
  1211. // (ルートラインごと)
  1212. if (clothParams.AlgorithmType == ClothParams.Algorithm.Algorithm_2)
  1213. {
  1214. var compositeRotationData = new List<CompositeRotationConstraint.RotationData>();
  1215. var compositeRotationRootInfo = new List<CompositeRotationConstraint.RootInfo>();
  1216. if (clothParams.UseClampRotation || clothParams.UseRestoreRotation)
  1217. {
  1218. foreach (var lineIndexList in rootLineList)
  1219. {
  1220. if (lineIndexList.Count <= 1)
  1221. continue;
  1222. var info = new CompositeRotationConstraint.RootInfo();
  1223. info.startIndex = (ushort)compositeRotationData.Count;
  1224. int cnt = 0;
  1225. for (int i = 0; i < lineIndexList.Count; i++)
  1226. {
  1227. int index = lineIndexList[i];
  1228. int pindex = parentList[index];
  1229. if (pindex < 0)
  1230. continue;
  1231. var data = new CompositeRotationConstraint.RotationData();
  1232. data.vertexIndex = index;
  1233. data.parentVertexIndex = pindex;
  1234. int vindex = useVertexList[index];
  1235. int pvindex = useVertexList[pindex];
  1236. Vector3 v = wposList[vindex] - wposList[pvindex];
  1237. v.Normalize();
  1238. var pq = Quaternion.LookRotation(wnorList[pvindex], wtanList[pvindex]);
  1239. var ipq = Quaternion.Inverse(pq);
  1240. Vector3 lpos = ipq * v;
  1241. var q = Quaternion.LookRotation(wnorList[vindex], wtanList[vindex]);
  1242. Quaternion lrot = ipq * q;
  1243. data.localPos = lpos;
  1244. data.localRot = lrot;
  1245. compositeRotationData.Add(data);
  1246. cnt++;
  1247. }
  1248. if (cnt > 0)
  1249. {
  1250. info.dataLength = (ushort)cnt;
  1251. compositeRotationRootInfo.Add(info);
  1252. }
  1253. }
  1254. }
  1255. if (compositeRotationData.Count > 0)
  1256. {
  1257. this.compositeRotationDataList = compositeRotationData.ToArray();
  1258. this.compositeRotationRootInfoList = compositeRotationRootInfo.ToArray();
  1259. }
  1260. }
  1261. // ねじれ拘束[Algorithm 2]
  1262. if (clothParams.UseTriangleBend && clothParams.GetUseTwistCorrection(clothParams.AlgorithmType) && triangleList.Count > 0)
  1263. {
  1264. var twistData = new List<TwistConstraint.TwistData>();
  1265. for (int index0 = 0; index0 < useVertexList.Count; index0++)
  1266. {
  1267. if (IsMoveVertex(index0) == false)
  1268. continue;
  1269. int vindex0 = useVertexList[index0];
  1270. float depth0 = vertexDepthList[index0];
  1271. var vlist = meshVLink[vindex0];
  1272. foreach (var vindex1 in vlist)
  1273. {
  1274. int index1 = useVertexList.IndexOf(vindex1);
  1275. if (index1 < 0)
  1276. continue;
  1277. // デプスが同じか判定する
  1278. float depth1 = vertexDepthList[index1];
  1279. if (depth0 == depth1)
  1280. {
  1281. // この2つの頂点をねじれ復元として登録する
  1282. //Debug.Log($"Twist ({index0}-{index1})");
  1283. var data = new TwistConstraint.TwistData()
  1284. {
  1285. vertexIndex0 = (ushort)index0,
  1286. vertexIndex1 = (ushort)index1
  1287. };
  1288. twistData.Add(data);
  1289. }
  1290. }
  1291. }
  1292. if (twistData.Count > 0)
  1293. {
  1294. // ジョブシステム用にデータを加工して登録
  1295. var builder = new ReferenceDataBuilder<TwistConstraint.TwistData>();
  1296. builder.Init(useVertexList.Count);
  1297. foreach (var data in twistData)
  1298. {
  1299. builder.AddData(data, data.vertexIndex0);
  1300. }
  1301. (var refDataList, var dataList) = builder.GetDirectReferenceData();
  1302. this.twistDataList = dataList.ToArray();
  1303. this.twistReferenceList = refDataList.ToArray();
  1304. }
  1305. }
  1306. #if false
  1307. var twistData = new List<TwistConstraint.TwistData>();
  1308. if (clothParams.UseTriangleBend && clothParams.UseTriangleBendTwistCorrection && triangleList.Count > 0)
  1309. {
  1310. // エッジをキーとした隣接トライアングル
  1311. Dictionary<uint, List<int>> triangleEdgeDict = MeshUtility.GetTriangleEdgePair(triangleList);
  1312. int tcnt = triangleList.Count / 3;
  1313. for (int i = 0; i < tcnt; i++)
  1314. {
  1315. int index = i * 3;
  1316. int v0 = triangleList[index];
  1317. int v1 = triangleList[index + 1];
  1318. int v2 = triangleList[index + 2];
  1319. int i0 = useVertexList.IndexOf(v0);
  1320. int i1 = useVertexList.IndexOf(v1);
  1321. int i2 = useVertexList.IndexOf(v2);
  1322. if (i0 < 0 || i1 < 0 || i2 < 0)
  1323. continue;
  1324. if (IsMoveVertex(i0) == false && IsMoveVertex(i1) == false && IsMoveVertex(i2) == false)
  1325. continue;
  1326. uint edge01 = DataUtility.PackPair(v0, v1);
  1327. uint edge02 = DataUtility.PackPair(v0, v2);
  1328. uint edge12 = DataUtility.PackPair(v1, v2);
  1329. int edgeCnt01 = triangleEdgeDict[edge01].Count;
  1330. int edgeCnt02 = triangleEdgeDict[edge02].Count;
  1331. int edgeCnt12 = triangleEdgeDict[edge12].Count;
  1332. // v0
  1333. if (edgeCnt01 == 1 && edgeCnt02 == 1 && parentList[i1] == i0 && parentList[i2] == i0)
  1334. {
  1335. if (IsMoveVertex(i1) && IsMoveVertex(i2))
  1336. {
  1337. Debug.Log($"Twist: ({i0}-{i1}-{i2})");
  1338. var data = new TwistConstraint.TwistData()
  1339. {
  1340. vertexIndex0 = (ushort)i1,
  1341. vertexIndex1 = (ushort)i2
  1342. };
  1343. twistData.Add(data);
  1344. }
  1345. }
  1346. // v1
  1347. if (edgeCnt01 == 1 && edgeCnt12 == 1 && parentList[i0] == i1 && parentList[i2] == i1)
  1348. {
  1349. if (IsMoveVertex(i0) && IsMoveVertex(i2))
  1350. {
  1351. Debug.Log($"Twist: ({i1}-{i0}-{i2})");
  1352. var data = new TwistConstraint.TwistData()
  1353. {
  1354. vertexIndex0 = (ushort)i0,
  1355. vertexIndex1 = (ushort)i2
  1356. };
  1357. twistData.Add(data);
  1358. }
  1359. }
  1360. // v2
  1361. if (edgeCnt02 == 1 && edgeCnt12 == 1 && parentList[i0] == i2 && parentList[i1] == i2)
  1362. {
  1363. if (IsMoveVertex(i0) && IsMoveVertex(i1))
  1364. {
  1365. Debug.Log($"Twist: ({i2}-{i0}-{i1})");
  1366. var data = new TwistConstraint.TwistData()
  1367. {
  1368. vertexIndex0 = (ushort)i0,
  1369. vertexIndex1 = (ushort)i1
  1370. };
  1371. twistData.Add(data);
  1372. }
  1373. }
  1374. }
  1375. }
  1376. if (twistData.Count > 0)
  1377. {
  1378. this.twistDataList = twistData.ToArray();
  1379. }
  1380. #endif
  1381. // トライアングル回転調整(BoneClothのGrid接続時のみ)
  1382. var boneCloth = team as MagicaBoneCloth;
  1383. if (boneCloth != null && boneCloth.ClothTarget.IsMeshConnection && triangleList.Count > 0)
  1384. {
  1385. // これは計算の有無に関係なく全頂点分登録する
  1386. var triangleRotationData = new List<TriangleWorker.TriangleRotationData>();
  1387. var triangleRotationIndexData = new List<int>();
  1388. // 頂点(親子構造)情報リスト
  1389. var infoList = GetUseVertexInfoList(parentList);
  1390. // それほど数は多くないはずなので、とりあえず力技で構築
  1391. int tcnt = triangleList.Count / 3;
  1392. for (int i = 0; i < useVertexList.Count; i++)
  1393. {
  1394. int vindex = useVertexList[i];
  1395. var vinfo = infoList[i];
  1396. var data = new TriangleWorker.TriangleRotationData();
  1397. int useCount = 0;
  1398. int startIndex = triangleRotationIndexData.Count;
  1399. Vector3 nor = Vector3.zero;
  1400. // 接線ターゲット
  1401. // 親がいるなら親、居ない場合は子の1つ
  1402. int targetIndex = -1;
  1403. if (vinfo.parentVertexIndex >= 0)
  1404. {
  1405. targetIndex = vinfo.parentVertexIndex;
  1406. }
  1407. else if (vinfo.childVertexList.Count > 0)
  1408. {
  1409. targetIndex = vinfo.childVertexList[0];
  1410. }
  1411. // 接続トライアングル情報
  1412. if (targetIndex >= 0)
  1413. {
  1414. // 頂点が接続している頂点インデックスリスト
  1415. var linkIndexList = new List<int>();
  1416. linkIndexList.Add(vinfo.parentVertexIndex);
  1417. linkIndexList.AddRange(vinfo.childVertexList);
  1418. for (int j = 0; j < tcnt; j++)
  1419. {
  1420. int v0 = triangleList[j * 3];
  1421. int v1 = triangleList[j * 3 + 1];
  1422. int v2 = triangleList[j * 3 + 2];
  1423. int i0 = useVertexList.IndexOf(v0);
  1424. int i1 = useVertexList.IndexOf(v1);
  1425. int i2 = useVertexList.IndexOf(v2);
  1426. if ((vindex == v0 || vindex == v1 || vindex == v2)
  1427. && (i0 >= 0 && i1 >= 0 && i2 >= 0)
  1428. //&& (linkIndexList.Contains(i0) || linkIndexList.Contains(i1) || linkIndexList.Contains(i2))
  1429. )
  1430. {
  1431. // このトライアングルを登録
  1432. triangleRotationIndexData.Add(i0);
  1433. triangleRotationIndexData.Add(i1);
  1434. triangleRotationIndexData.Add(i2);
  1435. useCount++;
  1436. // トリアングル法線
  1437. var n = Vector3.Cross(wposList[v1] - wposList[v0], wposList[v2] - wposList[v0]);
  1438. nor += n.normalized;
  1439. }
  1440. }
  1441. }
  1442. // 姿勢
  1443. Quaternion lrot = Quaternion.identity;
  1444. if (useCount > 0)
  1445. {
  1446. // 法線
  1447. nor.Normalize();
  1448. // 接線
  1449. var tan = wposList[useVertexList[targetIndex]] - wposList[vindex];
  1450. tan.Normalize();
  1451. // ローカル回転
  1452. var q = Quaternion.LookRotation(nor, tan);
  1453. var iq = Quaternion.Inverse(q);
  1454. var rot = Quaternion.LookRotation(wnorList[vindex], wtanList[vindex]); // Transform.rotation
  1455. lrot = iq * rot;
  1456. }
  1457. // データ格納
  1458. data.targetIndex = targetIndex >= 0 ? targetIndex : -1;
  1459. data.triangleCount = useCount;
  1460. data.triangleStartIndex = startIndex;
  1461. data.localRot = lrot;
  1462. // この頂点がトライアングル回転補間として有効ならばフラグを立てる(これはLine回転補間と重複させないため)
  1463. if (useCount > 0)
  1464. {
  1465. SetFlag(i, VertexFlag_TriangleRotation);
  1466. }
  1467. triangleRotationData.Add(data);
  1468. }
  1469. if (triangleRotationData.Count > 0)
  1470. {
  1471. this.triangleRotationDataList = triangleRotationData.ToArray();
  1472. this.triangleRotationIndexList = triangleRotationIndexData.ToArray();
  1473. }
  1474. }
  1475. // ライン回転調整
  1476. var lineRotationData = new List<LineWorker.LineRotationData>();
  1477. var lineRotationRootInfo = new List<LineWorker.LineRotationRootInfo>();
  1478. if (lineList.Count > 0)
  1479. {
  1480. // ラインとして利用されている頂点インデックス
  1481. var useLineVertexSet = new HashSet<int>();
  1482. foreach (var vindex in lineList)
  1483. useLineVertexSet.Add(vindex);
  1484. // 頂点(親子構造)情報リスト
  1485. var infoList = GetUseVertexInfoList(parentList);
  1486. for (int i = 0; i < useVertexList.Count; i++)
  1487. {
  1488. int index = i;
  1489. int vindex = useVertexList[i];
  1490. var vinfo = infoList[index];
  1491. if (vinfo.parentVertexIndex >= 0)
  1492. continue;
  1493. // ルートを発見
  1494. // ここから頂点階層をトレースする
  1495. var info = new LineWorker.LineRotationRootInfo();
  1496. info.startIndex = (ushort)lineRotationData.Count;
  1497. int startIndex = lineRotationData.Count;
  1498. int cnt = 0;
  1499. int useLineCount = 0;
  1500. var tempLineRotationData = new List<LineWorker.LineRotationData>();
  1501. //var vinfo = infoList[index];
  1502. var vqueue = new Queue<VertexInfo>();
  1503. vqueue.Enqueue(vinfo);
  1504. while (vqueue.Count > 0)
  1505. {
  1506. vinfo = vqueue.Dequeue();
  1507. // データ作成
  1508. var data = new LineWorker.LineRotationData();
  1509. index = vinfo.vertexIndex;
  1510. data.vertexIndex = index;
  1511. //data.parentVertexIndex = -1;
  1512. data.localPos = float3.zero;
  1513. data.localRot = quaternion.identity;
  1514. // ラインとして利用されているかチェック
  1515. if (useLineVertexSet.Contains(useVertexList[index]))
  1516. useLineCount++;
  1517. if (vinfo.parentInfo != null)
  1518. {
  1519. // parent index
  1520. int pindex = vinfo.parentVertexIndex;
  1521. //data.parentVertexIndex = pindex;
  1522. // lpos, lrot
  1523. int mvindex = useVertexList[index];
  1524. int pmvindex = useVertexList[pindex];
  1525. Vector3 v = wposList[mvindex] - wposList[pmvindex];
  1526. //v.Normalize();
  1527. var pq = Quaternion.LookRotation(wnorList[pmvindex], wtanList[pmvindex]);
  1528. var ipq = Quaternion.Inverse(pq);
  1529. Vector3 lpos = ipq * v;
  1530. var q = Quaternion.LookRotation(wnorList[mvindex], wtanList[mvindex]);
  1531. Quaternion lrot = ipq * q;
  1532. data.localPos = lpos; // 親から自身へのローカル座標
  1533. data.localRot = lrot; // 親から自身へのローカル回転
  1534. }
  1535. if (vinfo.childInfoList.Count > 0)
  1536. {
  1537. data.childCount = vinfo.childInfoList.Count;
  1538. data.childStartDataIndex = startIndex + 1;
  1539. }
  1540. //lineRotationData.Add(data);
  1541. tempLineRotationData.Add(data);
  1542. cnt++;
  1543. startIndex += vinfo.childInfoList.Count;
  1544. // 子
  1545. foreach (var cinfo in vinfo.childInfoList)
  1546. {
  1547. vqueue.Enqueue(cinfo);
  1548. }
  1549. }
  1550. info.dataLength = (ushort)cnt;
  1551. // このデータブロックが1つもラインとして利用されていない場合は無効
  1552. if (useLineCount == 0)
  1553. continue;
  1554. // データカウント1以下は無効
  1555. if (cnt <= 1)
  1556. continue;
  1557. // 登録
  1558. lineRotationData.AddRange(tempLineRotationData);
  1559. lineRotationRootInfo.Add(info);
  1560. }
  1561. }
  1562. if (lineRotationData.Count > 0)
  1563. {
  1564. this.lineRotationDataList = lineRotationData.ToArray();
  1565. this.lineRotationRootInfoList = lineRotationRootInfo.ToArray();
  1566. }
  1567. // トライアングルベンド拘束
  1568. var triangleBendData = new List<TriangleBendConstraint.TriangleBendData>();
  1569. if (clothParams.UseTriangleBend && triangleList.Count > 0)
  1570. {
  1571. Dictionary<uint, List<int>> triangleEdgeDict = MeshUtility.GetTriangleEdgePair(triangleList);
  1572. foreach (var keyVal in triangleEdgeDict)
  1573. {
  1574. // 辺の頂点分解
  1575. int vindex2, vindex3;
  1576. DataUtility.UnpackPair(keyVal.Key, out vindex2, out vindex3);
  1577. int v2 = useVertexList.IndexOf(vindex2);
  1578. int v3 = useVertexList.IndexOf(vindex3);
  1579. if (v2 < 0 || v3 < 0)
  1580. continue;
  1581. if (IsInvalidVertex(v2) || IsExtendVertex(v2))
  1582. continue;
  1583. if (IsInvalidVertex(v3) || IsExtendVertex(v3))
  1584. continue;
  1585. if ((IsFixedVertex(v2) || IsExtendVertex(v2)) && (IsFixedVertex(v3) || IsExtendVertex(v3)))
  1586. continue;
  1587. // 接続トライアングル
  1588. List<int> tlist = keyVal.Value;
  1589. for (int i = 0; i < (tlist.Count - 1); i++)
  1590. {
  1591. int tindex0 = tlist[i];
  1592. int vindex0 = MeshUtility.RestTriangleVertex(tindex0, vindex2, vindex3, triangleList);
  1593. int v0 = useVertexList.IndexOf(vindex0);
  1594. if (v0 < 0)
  1595. continue;
  1596. if (IsInvalidVertex(v0) || IsExtendVertex(v0))
  1597. continue;
  1598. for (int j = i + 1; j < tlist.Count; j++)
  1599. {
  1600. int tindex1 = tlist[j];
  1601. int vindex1 = MeshUtility.RestTriangleVertex(tindex1, vindex2, vindex3, triangleList);
  1602. int v1 = useVertexList.IndexOf(vindex1);
  1603. if (v1 < 0)
  1604. continue;
  1605. if (IsInvalidVertex(v1) || IsExtendVertex(v1))
  1606. continue;
  1607. // 登録
  1608. // v2 +
  1609. // /|\
  1610. // v0 + | + v1
  1611. // \|/
  1612. // v3 +
  1613. // v2-v3が接続辺
  1614. RegistTriangleBend(v0, v1, v2, v3, wposList, clothParams, triangleBendData);
  1615. }
  1616. }
  1617. }
  1618. #if true
  1619. // TriangleのX型補強
  1620. if (clothParams.GetUseTwistCorrection(clothParams.AlgorithmType))
  1621. {
  1622. var vtdict = MeshUtility.GetVertexToTriangles(triangleList);
  1623. foreach (var kv in vtdict)
  1624. {
  1625. // 頂点に接続するトライアングルが2
  1626. var tset = kv.Value;
  1627. if (tset.Count != 2)
  1628. continue;
  1629. // トライアングルの接続がX型か判定する
  1630. var tlist = new List<int>(tset);
  1631. for (int i = 0; i < tlist.Count - 1; i++)
  1632. {
  1633. int tindex0 = tlist[i];
  1634. int tindex1 = tlist[i + 1];
  1635. // 2つのトライアングルが隣接していない判定
  1636. if (MeshUtility.CheckAdjacentTriangle(tindex0, tindex1, triangleList))
  1637. continue;
  1638. // この2つのトライアングルはX型
  1639. int v0 = kv.Key;
  1640. int v1, v2, v3, v4;
  1641. MeshUtility.RestTriangleVertex(tindex0, v0, triangleList, out v1, out v2);
  1642. MeshUtility.RestTriangleVertex(tindex1, v0, triangleList, out v3, out v4);
  1643. // トライアングルベンド形成
  1644. int index0 = useVertexList.IndexOf(v0);
  1645. int index1 = useVertexList.IndexOf(v1);
  1646. int index2 = useVertexList.IndexOf(v2);
  1647. int index3 = useVertexList.IndexOf(v3);
  1648. int index4 = useVertexList.IndexOf(v4);
  1649. if (index0 < 0 || index1 < 0 || index2 < 0 || index3 < 0 || index4 < 0)
  1650. continue;
  1651. if (IsMoveVertex(index0) == false)
  1652. continue;
  1653. // 登録
  1654. // v2 +
  1655. // /|\
  1656. // v0 + | + v1
  1657. // \|/
  1658. // v3 +
  1659. // v2-v3が接続辺
  1660. RegistTriangleBend(index1, index4, index0, index2, wposList, clothParams, triangleBendData);
  1661. RegistTriangleBend(index2, index3, index0, index4, wposList, clothParams, triangleBendData);
  1662. RegistTriangleBend(index4, index1, index0, index3, wposList, clothParams, triangleBendData);
  1663. RegistTriangleBend(index3, index2, index0, index1, wposList, clothParams, triangleBendData);
  1664. //Debug.Log($"X Triangle Bend: T0({index0}-{index1}-{index2}), T1({index0}-{index3}-{index4}) ");
  1665. }
  1666. }
  1667. }
  1668. #endif
  1669. #if true
  1670. // Line-Triangle補正
  1671. if (clothParams.GetUseTwistCorrection(clothParams.AlgorithmType) && lineList.Count > 0)
  1672. {
  1673. var compIndexSet = new HashSet<int>();
  1674. for (int i = 0; i < triangleList.Count; i++)
  1675. {
  1676. int tvindex0 = triangleList[i];
  1677. if (compIndexSet.Contains(tvindex0))
  1678. continue;
  1679. compIndexSet.Add(tvindex0);
  1680. // 接続するラインの判定
  1681. int lineIndex0 = lineList.IndexOf(tvindex0);
  1682. if (lineIndex0 < 0)
  1683. continue;
  1684. // 接続するもう片方のライン頂点
  1685. int lineIndex1 = lineIndex0 % 2 == 0 ? lineIndex0 + 1 : lineIndex0 - 1;
  1686. int lvindex0 = tvindex0;
  1687. int lvindex1 = lineList[lineIndex1];
  1688. // このラインがトライアングルとして利用されている場合は駄目
  1689. uint linePair = DataUtility.PackPair(lvindex0, lvindex1);
  1690. if (triangleEdgeDict.ContainsKey(linePair))
  1691. continue;
  1692. // トライアングルの残りの2点
  1693. int localIndex = i % 3;
  1694. int tvindex1 = -1;
  1695. int tvindex2 = -1;
  1696. if (localIndex == 0)
  1697. {
  1698. tvindex1 = triangleList[i + 1];
  1699. tvindex2 = triangleList[i + 2];
  1700. }
  1701. else if (localIndex == 1)
  1702. {
  1703. tvindex1 = triangleList[i + 1];
  1704. tvindex2 = triangleList[i - 1];
  1705. }
  1706. else
  1707. {
  1708. tvindex1 = triangleList[i - 1];
  1709. tvindex2 = triangleList[i - 2];
  1710. }
  1711. // トライアングルベンド形成
  1712. int tindex0 = useVertexList.IndexOf(tvindex0);
  1713. int tindex1 = useVertexList.IndexOf(tvindex1);
  1714. int tindex2 = useVertexList.IndexOf(tvindex2);
  1715. int lindex0 = useVertexList.IndexOf(lvindex0);
  1716. int lindex1 = useVertexList.IndexOf(lvindex1);
  1717. if (tindex0 < 0 || tindex1 < 0 || tindex2 < 0 || lindex0 < 0 || lindex1 < 0)
  1718. continue;
  1719. if (IsMoveVertex(tindex0) == false && IsMoveVertex(tindex1) == false && IsMoveVertex(tindex2) == false)
  1720. continue;
  1721. // 登録
  1722. // v2 +
  1723. // /|\
  1724. // v0 + | + v1
  1725. // \|/
  1726. // v3 +
  1727. // v2-v3が接続辺
  1728. RegistTriangleBend(tindex1, tindex2, tindex0, lindex1, wposList, clothParams, triangleBendData);
  1729. RegistTriangleBend(lindex1, tindex1, tindex0, tindex2, wposList, clothParams, triangleBendData);
  1730. RegistTriangleBend(lindex1, tindex2, tindex0, tindex1, wposList, clothParams, triangleBendData);
  1731. //Debug.Log($"Add Triangle Bend: T({tindex0}-{tindex1}-{tindex2}) L({lindex0}-{lindex1})");
  1732. }
  1733. }
  1734. #endif
  1735. #if false
  1736. // 固定を含むトライアングルの特殊復元(IncludeFixed)
  1737. // このデータはチェックの有無に関わらず常に作成される
  1738. int tcnt = triangleList.Count / 3;
  1739. for (int i = 0; i < tcnt; i++)
  1740. {
  1741. int v0 = triangleList[i * 3];
  1742. int v1 = triangleList[i * 3 + 1];
  1743. int v2 = triangleList[i * 3 + 2];
  1744. int i0 = useVertexList.IndexOf(v0);
  1745. int i1 = useVertexList.IndexOf(v1);
  1746. int i2 = useVertexList.IndexOf(v2);
  1747. if (i0 < 0 || i1 < 0 || i2 < 0)
  1748. continue;
  1749. // 移動頂点の数
  1750. int moveCnt = (IsMoveVertex(i0) ? 1 : 0) + (IsMoveVertex(i1) ? 1 : 0) + (IsMoveVertex(i2) ? 1 : 0);
  1751. // 移動が1つか2つのトライアングルのみ登録する
  1752. if (moveCnt == 1 || moveCnt == 2)
  1753. {
  1754. // 登録
  1755. // v0 +
  1756. // / \
  1757. // v1 +---+ v2
  1758. //
  1759. // v3 = -1
  1760. //Debug.Log($"special triangle ({v0}-{v1}-{v2})");
  1761. var data = new TriangleBendConstraint.TriangleBendData();
  1762. data.vindex0 = i0;
  1763. data.vindex1 = i1;
  1764. data.vindex2 = i2;
  1765. data.vindex3 = -1;
  1766. data.restAngle = 0.0f;
  1767. data.depth = 0.0f;
  1768. triangleBendData.Add(data);
  1769. }
  1770. }
  1771. #endif
  1772. }
  1773. if (triangleBendData.Count > 0)
  1774. {
  1775. // ジョブシステム用にデータを加工して登録
  1776. var builder = new ReferenceDataBuilder<TriangleBendConstraint.TriangleBendData>();
  1777. builder.Init(useVertexList.Count);
  1778. foreach (var data in triangleBendData)
  1779. {
  1780. if (data.IsPositionBend())
  1781. {
  1782. //Debug.Log($"add data ({data.vindex0}-{data.vindex1}-{data.vindex2})");
  1783. builder.AddData(data, data.vindex0, data.vindex1, data.vindex2);
  1784. }
  1785. else
  1786. builder.AddData(data, data.vindex0, data.vindex1, data.vindex2, data.vindex3);
  1787. }
  1788. (var refDataList, var dataIndexList, var dataToDataIndexList) = builder.GetIndirectReferenceData();
  1789. for (int i = 0; i < triangleBendData.Count; i++)
  1790. {
  1791. var data = triangleBendData[i];
  1792. data.writeIndex0 = dataToDataIndexList[i][0];
  1793. data.writeIndex1 = dataToDataIndexList[i][1];
  1794. data.writeIndex2 = dataToDataIndexList[i][2];
  1795. if (data.IsPositionBend())
  1796. data.writeIndex3 = -1;
  1797. else
  1798. data.writeIndex3 = dataToDataIndexList[i][3];
  1799. triangleBendData[i] = data;
  1800. }
  1801. this.triangleBendDataList = triangleBendData.ToArray();
  1802. this.triangleBendReferenceList = refDataList.ToArray();
  1803. this.triangleBendWriteBufferCount = dataIndexList.Count;
  1804. }
  1805. // 浸透制限拘束
  1806. if (clothParams.UsePenetration)
  1807. {
  1808. var mode = clothParams.GetPenetrationMode();
  1809. if (mode == ClothParams.PenetrationMode.SurfacePenetration || mode == ClothParams.PenetrationMode.ColliderPenetration)
  1810. {
  1811. var penetrationData = new List<PenetrationConstraint.PenetrationData>();
  1812. for (int i = 0; i < useVertexList.Count; i++)
  1813. {
  1814. // 移動頂点以外は不要
  1815. if (IsMoveVertex(i) == false)
  1816. continue;
  1817. int vindex = useVertexList[i];
  1818. var pos = wposList[vindex];
  1819. float depth = vertexDepthList[i];
  1820. // 拘束データは1パーティクルにつき2つ固定
  1821. var dataList = new List<PenetrationConstraint.PenetrationData>();
  1822. if (mode == ClothParams.PenetrationMode.SurfacePenetration)
  1823. {
  1824. // サーフェス浸透
  1825. if (depth <= clothParams.PenetrationMaxDepth)
  1826. {
  1827. // 浸透軸
  1828. Vector3 dir = Vector3.zero;
  1829. switch (clothParams.GetPenetrationAxis())
  1830. {
  1831. case ClothParams.PenetrationAxis.X:
  1832. dir = Vector3.right;
  1833. break;
  1834. case ClothParams.PenetrationAxis.Y:
  1835. dir = Vector3.up;
  1836. break;
  1837. case ClothParams.PenetrationAxis.Z:
  1838. dir = Vector3.forward;
  1839. break;
  1840. case ClothParams.PenetrationAxis.InverseX:
  1841. dir = Vector3.left;
  1842. break;
  1843. case ClothParams.PenetrationAxis.InverseY:
  1844. dir = Vector3.down;
  1845. break;
  1846. case ClothParams.PenetrationAxis.InverseZ:
  1847. dir = Vector3.back;
  1848. break;
  1849. }
  1850. var data = new PenetrationConstraint.PenetrationData();
  1851. data.vertexIndex = (short)i;
  1852. data.localDir = dir;
  1853. dataList.Add(data);
  1854. }
  1855. }
  1856. else if (mode == ClothParams.PenetrationMode.ColliderPenetration)
  1857. {
  1858. // コライダー浸透
  1859. if (depth <= clothParams.PenetrationMaxDepth)
  1860. {
  1861. // 全コライダーループ
  1862. for (int j = 0; j < teamData.ColliderCount; j++)
  1863. {
  1864. var col = teamData.ColliderList[j];
  1865. if (col == null)
  1866. continue;
  1867. // 無視リストチェック
  1868. if (teamData.PenetrationIgnoreColliderList.Contains(col))
  1869. continue;
  1870. // コライダーへの最近接点を求める
  1871. Vector3 p, dir, d;
  1872. if (col.CalcNearPoint(pos, out p, out dir, out d, false) == false)
  1873. continue;
  1874. // 距離判定
  1875. var dist = Vector3.Distance(pos, p);
  1876. //if (dist > clothParams.PenetrationLength)
  1877. // continue;
  1878. if (dist > clothParams.GetPenetrationConnectDistance().Evaluate(depth))
  1879. continue;
  1880. var data = new PenetrationConstraint.PenetrationData();
  1881. data.vertexIndex = (short)i;
  1882. data.colliderIndex = (short)j;
  1883. //data.localPos = col.transform.InverseTransformPoint(p); // 衝突地点
  1884. //data.localPos = col.transform.InverseTransformPoint(p + dir * 0.02f); // 衝突地点
  1885. //data.localPos = col.transform.InverseTransformPoint(d + dir * 0.01f); // 中心軸
  1886. data.localPos = col.transform.InverseTransformPoint(d); // 中心軸
  1887. //data.localPos = col.transform.InverseTransformPoint((p + d) * 0.5f); // 衝突位置と中心軸の真ん中
  1888. //data.localDir = col.transform.InverseTransformDirection(dir);
  1889. data.localDir = col.transform.InverseTransformDirection(pos - d); // 一旦このベクトルで収める
  1890. data.distance = dist;
  1891. dataList.Add(data);
  1892. }
  1893. // 距離の昇順でソート
  1894. if (dataList.Count >= 2)
  1895. dataList.Sort((a, b) => a.distance < b.distance ? -1 : 1);
  1896. }
  1897. // 2つ目がある場合はその距離倍率でキャンセル判定
  1898. if (dataList.Count >= 2)
  1899. {
  1900. const float distmul = 1.5f;
  1901. float dist = dataList[0].distance * distmul;
  1902. for (int j = 1; j < dataList.Count;)
  1903. {
  1904. if (dataList[j].distance > dist)
  1905. {
  1906. dataList.RemoveAt(j);
  1907. continue;
  1908. }
  1909. j++;
  1910. }
  1911. }
  1912. // データ最大数
  1913. const int PenetrationMaxDataCount = 2;
  1914. if (dataList.Count > PenetrationMaxDataCount)
  1915. {
  1916. dataList.RemoveRange(2, dataList.Count - 2);
  1917. }
  1918. // データ整形
  1919. for (int j = 0; j < dataList.Count; j++)
  1920. {
  1921. var data = dataList[j];
  1922. data.distance = math.length(data.localDir);
  1923. data.localDir = math.normalize(data.localDir);
  1924. dataList[j] = data;
  1925. }
  1926. //Debug.Log($"[{vindex}] dcnt={dataList.Count}");
  1927. }
  1928. // データ追加
  1929. foreach (var data in dataList)
  1930. {
  1931. penetrationData.Add(data);
  1932. }
  1933. }
  1934. // データ格納
  1935. if (penetrationData.Count > 0)
  1936. {
  1937. // ジョブシステム用にデータを加工して登録
  1938. var builder = new ReferenceDataBuilder<PenetrationConstraint.PenetrationData>();
  1939. builder.Init(useVertexList.Count);
  1940. foreach (var data in penetrationData)
  1941. {
  1942. builder.AddData(data, data.vertexIndex);
  1943. }
  1944. (var refDataList, var dataList) = builder.GetDirectReferenceData();
  1945. this.penetrationDataList = dataList.ToArray();
  1946. this.penetrationReferenceList = refDataList.ToArray();
  1947. this.penetrationMode = clothParams.GetPenetrationMode(); // 作成時のモードを記録
  1948. }
  1949. }
  1950. #if false // 一旦休眠
  1951. else if (mode == ClothParams.PenetrationMode.BonePenetration)
  1952. {
  1953. // Bone Penetration
  1954. // すでにデータは作られている
  1955. if (penetrationDirections.Count > 0)
  1956. {
  1957. this.penetrationDirectionDataList = penetrationDirections.ToArray();
  1958. this.penetrationMode = clothParams.GetPenetrationMode(); // 作成時のモードを記録
  1959. }
  1960. }
  1961. #endif
  1962. }
  1963. #if false
  1964. // エッジコリジョン拘束
  1965. var edgeCollisionData = new List<EdgeCollisionConstraint.EdgeCollisionData>();
  1966. if (clothParams.UseEdgeCollision)
  1967. {
  1968. // 両方移動頂点のエッジをデータ化する
  1969. distSet.Clear();
  1970. for (int i = 0; i < useVertexList.Count; i++)
  1971. {
  1972. if (IsInvalidVertex(i) || IsExtendVertex(i))
  1973. continue;
  1974. int vindex0 = useVertexList[i];
  1975. var vlist = meshVLink[vindex0];
  1976. foreach (var vindex in vlist)
  1977. {
  1978. int vi = useVertexList.IndexOf(vindex);
  1979. if (vi < 0)
  1980. continue;
  1981. if (IsInvalidVertex(vi) || IsExtendVertex(vi))
  1982. continue;
  1983. if ((IsFixedVertex(i) || IsExtendVertex(i)) && (IsFixedVertex(vi) || IsExtendVertex(vi)))
  1984. continue;
  1985. uint pack = DataUtility.PackPair(i, vi);
  1986. if (distSet.Contains(pack))
  1987. continue;
  1988. // 登録
  1989. var data = new EdgeCollisionConstraint.EdgeCollisionData();
  1990. data.vindex0 = (ushort)i;
  1991. data.vindex1 = (ushort)vi;
  1992. edgeCollisionData.Add(data);
  1993. distSet.Add(pack);
  1994. }
  1995. }
  1996. }
  1997. if (edgeCollisionData.Count > 0)
  1998. {
  1999. // ジョブシステム用にデータを加工して登録
  2000. var builder = new ReferenceDataBuilder<EdgeCollisionConstraint.EdgeCollisionData>();
  2001. builder.Init(useVertexList.Count);
  2002. foreach (var data in edgeCollisionData)
  2003. {
  2004. builder.AddData(data, data.vindex0, data.vindex1);
  2005. }
  2006. (var refDataList, var dataIndexList, var dataToDataIndexList) = builder.GetIndirectReferenceData();
  2007. for (int i = 0; i < edgeCollisionData.Count; i++)
  2008. {
  2009. var data = edgeCollisionData[i];
  2010. data.writeIndex0 = dataToDataIndexList[i][0];
  2011. data.writeIndex1 = dataToDataIndexList[i][1];
  2012. edgeCollisionData[i] = data;
  2013. }
  2014. this.edgeCollisionDataList = edgeCollisionData.ToArray();
  2015. this.edgeCollisionReferenceList = refDataList.ToArray();
  2016. this.edgeCollisionWriteBufferCount = dataIndexList.Count;
  2017. }
  2018. #endif
  2019. #if false // 一旦休眠
  2020. // ベーススキニング
  2021. if (team.SkinningMode == PhysicsTeam.TeamSkinningMode.GenerateFromBones && teamData.SkinningBoneList.Count > 0)
  2022. {
  2023. // すでにデータは作られている
  2024. if (baseSkinningData.Count > 0 && bindPoses.Count > 0)
  2025. {
  2026. this.baseSkinningDataList = baseSkinningData.ToArray();
  2027. this.baseSkinningBindPoseList = bindPoses.ToArray();
  2028. }
  2029. }
  2030. #endif
  2031. #if false
  2032. // ボリューム拘束
  2033. var volumeData = new List<VolumeConstraint.VolumeData>();
  2034. if (clothParams.UseVolume)
  2035. {
  2036. // テトラメッシュ構築(これはメッシュの全頂点から)
  2037. int tetraCount;
  2038. List<int> tetraIndexList;
  2039. List<float> tetraSizeList;
  2040. MeshUtility.CalcTetraMesh(wposList, out tetraCount, out tetraIndexList, out tetraSizeList);
  2041. // テトラメッシュ選別(利用頂点のみ+テトラサイズ)
  2042. for (int i = 0; i < tetraCount; i++)
  2043. {
  2044. if (tetraSizeList[i] > clothParams.GetMaxVolumeLength())
  2045. continue;
  2046. int index = i * 4;
  2047. int moveCnt = 0;
  2048. List<int> indexList = new List<int>();
  2049. for (int j = 0; j < 4; j++)
  2050. {
  2051. // 使用頂点インデックスに変換
  2052. int vi = useVertexList.IndexOf(tetraIndexList[index + j]);
  2053. // 使用頂点判定
  2054. if (vi < 0)
  2055. {
  2056. break;
  2057. }
  2058. // 移動頂点判定
  2059. if (IsMoveVertex(vi))
  2060. moveCnt++;
  2061. indexList.Add(vi);
  2062. }
  2063. // 4点がすべて使用され1つ以上の移動点を含む場合のみボリューム拘束を作成する
  2064. if (indexList.Count == 4 && moveCnt > 0)
  2065. {
  2066. var vindex0 = tetraIndexList[index];
  2067. var vindex1 = tetraIndexList[index + 1];
  2068. var vindex2 = tetraIndexList[index + 2];
  2069. var vindex3 = tetraIndexList[index + 3];
  2070. // 方向性拘束の作成
  2071. // テトラの4面のうちメッシュのトライアングルに含まれるもの、かつその中でデプスの平均値が最も小さいものを選出
  2072. List<int> dirList = SortTetra(vindex0, vindex1, vindex2, vindex3, meshVertexDepthList);
  2073. if (dirList != null)
  2074. {
  2075. // 並び替え
  2076. vindex0 = dirList[0];
  2077. vindex1 = dirList[1];
  2078. vindex2 = dirList[2];
  2079. vindex3 = dirList[3];
  2080. }
  2081. var v0 = useVertexList.IndexOf(vindex0);
  2082. var v1 = useVertexList.IndexOf(vindex1);
  2083. var v2 = useVertexList.IndexOf(vindex2);
  2084. var v3 = useVertexList.IndexOf(vindex3);
  2085. // ボリューム行列(データ作成時だけで大丈夫か?)
  2086. var ivMat = float3x3.identity;
  2087. float3x3 m;
  2088. var pos0 = wposList[vindex0];
  2089. var pos1 = wposList[vindex1];
  2090. var pos2 = wposList[vindex2];
  2091. var pos3 = wposList[vindex3];
  2092. m.c0 = pos1 - pos0;
  2093. m.c1 = pos2 - pos0;
  2094. m.c2 = pos3 - pos0;
  2095. float det = math.determinant(m);
  2096. if (math.abs(det) > 1e-6f)
  2097. {
  2098. // 方向性
  2099. int dir = 0;
  2100. if (dirList != null)
  2101. {
  2102. // 方向性
  2103. var n = math.normalize(math.cross(m.c0, m.c1));
  2104. dir = math.dot(n, m.c2) > 0.0f ? 1 : -1;
  2105. }
  2106. // 登録
  2107. if (dir != 0)
  2108. {
  2109. var data = new VolumeConstraint.VolumeData();
  2110. data.vindex0 = v0;
  2111. data.vindex1 = v1;
  2112. data.vindex2 = v2;
  2113. data.vindex3 = v3;
  2114. data.depth = (vertexDepthList[v0] + vertexDepthList[v1] + vertexDepthList[v2] + vertexDepthList[v3]) / 4.0f;
  2115. data.ivMat = math.inverse(m);
  2116. data.direction = dir;
  2117. volumeData.Add(data);
  2118. }
  2119. }
  2120. }
  2121. }
  2122. }
  2123. if (volumeData.Count > 0)
  2124. {
  2125. // ジョブシステム用にデータを加工して登録
  2126. var builder = new ReferenceDataBuilder<VolumeConstraint.VolumeData>();
  2127. builder.Init(useVertexList.Count);
  2128. foreach (var data in volumeData)
  2129. {
  2130. builder.AddData(data, data.vindex0, data.vindex1, data.vindex2, data.vindex3);
  2131. }
  2132. (var refDataList, var dataIndexList, var dataToDataIndexList) = builder.GetIndirectReferenceData();
  2133. for (int i = 0; i < volumeData.Count; i++)
  2134. {
  2135. var data = volumeData[i];
  2136. data.writeIndex0 = dataToDataIndexList[i][0];
  2137. data.writeIndex1 = dataToDataIndexList[i][1];
  2138. data.writeIndex2 = dataToDataIndexList[i][2];
  2139. data.writeIndex3 = dataToDataIndexList[i][3];
  2140. volumeData[i] = data;
  2141. }
  2142. this.volumeDataList = volumeData.ToArray();
  2143. this.volumeReferenceList = refDataList.ToArray();
  2144. this.volumeWriteBufferCount = dataIndexList.Count;
  2145. }
  2146. #endif
  2147. }
  2148. #if false // 一旦休眠
  2149. /// <summary>
  2150. /// BoneListからスキニング情報を作成する
  2151. /// </summary>
  2152. /// <param name="team"></param>
  2153. /// <param name="teamData"></param>
  2154. /// <param name="wposList"></param>
  2155. /// <param name="wnorList"></param>
  2156. /// <param name="wtanList"></param>
  2157. /// <returns></returns>
  2158. (List<BaseSkinningWorker.BaseSkinningData>, List<float4x4>, List<float3>) CreateSkinningData(
  2159. PhysicsTeam team,
  2160. PhysicsTeamData teamData,
  2161. ClothParams clothParams,
  2162. List<Vector3> wposList,
  2163. List<Vector3> wnorList,
  2164. List<Vector3> wtanList
  2165. )
  2166. {
  2167. var baseSkinningData = new List<BaseSkinningWorker.BaseSkinningData>();
  2168. var bindPoses = new List<float4x4>();
  2169. var penetrationDirections = new List<float3>();
  2170. // 作成判定
  2171. bool create = false;
  2172. if (team.SkinningMode == PhysicsTeam.TeamSkinningMode.GenerateFromBones)
  2173. create = true;
  2174. if (clothParams.UsePenetration && clothParams.GetPenetrationMode() == ClothParams.PenetrationMode.BonePenetration)
  2175. create = true;
  2176. if (teamData.SkinningBoneList.Count == 0)
  2177. create = false;
  2178. if (create == false)
  2179. return (baseSkinningData, bindPoses, penetrationDirections);
  2180. // ボーン情報
  2181. var boneInfos = new List<PenetrationBone>();
  2182. var boneList = teamData.SkinningBoneList;
  2183. foreach (var bone in boneList)
  2184. {
  2185. if (bone != null)
  2186. {
  2187. for (int i = 0; i < bone.childCount; i++)
  2188. {
  2189. var cbone = bone.GetChild(i);
  2190. if (boneList.Contains(cbone))
  2191. {
  2192. float dist = Vector3.Distance(bone.position, cbone.position);
  2193. if (dist > 0.001f)
  2194. {
  2195. var boneInfo = new PenetrationBone();
  2196. boneInfo.bone = bone;
  2197. boneInfo.childBone = cbone;
  2198. boneInfos.Add(boneInfo);
  2199. }
  2200. }
  2201. }
  2202. }
  2203. }
  2204. if (boneInfos.Count > 0)
  2205. {
  2206. var t = team.transform;
  2207. var tLtoW = t.localToWorldMatrix;
  2208. // バインドポーズの算出
  2209. foreach (var bone in boneList)
  2210. {
  2211. float4x4 bindPose = bone.worldToLocalMatrix * tLtoW;
  2212. bindPoses.Add(bindPose);
  2213. }
  2214. // データは全頂点分作成する
  2215. var workList = new List<PenetrationWork>();
  2216. for (int i = 0; i < useVertexList.Count; i++)
  2217. {
  2218. var data = new BaseSkinningWorker.BaseSkinningData();
  2219. int vindex = useVertexList[i];
  2220. var pos = wposList[vindex];
  2221. var nor = wnorList[vindex];
  2222. var tan = wtanList[vindex];
  2223. // 接続ボーンとウエイト
  2224. workList.Clear();
  2225. // 各ボーンへの距離を求める
  2226. foreach (var boneInfo in boneInfos)
  2227. {
  2228. float3 bpos0 = boneInfo.bone.position;
  2229. float3 bpos1 = boneInfo.childBone.position;
  2230. float3 d = MathUtility.ClosestPtPointSegment(pos, bpos0, bpos1);
  2231. float dist = math.distance(pos, d);
  2232. var work = new PenetrationWork();
  2233. work.bone = boneInfo.bone;
  2234. work.childBone = boneInfo.childBone;
  2235. work.boneIndex = teamData.SkinningBoneList.IndexOf(boneInfo.bone);
  2236. float3 pos2 = pos;
  2237. var v = pos2 - d;
  2238. var bv = bpos1 - bpos0;
  2239. //float ang = Vector3.Angle(v, bv);
  2240. float dot = math.dot(math.normalize(v), math.normalize(bv));
  2241. //float ratio = 1.0f - math.abs(ang);
  2242. //dist = dist * (math.abs(dot) + 1e-06f);
  2243. //dist = dist * (math.abs(dot) + 0.3f);
  2244. dist = dist * (math.abs(dot) * 0.3f + 0.7f);
  2245. work.distance = dist;
  2246. workList.Add(work);
  2247. }
  2248. // 昇順にソートする
  2249. workList.Sort((a, b) => a.distance < b.distance ? -1 : 1);
  2250. #if true
  2251. // 同じボーンは詰める
  2252. for (int j = 1; j < workList.Count;)
  2253. {
  2254. var work = workList[j];
  2255. int index = workList.FindIndex(x => x.bone == work.bone);
  2256. if (index < j)
  2257. {
  2258. workList.RemoveAt(j);
  2259. }
  2260. else
  2261. j++;
  2262. }
  2263. #endif
  2264. // 最大4つにクランプ
  2265. if (workList.Count > 4)
  2266. workList.RemoveRange(4, workList.Count - 4);
  2267. // ウエイト算出
  2268. // (0)最小距離のn%を減算する
  2269. const float lengthWeight = 0.8f; // 0.8/0.65
  2270. float mindist = workList[0].distance * lengthWeight;
  2271. foreach (var work in workList)
  2272. work.weight = work.distance - mindist;
  2273. // (1)distanceをn乗する
  2274. const float pow = 2.0f; // 2.0f/3.0
  2275. foreach (var work in workList)
  2276. work.weight = Mathf.Pow(work.weight, pow);
  2277. // (2)最小値の逆数にする
  2278. float min = Mathf.Max(workList[0].weight, 1e-06f);
  2279. //Debug.Assert(min > 1e-06f);
  2280. float sum = 0;
  2281. foreach (var work in workList)
  2282. {
  2283. work.weight = min / work.weight;
  2284. sum += work.weight;
  2285. }
  2286. // (3)割合を出す
  2287. foreach (var work in workList)
  2288. work.weight /= sum;
  2289. // (4)極小のウエイトは削除する
  2290. sum = 0;
  2291. for (int j = 0; j < workList.Count;)
  2292. {
  2293. if (workList[j].weight < 0.01f)
  2294. workList.RemoveAt(j);
  2295. else
  2296. {
  2297. sum += workList[j].weight;
  2298. j++;
  2299. }
  2300. }
  2301. Debug.Assert(sum > 0);
  2302. // (5)再度1.0に平均化
  2303. foreach (var work in workList)
  2304. work.weight /= sum;
  2305. // コンポーネントのローカル座標へ変換
  2306. var lpos = t.InverseTransformPoint(pos);
  2307. var lnor = t.InverseTransformDirection(nor);
  2308. var ltan = t.InverseTransformDirection(tan);
  2309. // データ作成
  2310. data.localPos = lpos;
  2311. data.localNor = lnor;
  2312. data.localTan = ltan;
  2313. for (int j = 0; j < workList.Count; j++)
  2314. {
  2315. data.boneIndices[j] = workList[j].boneIndex;
  2316. data.weights[j] = workList[j].weight;
  2317. }
  2318. data.weightCount = (short)workList.Count;
  2319. baseSkinningData.Add(data);
  2320. // Bone Penetration用の浸透方向を求める
  2321. float3 pv = 0;
  2322. foreach (var work in workList)
  2323. {
  2324. var bpos0 = work.bone.position;
  2325. var bpos1 = work.childBone.position;
  2326. Vector3 d = MathUtility.ClosestPtPointSegmentNoClamp(pos, bpos0, bpos1);
  2327. float3 v = (pos - d).normalized * work.weight;
  2328. pv += v;
  2329. }
  2330. quaternion rot = quaternion.LookRotation(nor, tan);
  2331. float3 lpv = math.mul(math.inverse(rot), pv);
  2332. lpv = math.normalize(lpv);
  2333. penetrationDirections.Add(lpv);
  2334. }
  2335. }
  2336. return (baseSkinningData, bindPoses, penetrationDirections);
  2337. }
  2338. #endif
  2339. /// <summary>
  2340. /// トライアングルベンドの登録
  2341. /// </summary>
  2342. /// <param name="v0"></param>
  2343. /// <param name="v1"></param>
  2344. /// <param name="v2"></param>
  2345. /// <param name="v3"></param>
  2346. /// <param name="wposList"></param>
  2347. /// <param name="clothParams"></param>
  2348. /// <param name="triangleBendData"></param>
  2349. void RegistTriangleBend(
  2350. int v0, int v1, int v2, int v3,
  2351. List<Vector3> wposList,
  2352. ClothParams clothParams,
  2353. List<TriangleBendConstraint.TriangleBendData> triangleBendData
  2354. )
  2355. {
  2356. // 登録
  2357. // v2 +
  2358. // /|\
  2359. // v0 + | + v1
  2360. // \|/
  2361. // v3 +
  2362. // v2-v3が接続辺
  2363. int vindex0 = useVertexList[v0];
  2364. int vindex1 = useVertexList[v1];
  2365. int vindex2 = useVertexList[v2];
  2366. int vindex3 = useVertexList[v3];
  2367. float restAngle;
  2368. var p0 = wposList[vindex0];
  2369. var p1 = wposList[vindex1];
  2370. var p2 = wposList[vindex2];
  2371. var p3 = wposList[vindex3];
  2372. if (CalcTriangleBendRestAngle(p0, p1, p2, p3, out restAngle))
  2373. {
  2374. var data = new TriangleBendConstraint.TriangleBendData();
  2375. data.vindex0 = v0;
  2376. data.vindex1 = v1;
  2377. data.vindex2 = v2;
  2378. data.vindex3 = v3;
  2379. data.restAngle = restAngle;
  2380. data.depth = (vertexDepthList[v2] + vertexDepthList[v3]) * 0.5f;
  2381. switch (clothParams.AlgorithmType)
  2382. {
  2383. case ClothParams.Algorithm.Algorithm_1:
  2384. // 双方向 (旧式)
  2385. triangleBendData.Add(data);
  2386. break;
  2387. case ClothParams.Algorithm.Algorithm_2:
  2388. // 厳密
  2389. // 内角制限:すべてに適用すると振動がひどくなる
  2390. //Debug.Log($"restAngle={Mathf.Rad2Deg * restAngle}");
  2391. if (math.degrees(restAngle) >= 90.0f)
  2392. {
  2393. //Debug.Log($"skip! ({v0}-{v1}-{v2}-{v3})");
  2394. return;
  2395. }
  2396. // Strict用データ作成
  2397. // 方向性
  2398. float3 e = p3 - p2;
  2399. var n1 = MathUtility.TriangleNormal(p0, p2, p3);
  2400. var n2 = MathUtility.TriangleNormal(p1, p3, p2);
  2401. var dir = math.dot(math.cross(n1, n2), e);
  2402. dir = dir == 0.0f ? 1 : dir;
  2403. data.direction = dir;
  2404. data.restAngle = restAngle * math.sign(data.direction);
  2405. triangleBendData.Add(data);
  2406. break;
  2407. }
  2408. }
  2409. }
  2410. List<int> SortTetra(int v0, int v1, int v2, int v3, List<float> meshVertexDepthList)
  2411. {
  2412. List<int> result = new List<int>();
  2413. result.Add(v0);
  2414. result.Add(v1);
  2415. result.Add(v2);
  2416. result.Add(v3);
  2417. // デプスの昇順にソート
  2418. result.Sort((a, b) => meshVertexDepthList[a] < meshVertexDepthList[b] ? -1 : 1);
  2419. return result;
  2420. }
  2421. /// <summary>
  2422. /// テトラの4面に対してメッシュトライアングルに属する面を算出しデプスソートしたのち最もデプスが小さい面構成を返す
  2423. /// 返された面の頂点0,1,2が基準面となり、3が方向性を示す頂点となる
  2424. /// </summary>
  2425. /// <param name="v0"></param>
  2426. /// <param name="v1"></param>
  2427. /// <param name="v2"></param>
  2428. /// <param name="v3"></param>
  2429. /// <param name="trianglePackSet"></param>
  2430. /// <param name="meshVertexDepthList"></param>
  2431. /// <returns></returns>
  2432. List<int> CheckTetraDirection(int v0, int v1, int v2, int v3, HashSet<ulong> trianglePackSet, List<float> meshVertexDepthList)
  2433. {
  2434. // メッシュに属する面のリストを摘出する
  2435. ulong pack0 = DataUtility.PackTriple(v0, v1, v2);
  2436. ulong pack1 = DataUtility.PackTriple(v0, v2, v3);
  2437. ulong pack2 = DataUtility.PackTriple(v0, v3, v1);
  2438. ulong pack3 = DataUtility.PackTriple(v1, v2, v3);
  2439. List<ulong> packList = new List<ulong>();
  2440. packList.Add(pack0);
  2441. packList.Add(pack1);
  2442. packList.Add(pack2);
  2443. packList.Add(pack3);
  2444. for (int i = 0; i < packList.Count;)
  2445. {
  2446. if (trianglePackSet.Contains(packList[i]) == false)
  2447. {
  2448. packList.RemoveAt(i);
  2449. continue;
  2450. }
  2451. i++;
  2452. }
  2453. if (packList.Count == 0)
  2454. return null; // なし
  2455. // 面をデプス値の昇順でソートする
  2456. // ソート後の最初の面が基準面となる
  2457. if (packList.Count >= 2)
  2458. {
  2459. packList.Sort((a, b) =>
  2460. {
  2461. int a0, a1, a2, b0, b1, b2;
  2462. DataUtility.UnpackTriple(a, out a0, out a1, out a2);
  2463. DataUtility.UnpackTriple(b, out b0, out b1, out b2);
  2464. float depth0 = (meshVertexDepthList[a0] + meshVertexDepthList[a1] + meshVertexDepthList[a2]) / 3.0f;
  2465. float depth1 = (meshVertexDepthList[b0] + meshVertexDepthList[b1] + meshVertexDepthList[b2]) / 3.0f;
  2466. //return depth0 < depth1 ? -1 : 1;
  2467. return depth0 > depth1 ? -1 : 1;
  2468. });
  2469. }
  2470. // データ整形(メッシュトライアングルの頂点を0,1,2に格納、残りの1つを3に格納)
  2471. List<int> restList = new List<int>();
  2472. restList.Add(v0);
  2473. restList.Add(v1);
  2474. restList.Add(v2);
  2475. restList.Add(v3);
  2476. ulong pack = packList[0];
  2477. DataUtility.UnpackTriple(pack, out v0, out v1, out v2);
  2478. restList.Remove(v0);
  2479. restList.Remove(v1);
  2480. restList.Remove(v2);
  2481. v3 = restList[0];
  2482. if (meshVertexDepthList[v3] == 0.0f)
  2483. return null;
  2484. List<int> result = new List<int>();
  2485. result.Add(v0);
  2486. result.Add(v1);
  2487. result.Add(v2);
  2488. result.Add(v3);
  2489. return result;
  2490. }
  2491. /// <summary>
  2492. /// トライアングルベンドの復元角度を求める
  2493. /// 2つの三角形、p2-p3が共通辺で、p0/p1が端の独立点
  2494. /// p2 +
  2495. /// /|\
  2496. /// p0 + | + p1
  2497. /// \|/
  2498. /// p3 +
  2499. /// </summary>
  2500. /// <param name="p0"></param>
  2501. /// <param name="p1"></param>
  2502. /// <param name="p2"></param>
  2503. /// <param name="p3"></param>
  2504. /// <returns>トライアングル角度(ラジアン)</returns>
  2505. bool CalcTriangleBendRestAngle(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, out float restAngle)
  2506. {
  2507. restAngle = 0;
  2508. Vector3 e = p3 - p2;
  2509. float elen = e.magnitude;
  2510. if (elen < 1e-6f)
  2511. return false;
  2512. Vector3 n1 = Vector3.Cross((p2 - p0), (p3 - p0));
  2513. Vector3 n2 = Vector3.Cross((p3 - p1), (p2 - p1));
  2514. // これいる?
  2515. n1 /= n1.sqrMagnitude;
  2516. n2 /= n2.sqrMagnitude;
  2517. n1.Normalize();
  2518. n2.Normalize();
  2519. float dot = Vector3.Dot(n1, n2);
  2520. if (dot < -1.0f)
  2521. dot = -1.0f;
  2522. if (dot > 1.0f)
  2523. dot = 1.0f;
  2524. // ただしこれでは折り曲げが左右どちらでも同じ角度で同じ値になってしまうが。。問題はないか?
  2525. restAngle = Mathf.Acos(dot);
  2526. return true;
  2527. }
  2528. /// <summary>
  2529. /// メッシュの頂点に対応するデプスリストを作成して返す
  2530. /// </summary>
  2531. /// <param name="vertexCount"></param>
  2532. /// <param name="depthList"></param>
  2533. /// <returns></returns>
  2534. List<float> GetMeshVertexDepthList(int vertexCount, List<float> depthList)
  2535. {
  2536. var meshVertexDepthList = new List<float>();
  2537. for (int i = 0; i < vertexCount; i++)
  2538. {
  2539. float depth = 0;
  2540. int vindex = i;
  2541. int vi = useVertexList.IndexOf(vindex);
  2542. if (vi >= 0)
  2543. {
  2544. depth = depthList[vi];
  2545. }
  2546. meshVertexDepthList.Add(depth);
  2547. }
  2548. return meshVertexDepthList;
  2549. }
  2550. /// <summary>
  2551. /// 使用頂点ごとの親頂点をリスト化して返す
  2552. /// 親が発見できない場合は(-1)となる。
  2553. /// </summary>
  2554. /// <param name="scr"></param>
  2555. /// <param name="vertexCount"></param>
  2556. /// <param name="vlink">頂点が接続する他の頂点セット</param>
  2557. /// <param name="wposList"></param>
  2558. /// <param name="depthList"></param>
  2559. /// <returns></returns>
  2560. List<int> GetUseParentVertexList(int vertexCount, List<HashSet<int>> vlink, List<Vector3> wposList, List<float> depthList)
  2561. {
  2562. // アプローチ3
  2563. var parentArray = new List<int>();
  2564. List<int> sortIndexList = new List<int>();
  2565. List<int> fixedIndexList = new List<int>();
  2566. for (int i = 0; i < vertexCount; i++)
  2567. {
  2568. parentArray.Add(-1);
  2569. int index = useVertexList.IndexOf(i);
  2570. if (index < 0)
  2571. continue;
  2572. //Debug.Log("[" + i + "] vi:" + vi);
  2573. sortIndexList.Add(index);
  2574. if (IsFixedVertex(index))
  2575. fixedIndexList.Add(index);
  2576. }
  2577. // 最も近い固定頂点の座標と距離を求める
  2578. List<Vector3> nearFixedPosList = new List<Vector3>();
  2579. List<float> nearFixedDistList = new List<float>();
  2580. for (int i = 0; i < depthList.Count; i++)
  2581. {
  2582. Vector3 fpos = Vector3.zero;
  2583. float fdist = 0;
  2584. var vi = useVertexList[i];
  2585. var pos = wposList[vi];
  2586. float min = 10000;
  2587. Vector3 dir = Vector3.zero;
  2588. foreach (var fixindex in fixedIndexList)
  2589. {
  2590. int fvi = useVertexList[fixindex];
  2591. var len = Vector3.Distance(pos, wposList[fvi]);
  2592. if (len < min)
  2593. {
  2594. min = len;
  2595. fpos = wposList[fvi];
  2596. fdist = len;
  2597. }
  2598. }
  2599. nearFixedPosList.Add(fpos);
  2600. nearFixedDistList.Add(fdist);
  2601. }
  2602. if (fixedIndexList.Count > 0)
  2603. {
  2604. // デプスの降順でソート、同じ場合は最寄りの固定頂点距離の降順でソート
  2605. sortIndexList.Sort((a, b) =>
  2606. {
  2607. if (depthList[a] > depthList[b])
  2608. return -1;
  2609. if (depthList[a] < depthList[b])
  2610. return 1;
  2611. // 2nd
  2612. return nearFixedDistList[a] > nearFixedDistList[b] ? -1 : 1;
  2613. });
  2614. // デプスの降順で処理
  2615. HashSet<int> compSet = new HashSet<int>();
  2616. foreach (var index in sortIndexList)
  2617. {
  2618. if (IsMoveVertex(index) == false)
  2619. continue;
  2620. int vi = useVertexList[index];
  2621. var pos = wposList[vi];
  2622. // 最も近い固定頂点への方向
  2623. Vector3 dir = (nearFixedPosList[index] - pos).normalized;
  2624. #if false
  2625. // 現在の接続子の方向
  2626. if (childPosList[index].Count > 0)
  2627. {
  2628. Vector3 cdir = Vector3.zero;
  2629. foreach (var cpos in childPosList[index])
  2630. {
  2631. cdir += (pos - cpos).normalized;
  2632. }
  2633. cdir.Normalize();
  2634. // 今までの子の方向割合を強めにする
  2635. cdir *= 2.0f; // 2.0f?
  2636. // 最も近い固定頂点方向と平均化する
  2637. dir = (dir + cdir).normalized;
  2638. }
  2639. #endif
  2640. // 接続点の中で最も固定点方向へ近いものを親としてつなぐ
  2641. var vset = vlink[vi];
  2642. int parent = -1;
  2643. int pindex = -1;
  2644. Vector3 ppos = Vector3.zero;
  2645. float maxdot = -10000;
  2646. foreach (var vindex in vset)
  2647. {
  2648. var lindex = useVertexList.IndexOf(vindex);
  2649. if (lindex < 0)
  2650. continue;
  2651. if (depthList[lindex] > depthList[index])
  2652. continue;
  2653. // 無限ループ防止
  2654. int pvi = vindex;
  2655. while (parentArray[pvi] >= 0)
  2656. {
  2657. pvi = parentArray[pvi];
  2658. if (pvi == vi)
  2659. {
  2660. //Debug.Log("無限ループになるぞ->vi:" + vi);
  2661. break;
  2662. }
  2663. }
  2664. if (pvi == vi)
  2665. continue;
  2666. var linkpos = wposList[vindex];
  2667. Vector3 d = (linkpos - pos).normalized;
  2668. var dot = Vector3.Dot(dir, d);
  2669. if (dot > maxdot)
  2670. {
  2671. maxdot = dot;
  2672. parent = vindex;
  2673. pindex = lindex;
  2674. ppos = linkpos;
  2675. }
  2676. }
  2677. // 接続
  2678. if (pindex >= 0)
  2679. {
  2680. parentArray[vi] = parent;
  2681. }
  2682. }
  2683. }
  2684. var parentList = new List<int>();
  2685. foreach (var vindex in useVertexList)
  2686. parentList.Add(useVertexList.IndexOf(parentArray[vindex]));
  2687. // 最後に末端のパーティクルは親の親方向と合致する向きに切り替える
  2688. //for (int i = 0; i < useVertexList.Count; i++)
  2689. //{
  2690. // if (parentList.Contains(i) == false && selectionData[i] != SelectionData.Extend && IsMoveVertex(i))
  2691. // {
  2692. // // この頂点は末端
  2693. // Debug.Log("a:" + i);
  2694. // }
  2695. //}
  2696. return parentList;
  2697. }
  2698. #if false
  2699. /// <summary>
  2700. /// 使用頂点ごとの親頂点をリスト化して返す
  2701. /// 親が発見できない場合は(-1)となる。
  2702. /// 親が複数ある場合は最も距離が近い親を選択する
  2703. /// </summary>
  2704. /// <param name="scr"></param>
  2705. /// <param name="vertexCount"></param>
  2706. /// <param name="vlink">頂点が接続する他の頂点セット</param>
  2707. /// <param name="wposList"></param>
  2708. /// <param name="depthList"></param>
  2709. /// <returns></returns>
  2710. List<int> GetUseParentVertexList_Old(int vertexCount, List<HashSet<int>> vlink, List<Vector3> wposList, List<float> depthList)
  2711. {
  2712. // アプローチ2
  2713. int[] parentArray = new int[vertexCount];
  2714. int[] levelArray = new int[vertexCount];
  2715. HashSet<int> useVertexSet = new HashSet<int>();
  2716. List<int> restVertexList = new List<int>();
  2717. for (int i = 0; i < vertexCount; i++)
  2718. {
  2719. restVertexList.Add(i);
  2720. parentArray[i] = -1;
  2721. }
  2722. int level = 0;
  2723. while (restVertexList.Count > 0)
  2724. {
  2725. int oldcnt = useVertexSet.Count;
  2726. for (int i = 0; i < restVertexList.Count;)
  2727. {
  2728. int vindex = restVertexList[i];
  2729. int vi = useVertexList.IndexOf(vindex);
  2730. if (level == 0)
  2731. {
  2732. // 最初に無効、固定頂点を登録する
  2733. if (vi < 0 || IsInvalidVertex(vi) || IsFixedVertex(vi) || IsExtendVertex(vi))
  2734. {
  2735. parentArray[vindex] = -1;
  2736. useVertexSet.Add(vindex);
  2737. restVertexList.RemoveAt(i);
  2738. levelArray[vindex] = level;
  2739. continue;
  2740. }
  2741. }
  2742. else if (level == 1)
  2743. {
  2744. // 固定頂点につながる移動頂点を登録
  2745. int parentIndex = -1;
  2746. float mindist = 10000;
  2747. var vlist = vlink[vindex];
  2748. foreach (var vindex2 in vlist)
  2749. {
  2750. int vi2 = useVertexList.IndexOf(vindex2);
  2751. if (vi2 < 0)
  2752. continue;
  2753. if (IsFixedVertex(vi2) == false)
  2754. continue;
  2755. // 対象への距離判定
  2756. var v1 = wposList[vindex2] - wposList[vindex];
  2757. var len = v1.magnitude;
  2758. if (len < mindist)
  2759. {
  2760. parentIndex = vindex2;
  2761. mindist = len;
  2762. }
  2763. }
  2764. if (parentIndex >= 0)
  2765. {
  2766. parentArray[vindex] = parentIndex;
  2767. useVertexSet.Add(vindex);
  2768. restVertexList.RemoveAt(i);
  2769. levelArray[vindex] = level;
  2770. continue;
  2771. }
  2772. }
  2773. else
  2774. {
  2775. // 最後に固定頂点に隣接する移動頂点を計算
  2776. int parentIndex = -1;
  2777. float minang = 360.0f;
  2778. var vlist = vlink[vindex];
  2779. foreach (var vindex2 in vlist)
  2780. {
  2781. int vi2 = useVertexList.IndexOf(vindex2);
  2782. if (vi2 < 0)
  2783. continue;
  2784. if (IsMoveVertex(vi2) == false)
  2785. continue;
  2786. if (useVertexSet.Contains(vindex2) == false)
  2787. continue;
  2788. //if (depthList[vindex2] >= depthList[vindex])
  2789. // continue;
  2790. // レベル判定
  2791. if (levelArray[vindex2] >= level)
  2792. continue;
  2793. // 対象への方向と、その対象から親への方向を取得
  2794. var v1 = wposList[vindex2] - wposList[vindex];
  2795. var v2 = wposList[parentArray[vindex2]] - wposList[vindex2];
  2796. // 角度
  2797. var ang = Vector3.Angle(v1, v2);
  2798. if (ang < minang)
  2799. {
  2800. parentIndex = vindex2;
  2801. minang = ang;
  2802. }
  2803. }
  2804. if (parentIndex >= 0)
  2805. {
  2806. parentArray[vindex] = parentIndex;
  2807. useVertexSet.Add(vindex);
  2808. restVertexList.RemoveAt(i);
  2809. levelArray[vindex] = level;
  2810. continue;
  2811. }
  2812. }
  2813. i++;
  2814. }
  2815. level++;
  2816. // 内容に一切変化がない場合は終了
  2817. if (oldcnt == useVertexSet.Count)
  2818. break;
  2819. }
  2820. var parentList = new List<int>();
  2821. foreach (var vindex in useVertexList)
  2822. parentList.Add(useVertexList.IndexOf(parentArray[vindex]));
  2823. return parentList;
  2824. }
  2825. #endif
  2826. /// <summary>
  2827. /// 使用頂点ごとのルート頂点をリスト化して返す
  2828. /// ルートが発見できない場合は(-1)となる。
  2829. /// </summary>
  2830. /// <param name="parentVertexList"></param>
  2831. /// <returns></returns>
  2832. List<int> GetUseRootVertexList(List<int> parentVertexList)
  2833. {
  2834. List<int> rootList = new List<int>();
  2835. for (int i = 0; i < useVertexList.Count; i++)
  2836. {
  2837. int rootIndex = -1;
  2838. int parentIndex = parentVertexList[i];
  2839. while (parentIndex >= 0)
  2840. {
  2841. rootIndex = parentIndex;
  2842. parentIndex = parentVertexList[parentIndex];
  2843. }
  2844. rootList.Add(rootIndex);
  2845. }
  2846. return rootList;
  2847. }
  2848. /// <summary>
  2849. /// 頂点作業クラス
  2850. /// </summary>
  2851. private class VertexInfo
  2852. {
  2853. public int vertexIndex;
  2854. public int parentVertexIndex = -1;
  2855. public List<int> childVertexList = new List<int>();
  2856. public VertexInfo parentInfo;
  2857. public List<VertexInfo> childInfoList = new List<VertexInfo>();
  2858. }
  2859. /// <summary>
  2860. /// 頂点作業情報の作成
  2861. /// </summary>
  2862. /// <param name="parentVertexList"></param>
  2863. /// <returns></returns>
  2864. List<VertexInfo> GetUseVertexInfoList(List<int> parentVertexList)
  2865. {
  2866. List<VertexInfo> infoList = new List<VertexInfo>();
  2867. for (int i = 0; i < useVertexList.Count; i++)
  2868. {
  2869. var info = new VertexInfo();
  2870. info.vertexIndex = i;
  2871. infoList.Add(info);
  2872. }
  2873. for (int i = 0; i < useVertexList.Count; i++)
  2874. {
  2875. int parentIndex = parentVertexList[i];
  2876. if (parentIndex >= 0)
  2877. {
  2878. var info = infoList[i];
  2879. var pinfo = infoList[parentIndex];
  2880. info.parentVertexIndex = parentIndex;
  2881. info.parentInfo = pinfo;
  2882. pinfo.childVertexList.Add(i);
  2883. pinfo.childInfoList.Add(info);
  2884. }
  2885. }
  2886. return infoList;
  2887. }
  2888. /// <summary>
  2889. /// 使用頂点ベースのルートから続く一連の頂点リストを返す
  2890. /// </summary>
  2891. /// <param name="parentVertexList"></param>
  2892. /// <returns></returns>
  2893. List<List<int>> GetUseRootLineList(List<int> parentVertexList)
  2894. {
  2895. // 頂点情報作成
  2896. var linkList = GetUseVertexInfoList(parentVertexList);
  2897. // ルートごとのルートから続く一連のインデックスリストを構築
  2898. List<List<int>> rootLine = new List<List<int>>();
  2899. foreach (var link in linkList)
  2900. {
  2901. if (link.parentVertexIndex >= 0)
  2902. continue;
  2903. // ただしルートから分岐する子は別ラインとして登録する
  2904. for (int i = 0; i < link.childVertexList.Count; i++)
  2905. {
  2906. List<int> indexList = new List<int>();
  2907. Queue<VertexInfo> vq = new Queue<VertexInfo>();
  2908. indexList.Add(link.vertexIndex);
  2909. vq.Enqueue(linkList[link.childVertexList[i]]);
  2910. while (vq.Count > 0)
  2911. {
  2912. var vdata = vq.Dequeue();
  2913. indexList.Add(vdata.vertexIndex);
  2914. foreach (var cindex in vdata.childVertexList)
  2915. {
  2916. vq.Enqueue(linkList[cindex]);
  2917. }
  2918. }
  2919. rootLine.Add(indexList);
  2920. }
  2921. }
  2922. return rootLine;
  2923. }
  2924. }
  2925. }