123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- // You need to define AVPRO_PACKAGE_VFXGRAPH manually to use this script
- // We could set up the asmdef to reference the package, but the package doesn't
- // existing in Unity 2017 etc, and it throws an error due to missing reference
- //#define AVPRO_PACKAGE_VFXGRAPH
- #if (UNITY_2018_3_OR_NEWER && AVPRO_PACKAGE_VFXGRAPH)
- using UnityEngine;
- using UnityEngine.VFX;
- using RenderHeads.Media.AVProVideo;
- //-----------------------------------------------------------------------------
- // Copyright 2019-2021 RenderHeads Ltd. All rights reserved.
- //-----------------------------------------------------------------------------
- namespace RenderHeads.Media.AVProVideo
- {
- /// <summary>
- /// Sets the texture from the MediaPlayer to a texture parameter in a VFX Graph
- /// </summary>
- [AddComponentMenu("AVPro Video/Apply To VFX Graph", 300)]
- [HelpURL("http://renderheads.com/products/avpro-video/")]
- public class ApplyToVfxGraph : MonoBehaviour
- {
- [Header("Media Source")]
- [SerializeField] MediaPlayer _mediaPlayer = null;
- [Tooltip("Default texture to display when the video texture is preparing")]
- [SerializeField] Texture2D _defaultTexture = null;
- [Space(8f)]
- [Header("VFX Target")]
- [SerializeField] VisualEffect _visualEffect = null;
- [SerializeField] string _texturePropertyName = string.Empty;
- public Texture2D DefaultTexture
- {
- get { return _defaultTexture; }
- set { if (_defaultTexture != value) { _defaultTexture = value; _isDirty = true; } }
- }
- public VisualEffect VisualEffect
- {
- get { return _visualEffect; }
- set { _visualEffect = value; _isDirty = true; }
- }
- public string TexturePropertyName
- {
- get { return _texturePropertyName; }
- set { _texturePropertyName = value; _textureProp = Shader.PropertyToID(_texturePropertyName); _isDirty = true; }
- }
- public MediaPlayer MediaPlayer
- {
- get { return _mediaPlayer; }
- set { ChangeMediaPlayer(value); }
- }
- private bool _isDirty = true;
- private int _textureProp = 0;
- void OnEnable()
- {
- // Force evaluations
- TexturePropertyName = _texturePropertyName;
- if (_mediaPlayer != null)
- {
- _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
- }
- }
- #if UNITY_EDITOR
- void OnValidate()
- {
- _isDirty = true;
- }
- #endif
- void OnDisable()
- {
- if (_mediaPlayer != null)
- {
- _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
- }
- }
- // Callback function to handle events
- private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
- {
- switch (et)
- {
- case MediaPlayerEvent.EventType.FirstFrameReady:
- case MediaPlayerEvent.EventType.PropertiesChanged:
- ForceUpdate();
- break;
- case MediaPlayerEvent.EventType.Closing:
- _isDirty = true; // Allow the update to happen on the next frame, as the video is still closing
- break;
- }
- }
- private void ChangeMediaPlayer(MediaPlayer player)
- {
- if (_mediaPlayer != player)
- {
- if (_mediaPlayer != null)
- {
- _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
- }
- _mediaPlayer = player;
- if (_mediaPlayer != null)
- {
- _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
- }
- _isDirty = true;
- }
- }
- public void ForceUpdate()
- {
- _isDirty = true;
- LateUpdate();
- }
- void LateUpdate()
- {
- if (_isDirty)
- {
- ApplyMapping();
- }
- }
- private void ApplyMapping()
- {
- if (_visualEffect != null && !string.IsNullOrEmpty(_texturePropertyName))
- {
- if (_mediaPlayer != null && _mediaPlayer.TextureProducer != null && _mediaPlayer.TextureProducer.GetTexture() != null)
- {
- _visualEffect.SetTexture(_textureProp, _mediaPlayer.TextureProducer.GetTexture());
- _isDirty = false;
- }
- else if (_defaultTexture != null)
- {
- _visualEffect.SetTexture(_textureProp, _defaultTexture);
- _isDirty = false;
- }
- }
- }
- }
- }
- #endif
|