Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

PreviewRenderUtility and Handles

Discussion in 'Scripting' started by NosorozecKaszle, Apr 2, 2020.

  1. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9
    Hello

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

    1. I've managed to render preview of prefab with some just visual handles. I want to hide lines behind mesh, but Handles.zTest = CompareFunction.LessEqual; doesn't change anything. upload_2020-4-2_18-26-7.png
    2. When preview is streched too much verticaly handles are not aligned (as seen on previous screen). upload_2020-4-2_18-27-28.png
    3. Now I am at point when I want to add interactive handles (I want to make editor for 3d array), but they don't react to mouse at all.

    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!
     
  2. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9
    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
     
  3. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9
    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
     
    ThermalFusion likes this.
  4. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9
    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
     
  5. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    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.

    Code (CSharp):
    1.         public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
    2.         {
    3.             if (Event.current.type == EventType.Repaint)
    4.             {
    5.                 var matrix = Matrix4x4.identity;
    6.                 _pru.BeginPreview(r, background);
    7.                 _pru.DrawMesh(mesh, matrix, material, 0);
    8.                 _pru.Render(true, false);
    9.                 _pru.EndAndDrawPreview(r);
    10.  
    11.                 using(new Handles.DrawingScope(Color.green, matrix))
    12.                 {
    13.                     Handles.SetCamera(_pru.camera);
    14.                     Handles.DrawWireCube(Vector3.zero, Vector3.one * 0.5f);
    15.                 }
    16.             }
    17.         }
    18.        
    19.         //Called in HasPreviewGUI
    20.         private void SetPreviewArea()
    21.         {
    22.             if (_pru == null)
    23.             {
    24.                 _pru = new PreviewRenderUtility();
    25.             }
    26.             _pru.camera.transform.position = Vector3.zero;
    27.             _pru.camera.transform.rotation = Quaternion.Euler(30, -45, 0);
    28.             _pru.camera.transform.position = _pru.camera.transform.forward * -6;
    29.             _pru.camera.cameraType = CameraType.SceneView;
    30.             _pru.camera.fieldOfView = _pru.cameraFieldOfView;
    31.         }
    32.  
    Here is the result:
    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.
     
  6. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9

    Check if this will help https://forum.unity.com/threads/wri...at-mimics-the-scene-view.613231/#post-5754448
     
    Yandalf likes this.
  7. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    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?
     
  8. leveland

    leveland

    Joined:
    Apr 1, 2017
    Posts:
    2
    I was struggling with this for several hours and I figured out my bug just by swapping two lines.
    To render Handles use this:

    Code (CSharp):
    1. //Use this in OnGUI
    2.  
    3. previewRendererUtility.BeginPreview(contentRect, "");
    4. previewRendererUtility.camera.Render();
    5.  
    6. using (new Handles.DrawingScope(Matrix4x4.identity)
    7. {
    8.      Handles.SetCamera(previewRendererUtility.camera);
    9.      //Your custom handles function.
    10.      OnDrawHandles();
    11. }
    12.  
    13. 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!