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.
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;
No need to call base.DrawViewportWires(args); inside of DrawViewportMeshes().
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);
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;
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)