SkewingHandle.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace VLB
  4. {
  5. [ExecuteInEditMode]
  6. [HelpURL(Consts.HelpUrlSkewingHandle)]
  7. public class SkewingHandle : MonoBehaviour
  8. {
  9. public VolumetricLightBeam volumetricLightBeam = null;
  10. public bool shouldUpdateEachFrame = false;
  11. #if UNITY_EDITOR
  12. void Update()
  13. {
  14. if (!Application.isPlaying && CanSetSkewingVector())
  15. SetSkewingVector();
  16. }
  17. #endif
  18. public bool IsAttachedToSelf() { return volumetricLightBeam != null && volumetricLightBeam.gameObject == this.gameObject; }
  19. public bool CanSetSkewingVector() { return volumetricLightBeam != null && volumetricLightBeam.canHaveMeshSkewing; }
  20. public bool CanUpdateEachFrame() { return CanSetSkewingVector() && volumetricLightBeam.trackChangesDuringPlaytime; }
  21. bool ShouldUpdateEachFrame() { return shouldUpdateEachFrame && CanUpdateEachFrame(); }
  22. void OnEnable()
  23. {
  24. if(CanSetSkewingVector())
  25. SetSkewingVector();
  26. }
  27. void Start()
  28. {
  29. if (Application.isPlaying && ShouldUpdateEachFrame())
  30. {
  31. StartCoroutine(CoUpdate());
  32. }
  33. }
  34. IEnumerator CoUpdate()
  35. {
  36. while(ShouldUpdateEachFrame())
  37. {
  38. SetSkewingVector();
  39. yield return null;
  40. }
  41. }
  42. void SetSkewingVector()
  43. {
  44. Debug.Assert(CanSetSkewingVector());
  45. var vec = volumetricLightBeam.transform.InverseTransformPoint(transform.position);
  46. volumetricLightBeam.skewingLocalForwardDirection = vec;
  47. }
  48. }
  49. }