PreviewRenderUtility and Handles

Hello

I’m currently making level editor and I’ve stumbled on couple problems with PreviewRenderUtility and Handles

I assume those problems are related and seem like preview and handles are rendered separately.

Does anyone have some idea or an example how to make them work together? Or maybe there is a better way to achieve something like this?

Thank you!

Found fix for issue 2:
render with updatefov set to false - PreviewRenderUtility.Render(updatefov: false)

alternatively fov for the camera can be set to the same value as PreviewRenderUtility is setting when updatefov is true while rendering handles

1 Like

Found workaround for issuer 1:
camera type in PreviewRenderUtility have to be set to SceneView
camera.cameraType = CameraType.SceneView;

After that Handles.zTest works as expected

1 Like

Found solution to issue 3:
offset from bottom left corner of window to bottom left corner of preview area must be added to Event.current.mousePosition.
Also pixelRect and aspect of camera in PreviewRenderUtility must be updated to size of preview area in Layout pass

Sorry for necroing this thread, but I’m currently trying to get Handles working inside a Preview window using RenderPreviewUtility, but I’m having trouble lining things up. The Handles drawing seems to be completely misaligned from the preview scene, and scales and anchors depending on the preview window size.

        public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
        {
            if (Event.current.type == EventType.Repaint)
            {
                var matrix = Matrix4x4.identity;
                _pru.BeginPreview(r, background);
                _pru.DrawMesh(mesh, matrix, material, 0);
                _pru.Render(true, false);
                _pru.EndAndDrawPreview(r);

                using(new Handles.DrawingScope(Color.green, matrix))
                {
                    Handles.SetCamera(_pru.camera);
                    Handles.DrawWireCube(Vector3.zero, Vector3.one * 0.5f);
                }
            }
        }
       
        //Called in HasPreviewGUI
        private void SetPreviewArea()
        {
            if (_pru == null)
            {
                _pru = new PreviewRenderUtility();
            }
            _pru.camera.transform.position = Vector3.zero;
            _pru.camera.transform.rotation = Quaternion.Euler(30, -45, 0);
            _pru.camera.transform.position = _pru.camera.transform.forward * -6;
            _pru.camera.cameraType = CameraType.SceneView;
            _pru.camera.fieldOfView = _pru.cameraFieldOfView;
        }

Here is the result:
7991997--1026972--BrokenPreview.PNG
I’ve also tried setting the Handles Matrix to the Camera’s projection matrix but that seemed to completely mess things up even further.

Check if this will help https://discussions.unity.com/t/728634/43

1 Like

Hey, thanks for getting back to me!
I’ve taken a quick look at that thread and your posts there and split my code up the way you did in the highlighted post (minus the event mouseposition manipulation as my handles aren’t interactive for now), and although there’s improvement I’m still having issues when the preview is scaled below a certain treshold (I assume that 1024 problem talked about elsewhere in the thread). Interestingly this only happens with width scaling, height scaling seems consistent.

On a side note, there also seems to be a Handles.SetCamera method that accepts both a camera and a rectangle, however using that causes even more buggy behaviour (and even breaks the rendering of the meshes prior in some cases). Any clue what’s up with that?

I was struggling with this for several hours and I figured out my bug just by swapping two lines.
To render Handles use this:

//Use this in OnGUI

previewRendererUtility.BeginPreview(contentRect, "");
previewRendererUtility.camera.Render();

using (new Handles.DrawingScope(Matrix4x4.identity) 
{
     Handles.SetCamera(previewRendererUtility.camera);
     //Your custom handles function.
     OnDrawHandles();
}

previewRendererUtility.EndAndDrawPreview(contentRect);

The thing is, you have to Begin preview, then render camera and after render Handles. Then End preview and draw it. ORDER IS IMPORTANT!

Hope this will save someone from 2-3 hours struggle. Cheers!

1 Like

Thanks so much for fix #1 this was driving me insane