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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems with Material

Discussion in 'Scripting' started by Josenifftodd, Apr 10, 2016.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    So I can't seem to be able to combine my material once the mesh is combined together.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    4. public class CubeSphere : MonoBehaviour {
    5.  
    6.     public int gridSize;
    7.  
    8.     public float radius = 1f;
    9.  
    10.     private Mesh mesh;
    11.     private Vector3[] vertices;
    12.     private Vector3[] normals;
    13.     public Color32[] cubeUV;
    14.  
    15.     private void Update () {
    16.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    17.         Debug.Log("Submeshes: " + mesh.subMeshCount);
    18.     }
    19.  
    20.     private void Start ()
    21.     {
    22.         Generate();
    23.         //CombineMesh();
    24.      
    25.     }
    26.  
    27.     private void Generate () {
    28.         GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    29.         mesh.name = "Procedural Planet";
    30.         CreateVertices();
    31.         CreateTriangles();
    32.         CreateColliders();
    33.         mesh.RecalculateBounds();
    34.         mesh.Optimize();
    35.     }
    36.  
    37.     private void CombineMesh ()
    38.     {
    39.         MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
    40.         CombineInstance[] combine = new CombineInstance[meshFilters.Length];
    41.         int i = 0;
    42.         while (i < meshFilters.Length)
    43.         {
    44.             combine[i].mesh = meshFilters[i].sharedMesh;
    45.             combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
    46.             meshFilters[i].gameObject.SetActive(true);
    47.             i++;
    48.         }
    49.         transform.GetComponent<MeshFilter>().mesh = new Mesh();
    50.         transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
    51.         transform.gameObject.SetActive(true);
    52.     }
    53.  
    54.     private void CreateVertices () {
    55.         int cornerVertices = 8;
    56.         int edgeVertices = (gridSize + gridSize + gridSize - 3) * 4;
    57.         int faceVertices = (
    58.             (gridSize - 1) * (gridSize - 1) +
    59.             (gridSize - 1) * (gridSize - 1) +
    60.             (gridSize - 1) * (gridSize - 1)) * 2;
    61.         vertices = new Vector3[cornerVertices + edgeVertices + faceVertices];
    62.         normals = new Vector3[vertices.Length];
    63.         cubeUV = new Color32[vertices.Length];
    64.  
    65.         int v = 0;
    66.         for (int y = 0; y <= gridSize; y++) {
    67.             for (int x = 0; x <= gridSize; x++) {
    68.                 SetVertex(v++, x, y, 0);
    69.             }
    70.             for (int z = 1; z <= gridSize; z++) {
    71.                 SetVertex(v++, gridSize, y, z);
    72.             }
    73.             for (int x = gridSize - 1; x >= 0; x--) {
    74.                 SetVertex(v++, x, y, gridSize);
    75.             }
    76.             for (int z = gridSize - 1; z > 0; z--) {
    77.                 SetVertex(v++, 0, y, z);
    78.             }
    79.         }
    80.         for (int z = 1; z < gridSize; z++) {
    81.             for (int x = 1; x < gridSize; x++) {
    82.                 SetVertex(v++, x, gridSize, z);
    83.             }
    84.         }
    85.         for (int z = 1; z < gridSize; z++) {
    86.             for (int x = 1; x < gridSize; x++) {
    87.                 SetVertex(v++, x, 0, z);
    88.             }
    89.         }
    90.  
    91.         mesh.vertices = vertices;
    92.         mesh.normals = normals;
    93.         mesh.colors32 = cubeUV;    
    94.     }
    95.  
    96.     private void SetVertex (int i, int x, int y, int z) {
    97.         Vector3 v = new Vector3(x, y, z) * 2f / gridSize - Vector3.one;
    98.         float x2 = v.x * v.x;
    99.         float y2 = v.y * v.y;
    100.         float z2 = v.z * v.z;
    101.         Vector3 s;
    102.         s.x = v.x * Mathf.Sqrt(1f - y2 / 2f - z2 / 2f + y2 * z2 / 3f);
    103.         s.y = v.y * Mathf.Sqrt(1f - x2 / 2f - z2 / 2f + x2 * z2 / 3f);
    104.         s.z = v.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f);
    105.         normals[i] = s;
    106.         vertices[i] = normals[i] * radius;
    107.         cubeUV[i] = new Color32((byte)x, (byte)y, (byte)z, 0);
    108.     }
    109.  
    110.     private void CreateTriangles () {
    111.         int[] trianglesZ = new int[(gridSize * gridSize) * 12];
    112.         int[] trianglesX = new int[(gridSize * gridSize) * 12];
    113.         int[] trianglesY = new int[(gridSize * gridSize) * 12];
    114.         int ring = (gridSize + gridSize) * 2;
    115.         int tZ = 0, tX = 0, tY = 0, v = 0;
    116.  
    117.     for (int y = 0; y < gridSize; y++, v++) {
    118.             for (int q = 0; q < gridSize; q++, v++) {
    119.                 tZ = SetQuad(trianglesZ, tZ, v, v + 1, v + ring, v + ring + 1);
    120.             }
    121.             for (int q = 0; q < gridSize; q++, v++) {
    122.                 tX = SetQuad(trianglesX, tX, v, v + 1, v + ring, v + ring + 1);
    123.             }
    124.             for (int q = 0; q < gridSize; q++, v++) {
    125.                 tZ = SetQuad(trianglesZ, tZ, v, v + 1, v + ring, v + ring + 1);
    126.             }
    127.             for (int q = 0; q < gridSize - 1; q++, v++) {
    128.                 tX = SetQuad(trianglesX, tX, v, v + 1, v + ring, v + ring + 1);
    129.             }
    130.             tX = SetQuad(trianglesX, tX, v, v - ring + 1, v + ring, v + 1);
    131.          
    132.         }
    133.  
    134.         tY = CreateTopFace(trianglesY, tY, ring);
    135.         tY = CreateBottomFace(trianglesY, tY, ring);
    136.  
    137.         mesh.subMeshCount = 3;
    138.         mesh.SetTriangles(trianglesZ, 0);
    139.         mesh.SetTriangles(trianglesX, 1);
    140.         mesh.SetTriangles(trianglesY, 2);
    141.     }
    142.  
    143.     private int CreateTopFace (int[] triangles, int t, int ring) {
    144.         int v = ring * gridSize;
    145.         for (int x = 0; x < gridSize - 1; x++, v++) {
    146.             t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + ring);
    147.         }
    148.         t = SetQuad(triangles, t, v, v + 1, v + ring - 1, v + 2);
    149.  
    150.         int vMin = ring * (gridSize + 1) - 1;
    151.         int vMid = vMin + 1;
    152.         int vMax = v + 2;
    153.  
    154.         for (int z = 1; z < gridSize - 1; z++, vMin--, vMid++, vMax++) {
    155.             t = SetQuad(triangles, t, vMin, vMid, vMin - 1, vMid + gridSize - 1);
    156.             for (int x = 1; x < gridSize - 1; x++, vMid++) {
    157.                 t = SetQuad(
    158.                     triangles, t,
    159.                     vMid, vMid + 1, vMid + gridSize - 1, vMid + gridSize);
    160.             }
    161.             t = SetQuad(triangles, t, vMid, vMax, vMid + gridSize - 1, vMax + 1);
    162.         }
    163.  
    164.         int vTop = vMin - 2;
    165.         t = SetQuad(triangles, t, vMin, vMid, vTop + 1, vTop);
    166.         for (int x = 1; x < gridSize - 1; x++, vTop--, vMid++) {
    167.             t = SetQuad(triangles, t, vMid, vMid + 1, vTop, vTop - 1);
    168.         }
    169.         t = SetQuad(triangles, t, vMid, vTop - 2, vTop, vTop - 1);
    170.  
    171.         return t;
    172.     }
    173.  
    174.     private int CreateBottomFace (int[] triangles, int t, int ring) {
    175.         int v = 1;
    176.         int vMid = vertices.Length - (gridSize - 1) * (gridSize - 1);
    177.         t = SetQuad(triangles, t, ring - 1, vMid, 0, 1);
    178.         for (int x = 1; x < gridSize - 1; x++, v++, vMid++) {
    179.             t = SetQuad(triangles, t, vMid, vMid + 1, v, v + 1);
    180.         }
    181.         t = SetQuad(triangles, t, vMid, v + 2, v, v + 1);
    182.  
    183.         int vMin = ring - 2;
    184.         vMid -= gridSize - 2;
    185.         int vMax = v + 2;
    186.  
    187.         for (int z = 1; z < gridSize - 1; z++, vMin--, vMid++, vMax++) {
    188.             t = SetQuad(triangles, t, vMin, vMid + gridSize - 1, vMin + 1, vMid);
    189.             for (int x = 1; x < gridSize - 1; x++, vMid++) {
    190.                 t = SetQuad(
    191.                     triangles, t,
    192.                     vMid + gridSize - 1, vMid + gridSize, vMid, vMid + 1);
    193.             }
    194.             t = SetQuad(triangles, t, vMid + gridSize - 1, vMax + 1, vMid, vMax);
    195.         }
    196.  
    197.         int vTop = vMin - 1;
    198.         t = SetQuad(triangles, t, vTop + 1, vTop, vTop + 2, vMid);
    199.         for (int x = 1; x < gridSize - 1; x++, vTop--, vMid++) {
    200.             t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vMid + 1);
    201.         }
    202.         t = SetQuad(triangles, t, vTop, vTop - 1, vMid, vTop - 2);
    203.  
    204.         return t;
    205.     }
    206.  
    207.     private static int
    208.     SetQuad (int[] triangles, int i, int v00, int v10, int v01, int v11) {
    209.         triangles[i] = v00;
    210.         triangles[i + 1] = triangles[i + 4] = v01;
    211.         triangles[i + 2] = triangles[i + 3] = v10;
    212.         triangles[i + 5] = v11;
    213.         return i + 6;
    214.     }
    215.  
    216.     private void CreateColliders () {
    217.         gameObject.AddComponent<MeshCollider>();
    218.     }
    219. }
    Do you know what I'm doing wrong? my texture is a normal map texture that has perlin noise on it which is generated using the reference for noise on unity's examples. Once it's combined it tells me they is no submeshes anymore, and that they is 1. but when it displays it in the scene it only shows the faces on X.

    EDIT: try the script yourself: Put the script onto a empty game object an press play. Remember to attach a material and uncomment CombineMesh();
     
  2. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I've figured out it actually splits the mesh up into 3 different submeshes. I figure I need this for when I implement the LOD system. I'm just not sure what I change so it doesn't make the meshes have 3 different materials.