x


Tiling UV mapping?

My screenshot describes my question clearly (better than words I think)

alt text

So, is it possible to have more control with the UV mapping and decide which portion of the texture to map on the Mesh (in a tiling style).

more ▼

asked Jul 31 '12 at 02:03 PM

the_scholar gravatar image

the_scholar
78 2 6 10

and your point being?

Jul 31 '12 at 03:02 PM BeB_Wir3

@BeB_Wir3: Don't post such comments as an answer! Answers should answer the question.

I've converted your answer into a comment

Jul 31 '12 at 03:03 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

At this point are you familiar with the uv array? http://docs.unity3d.com/Documentation/ScriptReference/Mesh-uv.html

which is just part of Mesh http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html

Based on what you have described: Just play with uv values of 0.0, 0.5 and 1.0.

You'll soon get the idea! Your image just goes from 0 to 1 in each direction, it's that simple. Your quadrants are just the obvious. DRAW A DIAGRAM of your eight triangles and start setting the uvs. Enjoy!

if you get big into vertices consider this fount of interesting information and downloads, etc etc http://answers.unity3d.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html

Now here's a diagram:

alt text

large image: CLICK HERE TO SEE DIAGRAM PROPERLY

So, say you want your ELEPHANT to appear in area D. The elephant obviously enough exists at the coords I have written here...

alt text

Your area D is two triangles (7 and 8). So .......... there are six verts (two triangles ... 2x3 = 6 !!) Just set the six verts to those values!! 0,,5 etc. It's that simple !!!

OF COURSE as Owen points out, this "hello mesh" exercise only works if everything is completely regularly-spaced and so on. Enjoy!

Helpful notes

  1. If you're learning to make procedural mesh ... why bother? There's always an easier way! :)

  2. When you're making procedural mesh, DO NOT BOTHER trying to make "shared" vertices in the vertices array.

  3. When making procedural mesh, it is (typically) poor practice to bother trying to share the verts in the vert array.

  4. Don't forget to play with the normals too! Try adding a torque to the normals and precessing them!!

  5. If, ignoring point 1, you decide to get in to making mesh. It is utterly critical to remember. When it comes to doing the actual weave, YOU MUST DRAW A DIAGRAM. Please do not ignore this point for your own sanity!

  6. Read point 5 over and over.

  7. Don't forget thee tris are CLOCKWISE in unity. I think.

  8. It is critical that one fully internalises the following......

  9. http://answers.unity3d.com/questions/266972/detecting-mesh-orientation.html (thank God for Whydoidoit)

  10. it is critical one understands the common misunderstanding even sophisticated people make, which is explained in extravagant length here

  11. http://answers.unity3d.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html

  12. Triple-check - have you addressed point 6?

  13. Also address point 5 !!

  14. The hitchhiker's guide suggests that anyone who weaves mesh prefers a Canson "Croquis" 90g/m2 sketch block (the one with the orange cover) for drawing diagrams - I have one of the rare A3s just now, it's great. Of course, you could use any sketchpad, but you know what Einstein said. Enjoy!

ele.jpg (34.5 kB)
bignotepad.jpg (201.3 kB)
more ▼

answered Jul 31 '12 at 04:03 PM

Fattie gravatar image

Fattie
18.9k 57 86 146

Yeah, I'm sorry for the answer. The Unity team should make the comment versus answer thingie a little more obvious for new members :D I suppose that even for old members it's easy to make a mistake and use the comment box to comment normally. :)

Fattie, you're right. I'm greatly interested in the 'Hello Mesh' learning process.

I kind of understand your diagram very perfectly (theatrically) but programming-wise I'm still confused how things are connected together.

On your example, you refer the uv array having a relationship with the triangle array. I thought the uv array was mainly having a relationship with the vertices array from the Mesh class.

Besides, for what I want to accomplish I'm not intending to use a 3d software to build the models and textures. I need to build everything dynamically inside Unity. That's why I have to understand the Mesh stuff as perfectly as possible. I also googled keywords like "mesh programming" and I didn't find anything sadly.

Is it possible for you to code a small demo (very simple to understand) using the script based on my image or your diagram?

Thank you very much for all your support! I really appreciate it!

Aug 01 '12 at 08:28 AM the_scholar
(comments are locked)
10|3000 characters needed characters left

