Wrong shading using DrawMeshShaded

I’m writing a c# plugin for rhino 8.
I get the wrong shading of the preview (the box on the left). I expext to get a preview like vanilla components (box on the right).

What am I missing?

Thanks

My code

I’m overwriting the preview with this code.

In Component.cs:

public override void DrawViewportMeshes(IGH_PreviewArgs args)
{
    base.DrawViewportMeshes(args);

    DisplayMaterial material = Attributes.Selected ? args.ShadeMaterial_Selected : args.ShadeMaterial;

    System.Drawing.Color color = Attributes.Selected ? args.WireColour_Selected : args.WireColour;
    int thickness = Attributes.Selected ? 2 : args.DefaultCurveThickness;

     Preview.drawViewportBreps(args, material, color, thickness, previewBreps);
}

In Preview.cs there is a Preview class with drawViewportBreps method:

public static void drawViewportBreps(IGH_PreviewArgs args, DisplayMaterial material, Color color, int thickness, List<Brep> breps = null)
{
    // Ensure the list is not null AND contains items
    if (breps != null && breps.Count > 0)
    {
        foreach (Brep brep in breps)
        {
            if (brep == null || !brep.IsValid) continue;

            MeshingParameters meshingParameters = new MeshingParameters();

            Rhino.Geometry.Mesh[] meshes = Rhino.Geometry.Mesh.CreateFromBrep(brep, meshingParameters);

            foreach (Rhino.Geometry.Mesh mesh in meshes)
            {
                args.Display.DrawMeshShaded(mesh, material);
            }

            Curve[] wires = brep.GetWireframe(0);

            if (wires != null)
            {
                foreach (Curve wire in wires)
                {
                    args.Display.DrawCurve(wire, color, thickness);
                }
            }
        }
    }

Hi @Mattia_Bressanelli,

Any reason you draw the Brep using DisplayPipleline.DrawBrepShaded?

– Dale

Hi,
I tried to use DrawBrepShaded but it looks even worse.

So if I change it to:

public static void drawViewportBreps(IGH_PreviewArgs args, DisplayMaterial material, Color color, int thickness, List<Brep> breps = null)
{
    if (breps != null && breps.Count > 0)
    {
        foreach (Brep brep in breps)
        {
            args.Display.DrawBrepShaded(brep, material);
        }
    }
}

I get (this is a more complex brep just so that the problem is more evident than in the box above):

If I bake the Brep and select it from vanilla gh components it looks just fine:

Thanks

I fixed the issue adding the following:

brep.Repair(Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
brep.SetTolerancesBoxesAndFlags(true, true, true, true, true, true, true, true);
brep.Faces.SplitKinkyFaces(1.0 * Math.PI / 180.0, true);
brep.Compact();

the full function is now:

public static void drawViewportBreps(IGH_PreviewArgs args, DisplayMaterial material, Color color, int thickness, List<Brep> breps = null)
{
    if (breps != null && breps.Count > 0)
    {
        foreach (Brep brep in breps)
        {
            brep.Repair(Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
            brep.SetTolerancesBoxesAndFlags(true, true, true, true, true, true, true, true);
            brep.Faces.SplitKinkyFaces(1.0 * Math.PI / 180.0, true);
            brep.Compact();

            Mesh[] meshes = Mesh.CreateFromBrep(brep, MeshingParameters.QualityRenderMesh);

            var combinedMesh = new Mesh();
            foreach (var m in meshes)
            {
                combinedMesh.Append(m);
            }

            args.Display.DrawMeshShaded(combinedMesh, material);

            List<BrepEdge> edges = brep.Edges.ToList();
            List<Curve> curves = new List<Curve>();

            foreach (BrepEdge edge in edges)
            {
                curves.Add(edge.EdgeCurve);
            }

            foreach (Curve curve in curves)
            {
                args.Display.DrawCurve(curve, color, thickness);
            }
        }
    }
}

called in the component override like:

public override void DrawViewportMeshes(IGH_PreviewArgs args)
{
    base.DrawViewportMeshes(args);

    DisplayMaterial material = Attributes.Selected ? args.ShadeMaterial_Selected : args.ShadeMaterial;

    System.Drawing.Color color = Attributes.Selected ? args.WireColour_Selected : args.WireColour;
    int thickness = Attributes.Selected ? 2 : args.DefaultCurveThickness;

    Preview.drawViewportBreps(args, material, color, thickness, previewSolids);
}