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

    AleVerDes

    Joined:
    Jun 16, 2014
    Posts:
    40
    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);