Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by kankeus · Feb 09, 2021 at 01:01 PM · camerashadercanvaspost processing

Representing Lens Distortion coordinates in 2D I with my custom lens shader - What am I doing wrong?

Hi,

I am experimenting with something lens related and I have a 'strange' problem:


alt text


Scenario:

  • I have a grid/checkerboard (black/blue) in 3D space to visualize the lens distortion

  • I want to represent the distortion in 2D space (Canvas) by converting the checkerboard from world space to canvas (red grid).

  • The checkerboard and the red grid should match each other.

I have done this by using the same distortion algorithm with my shader and the C# script that generates the red UI grid.


The 2D representation of the camera is working great without my shader, but when I enable the shader, it works as if it is inverted (as seen in the gif above). I have simplified the shader algorithm to show the problem easier here.

The shader logic:


     float2 GetSimpleDistortion(float2 uv) {
         float2 center = float2(0.5, 0.5); // Center of the screen
         float2 d = uv - center; // Distance vector from center
         float r = sqrt(d.x * d.x + d.y * d.y); // Distance from center (hypothenusa)
         float2 newuv = float2(0.0, 0.0);
 
         newuv.x = uv.x + d.x * r * r * _simpleK; // move away from the center
         newuv.y = uv.y + d.y * r * r * _simpleK;
 
         return newuv;
     }
 
 
 
 
     float4 LensEffectFragmentProgram(PostProcessVaryings input) : SV_Target
     {
         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
 
         float2 uv = UnityStereoTransformScreenSpaceTex(input.texcoord);    
         uv = GetSimpleDistortion(uv);    
         float4 color = LOAD_TEXTURE2D_X(_MainTex, uv * _ScreenSize.xy);    

         return color;
     }



And then in my C# script, I am distorting the red UI grid with the same algorithm:


     private Vector2 GetSimpleLensDistortion(Vector3 viewPort, MQMLensEffect _lensEffect)
     {
         Vector2 uv = new Vector2(viewPort.x, viewPort.y);
 
         Vector2 center = new Vector2(0.5f, 0.5f); // Center of the screen
         Vector2 d = uv - center; // Distance vector from center
         float r = Mathf.Sqrt(d.x * d.x + d.y * d.y); // Distance from center (hypothenusa)
         Vector2 newuv = Vector2.zero;
 
         newuv.x = uv.x + d.x * r * r * _lensEffect.simpleK.value; // move away from the center
         newuv.y = uv.y + d.y * r * r * _lensEffect.simpleK.value;
 
         return newuv;
     }
 



And this is how I move the UI grid:

         for (int x = 0; x < sizex; x++)
         {
             for (int y = 0; y < sizey; y++)
             {
                 Vector3 viewport = cam.WorldToViewportPoint(grid3d_points[x,y].position);
                 Vector2 uv = GetSimpleLensDistortion(viewport, _lensEffect);
                 var newViewport = new Vector3(uv.x, uv.y, viewport.z);
                 var canvas_pos =  Vector3.Scale(newViewport, canvasRect.sizeDelta);
                 screen_handles[x,y].anchoredPosition = new Vector2(canvas_pos.x, canvas_pos.y);
            }
        }


I am doing this by converting the 3D grid points to viewport coordinates - which I think should correspond with the shader's UV-coordinates? - and then to canvas coordinates. But somehow they don't match.

The only variable controlling the distortion is a float 'simpleK' - if simpleK == 0, there is no distortion and the grids match perfectly. It feels like it is working almost, but inverted - as in I am watching the effect from 'behind' or something. And actually, if I invert the simpleK (simpleK = -simpleK) variable only in the C# script, it almost works, but with little bit strange scaling.

Here is an gif showing what happens if simpleK = -simpleK in the C# script:


alt text



So the final question is:

What am I missing in my 2D calculations? Something related to camera frustum or some simple factor or what? I am lost.

Help, please.


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

271 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Post process layer doesnt scale to the camera size 1 Answer

Would it be possible to make a post process cell shader like BOTW in Unity? 0 Answers

missing shader. postprocess pass render pass will not execute Error [Solution] 0 Answers

Canvas without Camera? 2 Answers

Move elements in canvas like a moving camera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges