Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

SceneView.onSceneGUIDelegate and Camera MVP Matrix

Discussion in 'General Graphics' started by texelated, Mar 9, 2015.

  1. texelated

    texelated

    Joined:
    Oct 6, 2014
    Posts:
    2
    I'm currently trying to compute the proper matrix for rendering a 2d square onto the screen inside the callback of onSceneGUIDelegate. I am using this callback because I'm working from the EditorWindow class.

    I'm computing the matrix currently as follows:
    Code (CSharp):
    1.  
    2. testMesh.vertices = new Vector3[] {
    3.    new Vector3 ( 5.0f, 5.0f, 0),
    4.    new Vector3 (-5.0f, 5.0f, 0),
    5.    new Vector3 (-5.0f, -5.0f, 0),
    6.    new Vector3 ( 5.0f, -5.0f, 0)
    7.  };
    8. testMesh.normals = new Vector3[] {
    9.    new Vector3 (0, 0, 1),
    10.    new Vector3 (0, 0, 1),
    11.    new Vector3 (0, 0, 1),
    12.    new Vector3 (0, 0, 1)
    13.  };
    14. testMesh.uv = new Vector2[] {
    15.    new Vector3 (0, 0),
    16.    new Vector3 (1, 0),
    17.    new Vector3 (1, 1),
    18.    new Vector3 (0, 1)
    19.  };
    20. testMesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };
    21.  
    22. testMaterial.color = new Color( 1.0f, 0.0f, 1.0f, 0.5f );
    23. testMaterial.SetPass (0);
    24.  
    25. Camera current = Camera.current;
    26. Matrix4x4 model = current.transform.localToWorldMatrix;
    27. Matrix4x4 view = current.worldToCameraMatrix;
    28. Matrix4x4 proj = current.projectionMatrix;
    29. Matrix4x4 mvp = model * view * proj;
    30. //Matrix4x4 mvp = proj * view * model;
    31. Graphics.DrawMeshNow (testMesh, mvp.inverse);
    My testMesh is simply geometry that is from [-5,-5] to [5, 5]. If I simply use the identity matrix I can see the geometry in world space. So I feel my triangles, materials, and uvs are correct. I've tinkered with the following:
    • Trying MVP vs PVM ordering,
    • inverted not inverted matrix
    • flipping the orientation of the triangles so they face the other way.
    Ideally, I'd like the mesh to be rendered in screen space.