using System;
using UnityEngine;
using UnityEngine.UI;

namespace RenderHeads.Media.AVProVideo
{
	[AddComponentMenu("AVPro Video/Update Stereo Material", 400)]
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	public class UpdateStereoMaterial : MonoBehaviour
	{
		private void Awake()
		{
			this._cameraPositionId = Shader.PropertyToID("_cameraPosition");
			this._viewMatrixId = Shader.PropertyToID("_ViewMatrix");
			if (this._camera == null)
			{
				Debug.LogWarning("[AVProVideo] No camera set for UpdateStereoMaterial component. If you are rendering in stereo then it is recommended to set this.");
			}
		}

		private void SetupMaterial(Material m, Camera camera)
		{
			m.SetVector(this._cameraPositionId, camera.transform.position);
			m.SetMatrix(this._viewMatrixId, camera.worldToCameraMatrix.transpose);
		}

		private void LateUpdate()
		{
			Camera camera = this._camera;
			if (camera == null)
			{
				camera = Camera.main;
			}
			if (this._renderer == null && this._material == null)
			{
				this._renderer = base.gameObject.GetComponent<MeshRenderer>();
			}
			if (camera != null)
			{
				if (this._renderer != null)
				{
					this.SetupMaterial(this._renderer.material, camera);
				}
				if (this._material != null)
				{
					this.SetupMaterial(this._material, camera);
				}
				if (this._uGuiComponent != null)
				{
					this.SetupMaterial(this._uGuiComponent.material, camera);
				}
			}
		}

		[Header("Stereo camera")]
		public Camera _camera;

		[Header("Rendering elements")]
		public MeshRenderer _renderer;

		public Graphic _uGuiComponent;

		public Material _material;

		private int _cameraPositionId;

		private int _viewMatrixId;
	}
}
