CopyToSurface.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. #if ZED_HDRP || ZED_URP
  6. using UnityEngine.Rendering;
  7. #endif
  8. /// <summary>
  9. /// Copies the output of the camera to the selected RawImage, and/or Renderer as a material.
  10. /// Use to look at the camera on something other than the final screen output, like a plane or UI element.
  11. /// </summary>
  12. [RequireComponent(typeof(Camera))]
  13. public class CopyToSurface : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// 2D Raw Image object that you can have the camera output copied to.
  17. /// </summary>
  18. [Tooltip("2D Raw Image object that you can have the camera output copied to.")]
  19. public RawImage canvasRawImage;
  20. /// <summary>
  21. /// 3D Renderer that will have its mainTexture set to the camera texture.
  22. /// </summary>
  23. [Tooltip("3D Renderer that will have its mainTexture set to the camera texture.")]
  24. public Renderer worldRenderer;
  25. /// <summary>
  26. /// If worldRenderer is set, this is the name of the texture property that will be set with the camera image.
  27. /// "_MainTex" works for most Standard render pipeline materials. "_BaseMap" works for most lit URP materials and "_BaseColorMap" for lit HDRP materials.
  28. /// </summary>
  29. [Tooltip("If worldRenderer is set, this is the name of the texture property that will be set with the camera image.\r\n\n" +
  30. "'_MainTex' works for most Standard render pipeline materials, '_BaseMap' works for most lit URP materials and '_BaseColorMap' for lit HDRP materials.")]
  31. #if !ZED_HDRP && !ZED_URP
  32. public string rendererTextureProperty = "_MainTex";
  33. #elif ZED_URP
  34. public string rendererTextureProperty = "_BaseMap";
  35. #elif ZED_HDRP
  36. public string rendererTextureProperty = "_BaseColorMap";
  37. #endif
  38. private Camera cam;
  39. private RenderTexture copyTexture;
  40. // Use this for initialization
  41. void Start()
  42. {
  43. cam = GetComponent<Camera>();
  44. #if !ZED_URP
  45. copyTexture = new RenderTexture(cam.pixelWidth, cam.pixelHeight, 0);
  46. #else
  47. copyTexture = new RenderTexture(cam.pixelWidth, cam.pixelHeight, 0, UnityEngine.Experimental.Rendering.GraphicsFormat.B10G11R11_UFloatPack32);
  48. #endif
  49. copyTexture.Create();
  50. if (canvasRawImage) canvasRawImage.texture = copyTexture;
  51. if (worldRenderer) worldRenderer.material.SetTexture(rendererTextureProperty, copyTexture);
  52. #if ZED_HDRP || ZED_URP
  53. RenderPipelineManager.beginFrameRendering += SRPStartDraw;
  54. RenderPipelineManager.endFrameRendering += SRPEndDraw;
  55. #endif
  56. }
  57. #if !ZED_HDRP && !ZED_URP
  58. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  59. {
  60. Graphics.Blit(source, copyTexture);
  61. Graphics.Blit(source, destination);
  62. }
  63. #else
  64. private void SRPStartDraw(ScriptableRenderContext context, Camera[] rendcam)
  65. {
  66. cam.targetTexture = copyTexture;
  67. }
  68. private void SRPEndDraw(ScriptableRenderContext context, Camera[] rendcam)
  69. {
  70. cam.targetTexture = null;
  71. Graphics.Blit(copyTexture, (RenderTexture)null);
  72. }
  73. #endif
  74. private void OnApplicationQuit()
  75. {
  76. if (copyTexture) copyTexture.Release();
  77. #if ZED_HDRP || ZED_URP
  78. RenderPipelineManager.beginFrameRendering -= SRPStartDraw;
  79. RenderPipelineManager.endFrameRendering -= SRPEndDraw;
  80. #endif
  81. }
  82. }