Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Muzz5 · Jun 13, 2011 at 08:38 PM · meshproceduraluvoutofbounds

Procedural Mesh UVing

When you're making a mesh from hand, (see here), how does the mesh.uv buisness work? Because if you've got an array, which you've got from generating thousands of vertices, then I'm not writing out Vector2(x,y) thousands of times! What am I meant to do? How can I split up my array, perhaps?

EDIT: That wasn't very clear. What I mean is, I have this compiler error: Mesh.uv is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array. In the mesh example, you appear to have to write out the Vector2 statement as many times as there are vertics. I'm looking for a way to do this on the fly.

Comment
Add comment · Show 3
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 Jessy · Jun 13, 2011 at 08:46 PM 2
Share

What are you talking about, "mesh.uv business"? Every case is going to be different. You generate the UVs in the cleanest way possible. The array is generally going to be procedurally generated, with you feeding it some kind of ruleset which generates the Vector2's in at least one loop. This isn't unique to UVs; this happens with any kind of substantial array.

avatar image Bunny83 · Jun 13, 2011 at 09:31 PM 0
Share

What do you mean by "on the fly". You have to create a Vector2[] with the same size of your vertices array. Do you want to use the x ans y vertex coordinates as UVs? Just use a for loop and copy them....

avatar image Bunny83 · Jun 13, 2011 at 09:40 PM 0
Share

I've added an example to my answer if that's what you wanted to do.

2 Replies

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

Answer by Bunny83 · Jun 13, 2011 at 08:54 PM

The Mesh class gives you 6 arrays:

  • .vertices

  • .colors

  • .normals

  • .tangents

  • .uv

  • .uv2

to specify your vertex-data. Each of those array, if they are used, have to have the same size as the vertices array. So colors[4] will be the vertex color of your 5th vertex and belongs to the position in vertices[4].

Those 6 arrays work as one array and define your vertices.

The triangles array hold indices to those arrays above to form triangles. The size must always be a multiple of 3 (since a triangle have 3 corners).

I'm not quite sure what's your actual problem... Do you have trouble to fill your uvs? Noone can help you here. UVs are normally created by the artist during unwrapping. If you create all dynamically you have to know how you need them...


An example to use the x,y coordinates as UVs:

 var myVectices : Vector3[]; // you have created your vertices already and stored in this array
 
 var myUVs : Vector2[] = new Vector2[myVectices.Length]; // Create array with the same element count
 for(var i = 0; i<myVectices.Length; i++)
 {
     myUVs[i] = Vector2(myVectices[i].x,myVectices[i].y);
 }

It doesn't make much sense in most cases to use the x,y values directly (except they are within 0..1 or you have a tiled texture)

Comment
Add comment · Show 4 · 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 Bunny83 · Jun 13, 2011 at 09:39 PM 0
Share

Ohh and make sure you assign your vertices array first! If you assign the UV array to your mesh before you've set the vertices array you will get an out-of-bounds error like explained in the docs...

avatar image Muzz5 · Jun 13, 2011 at 09:46 PM 0
Share

Sorry if I'm being really n00bish today, but don't I need to iterate through the array first? Because otherwise I'll get the 'x is not a member of Vector3[]' error?

avatar image Bunny83 · Jun 13, 2011 at 10:42 PM 0
Share

$$anonymous$$y bad! I've changed it ;)

avatar image Muzz5 · Jun 14, 2011 at 07:21 AM 0
Share

Thanks! Upvoted.

avatar image
0

Answer by hellcats · Jun 13, 2011 at 08:51 PM

Just use a for loop (assuming you have some kind of way of generating your uv coordinates for a given x,y,z vertex location). I just used Sin and Cos here.

 var uv = new Vector2[1000];
 for (var i = 0; i < 1000; i++) {
     uv[i].x = Mathf.Sin(i / 1000.0f);
     uv[i].y = Mathf.Cos(i / 1000.0f);
 }
 
 mesh.uv = uv;
Comment
Add comment · Show 5 · 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 Bunny83 · Jun 13, 2011 at 08:58 PM 1
Share

Nice example but $$anonymous$$athf.Sin/Cos takes an angle in radian not a value between 0 and 1 ;)

avatar image hellcats · Jun 14, 2011 at 02:16 AM 0
Share

Ah, but a value between 0 and 1 still is a radian :) I was just trying to make some smoothly varying values for u and v.

avatar image Bunny83 · Jun 15, 2011 at 12:43 AM 0
Share

:D sure it is but doesn't make much sense :D

Of course it's just a sample ;)

avatar image Javity · Mar 23, 2014 at 06:26 PM 0
Share

Isn't there an un-guaranteed order into your cached array of vertices? I mean, when you reference .mesh.vertices, [0] isn't necessarily going to be the "first" vertex is it? There really isn't a first vertex at all is there - the order into the array will vary per mesh eh? This issue has thrown me off where the .uvs array is concerned and the need to know which index the 0,0 uv is in and so forth. This issue gets off into uv issues I suppose, but I admit confusion as the need to "know" the order of placement into any given array so you "know" which uv you are working with in order to animate or use for re-cropping a texture.

avatar image jankymidget Javity · Jul 10, 2017 at 08:33 PM 0
Share

Unless the engine rearranges the elements of the array, arrays will always be consecutive within the memory space allocated.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

UV Mapping a Sphere at runtime 1 Answer

Manual UV-Mapping of a Primitive Cube 1 Answer

Mesh.uv is out of range 1 Answer

Procedural Mesh/UV 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