ApplyToMesh.cs 6.5 KB

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