Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Simple Mesh manipulations

Discussion in 'Scripting' started by roger0, Apr 3, 2012.

  1. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Could someone explain how to do these mesh transformations in scripting? I would prefer someone to provide the code in order to do them and comment the code explaining whats going on in detail. The examples are simulated and were not done in unity.

    A box translating its vertices (3D rendered)
    http://www.youtube.com/watch?v=4X_nUYyEoao

    A box extruding on one of its sides. (done with screenCaptured images)
    http://www.youtube.com/watch?v=aQNBiTtEowg


    I want to start learning how to manipulate meshes in Unity in order to build new tools. Although theres very little information on the web on mesh deformation or manipulation in Unity. When people ask about it most of the time they get directed to the .mesh scripting reference page or the procedural example project. But that does not actually teach how to do anything.

    Any help would be greatly appreciated.
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    can anyone help? This seems to be like a simple thing.
     
  3. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    no one?
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You would need to first figure out how to select the polygons that you want to extrude, during the extrusion process simply adding 10 polygons and 20 for a square or 7 and 15 for a triangle vertices is not that hard to do.

    You could easily calculate the normals of all the used vertices and get a generalized direction for your extrusion. Then it is all just orignal vertex + (direction * distance) to get your other vertices.
     
  5. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Thanks, although I will need more explanation in order to begin. I dont know anything about selecting polygons or adding polygons for an extrusion like this. I'm mostly a 3D artist and what im used to is just selecting polygons and extruding them but when your talking about code I have no idea.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Sorry if this is too basic, but when you say you're "mostly a 3D artist", do you know how to code? Because if not, you need to learn that first. The mesh scripting pages in the Unity docs, plus looking through the procedural examples, should be enough to learn what you need. That's what I used.

    --Eric
     
  7. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I'm very beginner at coding (3-4 months). I know some basic javascript and unity language. And i've experimented with various scripts already. But things that are clearly obvious to most programmers would probably not be to me.

    I'm curious why theres no tutorials on mesh scripting or working with vertices through code in Unity. Is it a super advanced thing?

    Anyway, if someone could help me with the examples it would help get my foot in the door.
     
  8. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,679
    Quite the opposite, its a very simple thing, you just have to alter the Vector3 values for the mesh, as pointed out the harder part is deciding which values to alter. It gets a little harder if you are planning on adding new faces but still its basic stuff. There isnt much a tutorial can really say, it will show you how to get the array of vertex values and how to set it back to the mesh after you have updated it, but in between what you do with the vertex values is kinda up to you.

    If you do take the time to look at the procedural examples and read through the code (each example is very short) you will see what is going on. Take the code and tinker with it.
     
  9. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I am looking at the procedural project right now, but while im doing that can someone provide a code and explanation of how to do my examples? I am very good at learning myself. But I want to have more options available.
     
  10. RANDOM++

    RANDOM++

    Joined:
    Mar 10, 2012
    Posts:
    68
    I think it´s going to be a pain in the ass and you end up reading things like this.

    http://en.wikipedia.org/wiki/Euclidean_space

    Unless someone else can prove me wrong ofcourse, I´m pretty sure it´s stuff that involves a lot of math, because everytime I want to know something about vectors/geometry this is what I get hehe.
     
  11. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    can someone please help me?
     
  12. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    So basically you need to stop, and learn how to create and edit meshes. Reading RANDOM++'s link is probably the worst way to begin because it has no initial practical application to Unity.

    The basics are just as they are in 3ds max or maya or whatever. Vertices, Faces and UV's.

    So for every face(triangle) you have to have 3 points. For each point, you have to have 1 UV.

    http://unity3d.com/support/documentation/ScriptReference/Mesh.html

    So now we look at the documentation and see that it is requesting Verts, UV's and Triangles. The verts are a simple array of Vector3's, UV's are a simple array of Vector2's. Triangles are the exception. They are a list of int's grouped in three's. Each group defines point 1, 2 and 3 of the triangle face from the vertices face. They are listed Clockwise.

    Disassembling them is quite easy once you know how they go together.
     
  13. RANDOM++

    RANDOM++

    Joined:
    Mar 10, 2012
    Posts:
    68
    I agree my link isn't the chicken with the golden eggs hehe.
     
  14. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    When I copy those examples on the .mesh scripting page and paste them into my editor, the whole code is on one line. How do I fix that? I'm using MonoDevelop.

    I created a empty game object and attached the first example code onto it. But once the game starts I get a null reference exception.
     
    Last edited: Apr 5, 2012
  15. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    press enter a few times after semi-colos ;

    Also make sure your in the right language
     
  16. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I'm pretty sure im in javascript. I think the code will compile the same even if they are on the same line right?

    also what about the null reference exception?
     
  17. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I'm back.

    The only thing I need is for someone to provide a code and explanation for the above examples. After that i'll start experimenting myself more.

    The procedural example project has basic scripts to study but I need something even more basic.
     
    Last edited: Apr 6, 2012
  18. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    This is a basic link to a mesh that creates a cube:

    http://forum.unity3d.com/threads/43421-Create-a-dynamic-mesh-(cube)?p=830009&viewfull=1#post830009

    Without completely generating everything you need, no one is going to produce the code that you will need to get it done.

    Your hurdles are 1) figuring out which faces that you want to extrude and the method of which you want to extrude them. 2) removing the old face or using it in the extrusion, and generating the new triangles for the extrusion.

    There are 3 basic extrusion methods:
    1) Extrude in the same direction
    2) Extrude in normalized directions
    3) Extrude in normalized vertex directions
     
  19. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Alright I looked at the script, although the last code posted had errors. So I tried Jessys code.

    I created a empty game object, added a mesh filter and mesh renderer, and assigned this script to it. It does make partial sense. Although im very confused with the facelist. Why are the numbers in the array listed in a sporadic manner? Also the comments next to them I can't understand. I think they are in a different language. But what could they mean as far as the code?

    Also what is the size variable doing?

    Code (csharp):
    1.  
    2. var size = 1500;
    3.  
    4. function Start ()
    5. {
    6. makeCube();
    7.  
    8. }
    9.  
    10. function makeCube () {  
    11.     var vertexList = [
    12.         Vector3(-size, -size, -size),
    13.         Vector3(-size,  size, -size),
    14.         Vector3( size,  size, -size),
    15.         Vector3( size, -size, -size),
    16.         Vector3( size, -size,  size),
    17.         Vector3( size,  size,  size),
    18.         Vector3(-size,  size,  size),
    19.         Vector3(-size, -size,  size)
    20.     ];
    21.    
    22.     var faceList = [
    23.         0, 1, 3, //   1: face arrière
    24.         0, 2, 3,
    25.         3, 2, 5, //   2: face droite
    26.         3, 5, 4,
    27.         5, 2, 1, //   3: face dessue
    28.         5, 1, 6,
    29.         3, 4, 7, //   4: face dessous
    30.         3, 7, 0,
    31.         0, 7, 6, //   5: face gauche
    32.         0, 6, 1,
    33.         4, 5, 6, //   6: face avant
    34.         4, 6, 7
    35.     ];
    36.            
    37.     var mesh = new Mesh();
    38.     mesh.vertices = vertexList;
    39.     mesh.triangles = faceList;
    40.     mesh.RecalculateNormals();
    41.    
    42.     (GetComponent(MeshFilter) as MeshFilter).mesh = mesh;      
    43. }
    44.  
    45. @script RequireComponent(MeshFilter)
    46. @script RequireComponent(MeshRenderer)
     
  20. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Hmmm... Maybe you dont understand about 3d.

    $boxy.jpg

    OK, lets assume you are making a basic box. The picture above shows you a hidden line box (the back side is not visible) I have made 8 points that represent the vertices and labeled them 0 thru 7.

    Lets focus on the top of the box. In order to draw the box in Unity, you will need to give it faces. The top of the box shows that it uses 4 vertices to make up the top surface. 0, 1, 2 and 3. now, we have to define triangles to fill the area. We cant just say, make a polygon here, we have to define each triangle in the polygon. Also, we have to define these in Clockwise fashion. So 0, 1 and 2 are clockwise from the top side. That is one triangle, the second would be 1, 3 and 2.

    Lets go to the bottom side, 4, 5, 6 and 7. They are drawn clockwise from the bottom view, so they would be 4, 6, 5 and 5, 6, 7.

    This is why the numbers appear in some random order.

    As far as size. Each unit in the box is 1 unit in space. IF... the localScale of the object was 1,1,1. So an object 1500 units by 1500 units by 1500 units at 1,1,1 scale is 1500, 1500, 1500. (or huge)

    now, UV's are the part you didnt copy over. UV's measure a texture map from the lower left corner and are measured in only X and Y. So you can set orientation of the UV map on the cube as well. The top UV's would be 0: (0,0) 1: (0,1), 2: (1,0) and 3: (1,1) So 0 is the lower left corner of the texture map and 3 is the upper right. (0,0 to 1,1)

    Smoothing is not represented in this. Each face that shares a vertices with another face is smoothed when using mesh.RecalculateNormals(). This means that the sides of the cube would be all funky looking if they shared all the vertices in common. To overcome this, you would set each side with it's own vertices. So the top would get 0,1,2,3 then the front, 4,5,6,7 then the right: 8,9,10,11 and so on.
     
    Last edited: Apr 6, 2012
  21. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I'm starting to get it a little more. How come all of the vector3's X Y Z positions are set to negetive size?

    The faceList is making more sense. Although the one in the code is arranged differently than your graphic.

    How does the computer know what kind of shape your connecting the points for? All of the Vector3's or ''vertices'' seem to be set to -1500,-1500,-1500 in world space. How does that make a cube?
     
  22. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Not all the values are negative, because the cube spans the negative and positive space. So -size, -size, -size is the minimum of the cube and size, size, size is the maximum. All the other points are made of a combination of -size and size. So, his 1500 size spans -1500 to 1500, or 3000 units.

    It doesn't actually know its a cube, its a list of verts and faces, that is all the engine cares about. It is how the verts and faces are arranged that makes us think its a cube.
     
  23. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Ok im back. Sorry for responding a day later. But if your on, I have another question. I understand the vertexList is a group of points in space. Which should be an outline of a cube, if viewed visually. Although, how do you number those points as 0,1,2,3 etc. So it knows where to draw the triangles?
     
  24. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I tried making a code of my own. I want to create just one triangle. Although when I play it, nothing happens. I have a mesh filter and renderer attached to the empty gameobject.

    Code (csharp):
    1.  
    2.  
    3. var v1 : Transform;
    4. var v2 : Transform;
    5. var v3 : Transform;
    6.  
    7.  
    8. function Update () {
    9.  
    10. MakeTriangle ();
    11.  
    12. }
    13.  
    14. function MakeTriangle () {
    15.  
    16. var vertexList =[
    17. v1.position,
    18. v2.position,
    19. v3.position
    20. ];
    21.  
    22.  
    23.  
    24.  
    25. var faceList = [0,1,2];
    26.  
    27. var mesh  = new Mesh();
    28. mesh.vertices = vertexList;
    29. mesh.triangles = faceList;
    30. mesh.RecalculateNormals();
    31.  
    32. (GetComponent(MeshFilter) as MeshFilter).mesh = mesh;
    33. }
    34.  
    35. @script RequireComponent(MeshFilter)
    36. @script RequireComponent(MeshRenderer
     
    Last edited: Apr 8, 2012
  25. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
  26. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Works perfectly.

    Questions: Are any of your 3 transforms the same transform. (i.e. do two points share the same space?) Is the gameobject that you put this on at world zero? (if not, then your positioning your vertices offset from the transforms that you have selected to the position of your game object)

    Hint: put the game object at world center. Create 3 boxes and assign them to each of the 3 transforms. Play it, go back into the Scene veiw and look on top of and below the 3 boxes to make sure which order you are assigning them in. ;)
     
  27. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    alright, I was able to get one triangle to appear. The problem was I did not have it at position 0,0,0.

    However, i'm confused as to how someone is suppost to know what points they are connecting when making the faceList. There just a bunch of numbers. Is there a way to use gameobjects instead? Such as draw a triangle from point_1.transform.position, to point_2.transform.position, and point_3.transform.position.

    I tried creating a 3d triangle with this code but it says failed setting triangles. Some indices are referencing out of bounds vertices.


    Code (csharp):
    1.  
    2. var v1 : Transform;
    3. var v2 : Transform;
    4. var v3 : Transform;
    5.  
    6.  
    7. function Update () {
    8.  
    9. MakeTriangle ();
    10.  
    11. }
    12.  
    13. function MakeTriangle () {
    14.  
    15. var vertexList =[
    16. v1.position,
    17. v2.position,
    18. v3.position
    19. ];
    20.  
    21.  
    22.  
    23.  
    24. var faceList = [0,1,2,
    25.                0,1,3,
    26.                0,2,3,
    27.                1,2,3
    28.                ];
    29.        
    30. var mesh  = new Mesh();
    31. mesh.vertices = vertexList;
    32. mesh.triangles = faceList;
    33. mesh.RecalculateNormals();
    34.  
    35. (GetComponent(MeshFilter) as MeshFilter).mesh = mesh;
    36. }
    37.  
    38. @script RequireComponent(MeshFilter)
    39. @script RequireComponent(MeshRenderer)
     
  28. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
  29. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    how do you know what points you are connecting when making the faceList?
     
  30. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I thought I replied to this... The whole thing works perfectly. And you can see what you are doing providing one big thing....

    The object you are connected to has to be at world center and have no scaling or rotation.

    If it is not, then whatever you are making is offset to that position, rotation and scale.
     
  31. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I'm confused as to how to go about making the faceList. How do I know what the vertices are numbered as? So that I can connect them properly.

    If you try my last script it gets an error....
     
  32. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Ok I got a solid triangle to appear. How do I make it have a texture instead of solid purple? Also some faces are turned the wrong way. I guess fixing that is a matter of organizing the faceList differently.

    However, im still confused as how someone is suppost to know what they are connecting when making the faceList. is there a more visual way to do it? What vertex exactly would be #1 in the faceList? Or #2,3, etc.



     
  33. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    can someone clarify the faceList for me?
     
  34. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    bumping for today
     
  35. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's pink because it has no material assigned to it.
     
  36. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    how do I assign UV's to it? I try applying a texture but it does not show.

    Also some of the triangles are turned the wrong way, how can I fix that?
     
  37. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    UVs are an array of Vector2s where the X and Y values are clamped from 0-1. (0,0) is the bottom left of a texture and (1,1) is the top right. The triangle index in mesh.triangles is used to determine the UV coordinate for a given vertex. If you were mapping a simple quad onto a square texture you would have 4 UV coordinates - (0,0), (0,1), (1,0), (1,1).

    To fix triangles facing the wrong way reverse the 1st and 3rd indices for that triangle (ie if 4,5,6 is wrong change it to 6,5,4).
     
  38. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Thanks for getting back, but could you explain more of how to assign texture cordinates? I dont know where to start in the code.


    How do I know what indices belong to a certain triangle?
     
  39. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Create a Vector2[4], fill it up with the values I laid out previously, and then assign it to mesh.uv.


    I would suggest getting a sheet of graph paper and drawing your mesh on it so you can keep track of which vertex is where.
     
  40. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    what I mean is how do I know what "0" is or "1" is in the faceList. 0 as in v1? (first variable in the vertexList array). Then "1" would equal v2(second variable in vertexList array)? And 2 would equal = v3?
     
  41. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Correct.
     
  42. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Alright I'm able to generate a 4 sided triangle and assign it uv's. Although im still confused as to what im actually doing when making the uv array for mesh.uv. All I did was put in random values in the uv array and see if it looked correct in the game.

    I only had to use two values (1.0, and 0.0) in order to get the textures to show correct on the triangles. But I would think I would have to use 3 values since im using polygons with 3 points. Although i've tried using 3 values in the uv arrays Vector2's but I get an error. Does each Vector2 in the mesh.uv array correlate with a polygon in the mesh.triangles array?


    here is what im visualizing about uv's and there corners. Please tell me if im thinking correct.




     
  43. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I almost got this down I just need to understand how UV's work.
     
  44. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    I was going to suggest you buy MegaFiers and you'll have a specialized general purpose solution, though a contradiction that seems. He gave you a tip elsewhere in this thread.
     
  45. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Before I start buying things and using existing tools, I want to know how to do the basics so I know whats going on. And also I want to build my own tools.
     
  46. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    can someone comment on my post with the pictures of the square and triangle?
     
  47. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,679
    The answer for that would be 0.5, 1.0
     
  48. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    huh? what would be 0.5, 1.0?

    How do I map a triangle? I would think it would need 3 uv coordinates since its 3 points. But I was able to map them with just two coordinates. What is really going on?
     
  49. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    please someone help. The internet is practically wiped clean of any information about vertex creation or assigning uv's to meshes. I dont understand the logic behind creating uv's and mapping them onto polygons. I get it from a artist viewpoint using 3d programs but not in script.
     
    Last edited: Apr 16, 2012
  50. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The Mesh documentation in the manual explains this pretty thoroughly actually. Your arrays backing vertices, uv, and normals need to all be the same length. triangles references the index to pull values from those 3 arrays in order to fill the properties of a single vertex. Like I explained previously - UVs are range clamped from 0-1. 0,0 is bottom left 1,1 is top right so any value in between is some spot between bottom left and top right.

    Think of a vertex as a container containing values for it's Vector3 position in local space, up to 2 UV coordinates, and a normal which determines what direction the vertex is facing.