CubemapCube.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System;
  2. using UnityEngine;
  3. namespace RenderHeads.Media.AVProVideo
  4. {
  5. [RequireComponent(typeof(MeshRenderer))]
  6. [RequireComponent(typeof(MeshFilter))]
  7. [AddComponentMenu("AVPro Video/Cubemap Cube (VR)", 400)]
  8. [HelpURL("http://renderheads.com/product/avpro-video/")]
  9. public class CubemapCube : MonoBehaviour
  10. {
  11. public MediaPlayer Player
  12. {
  13. get
  14. {
  15. return this._mediaPlayer;
  16. }
  17. set
  18. {
  19. this._mediaPlayer = value;
  20. }
  21. }
  22. private void Awake()
  23. {
  24. if (CubemapCube._propApplyGamma == 0)
  25. {
  26. CubemapCube._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
  27. }
  28. if (CubemapCube._propUseYpCbCr == 0)
  29. {
  30. CubemapCube._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
  31. }
  32. if (CubemapCube._propChromaTex == 0)
  33. {
  34. CubemapCube._propChromaTex = Shader.PropertyToID("_ChromaTex");
  35. }
  36. }
  37. private void Start()
  38. {
  39. if (this._mesh == null)
  40. {
  41. this._mesh = new Mesh();
  42. this._mesh.MarkDynamic();
  43. MeshFilter component = base.GetComponent<MeshFilter>();
  44. if (component != null)
  45. {
  46. component.mesh = this._mesh;
  47. }
  48. this._renderer = base.GetComponent<MeshRenderer>();
  49. if (this._renderer != null)
  50. {
  51. this._renderer.material = this._material;
  52. }
  53. this.BuildMesh();
  54. }
  55. }
  56. private void OnDestroy()
  57. {
  58. if (this._mesh != null)
  59. {
  60. MeshFilter component = base.GetComponent<MeshFilter>();
  61. if (component != null)
  62. {
  63. component.mesh = null;
  64. }
  65. UnityEngine.Object.Destroy(this._mesh);
  66. this._mesh = null;
  67. }
  68. if (this._renderer != null)
  69. {
  70. this._renderer.material = null;
  71. this._renderer = null;
  72. }
  73. }
  74. private void LateUpdate()
  75. {
  76. if (Application.isPlaying)
  77. {
  78. if (this._mediaPlayer != null && this._mediaPlayer.Control != null)
  79. {
  80. if (this._mediaPlayer.TextureProducer != null)
  81. {
  82. Texture texture = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[0] : null;
  83. Texture texture2 = (!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(0) : texture;
  84. bool flag = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
  85. if (this._texture != texture2 || this._verticalFlip != flag || (texture2 != null && (this._textureWidth != texture2.width || this._textureHeight != texture2.height)))
  86. {
  87. this._texture = texture2;
  88. if (texture2 != null)
  89. {
  90. this.UpdateMeshUV(texture2.width, texture2.height, flag);
  91. }
  92. }
  93. if (this._renderer.material.HasProperty(CubemapCube._propApplyGamma) && this._mediaPlayer.Info != null)
  94. {
  95. Helper.SetupGammaMaterial(this._renderer.material, this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
  96. }
  97. if (this._renderer.material.HasProperty(CubemapCube._propUseYpCbCr) && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
  98. {
  99. this._renderer.material.EnableKeyword("USE_YPCBCR");
  100. Texture texture3 = (this._mediaPlayer.FrameResampler != null && this._mediaPlayer.FrameResampler.OutputTexture != null) ? this._mediaPlayer.FrameResampler.OutputTexture[1] : null;
  101. this._renderer.material.SetTexture(CubemapCube._propChromaTex, (!this._mediaPlayer.m_Resample) ? this._mediaPlayer.TextureProducer.GetTexture(1) : texture3);
  102. }
  103. }
  104. this._renderer.material.mainTexture = this._texture;
  105. }
  106. else
  107. {
  108. this._renderer.material.mainTexture = null;
  109. }
  110. }
  111. }
  112. private void BuildMesh()
  113. {
  114. Vector3 b = new Vector3(-0.5f, -0.5f, -0.5f);
  115. Vector3[] array = new Vector3[]
  116. {
  117. new Vector3(0f, -1f, 0f) - b,
  118. new Vector3(0f, 0f, 0f) - b,
  119. new Vector3(0f, 0f, -1f) - b,
  120. new Vector3(0f, -1f, -1f) - b,
  121. new Vector3(0f, 0f, 0f) - b,
  122. new Vector3(-1f, 0f, 0f) - b,
  123. new Vector3(-1f, 0f, -1f) - b,
  124. new Vector3(0f, 0f, -1f) - b,
  125. new Vector3(-1f, 0f, 0f) - b,
  126. new Vector3(-1f, -1f, 0f) - b,
  127. new Vector3(-1f, -1f, -1f) - b,
  128. new Vector3(-1f, 0f, -1f) - b,
  129. new Vector3(-1f, -1f, 0f) - b,
  130. new Vector3(0f, -1f, 0f) - b,
  131. new Vector3(0f, -1f, -1f) - b,
  132. new Vector3(-1f, -1f, -1f) - b,
  133. new Vector3(0f, -1f, -1f) - b,
  134. new Vector3(0f, 0f, -1f) - b,
  135. new Vector3(-1f, 0f, -1f) - b,
  136. new Vector3(-1f, -1f, -1f) - b,
  137. new Vector3(-1f, -1f, 0f) - b,
  138. new Vector3(-1f, 0f, 0f) - b,
  139. new Vector3(0f, 0f, 0f) - b,
  140. new Vector3(0f, -1f, 0f) - b
  141. };
  142. Matrix4x4 matrix4x = Matrix4x4.TRS(Vector3.zero, Quaternion.AngleAxis(-90f, Vector3.right), Vector3.one);
  143. for (int i = 0; i < array.Length; i++)
  144. {
  145. array[i] = matrix4x.MultiplyPoint(array[i]);
  146. }
  147. this._mesh.vertices = array;
  148. this._mesh.triangles = new int[]
  149. {
  150. 0,
  151. 1,
  152. 2,
  153. 0,
  154. 2,
  155. 3,
  156. 4,
  157. 5,
  158. 6,
  159. 4,
  160. 6,
  161. 7,
  162. 8,
  163. 9,
  164. 10,
  165. 8,
  166. 10,
  167. 11,
  168. 12,
  169. 13,
  170. 14,
  171. 12,
  172. 14,
  173. 15,
  174. 16,
  175. 17,
  176. 18,
  177. 16,
  178. 18,
  179. 19,
  180. 20,
  181. 21,
  182. 22,
  183. 20,
  184. 22,
  185. 23
  186. };
  187. this._mesh.normals = new Vector3[]
  188. {
  189. new Vector3(-1f, 0f, 0f),
  190. new Vector3(-1f, 0f, 0f),
  191. new Vector3(-1f, 0f, 0f),
  192. new Vector3(-1f, 0f, 0f),
  193. new Vector3(0f, -1f, 0f),
  194. new Vector3(0f, -1f, 0f),
  195. new Vector3(0f, -1f, 0f),
  196. new Vector3(0f, -1f, 0f),
  197. new Vector3(1f, 0f, 0f),
  198. new Vector3(1f, 0f, 0f),
  199. new Vector3(1f, 0f, 0f),
  200. new Vector3(1f, 0f, 0f),
  201. new Vector3(0f, 1f, 0f),
  202. new Vector3(0f, 1f, 0f),
  203. new Vector3(0f, 1f, 0f),
  204. new Vector3(0f, 1f, 0f),
  205. new Vector3(0f, 0f, 1f),
  206. new Vector3(0f, 0f, 1f),
  207. new Vector3(0f, 0f, 1f),
  208. new Vector3(0f, 0f, 1f),
  209. new Vector3(0f, 0f, -1f),
  210. new Vector3(0f, 0f, -1f),
  211. new Vector3(0f, 0f, -1f),
  212. new Vector3(0f, 0f, -1f)
  213. };
  214. this.UpdateMeshUV(512, 512, false);
  215. }
  216. private void UpdateMeshUV(int textureWidth, int textureHeight, bool flipY)
  217. {
  218. this._textureWidth = textureWidth;
  219. this._textureHeight = textureHeight;
  220. this._verticalFlip = flipY;
  221. float num = (float)textureWidth;
  222. float num2 = (float)textureHeight;
  223. float num3 = num / 3f;
  224. float num4 = Mathf.Floor((this.expansion_coeff * num3 - num3) / 2f);
  225. float num5 = num4 / num;
  226. float num6 = num4 / num2;
  227. Vector2[] array = null;
  228. if (this._layout == CubemapCube.Layout.Facebook360Capture)
  229. {
  230. array = new Vector2[]
  231. {
  232. new Vector2(0.33333334f + num5, 0.5f - num6),
  233. new Vector2(0.6666667f - num5, 0.5f - num6),
  234. new Vector2(0.6666667f - num5, num6),
  235. new Vector2(0.33333334f + num5, num6),
  236. new Vector2(0.33333334f + num5, 1f - num6),
  237. new Vector2(0.6666667f - num5, 1f - num6),
  238. new Vector2(0.6666667f - num5, 0.5f + num6),
  239. new Vector2(0.33333334f + num5, 0.5f + num6),
  240. new Vector2(num5, 0.5f - num6),
  241. new Vector2(0.33333334f - num5, 0.5f - num6),
  242. new Vector2(0.33333334f - num5, num6),
  243. new Vector2(num5, num6),
  244. new Vector2(0.6666667f + num5, 1f - num6),
  245. new Vector2(1f - num5, 1f - num6),
  246. new Vector2(1f - num5, 0.5f + num6),
  247. new Vector2(0.6666667f + num5, 0.5f + num6),
  248. new Vector2(0.6666667f + num5, num6),
  249. new Vector2(0.6666667f + num5, 0.5f - num6),
  250. new Vector2(1f - num5, 0.5f - num6),
  251. new Vector2(1f - num5, num6),
  252. new Vector2(0.33333334f - num5, 1f - num6),
  253. new Vector2(0.33333334f - num5, 0.5f + num6),
  254. new Vector2(num5, 0.5f + num6),
  255. new Vector2(num5, 1f - num6)
  256. };
  257. }
  258. else if (this._layout == CubemapCube.Layout.FacebookTransform32)
  259. {
  260. array = new Vector2[]
  261. {
  262. new Vector2(0.33333334f + num5, 1f - num6),
  263. new Vector2(0.6666667f - num5, 1f - num6),
  264. new Vector2(0.6666667f - num5, 0.5f + num6),
  265. new Vector2(0.33333334f + num5, 0.5f + num6),
  266. new Vector2(0.33333334f + num5, 0.5f - num6),
  267. new Vector2(0.6666667f - num5, 0.5f - num6),
  268. new Vector2(0.6666667f - num5, num6),
  269. new Vector2(0.33333334f + num5, num6),
  270. new Vector2(num5, 1f - num6),
  271. new Vector2(0.33333334f - num5, 1f - num6),
  272. new Vector2(0.33333334f - num5, 0.5f + num6),
  273. new Vector2(num5, 0.5f + num6),
  274. new Vector2(0.6666667f + num5, 0.5f - num6),
  275. new Vector2(1f - num5, 0.5f - num6),
  276. new Vector2(1f - num5, num6),
  277. new Vector2(0.6666667f + num5, num6),
  278. new Vector2(num5, num6),
  279. new Vector2(num5, 0.5f - num6),
  280. new Vector2(0.33333334f - num5, 0.5f - num6),
  281. new Vector2(0.33333334f - num5, num6),
  282. new Vector2(1f - num5, 1f - num6),
  283. new Vector2(1f - num5, 0.5f + num6),
  284. new Vector2(0.6666667f + num5, 0.5f + num6),
  285. new Vector2(0.6666667f + num5, 1f - num6)
  286. };
  287. }
  288. if (flipY)
  289. {
  290. for (int i = 0; i < array.Length; i++)
  291. {
  292. array[i].y = 1f - array[i].y;
  293. }
  294. }
  295. this._mesh.uv = array;
  296. this._mesh.UploadMeshData(false);
  297. }
  298. private Mesh _mesh;
  299. protected MeshRenderer _renderer;
  300. [SerializeField]
  301. protected Material _material;
  302. [SerializeField]
  303. private MediaPlayer _mediaPlayer;
  304. [SerializeField]
  305. private float expansion_coeff = 1.01f;
  306. [SerializeField]
  307. private CubemapCube.Layout _layout;
  308. private Texture _texture;
  309. private bool _verticalFlip;
  310. private int _textureWidth;
  311. private int _textureHeight;
  312. private static int _propApplyGamma;
  313. private static int _propUseYpCbCr;
  314. private const string PropChromaTexName = "_ChromaTex";
  315. private static int _propChromaTex;
  316. public enum Layout
  317. {
  318. FacebookTransform32,
  319. Facebook360Capture
  320. }
  321. }
  322. }