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 /
avatar image
0
Question by ajk48n · Dec 01, 2014 at 04:20 AM · shaderaspect-ratioperspectivescreensize

Vertex shader coordinates in clip space

I've constructed a matrix that calculates a 4-corner pin perspective transformation. The matrix is calculated in a script, and then passed on to a CG shader.

I've got 4 cubes that represent the 4 corners of my plane, and as they are translated the matrix gets updated and passed to the shader.

At the moment, everything works perfectly when the aspect ratio of the camera is square, and my 4 control points are located at (-1, -1), (-1, 1), (1, 1), and (1, -1). Now I believe this is working because the vertex shader is using coordinates in clip space which run from -1 to 1.

The problem comes when I have an aspect ratio of the camera that is not square. The control points no longer match up with the clip space values, and I get the wrong scaling on my perspective transform.

I'm pretty sure I need to factor in _ScreenParams somewhere in either the shader or script code in order to re-adjust the position values, but I'm not sure how exactly to go about this.

My shader code is as follows:

 v2f vert( appdata_img v )
 {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     o.uv = v.texcoord;
 
     o.pos = mul(H, o.pos);
     return o;
 }

Thanks for any help

alt text

perspective-distortion.jpg (91.8 kB)
Comment
Add comment · Show 1
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
avatar image PAHeartBeat · Dec 01, 2014 at 05:44 AM 0
Share

Hi,,

If you want to use _ScreenParams, it should in shader, but I have limited knowldge of shader so I want know how you are calulate matrix "H" in your script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ajk48n · Dec 01, 2014 at 06:24 AM

Well I've figured this out, so just for anyone else who is interested, here is the solution.

This relies on a combination of adjusting the original transformation matrix by the aspect ratio, as well as adjusting the vertex.x position in the shader.

My matrix was of the form:

 x0, y0, 1, 0, 0, 0, -u0*x0, -u0*y0
 0, 0, 0, x0, y0, 1, -v0*x0, -v0*y0

and needed to be adjusted to:

 aspect = (float)Screen.width / (float)Screen.height;
 
 x0 = x0/aspect;
 
 x0, y0, 1, 0, 0, 0, -u0*x0, -u0*y0
 0, 0, 0, x0, y0, 1, -v0*x0, -v0*y0


This will compensate the matrix for the aspect ratio, but the shader itself will be too wide/narrow now. So in the shader, the opposite compensation needs to occur, using:

 v2f vert( appdata_img v )
 {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     o.uv = v.texcoord;
     o.pos = mul(H, o.pos);
                 
     o.pos.x /= (_ScreenParams.x / _ScreenParams.y);
                 
     return o;
 }


Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Input.mouseposition affected by game scene size 2 Answers

Dynamic, warped text 0 Answers

Sphere appears to elongate further away I get 1 Answer

How do I Keep game objects on the screen proportional While building games for different aspect ratios? 0 Answers

Is that possible to create my own custom projector using unity 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