WindController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace MagicaCloth
  7. {
  8. public class WindController : MonoBehaviour
  9. {
  10. [SerializeField]
  11. private WindZone unityWindZone = null;
  12. [SerializeField]
  13. private float unityWindZoneScale = 0.1f;
  14. [SerializeField]
  15. private Renderer arrowRenderer = null;
  16. [SerializeField]
  17. private Gradient arrowGradient = new Gradient();
  18. [SerializeField]
  19. private List<Transform> rotationTransforms = new List<Transform>();
  20. [SerializeField]
  21. private GameObject blastWavePrefab;
  22. [SerializeField]
  23. private float blastWaveSpawnRadius = 3.0f;
  24. private float angleY = 0.0f;
  25. private float angleX = 0.0f;
  26. void Start()
  27. {
  28. }
  29. public void OnDirectionY(float value)
  30. {
  31. angleY = value;
  32. UpdateDirection();
  33. }
  34. public void OnDirectionX(float value)
  35. {
  36. angleX = value;
  37. UpdateDirection();
  38. }
  39. public void OnMain(float value)
  40. {
  41. Wind.Main = value;
  42. // Link Unit Wind Zone
  43. if (unityWindZone)
  44. {
  45. unityWindZone.windMain = value * unityWindZoneScale;
  46. }
  47. // arrow color
  48. if (arrowRenderer)
  49. {
  50. var t = Mathf.InverseLerp(0.0f, 50.0f, value);
  51. var col = arrowGradient.Evaluate(t);
  52. arrowRenderer.material.color = col;
  53. }
  54. }
  55. public void OnTurbulence(float value)
  56. {
  57. Wind.Turbulence = value;
  58. }
  59. public void OnFrequency(float value)
  60. {
  61. Wind.Frequency = value;
  62. }
  63. public void OnBlastWave()
  64. {
  65. if (blastWavePrefab == null)
  66. return;
  67. // position
  68. var lpos = Random.insideUnitSphere * blastWaveSpawnRadius;
  69. lpos.y = 0;
  70. var pos = transform.TransformPoint(lpos);
  71. // spawn blast wave
  72. Instantiate(blastWavePrefab, pos, Quaternion.identity);
  73. }
  74. private MagicaDirectionalWind Wind
  75. {
  76. get
  77. {
  78. return GetComponent<MagicaDirectionalWind>();
  79. }
  80. }
  81. private void UpdateDirection()
  82. {
  83. var lrot = Quaternion.Euler(angleX, angleY, 0.0f);
  84. foreach (var t in rotationTransforms)
  85. if (t)
  86. t.localRotation = lrot;
  87. //transform.rotation = Quaternion.Euler(angleX, angleY, 0.0f);
  88. Wind.DirectionAngleX = angleX;
  89. Wind.DirectionAngleY = angleY;
  90. }
  91. }
  92. }