MfxController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using UnityEngine;
  3. namespace MaterializationFX.Scripts
  4. {
  5. internal sealed class MfxController : MonoBehaviour
  6. {
  7. private const string PositionPropertyName = "_Position";
  8. private const string DirectionPropertyName = "_Direction";
  9. private const string PositionTypePropertyName = "_PositionType";
  10. public MfxType MfxShaderType;
  11. public MfxDirection MfxDirection;
  12. public PositionType PositionType;
  13. public AnimationCurve Position;
  14. public AnimationCurve PositionX;
  15. public AnimationCurve PositionY;
  16. public AnimationCurve PositionZ;
  17. public float ScaleTime = 1;
  18. public float ScalePosition = 1;
  19. public bool MofidyChildren;
  20. public GameObject TargetObject;
  21. public bool ByDistance;
  22. public Vector3 WorldPositionOffset;
  23. private float _startTime;
  24. private bool _isEnabled;
  25. private string _shaderName;
  26. private ShaderParameterSetter _shaderParameterSetter;
  27. private void Start()
  28. {
  29. var shaderName = MfxShaderType.GetFullShaderName();
  30. GameObject go;
  31. if (TargetObject != null && ByDistance)
  32. go = gameObject;
  33. else if (TargetObject != null)
  34. go = TargetObject;
  35. else
  36. go = gameObject;
  37. _shaderParameterSetter = new ShaderParameterSetter();
  38. _shaderParameterSetter.Init(go, shaderName, modifyChildren: MofidyChildren);
  39. var dissolveDirection = MfxDirection.ToVector3();
  40. _shaderParameterSetter.SetVector(DirectionPropertyName, dissolveDirection);
  41. _shaderParameterSetter.SetInt(PositionTypePropertyName, (int) PositionType);
  42. _startTime = Time.time;
  43. }
  44. private void Update()
  45. {
  46. if (!_isEnabled)
  47. return;
  48. if (TargetObject != null)
  49. {
  50. Vector3 worldPos;
  51. if (ByDistance)
  52. worldPos = TargetObject.transform.position - transform.position + WorldPositionOffset;
  53. else
  54. worldPos = transform.position + WorldPositionOffset;
  55. _shaderParameterSetter.SetVector(DirectionPropertyName, worldPos);
  56. return;
  57. }
  58. var time = Time.time - _startTime;
  59. switch (PositionType)
  60. {
  61. case PositionType.Local:
  62. var position = Position.Evaluate(time / ScaleTime) * ScalePosition;
  63. _shaderParameterSetter.SetFloat(PositionPropertyName, position);
  64. break;
  65. case PositionType.World:
  66. var posX = transform.position.x + PositionX.Evaluate(time / ScaleTime) * ScalePosition;
  67. var posY = transform.position.y + PositionY.Evaluate(time / ScaleTime) * ScalePosition;
  68. var posZ = transform.position.z + PositionZ.Evaluate(time / ScaleTime) * ScalePosition;
  69. var vector3 = new Vector3(posX, posY, posZ);
  70. _shaderParameterSetter.SetVector(DirectionPropertyName, vector3 + WorldPositionOffset);
  71. break;
  72. }
  73. }
  74. private void OnEnable()
  75. {
  76. _isEnabled = true;
  77. }
  78. private void OnDisable()
  79. {
  80. _isEnabled = false;
  81. }
  82. }
  83. internal enum MfxType
  84. {
  85. SingleAlbedo,
  86. TwoAlbedo,
  87. }
  88. internal enum MfxDirection
  89. {
  90. None,
  91. Normal,
  92. XAxys,
  93. YAxis,
  94. ZAxis
  95. }
  96. internal enum PositionType
  97. {
  98. Local,
  99. World
  100. }
  101. internal static class MfxControllerExtensions
  102. {
  103. public static string GetFullShaderName(this MfxType mfxType)
  104. {
  105. switch (mfxType)
  106. {
  107. case MfxType.SingleAlbedo:
  108. return "QFX/MFX/MfxSingleAlbedo";
  109. case MfxType.TwoAlbedo:
  110. return "QFX/MFX/MfxTwoAlbedo";
  111. default:
  112. throw new ArgumentOutOfRangeException("mfxType", mfxType, null);
  113. }
  114. }
  115. public static Vector3 ToVector3(this MfxDirection mfxDirection)
  116. {
  117. switch (mfxDirection)
  118. {
  119. case MfxDirection.None:
  120. case MfxDirection.Normal:
  121. return new Vector3(0, 0, 0);
  122. case MfxDirection.XAxys:
  123. return new Vector3(1, 0, 0);
  124. case MfxDirection.YAxis:
  125. return new Vector3(0, 1, 0);
  126. case MfxDirection.ZAxis:
  127. return new Vector3(0, 0, 1);
  128. default:
  129. return Vector3.zero;
  130. }
  131. }
  132. }
  133. }