SHARE
TWEET

Untitled

a guest Jul 2nd, 2019 279 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.LWRP;
  4. using UnityEngine.Rendering;
  5.  
  6. public class DepthNormals : ScriptableRendererFeature
  7. {
  8.     [System.Serializable]
  9.     public class DepthNormalsSettings
  10.     {
  11.         public RenderPassEvent Event = RenderPassEvent.AfterRenderingPrePasses;
  12.         public LayerMask renderLayerMask;
  13.         public int RenderTextureResolution = 1024;
  14.     }
  15.  
  16.     public DepthNormalsSettings settings = new DepthNormalsSettings();
  17.     private DepthNormalsPass depthNormalsPass;
  18.  
  19.     public override void Create()
  20.     {
  21.         if (depthNormalsPass == null)
  22.             depthNormalsPass = new DepthNormalsPass(settings.Event, RenderQueueRange.opaque, settings.renderLayerMask);
  23.     }
  24.  
  25.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  26.     {
  27.         int width, height;
  28.         if (settings.RenderTextureResolution <= 0)
  29.         {
  30.             width = 16;
  31.             height = 16;
  32.         }
  33.  
  34.         else
  35.         {
  36.             width = settings.RenderTextureResolution;
  37.             height = width;
  38.         }
  39.  
  40.         depthNormalsPass.Setup(new RenderTextureDescriptor(width, height), new RenderTargetHandle());
  41.         renderer.EnqueuePass(depthNormalsPass);
  42.     }
  43.  
  44.     void OnValidate()
  45.     {
  46.         depthNormalsPass?.SetLayerMask(settings.renderLayerMask);
  47.     }
  48. }
  49.  
  50.  
  51. public class DepthNormalsPass : ScriptableRenderPass
  52. {
  53.     int kDepthBufferBits = 32;
  54.  
  55.     private RenderTargetHandle depthAttachmentHandle { get; set; }
  56.     internal RenderTextureDescriptor descriptor { get; private set; }
  57.    
  58.     private FilteringSettings m_FilteringSettings;
  59.     string m_ProfilerTag = "Depth + Normals Prepass";
  60.     ShaderTagId m_ShaderTagId = new ShaderTagId("LightweightForward");
  61.    
  62.     private Material depthNormalsMaterial = null;
  63.    
  64.     public DepthNormalsPass(RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask)
  65.     {
  66.         m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  67.         renderPassEvent = evt;
  68.     }
  69.  
  70.  
  71.     public void Setup(RenderTextureDescriptor baseDescriptor, RenderTargetHandle depthAttachmentHandle)
  72.     {
  73.         this.depthAttachmentHandle = depthAttachmentHandle;
  74.         baseDescriptor.colorFormat = RenderTextureFormat.ARGB32;
  75.         baseDescriptor.depthBufferBits = kDepthBufferBits;
  76.         descriptor = baseDescriptor;
  77.        
  78.         depthNormalsMaterial =
  79.             CoreUtils.CreateEngineMaterial("Hidden/Internal-DepthNormalsTexture");
  80.     }
  81.  
  82.  
  83.     public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  84.     {
  85.         cmd.GetTemporaryRT(depthAttachmentHandle.id, descriptor, FilterMode.Point);
  86.         ConfigureTarget(depthAttachmentHandle.Identifier());
  87.         ConfigureClear(ClearFlag.All, Color.black);
  88.     }
  89.  
  90.  
  91.     public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  92.     {
  93.         CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  94.  
  95.         using (new ProfilingSample(cmd, m_ProfilerTag))
  96.         {
  97.             context.ExecuteCommandBuffer(cmd);
  98.             cmd.Clear();
  99.  
  100.             var sortFlags = renderingData.cameraData.defaultOpaqueSortFlags;
  101.             var drawSettings = CreateDrawingSettings(m_ShaderTagId, ref renderingData, sortFlags);
  102.             drawSettings.perObjectData = PerObjectData.None;
  103.  
  104.  
  105.             ref CameraData cameraData = ref renderingData.cameraData;
  106.             Camera camera = cameraData.camera;
  107.             if (cameraData.isStereoEnabled)
  108.                 context.StartMultiEye(camera);
  109.  
  110.  
  111.             drawSettings.overrideMaterial = depthNormalsMaterial;
  112.  
  113.             context.DrawRenderers(renderingData.cullResults, ref drawSettings,
  114.                 ref m_FilteringSettings);
  115.  
  116.             cmd.SetGlobalTexture("_CameraDepthNormalsTexture", depthAttachmentHandle.id);
  117.         }
  118.  
  119.         context.ExecuteCommandBuffer(cmd);
  120.         CommandBufferPool.Release(cmd);
  121.     }
  122.    
  123.     public void SetLayerMask(LayerMask mask)
  124.     {
  125.         m_FilteringSettings.layerMask = mask;
  126.     }
  127.    
  128.     public override void FrameCleanup(CommandBuffer cmd)
  129.     {
  130.         if (cmd == null)
  131.             throw new ArgumentNullException("cmd");
  132.  
  133.         if (depthAttachmentHandle != RenderTargetHandle.CameraTarget)
  134.         {
  135.             cmd.ReleaseTemporaryRT(depthAttachmentHandle.id);
  136.             depthAttachmentHandle = RenderTargetHandle.CameraTarget;
  137.         }
  138.     }
  139. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top