DisplayUGUI.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. #if UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_5 || UNITY_5_4_OR_NEWER
  2. #define UNITY_FEATURE_UGUI
  3. #endif
  4. #if UNITY_ANDROID && !UNITY_EDITOR
  5. #define REAL_ANDROID
  6. #endif
  7. #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN
  8. #define UNITY_PLATFORM_SUPPORTS_LINEAR
  9. #elif UNITY_IOS || UNITY_ANDROID
  10. #if UNITY_5_5_OR_NEWER || (UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3 && !UNITY_5_4)
  11. #define UNITY_PLATFORM_SUPPORTS_LINEAR
  12. #endif
  13. #endif
  14. #if (!UNITY_STANDALONE_WIN && !UNITY_EDITOR_WIN) && (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE || UNITY_IOS || UNITY_TVOS || UNITY_ANDROID)
  15. #define UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  16. #endif
  17. // Some older versions of Unity don't set the _TexelSize variable from uGUI so we need to set this manually
  18. #if ((!UNITY_5_4_OR_NEWER && !UNITY_5) || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3_0 || UNITY_5_3_1 || UNITY_5_3_2 || UNITY_5_3_3)
  19. #define UNITY_UGUI_NOSET_TEXELSIZE
  20. #endif
  21. #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0)
  22. #define UNITY_HELPATTRIB
  23. #endif
  24. using System.Collections.Generic;
  25. #if UNITY_FEATURE_UGUI
  26. using UnityEngine;
  27. using UnityEngine.UI;
  28. //-----------------------------------------------------------------------------
  29. // Copyright 2015-2020 RenderHeads Ltd. All rights reserved.
  30. //-----------------------------------------------------------------------------
  31. namespace RenderHeads.Media.AVProVideo
  32. {
  33. /// <summary>
  34. /// Displays the video from MediaPlayer component using uGUI
  35. /// </summary>
  36. [ExecuteInEditMode]
  37. #if UNITY_HELPATTRIB
  38. [HelpURL("http://renderheads.com/products/avpro-video/")]
  39. #endif
  40. [AddComponentMenu("AVPro Video/Display uGUI", 200)]
  41. public class DisplayUGUI : UnityEngine.UI.MaskableGraphic
  42. {
  43. [SerializeField]
  44. public MediaPlayer _mediaPlayer;
  45. [SerializeField]
  46. public Rect m_UVRect = new Rect(0f, 0f, 1f, 1f);
  47. [SerializeField]
  48. public bool _setNativeSize = false;
  49. [SerializeField]
  50. public ScaleMode _scaleMode = ScaleMode.ScaleToFit;
  51. [SerializeField]
  52. public bool _noDefaultDisplay = true;
  53. [SerializeField]
  54. public bool _displayInEditor = true;
  55. [SerializeField]
  56. public Texture _defaultTexture;
  57. private int _lastWidth;
  58. private int _lastHeight;
  59. private bool _flipY;
  60. private Texture _lastTexture;
  61. private static Shader _shaderStereoPacking;
  62. private static Shader _shaderAlphaPacking;
  63. #if REAL_ANDROID
  64. private static Shader _shaderAndroidOES;
  65. #endif
  66. private static int _propAlphaPack;
  67. private static int _propVertScale;
  68. private static int _propStereo;
  69. private static int _propApplyGamma;
  70. private static int _propUseYpCbCr;
  71. private const string PropChromaTexName = "_ChromaTex";
  72. private static int _propChromaTex;
  73. private const string PropYpCbCrTransformName = "_YpCbCrTransform";
  74. private static int _propYpCbCrTransform;
  75. private static int _propCroppingScalars;
  76. #if UNITY_UGUI_NOSET_TEXELSIZE
  77. private static int _propMainTextureTexelSize;
  78. #endif
  79. private bool _userMaterial = true;
  80. private Material _material;
  81. #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2_0 && !UNITY_5_2_1)
  82. private List<UIVertex> _vertices = new List<UIVertex>(4);
  83. private static List<int> QuadIndices = new List<int>(new int[] { 0, 1, 2, 2, 3, 0 });
  84. #endif
  85. protected override void Awake()
  86. {
  87. if (_propAlphaPack == 0)
  88. {
  89. _propStereo = Shader.PropertyToID("Stereo");
  90. _propAlphaPack = Shader.PropertyToID("AlphaPack");
  91. _propVertScale = Shader.PropertyToID("_VertScale");
  92. _propApplyGamma = Shader.PropertyToID("_ApplyGamma");
  93. _propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
  94. _propChromaTex = Shader.PropertyToID(PropChromaTexName);
  95. _propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
  96. _propYpCbCrTransform = Shader.PropertyToID(PropYpCbCrTransformName);
  97. _propCroppingScalars = Shader.PropertyToID("_CroppingScalars");
  98. #if UNITY_UGUI_NOSET_TEXELSIZE
  99. _propMainTextureTexelSize = Shader.PropertyToID("_MainTex_TexelSize");
  100. #endif
  101. }
  102. #if UNITY_IOS
  103. bool hasMask = HasMask(gameObject);
  104. if (hasMask)
  105. {
  106. Debug.LogWarning("[AVProVideo] Using DisplayUGUI with a Mask necessitates disabling YpCbCr mode on the MediaPlayer. Memory consumption will increase.");
  107. _mediaPlayer.PlatformOptionsIOS.useYpCbCr420Textures = false;
  108. }
  109. #endif
  110. base.Awake();
  111. }
  112. private static bool HasMask(GameObject obj)
  113. {
  114. if (obj.GetComponent<Mask>() != null)
  115. return true;
  116. if (obj.transform.parent != null)
  117. return HasMask(obj.transform.parent.gameObject);
  118. return false;
  119. }
  120. private static Shader EnsureShader(Shader shader, string name)
  121. {
  122. if (shader == null)
  123. {
  124. shader = Shader.Find(name);
  125. if (shader == null)
  126. {
  127. Debug.LogWarning("[AVProVideo] Missing shader " + name);
  128. }
  129. }
  130. return shader;
  131. }
  132. private static Shader EnsureAlphaPackingShader()
  133. {
  134. _shaderAlphaPacking = EnsureShader(_shaderAlphaPacking, "AVProVideo/UI/Transparent Packed");
  135. return _shaderAlphaPacking;
  136. }
  137. private static Shader EnsureStereoPackingShader()
  138. {
  139. _shaderStereoPacking = EnsureShader(_shaderStereoPacking, "AVProVideo/UI/Stereo");
  140. return _shaderStereoPacking;
  141. }
  142. #if REAL_ANDROID
  143. private Shader EnsureAndroidOESShader()
  144. {
  145. _shaderAndroidOES = EnsureShader(_shaderAndroidOES, "AVProVideo/UI/AndroidOES");
  146. return _shaderAndroidOES;
  147. }
  148. #endif
  149. protected override void Start()
  150. {
  151. _userMaterial = (this.m_Material != null);
  152. if (_userMaterial)
  153. {
  154. _material = new Material(this.material);
  155. this.material = _material;
  156. }
  157. base.Start();
  158. }
  159. protected override void OnDestroy()
  160. {
  161. // Destroy existing material
  162. if (_material != null)
  163. {
  164. this.material = null;
  165. #if UNITY_EDITOR
  166. Material.DestroyImmediate(_material);
  167. #else
  168. Material.Destroy(_material);
  169. #endif
  170. _material = null;
  171. }
  172. base.OnDestroy();
  173. }
  174. private Shader GetRequiredShader()
  175. {
  176. Shader result = null;
  177. switch (_mediaPlayer.m_StereoPacking)
  178. {
  179. case StereoPacking.None:
  180. break;
  181. case StereoPacking.LeftRight:
  182. case StereoPacking.TopBottom:
  183. result = EnsureStereoPackingShader();
  184. break;
  185. }
  186. switch (_mediaPlayer.m_AlphaPacking)
  187. {
  188. case AlphaPacking.None:
  189. break;
  190. case AlphaPacking.LeftRight:
  191. case AlphaPacking.TopBottom:
  192. result = EnsureAlphaPackingShader();
  193. break;
  194. }
  195. #if UNITY_PLATFORM_SUPPORTS_LINEAR
  196. if (result == null && _mediaPlayer.Info != null)
  197. {
  198. if (QualitySettings.activeColorSpace == ColorSpace.Linear && !_mediaPlayer.Info.PlayerSupportsLinearColorSpace())
  199. {
  200. result = EnsureAlphaPackingShader();
  201. }
  202. }
  203. #endif
  204. if (result == null && _mediaPlayer.TextureProducer != null && _mediaPlayer.TextureProducer.GetTextureCount() == 2)
  205. {
  206. result = EnsureAlphaPackingShader();
  207. }
  208. #if REAL_ANDROID
  209. if (_mediaPlayer.PlatformOptionsAndroid.useFastOesPath)
  210. {
  211. result = EnsureAndroidOESShader();
  212. }
  213. #endif
  214. return result;
  215. }
  216. /// <summary>
  217. /// Returns the texture used to draw this Graphic.
  218. /// </summary>
  219. public override Texture mainTexture
  220. {
  221. get
  222. {
  223. Texture result = Texture2D.whiteTexture;
  224. if (HasValidTexture())
  225. {
  226. Texture resamplerTex = _mediaPlayer.FrameResampler == null || _mediaPlayer.FrameResampler.OutputTexture == null ? null : _mediaPlayer.FrameResampler.OutputTexture[0];
  227. result = _mediaPlayer.m_Resample ? resamplerTex : _mediaPlayer.TextureProducer.GetTexture();
  228. }
  229. else
  230. {
  231. if (_noDefaultDisplay)
  232. {
  233. result = null;
  234. }
  235. else if (_defaultTexture != null)
  236. {
  237. result = _defaultTexture;
  238. }
  239. #if UNITY_EDITOR
  240. if (result == null && _displayInEditor)
  241. {
  242. result = Resources.Load<Texture2D>("AVProVideoIcon");
  243. }
  244. #endif
  245. }
  246. return result;
  247. }
  248. }
  249. public bool HasValidTexture()
  250. {
  251. return (_mediaPlayer != null && _mediaPlayer.TextureProducer != null && _mediaPlayer.TextureProducer.GetTexture() != null);
  252. }
  253. private void UpdateInternalMaterial()
  254. {
  255. if (_mediaPlayer != null)
  256. {
  257. // Get required shader
  258. Shader currentShader = null;
  259. if (_material != null)
  260. {
  261. currentShader = _material.shader;
  262. }
  263. Shader nextShader = GetRequiredShader();
  264. // If the shader requirement has changed
  265. if (currentShader != nextShader)
  266. {
  267. // Destroy existing material
  268. if (_material != null)
  269. {
  270. this.material = null;
  271. #if UNITY_EDITOR
  272. Material.DestroyImmediate(_material);
  273. #else
  274. Material.Destroy(_material);
  275. #endif
  276. _material = null;
  277. }
  278. // Create new material
  279. if (nextShader != null)
  280. {
  281. _material = new Material(nextShader);
  282. }
  283. }
  284. this.material = _material;
  285. }
  286. }
  287. // We do a LateUpdate() to allow for any changes in the texture that may have happened in Update()
  288. void LateUpdate()
  289. {
  290. if (_setNativeSize)
  291. {
  292. SetNativeSize();
  293. }
  294. if (_lastTexture != mainTexture)
  295. {
  296. _lastTexture = mainTexture;
  297. SetVerticesDirty();
  298. SetMaterialDirty();
  299. }
  300. if (HasValidTexture())
  301. {
  302. if (mainTexture != null)
  303. {
  304. if (mainTexture.width != _lastWidth || mainTexture.height != _lastHeight)
  305. {
  306. _lastWidth = mainTexture.width;
  307. _lastHeight = mainTexture.height;
  308. SetVerticesDirty();
  309. SetMaterialDirty();
  310. }
  311. }
  312. }
  313. if (!_userMaterial && Application.isPlaying)
  314. {
  315. UpdateInternalMaterial();
  316. }
  317. if (material != null && _mediaPlayer != null)
  318. {
  319. // YpCbCr support
  320. if (material.HasProperty(_propUseYpCbCr) && _mediaPlayer.TextureProducer != null && _mediaPlayer.TextureProducer.GetTextureCount() == 2)
  321. {
  322. material.EnableKeyword("USE_YPCBCR");
  323. material.SetMatrix(_propYpCbCrTransform, _mediaPlayer.TextureProducer.GetYpCbCrTransform());
  324. Texture resamplerTex = _mediaPlayer.FrameResampler == null || _mediaPlayer.FrameResampler.OutputTexture == null ? null : _mediaPlayer.FrameResampler.OutputTexture[1];
  325. material.SetTexture(_propChromaTex, _mediaPlayer.m_Resample ? resamplerTex : _mediaPlayer.TextureProducer.GetTexture(1));
  326. }
  327. // Apply changes for alpha videos
  328. if (material.HasProperty(_propAlphaPack))
  329. {
  330. Helper.SetupAlphaPackedMaterial(material, _mediaPlayer.m_AlphaPacking);
  331. if (_flipY && _mediaPlayer.m_AlphaPacking != AlphaPacking.None)
  332. {
  333. material.SetFloat(_propVertScale, -1f);
  334. }
  335. else
  336. {
  337. material.SetFloat(_propVertScale, 1f);
  338. }
  339. #if UNITY_UGUI_NOSET_TEXELSIZE
  340. if (mainTexture != null)
  341. {
  342. material.SetVector(_propMainTextureTexelSize, new Vector4(1.0f / mainTexture.width, 1.0f / mainTexture.height, mainTexture.width, mainTexture.height));
  343. }
  344. #endif
  345. }
  346. // Apply changes for stereo videos
  347. if (material.HasProperty(_propStereo))
  348. {
  349. Helper.SetupStereoMaterial(material, _mediaPlayer.m_StereoPacking, _mediaPlayer.m_DisplayDebugStereoColorTint);
  350. }
  351. #if UNITY_PLATFORM_SUPPORTS_LINEAR
  352. if (material.HasProperty(_propApplyGamma) && _mediaPlayer.Info != null)
  353. {
  354. Helper.SetupGammaMaterial(material, _mediaPlayer.Info.PlayerSupportsLinearColorSpace());
  355. }
  356. #else
  357. _propApplyGamma |= 0; // Prevent compiler warning about unused variable
  358. #endif
  359. #if (!UNITY_EDITOR && UNITY_ANDROID)
  360. // Adjust for cropping (when the decoder decodes in blocks that overrun the video frame size, it pads), OES only as we apply this lower down for none-OES
  361. if (_mediaPlayer.PlatformOptionsAndroid.useFastOesPath &&
  362. _mediaPlayer.Info != null &&
  363. material.HasProperty(_propCroppingScalars) )
  364. {
  365. float[] transform = _mediaPlayer.Info.GetTextureTransform();
  366. if (transform != null)
  367. {
  368. material.SetVector(_propCroppingScalars, new Vector4(transform[0], transform[3], 1.0f, 1.0f));
  369. }
  370. }
  371. #else
  372. _propCroppingScalars |= 0; // Prevent compiler warning about unused variable
  373. #endif
  374. }
  375. }
  376. /// <summary>
  377. /// Texture to be used.
  378. /// </summary>
  379. public MediaPlayer CurrentMediaPlayer
  380. {
  381. get
  382. {
  383. return _mediaPlayer;
  384. }
  385. set
  386. {
  387. if (_mediaPlayer != value)
  388. {
  389. _mediaPlayer = value;
  390. //SetVerticesDirty();
  391. SetMaterialDirty();
  392. }
  393. }
  394. }
  395. /// <summary>
  396. /// UV rectangle used by the texture.
  397. /// </summary>
  398. public Rect uvRect
  399. {
  400. get
  401. {
  402. return m_UVRect;
  403. }
  404. set
  405. {
  406. if (m_UVRect == value)
  407. {
  408. return;
  409. }
  410. m_UVRect = value;
  411. SetVerticesDirty();
  412. }
  413. }
  414. /// <summary>
  415. /// Adjust the scale of the Graphic to make it pixel-perfect.
  416. /// </summary>
  417. [ContextMenu("Set Native Size")]
  418. public override void SetNativeSize()
  419. {
  420. Texture tex = mainTexture;
  421. if (tex != null)
  422. {
  423. int w = Mathf.RoundToInt(tex.width * uvRect.width);
  424. int h = Mathf.RoundToInt(tex.height * uvRect.height);
  425. if (_mediaPlayer != null)
  426. {
  427. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM && !REAL_ANDROID
  428. if (_mediaPlayer.Info != null)
  429. {
  430. Orientation ori = Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform());
  431. if (ori == Orientation.Portrait || ori == Orientation.PortraitFlipped)
  432. {
  433. w = Mathf.RoundToInt(tex.height * uvRect.width);
  434. h = Mathf.RoundToInt(tex.width * uvRect.height);
  435. }
  436. }
  437. #endif
  438. if (_mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || _mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
  439. {
  440. w /= 2;
  441. }
  442. else if (_mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || _mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
  443. {
  444. h /= 2;
  445. }
  446. }
  447. rectTransform.anchorMax = rectTransform.anchorMin;
  448. rectTransform.sizeDelta = new Vector2(w, h);
  449. }
  450. }
  451. /// <summary>
  452. /// Update all renderer data.
  453. /// </summary>
  454. // OnFillVBO deprecated by 5.2
  455. // OnPopulateMesh(Mesh mesh) deprecated by 5.2 patch 1
  456. #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2_0)
  457. /* protected override void OnPopulateMesh(Mesh mesh)
  458. {
  459. List<UIVertex> verts = new List<UIVertex>();
  460. _OnFillVBO( verts );
  461. var quad = new UIVertex[4];
  462. for (int i = 0; i < vbo.Count; i += 4)
  463. {
  464. vbo.CopyTo(i, quad, 0, 4);
  465. vh.AddUIVertexQuad(quad);
  466. }
  467. vh.FillMesh( toFill );
  468. }*/
  469. #if !UNITY_5_2_1
  470. protected override void OnPopulateMesh(VertexHelper vh)
  471. {
  472. vh.Clear();
  473. _OnFillVBO(_vertices);
  474. vh.AddUIVertexStream(_vertices, QuadIndices);
  475. }
  476. #endif
  477. #endif
  478. #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0 && !UNITY_5_1)
  479. [System.Obsolete("This method is not called from Unity 5.2 and above")]
  480. #endif
  481. protected override void OnFillVBO(List<UIVertex> vbo)
  482. {
  483. _OnFillVBO(vbo);
  484. }
  485. private void _OnFillVBO(List<UIVertex> vbo)
  486. {
  487. _flipY = false;
  488. if (HasValidTexture())
  489. {
  490. _flipY = _mediaPlayer.TextureProducer.RequiresVerticalFlip();
  491. }
  492. Rect uvRect = m_UVRect;
  493. Vector4 v = GetDrawingDimensions(_scaleMode, ref uvRect);
  494. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  495. Matrix4x4 m = Matrix4x4.identity;
  496. if (HasValidTexture())
  497. {
  498. m = Helper.GetMatrixForOrientation(Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform()));
  499. }
  500. #endif
  501. vbo.Clear();
  502. var vert = UIVertex.simpleVert;
  503. vert.color = color;
  504. vert.position = new Vector2(v.x, v.y);
  505. vert.uv0 = new Vector2(uvRect.xMin, uvRect.yMin);
  506. if (_flipY)
  507. {
  508. vert.uv0 = new Vector2(uvRect.xMin, 1.0f - uvRect.yMin);
  509. }
  510. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  511. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  512. #endif
  513. vbo.Add(vert);
  514. vert.position = new Vector2(v.x, v.w);
  515. vert.uv0 = new Vector2(uvRect.xMin, uvRect.yMax);
  516. if (_flipY)
  517. {
  518. vert.uv0 = new Vector2(uvRect.xMin, 1.0f - uvRect.yMax);
  519. }
  520. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  521. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  522. #endif
  523. vbo.Add(vert);
  524. vert.position = new Vector2(v.z, v.w);
  525. vert.uv0 = new Vector2(uvRect.xMax, uvRect.yMax);
  526. if (_flipY)
  527. {
  528. vert.uv0 = new Vector2(uvRect.xMax, 1.0f - uvRect.yMax);
  529. }
  530. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  531. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  532. #endif
  533. vbo.Add(vert);
  534. vert.position = new Vector2(v.z, v.y);
  535. vert.uv0 = new Vector2(uvRect.xMax, uvRect.yMin);
  536. if (_flipY)
  537. {
  538. vert.uv0 = new Vector2(uvRect.xMax, 1.0f - uvRect.yMin);
  539. }
  540. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
  541. vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
  542. #endif
  543. vbo.Add(vert);
  544. }
  545. private Vector4 GetDrawingDimensions(ScaleMode scaleMode, ref Rect uvRect)
  546. {
  547. Vector4 returnSize = Vector4.zero;
  548. if (mainTexture != null)
  549. {
  550. var padding = Vector4.zero;
  551. var textureSize = new Vector2(mainTexture.width, mainTexture.height);
  552. {
  553. // Adjust textureSize based on orientation
  554. #if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM && !REAL_ANDROID
  555. if (HasValidTexture())
  556. {
  557. Matrix4x4 m = Helper.GetMatrixForOrientation(Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform()));
  558. textureSize = m.MultiplyVector(textureSize);
  559. textureSize.x = Mathf.Abs(textureSize.x);
  560. textureSize.y = Mathf.Abs(textureSize.y);
  561. }
  562. #endif
  563. // Adjust textureSize based on alpha packing
  564. if (_mediaPlayer != null)
  565. {
  566. if (_mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || _mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
  567. {
  568. textureSize.x /= 2f;
  569. }
  570. else if (_mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || _mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
  571. {
  572. textureSize.y /= 2f;
  573. }
  574. }
  575. }
  576. Rect r = GetPixelAdjustedRect();
  577. // Fit the above textureSize into rectangle r
  578. int spriteW = Mathf.RoundToInt(textureSize.x);
  579. int spriteH = Mathf.RoundToInt(textureSize.y);
  580. var size = new Vector4(padding.x / spriteW,
  581. padding.y / spriteH,
  582. (spriteW - padding.z) / spriteW,
  583. (spriteH - padding.w) / spriteH);
  584. {
  585. if (textureSize.sqrMagnitude > 0.0f)
  586. {
  587. if (scaleMode == ScaleMode.ScaleToFit)
  588. {
  589. float spriteRatio = textureSize.x / textureSize.y;
  590. float rectRatio = r.width / r.height;
  591. if (spriteRatio > rectRatio)
  592. {
  593. float oldHeight = r.height;
  594. r.height = r.width * (1.0f / spriteRatio);
  595. r.y += (oldHeight - r.height) * rectTransform.pivot.y;
  596. }
  597. else
  598. {
  599. float oldWidth = r.width;
  600. r.width = r.height * spriteRatio;
  601. r.x += (oldWidth - r.width) * rectTransform.pivot.x;
  602. }
  603. }
  604. else if (scaleMode == ScaleMode.ScaleAndCrop)
  605. {
  606. float aspectRatio = textureSize.x / textureSize.y;
  607. float screenRatio = r.width / r.height;
  608. if (screenRatio > aspectRatio)
  609. {
  610. float adjust = aspectRatio / screenRatio;
  611. uvRect = new Rect(uvRect.xMin, (uvRect.yMin * adjust) + (1f - adjust) * 0.5f, uvRect.width, adjust * uvRect.height);
  612. }
  613. else
  614. {
  615. float adjust = screenRatio / aspectRatio;
  616. uvRect = new Rect(uvRect.xMin * adjust + (0.5f - adjust * 0.5f), uvRect.yMin, adjust * uvRect.width, uvRect.height);
  617. }
  618. }
  619. }
  620. }
  621. returnSize = new Vector4(r.x + r.width * size.x,
  622. r.y + r.height * size.y,
  623. r.x + r.width * size.z,
  624. r.y + r.height * size.w);
  625. }
  626. return returnSize;
  627. }
  628. }
  629. }
  630. #endif