DisplayIMGUI.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using UnityEngine;
  3. namespace RenderHeads.Media.AVProVideo
  4. {
  5. [AddComponentMenu("AVPro Video/Display IMGUI", 200)]
  6. [HelpURL("http://renderheads.com/product/avpro-video/")]
  7. [ExecuteInEditMode]
  8. public class DisplayIMGUI : MonoBehaviour
  9. {
  10. private void Awake()
  11. {
  12. if (DisplayIMGUI._propAlphaPack == 0)
  13. {
  14. DisplayIMGUI._propAlphaPack = Shader.PropertyToID("AlphaPack");
  15. DisplayIMGUI._propVertScale = Shader.PropertyToID("_VertScale");
  16. DisplayIMGUI._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
  17. DisplayIMGUI._propChromaTex = Shader.PropertyToID("_ChromaTex");
  18. }
  19. }
  20. private void Start()
  21. {
  22. if (!this._useDepth)
  23. {
  24. base.useGUILayout = false;
  25. }
  26. if (DisplayIMGUI._shaderAlphaPacking == null)
  27. {
  28. DisplayIMGUI._shaderAlphaPacking = Shader.Find("AVProVideo/IMGUI/Texture Transparent");
  29. if (DisplayIMGUI._shaderAlphaPacking == null)
  30. {
  31. Debug.LogWarning("[AVProVideo] Missing shader AVProVideo/IMGUI/Transparent Packed");
  32. }
  33. }
  34. }
  35. private void OnDestroy()
  36. {
  37. if (this._material != null)
  38. {
  39. UnityEngine.Object.Destroy(this._material);
  40. this._material = null;
  41. }
  42. }
  43. private Shader GetRequiredShader()
  44. {
  45. Shader shader = null;
  46. AlphaPacking alphaPacking = this._mediaPlayer.m_AlphaPacking;
  47. if (alphaPacking != AlphaPacking.None)
  48. {
  49. if (alphaPacking == AlphaPacking.LeftRight || alphaPacking == AlphaPacking.TopBottom)
  50. {
  51. shader = DisplayIMGUI._shaderAlphaPacking;
  52. }
  53. }
  54. if (shader == null && this._mediaPlayer.Info != null && QualitySettings.activeColorSpace == ColorSpace.Linear && this._mediaPlayer.Info.PlayerSupportsLinearColorSpace())
  55. {
  56. shader = DisplayIMGUI._shaderAlphaPacking;
  57. }
  58. if (shader == null && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
  59. {
  60. shader = DisplayIMGUI._shaderAlphaPacking;
  61. }
  62. return shader;
  63. }
  64. private void Update()
  65. {
  66. if (this._mediaPlayer != null)
  67. {
  68. Shader x = null;
  69. if (this._material != null)
  70. {
  71. x = this._material.shader;
  72. }
  73. Shader requiredShader = this.GetRequiredShader();
  74. if (x != requiredShader)
  75. {
  76. if (this._material != null)
  77. {
  78. UnityEngine.Object.Destroy(this._material);
  79. this._material = null;
  80. }
  81. if (requiredShader != null)
  82. {
  83. this._material = new Material(requiredShader);
  84. }
  85. }
  86. if (this._material != null)
  87. {
  88. if (this._material.HasProperty(DisplayIMGUI._propAlphaPack))
  89. {
  90. Helper.SetupAlphaPackedMaterial(this._material, this._mediaPlayer.m_AlphaPacking);
  91. }
  92. if (this._material.HasProperty(DisplayIMGUI._propApplyGamma) && this._mediaPlayer.Info != null)
  93. {
  94. Helper.SetupGammaMaterial(this._material, !this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
  95. }
  96. }
  97. }
  98. }
  99. private void OnGUI()
  100. {
  101. if (this._mediaPlayer == null)
  102. {
  103. return;
  104. }
  105. bool flag = false;
  106. Texture texture = null;
  107. if (this._displayInEditor)
  108. {
  109. }
  110. if (this._mediaPlayer.Info != null && !this._mediaPlayer.Info.HasVideo())
  111. {
  112. texture = null;
  113. }
  114. if (this._mediaPlayer.TextureProducer != null)
  115. {
  116. if (((!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(0) : this._mediaPlayer.FrameResampler.OutputTexture[0]) != null)
  117. {
  118. texture = ((!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(0) : this._mediaPlayer.FrameResampler.OutputTexture[0]);
  119. flag = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
  120. }
  121. if (this._mediaPlayer.TextureProducer.GetTextureCount() == 2 && this._material != null)
  122. {
  123. Texture texture2 = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[1] : null;
  124. Texture value = (!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(1) : texture2;
  125. this._material.SetTexture(DisplayIMGUI._propChromaTex, value);
  126. this._material.EnableKeyword("USE_YPCBCR");
  127. }
  128. }
  129. if (texture != null && (!this._alphaBlend || this._color.a > 0f))
  130. {
  131. GUI.depth = this._depth;
  132. GUI.color = this._color;
  133. Rect rect = this.GetRect();
  134. if (this._material != null)
  135. {
  136. if (flag)
  137. {
  138. this._material.SetFloat(DisplayIMGUI._propVertScale, -1f);
  139. }
  140. else
  141. {
  142. this._material.SetFloat(DisplayIMGUI._propVertScale, 1f);
  143. }
  144. Helper.DrawTexture(rect, texture, this._scaleMode, this._mediaPlayer.m_AlphaPacking, this._material);
  145. }
  146. else
  147. {
  148. if (flag)
  149. {
  150. GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(0f, rect.y + rect.height / 2f));
  151. }
  152. GUI.DrawTexture(rect, texture, this._scaleMode, this._alphaBlend);
  153. }
  154. }
  155. }
  156. public Rect GetRect()
  157. {
  158. Rect result;
  159. if (this._fullScreen)
  160. {
  161. result = new Rect(0f, 0f, (float)Screen.width, (float)Screen.height);
  162. }
  163. else
  164. {
  165. result = new Rect(this._x * (float)(Screen.width - 1), this._y * (float)(Screen.height - 1), this._width * (float)Screen.width, this._height * (float)Screen.height);
  166. }
  167. return result;
  168. }
  169. private const string PropChromaTexName = "_ChromaTex";
  170. public MediaPlayer _mediaPlayer;
  171. public bool _displayInEditor = true;
  172. public ScaleMode _scaleMode = ScaleMode.ScaleToFit;
  173. public Color _color = Color.white;
  174. public bool _alphaBlend;
  175. [SerializeField]
  176. private bool _useDepth;
  177. public int _depth;
  178. public bool _fullScreen = true;
  179. [Range(0f, 1f)]
  180. public float _x;
  181. [Range(0f, 1f)]
  182. public float _y;
  183. [Range(0f, 1f)]
  184. public float _width = 1f;
  185. [Range(0f, 1f)]
  186. public float _height = 1f;
  187. private static int _propAlphaPack;
  188. private static int _propVertScale;
  189. private static int _propApplyGamma;
  190. private static int _propChromaTex;
  191. private static Shader _shaderAlphaPacking;
  192. private Material _material;
  193. }
  194. }