unfold/unwrap

Blitz3D Forums/Blitz3D Programming/unfold/unwrap

b32(Posted 2006) [#1]
Is is possible, when I import a mesh that has no UV coords to generate them by unfolding or unwrapping ?
I would like to do this by using code, so I can generate UV coords while the program is running.
If it is possible, how should I do this ? Is there any good information on the net somewhere ?


Beaker(Posted 2006) [#2]
What kind of model is it? Character, level or other?

There is some code in the archives for simple mapping types: sphere, cubic etc.
http://www.blitzbasic.com/codearcs/codearcs.php?code=276
http://www.blitzbasic.com/codearcs/codearcs.php?code=277


b32(Posted 2006) [#3]
Thanks for the examples. I was mainly focussing on unfolding meshes, so I didn't saw them yet.

I would like to use the routine in a texpaint like painting program. Paintmesh is a Blitz3D example (I believe it was also written by birdie) where you can paint to the surface of an object.

In my program, I would like the users to load any .X or .3DS mesh available.

I noticed sometimes meshes have no texture coordinates. For instance the program LDraw exports its LEGO building blocks without texture coordinates.
I would like my program to generate texture coordinates in such a case, so the object could still be painted on.

Suppose a mesh is made out of paper. Then they could be unfolded to a flat surface. I would like to generate texture coordinates in that way, but my mathematical skills are a bit insufficient at this point.

I was thinking about single meshes. Unfolding an entire gamelevel would be cool, however my guess would be, complicated as well. Unfolding (or unwrapping?) single meshes would be quite a big step for me allready.

In the meanwhile, I tried to write a cubemapping routine.
I think it could also be a solution allready, however I am still interested in unfolding meshes. I believe this technique is called "unwrapping uwv" coordinates ? Some programs seem to be able to do this, but I would like to know how it's been done ?

Since birdies cubemapping example is not in the public domain, here is mine. One may use it in any form.
This code is yet unfinished. With more complicated meshes, it generates overlapping texture triangles. The idea is to translate the overlapping triangles to another part of the texture.




Beaker(Posted 2006) [#4]
Have you seen Tattoo (made in Blitz3D)?
http://www.terabit.nildram.co.uk/tattoo/
..it relies on existing UVs.

There are many unwrap algorithms, and none of them simple, so good luck. Levels aren't so problematic cos you can use a cubic routine (probly not for painting tho), and don't have as many problems with mesh splits.

(use [ codebox ] instead for code above)


b32(Posted 2006) [#5]
Nice program! That would be the general idea, only a bit more basic I suppose. The idea of using a spraycan could be a nice feature to make nice, smooth textures.

Do you know where to find more information about unwrapping uwv by code ?
To unwrap a surface, I thought about doing the following:

(1) start of at a certain triangle and rotate it, until its normal faces forward.
(2) Then take every 2 points of the triangle, and take any other 3rd point that is connected to it. Rotate that triangle until its normals matches the first one.
Then use a recursive structure (or simulate it with a type) to unfold the entire mesh in all directions, until the "next" triangle found is the first one. (the one I started with)

edit:
Well, I did started on this, and it seems to be the first step towards unwrapping.
I found some nice information about the process here:
http://www.3dtotal.com/ffa/tutorials/max/UVW_mapping_an_object/UVW_mapping_an_object1.asp

If anybody still has any suggestions, they are welcome. In the meanwhile I will look further into it.


b32(Posted 2006) [#6]
Could someone please have a look at my first attempt to unwrap UV's to see if it's going into the right direction ?
It rotates each triangle towards the front and uses one of the previous rendered coordinates to shift it.
(It only works on a part of a Sphere at the moment, but it's the main idea that counts.)

Anybody any idea if this could go into the right direction ?


jfk EO-11110(Posted 2006) [#7]
I think there is no generic unwrapping way, instead choose one that fits your needs best from eg:

-spherical
-cylindrical
-box
-plane
-decal
-view

I take it what you are trying to do is spherical mapping. This may be tricky when your mesh has concave parts, you will have to shrink the surrounding nonconcave tris to allow unfolded concave tris.

Also, it may be simple to unfold a uniform grid made of quads, but unfortunatly meshes like characters etc. usually are made of very nonuniform grids.


b32(Posted 2006) [#8]
Ah thanks for clearing this up, jfk. You helped me the other time with a question on image pointers and RTLMoveMemory. Thanks again.

Spherical wrapping, is that calculating two angles from the center to a points and using them as u# and v# ?
I wrote something like that and also a cubemapping code, but like you said, they don't seem fit for every mesh.

I was still wondering if there is some classical solution for this problem. Hmm, shame there isn't.

This "decal" unwrapping sounds interesting. How about LSCM or pelt unwrapping? I read about that in the description of Blender.

What I find so frustrating is that in real life, unwrapping can be done easily. I have made a few paper models to study on. I still think writing an algorithm should be obtainable somehow.
I can imagine some shapes can not be unfolded without overlapping triangles. But I thought about checking for overlaps and avoiding it by moveing one of the parts somewhere else on the texture.

At first, I am looking for something like this:


I am thinking along two lines:

Based on triangles:
If I start of with just one triangle. Then I rotate the mesh so, that this triangle points forwards.
Then I fix this triangle and move on to all triangles that are connected. The connected spline becomes the rotation axis and the rest of the mesh rotates around it until this face is also pointing forward.
I repeat this for all triangles, until there are no more triangles connected, or the original triangle is reached or the triangle found is allready locked.

A second idea is based on vertices:
I could also calculate the distances from each vertex to another and then use this distance table to calculate the texture vertices.

However, this seems to be a very difficult calculation and I'm not even sure if it can be done.

So, this would bring my mathematical part of the question back to this:

I have three points.
If I know the coordinates of two points and I know for every point it's distance to the other two, can I calculate the (x,y)- coordinate of the 3rd one ?


jfk EO-11110(Posted 2006) [#9]
yes you can, it's the intersection point of two circles with a radius that is the same as the distances. I ain't got the code right now, but maybe there is something in the code archives.

Your unfolding method that creates a stripe based on the connection of 2 of 3 vertices is fascinating, tho you will not be able to paint them in a natural way, compared to say cylindrical mapping.

For a simple solution you could also take the x,y,z coords and use sqr((x^2)+(z^2)) for U and y for V (of course proportionally calculated down from the meshsize to 1.0).


b32(Posted 2006) [#10]
Ah, I see. "Intersecting circles" on google gave me some interesting mathematical information.

I think for the meshpainting program, the main issues are: (1) the triangles are not stretched.
(2) the triangles do not overlap.

So if my distance-based idea fails I'll go for a simpler solution and arrange the (normalized, facing front) triangles by their index number from left to right and top-down.
It has a few small disadvantages, but it is a workable (and less complex) solution.
But first, I will study a bit more on this.

For anyone interested, here is the code I wrote for computing the (two) intersection points of two circles.

Input:
x, y coordinates for point A, B and D
distance AB, AC, BC, CD

Output:
cx, cy




Beaker(Posted 2006) [#11]
How will you handle a paint stroke that covers several triangles, that are aligned on the mesh, but aren't actually aligned on your texture? (ie. it jumps a seam).

You should put your circle intersection code in the code archive.


b32(Posted 2006) [#12]
Thanks for the tip! I will do that, but I should edit it a bit first, because now it is a bit too case specific.

And you are right: the seams would be one of the main issues of this method. The idea was clipping the brush onto the triangle.
An idea would be this: still clipping, but then drawing the brush on the second triangle too.

With the intersection code, I'm heading for this:

(1) I compute a distance matrix from the 3d model's vertices like this:
-At the first step I calculate only direct distances. (from a vertex to all vectices that are connected to it)
-I add these direct distances to calculate all indirect distances. (vertices that are only indirectly connected, via another vertex)

Then I start off with any two vertices v1 and v2.

I map v1 at (0, 0)
and v2 at (0, distance(v1, v2))

I generate a third point v3 based on v1 and v2.
There are two options for it, I just pick one.

Then I have coordinates for three points and a distance table and I am sort of hoping I can use the algorithm above to compute the other coordinates.
At the end I would have a 2D mapped surface, that I can rescale and rotate to fit a texture.


b32(Posted 2006) [#13]
The distance-based method didn't work so far. However the other method gave me some results, so I thought I post it.