using System;
using UnityEngine;

public class OVRLipSyncContextTextureFlip : MonoBehaviour
{
	private void Start()
	{
		this.lipsyncContext = base.GetComponent<OVRLipSyncContextBase>();
		if (this.lipsyncContext == null)
		{
			Debug.Log("LipSyncContextTextureFlip.Start WARNING: No lip sync context component set to object");
		}
	}

	private void Update()
	{
		if (this.lipsyncContext != null && this.material != null)
		{
			OVRLipSync.Frame currentPhonemeFrame = this.lipsyncContext.GetCurrentPhonemeFrame();
			if (currentPhonemeFrame != null)
			{
				for (int i = 0; i < currentPhonemeFrame.Visemes.Length; i++)
				{
					this.oldFrame.Visemes[i] = this.oldFrame.Visemes[i] * this.smoothing + currentPhonemeFrame.Visemes[i] * (1f - this.smoothing);
				}
				this.SetVisemeToTexture();
			}
		}
	}

	private void SetVisemeToTexture()
	{
		int num = -1;
		float num2 = 0f;
		for (int i = 0; i < this.oldFrame.Visemes.Length; i++)
		{
			if (this.oldFrame.Visemes[i] > num2)
			{
				num = i;
				num2 = this.oldFrame.Visemes[i];
			}
		}
		if (num != -1 && num < this.Textures.Length)
		{
			Texture texture = this.Textures[num];
			if (texture != null)
			{
				this.material.SetTexture("_MainTex", texture);
			}
		}
	}

	public Material material;

	public Texture[] Textures = new Texture[OVRLipSync.VisemeCount];

	public float smoothing;

	private OVRLipSyncContextBase lipsyncContext;

	private OVRLipSync.Frame oldFrame = new OVRLipSync.Frame();
}
