ApplyToVfxGraph.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // You need to define AVPRO_PACKAGE_VFXGRAPH manually to use this script
  2. // We could set up the asmdef to reference the package, but the package doesn't
  3. // existing in Unity 2017 etc, and it throws an error due to missing reference
  4. //#define AVPRO_PACKAGE_VFXGRAPH
  5. #if (UNITY_2018_3_OR_NEWER && AVPRO_PACKAGE_VFXGRAPH)
  6. using UnityEngine;
  7. using UnityEngine.VFX;
  8. using RenderHeads.Media.AVProVideo;
  9. //-----------------------------------------------------------------------------
  10. // Copyright 2019-2021 RenderHeads Ltd. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12. namespace RenderHeads.Media.AVProVideo
  13. {
  14. /// <summary>
  15. /// Sets the texture from the MediaPlayer to a texture parameter in a VFX Graph
  16. /// </summary>
  17. [AddComponentMenu("AVPro Video/Apply To VFX Graph", 300)]
  18. [HelpURL("http://renderheads.com/products/avpro-video/")]
  19. public class ApplyToVfxGraph : MonoBehaviour
  20. {
  21. [Header("Media Source")]
  22. [SerializeField] MediaPlayer _mediaPlayer = null;
  23. [Tooltip("Default texture to display when the video texture is preparing")]
  24. [SerializeField] Texture2D _defaultTexture = null;
  25. [Space(8f)]
  26. [Header("VFX Target")]
  27. [SerializeField] VisualEffect _visualEffect = null;
  28. [SerializeField] string _texturePropertyName = string.Empty;
  29. public Texture2D DefaultTexture
  30. {
  31. get { return _defaultTexture; }
  32. set { if (_defaultTexture != value) { _defaultTexture = value; _isDirty = true; } }
  33. }
  34. public VisualEffect VisualEffect
  35. {
  36. get { return _visualEffect; }
  37. set { _visualEffect = value; _isDirty = true; }
  38. }
  39. public string TexturePropertyName
  40. {
  41. get { return _texturePropertyName; }
  42. set { _texturePropertyName = value; _textureProp = Shader.PropertyToID(_texturePropertyName); _isDirty = true; }
  43. }
  44. public MediaPlayer MediaPlayer
  45. {
  46. get { return _mediaPlayer; }
  47. set { ChangeMediaPlayer(value); }
  48. }
  49. private bool _isDirty = true;
  50. private int _textureProp = 0;
  51. void OnEnable()
  52. {
  53. // Force evaluations
  54. TexturePropertyName = _texturePropertyName;
  55. if (_mediaPlayer != null)
  56. {
  57. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  58. }
  59. }
  60. #if UNITY_EDITOR
  61. void OnValidate()
  62. {
  63. _isDirty = true;
  64. }
  65. #endif
  66. void OnDisable()
  67. {
  68. if (_mediaPlayer != null)
  69. {
  70. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  71. }
  72. }
  73. // Callback function to handle events
  74. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  75. {
  76. switch (et)
  77. {
  78. case MediaPlayerEvent.EventType.FirstFrameReady:
  79. case MediaPlayerEvent.EventType.PropertiesChanged:
  80. ForceUpdate();
  81. break;
  82. case MediaPlayerEvent.EventType.Closing:
  83. _isDirty = true; // Allow the update to happen on the next frame, as the video is still closing
  84. break;
  85. }
  86. }
  87. private void ChangeMediaPlayer(MediaPlayer player)
  88. {
  89. if (_mediaPlayer != player)
  90. {
  91. if (_mediaPlayer != null)
  92. {
  93. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  94. }
  95. _mediaPlayer = player;
  96. if (_mediaPlayer != null)
  97. {
  98. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  99. }
  100. _isDirty = true;
  101. }
  102. }
  103. public void ForceUpdate()
  104. {
  105. _isDirty = true;
  106. LateUpdate();
  107. }
  108. void LateUpdate()
  109. {
  110. if (_isDirty)
  111. {
  112. ApplyMapping();
  113. }
  114. }
  115. private void ApplyMapping()
  116. {
  117. if (_visualEffect != null && !string.IsNullOrEmpty(_texturePropertyName))
  118. {
  119. if (_mediaPlayer != null && _mediaPlayer.TextureProducer != null && _mediaPlayer.TextureProducer.GetTexture() != null)
  120. {
  121. _visualEffect.SetTexture(_textureProp, _mediaPlayer.TextureProducer.GetTexture());
  122. _isDirty = false;
  123. }
  124. else if (_defaultTexture != null)
  125. {
  126. _visualEffect.SetTexture(_textureProp, _defaultTexture);
  127. _isDirty = false;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. #endif