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

    AleVerDes

    Joined:
    Jun 16, 2014
    Posts:
    35
    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
    https://docs.unity3d.com/Packages/c...tainers/create-custom-renderer-feature-1.html
    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 = cameraData.camera;
    3.  
    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 - Vector3.one;
    2. position.z = 0;
    3.  
    4. var matrix = Matrix4x4.TRS(position, Quaternion.identity, scale * lensFlare.Scale);
    5. cmd.DrawMesh(_mesh, matrix, _lensFlareMaterial, 0, 0);