• 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 AntLewis · Jun 09, 2013 at 04:37 PM · textureuv

UV issues - help!

Hi guys, I've generated an object at run time and having issues with texturing it, down to UV's I believe. I'm using the code below to generate the UV"s

     private void SetTextureCoordinates()
     {
       Vector2[] uvs = new Vector2[newVertices.Length];
         int i = 0;
         while (i < uvs.Length) {
             uvs[i] = new Vector2(newVertices[i].x, newVertices[i].z);
             i++;
         }
       
         mesh.uv = uvs;
     }

But the results I'm getting are odd (see attached screen shot). Anyone any ideas where I"m going wrong?

Thanksalt text

uv.jpg (511.3 kB)
Comment
Add comment · Show 7
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 AntLewis · Jun 10, 2013 at 10:16 AM 0
Share

Anyone :-)?

avatar image saschandroid · Jun 10, 2013 at 10:49 AM 1
Share

It is difficult to say what you are doing wrong if you don't tell us how your object should look like. :)

avatar image AntLewis · Jun 10, 2013 at 11:06 AM 0
Share

Ha very true :-) Well as far as I understand with the code above, each uv is matches to each vertex, so I'd imagine that the texture would have been rendered across one quad - in the screen shot above it seems to be tiling it 3 times horizontally, and 8 times vertically (or thereabouts).

avatar image Graham-Dunnett ♦♦ · Jun 10, 2013 at 11:30 AM 1
Share

How large is your object in world coordinates. UVs are usually in the range 0..1, and your object might be bigger than that, maybe even 3 units in x and 8 units in z.

avatar image AntLewis · Jun 10, 2013 at 12:09 PM 0
Share

Its approximately 100 units across, around 8 in Z and around 30 in Y

Show more comments

1 Reply

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

Answer by AmoralAckbar · Jun 10, 2013 at 03:41 PM

UV Coordinates are set from 0 to 1 across the whole area you want the texture to apply to. As your code is now, you are setting the UV coordinates in full steps based on the physical location of the vertices in space. As a result, you are getting a ton of tiling because the texture thinks that you want it to display fully in each of those increments.

I'm not sure how you want the tiling to work, but let's assume you wanted a single texture to apply across an entire mesh defined by your vertices. In this case, you would want to set the UVs as you go across the mesh based on the % of the total mesh. Something like:

 for (int z = 0; z < maxZ; z++)
   {
   for (int x = 0; x < maxX; x++)
     {
     uvs[i] = new Vector2(x / (maxX - 1.0f), z / (maxZ - 1.0f));
     i++;    
     }
   }

This probably won't work for what you are trying to do, but I am trying to get the idea across. In this case, my UV values are going from 0.0 all the way up to 1.0 in both X and Z directions as you go across the entire mesh. If you wanted to tile differently, you can modify the looping so that the 0.0-1.0 range is repeated in smaller chunks.

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 AntLewis · Jun 10, 2013 at 04:16 PM 0
Share

Thanks so much for taking the time to explain that - much appreciated.

avatar image AmoralAckbar · Jun 10, 2013 at 04:26 PM 0
Share

No problem. Had a hell of a time learning how to mesh and texture in script, so happy to pass along.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Assigning UV Map to model at runtime 0 Answers

SkinnedMeshRenderer offset UVs to match texture atlas? 1 Answer

Blender UV map texture problem? 1 Answer

Weird "ghost" texture issue with Transparent Texture/Shader 2 Answers

RaycastHit textureCoords return Vector2.zero on iOS only despite proper setup 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