ApplyToMesh.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using UnityEngine;
  3. namespace RenderHeads.Media.AVProVideo
  4. {
  5. [AddComponentMenu("AVPro Video/Apply To Mesh", 300)]
  6. [HelpURL("http://renderheads.com/product/avpro-video/")]
  7. public class ApplyToMesh : MonoBehaviour
  8. {
  9. public MediaPlayer Player
  10. {
  11. get
  12. {
  13. return this._media;
  14. }
  15. set
  16. {
  17. if (this._media != value)
  18. {
  19. this._media = value;
  20. this._isDirty = true;
  21. }
  22. }
  23. }
  24. public Texture2D DefaultTexture
  25. {
  26. get
  27. {
  28. return this._defaultTexture;
  29. }
  30. set
  31. {
  32. if (this._defaultTexture != value)
  33. {
  34. this._defaultTexture = value;
  35. this._isDirty = true;
  36. }
  37. }
  38. }
  39. public Renderer MeshRenderer
  40. {
  41. get
  42. {
  43. return this._mesh;
  44. }
  45. set
  46. {
  47. if (this._mesh != value)
  48. {
  49. this._mesh = value;
  50. this._isDirty = true;
  51. }
  52. }
  53. }
  54. public string TexturePropertyName
  55. {
  56. get
  57. {
  58. return this._texturePropertyName;
  59. }
  60. set
  61. {
  62. if (this._texturePropertyName != value)
  63. {
  64. this._texturePropertyName = value;
  65. this._propTexture = Shader.PropertyToID(this._texturePropertyName);
  66. this._isDirty = true;
  67. }
  68. }
  69. }
  70. public Vector2 Offset
  71. {
  72. get
  73. {
  74. return this._offset;
  75. }
  76. set
  77. {
  78. if (this._offset != value)
  79. {
  80. this._offset = value;
  81. this._isDirty = true;
  82. }
  83. }
  84. }
  85. public Vector2 Scale
  86. {
  87. get
  88. {
  89. return this._scale;
  90. }
  91. set
  92. {
  93. if (this._scale != value)
  94. {
  95. this._scale = value;
  96. this._isDirty = true;
  97. }
  98. }
  99. }
  100. public void ForceUpdate()
  101. {
  102. this._isDirty = true;
  103. this.LateUpdate();
  104. }
  105. private void Awake()
  106. {
  107. if (ApplyToMesh._propStereo == 0 || ApplyToMesh._propAlphaPack == 0)
  108. {
  109. ApplyToMesh._propStereo = Shader.PropertyToID("Stereo");
  110. ApplyToMesh._propAlphaPack = Shader.PropertyToID("AlphaPack");
  111. ApplyToMesh._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
  112. }
  113. if (ApplyToMesh._propChromaTex == 0)
  114. {
  115. ApplyToMesh._propChromaTex = Shader.PropertyToID("_ChromaTex");
  116. }
  117. if (ApplyToMesh._propUseYpCbCr == 0)
  118. {
  119. ApplyToMesh._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
  120. }
  121. }
  122. private void LateUpdate()
  123. {
  124. bool flag = false;
  125. if (this._media != null && this._media.TextureProducer != null)
  126. {
  127. Texture texture = (this._media.FrameResampler != null && this._media.FrameResampler.OutputTexture != null) ? this._media.FrameResampler.OutputTexture[0] : null;
  128. Texture texture2 = (!this._media.m_Resample) ? this._media.TextureProducer.GetTexture(0) : texture;
  129. if (texture2 != null)
  130. {
  131. if (texture2 != this._lastTextureApplied)
  132. {
  133. this._isDirty = true;
  134. }
  135. if (this._isDirty)
  136. {
  137. int num = (!this._media.m_Resample) ? this._media.TextureProducer.GetTextureCount() : 1;
  138. for (int i = 0; i < num; i++)
  139. {
  140. Texture texture3 = (this._media.FrameResampler != null && this._media.FrameResampler.OutputTexture != null) ? this._media.FrameResampler.OutputTexture[i] : null;
  141. texture2 = ((!this._media.m_Resample) ? this._media.TextureProducer.GetTexture(i) : texture3);
  142. if (texture2 != null)
  143. {
  144. this.ApplyMapping(texture2, this._media.TextureProducer.RequiresVerticalFlip(), i);
  145. }
  146. }
  147. }
  148. flag = true;
  149. }
  150. }
  151. if (!flag)
  152. {
  153. if (this._defaultTexture != this._lastTextureApplied)
  154. {
  155. this._isDirty = true;
  156. }
  157. if (this._isDirty)
  158. {
  159. this.ApplyMapping(this._defaultTexture, false, 0);
  160. }
  161. }
  162. }
  163. private void ApplyMapping(Texture texture, bool requiresYFlip, int plane = 0)
  164. {
  165. if (this._mesh != null)
  166. {
  167. this._isDirty = false;
  168. Material[] materials = this._mesh.materials;
  169. if (materials != null)
  170. {
  171. foreach (Material material in materials)
  172. {
  173. if (material != null)
  174. {
  175. if (plane == 0)
  176. {
  177. material.SetTexture(this._propTexture, texture);
  178. this._lastTextureApplied = texture;
  179. if (texture != null)
  180. {
  181. if (requiresYFlip)
  182. {
  183. material.SetTextureScale(this._propTexture, new Vector2(this._scale.x, -this._scale.y));
  184. material.SetTextureOffset(this._propTexture, Vector2.up + this._offset);
  185. }
  186. else
  187. {
  188. material.SetTextureScale(this._propTexture, this._scale);
  189. material.SetTextureOffset(this._propTexture, this._offset);
  190. }
  191. }
  192. }
  193. else if (plane == 1 && material.HasProperty(ApplyToMesh._propUseYpCbCr) && material.HasProperty(ApplyToMesh._propChromaTex))
  194. {
  195. material.EnableKeyword("USE_YPCBCR");
  196. material.SetTexture(ApplyToMesh._propChromaTex, texture);
  197. if (requiresYFlip)
  198. {
  199. material.SetTextureScale(ApplyToMesh._propChromaTex, new Vector2(this._scale.x, -this._scale.y));
  200. material.SetTextureOffset(ApplyToMesh._propChromaTex, Vector2.up + this._offset);
  201. }
  202. else
  203. {
  204. material.SetTextureScale(ApplyToMesh._propChromaTex, this._scale);
  205. material.SetTextureOffset(ApplyToMesh._propChromaTex, this._offset);
  206. }
  207. }
  208. if (this._media != null)
  209. {
  210. if (material.HasProperty(ApplyToMesh._propStereo))
  211. {
  212. Helper.SetupStereoMaterial(material, this._media.m_StereoPacking, this._media.m_DisplayDebugStereoColorTint);
  213. }
  214. if (material.HasProperty(ApplyToMesh._propAlphaPack))
  215. {
  216. Helper.SetupAlphaPackedMaterial(material, this._media.m_AlphaPacking);
  217. }
  218. if (material.HasProperty(ApplyToMesh._propApplyGamma) && this._media.Info != null)
  219. {
  220. Helper.SetupGammaMaterial(material, this._media.Info.PlayerSupportsLinearColorSpace());
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. private void OnEnable()
  229. {
  230. if (this._mesh == null)
  231. {
  232. this._mesh = base.GetComponent<MeshRenderer>();
  233. if (this._mesh == null)
  234. {
  235. Debug.LogWarning("[AVProVideo] No mesh renderer set or found in gameobject");
  236. }
  237. }
  238. this._propTexture = Shader.PropertyToID(this._texturePropertyName);
  239. this._isDirty = true;
  240. if (this._mesh != null)
  241. {
  242. this.LateUpdate();
  243. }
  244. }
  245. private void OnDisable()
  246. {
  247. this.ApplyMapping(this._defaultTexture, false, 0);
  248. }
  249. [Header("Media Source")]
  250. [SerializeField]
  251. private MediaPlayer _media;
  252. [Tooltip("Default texture to display when the video texture is preparing")]
  253. [SerializeField]
  254. private Texture2D _defaultTexture;
  255. [Space(8f)]
  256. [Header("Renderer Target")]
  257. [SerializeField]
  258. private Renderer _mesh;
  259. [SerializeField]
  260. private string _texturePropertyName = "_MainTex";
  261. [SerializeField]
  262. private Vector2 _offset = Vector2.zero;
  263. [SerializeField]
  264. private Vector2 _scale = Vector2.one;
  265. private bool _isDirty;
  266. private Texture _lastTextureApplied;
  267. private int _propTexture;
  268. private static int _propStereo;
  269. private static int _propAlphaPack;
  270. private static int _propApplyGamma;
  271. private const string PropChromaTexName = "_ChromaTex";
  272. private static int _propChromaTex;
  273. private const string PropUseYpCbCrName = "_UseYpCbCr";
  274. private static int _propUseYpCbCr;
  275. }
  276. }