123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using System;
- using UnityEngine;
- namespace RenderHeads.Media.AVProVideo
- {
- [RequireComponent(typeof(MeshRenderer))]
- [RequireComponent(typeof(MeshFilter))]
- [AddComponentMenu("AVPro Video/Cubemap Cube (VR)", 400)]
- [HelpURL("http://renderheads.com/product/avpro-video/")]
- public class CubemapCube : MonoBehaviour
- {
- public MediaPlayer Player
- {
- get
- {
- return this._mediaPlayer;
- }
- set
- {
- this._mediaPlayer = value;
- }
- }
- private void Awake()
- {
- if (CubemapCube._propApplyGamma == 0)
- {
- CubemapCube._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
- }
- if (CubemapCube._propUseYpCbCr == 0)
- {
- CubemapCube._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
- }
- if (CubemapCube._propChromaTex == 0)
- {
- CubemapCube._propChromaTex = Shader.PropertyToID("_ChromaTex");
- }
- }
- private void Start()
- {
- if (this._mesh == null)
- {
- this._mesh = new Mesh();
- this._mesh.MarkDynamic();
- MeshFilter component = base.GetComponent<MeshFilter>();
- if (component != null)
- {
- component.mesh = this._mesh;
- }
- this._renderer = base.GetComponent<MeshRenderer>();
- if (this._renderer != null)
- {
- this._renderer.material = this._material;
- }
- this.BuildMesh();
- }
- }
- private void OnDestroy()
- {
- if (this._mesh != null)
- {
- MeshFilter component = base.GetComponent<MeshFilter>();
- if (component != null)
- {
- component.mesh = null;
- }
- UnityEngine.Object.Destroy(this._mesh);
- this._mesh = null;
- }
- if (this._renderer != null)
- {
- this._renderer.material = null;
- this._renderer = null;
- }
- }
- private void LateUpdate()
- {
- if (Application.isPlaying)
- {
- if (this._mediaPlayer != null && this._mediaPlayer.Control != null)
- {
- if (this._mediaPlayer.TextureProducer != null)
- {
- Texture texture = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[0] : null;
- Texture texture2 = (!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(0) : texture;
- bool flag = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
- if (this._texture != texture2 || this._verticalFlip != flag || (texture2 != null && (this._textureWidth != texture2.width || this._textureHeight != texture2.height)))
- {
- this._texture = texture2;
- if (texture2 != null)
- {
- this.UpdateMeshUV(texture2.width, texture2.height, flag);
- }
- }
- if (this._renderer.material.HasProperty(CubemapCube._propApplyGamma) && this._mediaPlayer.Info != null)
- {
- Helper.SetupGammaMaterial(this._renderer.material, this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
- }
- if (this._renderer.material.HasProperty(CubemapCube._propUseYpCbCr) && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
- {
- this._renderer.material.EnableKeyword("USE_YPCBCR");
- Texture texture3 = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[1] : null;
- this._renderer.material.SetTexture(CubemapCube._propChromaTex, (!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(1) : texture3);
- }
- }
- this._renderer.material.mainTexture = this._texture;
- }
- else
- {
- this._renderer.material.mainTexture = null;
- }
- }
- }
- private void BuildMesh()
- {
- Vector3 b = new Vector3(-0.5f, -0.5f, -0.5f);
- Vector3[] array = new Vector3[]
- {
- new Vector3(0f, -1f, 0f) - b,
- new Vector3(0f, 0f, 0f) - b,
- new Vector3(0f, 0f, -1f) - b,
- new Vector3(0f, -1f, -1f) - b,
- new Vector3(0f, 0f, 0f) - b,
- new Vector3(-1f, 0f, 0f) - b,
- new Vector3(-1f, 0f, -1f) - b,
- new Vector3(0f, 0f, -1f) - b,
- new Vector3(-1f, 0f, 0f) - b,
- new Vector3(-1f, -1f, 0f) - b,
- new Vector3(-1f, -1f, -1f) - b,
- new Vector3(-1f, 0f, -1f) - b,
- new Vector3(-1f, -1f, 0f) - b,
- new Vector3(0f, -1f, 0f) - b,
- new Vector3(0f, -1f, -1f) - b,
- new Vector3(-1f, -1f, -1f) - b,
- new Vector3(0f, -1f, -1f) - b,
- new Vector3(0f, 0f, -1f) - b,
- new Vector3(-1f, 0f, -1f) - b,
- new Vector3(-1f, -1f, -1f) - b,
- new Vector3(-1f, -1f, 0f) - b,
- new Vector3(-1f, 0f, 0f) - b,
- new Vector3(0f, 0f, 0f) - b,
- new Vector3(0f, -1f, 0f) - b
- };
- Matrix4x4 matrix4x = Matrix4x4.TRS(Vector3.zero, Quaternion.AngleAxis(-90f, Vector3.right), Vector3.one);
- for (int i = 0; i < array.Length; i++)
- {
- array[i] = matrix4x.MultiplyPoint(array[i]);
- }
- this._mesh.vertices = array;
- this._mesh.triangles = new int[]
- {
- 0,
- 1,
- 2,
- 0,
- 2,
- 3,
- 4,
- 5,
- 6,
- 4,
- 6,
- 7,
- 8,
- 9,
- 10,
- 8,
- 10,
- 11,
- 12,
- 13,
- 14,
- 12,
- 14,
- 15,
- 16,
- 17,
- 18,
- 16,
- 18,
- 19,
- 20,
- 21,
- 22,
- 20,
- 22,
- 23
- };
- this._mesh.normals = new Vector3[]
- {
- new Vector3(-1f, 0f, 0f),
- new Vector3(-1f, 0f, 0f),
- new Vector3(-1f, 0f, 0f),
- new Vector3(-1f, 0f, 0f),
- new Vector3(0f, -1f, 0f),
- new Vector3(0f, -1f, 0f),
- new Vector3(0f, -1f, 0f),
- new Vector3(0f, -1f, 0f),
- new Vector3(1f, 0f, 0f),
- new Vector3(1f, 0f, 0f),
- new Vector3(1f, 0f, 0f),
- new Vector3(1f, 0f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 0f, 1f),
- new Vector3(0f, 0f, 1f),
- new Vector3(0f, 0f, 1f),
- new Vector3(0f, 0f, 1f),
- new Vector3(0f, 0f, -1f),
- new Vector3(0f, 0f, -1f),
- new Vector3(0f, 0f, -1f),
- new Vector3(0f, 0f, -1f)
- };
- this.UpdateMeshUV(512, 512, false);
- }
- private void UpdateMeshUV(int textureWidth, int textureHeight, bool flipY)
- {
- this._textureWidth = textureWidth;
- this._textureHeight = textureHeight;
- this._verticalFlip = flipY;
- float num = (float)textureWidth;
- float num2 = (float)textureHeight;
- float num3 = num / 3f;
- float num4 = Mathf.Floor((this.expansion_coeff * num3 - num3) / 2f);
- float num5 = num4 / num;
- float num6 = num4 / num2;
- Vector2[] array = null;
- if (this._layout == CubemapCube.Layout.Facebook360Capture)
- {
- array = new Vector2[]
- {
- new Vector2(0.333333343f + num5, 0.5f - num6),
- new Vector2(0.6666667f - num5, 0.5f - num6),
- new Vector2(0.6666667f - num5, num6),
- new Vector2(0.333333343f + num5, num6),
- new Vector2(0.333333343f + num5, 1f - num6),
- new Vector2(0.6666667f - num5, 1f - num6),
- new Vector2(0.6666667f - num5, 0.5f + num6),
- new Vector2(0.333333343f + num5, 0.5f + num6),
- new Vector2(num5, 0.5f - num6),
- new Vector2(0.333333343f - num5, 0.5f - num6),
- new Vector2(0.333333343f - num5, num6),
- new Vector2(num5, num6),
- new Vector2(0.6666667f + num5, 1f - num6),
- new Vector2(1f - num5, 1f - num6),
- new Vector2(1f - num5, 0.5f + num6),
- new Vector2(0.6666667f + num5, 0.5f + num6),
- new Vector2(0.6666667f + num5, num6),
- new Vector2(0.6666667f + num5, 0.5f - num6),
- new Vector2(1f - num5, 0.5f - num6),
- new Vector2(1f - num5, num6),
- new Vector2(0.333333343f - num5, 1f - num6),
- new Vector2(0.333333343f - num5, 0.5f + num6),
- new Vector2(num5, 0.5f + num6),
- new Vector2(num5, 1f - num6)
- };
- }
- else if (this._layout == CubemapCube.Layout.FacebookTransform32)
- {
- array = new Vector2[]
- {
- new Vector2(0.333333343f + num5, 1f - num6),
- new Vector2(0.6666667f - num5, 1f - num6),
- new Vector2(0.6666667f - num5, 0.5f + num6),
- new Vector2(0.333333343f + num5, 0.5f + num6),
- new Vector2(0.333333343f + num5, 0.5f - num6),
- new Vector2(0.6666667f - num5, 0.5f - num6),
- new Vector2(0.6666667f - num5, num6),
- new Vector2(0.333333343f + num5, num6),
- new Vector2(num5, 1f - num6),
- new Vector2(0.333333343f - num5, 1f - num6),
- new Vector2(0.333333343f - num5, 0.5f + num6),
- new Vector2(num5, 0.5f + num6),
- new Vector2(0.6666667f + num5, 0.5f - num6),
- new Vector2(1f - num5, 0.5f - num6),
- new Vector2(1f - num5, num6),
- new Vector2(0.6666667f + num5, num6),
- new Vector2(num5, num6),
- new Vector2(num5, 0.5f - num6),
- new Vector2(0.333333343f - num5, 0.5f - num6),
- new Vector2(0.333333343f - num5, num6),
- new Vector2(1f - num5, 1f - num6),
- new Vector2(1f - num5, 0.5f + num6),
- new Vector2(0.6666667f + num5, 0.5f + num6),
- new Vector2(0.6666667f + num5, 1f - num6)
- };
- }
- if (flipY)
- {
- for (int i = 0; i < array.Length; i++)
- {
- array[i].y = 1f - array[i].y;
- }
- }
- this._mesh.uv = array;
- this._mesh.UploadMeshData(false);
- }
- private Mesh _mesh;
- protected MeshRenderer _renderer;
- [SerializeField]
- protected Material _material;
- [SerializeField]
- private MediaPlayer _mediaPlayer;
- [SerializeField]
- private float expansion_coeff = 1.01f;
- [SerializeField]
- private CubemapCube.Layout _layout;
- private Texture _texture;
- private bool _verticalFlip;
- private int _textureWidth;
- private int _textureHeight;
- private static int _propApplyGamma;
- private static int _propUseYpCbCr;
- private const string PropChromaTexName = "_ChromaTex";
- private static int _propChromaTex;
- public enum Layout
- {
- FacebookTransform32,
- Facebook360Capture
- }
- }
- }
|