Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.6 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(Camera))]
  5. [ImageEffectAllowedInSceneView]
  6. [ExecuteInEditMode]
  7. public class Pixelize : MonoBehaviour
  8. {
  9. #region Private Members
  10. private Shader _screenAndMaskShader;
  11. private Material _screenAndMaskMaterial;
  12. private RenderTexture _temporaryRenderTexture;
  13. private Shader _combineLayersShader;
  14. private Material _combineLayersMaterial;
  15. #endregion
  16. #region Properties
  17. private Shader ScreenAndMaskShader
  18. {
  19. get
  20. {
  21. if(_screenAndMaskShader == null)
  22. {
  23. _screenAndMaskShader = Shader.Find("Hidden/PostProcess/Pixelize/ScreenAndMask");
  24. }
  25. return _screenAndMaskShader;
  26. }
  27. }
  28. private Material ScreenAndMaskMaterial
  29. {
  30. get
  31. {
  32. if(_screenAndMaskMaterial == null)
  33. {
  34. _screenAndMaskMaterial = new Material(ScreenAndMaskShader);
  35. }
  36. return _screenAndMaskMaterial;
  37. }
  38. }
  39. private RenderTexture TemporaryRenderTarget
  40. {
  41. get
  42. {
  43. if(_temporaryRenderTexture == null)
  44. {
  45. CreateTemporaryRenderTarget();
  46. }
  47. return _temporaryRenderTexture;
  48. }
  49. }
  50. private Shader CombineLayersShader
  51. {
  52. get
  53. {
  54. if(_combineLayersShader == null)
  55. {
  56. _combineLayersShader = Shader.Find("Hidden/PostProcess/Pixelize/CombineLayers");
  57. }
  58. return _combineLayersShader;
  59. }
  60. }
  61. private Material CombineLayersMaterial
  62. {
  63. get
  64. {
  65. if(_combineLayersMaterial == null)
  66. {
  67. _combineLayersMaterial = new Material(CombineLayersShader);
  68. }
  69. return _combineLayersMaterial;
  70. }
  71. }
  72. #endregion
  73. #region Functions
  74. void OnRenderImage(RenderTexture src, RenderTexture dest)
  75. {
  76. CheckTemporaryRenderTarget();
  77. Graphics.Blit(src, TemporaryRenderTarget, ScreenAndMaskMaterial);
  78. Graphics.Blit(TemporaryRenderTarget, dest, CombineLayersMaterial);
  79. }
  80. private void CreateTemporaryRenderTarget()
  81. {
  82. _temporaryRenderTexture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); // better bit precision on Alpha would be preferable but 8 is enough for the current pixelization effect which is already banded
  83. _temporaryRenderTexture.useMipMap = true;
  84. _temporaryRenderTexture.autoGenerateMips = true;
  85. _temporaryRenderTexture.wrapMode = TextureWrapMode.Clamp;
  86. _temporaryRenderTexture.filterMode = FilterMode.Point;
  87. _temporaryRenderTexture.Create();
  88. }
  89. private void CheckTemporaryRenderTarget()
  90. {
  91. if(TemporaryRenderTarget.width != Screen.width || TemporaryRenderTarget.width != Screen.height)
  92. {
  93. ReleaseTemporaryRenderTarget();
  94. }
  95. }
  96. private void ReleaseTemporaryRenderTarget()
  97. {
  98. _temporaryRenderTexture.Release();
  99. _temporaryRenderTexture = null;
  100. }
  101. #endregion
  102. }