using System;
using System.IO;
using UnityEngine;

public class PhotoCustomObjectSphere : BasePhotoCustomObject
{
	public override byte[] imageBinarys
	{
		get
		{
			return this.imageBinary;
		}
	}

	public override BasePhotoCustomObject.Type type
	{
		get
		{
			return BasePhotoCustomObject.Type.Sphere;
		}
	}

	public override Texture2D mainTexture
	{
		get
		{
			return this.texture;
		}
	}

	public bool isAlphaTexture
	{
		get
		{
			return this.isAlphaTexture_;
		}
	}

	public PhotoCustomObjectSphere.RenderingType renderingType
	{
		get
		{
			return this.renderingType_;
		}
		set
		{
			this.renderingType_ = value;
			if (this.material == null)
			{
				return;
			}
			if (this.renderingType_ == PhotoCustomObjectSphere.RenderingType.TwoSides)
			{
				this.material.SetFloat("_Cull", 0f);
			}
			else if (this.renderingType_ == PhotoCustomObjectSphere.RenderingType.Front)
			{
				this.material.SetFloat("_Cull", 2f);
			}
			else if (this.renderingType_ == PhotoCustomObjectSphere.RenderingType.Back)
			{
				this.material.SetFloat("_Cull", 1f);
			}
		}
	}

	public override void Awake()
	{
		base.Awake();
		this.material = new Material(Shader.Find("CM3D2/Unlit_Texture_Photo_MyObject"));
		foreach (Renderer renderer in this.texRenderers)
		{
			if (renderer != null)
			{
				renderer.material = this.material;
			}
		}
		this.renderingType = PhotoCustomObjectSphere.RenderingType.TwoSides;
		this.material.SetFloat("_ZWrite", 1f);
		this.material.renderQueue = 2980;
		this.scaleTransform.gameObject.SetActive(false);
	}

	public override void OnDestroy()
	{
		base.OnDestroy();
		this.ReleaseTexture();
	}

	public override void Clear()
	{
		base.Clear();
		if (this.material != null && this.material.mainTexture != null)
		{
			this.material.mainTexture = null;
		}
		if (this.scaleTransform != null)
		{
			this.scaleTransform.gameObject.SetActive(false);
		}
		this.ReleaseTexture();
	}

	protected override bool CreateTexture2D(byte[] imageBinary)
	{
		this.ReleaseTexture();
		this.texture = new Texture2D(16, 16);
		if (!this.texture.LoadImage(imageBinary))
		{
			this.ReleaseTexture();
			this.scaleTransform.gameObject.SetActive(false);
			return false;
		}
		if (this.alphaCheck)
		{
			this.isAlphaTexture_ = false;
			Color[] pixels = this.texture.GetPixels();
			for (int i = 0; i < pixels.Length; i++)
			{
				if (pixels[i].a != 1f)
				{
					this.isAlphaTexture_ = true;
					break;
				}
			}
		}
		this.material.SetFloat("_ZWrite", (float)((!this.isAlphaTexture_) ? 1 : 0));
		this.imageBinary = new byte[imageBinary.Length];
		Array.Copy(imageBinary, this.imageBinary, imageBinary.Length);
		this.material.mainTexture = this.texture;
		this.scaleTransform.gameObject.SetActive(true);
		return true;
	}

	protected override void SetTextureColor(Color setColor)
	{
		if (this.material != null)
		{
			this.material.SetColor("_Color", setColor);
		}
	}

	protected override void SetScale(float scale)
	{
		if (this.scaleTransform != null)
		{
			this.scaleTransform.localScale = new Vector3(scale, scale, scale);
		}
	}

	protected override void OnSerialize(BinaryWriter writer)
	{
		writer.Write(1002);
		writer.Write(this.renderingType.ToString());
		writer.Write(this.isAlphaTexture_);
		writer.Write(this.imageBinary.Length);
		writer.Write(this.imageBinary);
	}

	protected override bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader)
	{
		if (type != BasePhotoCustomObject.Type.Sphere)
		{
			return false;
		}
		int num = 1000;
		if (1120 < gameVersion)
		{
			num = reader.ReadInt32();
		}
		if (1002 <= num)
		{
			this.renderingType = (PhotoCustomObjectSphere.RenderingType)Enum.Parse(typeof(PhotoCustomObjectSphere.RenderingType), reader.ReadString());
		}
		else
		{
			this.renderingType = PhotoCustomObjectSphere.RenderingType.TwoSides;
		}
		if (1001 <= num)
		{
			this.isAlphaTexture_ = reader.ReadBoolean();
			this.alphaCheck = false;
		}
		int count = reader.ReadInt32();
		base.SetTexture(reader.ReadBytes(count));
		this.alphaCheck = true;
		return true;
	}

	private void ReleaseTexture()
	{
		if (this.texture != null)
		{
			UnityEngine.Object.DestroyImmediate(this.texture);
			this.texture = null;
			this.imageBinary = null;
		}
	}

	[SerializeField]
	protected Transform scaleTransform;

	[SerializeField]
	protected Renderer[] texRenderers;

	protected Texture2D texture;

	protected Material material;

	protected byte[] imageBinary = new byte[0];

	protected bool isAlphaTexture_;

	private bool alphaCheck = true;

	private PhotoCustomObjectSphere.RenderingType renderingType_;

	public enum RenderingType
	{
		TwoSides,
		Front,
		Back
	}
}
