Search Unity

Create a cube mesh in script with floats?

Discussion in 'Scripting' started by Vexer, Jul 11, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys is there an easy way to change the triangle ints with a float?
    Because i would like to make a cube that's 0.5 units big.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5.  
    6. [RequireComponent(typeof(MeshFilter))]
    7. [RequireComponent(typeof(MeshRenderer))]
    8. public class MapGenerator : MonoBehaviour
    9. {
    10.  
    11.     void Start()
    12.     {
    13.         CreateCube();
    14.     }
    15.  
    16.     private void CreateCube()
    17.     {
    18.         Vector3[] vertices = {
    19.             new Vector3 (0, 0, 0),
    20.             new Vector3 (0.5f, 0, 0),
    21.             new Vector3 (0.5f, 1, 0),
    22.             new Vector3 (0, 0.5f, 0),
    23.             new Vector3 (0, 0.5f, 0.5f),
    24.             new Vector3 (0.5f, 0.5f, 0.5f),
    25.             new Vector3 (0.5f, 0, 0.5f),
    26.             new Vector3 (0, 0, 0.5f),
    27.         };
    28.  
    29.         int[] triangles = {
    30.             0, 1, 0.5f, //face front
    31.             0, 1.5f, 1,
    32.             1, 1.5f, 2, //face top
    33.             1, 2, 2.5f,
    34.             1, 1, 2.5f, //face right
    35.             0.5f, 2.5f, 3,
    36.             0, 3.5f, 2, //face left
    37.             0, 2, 1.5f,
    38.             2.5f, 2, 3.5f, //face back
    39.             2.5f, 3.5f, 3,
    40.             0, 3, 3.5f, //face bottom
    41.             0, 0.5f, 3
    42.         };
    43.  
    44.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    45.         mesh.Clear();
    46.         mesh.vertices = vertices;
    47.         mesh.triangles = triangles;
    48.         MeshUtility.Optimize(mesh);
    49.         mesh.RecalculateNormals();
    50.     }
    51. }
    52.  
     
    Last edited: Jul 11, 2018
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    the int[] triangles are the indices of the vertices. you can't index a 0.5 position in an array. just put the vertices where you want them in 3d space (floats are ok there!) and create the triangles out of them via the indices array.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Could you please give me some sort of example code i don't really get what you mean and i've been trying to fix this for 3 days now..
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Code (csharp):
    1.  
    2.             0, 1, 0.5f, //face front
    3.  
    the 0.5 there is wrong. just replace it with the index of the vertex you want to form the 3rd corner of your triangle. keep in mind that the order of vertices determines the direction of the triangle and thus from which side you see it. There is plenty of example code and tutorials for this out there so I'm certainly not going to repeat this. If you have specific questions feel free to ask though.

    You have 2 arrays. the vertices contain the points in 3d space which make up the corners of your triangles. the triangles array just contains the index of the vertices (the position in the first array) which form each triangle. its best to draw your cube on paper and number the corners in the same order like in your code. then make each quad out of 2 triangles and just write down the indices. then put them in your code and you are good to go. remember the correct order because backward triangles are culled.
     
  5. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Fixed it (thx) btw do you have any idea why my box has this strange color and is not completely green like it should be could that have to do something with uvs? and if so could you link me or explain to me how i can fix it because since i don't know what's causing the error i can't find anything on google on it: https://gyazo.com/2ebbcc3bc80db5f58178861dbbd25e03

    Code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5.  
    6. [RequireComponent(typeof(MeshFilter))]
    7. [RequireComponent(typeof(MeshRenderer))]
    8. public class MapGenerator : MonoBehaviour
    9. {
    10.  
    11.     void Start()
    12.     {
    13.         CreateCube();
    14.  
    15.     }
    16.  
    17.     private void CreateCube()
    18.     {
    19.  
    20.         Vector3[] vertices = {
    21.             new Vector3 (0, 0, 0),
    22.             new Vector3 (0.5f, 0, 0),
    23.             new Vector3 (0.5f, 0.5f, 0),
    24.             new Vector3 (0, 0.5f, 0),
    25.             new Vector3 (0, 0.5f, 0.5f),
    26.             new Vector3 (0.5f, 0.5f, 0.5f),
    27.             new Vector3 (0.5f, 0, 0.5f),
    28.             new Vector3 (0, 0, 0.5f),
    29.         };
    30.  
    31.         int[] triangles = {
    32.          0, 2, 1, //face front
    33.          0, 3, 2,
    34.          2, 3, 4, //face top
    35.          2, 4, 5,
    36.          1, 2, 5, //face right
    37.          1, 5, 6,
    38.          0, 7, 4, //face left
    39.          0, 4, 3,
    40.          5, 4, 7, //face back
    41.          5, 7, 6,
    42.          0, 6, 7, //face bottom
    43.          0, 1, 6
    44.          };
    45.  
    46.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    47.         mesh.Clear();
    48.         mesh.vertices = vertices;
    49.         mesh.triangles = triangles;
    50.         this.GetComponent<Renderer>().material.color = Color.green;
    51.         MeshUtility.Optimize(mesh);
    52.         mesh.RecalculateNormals();
    53.     }
    54. }
    55.  
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    There is no obvious issue apparent to me. Do you have a Texture assigned to the Material? Do you have assigned a Material at all? I would try to create a material and assign it to the cube. Make sure it uses a proper shader and maybe a monochrome texture. If that does not work try to assign the uvs to the vertices.