Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Create a dynamic mesh (cube)

Discussion in 'Scripting' started by Alain_, Mar 13, 2010.

  1. Alain_

    Alain_

    Joined:
    Mar 10, 2010
    Posts:
    18
    Hi everybody I'am new on the forum and is my first week with Unity (6 years Shockwave 3D)
    I'am trying to create a dynamic mesh a start with a simple cube

    here is my code and a put the script on the main Camera

    i saw nothing on the scene and no error message.

    what I'am doing wrong


    Code (csharp):
    1. function makeCube () {
    2.  
    3.  
    4. var size = 1500;
    5.  
    6. var VertexList = [
    7.     Vector3(-size, -size,  -size),
    8.     Vector3(-size,  size,  -size),
    9.     Vector3( size,  size,  -size),
    10.     Vector3( size, -size,  -size),
    11.     Vector3( size, -size,   size),
    12.     Vector3( size,  size,   size),
    13.     Vector3(-size,   size,  size),
    14.     Vector3(-size, -size,  size)
    15.     ];
    16.  
    17. var FaceList = [
    18.     0, 1, 3, //   1: face arrière
    19.     0, 2, 3,
    20.     3, 2, 5, //   2: face droite
    21.     3, 5, 4,
    22.     5, 2, 1, //   3: face dessue
    23.     5, 1, 6,
    24.     3, 4, 7, //   4: face dessous
    25.     3, 7, 0,
    26.     0, 7, 6, //   5: face gauche
    27.     0, 6, 1,
    28.     4, 5, 6, //   6: face avant
    29.     4, 6, 7
    30.     ];
    31.    
    32.    
    33.  
    34.  
    35.     var newUVs = [
    36.     Vector2(0,0), //   1: face arrière
    37.     Vector2(0,0),
    38.     Vector2(0,0), //   2: face droite
    39.     Vector2(0,0),
    40.     Vector2(0,0), //   3: face dessue
    41.     Vector2(0,0),
    42.     Vector2(0,0), //   4: face dessous
    43.     Vector2(0,0),
    44.     Vector2(0,0), //   5: face gauche
    45.     Vector2(0,0),
    46.     Vector2(0,0), //   6: face avant
    47.     Vector2(0,0)
    48.     ];
    49.    
    50.     var mesh = new Mesh ();
    51.  
    52.     meshFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;
    53.  
    54.     mesh.vertices = VertexList;
    55.    
    56.     //mesh.uv = newUVs;
    57.     mesh.triangles = FaceList;
    58.    
    59.     mesh.RecalculateNormals();
    60.    
    61.     //meshFilter.transform.position = Vector3(0, 0.5, 0);
    62.  
    63. }
    if somebody a script and little explanation to do this kind of stuff to help me to understand how to do a dynamic mesh


    Sorry for my poor english

    cheers,
    Alain
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I assume you're actually calling the makeCube function from somewhere. I also assume that the object the script is attached to has a mesh filter and a mesh renderer. If not, you need to fix those things.

    Also,

    Code (csharp):
    1.     meshFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;
    doesn't do anything. You have to assign the mesh you're creating to the mesh filter:

    Code (csharp):
    1.     GetComponent(MeshFilter).mesh = mesh;
    --Eric
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Welcome! :D

    There are a few things in that script that prevent it from working as-is.
    Edit: Which Eric already touched upon! 8)

    • As written, the function never gets called. That might be obvious and you might know about it, but I put in a Start() function anyway to make sure.
    • You may not have a MeshRenderer attached to the Game Object. I made the script require it at the bottom.
    • You never actually assigned the mesh to the MeshFilter.
    Another thing that won't stop it from working, but is good practice, is to put your variables (size) outside of functions, so you can change them in the Editor.

    Also, in Unity, the variables that you have capitalized (VertexList, FaceList), by convention, are not.

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

    Alain_

    Joined:
    Mar 10, 2010
    Posts:
    18
    Thank for help


    Now I can see the cube. :p

    I try to add the textureCoord but with this code is not good i have this error message (textureCoord is not a member of "UnityEngine.MEsh")


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

    Probably is in the Component(MeshRenderer) but how to write the code in the correct format.

    How to place the cube in the scene i try this and is doesn't work
    //mesh.transform.position = Vector3(0, 0.5, 0)
    //mesh.position = Vector3(0, 0.5, 0)


    cheers,
    Alain
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's "uv" rather than "textureCoord". However, that still won't work, because you need to have the same number of items in the array for uvs (and other things like normals etc.) as you have for the vertices. I'd recommend reading the docs on the Mesh class.

    For moving the cube, you need to set the object's transform.

    --Eric
     
  6. Mauro

    Mauro

    Joined:
    Feb 14, 2012
    Posts:
    2
    Code (csharp):
    1.  
    2.    1.
    3.       var size = 1;
    4.    2.
    5.        
    6.    3.
    7.       function Start () {makeCube();}
    8.    4.
    9.        
    10.    5.
    11.       function makeCube () {  
    12.    6.
    13.          var vertexList = [
    14.    7.
    15.             Vector3(-size, -size, -size),
    16.    8.
    17.             Vector3(-size,  size, -size),
    18.    9.
    19.             Vector3( size,  size, -size),
    20.   10.
    21.             Vector3( size, -size, -size),
    22.   11.
    23.             Vector3( size, -size,  size),
    24.   12.
    25.             Vector3( size,  size,  size),
    26.   13.
    27.             Vector3(-size,  size,  size),
    28.   14.
    29.             Vector3(-size, -size,  size)
    30.   15.
    31.          ];
    32.   16.
    33.          
    34.   17.
    35.           var faceList = [
    36.   18.
    37.             0, 1, 3, //   1: face arrière
    38.   19.
    39.             0, 2, 3,
    40.   20.
    41.             3, 2, 5, //   2: face droite
    42.   21.
    43.             3, 5, 4,
    44.   22.
    45.             5, 2, 1, //   3: face dessue
    46.   23.
    47.             5, 1, 6,
    48.   24.
    49.             3, 4, 7, //   4: face dessous
    50.   25.
    51.             3, 7, 0,
    52.   26.
    53.             0, 7, 6, //   5: face gauche
    54.   27.
    55.             0, 6, 1,
    56.   28.
    57.             4, 5, 6, //   6: face avant
    58.   29.
    59.             4, 6, 7
    60.   30.
    61.          ];
    62.   31.
    63.            
    64.   32.
    65.            var textureCoordinate= [
    66.   33.
    67.           Vector2(0.0,  0.0),
    68.   34.
    69.           Vector2(1.0,  0.0),
    70.   35.
    71.           Vector2(0.0,  1.0),
    72.   36.
    73.           Vector2(1.0,  0.0)
    74.   37.
    75.           ];
    76.   38.
    77.          
    78.   39.
    79.        
    80.   40.
    81.          var mesh = new Mesh();
    82.   41.
    83.          mesh.vertices = vertexList;
    84.   42.
    85.          mesh.triangles = faceList;
    86.   43.
    87.          
    88.   44.
    89.          mesh.uv = textureCoordinate;
    90.   45.
    91.          mesh.RecalculateNormals();
    92.   46.
    93.          renderer.material.mainTexture = Resources.Load("file_texture_name_at_folder");
    94.   47.
    95.         renderer.material.color = Color.red; //for example
    96.   48.
    97.        
    98.   49.
    99.          
    100.   50.
    101.        
    102.   51.
    103.       }
    104.   52.
    105.        
    106.   53.
    107.       @script RequireComponent(MeshFilter)
    108.   54.
    109.       @script RequireComponent(MeshRenderer)
    110.  
     
  7. richy486

    richy486

    Joined:
    Feb 23, 2013
    Posts:
    1
    Should the first three elements in faceList be 0, 1, 2 ?
     
  8. Sildaekar

    Sildaekar

    Joined:
    Jul 8, 2013
    Posts:
    95
    You're about a two years too late man :D