SpriteRenderer & Immediate-Mode Drawing rendering order

Avatar
  • updated
  • Answered

First off, just want to say awesome work on the asset!

So I'm having a bit of trouble understanding how I would modify the order my Immediate mode lines are drawn in in order to make them drawn behind the sprites in my game. Working with the Line component there is no issue as I can modify the layer / order values in the inspector, however when drawing my lines in immediate mode I'm quite stumped as to how to move forward. 

Here is the code I'm using to draw my lines in my game. (Sorry for the image - just realized there was a code format button)
  

Image 353

More Context

Image 354


Any help here would be awesome, thanks so much in advance!

Reporting a bug? please specify Unity version:
Reporting a bug? please specify Shapes version:
3.2.3
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Answered

if you want to draw behind transparent objects, then you need to insert the draw command before transparent objects are rendered. There is unfortunately no other more granular way to sort them, unless you use components instead, or use opaque shaders that write to the depth buffer

anyway! Draw.Command has a second parameter where you specify when during the render process these shapes should be drawn. In your case, you'll want to draw at CameraEvent.BeforeForwardAlpha, which will make these lines be behind any other transparent objects in your scene

Avatar
Freya Holmér creator
  • Answer
  • Answered

if you want to draw behind transparent objects, then you need to insert the draw command before transparent objects are rendered. There is unfortunately no other more granular way to sort them, unless you use components instead, or use opaque shaders that write to the depth buffer

anyway! Draw.Command has a second parameter where you specify when during the render process these shapes should be drawn. In your case, you'll want to draw at CameraEvent.BeforeForwardAlpha, which will make these lines be behind any other transparent objects in your scene

Avatar
Thomas Brown
Quote from Freya Holmér

if you want to draw behind transparent objects, then you need to insert the draw command before transparent objects are rendered. There is unfortunately no other more granular way to sort them, unless you use components instead, or use opaque shaders that write to the depth buffer

anyway! Draw.Command has a second parameter where you specify when during the render process these shapes should be drawn. In your case, you'll want to draw at CameraEvent.BeforeForwardAlpha, which will make these lines be behind any other transparent objects in your scene

Ah thanks so much. That did the trick!

Sorry also if the question seemed a bit trivial, I'm a bit of a rendering newbie. In retrospect that makes a lot of sense given the constraints you laid out about immediate mode. I tried to scan your code & work it out myself but the pieces weren't quite fitting together yet haha.

Anyways thanks again, keep up the great work!

Fix With Celebratory Image

public class TrainingLineRenderer : ImmediateModeShapeDrawer
{
    //...

    public override void DrawShapes(Camera camera)
    {
        // only draw on the main camera
        if (camera != Camera.main)
        {
            return;
        }
    
        //draw the line behind any transparent objects in scene
        using (Draw.Command(camera, CameraEvent.BeforeForwardAlpha))
        {
            DrawLine(LineOffset);
        }
    }

    //...
}



Avatar
Freya Holmér creator

glad it worked out

Avatar
Spencer Hargiss

CameraEvent.BeforeForwardAlpha didn't work out for me in Unity 2020.3.

This worked instead:

public override void DrawShapes(Camera cam)
    {
        using (Draw.Command(cam, cameraEvent: UnityEngine.Rendering.Universal.RenderPassEvent.BeforeRenderingTransparents))
{ //.....
} }