-
-
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. |
@daedalic-zz I tried it out in a VR project and it worked for me, but I dont have access to an HMD right now to test it out.
Have you made sure the layer that your objects that use the transparent grab pass texture are disabled under the renderer settings "Filtering" options?
If thats not it, it might be another issue with grabbing the screen in the configuration step. This could conceivable cause issues when it comes to single pass rendering. You can try to move the cmd.Blit(...) call from "Configure" to before the first "context.ExecuteCommandBuffer" call in the "Execute" method. Like this:
...
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(settings.TextureName);
cmd.Blit(colorAttachment, tempColorTarget.Identifier());
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
SortingCriteria sortingCriteria = SortingCriteria.BackToFront;
DrawingSettings drawingSettings = CreateDrawingSettings(shaderTagIdList, ref renderingData, sortingCriteria);
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings, ref renderStateBlock);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
...
Could also be an issue where "colorAttachment" in the Blit call needs to be something else. You could try replacing it with "BuiltinRenderTextureType.CameraTarget" or "BuiltinRenderTextureType.CurrentActive" and see if that works.
Other than that Im not sure, I dont have much experience with VR development. Hopefully some of these ideas might work.
@Refsa Thanks for the ideas. It took a lot of digging and some debugging but I just got it working. I treated the GrabPassTransparent property as a Texture2DArray and then got the unity_StereoEyeIndex value out of a custom function to determine the index. Unlike this method, the scene color node handled both eyes without any extra work. No change was needed to your custom render pass.
Now I just need to zoom in on the texture to the part that overlaps between the eyes. Hopefully that doesn't end up being more zoomed in than the result of the scene color node.
So I have the GrabScreenFeature set to a new layer and then excluded that layer from the renderer's layer masks. While this solution works perfectly in the game view, the objects are not being rendered at all in the scene view. Am I doing something wrong or is it the same for everyone here? Any solutions?
@Jobus0 Yeah, that is an issue. I took a look at it and posted an updated version that works in scene view. This version should also be better in general by splitting grabbing the screen texture and rendering the objects into two passes.
Still some issues with objects in front of the object that uses the texture. Can be partially fixed in the shader, but have to look into a better solution another time.
Hope it works out for you
@Refsa Works perfectly now, thank you!
Works like a charm, thank you for making this public!
Has anyone implemented a UI Blur using this? Any advice? Thanks!
@Haapavuo I've experimented a little bit with it. A problem is that we cant make a custom pass for UI itself. Since we dont have control over the UI rendering we have to sort of work with what we got in terms of camera stacking and canvas stacking.
What kind of UI blur were you looking for? Blurring the scene in the background of the UI or blurring the UI itself?
I did some experimenting and got it somewhat working. I uploaded it to a different gist here. Still some issues to be resolved, but might be a good starting point.
@Refsa Thanks for the ideas. It took a lot of digging and some debugging but I just got it working. I treated the GrabPassTransparent property as a Texture2DArray and then got the unity_StereoEyeIndex value out of a custom function to determine the index. Unlike this method, the scene color node handled both eyes without any extra work. No change was needed to your custom render pass.
Now I just need to zoom in on the texture to the part that overlaps between the eyes. Hopefully that doesn't end up being more zoomed in than the result of the scene color node.
For me, the game view turns gray and the eyes gray and black when using single-pass vr in play mode. This happens regardless of having a mesh/material that's displaying the grab texture. I'm using an oculus quest, and just noticed it only happens when the quest is plugged in. Works fine in multi-pass. Did you have that similar problem, or did it not affect the general rendering of the headset at all?
Has anyone had luck using this with post processing effects? Despite having this run After Rendering
or After Rendering Post Processing
I can't seem to have it capture my post processing effects.
Hi, thank you for this! But I have a problem... It works on meshes, but it doesn't work for VFX in VFX graphs. I tried to make this with the quad output and the mesh output, but none works... when I put the distortion layer on VFX Object in my scene, it just disappear. Any solutions?
@Refsa Thanks for the ideas. It took a lot of digging and some debugging but I just got it working. I treated the GrabPassTransparent property as a Texture2DArray and then got the unity_StereoEyeIndex value out of a custom function to determine the index. Unlike this method, the scene color node handled both eyes without any extra work. No change was needed to your custom render pass.
Now I just need to zoom in on the texture to the part that overlaps between the eyes. Hopefully that doesn't end up being more zoomed in than the result of the scene color node.
Hi @daedalic-zz! I'm strugling with the same problem in Oculus Single Pass Instanced. Can you describe further how do you get 'unity_StereoEyeIndex' value to make it work?
I did this like this:
But still my screen is gray on Oculus Quest.
@gabrieloc post-processing is a bit funky with URP so the render target gets screwed up after it. From my experience the PP stack is changing a bit from version to version, so it can be a struggle to keep up with it. Scene rendering and game rendering also diverges after transparents and gets kind of messy. I posted a new gist of what I had time to come up with, only tested it on URP 11.
@KamilJurczenko A pretty common problem with this sort of effect, it's something both old and new games struggle with. I've seen it in both Skyward Sword and the never Kena Bridge of Spirits, although the latter is a lot better. Usually you can get okay results by discarding based on the distorted depth, but since it's a screen space effect it's hard to eliminate all these artifacts without more advanced techniques.
@Doragonne You need to create a "VFX Shader Graph" shader and enable "Experimental Operators/Blocks" under "Preferences -> Visual Effects". Then you can assign that shader graph in the newly exposed "Shader Graph" field in VFX Graph mesh output. The same shaders should work, you just need to reimplement them. Do note that it is experimental so results may vary.
@Refsa I already work with Experimental Operators/blocks and I already assign the Distortion shader in Shader Graph in Output Particle Quad and I also tried with Output Particle Mesh but it doesn't work :/. If my VFX gets the default layer, it appears but the distortion effect @CoolSteelBreeze. Then I put Distortion Layer on it and it disappear...
@Doragonne What version of URP are you on?
It works for me in VFX Graph on URP 11(2021.1.24f1) with both "Output Particle Quad" and "Output Particle Mesh" when using it with the render feature from the previous post. I have the Visual Effect object set to Distortion layer and it works then as well.
@Refsa Ok it's finally working! It works with your previous post indeed. Thank you for your reactvity
Hi @Refsa,
I have two questions:
- Is this still working or should I be looking for another solution?
- Do I have to create a texture2d myself in my assets folder and assign it to the shader? For now I can't get it to work even with following all the steps and double checking the layers. I want to render a transparent sphere with some distortion from the transparent objects behind it, but the texture just stays white and no distortion to be seen.
@jornvdmaat17 I missed your post, not sure if you found your solution.
As for the first question, it's hard to say depending on the version of URP used. I know that the more updated version here should work on URP 11 depending on which stage of the pipeline it's ran at.
For the second one you dont need to assign any textures as it is created and set to the material in the render feature. Since you get a white output it sounds like you might forgot to uncheck the "exposed" toggle on the texture you added to the graph blackboard. It should not have a green dot next to it.
Hope this helps
For some reason it works only when I set "Intermediate Texture" in the renderer to: Always. It doesn't work with the default value: Auto. Not sure what that option does but doc says "Using this option might have a significant performance impact on some platforms". Any suggestion?
@YvesAlbuquerque this render feature is from before the introduction of that setting. As such it probably wont work properly anymore. If you tell me the version of URP version you are using I can take a look at it later.
As for the performance impact of the option it probably has something to do with memory usage and composition. Rendering to an intermediate texture requires additional video memory and it later has to be composed with the backbuffer to be displayed. If you're targeting platforms with very little video memory, such as older phones and gpus, then this option might impact the performance. It would also depend on how many other render features are active that relies on an intermediate texture as well as the resolution that the game is rendering at.
Thanks for the reply. Indeed, It will be awesome to know a way to avoid this extra memory overhead if possible. I'm using URP 12.1.6 in 2021.3.2f1.
Hello, It was working perfectly but stopped working after I updated the project to Unity 2022.3.5f1
The console endlessly keeps printing:
" You can only call cameraColorTarget inside the scope of a ScriptableRenderPass. Otherwise the pipeline camera target texture might have not been created or might have already been disposed.
UnityEngine.Rendering.Universal.ScriptableRenderer:get_cameraColorTarget ()
GrabScreenFeature:AddRenderPasses (UnityEngine.Rendering.Universal.ScriptableRenderer,UnityEngine.Rendering.Universal.RenderingData&) (at Assets/FireGameStudios/VFX/Post-Processing/GrabScreenFeature.cs:108)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&) "
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
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?
@Refsa I'm just getting a grey GrabPassTransparent texture when I try using this in VR with single pass instanced rendering. Do you know if it's possible to get this to work and how it would be done if so?
Also this is working fine when I'm not using VR and my shader works in VR when using the scene color texture instead. I need the shader to render transparent objects though so it would be great if I could get your grab pass to work.