ReductionMesh.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace MagicaReductionMesh
  7. {
  8. /// <summary>
  9. /// リダクションメッシュ
  10. /// ・複数のメッシュを1つにマージ
  11. /// ・リダクション機能
  12. /// ・加工後のメッシュデータ取得
  13. /// </summary>
  14. public class ReductionMesh
  15. {
  16. /// <summary>
  17. /// 結合頂点ウエイト計算方式
  18. /// </summary>
  19. public enum ReductionWeightMode
  20. {
  21. /// <summary>
  22. /// 共有頂点からの距離によるウエイト付(従来方式)
  23. /// </summary>
  24. Distance = 0,
  25. /// <summary>
  26. /// 共有頂点に属するメッシュ頂点ウエイトの平均値
  27. /// </summary>
  28. Average = 1,
  29. /// <summary>
  30. /// 共有頂点からの距離によりウエイト(改良版)
  31. /// </summary>
  32. DistanceAverage = 2,
  33. }
  34. public ReductionWeightMode WeightMode { get; set; } = ReductionWeightMode.Distance;
  35. //=========================================================================================
  36. private MeshData meshData = new MeshData();
  37. private ReductionData reductionData = new ReductionData();
  38. private DebugData debugData = new DebugData();
  39. //=========================================================================================
  40. public MeshData MeshData
  41. {
  42. get
  43. {
  44. meshData.SetParent(this);
  45. return meshData;
  46. }
  47. }
  48. public ReductionData ReductionData
  49. {
  50. get
  51. {
  52. reductionData.SetParent(this);
  53. return reductionData;
  54. }
  55. }
  56. public DebugData DebugData
  57. {
  58. get
  59. {
  60. debugData.SetParent(this);
  61. return debugData;
  62. }
  63. }
  64. //=========================================================================================
  65. /// <summary>
  66. /// メッシュを追加する
  67. /// 登録したメッシュインデックスを返す
  68. /// </summary>
  69. /// <param name="isSkinning"></param>
  70. /// <param name="mesh"></param>
  71. /// <param name="bones"></param>
  72. public int AddMesh(bool isSkinning, Mesh mesh, List<Transform> bones, Matrix4x4[] bindPoseList, BoneWeight[] boneWeightList)
  73. {
  74. return MeshData.AddMesh(isSkinning, mesh, bones, bindPoseList, boneWeightList);
  75. }
  76. /// <summary>
  77. /// メッシュを追加する
  78. /// 登録したメッシュインデックスを返す(-1=エラー)
  79. /// </summary>
  80. /// <param name="ren"></param>
  81. public int AddMesh(Renderer ren)
  82. {
  83. if (ren == null)
  84. {
  85. Debug.LogError("Renderer is NUll!");
  86. return -1;
  87. }
  88. if (ren is SkinnedMeshRenderer)
  89. {
  90. var sren = ren as SkinnedMeshRenderer;
  91. return MeshData.AddMesh(true, sren.sharedMesh, new List<Transform>(sren.bones), sren.sharedMesh.bindposes, sren.sharedMesh.boneWeights);
  92. }
  93. else
  94. {
  95. var mfilter = ren.GetComponent<MeshFilter>();
  96. var bones = new List<Transform>();
  97. bones.Add(ren.transform);
  98. return MeshData.AddMesh(false, mfilter.sharedMesh, bones, null, null);
  99. }
  100. }
  101. /// <summary>
  102. /// メッシュを追加する
  103. /// 登録したメッシュインデックスを返す
  104. /// </summary>
  105. /// <param name="root"></param>
  106. /// <param name="posList"></param>
  107. /// <param name="norList"></param>
  108. /// <param name="tanList"></param>
  109. /// <param name="uvList"></param>
  110. /// <returns></returns>
  111. public int AddMesh(Transform root, List<Vector3> posList, List<Vector3> norList = null, List<Vector4> tanList = null, List<Vector2> uvList = null, List<int> triangleList = null)
  112. {
  113. return MeshData.AddMesh(root, posList, norList, tanList, uvList, triangleList);
  114. }
  115. /// <summary>
  116. /// リダクション実行
  117. /// </summary>
  118. /// <param name="zeroRadius">重複頂点のマージ距離(0.0f=実行しない)</param>
  119. /// <param name="radius">周辺頂点のマージ距離(0.0f=実行しない)</param>
  120. /// <param name="polygonLength">ポリゴン接続のマージ距離(0.0f=実行しない)</param>
  121. public void Reduction(float zeroRadius, float radius, float polygonLength, bool createTetra)
  122. {
  123. // ゼロ距離頂点をマージする
  124. if (zeroRadius > 0.0f)
  125. ReductionData.ReductionZeroDistance(zeroRadius);
  126. // 範囲内の頂点をマージする
  127. if (radius > 0.0f)
  128. ReductionData.ReductionRadius(radius);
  129. // ポリゴン接続から範囲内の頂点をマージする
  130. if (polygonLength > 0.0f)
  131. ReductionData.ReductionPolygonLink(polygonLength);
  132. // メッシュデータ更新
  133. MeshData.UpdateMeshData(createTetra);
  134. // 未使用ボーンの削除
  135. ReductionData.ReductionBone();
  136. }
  137. /// <summary>
  138. /// 最終メッシュデータを計算して返す
  139. /// 子頂点の親頂点に対するスキニングが不要な場合は(weightLength=0)に設定します。
  140. /// </summary>
  141. /// <param name="root">メッシュの基準トランスフォーム(この姿勢を元にローカル座標変換される)</param>
  142. public FinalData GetFinalData(Transform root)
  143. {
  144. return MeshData.GetFinalData(root);
  145. }
  146. }
  147. }