123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN
- #define UNITY_PLATFORM_SUPPORTS_LINEAR
- #elif UNITY_IOS || UNITY_ANDROID
- #if UNITY_5_5_OR_NEWER || (UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3 && !UNITY_5_4)
- #define UNITY_PLATFORM_SUPPORTS_LINEAR
- #endif
- #endif
- #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0)
- #define UNITY_HELPATTRIB
- #endif
- using UnityEngine;
- //-----------------------------------------------------------------------------
- // Copyright 2015-2020 RenderHeads Ltd. All rights reserved.
- //-----------------------------------------------------------------------------
- namespace RenderHeads.Media.AVProVideo
- {
- /// <summary>
- /// Displays the video from MediaPlayer component using IMGUI
- /// </summary>
- [AddComponentMenu("AVPro Video/Display IMGUI", 200)]
- #if UNITY_HELPATTRIB
- [HelpURL("http://renderheads.com/products/avpro-video/")]
- #endif
- [ExecuteInEditMode]
- public class DisplayIMGUI : MonoBehaviour
- {
- private const string PropChromaTexName = "_ChromaTex";
- private const string PropYpCbCrTransformName = "_YpCbCrTransform";
- public MediaPlayer _mediaPlayer;
- public bool _displayInEditor = true;
- public ScaleMode _scaleMode = ScaleMode.ScaleToFit;
- public Color _color = Color.white;
- public bool _alphaBlend = false;
- [SerializeField]
- private bool _useDepth = false;
- public int _depth = 0;
- public bool _fullScreen = true;
- [Range(0f, 1f)]
- public float _x = 0.0f;
- [Range(0f, 1f)]
- public float _y = 0.0f;
- [Range(0f, 1f)]
- public float _width = 1.0f;
- [Range(0f, 1f)]
- public float _height = 1.0f;
- private static int _propAlphaPack;
- private static int _propVertScale;
- private static int _propApplyGamma;
- private static int _propChromaTex;
- private static int _propYpCbCrTransform;
- private static Shader _shaderAlphaPacking;
- private Material _material;
- void Awake()
- {
- if (_propAlphaPack == 0)
- {
- _propAlphaPack = Shader.PropertyToID("AlphaPack");
- _propVertScale = Shader.PropertyToID("_VertScale");
- _propApplyGamma = Shader.PropertyToID("_ApplyGamma");
- _propChromaTex = Shader.PropertyToID(PropChromaTexName);
- _propYpCbCrTransform = Shader.PropertyToID(PropYpCbCrTransformName);
- }
- }
- void Start()
- {
- // Disabling this lets you skip the GUI layout phase which helps performance, but this also breaks the GUI.depth usage.
- if (!_useDepth)
- {
- this.useGUILayout = false;
- }
- if (_shaderAlphaPacking == null)
- {
- _shaderAlphaPacking = Shader.Find("AVProVideo/IMGUI/Texture Transparent");
- if (_shaderAlphaPacking == null)
- {
- Debug.LogWarning("[AVProVideo] Missing shader AVProVideo/IMGUI/Transparent Packed");
- }
- }
- }
- void OnDestroy()
- {
- // Destroy existing material
- if (_material != null)
- {
- #if UNITY_EDITOR
- Material.DestroyImmediate(_material);
- #else
- Material.Destroy(_material);
- #endif
- _material = null;
- }
- }
- private Shader GetRequiredShader()
- {
- Shader result = null;
- switch (_mediaPlayer.m_AlphaPacking)
- {
- case AlphaPacking.None:
- break;
- case AlphaPacking.LeftRight:
- case AlphaPacking.TopBottom:
- result = _shaderAlphaPacking;
- break;
- }
- #if UNITY_PLATFORM_SUPPORTS_LINEAR
- if (result == null && _mediaPlayer.Info != null)
- {
- // If the player does support generating sRGB textures then we need to use a shader to convert them for display via IMGUI
- if (QualitySettings.activeColorSpace == ColorSpace.Linear && !_mediaPlayer.Info.PlayerSupportsLinearColorSpace())
- {
- result = _shaderAlphaPacking;
- }
- }
- #endif
- if (result == null && _mediaPlayer.TextureProducer != null)
- {
- if (_mediaPlayer.TextureProducer.GetTextureCount() == 2)
- {
- result = _shaderAlphaPacking;
- }
- }
- return result;
- }
- void Update()
- {
- if (_mediaPlayer != null)
- {
- // Get required shader
- Shader currentShader = null;
- if (_material != null)
- {
- currentShader = _material.shader;
- }
- Shader nextShader = GetRequiredShader();
- // If the shader requirement has changed
- if (currentShader != nextShader)
- {
- // Destroy existing material
- if (_material != null)
- {
- #if UNITY_EDITOR
- Material.DestroyImmediate(_material);
- #else
- Material.Destroy(_material);
- #endif
- _material = null;
- }
- // Create new material
- if (nextShader != null)
- {
- _material = new Material(nextShader);
- }
- }
- // Apply material changes
- if (_material != null)
- {
- if (_material.HasProperty(_propAlphaPack))
- {
- Helper.SetupAlphaPackedMaterial(_material, _mediaPlayer.m_AlphaPacking);
- }
- #if UNITY_PLATFORM_SUPPORTS_LINEAR
- // Apply gamma
- if (_material.HasProperty(_propApplyGamma) && _mediaPlayer.Info != null)
- {
- Helper.SetupGammaMaterial(_material, _mediaPlayer.Info.PlayerSupportsLinearColorSpace());
- }
- #else
- _propApplyGamma |= 0;
- #endif
- }
- }
- }
- void OnGUI()
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying && _displayInEditor)
- {
- GUI.depth = _depth;
- GUI.color = _color;
- Rect rect = GetRect();
- Texture2D icon = Resources.Load<Texture2D>("AVProVideoIcon");
- Rect uv = rect;
- uv.x /= Screen.width;
- uv.width /= Screen.width;
- uv.y /= Screen.height;
- uv.height /= Screen.height;
- uv.width *= 16f;
- uv.height *= 16f;
- uv.x += 0.5f;
- uv.y += 0.5f;
- GUI.DrawTextureWithTexCoords(rect, icon, uv);
- return;
- }
- #endif
- if (_mediaPlayer == null)
- {
- return;
- }
- bool requiresVerticalFlip = false;
- Texture texture = null;
- if (_displayInEditor)
- {
- #if UNITY_EDITOR
- texture = Texture2D.whiteTexture;
- #endif
- }
- if (_mediaPlayer.Info != null && !_mediaPlayer.Info.HasVideo())
- {
- texture = null;
- }
- if (_mediaPlayer.TextureProducer != null)
- {
- if (_mediaPlayer.m_Resample)
- {
- if (_mediaPlayer.FrameResampler.OutputTexture != null && _mediaPlayer.FrameResampler.OutputTexture[0] != null)
- {
- texture = _mediaPlayer.FrameResampler.OutputTexture[0];
- requiresVerticalFlip = _mediaPlayer.TextureProducer.RequiresVerticalFlip();
- }
- }
- else
- {
- if (_mediaPlayer.TextureProducer.GetTexture() != null)
- {
- texture = _mediaPlayer.TextureProducer.GetTexture();
- requiresVerticalFlip = _mediaPlayer.TextureProducer.RequiresVerticalFlip();
- }
- }
- if (_mediaPlayer.TextureProducer.GetTextureCount() == 2 && _material != null)
- {
- Texture resamplerTex = _mediaPlayer.FrameResampler == null || _mediaPlayer.FrameResampler.OutputTexture == null ? null : _mediaPlayer.FrameResampler.OutputTexture[1];
- Texture chroma = _mediaPlayer.m_Resample ? resamplerTex : _mediaPlayer.TextureProducer.GetTexture(1);
- _material.SetTexture(_propChromaTex, chroma);
- _material.SetMatrix(_propYpCbCrTransform, _mediaPlayer.TextureProducer.GetYpCbCrTransform());
- _material.EnableKeyword("USE_YPCBCR");
- }
- }
- if (texture != null)
- {
- if (!_alphaBlend || _color.a > 0f)
- {
- GUI.depth = _depth;
- GUI.color = _color;
- Rect rect = GetRect();
- if (_material != null)
- {
- if (requiresVerticalFlip)
- {
- _material.SetFloat(_propVertScale, -1f);
- }
- else
- {
- _material.SetFloat(_propVertScale, 1f);
- }
- #if UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
- if (QualitySettings.activeColorSpace == ColorSpace.Linear && !GL.sRGBWrite)
- {
- // It seems that Graphics.Draw texture behaves differently than GUI.DrawTexture when it comes to sRGB writing
- // on newer versions of Unity (at least 2018.2.19 and above), so now we have to force the conversion to sRGB on writing
- GL.sRGBWrite = true;
- Helper.DrawTexture(rect, texture, _scaleMode, _mediaPlayer.m_AlphaPacking, _material);
- GL.sRGBWrite = false;
- }
- else
- {
- Helper.DrawTexture(rect, texture, _scaleMode, _mediaPlayer.m_AlphaPacking, _material);
- }
- #else
- Helper.DrawTexture(rect, texture, _scaleMode, _mediaPlayer.m_AlphaPacking, _material);
- #endif
- }
- else
- {
- if (requiresVerticalFlip)
- {
- GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(0f, rect.y + (rect.height / 2f)));
- }
- GUI.DrawTexture(rect, texture, _scaleMode, _alphaBlend);
- }
- }
- }
- }
- public Rect GetRect()
- {
- Rect rect;
- if (_fullScreen)
- {
- rect = new Rect(0.0f, 0.0f, Screen.width, Screen.height);
- }
- else
- {
- rect = new Rect(_x * (Screen.width - 1), _y * (Screen.height - 1), _width * Screen.width, _height * Screen.height);
- }
- return rect;
- }
- }
- }
|