ApplyToMaterial.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS
  2. #define UNITY_PLATFORM_SUPPORTS_YPCBCR
  3. #endif
  4. using UnityEngine;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProVideo
  9. {
  10. /// <summary>
  11. /// Sets up a material to display the video from a MediaPlayer
  12. /// </summary>
  13. [AddComponentMenu("AVPro Video/Apply To Material", 300)]
  14. [HelpURL("https://www.renderheads.com/products/avpro-video/")]
  15. public sealed class ApplyToMaterial : ApplyToBase
  16. {
  17. [Header("Display")]
  18. [Space(8f)]
  19. [Tooltip("Default texture to display when the video texture is preparing")]
  20. [SerializeField] Texture2D _defaultTexture = null;
  21. public Texture2D DefaultTexture
  22. {
  23. get { return _defaultTexture; }
  24. set { if (_defaultTexture != value) { _defaultTexture = value; _isDirty = true; } }
  25. }
  26. [Space(8f)]
  27. [Header("Material Target")]
  28. [SerializeField] Material _material = null;
  29. public Material Material
  30. {
  31. get { return _material; }
  32. set { if (_material != value) { _material = value; _isDirty = true; } }
  33. }
  34. [SerializeField] string _texturePropertyName = Helper.UnityBaseTextureName;
  35. public string TexturePropertyName
  36. {
  37. get { return _texturePropertyName; }
  38. set
  39. {
  40. if (_texturePropertyName != value)
  41. {
  42. _texturePropertyName = value;
  43. // TODO: if the property changes, remove it from the perioud SetTexture()
  44. _propTexture = new LazyShaderProperty(_texturePropertyName);
  45. _isDirty = true;
  46. }
  47. }
  48. }
  49. [SerializeField] Vector2 _offset = Vector2.zero;
  50. public Vector2 Offset
  51. {
  52. get { return _offset; }
  53. set { if (_offset != value) { _offset = value; _isDirty = true; } }
  54. }
  55. [SerializeField] Vector2 _scale = Vector2.one;
  56. public Vector2 Scale
  57. {
  58. get { return _scale; }
  59. set { if (_scale != value) { _scale = value; _isDirty = true; } }
  60. }
  61. private Texture _lastTextureApplied;
  62. private LazyShaderProperty _propTexture;
  63. private Texture _originalTexture;
  64. private Vector2 _originalScale = Vector2.one;
  65. private Vector2 _originalOffset = Vector2.zero;
  66. // We do a LateUpdate() to allow for any changes in the texture that may have happened in Update()
  67. private void LateUpdate()
  68. {
  69. Apply();
  70. }
  71. public override void Apply()
  72. {
  73. bool applied = false;
  74. if (_media != null && _media.TextureProducer != null)
  75. {
  76. Texture resamplerTex = _media.FrameResampler == null || _media.FrameResampler.OutputTexture == null ? null : _media.FrameResampler.OutputTexture[0];
  77. Texture texture = _media.UseResampler ? resamplerTex : _media.TextureProducer.GetTexture(0);
  78. if (texture != null)
  79. {
  80. // Check for changing texture
  81. if (texture != _lastTextureApplied)
  82. {
  83. _isDirty = true;
  84. }
  85. if (_isDirty)
  86. {
  87. int planeCount = _media.UseResampler ? 1 : _media.TextureProducer.GetTextureCount();
  88. for (int plane = 0; plane < planeCount; ++plane)
  89. {
  90. Texture resamplerTexPlane = _media.FrameResampler == null || _media.FrameResampler.OutputTexture == null ? null : _media.FrameResampler.OutputTexture[plane];
  91. texture = _media.UseResampler ? resamplerTexPlane : _media.TextureProducer.GetTexture(plane);
  92. if (texture != null)
  93. {
  94. ApplyMapping(texture, _media.TextureProducer.RequiresVerticalFlip(), plane);
  95. }
  96. }
  97. }
  98. applied = true;
  99. }
  100. }
  101. // If the media didn't apply a texture, then try to apply the default texture
  102. if (!applied)
  103. {
  104. if (_defaultTexture != _lastTextureApplied)
  105. {
  106. _isDirty = true;
  107. }
  108. if (_isDirty)
  109. {
  110. #if UNITY_PLATFORM_SUPPORTS_YPCBCR
  111. if (_material != null && _material.HasProperty(VideoRender.PropUseYpCbCr.Id))
  112. {
  113. _material.DisableKeyword(VideoRender.Keyword_UseYpCbCr);
  114. }
  115. #endif
  116. ApplyMapping(_defaultTexture, false);
  117. }
  118. }
  119. }
  120. private void ApplyMapping(Texture texture, bool requiresYFlip, int plane = 0)
  121. {
  122. if (_material != null)
  123. {
  124. _isDirty = false;
  125. if (plane == 0)
  126. {
  127. VideoRender.SetupMaterialForMedia(_material, _media, _propTexture.Id, texture, texture == _defaultTexture);
  128. _lastTextureApplied = texture;
  129. #if (!UNITY_EDITOR && UNITY_ANDROID)
  130. if (texture == _defaultTexture) { _material.EnableKeyword("USING_DEFAULT_TEXTURE"); }
  131. else { _material.DisableKeyword("USING_DEFAULT_TEXTURE"); }
  132. #endif
  133. if (texture != null)
  134. {
  135. if (requiresYFlip)
  136. {
  137. _material.SetTextureScale(_propTexture.Id, new Vector2(_scale.x, -_scale.y));
  138. _material.SetTextureOffset(_propTexture.Id, Vector2.up + _offset);
  139. }
  140. else
  141. {
  142. _material.SetTextureScale(_propTexture.Id, _scale);
  143. _material.SetTextureOffset(_propTexture.Id, _offset);
  144. }
  145. }
  146. }
  147. else if (plane == 1)
  148. {
  149. if (texture != null)
  150. {
  151. if (requiresYFlip)
  152. {
  153. _material.SetTextureScale(VideoRender.PropChromaTex.Id, new Vector2(_scale.x, -_scale.y));
  154. _material.SetTextureOffset(VideoRender.PropChromaTex.Id, Vector2.up + _offset);
  155. }
  156. else
  157. {
  158. _material.SetTextureScale(VideoRender.PropChromaTex.Id, _scale);
  159. _material.SetTextureOffset(VideoRender.PropChromaTex.Id, _offset);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. protected override void SaveProperties()
  166. {
  167. if (_material != null)
  168. {
  169. if (string.IsNullOrEmpty(_texturePropertyName))
  170. {
  171. _originalTexture = _material.mainTexture;
  172. _originalScale = _material.mainTextureScale;
  173. _originalOffset = _material.mainTextureOffset;
  174. }
  175. else
  176. {
  177. _originalTexture = _material.GetTexture(_texturePropertyName);
  178. _originalScale = _material.GetTextureScale(_texturePropertyName);
  179. _originalOffset = _material.GetTextureOffset(_texturePropertyName);
  180. }
  181. }
  182. _propTexture = new LazyShaderProperty(_texturePropertyName);
  183. }
  184. protected override void RestoreProperties()
  185. {
  186. if (_material != null)
  187. {
  188. if (string.IsNullOrEmpty(_texturePropertyName))
  189. {
  190. _material.mainTexture = _originalTexture;
  191. _material.mainTextureScale = _originalScale;
  192. _material.mainTextureOffset = _originalOffset;
  193. }
  194. else
  195. {
  196. _material.SetTexture(_texturePropertyName, _originalTexture);
  197. _material.SetTextureScale(_texturePropertyName, _originalScale);
  198. _material.SetTextureOffset(_texturePropertyName, _originalOffset);
  199. }
  200. }
  201. }
  202. }
  203. }