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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. 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);