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 Experimental 2D Lights and Shader Graph support in Universal RP (previously LWRP)

Discussion in '2D Experimental Preview' started by rustum, May 24, 2019.

  1. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    I have two sprites that overlap and both are semi-transparent. The alpha blends as normal, but for a certain Shader Graph I want to stop this because it looks bad and it's nearly impossible to get them to line up perfectly.

    Capture - Copy.PNG (overbright line in center between two sprites)

    Instead, I want to preserve the larger alpha at the overlap . I'm not sure how to do this. I tried accessing External Alpha _AlphaTex (is this right place?), but when I define a Texture2D on the blackboard by this name it'll throw an exception.

    Shader error in 'Shader Graphs/HighlightGradientShader': redefinition of '_AlphaTex' at .../Library/PackageCache/com.unity.render-pipelines.universal@7.3.1/Editor/ShaderGraph/Includes/SpriteUnlitPass.hlsl(2) (on d3d11)

    Compiling Vertex program with ETC1_EXTERNAL_ALPHA
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_FULL_HDR


    Is there a way around this?
     
  2. Teky

    Teky

    Joined:
    Aug 23, 2017
    Posts:
    8
    Hi first of all thanks for all the hard work! Is there any news on this subject? I have a bunch of shaders i would like to use and they need the scene color or _cameraOpaqueTexture. I have tried using a second camera but I loose to much performance only by enabling a second camera.
     
    SquaLLio likes this.
  3. SquaLLio

    SquaLLio

    Joined:
    Jun 16, 2019
    Posts:
    9

    There's a lot of us in the same boat, just waiting on this last feature to complete a game. Any bit of information on whether I should go another route, or continue to wait, would be greatly helpful...

    The problem is that I have no clue whether the time frame is like 2 months, or a year plus?
     
  4. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Does the latest URP 10 experimental version has any changes made to the 2DRenderer? Does _OpaqueTexture works in there? (for 2DRenderer)
     
  5. CrAzYpotpie

    CrAzYpotpie

    Joined:
    Sep 24, 2020
    Posts:
    11
    Just dove into the Universal rendering pipeline last night after finding out what it actually offered for 2D scenes (2D Lighting, mainly.) Really cool stuff! The first thing I wanted to use it for was a Halloween scene I had recently made with jack-o'-lanterns, so I decided to write a script which would emulate a candle flicker effect. After looking at the options in the inspector for the Light2D component, I noticed that modifying the falloffIntensity would provide the best effect for this. Much to my dismay, this variable is read only when trying to modify it in code.
    Is there any reason for this?

    Searching online for this found only one result, and it seems someone also wanted to do something similar at one point.

    https://forum.unity.com/threads/is-it-possible-to-change-falloffintensity-from-script.729545/

    Please expose this for us! Thank you!
     
  6. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    I'm glad to hear you like our work.

    There isn't a good reason for this at the moment. It should just be a matter of changing it to a settable property.
     
    CrAzYpotpie likes this.
  7. toad02

    toad02

    Joined:
    Mar 7, 2014
    Posts:
    4
    It took me a long time to figure out that this is why my shader wasn't working properly.
    Is there a place where we could formally express interest in having this feature available?
    I need it very much! Please :(
     
    SquaLLio likes this.
  8. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    For everyone I could find who was asking about this...
    @Teky @SquaLLio @essimoon2 @Alieldin @SoerenBachNielsen @Clockmagnet

    We probably won't officially have this feature available until 2021.1. However, I recently created a branch

    https://github.com/Unity-Technologies/Graphics/tree/2d/CameraSortingLayerTexture

    Core or Universal from this repo may have some dependencies on Unity 2021 apis. If you are unable to get it to work with the version of Universal you are using you can try just taking replacing the 2D directories in the Editor and Runtime directories of Universal with the ones from this branch.

    If you look at the 2D Renderer Asset you should see a drop-down called Camera Sorting Layer Texture Bounds. Layers will be render back to front as normal. When the specified layer is reached a copy will be triggered which will copy everything drawn so far (including the specified layer) to _CameraSortingLayerTexture. This shader variable can be used by both hand made shaders and in shadergraph.

    This isn't officially supported code yet, and is experimental but I'm going to try and get this in when I can.
    Let me know if there are any issues or if you have any suggestions.
     
    toad02 likes this.
  9. SquaLLio

    SquaLLio

    Joined:
    Jun 16, 2019
    Posts:
    9
    Thanks again Chris, as always. I haven't had a chance to try it yet, but does this mean the Scene Color node should work in Shader Graph now?

    Also, do you know if the SRP batcher has been implemented in the 2d renderer yet? Having trouble getting sprites with same material to batch, and the only thing google is returning is older post from this forum saying it doesn't work yet.
    Using the latest version of 2020.1
     
  10. Teky

    Teky

    Joined:
    Aug 23, 2017
    Posts:
    8
    Thanks for the hard work and listening to our request! Highly appreciated. As soon as I can I would give this a try
     
  11. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    Also interested in this. Batching of any sort seems broken right now.
     
    MaykeBr likes this.
  12. Teky

    Teky

    Joined:
    Aug 23, 2017
    Posts:
    8
    Hi! I have tried several times now to get this into my project but I was not able to do so. I think mainly because i don´t understand what I need to do.
    I cloned the repository and through the package manager installed the core, shadergraph and 2D renderer .Json files but after this i get several errors similar the one i quote next.

    C:\Users\Teky\Documents\GitHub\Graphics\com.unity.render-pipelines.core\Runtime\Utilities\CoreUtils.cs(891,20): error CS1061: 'ComputeShader' does not contain a definition for 'EnableKeyword' and no accessible extension method 'EnableKeyword' accepting a first argument of type 'ComputeShader' could be found.

    I tried also just replacing the runtime and editor folders with the ones in my library, and i get no errors but also nothing new (No dropdown under 2d pipeline Asset).

    Sorry if Im being a pain :O but i really have no idea where I am standing here, Thanks !
     
  13. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    If Scene Color wasn't working, this branch doesn't fix it. I can add it to our backlog though to make sure its at least on our list.

    There are currently compatibility issues with the SRP batcher. I'm not sure when or if these issues will be fixed. However, if normal sprite batching is broken, we either need to fix it or provide another way to batch.
     
  14. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    If you want to install all the packages you might need from that branch you will need:

    Core
    Shadergraph
    Universal

    and it sounds like you did that. But you will also probably need the very latest version of Unity. However, it's possible that it may not be a new enough version.

    My suggestion is that you sync or download the repo, and go into the Universal directory. From the editor directory and the runtime directory, grab the 2D folder and copy it over the 2D directories of an installed version of Universal that works with your editor.

    You will also need to find the 2D folder in the shader directory and do the same thing.
     
  15. Teky

    Teky

    Joined:
    Aug 23, 2017
    Posts:
    8
    Great thanks I Will try this then...
    Just to clarify, I am using 2019.4.8 LTS when you say I need the latest Unity version, you mean porting my Project to a 2020 version or update to the latest 2019.4.8.1 version because there might be a lot of work involved in porting to 2020. Just to know...

    Thanks again!
     
  16. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    If you do try the copying method, it would be great to hear your results.
     
  17. Starsmiao

    Starsmiao

    Joined:
    Aug 14, 2019
    Posts:
    10
    Hi,how was the work on performance going?I recently upgrade to Unity2020.1 with URP8.2,the Light2D still have bad performance on android platform.
     
  18. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    Can you describe what lighting features you are using?
     
  19. Starsmiao

    Starsmiao

    Joined:
    Aug 14, 2019
    Posts:
    10
    I use one global light(5 target layers) and 6 point lights(3 target layers) in my scene. I use Tmeline to control the point lights and make them on at night. But when the point lights are on,the frame rate decrease 20~30. It seems that those point lights out of the camera are still rendering. I have tried reduce the renderer texture scale to 0.3 and reduce sorting layers,but the performance is still bad on Android platform(CPU is Qualcomm Snapdragon 835,RAM 6GB).
     
  20. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    How many layers does your project have? Also, are you using shadows?
     
  21. Starsmiao

    Starsmiao

    Joined:
    Aug 14, 2019
    Posts:
    10
    I have 7 layers and 6 sorting layers and I am not using shadow because using shadow will decrease additional 10~20 FPS.
     
  22. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    If point lights are rendering off camera this is something that needs to be looked into.

    I can't say specifically how to best optimize your project, but reducing sorting layers, and your light texture size to a minimum is a good start. Also reducing blend styles to a minimum will help as well.

    As for lights, smaller lights and having fewer targeted sorting layers will have better performance.
     
  23. Starsmiao

    Starsmiao

    Joined:
    Aug 14, 2019
    Posts:
    10
    Thanks Chris,I will keep trying:)
     
  24. Djayp

    Djayp

    Joined:
    Feb 16, 2015
    Posts:
    114
    Hello ! Is there any reason for this in ShadowCaster2D ? At least TryGetComponent would be nice... but I wonder why we should try to find the renderer every frame.

    upload_2020-10-21_10-26-53.png
     
    NotaNaN likes this.
  25. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    I saw this recently, and yes this code shouldn't be there. Checking for a renderer is needed, but not at runtime and not in the update.
     
    NotaNaN and Djayp like this.
  26. Aksoq

    Aksoq

    Joined:
    Nov 11, 2019
    Posts:
    7
    Is it possible to move generated shadow mesh to a certain order in layer compared to other sprites? At the moment shadow is always above everything, but I want my explosion to be above shadow. Tried Z-values, Sorting Group on shadow caster item and light caster item. Unity 2019-3.
     
    Last edited: Oct 27, 2020
  27. WonkyDonky

    WonkyDonky

    Joined:
    Aug 24, 2014
    Posts:
    65
    Will Target Sorting Layers property be exposed to runtime in 2020.2?
     
    AzureMasters likes this.
  28. wlwl2

    wlwl2

    Joined:
    Jun 17, 2018
    Posts:
    29
    I'm spending a lot of time debugging the error `More than one global light on layer...` `UnityEngine.Networking.NetworkManagerHUD:Update()`. This error happens for each Sorting layer on unet Host start. Then I read the code:

    Code (CSharp):
    1.  
    2. // This should be rewritten to search only global lights
    3. List<Light2D> lights = s_Instance.m_Lights[blendStyleIndex];
    4.  
    in Light2D.cs.

    I'm working with multiplayer... (unet is the only LTS option now I think? and it's deprecated...)

    I'm sure that there's only one GlobalLight2D. I tried so many ways, but maybe the GlobalLight2D object is in a server or client that is not being stopped/destroyed somewhere? I can only see one GlobalLight2D though.

    The error seems to happen at a point which is too fast for me to stop, but I'm not sure if that is a network issue.

    Should this be fixed in Light2D.cs or is it actually fine? I'll probably find a solution for this, but I'll just keep this here for reference.

    Related to @Ferazel 's reply.
     
  29. wlwl2

    wlwl2

    Joined:
    Jun 17, 2018
    Posts:
    29
    Changed the type of light to anything other than Global, all the errors go away. Some of the error checks are done during OnEnable() that's maybe why I couldn't do a check on scene change.

    Edit: I no longer have any issues with using Global Light in multiplayer, but I'm now just wondering if this error check really needs to be different from the other lights. If it does, no problem.
     
    Last edited: Oct 30, 2020
  30. EyeDev44

    EyeDev44

    Joined:
    Apr 8, 2017
    Posts:
    149
    Чтe water shader with scene color node haven't been rendening since July, so I can't continue makeing the game. I use 2020.1. Shall I update to 2020.2 beta or I should wait until 2022?
     
  31. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    I haven't tested this but it may be possible to use a scene color node even with transparency-queue sprites if you render your scene first to a render texture used by an opaque material which is rendered with a second camera. That makes your whole scene opaque. Then you can have some transparent material on top of that which then has access to the scene color. But I'm not sure about that. What's the context in which you need it?
     
  32. FelipeQuevici

    FelipeQuevici

    Joined:
    Jan 8, 2013
    Posts:
    13
    Hey guys, so as most of us here, I want to use the _ColorOpaqueTexture, what I managed so far

    First there is also no way to use custom render features in the 2d renderer, but that was an easy fix, we just need this:

    https://github.com/Erdroy/ScriptableRenderPipeline/commit/38655346a64e6678fecd813ddadf294ed383dca6

    I saw in a post here (forgot wich) and that adds custom render features to the 2d Renderer, it's not really up to date, but it's not hard to implement on the current version of the URP something similar. It's working I've made a pass that adds image fx with custom materials.

    Now we need to create a render pass that saves the content of the screen to a global texture.

    So i found this:
    https://github.com/johnsietsma/ExtendingLWRP/tree/master/Assets/BlurryRefraction

    It creates a blurry copy of the scene in a texture called "_GrabBlurTexture" witch is accessible in HLSL shaders (no idea how to access in shader graph, but at least it's something.

    So now i just need to make a not blurred version of this so far my code is:

    Code (CSharp):
    1.  
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.Universal;
    4.  
    5. namespace UnityEngine.Experiemntal.Rendering.Universal
    6. {
    7.     /// <summary>
    8.     /// Copy the given color buffer to the given destination color buffer.
    9.     ///
    10.     /// You can use this pass to copy a color buffer to the destination,
    11.     /// so you can use it later in rendering. For example, you can copy
    12.     /// the opaque texture to use it for distortion effects.
    13.     /// </summary>
    14.     internal class GrabPassPass : ScriptableRenderPass
    15.     {
    16.         public FilterMode filterMode { get; set; }
    17.         private RenderTargetIdentifier source { get; set; }
    18.         private RenderTargetHandle destination { get; set; }
    19.  
    20.         RenderTargetHandle m_TemporaryColorTexture;
    21.         string m_ProfilerTag;
    22.  
    23.         /// <summary>
    24.         /// Create the CopyColorPass
    25.         /// </summary>
    26.         public GrabPassPass(RenderPassEvent renderPassEvent, string tag)
    27.         {
    28.             this.renderPassEvent = renderPassEvent;
    29.             m_ProfilerTag = tag;
    30.             m_TemporaryColorTexture.Init("_TemporaryColorTexture");
    31.         }
    32.  
    33.         /// <summary>
    34.         /// Configure the pass with the source and destination to execute on.
    35.         /// </summary>
    36.         /// <param name="source">Source Render Target</param>
    37.         /// <param name="destination">Destination Render Target</param>
    38.         public void Setup(RenderTargetIdentifier source, RenderTargetHandle destination)
    39.         {
    40.             this.source = source;
    41.             this.destination = destination;
    42.         }
    43.  
    44.         /// <inheritdoc/>
    45.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    46.         {
    47.             CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
    48.  
    49.             RenderTextureDescriptor opaqueDesc = renderingData.cameraData.cameraTargetDescriptor;
    50.             opaqueDesc.depthBufferBits = 0;
    51.  
    52.             cmd.GetTemporaryRT(m_TemporaryColorTexture.id, opaqueDesc, filterMode);
    53.  
    54.             cmd.SetGlobalTexture("_CameraOpaqueTexture", m_TemporaryColorTexture.id);
    55.             cmd.ReleaseTemporaryRT(m_TemporaryColorTexture.id);
    56.  
    57.             context.ExecuteCommandBuffer(cmd);
    58.             CommandBufferPool.Release(cmd);
    59.         }
    60.  
    61.         /// <inheritdoc/>
    62.         public override void FrameCleanup(CommandBuffer cmd)
    63.         {
    64.             //if (destination == RenderTargetHandle.CameraTarget)
    65.             cmd.ReleaseTemporaryRT(m_TemporaryColorTexture.id);
    66.         }
    67.     }
    68. }
    That is not working properly (because i honestly have no idea what is happening here i was mostly just messing around), it's not cleaning the texture properly so it turns black after some time, and also for some reason is taking into account the scene camera, not the game one, but it's kinda working (even the shader graph node scene color) and is saving the content of the camera.
    I fell it's not far from the solution. Can anyone help me out here?
     
    rootools, GamerXP and NotaNaN like this.
  33. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Is it planned to add emissive color to 2D renderer, so some pixels of a sprite ignore lighting completely? Is there any time estimation? Is there any way I can do that at the moment by modifying the URP?
    Thanks.
     
  34. Uthard

    Uthard

    Joined:
    Nov 19, 2020
    Posts:
    1
    Can I take into account white space (Orange) when lighting in 2D? Is there anything I can do to accommodate this space?
     
  35. WonkyDonky

    WonkyDonky

    Joined:
    Aug 24, 2014
    Posts:
    65
    Looks like Target Sorting Layers is still not availabe in code in 2020.2

    Will it be at some point? Any workarounds?
    @Chris_Chu
     
    EyeDev44 and AzureMasters like this.
  36. craftiii4

    craftiii4

    Joined:
    Dec 21, 2020
    Posts:
    1
    Does anyone know how I would make an object be invisible in sections that are in a shadow from a certain point light source?

    I've got a global light & it's set to not target this objects layer, but this results in a black cut-out in the location of the object.

    upload_2020-12-21_22-22-17.png

    If I don't have a global light, this works fine - but this isn't ideal. Any ideas? Thanks
     
  37. EyeDev44

    EyeDev44

    Joined:
    Apr 8, 2017
    Posts:
    149
    Still not available on 2021.1a?
     
  38. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Please add emission channel to the lighting!
     
    s-korovkin, pahe, EvOne and 4 others like this.
  39. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    @Chris_Chu was there any progress on global light direction? I was testing out the 2d lighting system recently and when using normal maps, the global light direction would be very welcomed.

    if not, whats the best way to simulate sunlight direction with normal mapped sprites? I was thinking of a huge point light that follows the camera...or just set my game at night ;)
     
    Last edited: Jan 14, 2021
    CrAzYpotpie, StenCG and NotaNaN like this.
  40. EyeDev44

    EyeDev44

    Joined:
    Apr 8, 2017
    Posts:
    149
    Okay, here is 2021.1. Scene color node is still broken. Is it again not in priority even in 2021?
     
    StenCG and EvOne like this.
  41. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    In that same area, I wanted to change the light2d freeform shape path by script, such that I can determine the light shape dynamically at runtime. This would be necessary for my stealth game. However, I found out that these properties are get-only as well. Would it be possible to expose these properties in the next release? If this is not possible in the near future, is there some way to contribute to or fork the package that allows me to easily upgrade to the official package at a later time?
     
  42. jeraldtapz_unity

    jeraldtapz_unity

    Joined:
    Feb 3, 2020
    Posts:
    3

    I saw this feature mentioned in 2021.1 beta blog post. I haven't found any documentation on it yet. I tried creating a texture in shader graph with reference "_CameraSortingLayerTexture" but nothing comes of it. Will this be officially or widely supported on official 2021.1 release?
     
  43. Ekibyo_Kaim

    Ekibyo_Kaim

    Joined:
    Mar 5, 2015
    Posts:
    11
    Hi Guys, I've been playing with the 2D lighting with tilemaps (Unity 2020.1.17), and I'm experiencing an issue with blendstyles.
    As you can see below, in Playmode, the light only affects some parts of the tilemap, and not at all like what I was expecting.


    On the other hand, in editor, it does look like what I was intending to make:


    Any idea what's causing this behaviour?
     
  44. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257

    If you file a bug report I'll take a look. There shouldn't be different things happening.
     
  45. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    It's supported, but will hopefully be better supported in 2021.2. Ill try and make some time to create a sample project
     
    yano_123 and jeffweber like this.
  46. Ekibyo_Kaim

    Ekibyo_Kaim

    Joined:
    Mar 5, 2015
    Posts:
    11
    I'll file a bug report then.

    I also found out why it's acting like that. The lighting won't work if the sprites are stored in a SpriteAtlas.
     
  47. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    That definitely shouldn't be happening. I will take a look when I get the bug report.
     
  48. SpiralCircus

    SpiralCircus

    Joined:
    Feb 1, 2014
    Posts:
    34
    Hi Chris,

    Have there been any changes to the public API on the Light2D component yet? Some key things that were missing before were the ability to change target sorting layers, freeform light polygon.

    It's so sad to see features that we can change in editor being hidden / encapsulated so they can't be changed from csharp code. I rely heavily on custom editor scripts as part of the workflow in design time, and I can't automate anything about these lights because so much of the editor functionality has been obfuscated.
     
    mountblanc, NotaNaN and Vanamerax like this.
  49. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Well, they have removed the Falloff offset for the freeform lights... that's a change (bad for me :( )
     
  50. EyeDev44

    EyeDev44

    Joined:
    Apr 8, 2017
    Posts:
    149
    2021.1.3f1. Still not fixed. upload_2021-4-16_19-9-31.png