Search Unity

Resolved Display issue in EditorWindow

Discussion in 'Scripting' started by Draad, May 21, 2023.

  1. Draad

    Draad

    Joined:
    Feb 17, 2011
    Posts:
    325
    Hello,

    I created an EditorWindow that allow to create meshes by dragging a selection. During the drag, camera follow mouse pointer. When I reach some positions, the selection preview (green) is getting sliced, I don't really understand why. Note that this issue only happens when dragging the selection on negatives values of axis, it does not happen at all when dragging on positive values.

    Screenshot 2023-05-20 at 11.35.54 PM.png

    Even if I zoom out, it remains sliced at the same place.
    Even more, if I validate the selection and it gets converted to some cubes, the cubes gets also sliced in the same position :

    Screenshot 2023-05-20 at 11.35.58 PM.png

    Now, if I go back to my scene view, the asset is displayed correctly :

    Screenshot 2023-05-20 at 11.36.23 PM.png


    I assumed it's a camera issue, my guess is it has to do with clipPlanes, but all my attempts failed so far. This is my camera settings :

    Code (CSharp):
    1.     // My orthographic camera
    2.     public CrafterCamera(Scene scene)
    3.     {
    4.       camera = new GameObject().AddComponent<Camera>();
    5.       camera.clearFlags = CameraClearFlags.SolidColor;
    6.       camera.backgroundColor = new Color(0.25f, 0.25f, 0.25f, 1);
    7.       camera.nearClipPlane = 0.3f;
    8.       camera.farClipPlane = 1000f;
    9.       camera.transform.Rotate(new Vector3(45, 45, 0));
    10.       camera.orthographic = true;
    11.       camera.scene = scene;
    12.       camera.hideFlags = HideFlags.HideAndDontSave;
    13.  
    14.       renderTexture = new RenderTexture(1, 1, 24);
    15.       renderTexture.hideFlags = HideFlags.HideAndDontSave;
    16.  
    17.       camera.targetTexture = renderTexture;
    18.  
    19.       camera.transform.position = new Vector3(-5, 10, -5);
    20.       focusPoint = Vector3.zero;
    21.  
    22.       EditorSceneManager.MoveGameObjectToScene(camera.gameObject, scene);
    23.     }
    24.  
    25.     // The different ways it gets moved // zoomed
    26.     public void Follow(Vector2 mouseDelta)
    27.     {
    28.       Vector3 difference = camera.transform.position - focusPoint;
    29.  
    30.       focusPoint += new Vector3(mouseDelta.x, mouseDelta.y, 0);
    31.       camera.transform.position = focusPoint + new Vector3(-5, 10, -5);
    32.     }
    33.  
    34.     public void Translate(Vector2 mouseDelta)
    35.     {
    36.       focusPoint += new Vector3(mouseDelta.x, mouseDelta.y, 0);
    37.       camera.transform.Translate(new Vector3(mouseDelta.x, mouseDelta.y, 0), Space.Self);
    38.     }
    39.  
    40.     public void FocusPoint(Vector3 position)
    41.     {
    42.       Vector3 difference = camera.transform.position - focusPoint;
    43.  
    44.       focusPoint = position;
    45.       camera.transform.position = focusPoint + difference;
    46.     }
    47.  
    48.     public void UpdateOrthographicSize (float offset)
    49.     {
    50.       camera.orthographicSize -= offset;
    51.  
    52.       if(camera.orthographicSize <= 0.5f)
    53.         camera.orthographicSize = 0.5f;
    54.     }
    55.  
    56.  
    57.     // The way it get rendered
    58.     public void Render(Rect position)
    59.     {
    60.       var rect = position;
    61.       rect.position = Vector2.zero;
    62.  
    63.       // Update render texture if required
    64.       if (rect.width != renderTexture.width || rect.height != renderTexture.height)
    65.       {
    66.         renderTexture.Release();
    67.         renderTexture.width = (int)rect.width;
    68.         renderTexture.height = (int)rect.height;
    69.       }
    70.  
    71.       camera.pixelRect = rect;
    72.       camera.Render();
    73.  
    74.       Graphics.DrawTexture(rect, renderTexture);
    75.     }
    76.  
    Thanks for your help
     
    Last edited: May 21, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    I agree, and it looks like it is your near clipping plane being too far forward.

    Did you know that for orthographic cameras you can actually set a negative nearClip value??

    I only recently realized this myself so perhaps that's a solution for you?
     
  3. Draad

    Draad

    Joined:
    Feb 17, 2011
    Posts:
    325
    Hooooooo, wow, a chance you told me I wouldn't even try this on my own haha. It did the trick, thanks !
     
    Kurt-Dekker likes this.