VideoScreen.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. namespace NRKernal.NRExamples
  11. {
  12. public class VideoScreen
  13. {
  14. protected readonly Vector2 LEFT_RIGHT_TEXTURE_SCALE = new Vector2(0.5f, 1f);
  15. protected readonly Vector2 LEFT_TEXTURE_OFFSET = new Vector2(0.0f, 0.0f);
  16. protected readonly Vector2 RIGHT_TEXTURE_OFFSET = new Vector2(0.5f, 0.0f);
  17. protected Shader m_Shader;
  18. protected Texture m_Texture;
  19. public VideoScreen()
  20. {
  21. CreateShader();
  22. }
  23. public virtual void SetContent(Texture content)
  24. {
  25. m_Texture = content;
  26. }
  27. public virtual void SetScreen(GameObject screen)
  28. {
  29. MeshRenderer[] render = screen.GetComponentsInChildren<MeshRenderer>();
  30. for (int i = 0; i < render.Length; i++)
  31. {
  32. render[i].material = new Material(m_Shader);
  33. }
  34. }
  35. protected void CreateShader()
  36. {
  37. m_Shader = Resources.Load<Shader>("Shaders/VideoShader");
  38. }
  39. }
  40. public class NormalScreen : VideoScreen
  41. {
  42. private Material m_Mat;
  43. public override void SetScreen(GameObject screen)
  44. {
  45. MeshRenderer[] render = screen.GetComponentsInChildren<MeshRenderer>();
  46. m_Mat = new Material(m_Shader);
  47. render[0].material = m_Mat;
  48. }
  49. public override void SetContent(Texture content)
  50. {
  51. base.SetContent(content);
  52. m_Mat.SetTexture("_MainTex", m_Texture);
  53. }
  54. }
  55. public class LeftRightScreen : VideoScreen
  56. {
  57. private Material m_LeftMat;
  58. private Material m_RightMat;
  59. public override void SetScreen(GameObject screen)
  60. {
  61. MeshRenderer[] render = screen.GetComponentsInChildren<MeshRenderer>();
  62. m_LeftMat = new Material(m_Shader);
  63. m_LeftMat.mainTextureScale = LEFT_RIGHT_TEXTURE_SCALE;
  64. m_LeftMat.mainTextureOffset = LEFT_TEXTURE_OFFSET;
  65. m_RightMat = new Material(m_Shader);
  66. m_RightMat.mainTextureScale = LEFT_RIGHT_TEXTURE_SCALE;
  67. m_RightMat.mainTextureOffset= RIGHT_TEXTURE_OFFSET;
  68. render[0].material = m_LeftMat;
  69. render[1].material = m_RightMat;
  70. }
  71. public override void SetContent(Texture content)
  72. {
  73. base.SetContent(content);
  74. m_LeftMat.SetTexture("_MainTex", m_Texture);
  75. m_RightMat.SetTexture("_MainTex", m_Texture);
  76. }
  77. }
  78. }