123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using System;
- using UnityEngine;
- namespace RenderHeads.Media.AVProVideo
- {
- [AddComponentMenu("AVPro Video/Apply To Material", 300)]
- [HelpURL("http://renderheads.com/product/avpro-video/")]
- public class ApplyToMaterial : MonoBehaviour
- {
- private void Awake()
- {
- if (ApplyToMaterial._propStereo == 0 || ApplyToMaterial._propAlphaPack == 0)
- {
- ApplyToMaterial._propStereo = Shader.PropertyToID("Stereo");
- ApplyToMaterial._propAlphaPack = Shader.PropertyToID("AlphaPack");
- ApplyToMaterial._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
- }
- if (ApplyToMaterial._propChromaTex == 0)
- {
- ApplyToMaterial._propChromaTex = Shader.PropertyToID("_ChromaTex");
- }
- if (ApplyToMaterial._propUseYpCbCr == 0)
- {
- ApplyToMaterial._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
- }
- }
- private void LateUpdate()
- {
- bool flag = false;
- if (this._media != null && this._media.TextureProducer != null)
- {
- int num = (!this._media.m_Resample) ? this._media.TextureProducer.GetTextureCount() : 1;
- for (int i = 0; i < num; i++)
- {
- Texture texture = (this._media.FrameResampler != null && this._media.FrameResampler.OutputTexture != null) ? this._media.FrameResampler.OutputTexture[i] : null;
- Texture texture2 = (!this._media.m_Resample) ? this._media.TextureProducer.GetTexture(i) : texture;
- if (texture2 != null)
- {
- this.ApplyMapping(texture2, this._media.TextureProducer.RequiresVerticalFlip(), i);
- flag = true;
- }
- }
- }
- if (!flag)
- {
- if (this._material.HasProperty(ApplyToMaterial._propUseYpCbCr))
- {
- this._material.DisableKeyword("USE_YPCBCR");
- }
- this.ApplyMapping(this._defaultTexture, false, 0);
- }
- }
- private void ApplyMapping(Texture texture, bool requiresYFlip, int plane = 0)
- {
- if (this._material != null)
- {
- if (plane == 0)
- {
- if (string.IsNullOrEmpty(this._texturePropertyName))
- {
- this._material.mainTexture = texture;
- if (texture != null)
- {
- if (requiresYFlip)
- {
- this._material.mainTextureScale = new Vector2(this._scale.x, -this._scale.y);
- this._material.mainTextureOffset = Vector2.up + this._offset;
- }
- else
- {
- this._material.mainTextureScale = this._scale;
- this._material.mainTextureOffset = this._offset;
- }
- }
- }
- else
- {
- this._material.SetTexture(this._texturePropertyName, texture);
- if (texture != null)
- {
- if (requiresYFlip)
- {
- this._material.SetTextureScale(this._texturePropertyName, new Vector2(this._scale.x, -this._scale.y));
- this._material.SetTextureOffset(this._texturePropertyName, Vector2.up + this._offset);
- }
- else
- {
- this._material.SetTextureScale(this._texturePropertyName, this._scale);
- this._material.SetTextureOffset(this._texturePropertyName, this._offset);
- }
- }
- }
- }
- else if (plane == 1)
- {
- if (this._material.HasProperty(ApplyToMaterial._propUseYpCbCr))
- {
- this._material.EnableKeyword("USE_YPCBCR");
- }
- if (this._material.HasProperty(ApplyToMaterial._propChromaTex))
- {
- this._material.SetTexture(ApplyToMaterial._propChromaTex, texture);
- if (texture != null)
- {
- if (requiresYFlip)
- {
- this._material.SetTextureScale(ApplyToMaterial._propChromaTex, new Vector2(this._scale.x, -this._scale.y));
- this._material.SetTextureOffset(ApplyToMaterial._propChromaTex, Vector2.up + this._offset);
- }
- else
- {
- this._material.SetTextureScale(ApplyToMaterial._propChromaTex, this._scale);
- this._material.SetTextureOffset(ApplyToMaterial._propChromaTex, this._offset);
- }
- }
- }
- }
- if (this._media != null)
- {
- if (this._material.HasProperty(ApplyToMaterial._propStereo))
- {
- Helper.SetupStereoMaterial(this._material, this._media.m_StereoPacking, this._media.m_DisplayDebugStereoColorTint);
- }
- if (this._material.HasProperty(ApplyToMaterial._propAlphaPack))
- {
- Helper.SetupAlphaPackedMaterial(this._material, this._media.m_AlphaPacking);
- }
- if (this._material.HasProperty(ApplyToMaterial._propApplyGamma) && this._media.Info != null)
- {
- Helper.SetupGammaMaterial(this._material, this._media.Info.PlayerSupportsLinearColorSpace());
- }
- }
- }
- }
- private void Start()
- {
- this.SaveProperties();
- this.LateUpdate();
- }
- private void OnEnable()
- {
- this.SaveProperties();
- this.LateUpdate();
- }
- private void OnDisable()
- {
- this.RestoreProperties();
- }
- private void SaveProperties()
- {
- if (this._material != null)
- {
- if (string.IsNullOrEmpty(this._texturePropertyName))
- {
- this._originalTexture = this._material.mainTexture;
- this._originalScale = this._material.mainTextureScale;
- this._originalOffset = this._material.mainTextureOffset;
- }
- else
- {
- this._originalTexture = this._material.GetTexture(this._texturePropertyName);
- this._originalScale = this._material.GetTextureScale(this._texturePropertyName);
- this._originalOffset = this._material.GetTextureOffset(this._texturePropertyName);
- }
- }
- }
- private void RestoreProperties()
- {
- if (this._material != null)
- {
- if (string.IsNullOrEmpty(this._texturePropertyName))
- {
- this._material.mainTexture = this._originalTexture;
- this._material.mainTextureScale = this._originalScale;
- this._material.mainTextureOffset = this._originalOffset;
- }
- else
- {
- this._material.SetTexture(this._texturePropertyName, this._originalTexture);
- this._material.SetTextureScale(this._texturePropertyName, this._originalScale);
- this._material.SetTextureOffset(this._texturePropertyName, this._originalOffset);
- }
- }
- }
- public Vector2 _offset = Vector2.zero;
- public Vector2 _scale = Vector2.one;
- public Material _material;
- public string _texturePropertyName;
- public MediaPlayer _media;
- public Texture2D _defaultTexture;
- private Texture _originalTexture;
- private Vector2 _originalScale = Vector2.one;
- private Vector2 _originalOffset = Vector2.zero;
- private static int _propStereo;
- private static int _propAlphaPack;
- private static int _propApplyGamma;
- private const string PropChromaTexName = "_ChromaTex";
- private static int _propChromaTex;
- private const string PropUseYpCbCrName = "_UseYpCbCr";
- private static int _propUseYpCbCr;
- }
- }
|