Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
25
Question by logty · Aug 08, 2011 at 08:23 PM · texturemeshuvssquare

How do UVs work?

I know this seems like a silly question but I have no experience in this area and all I know is that it has something to do with textures. At the moment I am creating a mesh at run time and I am unsure how to texture it exactly with the UVs. I have checked in the Unity script reference but their examples dont seem very explanatory in this area. If you could just tell me how I would put a texture onto a square (2 tri, 4 vert) mesh that would be great, Thanks!

Comment
Add comment · Show 2
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 CHPedersen · Aug 09, 2011 at 02:15 PM 0
Share

Great question, with a great title! Others will be able to easily search for this and find a good explanation below. :) +1!

avatar image $$anonymous$$ · Aug 24, 2014 at 07:39 PM 0
Share

I would very much like to upvote this question (if i had the rep for it.) Thanks for asking!

1 Reply

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

Answer by equalsequals · Aug 08, 2011 at 08:58 PM

UV(W) coordinates are a normalized (0 to 1) 2-Dimensional coordinate system, where the origin (0,0) exists in the bottom left corner of the space. UV is in this case synonymous to XY, but since XY is used in the XYZ 3D space so we use UVW in the texture space to avoid confusion.

Note that there is a W which corresponds to the Z axis, but it won't be used in a Texture2D as it is 2D.

So with that said, think of this 0 to 1 space as 0% to 100% of the image. If your texture is 128x128 then having a coordinate of 1 in either the U or V would mean that that particular vertex will exist at the normalized bounds of our texture space.

Example: a vertex with a UV of .5,.5 will be in the exact center of the image.

This works for non-square as well. Example: an image of 512,128 and a vertex with a UV coordinate of .5,.5 will exist at 256,64 in our texture.

Now, you asked how a 4vert quad would look, in short it would look something like this:

top-left 0,1 top-right 1,1 bottom-left 0,0 bottom-right 1,0

Depending on how you've assembled your triangles will vary on what vertex is what, but follow those guidelines and you should be able to figure it out.

[Edit] Here is a quick class that builds a quad. I did this step by step and tried to avoid a lot of the short hand I'd normally use to be as decipherable as possible.

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
 public class DrawQuad : MonoBehaviour 
 {

     // Use this for initialization
     void Start ()
     {
         Mesh mesh = new Mesh();
     
         Vector3[] vertices = new Vector3[4];
         vertices[0] = new Vector3(0,0,0); //top-left
         vertices[1] = new Vector3(1,0,0); //top-right
         vertices[2] = new Vector3(0,-1,0); //bottom-left
         vertices[3] = new Vector3(1,-1,0); //bottom-right
         
         mesh.vertices = vertices;
         
         int[] triangles = new int[6]{0,1,2,3,2,1};
         mesh.triangles = triangles;
         
         //this is also acceptable!
         //mesh.SetTriangleStrip(new int[4]{0,1,2,3}, 0);
     
         Vector2[] uvs = new Vector2[4];
         uvs[0] = new Vector2(0,1); //top-left
         uvs[1] = new Vector2(1,1); //top-right
         uvs[2] = new Vector2(0,0); //bottom-left
         uvs[3] = new Vector2(1,0); //bottom-right
     
         mesh.uv = uvs;
     
         Vector3[] normals = new Vector3[4]{Vector3.forward,Vector3.forward,Vector3.forward,Vector3.forward};
         mesh.normals = normals;
     
         //you could also call this instead...
         //mesh.RecalculateNormals();
     
     
         //grab our filter.. set the mesh
         MeshFilter filter = GetComponent<MeshFilter>();
         filter.mesh = mesh;
     
         //you can do your material stuff here...
         //MeshRenderer r = GetComponent<MeshRenderer>();
         //r.material = new Material(Shader.Find("my_shader_here"));
     
     }
 }

Hope that helps.

==

Comment
Add comment · Show 12 · 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 logty · Aug 09, 2011 at 12:43 AM 0
Share

Thanks, this is really helpful, the only problem is I am having a tough time getting this to work, is there any way you could give a scripting example?

avatar image Peter G · Aug 09, 2011 at 03:23 AM 2
Share

Good answer. Its worth adding that uvs outside of the (0,0) to (1,1) range will repeat the texture as long as clamping is off.

avatar image equalsequals · Aug 09, 2011 at 01:30 PM 0
Share

@Peter G Indeed. @logty will revise with a code snippet.

avatar image CHPedersen · Aug 09, 2011 at 02:16 PM 0
Share

Great answer! :) +1

avatar image logty · Aug 09, 2011 at 03:27 PM 0
Share

Thanks so much for this code, this is really helpful! A few more quick questions (if it is not to much of a hassle), How would combine multiple of these together? How do the triangles work exactly? What I am trying to get at is if I want to add other faces how would I go about doing that? Thanks again!

Show more comments

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

12 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

Related Questions

Rounding Error in Texture Creation? 0 Answers

How to get player's face onto character mesh 1 Answer

Setting Texture on Terrain at x,y coord in runtime 1 Answer

2 Textures on a single mesh 1 Answer

Best Technique for Generating Textures in Realtime 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