Draw To Screen Space Foreground From DrawViewportWires

Hi All,

I’m experimenting with implementing DrawViewportWires in GHPython for drawing e.g. legends on top of 3D geometry generated in Grasshopper. Here’s a quick example:

The issue I’m trying to resolve is that the rectangles (drawn with Draw2dRectangle ) are drawn behind the 3D geometry that is drawn by Grasshopper (though it still appears to obscure the wires of the 3D mesh), while the text is drawn in the foreground:

Here’s a minimal example and file of this behaviour:

201125_DrawRectangle2d_00.gh (4.9 KB)

I can see that this issue has been brought up previously by @andheum. But I’ve not been able to find any examples/code that demonstrate how one has resolved this (notably how to “add a callback to the DisplayPipeline.DrawForeground event”). I should also mention that I’ve been given great pointers by @dave_stasiuk and @tom_svilans on how they draw to the foreground from a display conduit in a compiled component. But before going down that path, I just wanted to make sure that I’m not missing something here.

So, is it possible to always draw to the screen space foreground from DrawViewportWires?



1 Like

Apologies for bumping. Maybe @stevebaer, @piac, or @DavidRutten have some suggestions?

You really want to have HUD type display done in the DrawForeGround stage of drawing. DrawForeground occurs after DrawViewportWires and has depth testing turned off which is exactly what you want with a HUD.

Here is what I was able to cook up. It’s not as clean as just having a DrawForeground override in the component class, but it should work. Let me know if you have any questions.

201125_DrawRectangle2d_00.gh (16.1 KB)

@piac helped me make a cleaner version.
Here’s an update 201125_DrawRectangle2d_00.gh (13.6 KB)


This is fantastic, thanks so much! Will have a go at implementing this into the legend component ASAP. You guys rock :metal:

I ended up using a combination of the two methods: Implementing both the hack to pipe into DrawViewportWires and the __enter__/__exit__ methods to add/remove the DrawForeground method. This means the component still behaves like a “normal” Grasshopper component (i.e. one can turn Preview off to stop drawing etc.).

It seems to work okay so far:

And here’s the minimal case again, with these edits:
201203_DrawRectangle2d_02.gh (5.3 KB)

Thanks again guys, very much appreciated :beers:

1 Like

I took a stab at a HUD display leveraging the DisplayPipeline.DrawForeground event handler in C#. Basic functionality works as expected, but I have a few questions on how to further customize it:

  1. The event fires for each viewport separately. Is there a way to limit the HUD to only the active viewport?

  2. Fill color of 2D shapes respects transparency, 2D text color ignores it. Is this intended behavior?

  3. Using the ViewCaptureToFile command with increased resolution doesn’t scale the HUD correctly and causes it to be drawn multiple times. Any ideas how to address this?


Any ideas @AndersDeleuran @stevebaer ?

201208_DrawRectangle2d_03.gh (6.2 KB)

I added this conditional to only render in the active viewport:

Afraid I don’t have any ideas on the other two issues.

1 Like

Thanks Anders,

works like a charm! C# equivalent below if anyone is interested:

    private void DrawForeground(object sender, DrawEventArgs e)
        if (e.Viewport.Id == Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewportID)
         // do magic
1 Like

Using the doc that gets passed as part of the DrawEventArgs is better to use over ActiveDoc

1 Like

Thanks @stevebaer, fixed that part:

private void DrawForeground(object sender, DrawEventArgs e)
    if (e.Viewport.Id == e.RhinoDoc.Views.ActiveView.ActiveViewportID)
     // do magic

Any suggestions on how to attack the scaling and transparency issues?