ApplyToMaterial.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using UnityEngine;
  3. namespace RenderHeads.Media.AVProVideo
  4. {
  5. [AddComponentMenu("AVPro Video/Apply To Material", 300)]
  6. [HelpURL("http://renderheads.com/product/avpro-video/")]
  7. public class ApplyToMaterial : MonoBehaviour
  8. {
  9. private void Awake()
  10. {
  11. if (ApplyToMaterial._propStereo == 0 || ApplyToMaterial._propAlphaPack == 0)
  12. {
  13. ApplyToMaterial._propStereo = Shader.PropertyToID("Stereo");
  14. ApplyToMaterial._propAlphaPack = Shader.PropertyToID("AlphaPack");
  15. ApplyToMaterial._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
  16. }
  17. if (ApplyToMaterial._propChromaTex == 0)
  18. {
  19. ApplyToMaterial._propChromaTex = Shader.PropertyToID("_ChromaTex");
  20. }
  21. if (ApplyToMaterial._propUseYpCbCr == 0)
  22. {
  23. ApplyToMaterial._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
  24. }
  25. }
  26. private void LateUpdate()
  27. {
  28. bool flag = false;
  29. if (this._media != null && this._media.TextureProducer != null)
  30. {
  31. int num = (!this._media.m_Resample) ? this._media.TextureProducer.GetTextureCount() : 1;
  32. for (int i = 0; i < num; i++)
  33. {
  34. Texture texture = (this._media.FrameResampler != null && this._media.FrameResampler.OutputTexture != null) ? this._media.FrameResampler.OutputTexture[i] : null;
  35. Texture texture2 = (!this._media.m_Resample) ? this._media.TextureProducer.GetTexture(i) : texture;
  36. if (texture2 != null)
  37. {
  38. this.ApplyMapping(texture2, this._media.TextureProducer.RequiresVerticalFlip(), i);
  39. flag = true;
  40. }
  41. }
  42. }
  43. if (!flag)
  44. {
  45. if (this._material.HasProperty(ApplyToMaterial._propUseYpCbCr))
  46. {
  47. this._material.DisableKeyword("USE_YPCBCR");
  48. }
  49. this.ApplyMapping(this._defaultTexture, false, 0);
  50. }
  51. }
  52. private void ApplyMapping(Texture texture, bool requiresYFlip, int plane = 0)
  53. {
  54. if (this._material != null)
  55. {
  56. if (plane == 0)
  57. {
  58. if (string.IsNullOrEmpty(this._texturePropertyName))
  59. {
  60. this._material.mainTexture = texture;
  61. if (texture != null)
  62. {
  63. if (requiresYFlip)
  64. {
  65. this._material.mainTextureScale = new Vector2(this._scale.x, -this._scale.y);
  66. this._material.mainTextureOffset = Vector2.up + this._offset;
  67. }
  68. else
  69. {
  70. this._material.mainTextureScale = this._scale;
  71. this._material.mainTextureOffset = this._offset;
  72. }
  73. }
  74. }
  75. else
  76. {
  77. this._material.SetTexture(this._texturePropertyName, texture);
  78. if (texture != null)
  79. {
  80. if (requiresYFlip)
  81. {
  82. this._material.SetTextureScale(this._texturePropertyName, new Vector2(this._scale.x, -this._scale.y));
  83. this._material.SetTextureOffset(this._texturePropertyName, Vector2.up + this._offset);
  84. }
  85. else
  86. {
  87. this._material.SetTextureScale(this._texturePropertyName, this._scale);
  88. this._material.SetTextureOffset(this._texturePropertyName, this._offset);
  89. }
  90. }
  91. }
  92. }
  93. else if (plane == 1)
  94. {
  95. if (this._material.HasProperty(ApplyToMaterial._propUseYpCbCr))
  96. {
  97. this._material.EnableKeyword("USE_YPCBCR");
  98. }
  99. if (this._material.HasProperty(ApplyToMaterial._propChromaTex))
  100. {
  101. this._material.SetTexture(ApplyToMaterial._propChromaTex, texture);
  102. if (texture != null)
  103. {
  104. if (requiresYFlip)
  105. {
  106. this._material.SetTextureScale(ApplyToMaterial._propChromaTex, new Vector2(this._scale.x, -this._scale.y));
  107. this._material.SetTextureOffset(ApplyToMaterial._propChromaTex, Vector2.up + this._offset);
  108. }
  109. else
  110. {
  111. this._material.SetTextureScale(ApplyToMaterial._propChromaTex, this._scale);
  112. this._material.SetTextureOffset(ApplyToMaterial._propChromaTex, this._offset);
  113. }
  114. }
  115. }
  116. }
  117. if (this._media != null)
  118. {
  119. if (this._material.HasProperty(ApplyToMaterial._propStereo))
  120. {
  121. Helper.SetupStereoMaterial(this._material, this._media.m_StereoPacking, this._media.m_DisplayDebugStereoColorTint);
  122. }
  123. if (this._material.HasProperty(ApplyToMaterial._propAlphaPack))
  124. {
  125. Helper.SetupAlphaPackedMaterial(this._material, this._media.m_AlphaPacking);
  126. }
  127. if (this._material.HasProperty(ApplyToMaterial._propApplyGamma) && this._media.Info != null)
  128. {
  129. Helper.SetupGammaMaterial(this._material, this._media.Info.PlayerSupportsLinearColorSpace());
  130. }
  131. }
  132. }
  133. }
  134. private void Start()
  135. {
  136. this.SaveProperties();
  137. this.LateUpdate();
  138. }
  139. private void OnEnable()
  140. {
  141. this.SaveProperties();
  142. this.LateUpdate();
  143. }
  144. private void OnDisable()
  145. {
  146. this.RestoreProperties();
  147. }
  148. private void SaveProperties()
  149. {
  150. if (this._material != null)
  151. {
  152. if (string.IsNullOrEmpty(this._texturePropertyName))
  153. {
  154. this._originalTexture = this._material.mainTexture;
  155. this._originalScale = this._material.mainTextureScale;
  156. this._originalOffset = this._material.mainTextureOffset;
  157. }
  158. else
  159. {
  160. this._originalTexture = this._material.GetTexture(this._texturePropertyName);
  161. this._originalScale = this._material.GetTextureScale(this._texturePropertyName);
  162. this._originalOffset = this._material.GetTextureOffset(this._texturePropertyName);
  163. }
  164. }
  165. }
  166. private void RestoreProperties()
  167. {
  168. if (this._material != null)
  169. {
  170. if (string.IsNullOrEmpty(this._texturePropertyName))
  171. {
  172. this._material.mainTexture = this._originalTexture;
  173. this._material.mainTextureScale = this._originalScale;
  174. this._material.mainTextureOffset = this._originalOffset;
  175. }
  176. else
  177. {
  178. this._material.SetTexture(this._texturePropertyName, this._originalTexture);
  179. this._material.SetTextureScale(this._texturePropertyName, this._originalScale);
  180. this._material.SetTextureOffset(this._texturePropertyName, this._originalOffset);
  181. }
  182. }
  183. }
  184. public Vector2 _offset = Vector2.zero;
  185. public Vector2 _scale = Vector2.one;
  186. public Material _material;
  187. public string _texturePropertyName;
  188. public MediaPlayer _media;
  189. public Texture2D _defaultTexture;
  190. private Texture _originalTexture;
  191. private Vector2 _originalScale = Vector2.one;
  192. private Vector2 _originalOffset = Vector2.zero;
  193. private static int _propStereo;
  194. private static int _propAlphaPack;
  195. private static int _propApplyGamma;
  196. private const string PropChromaTexName = "_ChromaTex";
  197. private static int _propChromaTex;
  198. private const string PropUseYpCbCrName = "_UseYpCbCr";
  199. private static int _propUseYpCbCr;
  200. }
  201. }