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.

901 lines
32 KiB

5 years ago
  1. using UnityEngine;
  2. using UnityEngine.Profiling;
  3. using FogVolumeUtilities;
  4. using UnityEngine.XR;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. [ExecuteInEditMode]
  9. public class FogVolumeRenderer : MonoBehaviour
  10. {
  11. bool ShowCamerasBack = true;
  12. public bool ShowCamera;
  13. //public bool DestroyOnDisable;
  14. private int m_screenWidth = 0;
  15. private int m_screenHeight = 0;
  16. public string FogVolumeResolution;
  17. // public bool RenderableInSceneView = true;
  18. public enum BlendMode
  19. {
  20. PremultipliedTransparency = (int)UnityEngine.Rendering.BlendMode.One,
  21. TraditionalTransparency = (int)UnityEngine.Rendering.BlendMode.SrcAlpha,
  22. };
  23. RenderTextureFormat rt_DepthFormat;
  24. public BlendMode _BlendMode = BlendMode.PremultipliedTransparency;
  25. public bool GenerateDepth = true;
  26. RenderTexture RT_FogVolume, RT_FogVolumeR;
  27. [SerializeField]
  28. [Range(0, 8)]
  29. public int _Downsample = 1;
  30. public void setDownsample(int val) { _Downsample = val; }
  31. //public bool useRectangularStereoRT = false;
  32. public bool _showBilateralEdge = false;
  33. public bool showBilateralEdge
  34. {
  35. set
  36. {
  37. if (value != _showBilateralEdge)
  38. ShowBilateralEdge(value);
  39. }
  40. get
  41. {
  42. return _showBilateralEdge;
  43. }
  44. }
  45. public void ShowBilateralEdge(bool b)
  46. {
  47. _showBilateralEdge = b;
  48. if (bilateralMaterial)
  49. {
  50. if (showBilateralEdge)
  51. bilateralMaterial.EnableKeyword("VISUALIZE_EDGE");
  52. else
  53. bilateralMaterial.DisableKeyword("VISUALIZE_EDGE");
  54. }
  55. }
  56. [SerializeField]
  57. //public FogVolumeCamera.UpsampleMode USMode = FogVolumeCamera.UpsampleMode.DOWNSAMPLE_CHESSBOARD;
  58. [System.Serializable]
  59. public enum UpsampleMode
  60. {
  61. DOWNSAMPLE_MIN,
  62. DOWNSAMPLE_MAX,
  63. DOWNSAMPLE_CHESSBOARD
  64. };
  65. public enum UpsampleMaterialPass
  66. {
  67. DEPTH_DOWNSAMPLE = 0,
  68. BILATERAL_UPSAMPLE = 1
  69. };
  70. Material bilateralMaterial;
  71. public bool _useBilateralUpsampling = true;
  72. public bool useBilateralUpsampling
  73. {
  74. get { return _useBilateralUpsampling; }
  75. set
  76. {
  77. if (_useBilateralUpsampling != value)
  78. SetUseBilateralUpsampling(value);
  79. }
  80. }
  81. void SetUseBilateralUpsampling(bool b)
  82. {
  83. _useBilateralUpsampling = b /*&& BilateralUpsamplingEnabled()*/;//works on dx9
  84. if (_useBilateralUpsampling)
  85. {
  86. if (bilateralMaterial == null)
  87. {
  88. bilateralMaterial = new Material(Shader.Find("Hidden/Upsample"));
  89. if (bilateralMaterial == null)
  90. Debug.Log("#ERROR# Hidden/Upsample");
  91. // refresh keywords
  92. UpdateBilateralDownsampleModeSwitch();
  93. ShowBilateralEdge(_showBilateralEdge);
  94. }
  95. }
  96. else
  97. {
  98. // release resources
  99. bilateralMaterial = null;
  100. }
  101. }
  102. // [SerializeField]
  103. public UpsampleMode _upsampleMode = UpsampleMode.DOWNSAMPLE_MAX;
  104. public UpsampleMode upsampleMode
  105. {
  106. set
  107. {
  108. if (value != _upsampleMode)
  109. SetUpsampleMode(value);
  110. }
  111. get
  112. {
  113. return _upsampleMode;
  114. }
  115. }
  116. void UpdateBilateralDownsampleModeSwitch()
  117. {
  118. if (bilateralMaterial != null)
  119. {
  120. switch (_upsampleMode)
  121. {
  122. case UpsampleMode.DOWNSAMPLE_MIN:
  123. bilateralMaterial.EnableKeyword("DOWNSAMPLE_DEPTH_MODE_MIN");
  124. bilateralMaterial.DisableKeyword("DOWNSAMPLE_DEPTH_MODE_MAX");
  125. bilateralMaterial.DisableKeyword("DOWNSAMPLE_DEPTH_MODE_CHESSBOARD");
  126. break;
  127. case UpsampleMode.DOWNSAMPLE_MAX:
  128. bilateralMaterial.DisableKeyword("DOWNSAMPLE_DEPTH_MODE_MIN");
  129. bilateralMaterial.EnableKeyword("DOWNSAMPLE_DEPTH_MODE_MAX");
  130. bilateralMaterial.DisableKeyword("DOWNSAMPLE_DEPTH_MODE_CHESSBOARD");
  131. break;
  132. case UpsampleMode.DOWNSAMPLE_CHESSBOARD:
  133. bilateralMaterial.DisableKeyword("DOWNSAMPLE_DEPTH_MODE_MIN");
  134. bilateralMaterial.DisableKeyword("DOWNSAMPLE_DEPTH_MODE_MAX");
  135. bilateralMaterial.EnableKeyword("DOWNSAMPLE_DEPTH_MODE_CHESSBOARD");
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. }
  142. void SetUpsampleMode(UpsampleMode value)
  143. {
  144. _upsampleMode = value;
  145. UpdateBilateralDownsampleModeSwitch();
  146. }
  147. Camera ThisCamera = null;
  148. RenderTexture RT_Depth, RT_DepthR;
  149. Shader depthShader = null;
  150. [HideInInspector]
  151. Camera _FogVolumeCamera;
  152. // [SerializeField]
  153. GameObject _FogVolumeCameraGO;
  154. [SerializeField]
  155. [Range(0, .01f)]
  156. public float upsampleDepthThreshold = 0.00187f;
  157. public bool HDR;
  158. public bool TAA = false;
  159. public FogVolumeTAA _TAA = null;
  160. private FogVolumePlaydeadTAA.VelocityBuffer _TAAvelocity = null;
  161. private FogVolumePlaydeadTAA.FrustumJitter _TAAjitter = null;
  162. //[HideInInspector]
  163. //public LayerMask DepthLayer = -1;
  164. [SerializeField]
  165. // [HideInInspector]
  166. //string _DepthLayersName = "Water";
  167. public int DepthLayer2 = 0;
  168. //public string DepthLayersName
  169. //{
  170. // get { return _DepthLayersName; }
  171. // set
  172. // {
  173. // if (_DepthLayersName != value)
  174. // SetDepthLayer(value);
  175. // }
  176. //}
  177. //void SetDepthLayer(string NewDepthLayersName)
  178. //{
  179. // _DepthLayersName = NewDepthLayersName;
  180. // DepthLayer = ThisCamera.cullingMask;
  181. // DepthLayer &= ~(1 << LayerMask.NameToLayer(_DepthLayersName));
  182. // //DepthLayer = LayerMask.NameToLayer(_DepthLayersName);
  183. //}
  184. //void OnValidate()
  185. //{
  186. // SetDepthLayer(_DepthLayersName);
  187. //}
  188. public RenderTextureReadWrite GetRTReadWrite()
  189. {
  190. //return RenderTextureReadWrite.Default;
  191. return (ThisCamera.allowHDR) ? RenderTextureReadWrite.Default : RenderTextureReadWrite.Linear;
  192. }
  193. public RenderTextureFormat GetRTFormat()
  194. {
  195. return (ThisCamera.allowHDR == true) ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;
  196. }
  197. protected void GetRT(ref RenderTexture rt, int2 size, string name)
  198. {
  199. // Release existing one
  200. ReleaseRT(rt);
  201. rt = RenderTexture.GetTemporary(size.x, size.y, 0, GetRTFormat(), GetRTReadWrite());
  202. rt.filterMode = FilterMode.Bilinear;
  203. rt.name = name;
  204. rt.wrapMode = TextureWrapMode.Clamp;
  205. }
  206. public void ReleaseRT(RenderTexture rt)
  207. {
  208. if (rt != null)
  209. {
  210. RenderTexture.ReleaseTemporary(rt);
  211. rt = null;
  212. }
  213. }
  214. protected void Get_RT_Depth(ref RenderTexture rt, int2 size, string name)
  215. {
  216. // Release existing one
  217. ReleaseRT(rt);
  218. rt = RenderTexture.GetTemporary(size.x, size.y, 16, rt_DepthFormat);
  219. rt.filterMode = FilterMode.Bilinear;
  220. rt.name = name;
  221. rt.wrapMode = TextureWrapMode.Clamp;
  222. }
  223. void RenderDepth()
  224. {
  225. if (GenerateDepth && _FogVolumeCamera)
  226. {
  227. if (_TAAjitter)
  228. {
  229. //_TAA.enabled = false;
  230. _TAAjitter.patternScale = 0;
  231. }
  232. Profiler.BeginSample("FogVolume Depth");
  233. //Gimme scene depth
  234. // ThisCamera.cullingMask = _FogVolumeCamera.cullingMask;//Render the same than scene camera
  235. //3.2.1p2
  236. _FogVolumeCamera.cullingMask = DepthLayer2;
  237. #region Stereo
  238. if (ThisCamera.stereoEnabled)
  239. {
  240. Shader.EnableKeyword("FOG_VOLUME_STEREO_ON");
  241. //Left eye
  242. if (ThisCamera.stereoTargetEye == StereoTargetEyeMask.Both || ThisCamera.stereoTargetEye == StereoTargetEyeMask.Left)
  243. {
  244. _FogVolumeCamera.worldToCameraMatrix = ThisCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Left);
  245. _FogVolumeCamera.projectionMatrix = ThisCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left);
  246. Get_RT_Depth(ref RT_Depth, new int2(m_screenWidth, m_screenHeight), "RT_DepthLeft");
  247. _FogVolumeCamera.targetTexture = RT_Depth;
  248. _FogVolumeCamera.RenderWithShader(depthShader, "RenderType");
  249. }
  250. //Right eye
  251. if (ThisCamera.stereoTargetEye == StereoTargetEyeMask.Both || ThisCamera.stereoTargetEye == StereoTargetEyeMask.Right)
  252. {
  253. _FogVolumeCamera.worldToCameraMatrix = ThisCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Right);
  254. _FogVolumeCamera.projectionMatrix = ThisCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right);
  255. Get_RT_Depth(ref RT_DepthR, new int2(m_screenWidth, m_screenHeight), "RT_DepthRight");
  256. _FogVolumeCamera.targetTexture = RT_DepthR;
  257. _FogVolumeCamera.RenderWithShader(depthShader, "RenderType");
  258. Shader.SetGlobalTexture("RT_DepthR", RT_DepthR);
  259. }
  260. }
  261. #endregion
  262. //MONO
  263. else
  264. {
  265. Shader.DisableKeyword("FOG_VOLUME_STEREO_ON");
  266. Get_RT_Depth(ref RT_Depth, new int2(m_screenWidth, m_screenHeight), "RT_Depth");
  267. //pepe
  268. _FogVolumeCamera.projectionMatrix = ThisCamera.projectionMatrix;
  269. _FogVolumeCamera.targetTexture = RT_Depth;
  270. _FogVolumeCamera.RenderWithShader(depthShader, "RenderType");
  271. }
  272. Shader.SetGlobalTexture("RT_Depth", RT_Depth);
  273. Profiler.EndSample();
  274. }
  275. }
  276. public RenderTexture[] lowProfileDepthRT;
  277. void ReleaseLowProfileDepthRT()
  278. {
  279. if (lowProfileDepthRT != null)
  280. {
  281. for (int i = 0; i < lowProfileDepthRT.Length; i++)
  282. RenderTexture.ReleaseTemporary(lowProfileDepthRT[i]);
  283. lowProfileDepthRT = null;
  284. }
  285. }
  286. public RenderTexture[] lowProfileDepthRRT;
  287. void ReleaseLowProfileDepthRRT()
  288. {
  289. if (lowProfileDepthRRT != null)
  290. {
  291. for (int i = 0; i < lowProfileDepthRRT.Length; i++)
  292. RenderTexture.ReleaseTemporary(lowProfileDepthRRT[i]);
  293. lowProfileDepthRRT = null;
  294. }
  295. }
  296. void RenderColor()
  297. {
  298. if (_TAA && _TAAjitter)
  299. {
  300. _TAA.enabled = TAA;
  301. _TAAvelocity.enabled = false;
  302. _TAAjitter.enabled = TAA;
  303. _TAAjitter.patternScale = 0.2f;
  304. }
  305. //Textured Fog
  306. _FogVolumeCamera.cullingMask = 1 << LayerMask.NameToLayer("FogVolume");//show Fog volume
  307. _FogVolumeCamera.cullingMask |= 1 << LayerMask.NameToLayer("FogVolumeShadowCaster");//show FogVolumeShadowCaster
  308. int2 resolution = new int2(m_screenWidth / _Downsample, m_screenHeight / _Downsample);
  309. FogVolumeResolution = resolution.x + " X " + resolution.y;
  310. if (ThisCamera.stereoEnabled)
  311. {
  312. Profiler.BeginSample("FogVolume Render Stereo");
  313. Shader.EnableKeyword("FOG_VOLUME_STEREO_ON");
  314. //Left eye
  315. if (ThisCamera.stereoTargetEye == StereoTargetEyeMask.Both || ThisCamera.stereoTargetEye == StereoTargetEyeMask.Left)
  316. {
  317. _FogVolumeCamera.projectionMatrix = ThisCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left);
  318. _FogVolumeCamera.worldToCameraMatrix = ThisCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Left);
  319. GetRT(ref RT_FogVolume, resolution, "RT_FogVolumeLeft");
  320. _FogVolumeCamera.targetTexture = RT_FogVolume;
  321. _FogVolumeCamera.Render();
  322. }
  323. //Right eye
  324. if (ThisCamera.stereoTargetEye == StereoTargetEyeMask.Both || ThisCamera.stereoTargetEye == StereoTargetEyeMask.Right)
  325. {
  326. _FogVolumeCamera.projectionMatrix = ThisCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right);
  327. _FogVolumeCamera.worldToCameraMatrix = ThisCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Right);
  328. GetRT(ref RT_FogVolumeR, resolution, "RT_FogVolumeRight");
  329. _FogVolumeCamera.targetTexture = RT_FogVolumeR;
  330. _FogVolumeCamera.Render();
  331. }
  332. Profiler.EndSample();
  333. }
  334. else
  335. {
  336. Profiler.BeginSample("FogVolume Render");
  337. Shader.DisableKeyword("FOG_VOLUME_STEREO_ON");
  338. _FogVolumeCamera.projectionMatrix = ThisCamera.projectionMatrix;
  339. GetRT(ref RT_FogVolume, resolution, "RT_FogVolume");
  340. _FogVolumeCamera.targetTexture = RT_FogVolume;
  341. _FogVolumeCamera.Render();
  342. Profiler.EndSample();
  343. }
  344. if (TAA)
  345. _TAA.TAA(ref RT_FogVolume);
  346. if (ThisCamera.stereoEnabled)
  347. {
  348. if (TAA)
  349. _TAA.TAA(ref RT_FogVolumeR);
  350. }
  351. if (useBilateralUpsampling && GenerateDepth)
  352. {
  353. //MONO
  354. #region BILATERAL_DEPTH_DOWNSAMPLE
  355. Profiler.BeginSample("FogVolume Upsample");
  356. // Compute downsampled depth-buffer for bilateral upsampling
  357. if (bilateralMaterial)
  358. {
  359. bilateralMaterial.SetInt("RightSide", 0);
  360. ReleaseLowProfileDepthRT();
  361. lowProfileDepthRT = new RenderTexture[_Downsample];
  362. for (int downsampleStep = 0; downsampleStep < _Downsample; downsampleStep++)
  363. {
  364. int targetWidth = m_screenWidth / (downsampleStep + 1);
  365. int targetHeight = m_screenHeight / (downsampleStep + 1);
  366. int stepWidth = m_screenWidth / Mathf.Max(downsampleStep, 1);
  367. int stepHeight = m_screenHeight / Mathf.Max(downsampleStep, 1);
  368. Vector4 texelSize = new Vector4(1.0f / stepWidth, 1.0f / stepHeight, 0.0f, 0.0f);
  369. bilateralMaterial.SetFloat("_UpsampleDepthThreshold", upsampleDepthThreshold);
  370. bilateralMaterial.SetVector("_TexelSize", texelSize);
  371. bilateralMaterial.SetTexture("_HiResDepthBuffer", RT_Depth);
  372. lowProfileDepthRT[downsampleStep] =
  373. RenderTexture.GetTemporary(targetWidth, targetHeight, 0, rt_DepthFormat, GetRTReadWrite());
  374. lowProfileDepthRT[downsampleStep].name = "lowProfileDepthRT_" + downsampleStep;
  375. Graphics.Blit(null, lowProfileDepthRT[downsampleStep], bilateralMaterial, (int)UpsampleMaterialPass.DEPTH_DOWNSAMPLE);
  376. }
  377. Shader.SetGlobalTexture("RT_Depth", lowProfileDepthRT[lowProfileDepthRT.Length - 1]);
  378. }
  379. #endregion
  380. #region BILATERAL_UPSAMPLE
  381. // Upsample convolution RT
  382. if (bilateralMaterial)
  383. {
  384. for (int downsampleStep = _Downsample - 1; downsampleStep >= 0; downsampleStep--)
  385. {
  386. int targetWidth = m_screenWidth / Mathf.Max(downsampleStep, 1);
  387. int targetHeight = m_screenHeight / Mathf.Max(downsampleStep, 1);
  388. // compute Low-res texel size
  389. int stepWidth = m_screenWidth / (downsampleStep + 1);
  390. int stepHeight = m_screenHeight / (downsampleStep + 1);
  391. Vector4 texelSize = new Vector4(1.0f / stepWidth, 1.0f / stepHeight, 0.0f, 0.0f);
  392. bilateralMaterial.SetVector("_TexelSize", texelSize);
  393. bilateralMaterial.SetVector("_InvdUV", new Vector4(RT_FogVolume.width, RT_FogVolume.height, 0, 0));
  394. // High-res depth texture
  395. bilateralMaterial.SetTexture("_HiResDepthBuffer", RT_Depth);
  396. bilateralMaterial.SetTexture("_LowResDepthBuffer", lowProfileDepthRT[downsampleStep]);
  397. bilateralMaterial.SetTexture("_LowResColor", RT_FogVolume);
  398. RenderTexture newRT = RenderTexture.GetTemporary(targetWidth, targetHeight, 0, GetRTFormat(), GetRTReadWrite());
  399. newRT.filterMode = FilterMode.Bilinear;
  400. Graphics.Blit(null, newRT, bilateralMaterial, (int)UpsampleMaterialPass.BILATERAL_UPSAMPLE);
  401. // Swap and release
  402. RenderTexture swapRT = RT_FogVolume;
  403. RT_FogVolume = newRT;
  404. RenderTexture.ReleaseTemporary(swapRT);
  405. }
  406. }
  407. ReleaseLowProfileDepthRT();
  408. #endregion
  409. //Stereo right side
  410. if (ThisCamera.stereoEnabled)
  411. {
  412. #region BILATERAL_DEPTH_DOWNSAMPLE
  413. Profiler.BeginSample("FogVolume Upsample Right");
  414. // Compute downsampled depth-buffer for bilateral upsampling
  415. if (bilateralMaterial)
  416. {
  417. bilateralMaterial.EnableKeyword("FOG_VOLUME_STEREO_ON");
  418. bilateralMaterial.SetInt("RightSide", 1);
  419. ReleaseLowProfileDepthRRT();
  420. lowProfileDepthRRT = new RenderTexture[_Downsample];
  421. for (int downsampleStep = 0; downsampleStep < _Downsample; downsampleStep++)
  422. {
  423. int targetWidth = m_screenWidth / (downsampleStep + 1);
  424. int targetHeight = m_screenHeight / (downsampleStep + 1);
  425. int stepWidth = m_screenWidth / Mathf.Max(downsampleStep, 1);
  426. int stepHeight = m_screenHeight / Mathf.Max(downsampleStep, 1);
  427. Vector4 texelSize = new Vector4(1.0f / stepWidth, 1.0f / stepHeight, 0.0f, 0.0f);
  428. bilateralMaterial.SetFloat("_UpsampleDepthThreshold", upsampleDepthThreshold);
  429. bilateralMaterial.SetVector("_TexelSize", texelSize);
  430. bilateralMaterial.SetTexture("_HiResDepthBufferR", RT_DepthR);
  431. lowProfileDepthRRT[downsampleStep] =
  432. RenderTexture.GetTemporary(targetWidth, targetHeight, 0, rt_DepthFormat, GetRTReadWrite());
  433. lowProfileDepthRRT[downsampleStep].name = "lowProfileDepthRRT_" + downsampleStep;
  434. Graphics.Blit(null, lowProfileDepthRRT[downsampleStep], bilateralMaterial, (int)UpsampleMaterialPass.DEPTH_DOWNSAMPLE);
  435. }
  436. Shader.SetGlobalTexture("RT_DepthR", lowProfileDepthRRT[lowProfileDepthRRT.Length - 1]);
  437. }
  438. #endregion
  439. #region BILATERAL_UPSAMPLE
  440. // Upsample convolution RT
  441. if (bilateralMaterial)
  442. {
  443. for (int downsampleStep = _Downsample - 1; downsampleStep >= 0; downsampleStep--)
  444. {
  445. int targetWidth = m_screenWidth / Mathf.Max(downsampleStep, 1);
  446. int targetHeight = m_screenHeight / Mathf.Max(downsampleStep, 1);
  447. // compute Low-res texel size
  448. int stepWidth = m_screenWidth / (downsampleStep + 1);
  449. int stepHeight = m_screenHeight / (downsampleStep + 1);
  450. Vector4 texelSize = new Vector4(1.0f / stepWidth, 1.0f / stepHeight, 0.0f, 0.0f);
  451. bilateralMaterial.SetVector("_TexelSize", texelSize);
  452. bilateralMaterial.SetVector("_InvdUV", new Vector4(RT_FogVolumeR.width, RT_FogVolumeR.height, 0, 0));
  453. // High-res depth texture
  454. bilateralMaterial.SetTexture("_HiResDepthBufferR", RT_DepthR);
  455. bilateralMaterial.SetTexture("_LowResDepthBufferR", lowProfileDepthRRT[downsampleStep]);
  456. bilateralMaterial.SetTexture("_LowResColorR", RT_FogVolumeR);
  457. RenderTexture newRT = RenderTexture.GetTemporary(targetWidth, targetHeight, 0, GetRTFormat(), GetRTReadWrite());
  458. newRT.filterMode = FilterMode.Bilinear;
  459. Graphics.Blit(null, newRT, bilateralMaterial, (int)UpsampleMaterialPass.BILATERAL_UPSAMPLE);
  460. // Swap and release
  461. RenderTexture swapRT = RT_FogVolumeR;
  462. RT_FogVolumeR = newRT;
  463. RenderTexture.ReleaseTemporary(swapRT);
  464. }
  465. }
  466. ReleaseLowProfileDepthRRT();
  467. #endregion
  468. }
  469. else
  470. bilateralMaterial.DisableKeyword("FOG_VOLUME_STEREO_ON");
  471. Profiler.EndSample();
  472. }
  473. if (ThisCamera.stereoEnabled)
  474. Shader.SetGlobalTexture("RT_FogVolumeR", RT_FogVolumeR);
  475. Shader.SetGlobalTexture("RT_FogVolume", RT_FogVolume);
  476. }
  477. void CameraUpdateSharedProperties()
  478. {
  479. //AspectRatio = (float)m_screenWidth / m_screenHeight;
  480. if (_FogVolumeCamera)
  481. {
  482. _FogVolumeCamera.farClipPlane = ThisCamera.farClipPlane;
  483. _FogVolumeCamera.nearClipPlane = ThisCamera.nearClipPlane;
  484. _FogVolumeCamera.allowHDR = ThisCamera.allowHDR;
  485. if (_FogVolumeCamera.stereoEnabled == false)
  486. _FogVolumeCamera.fieldOfView = ThisCamera.fieldOfView;
  487. }
  488. }
  489. void TAASetup()
  490. {
  491. if (_Downsample > 1 && TAA)
  492. {
  493. if (_FogVolumeCameraGO.GetComponent<FogVolumeTAA>() == null)
  494. _FogVolumeCameraGO.AddComponent<FogVolumeTAA>();
  495. _TAA = _FogVolumeCameraGO.GetComponent<FogVolumeTAA>();
  496. _TAAvelocity = _FogVolumeCameraGO.GetComponent<FogVolumePlaydeadTAA.VelocityBuffer>();
  497. _TAAjitter = _FogVolumeCameraGO.GetComponent<FogVolumePlaydeadTAA.FrustumJitter>();
  498. }
  499. }
  500. void CreateFogCamera()
  501. {
  502. //if (_Downsample > 1)
  503. {
  504. _FogVolumeCameraGO = new GameObject();
  505. _FogVolumeCameraGO.transform.parent = gameObject.transform;
  506. _FogVolumeCameraGO.transform.localEulerAngles = Vector3.zero;
  507. _FogVolumeCameraGO.transform.localPosition = Vector3.zero;
  508. _FogVolumeCameraGO.name = "FogVolumeCamera";
  509. //_FogVolumeCamera = _FogVolumeCameraGO.AddComponent<FogVolumeCamera>();
  510. _FogVolumeCamera = _FogVolumeCameraGO.AddComponent<Camera>();
  511. _FogVolumeCamera.depth = -666;
  512. _FogVolumeCamera.clearFlags = CameraClearFlags.SolidColor;
  513. _FogVolumeCamera.backgroundColor = new Color(0, 0, 0, 0);
  514. // _FogVolumeCameraGO.hideFlags = HideFlags.HideInHierarchy;
  515. _FogVolumeCamera.enabled = false;
  516. _FogVolumeCamera.renderingPath = RenderingPath.Forward;
  517. _FogVolumeCamera.allowMSAA = false;
  518. #if UNITY_EDITOR
  519. EditorExtension.ToggleInHierarchy(_FogVolumeCameraGO, ShowCamera);
  520. #endif
  521. }
  522. }
  523. void FindFogCamera()
  524. {
  525. _FogVolumeCameraGO = GameObject.Find("FogVolumeCamera");
  526. if (_FogVolumeCameraGO)
  527. {
  528. _FogVolumeCamera = _FogVolumeCameraGO.GetComponent<Camera>();
  529. //_FogVolumeCameraGO.transform.parent = gameObject.transform;
  530. }
  531. //if (_FogVolumeCameraGO)
  532. // DestroyImmediate(_FogVolumeCameraGO);//the RT is not created in VR on start. Resetting here for now
  533. if (_FogVolumeCameraGO == null)
  534. CreateFogCamera();
  535. //_FV_Baker = _FogVolumeCameraGO.GetComponent<FogVolumeCamera>();
  536. TAASetup();
  537. }
  538. Vector4 TexelSize = Vector4.zero;
  539. void TexelUpdate()
  540. {
  541. //if (_FogVolumeCamera.RT_FogVolume)
  542. {
  543. TexelSize.x = 1.0f / ThisCamera.pixelWidth;
  544. TexelSize.y = 1.0f / ThisCamera.pixelHeight;
  545. TexelSize.z = ThisCamera.pixelWidth;
  546. TexelSize.w = ThisCamera.pixelHeight;
  547. Shader.SetGlobalVector("RT_FogVolume_TexelSize", TexelSize);
  548. }
  549. // print(TexelSize);
  550. }
  551. void OnEnable()
  552. {
  553. SetUseBilateralUpsampling(_useBilateralUpsampling);
  554. SetUpsampleMode(_upsampleMode);
  555. ShowBilateralEdge(_showBilateralEdge);
  556. ThisCamera = gameObject.GetComponent<Camera>();
  557. depthShader = Shader.Find("Hidden/Fog Volume/Depth");
  558. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RFloat))
  559. rt_DepthFormat = RenderTextureFormat.RFloat;
  560. else
  561. rt_DepthFormat = RenderTextureFormat.DefaultHDR;
  562. if (depthShader == null) print("Hidden/Fog Volume/Depth #SHADER ERROR#");
  563. FindFogCamera();
  564. //clean old scenes . In 3.4 things are different. Find the existing camera and remove the unused scripts:
  565. Component[] components = _FogVolumeCameraGO.GetComponents<Component>();
  566. if (_FogVolumeCamera.GetComponent("FogVolumeCamera"))
  567. {
  568. print("Destroyed Old Camera");
  569. SafeDestroy(_FogVolumeCameraGO);
  570. CreateFogCamera();
  571. }
  572. for (int i = 0; i < components.Length; i++)
  573. {
  574. if (components[i] == null)
  575. {
  576. print("Destroyed Old Camera");
  577. SafeDestroy(_FogVolumeCameraGO);
  578. CreateFogCamera();
  579. break;
  580. }
  581. }
  582. //3.2.2 clean meshes. Some users pretends to add FogVolume.cs here ¬¬
  583. if (GetComponent<FogVolume>())
  584. {
  585. print("Don't add FogVolume here. Create a new one using the menu buttons and follow the instructions");
  586. DestroyImmediate(GetComponent<FogVolume>());
  587. }
  588. if (ThisCamera.GetComponent<MeshFilter>())
  589. DestroyImmediate(ThisCamera.GetComponent<MeshFilter>());
  590. if (ThisCamera.GetComponent<MeshRenderer>())
  591. DestroyImmediate(ThisCamera.GetComponent<MeshRenderer>());
  592. SurrogateMaterial = (Material)Resources.Load("Fog Volume Surrogate");
  593. //UpdateParams();
  594. if (DepthLayer2 == 0)
  595. DepthLayer2 = 1;
  596. //DepthLayer2 = ThisCamera.cullingMask;
  597. }
  598. void UpdateParams()
  599. {
  600. //_FV_Baker._Downsample = _Downsample;
  601. // if (_FogVolumeCamera && _Downsample > 1)
  602. {
  603. // _FV_Baker.useBilateralUpsampling = BilateralUpsampling;
  604. if (useBilateralUpsampling && GenerateDepth)
  605. {
  606. // _FV_Baker.upsampleMode = USMode;
  607. // _FV_Baker.showBilateralEdge = ShowBilateralEdge;
  608. // _FV_Baker.upsampleDepthThreshold = upsampleDepthThreshold;
  609. }
  610. if (GenerateDepth)
  611. {
  612. SurrogateMaterial.SetInt("_ztest", (int)UnityEngine.Rendering.CompareFunction.Always);
  613. //_FogVolumeCamera.DepthMask = instance.DepthLayer;
  614. // _FogVolumeCamera.DepthMask = ThisCamera.cullingMask;
  615. //_FogVolumeCamera.DepthMask &= ~(1 << DepthLayer2);
  616. DepthLayer2 &= ~(1 << LayerMask.NameToLayer("FogVolume"));//hide FogVolume
  617. DepthLayer2 &= ~(1 << LayerMask.NameToLayer("FogVolumeShadowCaster"));//hide FogVolumeShadowCaster
  618. DepthLayer2 &= ~(1 << LayerMask.NameToLayer("FogVolumeSurrogate"));//hide FogVolumeSurrogate
  619. DepthLayer2 &= ~(1 << LayerMask.NameToLayer("FogVolumeUniform"));//hide FogVolumeUniform
  620. DepthLayer2 &= ~(1 << LayerMask.NameToLayer("UI"));//hide UI
  621. //_FV_Baker.DepthMask = DepthLayer2;
  622. }
  623. else
  624. SurrogateMaterial.SetInt("_ztest", (int)UnityEngine.Rendering.CompareFunction.LessEqual);
  625. if (!_TAA)
  626. TAASetup();
  627. #if UNITY_5_6_OR_NEWER
  628. HDR = ThisCamera.allowHDR;
  629. #else
  630. HDR = ThisCamera.hdr;
  631. #endif
  632. }
  633. }
  634. Material SurrogateMaterial;
  635. public bool SceneBlur = true;
  636. void OnPreRender()
  637. {
  638. m_screenWidth = ThisCamera.pixelWidth;
  639. m_screenHeight = ThisCamera.pixelHeight;
  640. #if UNITY_EDITOR
  641. if (ThisCamera == null)
  642. ThisCamera = gameObject.GetComponent<Camera>();
  643. #endif
  644. //#if UNITY_EDITOR
  645. // // if destroyed...
  646. // FindFogCamera();
  647. //#endif
  648. if (_FogVolumeCamera == null
  649. && _Downsample > 1)//3.2.1
  650. FindFogCamera();
  651. if (_Downsample == 1) SafeDestroy(_FogVolumeCameraGO);
  652. CameraUpdateSharedProperties();
  653. if (_Downsample > 0 && _FogVolumeCameraGO && this.isActiveAndEnabled)
  654. {
  655. ThisCamera.cullingMask &= ~(1 << LayerMask.NameToLayer("FogVolume"));//hide FogVolume
  656. ThisCamera.cullingMask &= ~(1 << LayerMask.NameToLayer("FogVolumeShadowCaster"));//hide FogVolumeShadowCaster
  657. FogVolumeResolution = m_screenWidth + " X " + m_screenHeight;
  658. ThisCamera.cullingMask |= 1 << LayerMask.NameToLayer("FogVolumeSurrogate");//show surrogate
  659. }
  660. else
  661. {
  662. ThisCamera.cullingMask &= ~(1 << LayerMask.NameToLayer("FogVolumeSurrogate"));//hide surrogate
  663. ThisCamera.cullingMask |= 1 << LayerMask.NameToLayer("FogVolume");//show FogVolume
  664. ThisCamera.cullingMask |= 1 << LayerMask.NameToLayer("FogVolumeShadowCaster");//show FogVolumeShadowCaster
  665. }
  666. int InitialpixelLights = QualitySettings.pixelLightCount;
  667. ShadowQuality InitialShadows = QualitySettings.shadows;
  668. QualitySettings.pixelLightCount = 0;
  669. QualitySettings.shadows = ShadowQuality.Disable;
  670. #if UNITY_EDITOR
  671. if (ThisCamera == null)
  672. ThisCamera = gameObject.GetComponent<Camera>();
  673. #endif
  674. UpdateParams();
  675. SurrogateMaterial.SetInt("_SrcBlend", (int)_BlendMode);
  676. if (_Downsample > 1 && _FogVolumeCamera)
  677. {
  678. //SurrogateMaterial.SetInt("_SrcBlend", (int)_BlendMode);
  679. Shader.EnableKeyword("_FOG_LOWRES_RENDERER");
  680. Profiler.BeginSample("FogVolume Render");
  681. RenderDepth();
  682. RenderColor();
  683. Profiler.EndSample();
  684. // TexelUpdate();
  685. Shader.DisableKeyword("_FOG_LOWRES_RENDERER");
  686. }
  687. else
  688. {
  689. Shader.DisableKeyword("_FOG_LOWRES_RENDERER");
  690. }
  691. QualitySettings.pixelLightCount = InitialpixelLights;
  692. QualitySettings.shadows = InitialShadows;
  693. }
  694. void Update()
  695. {
  696. #if UNITY_EDITOR
  697. if (ShowCamerasBack != ShowCamera)
  698. {
  699. EditorExtension.ToggleInHierarchy(_FogVolumeCameraGO, ShowCamera);
  700. ShowCamerasBack = ShowCamera;
  701. }
  702. #endif
  703. }
  704. void SafeDestroy(Object obj)
  705. {
  706. if (obj != null)
  707. {
  708. //print("Destroyed " + obj);
  709. DestroyImmediate(obj);
  710. }
  711. obj = null;
  712. }
  713. void OnDisable()
  714. {
  715. // Shader.DisableKeyword("RENDER_SCENE_VIEW");
  716. Shader.DisableKeyword("_FOG_LOWRES_RENDERER");
  717. ThisCamera.cullingMask |= (1 << LayerMask.NameToLayer("FogVolume"));
  718. ThisCamera.cullingMask |= 1 << LayerMask.NameToLayer("FogVolumeShadowCaster");
  719. ThisCamera.cullingMask &= ~(1 << LayerMask.NameToLayer("FogVolumeSurrogate"));//hide surrogate
  720. SafeDestroy(_TAA);
  721. SafeDestroy(_TAAvelocity);
  722. SafeDestroy(_TAAjitter);
  723. SafeDestroy(_FogVolumeCameraGO);
  724. SafeDestroy(RT_FogVolume);
  725. SafeDestroy(RT_FogVolumeR);
  726. SafeDestroy(RT_Depth);
  727. SafeDestroy(RT_DepthR);
  728. //if (ThisCamera.depthTextureMode == DepthTextureMode.None)
  729. // Debug.LogWarning("............ATTENTION, this camera is not generating the required Depth for Fog Volume. " +
  730. // "Add -EnableDepthInForwardCamera.cs- to this camera");
  731. }
  732. }