ApplyToMesh.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 string TexturePropertyName
  65. {
  66. get { return _texturePropertyName; }
  67. set
  68. {
  69. if (_texturePropertyName != value)
  70. {
  71. _texturePropertyName = value;
  72. // TODO: if the property changes, remove it from the perioud SetTexture()
  73. _propTexture = new LazyShaderProperty(_texturePropertyName);
  74. _isDirty = true;
  75. }
  76. }
  77. }
  78. [SerializeField] Vector2 _offset = Vector2.zero;
  79. public Vector2 Offset
  80. {
  81. get { return _offset; }
  82. set { if (_offset != value) { _offset = value; _isDirty = true; } }
  83. }
  84. [SerializeField] Vector2 _scale = Vector2.one;
  85. public Vector2 Scale
  86. {
  87. get { return _scale; }
  88. set { if (_scale != value) { _scale = value; _isDirty = true; } }
  89. }
  90. private Texture _lastTextureApplied;
  91. private LazyShaderProperty _propTexture;
  92. // We do a LateUpdate() to allow for any changes in the texture that may have happened in Update()
  93. private void LateUpdate()
  94. {
  95. Apply();
  96. }
  97. public override void Apply()
  98. {
  99. bool applied = false;
  100. // Try to apply texture from media
  101. if (_media != null && _media.TextureProducer != null)
  102. {
  103. Texture resamplerTex = _media.FrameResampler == null || _media.FrameResampler.OutputTexture == null ? null : _media.FrameResampler.OutputTexture[0];
  104. Texture texture = _media.UseResampler ? resamplerTex : _media.TextureProducer.GetTexture(0);
  105. if (texture != null)
  106. {
  107. // Check for changing texture
  108. if (texture != _lastTextureApplied)
  109. {
  110. _isDirty = true;
  111. }
  112. if (_isDirty)
  113. {
  114. int planeCount = _media.UseResampler ? 1 : _media.TextureProducer.GetTextureCount();
  115. for (int plane = 0; plane < planeCount; plane++)
  116. {
  117. Texture resamplerTexPlane = _media.FrameResampler == null || _media.FrameResampler.OutputTexture == null ? null : _media.FrameResampler.OutputTexture[plane];
  118. texture = _media.UseResampler ? resamplerTexPlane : _media.TextureProducer.GetTexture(plane);
  119. if (texture != null)
  120. {
  121. ApplyMapping(texture, _media.TextureProducer.RequiresVerticalFlip(), plane, _materialIndex);
  122. }
  123. }
  124. }
  125. applied = true;
  126. }
  127. }
  128. // If the media didn't apply a texture, then try to apply the default texture
  129. if (!applied)
  130. {
  131. if (_defaultTexture != _lastTextureApplied)
  132. {
  133. _isDirty = true;
  134. }
  135. if (_isDirty)
  136. {
  137. ApplyMapping(_defaultTexture, false, 0, _materialIndex);
  138. }
  139. }
  140. }
  141. private void ApplyMapping(Texture texture, bool requiresYFlip, int plane, int materialIndex = -1)
  142. {
  143. if (_renderer != null)
  144. {
  145. _isDirty = false;
  146. Material[] meshMaterials = _renderer.materials;
  147. if (meshMaterials != null)
  148. {
  149. for (int i = 0; i < meshMaterials.Length; i++)
  150. {
  151. if (_materialIndex < 0 || i == _materialIndex)
  152. {
  153. Material mat = meshMaterials[i];
  154. if (mat != null)
  155. {
  156. if (plane == 0)
  157. {
  158. VideoRender.SetupMaterialForMedia(mat, _media, _propTexture.Id, texture, texture == _defaultTexture);
  159. _lastTextureApplied = texture;
  160. #if (!UNITY_EDITOR && UNITY_ANDROID)
  161. if(texture == _defaultTexture) { mat.EnableKeyword("USING_DEFAULT_TEXTURE"); }
  162. else { mat.DisableKeyword("USING_DEFAULT_TEXTURE"); }
  163. #endif
  164. if (texture != null)
  165. {
  166. if (requiresYFlip)
  167. {
  168. mat.SetTextureScale(_propTexture.Id, new Vector2(_scale.x, -_scale.y));
  169. mat.SetTextureOffset(_propTexture.Id, Vector2.up + _offset);
  170. }
  171. else
  172. {
  173. mat.SetTextureScale(_propTexture.Id, _scale);
  174. mat.SetTextureOffset(_propTexture.Id, _offset);
  175. }
  176. }
  177. }
  178. else if (plane == 1)
  179. {
  180. if (texture != null)
  181. {
  182. if (requiresYFlip)
  183. {
  184. mat.SetTextureScale(VideoRender.PropChromaTex.Id, new Vector2(_scale.x, -_scale.y));
  185. mat.SetTextureOffset(VideoRender.PropChromaTex.Id, Vector2.up + _offset);
  186. }
  187. else
  188. {
  189. mat.SetTextureScale(VideoRender.PropChromaTex.Id, _scale);
  190. mat.SetTextureOffset(VideoRender.PropChromaTex.Id, _offset);
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. protected override void OnEnable()
  201. {
  202. if (_renderer == null)
  203. {
  204. _renderer = this.GetComponent<MeshRenderer>();
  205. if (_renderer == null)
  206. {
  207. Debug.LogWarning("[AVProVideo] No MeshRenderer set or found in gameobject");
  208. }
  209. }
  210. _propTexture = new LazyShaderProperty(_texturePropertyName);
  211. ForceUpdate();
  212. }
  213. protected override void OnDisable()
  214. {
  215. ApplyMapping(_defaultTexture, false, 0, _materialIndex);
  216. }
  217. }
  218. }