-
-
Save elringus/69f0da9c71306f1ea0f575cc7568b31a to your computer and use it in GitHub Desktop.
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.Rendering.LWRP; | |
// Inheriting from `ScriptableRendererFeature` will add it to the | |
// `Renderer Features` list of the custom LWRP renderer data asset. | |
public class RenderMyCustomPass : ScriptableRendererFeature | |
{ | |
private class MyCustomPass : ScriptableRenderPass | |
{ | |
// Just a tag used to pick up a buffer from the pool. | |
private const string commandBufferName = nameof(MyCustomPass); | |
// Corresponds to `Tags { "LightMode" = "MyCustomPass" }` in the shaders. | |
// You have to add this tag for the corresponding shaders to associate them with this pass. | |
private static readonly ShaderTagId shaderTag = new ShaderTagId(nameof(MyCustomPass)); | |
// An arbitrary name to store temporary render texture. | |
private static readonly int tempRTPropertyId = Shader.PropertyToID("_TempRT"); | |
// Name of the grab texture used in the shaders. | |
private static readonly int grabTexturePropertyId = Shader.PropertyToID("_MyGrabTexture"); | |
public MyCustomPass () | |
{ | |
renderPassEvent = RenderPassEvent.AfterRenderingTransparents; | |
} | |
public override void Execute (ScriptableRenderContext context, ref RenderingData renderingData) | |
{ | |
// Grab screen texture and assign it to a global texture property. | |
var cmd = CommandBufferPool.Get(commandBufferName); | |
cmd.GetTemporaryRT(tempRTPropertyId, renderingData.cameraData.cameraTargetDescriptor); | |
cmd.Blit(BuiltinRenderTextureType.RenderTexture, tempRTPropertyId); | |
cmd.SetGlobalTexture(grabTexturePropertyId, tempRTPropertyId); | |
context.ExecuteCommandBuffer(cmd); | |
cmd.Clear(); | |
CommandBufferPool.Release(cmd); | |
// Draw the objects that are using materials associated with this pass. | |
var drawingSettings = CreateDrawingSettings(shaderTag, ref renderingData, SortingCriteria.CommonTransparent); | |
var filteringSettings = new FilteringSettings(RenderQueueRange.transparent); | |
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings); | |
} | |
public override void FrameCleanup (CommandBuffer cmd) | |
{ | |
base.FrameCleanup(cmd); | |
cmd.ReleaseTemporaryRT(tempRTPropertyId); | |
} | |
} | |
private MyCustomPass grabScreenPass; | |
public override void Create () | |
{ | |
grabScreenPass = new MyCustomPass(); | |
} | |
public override void AddRenderPasses (ScriptableRenderer renderer, ref RenderingData renderingData) | |
{ | |
renderer.EnqueuePass(grabScreenPass); | |
} | |
} |
I've used this to develop a special effect and it works perfectly in the game view but fails to render at all in the scene view. Is this normal? Any idea what might be going on?
Other than that issue this is perfect, though, thanks!
I've used this to develop a special effect and it works perfectly in the game view but fails to render at all in the scene view. Is this normal? Any idea what might be going on?
Other than that issue this is perfect, though, thanks!
I remember it wasn't drawing in scene for me as well. Not sure if it's a bug or I'm missing something.
Changing "BuiltinRenderTextureType.CameraTarget" to "BuiltinRenderTextureType.RenderTexture" seems to have fixed it
Changing "BuiltinRenderTextureType.CameraTarget" to "BuiltinRenderTextureType.RenderTexture" seems to have fixed it
Indeed, that works. Thanks for sharing!
has been very helpful.
thanks a lot.
You've saved us from Unity's terrible documentation, thank you!!! <3