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
7
Question by Kyth'Pth3hk · Aug 16, 2013 at 04:51 PM · uvuv mapping

How does UV mapping via script work?

I've been looking into UV mapping, in order to give different sections of the same mesh different textures, and the UV mapping needs to be done programmatically since the mesh's textures can change at runtime. Although there are already numerous questions and answers on this forum about the topic, I still seemingly cannot understand them. I don't understand how UV's, being an array of Vector2s, could tell how to texture an item. Could someone please explain to me how this works?

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 Owen-Reynolds · Aug 16, 2013 at 07:45 PM 0
Share

You generally don't change UVs when you change textures.

The only time you would is if the textures have different usable areas. Like one is a wood square, but another is bottom half stone and top half junk.

For characters or armour with texture swaps (where the texture isn't just a big tilable square) it's the artist's job to paint each texture using the same UV's.

2 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by robertbu · Aug 16, 2013 at 06:58 PM

UV coordinates specify a specific point on a texture by fractions in the range of 0.0 to 1.0.

alt text

So a value of (0.5, 0.5) would refer to the center-most pixel in a texture. In meshes, we are specifying the uvs for the vertices (which are used to define triangles). So all the pixels inside the triangles are filled by interpolating between the values we specify for the vertices.

So to experiment a bit:

  • Get the CreatePlane script from the UnityWiki.

  • Create a vertical plane but leave all other setting at default. This will create a two-triangle, four-vertices plane.

  • Add a material with a texture.

  • Attach this script:


    pragma strict

    var mesh : Mesh; var uvs : Vector2[];

    function Start() { mesh = GetComponent(MeshFilter).mesh; uvs = mesh.uv; for (var i = 0; i < uvs.Length; i++) Debug.Log(uvs[i]);

    }

    function Update() {

    // Upper right quarter if (Input.GetKeyDown(KeyCode.A)) {

       uvs[0] = Vector2(0.5, 0.5);
         uvs[1] = Vector2(1.0, 0.5);
         uvs[2] = Vector2(0.5, 1.0);
         mesh.uv = uvs;
         }
         
     // Middle quater
     if (Input.GetKeyDown(KeyCode.B)) {
         
         uvs[0] = Vector2(0.25, 0.25);
         uvs[1] = Vector2(0.75, 0.25);
         uvs[2] = Vector2(0.25, 0.75);
         uvs[3] = Vector2(0.75, 0.75);
         mesh.uv = uvs;
         }
     
     //Left half
     if (Input.GetKeyDown(KeyCode.C)) {
         
         uvs[0] = Vector2(0.0, 0.0);
         uvs[1] = Vector2(0.5, 0.0);
         uvs[2] = Vector2(0.0, 1.0);
         uvs[3] = Vector2(0.5, 1.0);
         mesh.uv = uvs;
         }
         
     //A mess
     if (Input.GetKeyDown(KeyCode.D)) {
         
         uvs[0] = Vector2(0.25, 0.55);
         uvs[1] = Vector2(0.0, 0.1);
         uvs[2] = Vector2(0.7, 0.2);
         uvs[3] = Vector2(0.4, 0.6);
         mesh.uv = uvs;
         }
     }
    
    

Pressing the 'A', 'B', 'C' and 'D' key resets the vertices in the mesh.

This simple example doesn't have an object with multiple materials and submeshes. While the uv concept is simple, identifying the right vertices and assigning the right values can be a challenge.


uvs.png (84.8 kB)
Comment
Add comment · Show 2 · 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 haroonalam · Aug 26, 2014 at 02:42 PM 0
Share

isn't "A" refers to Upper Right quarter of texture

avatar image robertbu · Aug 26, 2014 at 02:59 PM 0
Share

@haroonalam - yep. Fixed the text.

avatar image
2

Answer by Joyrider · Aug 16, 2013 at 05:33 PM

Every mesh is composed of 1 or more submeshes, and every submesh has a material ID. So if you have several materials on a mesh, your have several submeshes, each with their material.

Every submesh is composed of triangles, that you can find in the triangle list of your submesh

Every triangle, that is composed of 3 vertices, and each of those vertices have a UV coordinate that correspond to X and Y coordinates in your texture.

so if you know your submesh, you know what material you are using, and thus the texture. And if you know what triangle, you can get the ID of the vertices of that triangle and thus their UV.

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

17 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

Related Questions

Blender to Unity strange normal import problem (bright spots) 1 Answer

Delete Uv2 of a mesh? 1 Answer

Unity procedural mesh generation UV texture flickering horizontal lines 0 Answers

uv layout problem 0 Answers

Normalizing mesh UVS? 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