What transforms are in effect in OnSceneGUI for DrawMeshNow?

I’m trying to draw buttons in the scene view and in-scene stuff for a level editor. I have a level editor that’s a monobehaviour and I’m doing my drawing in OnSceneGUI. I’m totally happy to do it somewhere else with the editor as a different kind of object if that’s how it should be done! But I don’t know what that way is so I’m doing it this way right now.

I have a bunch of meshes, let’s say 5 meshes, and I want to draw a button for each mesh and then draw the mesh on the button. The AssetPreview.GetAssetPreview function is nice, but it is rendering my objects out as black silhouettes so I want to use DrawMeshNow with a material to draw them properly. I also want to draw the currently selected mesh (whatever button was clicked last) at the mouse pointer in the scene view after drawing a collision ray into the screen.

Here’s my current OnSceneGUI, which I’ve boiled down to remove some of the stuff that isn’t relevant…

    private void OnSceneGUI()
    {
        Handles.BeginGUI();

        int y = 60;
        int size = 100;
        int scrollBarWidth = 24;
        int fieldHeight = (int)SceneView.lastActiveSceneView.position.height - y - 40;
        Vector2 scrol = GUI.BeginScrollView(new Rect(10, y, size + scrollBarWidth, fieldHeight), new Vector2(0, scroll), new Rect(0, 0, size, size * available.Count));
        scroll = scrol.y;
        int localY = 0;
        int xGutter = 10;
        foreach (var collectable in available)
        {
            if (localY > scroll- size && localY < fieldHeight + scroll + size)
            {
                if (GUI.Button(new Rect(xGutter, localY, size, size), AssetPreview.GetAssetPreview(collectable.prefab)))
                {
                    selected = collectable;                   
                }

                Mesh mesh = collectable.prefab.GetComponent<MeshFilter>().sharedMesh;
                collectable.prefab.GetComponent<MeshRenderer>().sharedMaterial.SetPass(0);
                Bounds bounds = mesh.bounds;
                Matrix4x4 matrix =
                //Camera.current.worldToCameraMatrix;
                //Matrix4x4.TRS(new Vector3(xGutter + size/2, localY + scroll + size/2, 0), Quaternion.identity, Vector3.one * 100 / mesh.bounds.size.magnitude);
                //Matrix4x4.TRS(new Vector3(xGutter + size/2, localY + scroll + size/2, 0), Quaternion.identity, Vector3.one * 100 / mesh.bounds.size.magnitude);
                //Matrix4x4.Ortho(-bounds.extents.x,bounds.extents.x,-bounds.extents.y,bounds.extents.y,-bounds.extents.z,bounds.extents.z);
                //GUI.matrix;
                //matrix *= Camera.current.worldToCameraMatrix.inverse;
                Camera.current.worldToCameraMatrix.inverse;
                matrix *= GUI.matrix.inverse;
                matrix *= Matrix4x4.Ortho(-10,10,-10,10,-10,10);
                matrix *= Matrix4x4.TRS(new Vector3(xGutter + size/2, localY + scroll + size/2, 0), Quaternion.identity, Vector3.one * 100 / mesh.bounds.size.magnitude);
                matrix *= Matrix4x4.Scale(Vector3.one * 10);
                Graphics.DrawMeshNow(mesh, matrix);
            }
            localY += size;
        }
        GUI.EndScrollView();

        Handles.EndGUI();
    }

That’s just for the buttons, I’m happy to just sort the buttons first. I got the current camera’s inverse matrix idea from @Nothke and that’s really good, it gets rid of the scene view camera’s rotation, but there still seems to be a GUI matrix involved. When I scroll my scroll view the meshes I’m drawing move with it. Setting the GUI matrix to identity does nothing. Multiplying with inverse of gui matrix doesn’t seem to work either. Same with the handles matrix and the GL matrix. What matrices are left to destroy?? How can I control the rendering at this particular time?

please help me matrix this my family is dying

I think you would have better luck rendering the asset preview off-screen to a texture, then drawing that texture in the GUI with the usual GUI.DrawTexture. You can use Unity - Scripting API: SceneManagement.EditorSceneManager.NewPreviewScene to create an empty scene, along with Unity - Scripting API: Camera.overrideSceneCullingMask to render your camera with only the preview scene visible.

OK thanks Karl! That would take care of the buttons. I actually started on a similar plan - allocate a temporary rendertexture and render the object out to that. But again, the matrix was interfering with my drawing.
It would be useful to be able to render it real time as I would like ideally to be able to change the asset’s material and have that reflected in the button.

For rendering the object in the scene, I was going to avoid OnSceneGUI and just have a dummy gameobject and change the mesh in the object’s meshfilter to the currently selected asset’s mesh, assign a semi transparent material and move the object to a mouse cursor raycast’s hitpoint. I could still draw the handles in OnSceneGUI.

Okay, I am using an Odin editor window for this now, and using a dummy GameObject to draw the potential object in the scene view.

The lesson to learn from this is do not try to draw things in the scene view imo.