DisplayUGUI.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace RenderHeads.Media.AVProVideo
  6. {
  7. [ExecuteInEditMode]
  8. [HelpURL("http://renderheads.com/product/avpro-video/")]
  9. [AddComponentMenu("AVPro Video/Display uGUI", 200)]
  10. public class DisplayUGUI : MaskableGraphic
  11. {
  12. protected override void Awake()
  13. {
  14. if (DisplayUGUI._propAlphaPack == 0)
  15. {
  16. DisplayUGUI._propStereo = Shader.PropertyToID("Stereo");
  17. DisplayUGUI._propAlphaPack = Shader.PropertyToID("AlphaPack");
  18. DisplayUGUI._propVertScale = Shader.PropertyToID("_VertScale");
  19. DisplayUGUI._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
  20. DisplayUGUI._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
  21. DisplayUGUI._propChromaTex = Shader.PropertyToID("_ChromaTex");
  22. }
  23. if (DisplayUGUI._shaderAlphaPacking == null)
  24. {
  25. DisplayUGUI._shaderAlphaPacking = Shader.Find("AVProVideo/UI/Transparent Packed");
  26. if (DisplayUGUI._shaderAlphaPacking == null)
  27. {
  28. Debug.LogWarning("[AVProVideo] Missing shader AVProVideo/UI/Transparent Packed");
  29. }
  30. }
  31. if (DisplayUGUI._shaderStereoPacking == null)
  32. {
  33. DisplayUGUI._shaderStereoPacking = Shader.Find("AVProVideo/UI/Stereo");
  34. if (DisplayUGUI._shaderStereoPacking == null)
  35. {
  36. Debug.LogWarning("[AVProVideo] Missing shader AVProVideo/UI/Stereo");
  37. }
  38. }
  39. base.Awake();
  40. }
  41. protected override void Start()
  42. {
  43. this._userMaterial = (this.m_Material != null);
  44. base.Start();
  45. }
  46. protected override void OnDestroy()
  47. {
  48. if (this._material != null)
  49. {
  50. this.material = null;
  51. UnityEngine.Object.Destroy(this._material);
  52. this._material = null;
  53. }
  54. base.OnDestroy();
  55. }
  56. private Shader GetRequiredShader()
  57. {
  58. Shader shader = null;
  59. StereoPacking stereoPacking = this._mediaPlayer.m_StereoPacking;
  60. if (stereoPacking != StereoPacking.None)
  61. {
  62. if (stereoPacking == StereoPacking.LeftRight || stereoPacking == StereoPacking.TopBottom)
  63. {
  64. shader = DisplayUGUI._shaderStereoPacking;
  65. }
  66. }
  67. AlphaPacking alphaPacking = this._mediaPlayer.m_AlphaPacking;
  68. if (alphaPacking != AlphaPacking.None)
  69. {
  70. if (alphaPacking == AlphaPacking.LeftRight || alphaPacking == AlphaPacking.TopBottom)
  71. {
  72. shader = DisplayUGUI._shaderAlphaPacking;
  73. }
  74. }
  75. if (shader == null && this._mediaPlayer.Info != null && QualitySettings.activeColorSpace == ColorSpace.Linear && !this._mediaPlayer.Info.PlayerSupportsLinearColorSpace())
  76. {
  77. shader = DisplayUGUI._shaderAlphaPacking;
  78. }
  79. if (shader == null && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
  80. {
  81. shader = DisplayUGUI._shaderAlphaPacking;
  82. }
  83. return shader;
  84. }
  85. public override Texture mainTexture
  86. {
  87. get
  88. {
  89. Texture result = Texture2D.whiteTexture;
  90. if (this.HasValidTexture())
  91. {
  92. Texture texture = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[0] : null;
  93. result = ((!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(0) : texture);
  94. }
  95. else if (this._noDefaultDisplay)
  96. {
  97. result = null;
  98. }
  99. else if (this._defaultTexture != null)
  100. {
  101. result = this._defaultTexture;
  102. }
  103. return result;
  104. }
  105. }
  106. public bool HasValidTexture()
  107. {
  108. return this._mediaPlayer != null && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTexture(0) != null;
  109. }
  110. private void UpdateInternalMaterial()
  111. {
  112. if (this._mediaPlayer != null)
  113. {
  114. Shader x = null;
  115. if (this._material != null)
  116. {
  117. x = this._material.shader;
  118. }
  119. Shader requiredShader = this.GetRequiredShader();
  120. if (x != requiredShader)
  121. {
  122. if (this._material != null)
  123. {
  124. this.material = null;
  125. UnityEngine.Object.Destroy(this._material);
  126. this._material = null;
  127. }
  128. if (requiredShader != null)
  129. {
  130. this._material = new Material(requiredShader);
  131. }
  132. }
  133. this.material = this._material;
  134. }
  135. }
  136. private void LateUpdate()
  137. {
  138. if (this._setNativeSize)
  139. {
  140. this.SetNativeSize();
  141. }
  142. if (this._lastTexture != this.mainTexture)
  143. {
  144. this._lastTexture = this.mainTexture;
  145. this.SetVerticesDirty();
  146. }
  147. if (this.HasValidTexture() && this.mainTexture != null && (this.mainTexture.width != this._lastWidth || this.mainTexture.height != this._lastHeight))
  148. {
  149. this._lastWidth = this.mainTexture.width;
  150. this._lastHeight = this.mainTexture.height;
  151. this.SetVerticesDirty();
  152. }
  153. if (!this._userMaterial && Application.isPlaying)
  154. {
  155. this.UpdateInternalMaterial();
  156. }
  157. if (this.material != null && this._mediaPlayer != null)
  158. {
  159. if (this.material.HasProperty(DisplayUGUI._propUseYpCbCr) && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
  160. {
  161. this.material.EnableKeyword("USE_YPCBCR");
  162. Texture texture = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[1] : null;
  163. this.material.SetTexture(DisplayUGUI._propChromaTex, (!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(1) : texture);
  164. }
  165. if (this.material.HasProperty(DisplayUGUI._propAlphaPack))
  166. {
  167. Helper.SetupAlphaPackedMaterial(this.material, this._mediaPlayer.m_AlphaPacking);
  168. if (this._flipY && this._mediaPlayer.m_AlphaPacking != AlphaPacking.None)
  169. {
  170. this.material.SetFloat(DisplayUGUI._propVertScale, -1f);
  171. }
  172. else
  173. {
  174. this.material.SetFloat(DisplayUGUI._propVertScale, 1f);
  175. }
  176. }
  177. if (this.material.HasProperty(DisplayUGUI._propStereo))
  178. {
  179. Helper.SetupStereoMaterial(this.material, this._mediaPlayer.m_StereoPacking, this._mediaPlayer.m_DisplayDebugStereoColorTint);
  180. }
  181. if (this.material.HasProperty(DisplayUGUI._propApplyGamma) && this._mediaPlayer.Info != null)
  182. {
  183. Helper.SetupGammaMaterial(this.material, this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
  184. }
  185. }
  186. this.SetMaterialDirty();
  187. }
  188. public MediaPlayer CurrentMediaPlayer
  189. {
  190. get
  191. {
  192. return this._mediaPlayer;
  193. }
  194. set
  195. {
  196. if (this._mediaPlayer != value)
  197. {
  198. this._mediaPlayer = value;
  199. this.SetMaterialDirty();
  200. }
  201. }
  202. }
  203. public Rect uvRect
  204. {
  205. get
  206. {
  207. return this.m_UVRect;
  208. }
  209. set
  210. {
  211. if (this.m_UVRect == value)
  212. {
  213. return;
  214. }
  215. this.m_UVRect = value;
  216. this.SetVerticesDirty();
  217. }
  218. }
  219. [ContextMenu("Set Native Size")]
  220. public override void SetNativeSize()
  221. {
  222. Texture mainTexture = this.mainTexture;
  223. if (mainTexture != null)
  224. {
  225. int num = Mathf.RoundToInt((float)mainTexture.width * this.uvRect.width);
  226. int num2 = Mathf.RoundToInt((float)mainTexture.height * this.uvRect.height);
  227. if (this._mediaPlayer != null)
  228. {
  229. if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || this._mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
  230. {
  231. num /= 2;
  232. }
  233. else if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || this._mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
  234. {
  235. num2 /= 2;
  236. }
  237. }
  238. base.rectTransform.anchorMax = base.rectTransform.anchorMin;
  239. base.rectTransform.sizeDelta = new Vector2((float)num, (float)num2);
  240. }
  241. }
  242. protected override void OnPopulateMesh(VertexHelper vh)
  243. {
  244. vh.Clear();
  245. this._OnFillVBO(this._vertices);
  246. vh.AddUIVertexStream(this._vertices, DisplayUGUI.QuadIndices);
  247. }
  248. [Obsolete("This method is not called from Unity 5.2 and above")]
  249. protected override void OnFillVBO(List<UIVertex> vbo)
  250. {
  251. this._OnFillVBO(vbo);
  252. }
  253. private void _OnFillVBO(List<UIVertex> vbo)
  254. {
  255. this._flipY = false;
  256. if (this.HasValidTexture())
  257. {
  258. this._flipY = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
  259. }
  260. Rect uvrect = this.m_UVRect;
  261. Vector4 drawingDimensions = this.GetDrawingDimensions(this._scaleMode, ref uvrect);
  262. vbo.Clear();
  263. UIVertex simpleVert = UIVertex.simpleVert;
  264. simpleVert.color = this.color;
  265. simpleVert.position = new Vector2(drawingDimensions.x, drawingDimensions.y);
  266. simpleVert.uv0 = new Vector2(uvrect.xMin, uvrect.yMin);
  267. if (this._flipY)
  268. {
  269. simpleVert.uv0 = new Vector2(uvrect.xMin, 1f - uvrect.yMin);
  270. }
  271. vbo.Add(simpleVert);
  272. simpleVert.position = new Vector2(drawingDimensions.x, drawingDimensions.w);
  273. simpleVert.uv0 = new Vector2(uvrect.xMin, uvrect.yMax);
  274. if (this._flipY)
  275. {
  276. simpleVert.uv0 = new Vector2(uvrect.xMin, 1f - uvrect.yMax);
  277. }
  278. vbo.Add(simpleVert);
  279. simpleVert.position = new Vector2(drawingDimensions.z, drawingDimensions.w);
  280. simpleVert.uv0 = new Vector2(uvrect.xMax, uvrect.yMax);
  281. if (this._flipY)
  282. {
  283. simpleVert.uv0 = new Vector2(uvrect.xMax, 1f - uvrect.yMax);
  284. }
  285. vbo.Add(simpleVert);
  286. simpleVert.position = new Vector2(drawingDimensions.z, drawingDimensions.y);
  287. simpleVert.uv0 = new Vector2(uvrect.xMax, uvrect.yMin);
  288. if (this._flipY)
  289. {
  290. simpleVert.uv0 = new Vector2(uvrect.xMax, 1f - uvrect.yMin);
  291. }
  292. vbo.Add(simpleVert);
  293. }
  294. private Vector4 GetDrawingDimensions(ScaleMode scaleMode, ref Rect uvRect)
  295. {
  296. Vector4 zero = Vector4.zero;
  297. if (this.mainTexture != null)
  298. {
  299. Vector4 zero2 = Vector4.zero;
  300. Vector2 vector = new Vector2((float)this.mainTexture.width, (float)this.mainTexture.height);
  301. if (this._mediaPlayer != null)
  302. {
  303. if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || this._mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
  304. {
  305. vector.x /= 2f;
  306. }
  307. else if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || this._mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
  308. {
  309. vector.y /= 2f;
  310. }
  311. }
  312. Rect pixelAdjustedRect = base.GetPixelAdjustedRect();
  313. int num = Mathf.RoundToInt(vector.x);
  314. int num2 = Mathf.RoundToInt(vector.y);
  315. Vector4 vector2 = new Vector4(zero2.x / (float)num, zero2.y / (float)num2, ((float)num - zero2.z) / (float)num, ((float)num2 - zero2.w) / (float)num2);
  316. if (vector.sqrMagnitude > 0f)
  317. {
  318. if (scaleMode == ScaleMode.ScaleToFit)
  319. {
  320. float num3 = vector.x / vector.y;
  321. float num4 = pixelAdjustedRect.width / pixelAdjustedRect.height;
  322. if (num3 > num4)
  323. {
  324. float height = pixelAdjustedRect.height;
  325. pixelAdjustedRect.height = pixelAdjustedRect.width * (1f / num3);
  326. pixelAdjustedRect.y += (height - pixelAdjustedRect.height) * base.rectTransform.pivot.y;
  327. }
  328. else
  329. {
  330. float width = pixelAdjustedRect.width;
  331. pixelAdjustedRect.width = pixelAdjustedRect.height * num3;
  332. pixelAdjustedRect.x += (width - pixelAdjustedRect.width) * base.rectTransform.pivot.x;
  333. }
  334. }
  335. else if (scaleMode == ScaleMode.ScaleAndCrop)
  336. {
  337. float num5 = vector.x / vector.y;
  338. float num6 = pixelAdjustedRect.width / pixelAdjustedRect.height;
  339. if (num6 > num5)
  340. {
  341. float num7 = num5 / num6;
  342. uvRect = new Rect(0f, (1f - num7) * 0.5f, 1f, num7);
  343. }
  344. else
  345. {
  346. float num8 = num6 / num5;
  347. uvRect = new Rect(0.5f - num8 * 0.5f, 0f, num8, 1f);
  348. }
  349. }
  350. }
  351. zero = new Vector4(pixelAdjustedRect.x + pixelAdjustedRect.width * vector2.x, pixelAdjustedRect.y + pixelAdjustedRect.height * vector2.y, pixelAdjustedRect.x + pixelAdjustedRect.width * vector2.z, pixelAdjustedRect.y + pixelAdjustedRect.height * vector2.w);
  352. }
  353. return zero;
  354. }
  355. [SerializeField]
  356. public MediaPlayer _mediaPlayer;
  357. [SerializeField]
  358. public Rect m_UVRect = new Rect(0f, 0f, 1f, 1f);
  359. [SerializeField]
  360. public bool _setNativeSize;
  361. [SerializeField]
  362. public ScaleMode _scaleMode = ScaleMode.ScaleToFit;
  363. [SerializeField]
  364. public bool _noDefaultDisplay = true;
  365. [SerializeField]
  366. public bool _displayInEditor = true;
  367. [SerializeField]
  368. public Texture _defaultTexture;
  369. private int _lastWidth;
  370. private int _lastHeight;
  371. private bool _flipY;
  372. private Texture _lastTexture;
  373. private static Shader _shaderStereoPacking;
  374. private static Shader _shaderAlphaPacking;
  375. private static int _propAlphaPack;
  376. private static int _propVertScale;
  377. private static int _propStereo;
  378. private static int _propApplyGamma;
  379. private static int _propUseYpCbCr;
  380. private const string PropChromaTexName = "_ChromaTex";
  381. private static int _propChromaTex;
  382. private bool _userMaterial = true;
  383. private Material _material;
  384. private List<UIVertex> _vertices = new List<UIVertex>(4);
  385. private static List<int> QuadIndices = new List<int>(new int[]
  386. {
  387. 0,
  388. 1,
  389. 2,
  390. 2,
  391. 3,
  392. 0
  393. });
  394. }
  395. }