Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Cobo3 · Sep 18, 2018 at 09:16 AM · shadershadersshader programminguv

Apply an affine transformation

We have a square plane with a texture on it. We want to be able to specify the shape of the image by defining the coordinates of its 4 corners. Take the following images as examples of what we need. alt text

Please, keep in mind that we need the resulting transformation to be AFFINE. alt text

With the following code, we have gotten really close to achieving what we need, but we still can't figure out what we are doing wrong. If anybody can spot our mistake or give us some pointers on another solution, we would be more than happy.

 //We do this in the fragment function of the shader
 //(x1, y1)...(x4, y4) are the corrdinates of the four points, starting on the upper left corner and going clockwise.

 float2 uv = UV;
 UV.x += (x4 - x1) * uv.y - x4;
 UV.x /= (x3 - x4 + uv.y * (x2 - x1 - x3 + x4));                    
 UV.y += (y4 - y3) * uv.x - y4;
 UV.y /= (y1 - y4 + uv.x * (y2 - y3 - y1 + y4));

Thanks in advance!!

captura.png (204.5 kB)
capturapng2.png (304.4 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BruShEr · Dec 21, 2020 at 11:30 AM

Hello, I know that this is old thread, but the solution for your question is Quadrilateral Interpolation. You can find mathematical solution here: https://www.particleincell.com/2012/quad-interpolation/ (worked for me)

and

http://reedbeta.com/blog/quadrilateral-interpolation-part-2/ (did not work in all my cases)

Comment
Add comment · Show 1 · 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
avatar image JLaiCCS · Feb 18, 2021 at 01:59 AM 0
Share

Hi, can you explain the code used to apply the stuff from the first link? I don't quite understand, and also require affine transformations to be applied to 2D sprites in my project. @BruShEr

avatar image
1

Answer by Bunny83 · Sep 18, 2018 at 10:29 AM

What you're after is not affine mapping. affine transformations keep parallel lines of the source space parallel in the transformed space. See Affine mapping. What you want is a perspective correct mapping which actually causes a non linear transformation.


the rasterizer of the gpu automatically does a perspecive correction based on the perspective distance. Note that this only works for faces which are actually a rectangle but are distorted due to perspective projection. If you want to draw a pure 2d shape you would need to use s,t,r,q texture coordinates. Have a look over here as well as the linked tutorial post from the question

Comment
Add comment · Show 1 · 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
avatar image Cobo3 · Sep 18, 2018 at 10:50 AM 0
Share

Actually, perspective projection is what is shown in the right picture, labeled in red as "Not this". We want to achieve the effect of the left one, the one labeled in green as "We want this".

So, if what we want, which is the left image, is not an affine transformation, then what is it? Because we certainly don't want a perspective transformation.

Thanks!!

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

154 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

Related Questions

Is it possible to avoid writing to certain g-buffers? 1 Answer

Shader issue with a 170 upper angle 0 Answers

UnityObjectToClipPos is inverted? 0 Answers

post shader: reconstruct worldspace plane coordinates 1 Answer

How to make a "Cuphead" shader effect? 1 Answer


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