Preview geometry in front of grid in Rhino viewport

I am writing a component for Grasshopper in C# that only has 1 job for now, output a cylinder to the rhino viewport. I’ve got it working, but the geometry is drawn in front of the grid in the Rhino viewport, even though the cylinder sits below the XY plane. Is there a way to have the Rhino grid drawn where it should be?

Any other comments about how this component has been set up would also be great, it is my first component with zero outputs and I’m not sure if I’m doing it right.

Thanks!
AconityBuildPlate.cs (3.7 KB)

That’s just how Rhino renders shaded geometry. Try it yourself by creating the cylinder in Rhino document manually. It will still always render “in front” of CPlanes.

But for the improvements;

  1. No need to call base.DrawViewportWires(args); inside of DrawViewportMeshes().
  2. If you plan to use the same material everytime, it’s better to initialize the preview material immedietly in the constructor. No need to create a new DisplayMaterial everytime the DrawViewportMeshes is called.
private Mesh previewMesh = new Mesh();
private DisplayMaterial mat = new DisplayMaterial(Color.White);
  1. Alternatively to #2, don’t create your own material and just grab the one that Grasshopper already uses for unselected/selected geometry. You can retrieve it from the args passed into DrawViewportMeshes()
mat = Attributes.Selected ? args.ShadeMaterial_Selected : args.ShadeMaterial;
1 Like

Also, if you want to hide the preview when the components is disabled, you can check for Locked property of the component (Locked == true means component is disabled).

if (!Locked && previewMesh != null && previewMesh.IsValid)
1 Like

Awesome, I like the rendered view, but the grid is also good for my application so I might just stick with the selected / unselected approach.

Since I can’t have the grid drawn on top, I’ve defined a list of 2 lines that are aligned with the X and Y axis to ease visualisation:

        public override void DrawViewportMeshes(IGH_PreviewArgs args)
        {
            base.DrawViewportMeshes(args);
            if (previewMesh != null && previewMesh.IsValid)
            {
                args.Display.DrawMeshShaded(previewMesh, mat);
                foreach (var line in referenceLines)
                {
                    args.Display.DrawLine(line, Color.Black);
                }
            }
        }

And thats cool to check for the locked status too.

Thanks for your help, Ondrej!