It would probably be easier to just use 4 planes, then 4 materials (one for each numeral,) which you swap in and out. Other approaches might squeeze out a little more speed, but I not much and you probably won't need it.

Moving around UVs can easily stretch your texture, flip it, repeat ... . But to have a "non-continuous" texture, like the four 1's, all of the interior verts would need to have been previously split in the modelling program. That's just a basic concept in unwrapping.

If you did that then you could code using mesh to hand-set all 16 UV coords, but a giant pain looking up that the middle-right vert is stored in slot #7, etc... .

You could also write a custom shader, which would work for a 4-vert plane. For example, if(uv.x>0.5 && uv.y<0.5) uv.x-=0.5; would shift the 3 into the 4 slot(?). But there are 256 combinations, and might take several very mathy shades to cover them all.

more ▼

answered Jul 31 '12 at 04:06 PM

Owen Reynolds gravatar image

Owen Reynolds
11.2k 1 7 45

To elaborate, the hard part of setting the UVs in code is you have to know how the verts were numbered during the export. It's possible the verts making the lower-left square are numbered 0,1,4 and 5; or 3,7,6 and 4; or anything else.

The verts for the other 4 squares may not follow the same pattern, so you can't just reuse code with "vertNum+4".

To find the numbers, you can print out the vert coords in Unity. Lots of verts have duplicate coords, so you have to guess which is part of which square.

But, sure, once you get a 0-15 picture of how the plane verts are numbered, then you just need four lines that look like uv[3]=new float2(0,0.5f); setting uv's for whichever corner of the texture you want.

Jul 31 '12 at 06:20 PM Owen Reynolds

hmm .. export ... ?

it's only 8 triangles, you just make 'em ! you know Owen, I believe he only meant it as a "hello Mesh" learning example

all of what you say is true of course. unfortunately it appears to be the case of a disappearing new user, so another hour wasted

Jul 31 '12 at 07:15 PM Fattie

xD Hello Mesh! brilliant!

Jul 31 '12 at 09:53 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

So, I decided to make the mesh of my plane a bit simpler and made it only one cell rather than four cells.

It seems to work great, only the numeral '2' tile of my image appears on my plane when I play-test Unity.

alt text

So, if I come back to my previous four-cells plane example, it would be impossible to have the same results without adding more vertices (splitting) as Owen instructed?

more ▼

answered Aug 01 '12 at 09:44 AM

the_scholar gravatar image

the_scholar
78 2 6 10

The problem is the 5 inner vertices which are shared by two or four quads. You can only specify one texture coordinate per vertex, so it's not possible to set all four corners of all four tiles to different values.

You have to split the vertices, so each tile have it's own 4 vertices. That means your 2x2 quad mesh which has 9 vertices would need 16 vertices. When you create the mesh procedurally, it's even much easier. Always four vertices in the array will make up one quad. The triangle array will contain blocks of 6 values which always refer to one four-vertices-block.

I can post an example if you really want, but those two answers should be enough ;)

Aug 01 '12 at 10:22 AM Bunny83

not that I'm aware of. For example, the Unity native cube is made of 24 verts, not 8 as it would at first seem. You have more flexibility with manipulating vert UVs, colours and tangents.

Aug 01 '12 at 10:31 AM alucardj

No ;) You have 2 triangles per quad. One triangle needs 3 indices so one quad needs 6 indices. When you have 4 quads (2x2) you would need 4x4 vertices and 4x6 indices. So you need 16 vertices and 24 indices.

Aug 01 '12 at 11:02 AM Bunny83

ThumbUp to everyone on this page from me =]

Aug 01 '12 at 01:06 PM alucardj

If it makes things easier, you can safely think of quads/squares instead of triangles -- 4 verts/quad, total of 16, is just fine. The only time tris come into play is when making the tri array. "Quad" 4,5,9,10 becomes two tris 4,5,9 and 9,10,4 (which is an easy pattern.)

Don't get too hung up on "everything is really tris." Everything is also in binary, but the computer just handles it. Graphics cards gladly take quads as input (a tri-list is the last resort out of 5 ways to list faces, but easy to understand.) The modellers I know prefer to use quads whenever they can.

Also, suggest: vertices[0]=new Vector3(-1,1,0); instead of three lines.

Aug 01 '12 at 02:12 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x236
x28

asked: Jul 31 '12 at 02:03 PM

Seen: 2115 times

Last Updated: Apr 07 at 08:32 PM