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

Question cmd.DrawMesh and camera.WorldToViewportPoint

Discussion in 'VR' started by AleVerDes, Apr 10, 2023.

  1. AleVerDes


    Jun 16, 2014
    Hi all! I have a problem. I'm writing a custom simplified Lens Flare for VR, and I just want to draw a texture at certain coordinates.

    I used the standard example
    and it worked, it only worked in the editor.

    I modified the code and now I pass the eye index to the custom shader (in the shader itself I do the division into the left and right eyes using unity_StereoEyeIndex). The shader works fine, there are no questions to it.

    But the question arose about the transmission of Viewport coordinates - in VR it behaves inadequately and draws LensFlare only in Quaternion.identity rotation and in some undefined coordinates. Moreover, I tried to pass to camera.WorldToViewportPoint(lensFlare.transform.position, eye) and eye = Camera.MonoOrStereoscopicEye.Mono and normal eyes, everything is the same.

    What am I doing wrong?

    Code (CSharp):
    1. ref var cameraData = ref renderingData.cameraData;
    2. var camera =;
    4. cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
    5. var scale = new Vector3(1, camera.aspect, 1);
    Code (CSharp):
    1. var position = camera.WorldToViewportPoint(lensFlare.transform.position, eye) * 2 -;
    2. position.z = 0;
    4. var matrix = Matrix4x4.TRS(position, Quaternion.identity, scale * lensFlare.Scale);
    5. cmd.DrawMesh(_mesh, matrix, _lensFlareMaterial, 0, 0);