-
-
Save Refsa/54da34a9e2fc8e45472286572216ad17 to your computer and use it in GitHub Desktop.
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.Rendering.Universal; | |
public class GrabScreenFeature : ScriptableRendererFeature | |
{ | |
[System.Serializable] | |
public class Settings | |
{ | |
public string TextureName = "_GrabPassTransparent"; | |
public LayerMask LayerMask; | |
} | |
class GrabPass : ScriptableRenderPass | |
{ | |
RenderTargetHandle tempColorTarget; | |
Settings settings; | |
RenderTargetIdentifier cameraTarget; | |
public GrabPass(Settings s) | |
{ | |
settings = s; | |
renderPassEvent = RenderPassEvent.AfterRenderingTransparents; | |
tempColorTarget.Init(settings.TextureName); | |
} | |
public void Setup(RenderTargetIdentifier cameraTarget) | |
{ | |
this.cameraTarget = cameraTarget; | |
} | |
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) | |
{ | |
cmd.GetTemporaryRT(tempColorTarget.id, cameraTextureDescriptor); | |
cmd.SetGlobalTexture(settings.TextureName, tempColorTarget.Identifier()); | |
} | |
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) | |
{ | |
CommandBuffer cmd = CommandBufferPool.Get(); | |
context.ExecuteCommandBuffer(cmd); | |
cmd.Clear(); | |
Blit(cmd, cameraTarget, tempColorTarget.Identifier()); | |
context.ExecuteCommandBuffer(cmd); | |
CommandBufferPool.Release(cmd); | |
} | |
public override void FrameCleanup(CommandBuffer cmd) | |
{ | |
cmd.ReleaseTemporaryRT(tempColorTarget.id); | |
} | |
} | |
class RenderPass : ScriptableRenderPass | |
{ | |
Settings settings; | |
List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>(); | |
FilteringSettings m_FilteringSettings; | |
RenderStateBlock m_RenderStateBlock; | |
public RenderPass(Settings settings) | |
{ | |
this.settings = settings; | |
renderPassEvent = RenderPassEvent.AfterRenderingTransparents + 1; | |
m_ShaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit")); | |
m_ShaderTagIdList.Add(new ShaderTagId("UniversalForward")); | |
m_ShaderTagIdList.Add(new ShaderTagId("LightweightForward")); | |
m_FilteringSettings = new FilteringSettings(RenderQueueRange.all, settings.LayerMask); | |
m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing); | |
} | |
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) | |
{ | |
CommandBuffer cmd = CommandBufferPool.Get(); | |
context.ExecuteCommandBuffer(cmd); | |
cmd.Clear(); | |
DrawingSettings drawSettings; | |
drawSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, SortingCriteria.CommonTransparent); | |
context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref m_FilteringSettings, ref m_RenderStateBlock); | |
context.ExecuteCommandBuffer(cmd); | |
CommandBufferPool.Release(cmd); | |
} | |
} | |
GrabPass grabPass; | |
RenderPass renderPass; | |
[SerializeField] Settings settings; | |
public override void Create() | |
{ | |
grabPass = new GrabPass(settings); | |
renderPass = new RenderPass(settings); | |
} | |
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) | |
{ | |
grabPass.Setup(renderer.cameraColorTarget); | |
renderer.EnqueuePass(grabPass); | |
renderer.EnqueuePass(renderPass); | |
} | |
} |
MIT License | |
Copyright (c) 2020 Refsa | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
If anyone had a problem in the newer versions of Unity try changing this :
public void Setup(RenderTargetIdentifier cameraTarget) { this.cameraTarget = cameraTarget; }
to this :public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) { cameraTarget = renderingData.cameraData.renderer.cameraColorTarget; }
it worked for me
Thanks, that didn't do It for me for some reason though
Was anyone able to fix this? I also ran into this issue ^
Was anyone able to fix this? I also ran into this issue ^
Found this related updated API code. It seems it changed the Camera Setup to be only allowed on a new function scope (SetupRenderPasses).
https://docs.unity3d.com/Packages/[email protected]/manual/upgrade-guide-2022-1.html
Changed FROM:
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
grabPass.Setup(renderer.cameraColorTarget);
renderer.EnqueuePass(grabPass);
renderer.EnqueuePass(renderPass);
}
TO:
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(grabPass);
renderer.EnqueuePass(renderPass);
}
public override void SetupRenderPasses(ScriptableRenderer renderer,
in RenderingData renderingData)
{
// The target is used after allocation
grabPass.Setup(renderer.cameraColorTarget);
}
Would it work with Unity 6?
If anyone had a problem in the newer versions of Unity try changing this :
public void Setup(RenderTargetIdentifier cameraTarget) { this.cameraTarget = cameraTarget; }
to this :
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) { cameraTarget = renderingData.cameraData.renderer.cameraColorTarget; }
it worked for me