Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Simple mesh creation with material

Discussion in 'Scripting' started by pgavazzi, Oct 11, 2006.

  1. pgavazzi

    pgavazzi

    Joined:
    Oct 11, 2006
    Posts:
    3
    Hello,

    I need to create my meshes procedurally. I am making slow progress due to the lack of documentation. I was able to have my meshes appear without material, but as soon as I put the material no matter what shader I use, my mesh is invisible.

    Here is my code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent (typeof(MeshFilter))]
    6. public class CreateMeshTest : MonoBehaviour {
    7.     Material   newMaterial;
    8.     MeshFilter meshFilter;
    9.     Mesh       mesh;
    10.     Color[]    newColor;
    11.     int[]      newTriangles;
    12.     Vector3[]  newVertices;
    13.     Vector2[]  newUVs;
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.       mesh = new Mesh();
    18.       newColor = new Color[4];
    19.       newVertices = new Vector3[4];
    20.       newUVs = new Vector2[4];
    21.       newTriangles = new int[6];
    22.      
    23.       // simple quad
    24.       newVertices[0] = new Vector3(-10,-10,0);
    25.       newVertices[1] = new Vector3(10,-10,0);
    26.       newVertices[2] = new Vector3(10,10,0);
    27.       newVertices[3] = new Vector3(-10,10,0);
    28.      
    29.       // simple uv
    30.       newUVs[0] = new Vector2(0,0);
    31.       newUVs[1] = new Vector2(0,0);
    32.       newUVs[2] = new Vector2(0,0);
    33.       newUVs[3] = new Vector2(0,0);
    34.      
    35.       // all red
    36.       newColor[0] = new Color(1.0f,0.0f,0.0f);
    37.       newColor[1] = new Color(1.0f,0.0f,0.0f);
    38.       newColor[2] = new Color(1.0f,0.0f,0.0f);
    39.       newColor[3] = new Color(1.0f,0.0f,0.0f);
    40.      
    41.       // triangle indexes
    42.       newTriangles[0] = 0;
    43.       newTriangles[1] = 1;
    44.       newTriangles[2] = 2;
    45.       newTriangles[3] = 0;
    46.       newTriangles[4] = 2;
    47.       newTriangles[5] = 3;
    48.      
    49.       mesh.vertices = newVertices;
    50.       mesh.colors = newColor;
    51.       mesh.uv = newUVs;
    52.       mesh.triangles = newTriangles;
    53.      
    54.       meshFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;
    55.       meshFilter.mesh = mesh;
    56.       meshFilter.transform.position = new Vector3(1,1,1);
    57.       newMaterial = new Material(Shader.Find(" Diffuse"));
    58.       newMaterial.color = Color.red;
    59.       meshFilter.renderer.material = newMaterial;    
    60.     }
    61.    
    62.     // Update is called once per frame
    63.     void Update () {
    64.    
    65.     }
    66. }
    67.  
    From the Inspector I see my mesh as before, I see a material, but it seems incomplete (All the shader's saved properties are size 0). The mesh is a simple Quad.

    Has someone built anything similar to generate meshes from scratch?

    Any help would be appreciated,

    I have the non-PRO version, so using GL display lists is out of the question to build my meshes.

    Paul
     
  2. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    I added [ code ][ /code ] to your code, hope you dont mind, much easier to read. :D

    Bill
     
  3. pgavazzi

    pgavazzi

    Joined:
    Oct 11, 2006
    Posts:
    3
    Bill, don't mind at all :D

    Thanks it looks better,

    Paul
     
  4. pgavazzi

    pgavazzi

    Joined:
    Oct 11, 2006
    Posts:
    3
    Found why my mesh dissappeared. It had to do with the Quad not facing the camera and the triangles not being doublefaced.

    I still didn't find a way to make them doublefaced, but just reversing the order of my triangle definition made the quad appear:

    Code (csharp):
    1.  
    2.      // triangle indexes
    3.      newTriangles[0] = 2;
    4.      newTriangles[1] = 1;
    5.      newTriangles[2] = 0;
    6.      newTriangles[3] = 3;
    7.      newTriangles[4] = 2;
    8.      newTriangles[5] = 0;
    9.  
    Hope this helps someone else...

    Paul
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You don't seem to be defining any normals...you need mesh.normals as well, or else you get those kinds of problems of not being able to see stuff.

    --Eric
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes, triangles are single-sided. To make a double-sided mesh you either:
    * Write a shader that turns Culling off (but this can get you wrong lighting on the back sides), or
    * Manually duplicate the vertices and triangles in the mesh. So in quad case, you'd create a mesh with 2x more vertices and 2x more triangles. The second copy of the vertices would be the same except the normals will be negated; and the second copy of triangles will be the same just with "order" reversed (i.e. swap 2nd and 3rd index of each triangle).

    Eic5h5: not having normals won't make things visible or invisible. But normals are required to get any sensible lighting.