WindComponent.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. namespace MagicaCloth
  7. {
  8. /// <summary>
  9. /// 風コンポーネントの基底クラス
  10. /// </summary>
  11. public abstract partial class WindComponent : BaseComponent
  12. {
  13. [SerializeField]
  14. [Range(0.0f, Define.Compute.MaxWindMain)]
  15. protected float main = 5.0f;
  16. [SerializeField]
  17. [Range(0.0f, 1.0f)]
  18. protected float turbulence = 1.0f;
  19. [SerializeField]
  20. [Range(0.0f, 1.0f)]
  21. protected float frequency = 1.0f;
  22. [SerializeField]
  23. protected Vector3 areaSize = new Vector3(5.0f, 5.0f, 5.0f);
  24. [SerializeField]
  25. protected float areaRadius = 5.0f;
  26. //[SerializeField]
  27. //protected Vector3 anchor;
  28. [SerializeField]
  29. [Range(-180, 180)]
  30. protected float directionAngleX = 0;
  31. [SerializeField]
  32. [Range(-180, 180)]
  33. protected float directionAngleY = 0;
  34. [SerializeField]
  35. protected PhysicsManagerWindData.DirectionType directionType;
  36. [SerializeField]
  37. protected BezierParam attenuation = new BezierParam(1f, 1f, false, 0.0f, false);
  38. //=========================================================================================
  39. /// <summary>
  40. /// 風データID
  41. /// </summary>
  42. protected int windId = -1;
  43. /// <summary>
  44. /// 実行状態
  45. /// </summary>
  46. protected RuntimeStatus status = new RuntimeStatus();
  47. internal RuntimeStatus Status
  48. {
  49. get
  50. {
  51. return status;
  52. }
  53. }
  54. //=========================================================================================
  55. protected virtual void Reset()
  56. {
  57. ResetParams();
  58. }
  59. protected virtual void OnValidate()
  60. {
  61. //anchor = math.clamp(anchor, -1, 1);
  62. areaSize = math.max(areaSize, 0.1f);
  63. areaRadius = math.max(areaRadius, 0.1f);
  64. if (Application.isPlaying)
  65. status.SetDirty();
  66. }
  67. // Animator/Animationによるプロパティ変更時コールバック
  68. void OnDidApplyAnimationProperties()
  69. {
  70. if (Application.isPlaying)
  71. {
  72. status.SetDirty();
  73. }
  74. }
  75. protected virtual void Start()
  76. {
  77. Init();
  78. }
  79. internal virtual void OnEnable()
  80. {
  81. status.SetEnable(true);
  82. status.UpdateStatus();
  83. }
  84. internal virtual void OnDisable()
  85. {
  86. status.SetEnable(false);
  87. status.UpdateStatus();
  88. }
  89. protected virtual void OnDestroy()
  90. {
  91. OnDispose();
  92. status.SetDispose();
  93. }
  94. protected virtual void Update()
  95. {
  96. if (status.IsInitSuccess)
  97. {
  98. var error = !VerifyData();
  99. status.SetRuntimeError(error);
  100. status.UpdateStatus();
  101. if (status.IsActive)
  102. OnUpdate();
  103. }
  104. }
  105. //=========================================================================================
  106. /// <summary>
  107. /// 初期化
  108. /// 通常はStart()で呼ぶ
  109. /// </summary>
  110. /// <param name="vcnt"></param>
  111. void Init()
  112. {
  113. status.UpdateStatusAction = OnUpdateStatus;
  114. status.OwnerFunc = () => this;
  115. if (status.IsInitComplete || status.IsInitStart)
  116. return;
  117. status.SetInitStart();
  118. if (VerifyData() == false)
  119. {
  120. status.SetInitError();
  121. return;
  122. }
  123. OnInit();
  124. if (status.IsInitError)
  125. return;
  126. status.SetInitComplete();
  127. status.UpdateStatus();
  128. }
  129. // 実行状態の更新
  130. protected void OnUpdateStatus()
  131. {
  132. if (status.IsActive)
  133. {
  134. // 実行状態に入った
  135. OnActive();
  136. }
  137. else
  138. {
  139. // 実行状態から抜けた
  140. OnInactive();
  141. }
  142. }
  143. /// <summary>
  144. /// 現在のデータが正常(実行できる状態)か返す
  145. /// </summary>
  146. /// <returns></returns>
  147. internal virtual bool VerifyData()
  148. {
  149. return true;
  150. }
  151. //=========================================================================================
  152. /// <summary>
  153. /// 初期化
  154. /// </summary>
  155. protected virtual void OnInit()
  156. {
  157. // 風作成
  158. CreateWind();
  159. // すでにアクティブならば有効化
  160. if (Status.IsActive)
  161. EnableWind();
  162. }
  163. /// <summary>
  164. /// 破棄
  165. /// </summary>
  166. protected virtual void OnDispose()
  167. {
  168. if (MagicaPhysicsManager.IsInstance() == false)
  169. return;
  170. // 風を破棄する
  171. RemoveWind();
  172. }
  173. /// <summary>
  174. /// 更新
  175. /// </summary>
  176. protected virtual void OnUpdate()
  177. {
  178. // 内容変更に伴う再設定
  179. if (status.IsDirty)
  180. {
  181. status.ClearDirty();
  182. ChangeParameter();
  183. }
  184. }
  185. /// <summary>
  186. /// 実行状態に入った場合に呼ばれます
  187. /// </summary>
  188. protected virtual void OnActive()
  189. {
  190. // 風有効化
  191. EnableWind();
  192. }
  193. /// <summary>
  194. /// 実行状態から抜けた場合に呼ばれます
  195. /// </summary>
  196. protected virtual void OnInactive()
  197. {
  198. // 風無効化
  199. DisableWind();
  200. }
  201. //=========================================================================================
  202. /// <summary>
  203. /// 風有効化
  204. /// </summary>
  205. protected void EnableWind()
  206. {
  207. if (windId >= 0)
  208. MagicaPhysicsManager.Instance.Wind.SetEnable(windId, true, transform);
  209. }
  210. /// <summary>
  211. /// 風無効化
  212. /// </summary>
  213. protected void DisableWind()
  214. {
  215. if (MagicaPhysicsManager.IsInstance() == false)
  216. return;
  217. if (windId >= 0)
  218. MagicaPhysicsManager.Instance.Wind.SetEnable(windId, false, transform);
  219. }
  220. //=========================================================================================
  221. /// <summary>
  222. /// 風削除
  223. /// </summary>
  224. private void RemoveWind()
  225. {
  226. if (MagicaPhysicsManager.IsInstance())
  227. {
  228. if (windId >= 0)
  229. {
  230. MagicaPhysicsManager.Instance.Wind.RemoveWind(windId);
  231. }
  232. }
  233. windId = -1;
  234. }
  235. /// <summary>
  236. /// 風作成
  237. /// </summary>
  238. private void CreateWind()
  239. {
  240. windId = MagicaPhysicsManager.Instance.Wind.CreateWind(
  241. GetWindType(), GetShapeType(), GetAreaSize(), IsAddition(), main, turbulence, frequency,
  242. GetLocalDirection(), GetDirectionType(), GetAreaVolume(), GetAreaLength(),
  243. attenuation
  244. );
  245. status.ClearDirty();
  246. }
  247. /// <summary>
  248. /// 現在の風方向(ローカル)
  249. /// </summary>
  250. /// <returns></returns>
  251. internal Vector3 GetLocalDirection()
  252. {
  253. var q = Quaternion.Euler(directionAngleX, directionAngleY, 0.0f);
  254. return q * Vector3.forward;
  255. //var rot = transform.rotation * q;
  256. //return rot * Vector3.forward;
  257. }
  258. /// <summary>
  259. /// 風パラメータの変更設定
  260. /// </summary>
  261. private void ChangeParameter()
  262. {
  263. if (windId >= 0)
  264. {
  265. MagicaPhysicsManager.Instance.Wind.SetParameter(
  266. windId, GetAreaSize(), IsAddition(), main, turbulence, frequency,
  267. GetLocalDirection(), GetAreaVolume(), GetAreaLength(),
  268. attenuation
  269. );
  270. }
  271. }
  272. //=========================================================================================
  273. /// <summary>
  274. /// 風タイプを返す
  275. /// </summary>
  276. /// <returns></returns>
  277. public abstract PhysicsManagerWindData.WindType GetWindType();
  278. /// <summary>
  279. /// 形状タイプを返す
  280. /// </summary>
  281. /// <returns></returns>
  282. public abstract PhysicsManagerWindData.ShapeType GetShapeType();
  283. /// <summary>
  284. /// 風向きタイプを返す
  285. /// </summary>
  286. /// <returns></returns>
  287. public abstract PhysicsManagerWindData.DirectionType GetDirectionType();
  288. /// <summary>
  289. /// 風が加算モードか返す
  290. /// </summary>
  291. /// <returns></returns>
  292. public abstract bool IsAddition();
  293. /// <summary>
  294. /// エリアサイズを返す
  295. /// </summary>
  296. /// <returns></returns>
  297. public abstract Vector3 GetAreaSize();
  298. /// <summary>
  299. /// アンカー位置を返す
  300. /// </summary>
  301. /// <returns></returns>
  302. //public abstract Vector3 GetAnchor();
  303. /// <summary>
  304. /// 風エリアの体積を返す
  305. /// </summary>
  306. /// <returns></returns>
  307. public abstract float GetAreaVolume();
  308. /// <summary>
  309. /// 風エリアの最大距離を返す
  310. /// </summary>
  311. /// <returns></returns>
  312. public abstract float GetAreaLength();
  313. /// <summary>
  314. /// パラメータ初期化
  315. /// </summary>
  316. protected abstract void ResetParams();
  317. }
  318. }