CustomMeshObject with DrawMeshShaded inside OnDraw override

Hi everyone,

I’m fighting with CustomMeshObject, trying to keep its base geometry as a single mesh object, but draw it separately for distinct colors - tree trunk and crown. The idea seems to be straightforward and should be fairly easy to achieve, but I’m getting weird results in the viewport when selecting the item, it looks like the object is somehow “flipped” when selected.

Here’s my OnDraw override:

protected override void OnDraw(DrawEventArgs e)
{

EnsureSplitMeshes(); // Method that makes sure if cached elements are ready

if (_cachedTrunk != null && _cachedCrown != null)
{
    e.Display.DrawMeshShaded(_cachedTrunk, TrunkMat);
    e.Display.DrawMeshShaded(_cachedCrown, CrownMat);
}

// Just to keep Rhino selection highlight
if (IsSelected(false) > 0)
{
    base.OnDraw(e); // with it or without it i get "hollow" selection result
}

}

Here’s how it behaves in three variants:
Without base.OnDraw(e)

With it for the highlight feature:

And plain passthrough without any additional tweaks in the OnDraw override - so without “double colored” mesh - the only one that behaves as expected.

And the last one 4th actually, when in custom display mode - suddenly I see a reflection on a simple diffuse mat - those are set like this:
private static readonly DisplayMaterial TrunkMat = new DisplayMaterial(Color.FromArgb(152, 128, 92));
private static readonly DisplayMaterial CrownMat = new DisplayMaterial(Color.FromArgb(102, 192, 56));

Does anyone have a hint on how to avoid such glitches and show the same result as without selection (when with selection)?

Hi @D-W,

I have seen the “reflection behavior” on breps and meshes drawn in a custom display conduit as well.

I hadn’t solved it but will let you know if I learn more to that effect.

Silly question, but if you bake your tree as is and run DIR on it. Which direction are it’s normals?

My guess is it’s already flipped before you are getting it and it looks correct before you select it because you are seeing into the tree mesh and the green beyond on the opposite side of the canopy. Without lighting/shading it visually will look correctly oriented.

If, in document properties, display mode settings you turn off back face rendering I bet you’ll have a ghost tree there, no?

Hi @michaelvollrath! Thanks for chiming in!

Here’s the DIR (those which are inside pointing upwards belongs to cap of trunk cylinder)

Besides that, I set “cull backfaces” here, but no change - also, I don’t notice a ghost tree:

Or did I do it wrong somehow?

I found this insight from Dale:

Dividing it with:
e.Display.DrawingWires
and
e.Display.DrawingSurfaces

Seems to solve selected “hollow” and “reflection” problem - does not answer how to shade in wireframe when needed though sometimes it can be enough.