Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Workaround for URP's 2D Renderer RenderPasses #44

Closed
psuong opened this issue May 6, 2020 · 21 comments
Closed

Workaround for URP's 2D Renderer RenderPasses #44

psuong opened this issue May 6, 2020 · 21 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@psuong
Copy link
Member

psuong commented May 6, 2020

With Unity's 2D URP out in preview, I thought I'd highlight a way to inject scriptable render features into their 2DRenderer. Unity will support it down the line, but if anyone is intending to use this for their 2D needs, you can do the following:

  1. Open the Renderer2D.cs file in your package cache (you might honestly just want a local copy of the package cache since you want the changes to be permanent).
  2. Add the following snippet to the Setup(ScriptableRenderContext ctx, ref RenderingData renderingData) function.

For URP version 10 see the comment below

// URP Version 9 and earlier.
for (int i = 0; i < rendererFeatures.Count; i++) 
{
  if (rendererFeatures[i].isActive)
  {
    rendererFeatures[i].AddRenderPass(this, ref renderingData);
  }
}

This will allow you to initialize all render passes

  1. Click on a ScriptableObject instance for the 2D Renderer and switch the inspector from normal -> debug view by right clicking the inspector tab.

  2. On your FowardRenderer asset, click the drop down menu and drag the Orthographic Render Feature scriptable into the Renderer Features field.

image

Once Unity officially supports custom render passes in the 2D Renderer I'll close this issue, but this should be a decent workaround for the time being. :)

@psuong psuong added bug Something isn't working enhancement New feature or request labels May 6, 2020
@psuong psuong pinned this issue May 6, 2020
@psuong
Copy link
Member Author

psuong commented Aug 5, 2020

Updated the code snippet for 9.0.0-preview.35.

@psuong
Copy link
Member Author

psuong commented Aug 7, 2020

So I'm having some difficulty with 9.0.0-preview.35 since the render pass is likely going to be a mismatched sample. The renderpass will still work, but it's not ideal for it to continuously throw an error every frame due to mismatched sampling. I'll need to look at some updated examples.

Highly recommended to stick with version 9.0.0-preview.14.

@psuong
Copy link
Member Author

psuong commented Aug 8, 2020

@JoanOrtiga
Copy link

Hi, is there a workaround for this these days?
If i try your code snipet it tells me that AddRenderPass doesn't exisists.

@psuong
Copy link
Member Author

psuong commented Jan 8, 2021

Hi, is there a workaround for this these days?
If i try your code snipet it tells me that AddRenderPass doesn't exisists.

Which version of URP are you on? I believe I have to update the snippet if you are on URP 10.

@JoanOrtiga
Copy link

Hi, is there a workaround for this these days?
If i try your code snipet it tells me that AddRenderPass doesn't exisists.

Which version of URP are you on? I believe I have to update the snippet if you are on URP 10.

Yes, i'm at version 10.2

@psuong
Copy link
Member Author

psuong commented Jan 9, 2021

Hi, is there a workaround for this these days?
If i try your code snipet it tells me that AddRenderPass doesn't exisists.

Which version of URP are you on? I believe I have to update the snippet if you are on URP 10.

Yes, i'm at version 10.2

Kk, here's the snippet for version 10:

In Renderer2D.cs in the same function:

Setup(ScriptableRenderContext context, ref RenderingData renderingData) 
{
  ...
  for (int i = 0; i < rendererFeatures.Count; i++) 
  {
    if (rendererFeatures[i].isActive)
    {
      rendererFeatures[i].AddRenderPasses(this, ref renderingData);
    }
  }
}

@kameleon79
Copy link

@psuong
Thanks a lot for this workaround. I was successfully able to add Renderer Feature.
Now I wonder, is there a way to show up the Layer Filtering from the RenderObject pass that was available in the UniversalRenderPipelineAsset_Renderer ?

See attachment ! Thanks a lot !
URP_filtering
2dRendererMissingFilter

@psuong
Copy link
Member Author

psuong commented Jan 20, 2021

@psuong
Thanks a lot for this workaround. I was successfully able to add Renderer Feature.
Now I wonder, is there a way to show up the Layer Filtering from the RenderObject pass that was available in the UniversalRenderPipelineAsset_Renderer ?

See attachment ! Thanks a lot !
URP_filtering
2dRendererMissingFilter

Let me check - I wouldn't know from the top of my head. Have you also checked the Unity forums? One of their engineers might be able to respond with more specific details.

@kameleon79
Copy link

Hi!, Yes I did post the same issue on the Unity forum but Im still waiting for an anwser.

@psuong
Copy link
Member Author

psuong commented Jan 20, 2021

Hi!, Yes I did post the same issue on the Unity forum but Im still waiting for an anwser.

What do you need the layer filtering for regarding UGUIDOTS? I'm curious to see what the use case is.

