Advertisement
Guest User

Untitled

a guest
Jun 28th, 2021
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using System.Collections;
  5.  
  6. [ImageEffectAllowedInSceneView]
  7. [ExecuteInEditMode, RequireComponent(typeof(Camera))]
  8. public class ScreenTexture : MonoBehaviour
  9. {
  10.     public string textureSemanticName = "_GlobalScreenTexture";
  11.    
  12. #if UNITY_EDITOR
  13.     public bool reset;
  14. #endif
  15.  
  16.     private RenderTexture screenRenderTexture;
  17.     private CommandBuffer commandBuffer;
  18.     private Camera cameraComponent;
  19.  
  20.  
  21.     void OnEnable()
  22.     {
  23.         cameraComponent = GetComponent<Camera>();
  24.         SetupCommandBuffer();
  25.     }
  26.  
  27.     void OnDisable()
  28.     {
  29.    
  30.         cameraComponent.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, commandBuffer);
  31.  
  32. #if UNITY_EDITOR
  33.         if (UnityEditor.SceneView.currentDrawingSceneView != null && UnityEditor.SceneView.currentDrawingSceneView.camera != null)
  34.         {
  35.             UnityEditor.SceneView.currentDrawingSceneView.camera.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, commandBuffer);
  36.         }
  37. #endif
  38.  
  39.         ReleaseCommandBuffer();
  40.     }
  41.  
  42.     void SetupCommandBuffer()
  43.     {
  44.         commandBuffer = new CommandBuffer();
  45.         cameraComponent.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, commandBuffer);
  46.        
  47.         //old version, fine for getting screen texture but doesn't preserve opaque alpha
  48.         //commandBuffer.SetGlobalTexture(textureSemanticName, BuiltinRenderTextureType.CurrentActive);
  49.         // commandBuffer.SetGlobalTexture("_GlobalStencil", BuiltinRenderTextureType.CurrentActive);
  50.         // cameraComponent.AddCommandBuffer(CameraEvent., commandBuffer);
  51.  
  52.         // copy screen into temporary RT - this stops transparent effects interfering with the alpha buffer
  53.         var screenCopyID = Shader.PropertyToID("_GrabTexture");
  54.         commandBuffer.GetTemporaryRT(screenCopyID, Screen.width, Screen.height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear, QualitySettings.antiAliasing > 0 ? QualitySettings.antiAliasing : 1);//-1 gets full screen size RTs | -2 get half screen | -4 screen  / 4
  55.         commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, screenCopyID);
  56.         commandBuffer.SetGlobalTexture(textureSemanticName, screenCopyID);
  57.         commandBuffer.ReleaseTemporaryRT(screenCopyID);
  58.  
  59. //make the effect work with editor mode
  60. #if UNITY_EDITOR
  61.         if (UnityEditor.SceneView.currentDrawingSceneView != null && UnityEditor.SceneView.currentDrawingSceneView.camera != null)
  62.         {
  63.             UnityEditor.SceneView.currentDrawingSceneView.camera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, commandBuffer);
  64.         }
  65. #endif
  66.  
  67.     }
  68.  
  69.     void Update()
  70.     {
  71. #if UNITY_EDITOR
  72.         if (reset)
  73.         {
  74.             OnDisable();
  75.             OnEnable();
  76.             reset = false;
  77.         }
  78. #endif
  79.     }
  80.  
  81.     void ReleaseCommandBuffer()
  82.     {
  83.         commandBuffer.Clear();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement