WeatherDepthCamera.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Funly.SkyStudio {
  5. // Camera renders some depth and normal information from above the main camera.
  6. [RequireComponent(typeof(Camera))]
  7. public class WeatherDepthCamera : MonoBehaviour {
  8. Camera m_DepthCamera;
  9. // Replacement depth shader.
  10. [Tooltip("Shader used to render out depth + normal texture. This should be the sky studio depth shader.")]
  11. public Shader depthShader;
  12. // Texture with an overhead depth and normal render above the active main camera.
  13. [HideInInspector]
  14. public RenderTexture overheadDepthTexture;
  15. [Tooltip("You can help increase performance by only rendering periodically some number of frames.")]
  16. [Range(1, 60)]
  17. public int renderFrameInterval = 5;
  18. [Tooltip("The resolution of the texture. Higher resolution uses more rendering time but makes more precise weather along edges.")]
  19. [Range(128, 8192)]
  20. public int textureResolution = 1024;
  21. void Start() {
  22. m_DepthCamera = GetComponent<Camera>();
  23. // Disable the camera so we can control the rendering interval.
  24. m_DepthCamera.enabled = false;
  25. }
  26. void Update() {
  27. if (m_DepthCamera.enabled) {
  28. m_DepthCamera.enabled = false;
  29. }
  30. if (Time.frameCount % renderFrameInterval != 0) {
  31. return;
  32. }
  33. RenderOverheadCamera();
  34. }
  35. void RenderOverheadCamera()
  36. {
  37. PrepareRenderTexture();
  38. if (depthShader == null) {
  39. Debug.LogError("Can't render depth since depth shader is missing.");
  40. return;
  41. }
  42. RenderTexture previousRT = RenderTexture.active;
  43. RenderTexture.active = overheadDepthTexture;
  44. GL.Clear(true, true, Color.black);
  45. m_DepthCamera.RenderWithShader(depthShader, "RenderType");
  46. RenderTexture.active = previousRT;
  47. Shader.SetGlobalTexture("_OverheadDepthTex", overheadDepthTexture);
  48. Shader.SetGlobalVector("_OverheadDepthPosition", m_DepthCamera.transform.position);
  49. Shader.SetGlobalFloat("_OverheadDepthNearClip", m_DepthCamera.nearClipPlane);
  50. Shader.SetGlobalFloat("_OverheadDepthFarClip", m_DepthCamera.farClipPlane);
  51. }
  52. void PrepareRenderTexture() {
  53. if (overheadDepthTexture == null) {
  54. // Increase rendering performance by lowering the resoluttion of the depth render.
  55. int halfScreenResolution = Mathf.ClosestPowerOfTwo(Mathf.FloorToInt(textureResolution));
  56. // Based on what the platform supports we end up with more or less texture precision for depth.
  57. RenderTextureFormat bestFormat = RenderTextureFormat.ARGB32;
  58. // // Only Uncomment this if you need higher precision, should be fine without it.
  59. // if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) {
  60. // bestFormat = RenderTextureFormat.ARGBFloat;
  61. // } else if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB64)) {
  62. // bestFormat = RenderTextureFormat.ARGB64;
  63. // }
  64. overheadDepthTexture = new RenderTexture(
  65. halfScreenResolution, halfScreenResolution, 24,
  66. bestFormat, RenderTextureReadWrite.Linear);
  67. overheadDepthTexture.useMipMap = false;
  68. overheadDepthTexture.autoGenerateMips = false;
  69. overheadDepthTexture.filterMode = FilterMode.Point;
  70. overheadDepthTexture.antiAliasing = 2;
  71. }
  72. if (overheadDepthTexture.IsCreated() == false) {
  73. overheadDepthTexture.Create();
  74. }
  75. if (m_DepthCamera.targetTexture != overheadDepthTexture) {
  76. m_DepthCamera.targetTexture = overheadDepthTexture;
  77. }
  78. }
  79. }
  80. }