@kameleon79
Copy link

What do you need the layer filtering for regarding UGUIDOTS? I'm curious to see what the use case is.

Im trying to reproduce this field of view exemple which is working fine with the forward Renderer.
Its a mix of ignoring (filtering) some layer, then adding Renderer Feature to render the maked layer only where there is pixel on a mask layer.

See exemple in here : https://youtu.be/CSeUMTaNFYk?t=1148
Discuss topic is at 19 min in the video.

But I cant do it with the URP 2D Renderer since its missing Layer Filtering and Renderer Feature.
But Thanks to your workaround, cause now its only missing the Layer Filtering.

@psuong
Copy link
Member Author

psuong commented Jan 21, 2021

What do you need the layer filtering for regarding UGUIDOTS? I'm curious to see what the use case is.

Im trying to reproduce this field of view exemple which is working fine with the forward Renderer.
Its a mix of ignoring (filtering) some layer, then adding Renderer Feature to render the maked layer only where there is pixel on a mask layer.

See exemple in here : https://youtu.be/CSeUMTaNFYk?t=1148
Discuss topic is at 19 min in the video.

But I cant do it with the URP 2D Renderer since its missing Layer Filtering and Renderer Feature.
But Thanks to your workaround, cause now its only missing the Layer Filtering.

This looks like a general feature request for URP's 2D workflow more than a feature for UGUIDOTS. As for layering support in UGUIDOTS per canvas, I will need to look at how UGUI handles this feature, before migrating it to a DOTS compatible way. I don't know when I can support this, but feel free to fork this repository to contribute if you would like to work on this feature.

@kameleon79
Copy link

This looks like a general feature request for URP's 2D workflow more than a feature for UGUIDOTS. As for layering support in UGUIDOTS per canvas, I will need to look at how UGUI handles this feature, before migrating it to a DOTS compatible way. I don't know when I can support this, but feel free to fork this repository to contribute if you would like to work on this feature.

Its not my field of expertise, so I hope that Unity will come with an update of the URP 2D renderer.
Thanks anyway

@JoanOrtiga
Copy link

One unity developer told me that renderer features for 2d renderer was already in development, so I expect it to be available in ~2months

@kameleon79
Copy link

One unity developer told me that renderer features for 2d renderer was already in development, so I expect it to be available in ~2months

Amazing ! Thanks for your reply !
Ill focus on others feature in my game while the 2d renderer update is release.
Thanks !

@DiegoAvena
Copy link

Ok so first of all thanks for this! This workaround works perfectly for me, but the only issue I am having now, and I think many here will have or did have, is that the changes I make to the 2DRenderer script will be reset when I reopen the project because all of the Unity packages get imported again or something along those lines.

I did a lot of looking around for some solutions, and it seems like there are 3 main solutions:

1.) Copy the package onto your machine, and then import from disk inside of the package manager. The main issue with this though, is that if I am working in a team and I need my team members to pull the project, they will not be able to get the 2DRenderer script with my changes because it is only stored locally on my machine, so this is a no go for me.

2.) Create a github repo with the package in it, give all of my team members access to that repo, and then import the package via a git URL in the package manager. I have not tried this, but I am scared this one might not work for my needs either because the versioning system we are using is Unity Collab, not github.

3.) This one is the most attractive one to me: creating my own rendering script that basically contains the same exact code as 2DRenderer, but it is called something like Custom2DRenderer, and this script contains the code snippet mentioned here. Like 2DRenderer, it extends from ScriptableRenderer, but since it will be in my Assets folder, then the changes I make to it should not get erased and all of my team members should still get this updated script as a result. This is the current method I have been trying, writing up this script for it: https://github.com/DiegoAvena/Custom2DRenderer/blob/master/Custom2DRenderer.cs

Unfortunately, I have so far been unable to make my own version of 2DRenderer with these required changes due to errors like:

Line10 Render2DLightingPass** is inaccessible due to its protection level

Line 26 Renderer2DData does not contain a definition for postProcessData and no accessible extension method postProcessData accepting a first argument of type Renderer2DData could be found (are you missing a using directive or assembly reference?)

And a ton more errors.

These errors confuse me though because again, all of the code in this class is basically a copy and paste from 2DRenderer, which does not get these errors.

Has anyone here been able to write their own version of Renderer2D?

@psuong
Copy link
Member Author

psuong commented Jan 30, 2021

Ok so first of all thanks for this! This workaround works perfectly for me, but the only issue I am having now, and I think many here will have or did have, is that the changes I make to the 2DRenderer script will be reset when I reopen the project because all of the Unity packages get imported again or something along those lines.

I did a lot of looking around for some solutions, and it seems like there are 3 main solutions:

1.) Copy the package onto your machine, and then import from disk inside of the package manager. The main issue with this though, is that if I am working in a team and I need my team members to pull the project, they will not be able to get the 2DRenderer script with my changes because it is only stored locally on my machine, so this is a no go for me.

2.) Create a github repo with the package in it, give all of my team members access to that repo, and then import the package via a git URL in the package manager. I have not tried this, but I am scared this one might not work for my needs either because the versioning system we are using is Unity Collab, not github.

3.) This one is the most attractive one to me: creating my own rendering script that basically contains the same exact code as 2DRenderer, but it is called something like Custom2DRenderer, and this script contains the code snippet mentioned here. Like 2DRenderer, it extends from ScriptableRenderer, but since it will be in my Assets folder, then the changes I make to it should not get erased and all of my team members should still get this updated script as a result. This is the current method I have been trying, writing up this script for it: https://github.com/DiegoAvena/Custom2DRenderer/blob/master/Custom2DRenderer.cs

Unfortunately, I have so far been unable to make my own version of 2DRenderer with these required changes due to errors like:

Line10 Render2DLightingPass** is inaccessible due to its protection level

Line 26 Renderer2DData does not contain a definition for postProcessData and no accessible extension method postProcessData accepting a first argument of type Renderer2DData could be found (are you missing a using directive or assembly reference?)

And a ton more errors.

These errors confuse me though because again, all of the code in this class is basically a copy and paste from 2DRenderer, which does not get these errors.

Has anyone here been able to write their own version of Renderer2D?

This looks like a general issue, please keep this relevant to UGUIDOTS. What is it that you are trying to do with the 2DRenderer that allows you to utilize UGUIDOTS?

@DiegoAvena
Copy link

Ok so first of all thanks for this! This workaround works perfectly for me, but the only issue I am having now, and I think many here will have or did have, is that the changes I make to the 2DRenderer script will be reset when I reopen the project because all of the Unity packages get imported again or something along those lines.
I did a lot of looking around for some solutions, and it seems like there are 3 main solutions:
1.) Copy the package onto your machine, and then import from disk inside of the package manager. The main issue with this though, is that if I am working in a team and I need my team members to pull the project, they will not be able to get the 2DRenderer script with my changes because it is only stored locally on my machine, so this is a no go for me.
2.) Create a github repo with the package in it, give all of my team members access to that repo, and then import the package via a git URL in the package manager. I have not tried this, but I am scared this one might not work for my needs either because the versioning system we are using is Unity Collab, not github.
3.) This one is the most attractive one to me: creating my own rendering script that basically contains the same exact code as 2DRenderer, but it is called something like Custom2DRenderer, and this script contains the code snippet mentioned here. Like 2DRenderer, it extends from ScriptableRenderer, but since it will be in my Assets folder, then the changes I make to it should not get erased and all of my team members should still get this updated script as a result. This is the current method I have been trying, writing up this script for it: https://github.com/DiegoAvena/Custom2DRenderer/blob/master/Custom2DRenderer.cs
Unfortunately, I have so far been unable to make my own version of 2DRenderer with these required changes due to errors like:
Line10 Render2DLightingPass** is inaccessible due to its protection level
Line 26 Renderer2DData does not contain a definition for postProcessData and no accessible extension method postProcessData accepting a first argument of type Renderer2DData could be found (are you missing a using directive or assembly reference?)
And a ton more errors.
These errors confuse me though because again, all of the code in this class is basically a copy and paste from 2DRenderer, which does not get these errors.
Has anyone here been able to write their own version of Renderer2D?

This looks like a general issue, please keep this relevant to UGUIDOTS. What is it that you are trying to do with the 2DRenderer that allows you to utilize UGUIDOTS?

Sorry,

I came across this after looking for solutions to allowing for custom post processing effects to be used in conjunction with URP while using the Renderer2D, and this method did work out for me except that my changes kept getting overriden since it required me to edit a Unity package script. I have since figured out another way of doing custom post processing effects with URP and the Renderer2D, so disregard this (feel free to delete these comments as they might be irrelevant now that you point it out...).

@longtran2904
Copy link

For some reason, Unity keeps telling me 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.. I'm using Unity 2020.3.12f1 with URP 10.5.1

@psuong
Copy link
Member Author

psuong commented Jan 3, 2022

For some reason, Unity keeps telling me 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.. I'm using Unity 2020.3.12f1 with URP 10.5.1

No idea, the Orthographic Render Pass in UGUIDOTS doesn't manipulate cameraColorTargets at all. It just plays back a command buffer to draw a mesh.

I also have not been updating and maintaining UGUIDOTS in well over a year to explore different UI solutions that fit Unity DOTS. That said, URP 12+ supports Scriptable Render Passes in 2D, so I'm closing this issue.

@psuong psuong closed this as completed Jan 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants