Advertisement
Guest User

RealmsCamera.cs

a guest
Feb 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.94 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. namespace SonicRealms.Level
  5. {
  6.     public class RealmsCamera : MonoBehaviour
  7.     {
  8.         public float OrthographicSize
  9.         {
  10.             get { return _orthographicSize; }
  11.             set
  12.             {
  13.                 _orthographicSize = value;
  14.                 UpdateOrthographicSize();
  15.             }
  16.         }
  17.  
  18.         public int BaseWidth
  19.         {
  20.             get { return _baseWidth; }
  21.             set
  22.             {
  23.                 _baseWidth = value;
  24.                 UpdateRenderTextureSize();
  25.             }
  26.         }
  27.  
  28.         public int BaseHeight
  29.         {
  30.             get { return _baseHeight; }
  31.             set
  32.             {
  33.                 _baseHeight = value;
  34.                 UpdateRenderTextureSize();
  35.             }
  36.         }
  37.  
  38.         public string GlobalBackgroundTextureName
  39.         {
  40.             get { return _globalBackgroundTextureName; }
  41.             set
  42.             {
  43.                 _globalBackgroundTextureName = value;
  44.                 SetGlobalTextures();
  45.             }
  46.         }
  47.  
  48.         public string GlobalForegroundTextureName
  49.         {
  50.             get { return _globalForegroundTextureName; }
  51.             set
  52.             {
  53.                 _globalForegroundTextureName = value;
  54.                 SetGlobalTextures();
  55.             }
  56.         }
  57.  
  58.         public string GlobalOverlayTextureName
  59.         {
  60.             get { return _globalOverlayTextureName; }
  61.             set
  62.             {
  63.                 _globalOverlayTextureName = value;
  64.                 SetGlobalTextures();
  65.             }
  66.         }
  67.  
  68.         [SerializeField, HideInInspector]
  69.         private RenderTexture _backgroundRenderTexture;
  70.  
  71.         [SerializeField, HideInInspector]
  72.         private RenderTexture _foregroundRenderTexture;
  73.  
  74.         [SerializeField, HideInInspector]
  75.         private RenderTexture _overlayRenderTexture;
  76.  
  77.         [SerializeField, HideInInspector]
  78.         private Camera _backgroundCamera;
  79.  
  80.         [SerializeField, HideInInspector]
  81.         private Camera _foregroundCamera;
  82.  
  83.         [SerializeField, HideInInspector]
  84.         private Camera _overlayCamera;
  85.  
  86.         [SerializeField, HideInInspector]
  87.         private Camera _rendererCamera;
  88.  
  89.         [SerializeField, HideInInspector]
  90.         private MeshRenderer _cameraQuad;
  91.  
  92.         [Header("Dimensions")]
  93.         [SerializeField]
  94.         [Tooltip("Orthographic size of all cameras used by the Realms Camera.")]
  95.         private float _orthographicSize;
  96. #if UNITY_EDITOR
  97.         private float _prevOrthographicSize;
  98. #endif
  99.         [SerializeField]
  100.         [Tooltip("Width of the texture to which cameras render. The texture is then resized to fit the screen.")]
  101.         private int _baseWidth;
  102. #if UNITY_EDITOR
  103.         private int _prevBaseWidth;
  104. #endif
  105.         [SerializeField]
  106.         [Tooltip("Height of the texture to which cameras render. The texture is then resized to fit the screen.")]
  107.         private int _baseHeight;
  108. #if UNITY_EDITOR
  109.         private int _prevBaseHeight;
  110. #endif
  111.         [Header("Shaders")]
  112.         [SerializeField]
  113.         private string _globalBackgroundTextureName;
  114.  
  115.         [SerializeField]
  116.         private string _globalForegroundTextureName;
  117.  
  118.         [SerializeField]
  119.         private string _globalOverlayTextureName;
  120.        
  121.         [SerializeField, HideInInspector]
  122.         private Camera _baseBackgroundCamera;
  123.  
  124.         [SerializeField, HideInInspector]
  125.         private Camera _baseForegroundCamera;
  126.  
  127.         [SerializeField, HideInInspector]
  128.         private Camera _baseOverlayCamera;
  129.  
  130.         [SerializeField, HideInInspector]
  131.         private Camera _baseRendererCamera;
  132.  
  133.         [SerializeField, HideInInspector]
  134.         private MeshRenderer _baseRendererQuad;
  135.  
  136.         [SerializeField, HideInInspector]
  137.         private Material _baseRendererMaterial;
  138.  
  139.         private static List<RealmsCamera> _cameras;
  140.  
  141.         static RealmsCamera()
  142.         {
  143.             _cameras = new List<RealmsCamera>();
  144.         }
  145.  
  146.         public static RealmsCamera Current { get { return _cameras.Count > 0 ? _cameras[0] : null; } }
  147.         public static RealmsCamera[] All { get { return _cameras.ToArray(); } }
  148.  
  149.         public RenderTexture BackgroundRenderTexture { get { return _backgroundRenderTexture; } }
  150.         public RenderTexture ForegroundRenderTexture { get { return _foregroundRenderTexture; } }
  151.  
  152.         public void SetBaseSize(int width, int height)
  153.         {
  154.             _baseWidth = width;
  155.             _baseHeight = height;
  156.             UpdateRenderTextureSize();
  157.         }
  158.  
  159.         protected void Reset()
  160.         {
  161.             for (var i = transform.childCount - 1; i >= 0; --i)
  162.             {
  163.                 DestroyImmediate(transform.GetChild(i).gameObject);
  164.             }
  165.  
  166.             _baseWidth = 320;
  167.             _baseHeight = 224;
  168.  
  169.             _globalBackgroundTextureName = "_GlobalBackgroundTex";
  170.             _globalForegroundTextureName = "_GlobalForegroundTex";
  171.             _globalOverlayTextureName = "_GlobalOverlayTex";
  172.  
  173.             GenerateCameras();
  174.             GenerateRenderTextures();
  175.             SetGlobalTextures();
  176.             GenerateCameraQuad();
  177.         }
  178.  
  179.         protected void Awake()
  180.         {
  181.             SetGlobalTextures();
  182.  
  183.             _cameras.Add(this);
  184.         }
  185.  
  186. #if UNITY_EDITOR
  187.         protected void LateUpdate() //change to Update() later, might fix render to texture error
  188.         {
  189.             if (!Application.isPlaying)
  190.                 SetGlobalTextures();
  191.         }
  192. #endif
  193. #if UNITY_EDITOR
  194.         protected void OnValidate()
  195.         {
  196.             if (_prevOrthographicSize != _orthographicSize)
  197.             {
  198.                 UpdateOrthographicSize();
  199.  
  200.                 _prevOrthographicSize = _orthographicSize;
  201.             }
  202.  
  203.             if (_prevBaseHeight != _baseHeight)
  204.             {
  205.                 if (_baseHeight < 1)
  206.                     _baseHeight = 1;
  207.  
  208.                 SetRenderTextureSize(_baseWidth, _baseHeight);
  209.  
  210.                 _prevBaseHeight = _baseHeight;
  211.             }
  212.  
  213.             if (_prevBaseWidth != _baseWidth)
  214.             {
  215.                 if (_baseWidth < 1)
  216.                     _baseWidth = 1;
  217.  
  218.                 SetRenderTextureSize(_baseWidth, _baseHeight);
  219.  
  220.                 _prevBaseWidth = _baseWidth;
  221.             }
  222.         }
  223. #endif
  224.         private void UpdateOrthographicSize()
  225.         {
  226.             SetOrthographicSize(_orthographicSize);
  227.         }
  228.  
  229.         private void SetOrthographicSize(float orthographicSize)
  230.         {
  231.             _backgroundCamera.orthographicSize = orthographicSize;
  232.             _foregroundCamera.orthographicSize = orthographicSize;
  233.             _overlayCamera.orthographicSize = orthographicSize;
  234.         }
  235.  
  236.         private void UpdateRenderTextureSize()
  237.         {
  238.             SetRenderTextureSize(_baseWidth, _baseHeight);
  239.         }
  240.  
  241.         private void SetRenderTextureSize(int width, int height)
  242.         {
  243.             if (_backgroundRenderTexture != null)
  244.                 _backgroundRenderTexture.Release();
  245.  
  246.             if (_foregroundRenderTexture != null)
  247.                 _foregroundRenderTexture.Release();
  248.  
  249.             if (_overlayRenderTexture != null)
  250.                 _overlayRenderTexture.Release();
  251.  
  252.             _backgroundRenderTexture = _backgroundCamera.targetTexture = new RenderTexture(width, height, 16);
  253.             _backgroundRenderTexture.filterMode = FilterMode.Point;
  254.  
  255.             _foregroundRenderTexture = _foregroundCamera.targetTexture = new RenderTexture(width, height, 16);
  256.             _foregroundRenderTexture.filterMode = FilterMode.Point;
  257.  
  258.             _overlayRenderTexture = _overlayCamera.targetTexture = new RenderTexture(width, height, 16);
  259.             _overlayRenderTexture.filterMode = FilterMode.Point;
  260.  
  261.             _cameraQuad.transform.localScale = new Vector3(_baseWidth/(float) _baseHeight,
  262.                 _cameraQuad.transform.localScale.y,
  263.                 _cameraQuad.transform.localScale.z);
  264.  
  265.             SetGlobalTextures();
  266.         }
  267.  
  268.         private void GenerateCameras()
  269.         {
  270.             if (_backgroundCamera)
  271.                 DestroyImmediate(_backgroundCamera.gameObject);
  272.  
  273.             if (_foregroundCamera)
  274.                 DestroyImmediate(_foregroundCamera.gameObject);
  275.  
  276.             if (_overlayCamera)
  277.                 DestroyImmediate(_overlayCamera.gameObject);
  278.  
  279.             if (_rendererCamera)
  280.                 DestroyImmediate(_rendererCamera.gameObject);
  281.  
  282.             _backgroundCamera = Instantiate(_baseBackgroundCamera);
  283.             _backgroundCamera.transform.SetParent(transform);
  284.             _backgroundCamera.transform.localPosition = Vector3.zero;
  285.             _backgroundCamera.name = "Background Camera";
  286.  
  287.             _foregroundCamera = Instantiate(_baseForegroundCamera);
  288.             _foregroundCamera.transform.SetParent(transform);
  289.             _foregroundCamera.transform.localPosition = Vector3.zero;
  290.             _foregroundCamera.name = "Foreground Camera";
  291.  
  292.             _overlayCamera = Instantiate(_baseOverlayCamera);
  293.             _overlayCamera.transform.SetParent(transform);
  294.             _overlayCamera.transform.localPosition = Vector3.zero;
  295.             _overlayCamera.name = "Overlay Camera";
  296.  
  297.             _rendererCamera = Instantiate(_baseRendererCamera);
  298.             _rendererCamera.transform.SetParent(transform);
  299.             _rendererCamera.transform.localPosition = new Vector3(1000, 1000);
  300.             _rendererCamera.name = "Renderer Camera";
  301.         }
  302.  
  303.         private void GenerateRenderTextures()
  304.         {
  305.             if (_backgroundRenderTexture != null)
  306.             {
  307.                 DestroyImmediate(_backgroundRenderTexture);
  308.                 _backgroundRenderTexture = null;
  309.             }
  310.  
  311.             if (_foregroundRenderTexture != null)
  312.             {
  313.                 DestroyImmediate(_foregroundRenderTexture);
  314.                 _foregroundRenderTexture = null;
  315.             }
  316.  
  317.             if (_overlayRenderTexture != null)
  318.             {
  319.                 DestroyImmediate(_overlayRenderTexture);
  320.                 _overlayRenderTexture = null;
  321.             }
  322.  
  323.             _backgroundCamera.targetTexture = _backgroundRenderTexture = new RenderTexture(_baseWidth, _baseHeight, 16);
  324.             _backgroundRenderTexture.filterMode = FilterMode.Point;
  325.  
  326.             _foregroundCamera.targetTexture = _foregroundRenderTexture = new RenderTexture(_baseWidth, _baseHeight, 16);
  327.             _foregroundRenderTexture.filterMode = FilterMode.Point;
  328.  
  329.             _overlayCamera.targetTexture = _overlayRenderTexture = new RenderTexture(_baseWidth, _baseHeight, 16);
  330.             _overlayRenderTexture.filterMode = FilterMode.Point;
  331.         }
  332.  
  333.         private void GenerateCameraQuad()
  334.         {
  335.             if (_cameraQuad)
  336.             {
  337.                 DestroyImmediate(_cameraQuad.gameObject);
  338.                 _cameraQuad = null;
  339.             }
  340.  
  341.             _cameraQuad = Instantiate(_baseRendererQuad);
  342.             _cameraQuad.transform.SetParent(_rendererCamera.transform);
  343.             _cameraQuad.transform.localPosition = new Vector3(0, 0, 10);
  344.             _cameraQuad.sharedMaterial = _baseRendererMaterial;
  345.             _cameraQuad.name = "Renderer Quad";
  346.  
  347.             _cameraQuad.transform.localScale = new Vector3(_baseWidth/(float) _baseHeight,
  348.                 _cameraQuad.transform.localScale.y,
  349.                 _cameraQuad.transform.localScale.z);
  350.         }
  351.  
  352.         private void SetGlobalTextures()
  353.         {
  354.             if (!string.IsNullOrEmpty(_globalBackgroundTextureName))
  355.                 Shader.SetGlobalTexture(_globalBackgroundTextureName, _backgroundRenderTexture);
  356.  
  357.             if (!string.IsNullOrEmpty(_globalForegroundTextureName))
  358.                 Shader.SetGlobalTexture(_globalForegroundTextureName, _foregroundRenderTexture);
  359.  
  360.             if (!string.IsNullOrEmpty(_globalOverlayTextureName))
  361.                 Shader.SetGlobalTexture(_globalOverlayTextureName, _overlayRenderTexture);
  362.         }
  363.     }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement