Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Official 2D Renderer in Universal Render Pipeline in 2019.3

Discussion in '2D Experimental Preview' started by rustum, Feb 17, 2020.

Thread Status:
Not open for further replies.
  1. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    Some of those do apply, but the lighting one's are not applicable to the 2d lights
     
  2. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    Currently you cannot modify the lighting model in your Shader Graph, if that's what you're asking. All built-in shader variables related to lighting are not valid in the 2D Renderer.
     
    ratuspro likes this.
  3. luoweihong

    luoweihong

    Joined:
    May 27, 2019
    Posts:
    56
    how can I get that effect in LWRP 2d Render? all point light Pre-generated on a mask map in LWRP,

    dose it have some way to get the point light infomation in shader ? the target sortinglayer in the inspector in my game is not a good way to use..because there are many gameobject will be dynamic spawn...

     
    GilbertoBitt likes this.
  4. elZach

    elZach

    Joined:
    Apr 23, 2017
    Posts:
    48
    Could you please consider having public Getter/Setter for m_shapePath in Light2D and ShadowCaster2D.
    There's no reason anyone should have to place path-points by hand.

    This would also alleviate the missing TilemapShadowCaster as you could just write a script which generates fitting ShadowCasters from the CompositeCollider.


    I wrote a naive lightcaster, which I found quite beautiful, but isn't working at all in builds because of the sealed nature of those shape-paths.
     
    Last edited: Apr 14, 2020
    hzetta likes this.
  5. Jonathan-Westfall-8Bits

    Jonathan-Westfall-8Bits

    Joined:
    Sep 17, 2013
    Posts:
    271
    You wouldn't mind explaining your light caster script for the rest of us. That seems like it would be handy also love to hear how you did it.
     
  6. elZach

    elZach

    Joined:
    Apr 23, 2017
    Posts:
    48
    Sure can. I use the Light2D in freeform mode and write to it's shapePath. While the shapePath array isn't settable, the members of the array are. I then cast a bunch of rays from the characters head in all direction and feed them into the members of the shapePath.

    It's a naive one-day Implementation that I'm sure can be optimized a lot, but even like this it runs <1ms - so for a few lights absolutely ok.

    The trick is to spam click the shape of the freeform light a bunch of times to have a larger initial shapepath-array.

    As of now this does not work in builds but soley in the editor.



    Code (CSharp):
    1. public class DynamicLight : MonoBehaviour
    2. {
    3.     public bool showDebug = false;
    4.     Light2D light2D;
    5.     public LayerMask mask;
    6.     public Vector2 offset = Vector2.up;
    7.     Vector2[] worldPos = new Vector2[60];
    8.  
    9.     public float radius = 20f;
    10.     public int resolution = 24;
    11.     public float lerpSpeed = 30f;
    12.     public AnimationCurve distanceCurve = new AnimationCurve(new Keyframe(0f, 1f), new Keyframe(1f, 0.2f));
    13.     float angle;
    14.  
    15.     private void OnEnable()
    16.     {
    17.         light2D = GetComponent<Light2D>();
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         UpdateLightShape();
    23.     }
    24.  
    25.     void UpdateLightShape()
    26.     {
    27.         resolution = light2D.shapePath.Length;
    28.         if (resolution == 0) Debug.Log("NaNalarm!");
    29.         angle = 360f / (float)resolution;
    30.         RaycastHit2D hit = new RaycastHit2D();
    31.         Vector2 position = new Vector2(transform.position.x,transform.position.y);
    32.         for (int i = 0; i < resolution; i++)
    33.         {        
    34.             Vector2 dir = Quaternion.Euler(0f, 0f, (float)i * angle) * Vector2.up;
    35.             hit = Physics2D.Raycast(position + offset, dir,radius,mask);
    36.             if (hit)
    37.             {
    38.                 float lerpFactor = lerpSpeed * distanceCurve.Evaluate(hit.distance / radius);
    39.                 if (hit.distance == 0f) { continue; }
    40.                 worldPos[i] = Vector2.Lerp(worldPos[i], hit.point, Time.deltaTime * lerpFactor);
    41.             }
    42.             else
    43.             {
    44.                 worldPos[i] = Vector2.Lerp(worldPos[i], position + offset + dir * radius, Time.deltaTime * distanceCurve.GetLastKey().value * lerpSpeed);
    45.             }
    46.             Vector2 localPoint = worldPos[i]- position;
    47.             if (i == 0 || Vector2.Distance(light2D.shapePath[i - 1], localPoint) > 0.01f)
    48.                 light2D.shapePath[i] = localPoint;
    49.         }
    50.  
    51.         if(showDebug)
    52.             for(int i = 0; i < resolution-1; i++)
    53.             {
    54.                 if (Vector3.Distance(transform.TransformPoint(light2D.shapePath[i]), transform.TransformPoint(light2D.shapePath[i + 1])) <= 0.01f) Debug.LogWarning("Lightdistance: " + Vector3.Distance(transform.TransformPoint(light2D.shapePath[i]), transform.TransformPoint(light2D.shapePath[i + 1])));
    55.                 Debug.DrawLine(worldPos[i],worldPos[i+1]);
    56.             }
    57.     }
    58. }
    The lerpSpeed and animationcurve lead to the somewhat wobbly and round nature of the "shadow", something which wouldn't be reproducible by a correct implementation of a shadowsystem.
     
    Last edited: Apr 15, 2020
    gbkim likes this.
  7. Jonathan-Westfall-8Bits

    Jonathan-Westfall-8Bits

    Joined:
    Sep 17, 2013
    Posts:
    271
    Thank you very much this was a great read and I am sure it will help a lot of people.
     
    elZach likes this.
  8. ratuspro

    ratuspro

    Joined:
    Apr 5, 2015
    Posts:
    17
    Thanks guys! I'll try to create a new custom node to calculate the light direction (a compromised version) to create a cel shader.
     
  9. Driven

    Driven

    Joined:
    May 31, 2013
    Posts:
    77
    When a sprite lit by a 2D light reaches a high world position and continues moving, it flickers and offsets itself. The bigger the x pos the harder the flicker, no flicker in low coordinates.
    How can i prevent this behavior without resetting the positions to 0?
     
  10. cyanryanlion

    cyanryanlion

    Joined:
    Feb 17, 2020
    Posts:
    25

    Hey, I ran into this issue as well while I was tracking down some GC Allocs in my game. This alloc happens because the "Hidden/Universal Render Pipeline/FallbackError" isn't included in the built player, so trying to create the "error material" with it fails and keeps trying to do it again every frame. I added this shader to my Always Included Shaders list in Graphics settings to work around this, but there's probably a better solution such as including the shaders in the urp resources or something.

    The second GC Alloc I've removed locally is one in Render2DLightingPass.cs::Execute. It was creating a bool array every frame for checking if blend styles had been initialised. I've replaced this bool array locally with an int that I'm using as a byte mask (as there can only be a max of 4 blend styles anyway) to check if the blend styles are initialised instead.

    I'm on Unity 2020.1.0b5. Universal RP preview.14 9.0.0.
     
  11. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    This sounds like a bug. Can we get a sample project.
     
  12. Driven

    Driven

    Joined:
    May 31, 2013
    Posts:
    77
    @Chris_Chu i made a repro project, the case number is 1239313
    It happens on Android with GLES 2 - check out the scene "Exploration"
    i hope you can give me some feedback and a possible workaround :)
     
  13. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    Thanks. I'll keep an eye out for your bug report.
     
  14. Driven

    Driven

    Joined:
    May 31, 2013
    Posts:
    77
    It happens on ios with metal as well i forgot to mention.
     
  15. TotallyCoded

    TotallyCoded

    Joined:
    Dec 1, 2017
    Posts:
    3
    Is there any way to use stencil in shaders with 2d render?
     
  16. elZach

    elZach

    Joined:
    Apr 23, 2017
    Posts:
    48
    if its 2D you should be able to put any ol' image on screen anyways, right?

    Sounds like you maybe want to use stencil to cut holes in images? You could look into masks for that. Or if you want holes with shading just render an image with a normal map and a higher rendering order.
     
    Last edited: Apr 22, 2020
  17. cyanryanlion

    cyanryanlion

    Joined:
    Feb 17, 2020
    Posts:
    25
    Hey,

    Are there any plans to allow control over the _NormalMap Render Texture creation/resolution? This _NormalMap render texture seems huge for mobile memory where they have high resolution displays (e.g. 7.4MB on 1080x1920). Currently it looks like there's no exposed way to disable it or reduce the resolution of it as it's not scaled by blend style scale like the other RTs.

    Unity 2020.1.0b5
    URP preview 14 9.0.0
     
    Last edited: Apr 22, 2020
  18. Gungnira

    Gungnira

    Joined:
    Mar 2, 2019
    Posts:
    9
  19. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    I think this would make sense, and it would have some performance benefits as well. I'll add it to our backlog.
     
  20. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    Hm. Right now we don't have a good way to have users customize the light shaders. I'll add something to our backlog for this but can't say when you might see it.

    You can make modifications yourself to the 4 light shaders in Universal/Shaders/2D in the Universal RP package. I don't think those shaders will be changing in the near future, so you can either update them when you get a new package or fork our repo and make your changes.
     
    berkza likes this.
  21. luoweihong

    luoweihong

    Joined:
    May 27, 2019
    Posts:
    56

    so it means i can't do the custom lighting in 2d lwrp right now? like i want the point light data and the main Light Data to do the complex lighting
     
  22. Driven

    Driven

    Joined:
    May 31, 2013
    Posts:
    77
    Any news regarding the light offsetting in high coordinates? The game looks really like crap atm :/.
     
  23. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Dont want to sounds like a cry baby, but it feels like current status of 2drenderer needs some clearer roadmap with some dates to go with it. We cant just really rely on "cant promise" and "not sure when" to plan our development.

    Biggest issue for me seems like:
    1. No official shadow support for tilemap.
    2. Light layer needs to be reworked, as it redraws lights for every possible layers it is affecting it. This is huge cost for the mobile device.
    3. No emission parts for the lit shader.
    4. This is general in Urp, but post processing such as bloom is costly. Need to be able to adjust its performance configuration more agressively.
     
  24. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    157
    So much this. I've been asking lots of roadmap questions in separate forum threads. I started to think that 2D Renderer is a backlog entry, not a priority.
    I've been watching SRP repository and it seems commits are toward to optimization and bugfixes lately. It's something but I think it needs more love in a more publicly watchable way.
     
    rboerdijk likes this.
  25. rboerdijk

    rboerdijk

    Joined:
    Aug 4, 2018
    Posts:
    96
    Linking it with https://forum.unity.com/threads/wil...upport-renderer-features.747755/#post-5797888 - where I asked for a roadmap too - gives:
    5. Renderer Feature support (10 lines of code for initial support, and probably bugs which will need to be addressed after that ).
    6. Camera Stacking support, so we can have nicely blurred backgrounds using postprocessing ( changing 1 bool ).

    ... and there were some requests in here (_CameraOpaqueTexture issue):
    https://forum.unity.com/threads/exp...ph-support-in-lwrp.683623/page-9#post-5794663

    7. _CameraOpaqueTexture issue

    I understand the renderer is experimental, and also that not all features are going to be done 'now' and from the replies in various posts it's clear the focus is not on 2D but completely elsewhere. A roadmap would help answering how serious development of the 2d renderer is, what features are coming (or not) and when (6 months? 1 year? 2 years?) - and as a result if workarounds for issues need to be found on the developer-side.
     
    Last edited: May 5, 2020
  26. SpudGums

    SpudGums

    Joined:
    Mar 10, 2015
    Posts:
    7
    I simulate fake height in my 2D game by leaving the pivot points of the sprites at very bottom of their bounds and sorting their orders based on the y axis of their positions. A very common practice.
    I'd love to see this working with the new 2D shadows. I will leave here some of the things I found and leave some suggestions, even though I have no clue on how to implement them.

    The shadows could inherit the sorting order of the sprite or the sorting group. This way they wouldn't be rendered on top of sprites that should be in front of the shadow caster, like it happens with the character's head here:
    1.png

    For developers who choose to use this fake height, it would be amazing to have an option make lights stop influencing sprites when the light is behind it (higher y axis light position). In a scene with no global lights or any other 2d light, a sprite would behave like this, where I manually tinted the lower wall black:
    2.png

    Of course, it would be nice if the bottom wall was still lit and not pitch black if there were other light sources illuminating it, like a very dark grey tint if a developer was trying to simulate nightfall by reducing the global light's intensity. Only shaped lights behind the wall would stop influencing its sprite color.
    There is this article on Gamasutra where the developers of Graveyard Keeper say they achieved this by using a shader that reduces the light intensity on the sprite based on the light's y position.
    Heres the article:
    https://www.gamasutra.com/blogs/Svy..._Keeper_How_the_graphics_effects_are_made.php


    I don't know if this last thing is asking too much, but here it goes:
    Those walls in my example have shadow casters at their bottoms, with half a unit of height, which is the thickness I wanted the walls to have. You can see here that only the feet of the character are in the shadow because of the position and shape of the shadow casters:
    3.png

    It'd be lovely if, combined with the second example, where the wall is no longer lit by a light that is behind it, sprites positioned in front of the wall also stop being illuminated, as the wall would work as a barrier in between the light and sprites in front of it.

    Other stuff I would like to see:
    - An option to configure shadows max distance.
    - Soft edges for shadows.
    - Global light shadow casting and direction.
    - Automatic shadow caster shapes, where the light would use the sprite's alpha cutout mode to define the shadow shape.

    As I said in the beginning of this, I have no clue on how to implement those things, and for all I know everything I said could range from very simple to incredibly complicated.

    I just think it would be absolutely incredible if those things could be implemented.
    Simulating a 3D space with all those details in a true 2D world like that... I get goosebumps thinking of the things I'd be able to do... :D
     
    NotaNaN and MrPaparoz like this.
  27. javisarias

    javisarias

    Joined:
    Jan 21, 2017
    Posts:
    9
    This please! The Depth of Field Post Processing either blurs everything or nothing :(
     
    lil_sichen, gferrari, EvOne and 3 others like this.
  28. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    If you do a frame capture of the 2drenderer you will sort of see how 2drenderer does its lighting. And it will not be possible to do what you are asking. To do similar lighting as grave yard keepr you would be better off by just using legacy or normal urp. But then ofcourse this does not do the shadow casting for you. I personally think the current technique for generating light buffer per layer used is flawed. It will cause too many drawcalls and too many light buffers.. not very good for light weight solution.

    In any case is there any road map we can check to see the where 2drenderer go from here? What can we expect in short term and long term?
     
    elZach likes this.
  29. Jamez0r

    Jamez0r

    Joined:
    Jul 29, 2019
    Posts:
    206
    Hey @castor76 , could you explain how you figured out a bit of what is going on behind the scenes with the lighting by using the frame capture? Did you just do a Frame Debug and then step through it and sort of figure out what its doing? I'm trying to do some profiling/optimization for my game and feel like I have no information to work off of (I made a post here: https://forum.unity.com/threads/try...nderer-2dlights-could-use-some-advice.895640/).

    You're saying that each Layer (the sorting layers) generates its own lighting buffer? We use around 30 layers in our game - I assumed that having additional Layers would be 'free'. This is the sort of information I could really really use right now.

    Argh. I feel like I have a million questions but I don't want to hijack this thread. If you have time would you mind responding on my post (the link above) or messaging me directly? I'd super appreciate it
     
  30. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    We don't have anything public relating to a roadmap at the moment. In the short term we will be working on performance enhancements.
     
  31. MasoInar

    MasoInar

    Joined:
    Feb 20, 2014
    Posts:
    126
    Any news on camera stacking?
     
  32. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    I feel like 2D depth of field should be heavily considered ! It's been years since devs want to implement that in their games, with the 2D renderer I assumed this would be a top3 feature !
     
  33. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    There is some work in progress that is primarily for performance but will allow for this in the future. It's definitely something I'd like us to support as well since it will really enhance some games.
     
    lil_sichen, zfh2773 and RemDust like this.
  34. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Awesome to read !
    It would be such a game changer for us indies to be able to create assets only once and not with multiple iterations of blurs ! I guess you would just bake the DOF, not calculate it dynamically ?
     
  35. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    can we please have emission channel for the Default Sprite Lit Shader Input...
     
  36. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    After trying out 2D Light shadow, ... I was bitterly confused at how it is supposed to be useful especially with use self shadow...

    upload_2020-6-12_16-51-23.png

    I mean, this is not usable in my opinion. Self shadow is not respecting the alpha of the actual sprite accurately enough... and there is no way to set length of the shadow. Also no directional light means it is hard to do sun light simulation.
     
    NotaNaN likes this.
  37. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Also, it is very common to use transform's scale to flip the game object's facing direction in 2D game, but it seems if I do that the lighting thinks the light is behind or something. I think we should be able to have option to ignore the scale, or have some way to still have the light working with negative scale...

    -- Edit --

    I have worked around this by flipping normal's green channel inside Shader if it is not a front face.

    Not sure if this is the best way, but this is as far as I could get it working without too much changes in the actual game code side.
     
    Last edited: Jun 12, 2020
  38. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    It has been month since last time I posted here about emission channel for the 2drenderer.. I think this is trivial feature. It is certainly more important than having a .. normalmap channel for instance. While normal map usage maybe "sometimes" but emission channel is almost always used in any 2d game.
     
  39. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I get a warning like :

    "'
    There is a mismatch in secondary textures of sprites wasting texture space and potentially break draw call batching.
    "

    But both primary and secondary texture sizes are the same. Only difference is that one is Sprite and other one is set as Normal map.

    I am not sure how they are a mismatched? What does it mean exactly? I am using Unity 2020.2a12
     
  40. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    This please!

    I had to write this to set that field:
    https://forum.unity.com/threads/scr...owcaster2ds-for-tilemaps.906767/#post-5947943
     
  41. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    I also have an issue related to 2D shadows, I have added a picture.
    1. The shadow of the character runs through the tilemap, instead of stopping on the first wall.
    2. The same shadow affects the tilemap. I have solved this by setting up the sorting layers and duplicating the lights so they affect different sorting layers.
     
  42. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    This is probably caused by shadow not being supported for the tilemap. I have been asking about this for a very long time, but nothing practical has came from Unity so far.

    Also your "solution" practically decrease rendering performance by double because you had to duplicate the lights.
     
  43. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    I managed to kind of solve it by configuring just the Target Sorting Layers in the ShadowCaster2Ds components, with an extra light.
     
  44. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    What is depth buffer precision bits for the 2DRenderer? There is no option to check this and no way to find out what it is.

    I am asking because depth buffer precision seems a lot different in game camera than a scene camera. (At displaying scene with 2D option) I have scene rendering nicely but on game view, depth buffer precision seems to be way lower. I can only guess their differences to 2 things :

    1. Scene camera's 2D mode is not actually using orthographic camera.
    2. Scne camera's near and far clip plane value is different ( this may be obvious but my game camera's far clip is like 10 so it "should" have more accuracy than scene view camera? )

    Also I think there should be option to set depth buffer bits and usage of stencil or not, because if I want higher depth precision and don't want stencil, I should be able to do this.

    Any ideas?
     
    Last edited: Jun 27, 2020
  45. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    The depth buffer is set for 32 bits. If you have a project you can share that demonstrates the issue, I'd recommend filing a bug, as we have not seen this issue before.
     
  46. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    I know there has been a long wait for this. We have a mandate at the moment to focus on things that can improve performance. As well there are people waiting on us to get a few key missing features addressed and bugs fixed so we can get out of experimental.

    Unfortunately, any new features will probably be pushed until these things are done.


    It sounds to me like its correct. I'll ask my colleague about it.
     
    EvOne likes this.
  47. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Right, thanks for that info. But is there way to turn off stencil buffer in favor of more accurate depth buffer precision? Or is that even possible? Or will that make no difference to the depth buffer accuracy at all?
     
  48. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I can understand that the priority is set somewhere else, but we all need to be informed where you are at, and when will things be expected to be moving forward? There is no roadmap, no dates, and we are just waiting around eagerly not knowing how to plan our development around 2DRenderer?

    Just what kind of performance improvement work are you guys doing at Unity? Can you share with us what you guys are working on and when you are expecting to move on from that to "what" another "next" thing? What are those missing key features and bugs that you guys are working at?

    "until these things are done" oh well... oh man...
     
    Last edited: Jun 29, 2020
    elZach, EvOne and MrPaparoz like this.
  49. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    @Chris_Chu,

    I think I found some issue with using 2Drenderer combination with normal and depth.

    Basically writing to Normal buffer happens "before" writing or considering depth buffer at all, so the normal is written in just using maybe a sorting order only, and then writing to the depth only happens while compositing with lighting.

    Is this indeed an issue or is there work around it? This is clearly visible in the frame debugger. This basically makes using depth buffer and normal unusable...

    I have reported this to the Unity.

    Bug report number : 1259874

    ---

    Basically, I think writing to the normal buffer pass should act the same way as the lit pass. Otherwise there is no point.
     
    Last edited: Jul 3, 2020
    E_Urban likes this.
  50. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    Is this performance pass limited to the 2D Renderer in particular or are other 2D tools included? Because the Individual Mode TilemapRenderer could use some work: it's significantly worse than SpriteRenderers.
     
Thread Status:
Not open for further replies.