Advertisement
Guest User

Untitled

a guest
Oct 29th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. // See _ReadMe.txt
  7.  
  8. public class DeferredDecalSystem
  9. {
  10. static DeferredDecalSystem m_Instance;
  11. static public DeferredDecalSystem instance {
  12. get {
  13. if (m_Instance == null)
  14. m_Instance = new DeferredDecalSystem();
  15. return m_Instance;
  16. }
  17. }
  18.  
  19. internal HashSet<Decal> m_DecalsDiffuse = new HashSet<Decal>();
  20. internal HashSet<Decal> m_DecalsNormals = new HashSet<Decal>();
  21. internal HashSet<Decal> m_DecalsBoth = new HashSet<Decal>();
  22.  
  23. public void AddDecal (Decal d)
  24. {
  25. RemoveDecal (d);
  26. if (d.m_Kind == Decal.Kind.DiffuseOnly)
  27. m_DecalsDiffuse.Add (d);
  28. if (d.m_Kind == Decal.Kind.NormalsOnly)
  29. m_DecalsNormals.Add (d);
  30. if (d.m_Kind == Decal.Kind.Both)
  31. m_DecalsBoth.Add (d);
  32. }
  33. public void RemoveDecal (Decal d)
  34. {
  35. m_DecalsDiffuse.Remove (d);
  36. m_DecalsNormals.Remove (d);
  37. m_DecalsBoth.Remove (d);
  38. }
  39. }
  40.  
  41. [ExecuteInEditMode]
  42. public class DeferredDecalRenderer : MonoBehaviour
  43. {
  44. public Mesh m_CubeMesh;
  45. private Dictionary<Camera,CommandBuffer> m_Cameras = new Dictionary<Camera,CommandBuffer>();
  46.  
  47. public void OnDisable()
  48. {
  49. foreach (var cam in m_Cameras)
  50. {
  51. if (cam.Key)
  52. {
  53. cam.Key.RemoveCommandBuffer (CameraEvent.BeforeLighting, cam.Value);
  54. }
  55. }
  56. }
  57.  
  58. public void OnWillRenderObject()
  59. {
  60. var act = gameObject.activeInHierarchy && enabled;
  61. if (!act)
  62. {
  63. OnDisable();
  64. return;
  65. }
  66.  
  67. var cam = Camera.current;
  68. if (!cam)
  69. return;
  70.  
  71. CommandBuffer buf = null;
  72. if (m_Cameras.ContainsKey(cam))
  73. {
  74. buf = m_Cameras[cam];
  75. buf.Clear ();
  76. }
  77. else
  78. {
  79. buf = new CommandBuffer();
  80. buf.name = "Deferred decals";
  81. m_Cameras[cam] = buf;
  82.  
  83. // set this command buffer to be executed just before deferred lighting pass
  84. // in the camera
  85. cam.AddCommandBuffer (CameraEvent.BeforeLighting, buf);
  86. }
  87.  
  88. //@TODO: in a real system should cull decals, and possibly only
  89. // recreate the command buffer when something has changed.
  90.  
  91. var system = DeferredDecalSystem.instance;
  92.  
  93. // copy g-buffer normals into a temporary RT
  94. var normalsID = Shader.PropertyToID("_NormalsCopy");
  95. buf.GetTemporaryRT (normalsID, -1, -1);
  96. buf.Blit (BuiltinRenderTextureType.GBuffer2, normalsID);
  97. // render diffuse-only decals into diffuse channel
  98. buf.SetRenderTarget (BuiltinRenderTextureType.GBuffer0, BuiltinRenderTextureType.CameraTarget);
  99. foreach (var decal in system.m_DecalsDiffuse)
  100. {
  101. buf.DrawMesh (m_CubeMesh, decal.transform.localToWorldMatrix, decal.m_Material);
  102. }
  103. // render normals-only decals into normals channel
  104. buf.SetRenderTarget (BuiltinRenderTextureType.GBuffer2, BuiltinRenderTextureType.CameraTarget);
  105. foreach (var decal in system.m_DecalsNormals)
  106. {
  107. buf.DrawMesh (m_CubeMesh, decal.transform.localToWorldMatrix, decal.m_Material);
  108. }
  109. // render diffuse+normals decals into two MRTs
  110. RenderTargetIdentifier[] mrt = {BuiltinRenderTextureType.GBuffer0, BuiltinRenderTextureType.GBuffer2};
  111. buf.SetRenderTarget (mrt, BuiltinRenderTextureType.CameraTarget);
  112. foreach (var decal in system.m_DecalsBoth)
  113. {
  114. buf.DrawMesh (m_CubeMesh, decal.transform.localToWorldMatrix, decal.m_Material);
  115. }
  116. // release temporary normals RT
  117. buf.ReleaseTemporaryRT (normalsID);
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement