Issue with DrawMeshShaded in a Rhino 8 DisplayConduit

Hello there,
Rhino 8 is a sensation throughout, but I’m having an issue with the DrawMeshShaded in Rhino 8, which no more - draws a shaded mesh, not even in shaded display mode. I tried it both with framework 4.8, and .net 7.0 and on an Nvidia and an AMD GPU, didn’t make a difference.
Inside Tweener, its editing environment is based on a

public class AnimDisplayConduit3 : Rhino.Display.DisplayConduit

which overrides


protected override void PostDrawObjects (Rhino.Display.DrawEventArgs e) {
       base.PostDrawObjects(e);
       if (r.ABM2 != null) e.Display.DrawMeshShaded(r.ABM2, m_material);
}

where r.ABM2 is a mesh with uv-coordinates, and

public readonly Rhino.Display.DisplayMaterial m_material = new Rhino.Display.DisplayMaterial();

which is set with the sort-of-convoluted hack

        public void ResetTexture () {
            var simtext = Rhino.Render.RenderTexture
            .NewBitmapTexture(r.Tt.bm, Rhino.RhinoDoc.ActiveDoc)
            .SimulatedTexture(Rhino.Render.RenderTexture.TextureGeneration.Allow)
            .Texture();
            this.m_material.IsTwoSided = true;
            this.m_material.SetBitmapTexture(simtext, false);
            this.m_material.SetBitmapTexture(simtext, true);
        }

(is there anything better to get a DisplayMaterial from a bitmap)? This worked in Rhino 7, to draw a mesh, at least in the shaded mode (- which is why Tweener has to change modes to shaded for the editing environment, and to rendered after placing the mesh into the document to make the texture visible; is there a way I could avoid to set the viewports’ display modes altogether?), and is what makes Tweener run and fun. However, it broke in Rhino 8: the mesh is no more drawn in a shaded way by the PostDrawObjects method of the conduit! Instead, it just appears grey. Help appreciated.
I also have lots of other questions about the 7 → 8 migration, but that’s for another post.
Thanks in advance!
Mathias

Hi @mathias.fuchs,

Can you provide me some sample code that allows me to reproduce the problem? So code that works in V7 but not V8.

Thanks,

– Dale

Thank you for the answer, @dale. Yes, here you are. It took me quite some effort to boil down some minimal code that works in Rhino 7 but not in Rhino 8. (Code is not for performance, only to demonstrate the issue.) With the following code, the triangle is drawn with uv coordinates applied correctly in Rhino 7, but not in Rhino 8. The issue must be related to the way uv coordinates are applied in the rendering pipeline, so it should be reproducible with materials from doc.Materials as well, if you prefer.

    public class SomeExampleConduit : Rhino.Display.DisplayConduit {
        protected override void PostDrawObjects(Rhino.Display.DrawEventArgs e) {
            base.PostDrawObjects(e);

            Mesh cp = new Mesh();
            cp.Vertices.Add(0, 0, 0);
            cp.Vertices.Add(1, 0, 0);
            cp.Vertices.Add(0, 1, 0);
            cp.Faces.AddFace(new MeshFace(0, 1, 2));
            cp.Normals.ComputeNormals();
            cp.TextureCoordinates.Add(0, 0);
            cp.TextureCoordinates.Add(1, 0);
            cp.TextureCoordinates.Add(0, 1);

            var iv = cp.IsValid; // returns true


            var ma = new DisplayMaterial();
            Bitmap bitmap = Properties.Resources._21; // just some image from the resources
            var simtext = Rhino.Render.RenderTexture
                .NewBitmapTexture(bitmap, Rhino.RhinoDoc.ActiveDoc)
                .SimulatedTexture(Rhino.Render.RenderTexture.TextureGeneration.Allow)
                .Texture();

            ma.IsTwoSided = true;
            ma.SetBitmapTexture(simtext, false);
            ma.SetBitmapTexture(simtext, true);


            // is drawn differently in Rhino 8. Why?
            e.Display.DrawMeshShaded(cp, ma);
        }

        protected override void CalculateBoundingBox(CalculateBoundingBoxEventArgs e) {
            e.IncludeBoundingBox(new BoundingBox(new Point3d(0, 0, -1), new Point3d(1, 1, 1)));
        }
    }
}

Rhino 7 (don’t mind the violet curves):
grafik
Rhino 8:
grafik

Thanks for helping to find the cause, it really breaks Tweener. I don’t see an alternative to calling DrawMeshShaded and relying on the correct application of my uv coordinates.
M

Hi @mathias.fuchs,

I can repeat the display issue. I’ve logged the issue so we can look into this.

https://mcneel.myjetbrains.com/youtrack/issue/RH-78365

Thanks,

– Dale

Thank you, @dale . In the meantime, in case anyone seeing this had an idea how to work around DrawMeshShaded, it would be much appreciated.

Hey Mathias,

I’ve already replied to you directly, but I thought I’d mention it here as well for others to see…

There will be a fix for this in 8.1…but if you can’t wait until then, a simple workaround can be applied…

After setting up all of the texture coordinates for a mesh, you need to call

SetSurfaceParametersFromTextureCoordinates()

on the mesh. Using the example code above it would be this:

            cp.TextureCoordinates.Add(0, 0);
            cp.TextureCoordinates.Add(1, 0);
            cp.TextureCoordinates.Add(0, 1);
            
            cp.SetSurfaceParametersFromTextureCoordinates(); // NEW!

Note: This API is on V8 only. So you’ll need to update the project NuGet package.

Thanks,
-Jeff

2 Likes

Thanks @jeff , @dale , that worked! Tweener now works on Rhino 8, which is a great relief.
Mathias

RH-78365 is fixed in Rhino 8 Service Release 1