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 Sondre-S · Jan 21 at 09:14 PM · uv mappingquad

NEED HELP with UV mapping , skewed quad - how to make it horizontal [SOLVED]

this script creates a skewed quad, but it only works if the quad is vertical. How can I make the UV mapping work it the quad is horizontal?.

source: https://github.com/brandonjmatthews/unity-skew-quad/tree/master/Assets/SkewQuad

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public enum Mapping {
     SKEW,
     AFFINE,
     CLIP
 }
 
 [ExecuteInEditMode]
 public class skewQuad: MonoBehaviour {
     public Mapping mapping = Mapping.SKEW;
     public Vector3 TopLeft;
     public Vector3 TopRight;
     public Vector3 BottomLeft;
     public Vector3 BottomRight;
 
     Mesh mesh;
     MeshFilter mf;
 
     
     void Start () {
         mesh = new Mesh();
         mf = GetComponent<MeshFilter>();
 
     }
 
     void Update()
     {
         UpdateMeshAndTexture();
 
     }
 
     public void UpdateMeshAndTexture() {
         bool updated = false;
         // Build the Quad
         Vector3[] vertices = new Vector3[4] {
             BottomLeft,
             BottomRight,
             TopRight,
             TopLeft
         };
 
         mesh.vertices = vertices;
 
         int[] triangles = new int[6] {
             0,2,1, 
             0,3,2
         };
 
         mesh.triangles = triangles;
 
         Vector3[] normals = new Vector3[4] {
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward
         };
         mesh.normals = normals;
 
         Vector2[] uv = new Vector2[4] {
             new Vector2(0,0),
             new Vector2(1,0),
             new Vector2(1,1),
             new Vector2(0,1),
         };
 
 
         Vector2[] uv2 = new Vector2[4] {
             new Vector2(1,0),
             new Vector2(1,0),
             new Vector2(1,0),
             new Vector2(1,0),
         };
 
         switch(mapping) {
             case Mapping.SKEW:
                 // I used uv (qu, qv) and uv2 (q), there is potential for this to be an issue on different devices. 
 
                 float ax = vertices[2].x - vertices[0].x;
                 float ay = vertices[2].y - vertices[0].y;
                 float bx = vertices[3].x - vertices[1].x;
                 float by = vertices[3].y - vertices[1].y;
 
                 float cross = (ax * by) - (ay * bx);
 
                 // Only skew if it's possible (ie. clockwise tri vertices and convex shape)
                 if (cross != 0 && mapping == Mapping.SKEW){
             float cy = vertices[0].y - vertices[1].y;
                     float cx = vertices[0].x - vertices[1].x;
 
                     float s = (ax * cy - ay * cx) / cross;
             
                     if (s > 0 && s < 1) {
                         float t = (bx * cy - by * cx) / cross;
 
                         if (t > 0 && t < 1) {
                             float q0 = 1 / (1-t);
                             float q1 = 1 / (1-s);
                             float q2 = 1 / t;
                             float q3 = 1 / s;
                             uv[0] = new Vector2(uv[0].x * q0, uv[0].y * q0);
                             uv[1] = new Vector2(uv[1].x * q1, uv[1].y * q1);
                             uv[2] = new Vector2(uv[2].x * q2, uv[2].y * q2);
                             uv[3] = new Vector2(uv[3].x * q3, uv[3].y * q3);
                             uv2[0] = new Vector2(q0, 0);
                             uv2[1] = new Vector2(q1, 0);
                             uv2[2] = new Vector2(q2, 0);
                             uv2[3] = new Vector2(q3, 0);
                             updated = true;
                         }
                     }
                 }
 
                 if (!updated) {
                     uv[0] = vertices[0];
                     uv[1] = vertices[1];
                     uv[2] = vertices[2];
                     uv[3] = vertices[3];
 
                     uv2 = new Vector2[4] {
                 new Vector2(1,0),
                 new Vector2(1,0),
                 new Vector2(1,0),
                 new Vector2(1,0),
                          };
                     
                   throw new System.Exception("SKEW: Shape must be convex and triangle vertices must be clockwise.");
                 }
         break;
         case Mapping.CLIP:
             uv[0] = vertices[0];
             uv[1] = vertices[1];
             uv[2] = vertices[2];
             uv[3] = vertices[3];
 
             uv2 = new Vector2[4] {
                 new Vector2(1,0),
                 new Vector2(1,0),
                 new Vector2(1,0),
                 new Vector2(1,0),
             };
 
         break;
         case Mapping.AFFINE:
         break;
         }
 
         mesh.uv = uv;
         mesh.uv2 = uv2;
         mf.mesh = mesh;
 
     }
 }
 
 
 
 
 
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

1 Reply

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

Answer by Eno-Khaon · Feb 07 at 07:22 PM

I'm not in a position to verify my script examples (so there'll be some untested code here), but it looks like the axis-fixing occurs on these six lines:

 float ax = vertices[2].x - vertices[0].x;
 float ay = vertices[2].y - vertices[0].y;
 float bx = vertices[3].x - vertices[1].x;
 float by = vertices[3].y - vertices[1].y;
 // ...
 float cy = vertices[0].y - vertices[1].y;
 float cx = vertices[0].x - vertices[1].x;


With that in mind, you should be able to rectify this by defining axes rather then using x/y as-is. If you have a skewed quad, that shouldn't be too unreliable under the defined creation criteria ("SKEW: Shape must be convex and triangle vertices must be clockwise."), so you can use vertices [0/1/3] to define the axes.

Because the vertices are defined as...

 Vector3[] vertices = new Vector3[4] {
     BottomLeft,
     BottomRight,
     TopRight,
     TopLeft
 };

... we know that [1]-[0] is "horizontal" and [3]-[0] is "vertical" relative to this mesh. That said, we aren't necessarily expecting the mesh to be square, so those two vectors won't necessarily be perpendicular to each other. On that basis, one axis needs to be treated as absolute where the other is constructed based on the available information.

 Vector3 xAxis = (vertices[1] - vertices[0]).normalized;
 Vector3 yBase = (vertices[3] - vertices[0]);
 Vector3 xyCross = Vector3.Cross(xAxis, yBase);
 // Note, this can/will fail to give a viable vector if
 // xAxis and yBase are parallel
 Vector3 yAxis = Vector3.Cross(xyCross, xAxis).normalized);


Now, with the axes (xAxis and yAxis) established (and normalized), they can be substituted in place of "vertices[].x" and "vertices[].y":

 float ax = Vector3.Dot(vertices[2] - vertices[0], xAxis);
 float ay = Vector3.Dot(vertices[2] - vertices[0], yAxis);
 float bx = Vector3.Dot(vertices[3] - vertices[1], xAxis);
 float by = Vector3.Dot(vertices[3] - vertices[1], yAxis);
 // ...
 float cy = Vector3.Dot(vertices[0] - vertices[1], yAxis);
 float cx = Vector3.Dot(vertices[0] - vertices[1], xAxis);
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 Sondre-S · Feb 08 at 12:10 AM 0
Share

Thank you. That works perfectly!

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

132 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

Related Questions

set quad texture to tile from tileset ? 1 Answer

Sprite animation on quad with UV-coordinates 1 Answer

How to create 1x1 quad asset? 2 Answers

i have create a Quad but when my object is moving the camera is still there . how to move camera with the object ? 1 Answer

how does the animation window work in unity 5 2 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