Search Unity

Need help on UV'ing a procedural mesh

Discussion in 'Scripting' started by innociv, Jan 4, 2010.

  1. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    Before I say the problem I need ot note some things.

    I'm making a space game, and this is for the planets/moons/asteroids.

    Right now it's a segmented cube, but it'll be morphed into a sphere(making a cube-sphere). I figured it'd be best to get the UV'ing done before the morphing into a sphere.

    Then I'll be doing perlin noise and such to make a heightmap, and for the blending of textures.

    So, I'd need to UV it to apply a heightmap and all those textures, correct?

    Can I, and should I, have 6 seperate UV's for each face of the cube? Wouldn't that be easier when it comes to the perlin noise to have all of it different, and still tile? Rather than trying to unwrap onto one and make perlin noise that is seamless when they're all on one square.


    Then comes to.. how? I don't see how to make 6 seperate UV's for one object.
    Also, am I just plotting the UV points like triangles are? WIth this segmented cube, they're square planes, so lines right up with a square UV map. Then it'll stretch to fit seamless and fine when morphed to a sphere?

    Here is an image of it, I put numbers for the vertex positions.


    http://pastebin.com/m49b3d6f7 And here is a link to the source. (I need to tidy up all the duplicate triangle drawing code, I know, I started on that with the enum.)
     
  2. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    On IRC I was pointed to.. http://unity3d.com/support/documentation/ScriptReference/Mesh-triangles.html
    http://unity3d.com/support/documentation/ScriptReference/Mesh.SetTriangles.html

    It says to use set triangles to have submeshes, so I could divide the 6 sides up.
    But there aren't any examples of how to use it. Is the triangles: int[] supposed to be like "triangles: Tris[ (trisIndex) * 3 + 1 ] = vertexIndex - ( segments - 1 )"? Or is it a [0,0,0]?

    I guess i'll experiment, need sleep now. But maybe someone will have some good insight.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There are some mesh construction examples here. (Also here, although the mesh in that case isn't actually displayed, but it could be.)

    --Eric
     
  4. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    I've seen those. They're mostly about procedural modifying.

    I'll look again to see if there is an example for making submeshes, but I don't think there is.
     
  5. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    It's not clear to me exactly what you want to do, and in what manner the morphing is to take place.

    Maybe you can make a list of pre-morph conditions and post-morph conditions to make it more clear. For example, will the UVs be the same before and after morphing? Or not?
     
  6. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    I want 6 seperate UV's, one for each side of the cube.
    And for that to be preserved when it is morphed into a sphere and heightmapped.

    It's my understanding that the UV won't change when the vertices position does, it'll just be skewed some.

    It would be best to have the positions to compensate for the skewing, but not really a priority now.. cubesphere distortion and seaming is minimal, that's why I went with a cubesphere.


    So my question is how to make submeshes, I guess.
     
  7. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    Yeah I looked through and no submesh creation in the procedural examples. I had looked through them before I started make procedural mesh generation script, and even in the proceudral mesh examples it has it uses imported meshes.

    It has some good things to look at when I start my heightmapping and texturing procedurally, but nothing for this.

    http://unity3d.com/support/documentation/ScriptReference/Mesh.SetTriangles.html is what I need, I'm just not sure how to use it compared to mesh.Triangles.
    Maybe I'll figure it out with trial and error though.
     
  8. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    Is there a reason you can't model these 6 sides and UV-map them in a 3D modeling program, and then work the vertex positions in scripts?

    It is true that the UV coordinates won't change when you move the vertex positions. So I assume you want the UVs to be laid out for the target sphere. It is however probably entirely possible to morph the UVs aswell if it would make it look better.
     
  9. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    You mean to figure out how to position the vertexes all on one uv?

    I can think of how to do it simply by making a 't', or having 2 rows of 3.
    For one that makes a lot of blank space..
    But mainly, don't you think it'll be easier to make procedural textures(heightmap, terrain, etc) with 6 seperate squares(uvs)? It takes out figuring out how to position the pixels on all the different spots on the uv, I just have to make it fill the whole square and just make sure the edges will match.


    Anyways, I figured out how to set submeshes, but I can't find anything on how to set the uv for each.

    At least I think I'm setting the submeshes right. No errors, but I can't see them because they're transparent I believe.
     
  10. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
  11. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I think the word "submesh" may have been used a bit loosely here... ;-)

    I don't think you need separate UV arrays for what you are doing here. It's perfectly OK to have more than one vertex at a given position if it helps what you are doing. Each vertex along the edges can be split into two vertices, one for each face. Similarly, the corners will each have three vertices at the same position. If you do this, the UV coordinates don't have to be continuous as you follow around an edge from one face to the next. Essentially, you will still have a single mesh, but it will have several disconnected parts.
     
  12. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    I don't want the uv overlapping though. That would make a planet with 6 repeating areas. I need it all unique. Well, that's what I'm taking from you saying having them overlapping.

    I know I could split them to all be on one, but then I also need to make the procedural texture genetation seamless between all this different points on posibly different resolutions. It's much simpler in my mind to go from 0,0; 0,1; 1,1; 1,0 on 6 different uv's.

    I made submeshes as that's what I thought I need to have seperate uv's. There is only uv and uv2 for a single mesh's uv.
    Not to mention, probly makes LoD easier with culling non-visible submeshes.(Or does the engine automatically cull non-visible tri's?)
     
  13. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    Nevermind.. apparently I'm still limited to uv1 and uv2. I'd have to have seperate actual meshes.
     
  14. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    Alright forget all that other stuff.

    I decided to drop all that. and have seperate meshes for each face.

    And not only that, but divide each face into 4, then another 4, depending on view-distance for LoD and so the engines fustum culling will work.(the fustum culling doesn't work with just submeshes AFAIK, plus i need seperate UV)

    Ontop of that, I want to have the starting mesh-divisions dynamic.
    For asteroids, I might just have them start as 1 or 3 meshes. For moons, say 6. For planets i might them start as 24(6 faces, each face divided 4 times).
    So I have another var to go along with the segments and diameter.

    Ignore the part about submesh. I need to change it to 6 actual meshes, which is my question basically.
    http://pastebin.com/m278ac523
    Code is 1/4th the size now.

    So anyways now I have a new predicament, and that is that I basically need to
    Code (csharp):
    1.  
    2. modelMesh = new Mesh();
    3. modelMesh.name = "Sphere";//maybe I need some sort of identifier to figure out what part of the mesh I'm dealing with, or something?  Or that should replace "modelMesh"?
    4. modelMesh.Clear();
    5. //... draw code.
    6. modelMesh.vertices = Vertices;
    7. modelMesh.uv = UVs;
    8. modelMesh.triangles = Tris;
    9.  
    a different number of times, depending on my number of starting meshes.. I'm not figuring in the LoD divisions yet to make use of frustum culling.

    How would I dynamically create more of what i put in that code block? And assign a way to identify it..[/code]
     
  15. innociv

    innociv

    Joined:
    Dec 17, 2009
    Posts:
    36
    Resolved it.
     
  16. DaphydTheBard

    DaphydTheBard

    Joined:
    Mar 6, 2012
    Posts:
    101
    This might be a silly question, but out of interest, why go to all the trouble of creating the mesh yourself?

    Why not just use a sphere?
     
  17. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878