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

MeshCombineUtility Error

Discussion in 'Scripting' started by rmele09, Mar 6, 2015.

  1. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    688
    EDIT: Does anybody have a MeshCombineUtility.cs and CombineChildren.cs that works with Unity 5?

    I upgraded to Unity 5 last night and the MeshCombineUtility.cs has 3 errors in it now. I use javascript so I do not know how to fix this, any help would be great.

    These are the errors:

    1) Assets/Standard Assets/Scripts/MeshCombineUtility.cs(27,74): error CS1061: Type `UnityEngine.Mesh' does not contain a definition for `GetTriangleStrip' and no extension method `GetTriangleStrip' of type `UnityEngine.Mesh' could be found (are you missing a using directive or an assembly reference?)

    2) Assets/Standard Assets/Scripts/MeshCombineUtility.cs(130,73): error CS1061: Type `UnityEngine.Mesh' does not contain a definition for `GetTriangleStrip' and no extension method `GetTriangleStrip' of type `UnityEngine.Mesh' could be found (are you missing a using directive or an assembly reference?)

    3) Assets/Standard Assets/Scripts/MeshCombineUtility.cs(177,30): error CS1061: Type `UnityEngine.Mesh' does not contain a definition for `SetTriangleStrip' and no extension method `SetTriangleStrip' of type `UnityEngine.Mesh' could be found (are you missing a using directive or an assembly reference?)

    The errors are lines 27, 130 and 177 :

    CODE:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MeshCombineUtility {
    5.  
    6.     public struct MeshInstance
    7.     {
    8.         public Mesh      mesh;
    9.         public int       subMeshIndex;      
    10.         public Matrix4x4 transform;
    11.     }
    12.  
    13.     public static Mesh Combine (MeshInstance[] combines, bool generateStrips)
    14.     {
    15.         int vertexCount = 0;
    16.         int triangleCount = 0;
    17.         int stripCount = 0;
    18.         foreach( MeshInstance combine in combines )
    19.         {
    20.             if (combine.mesh)
    21.             {
    22.                 vertexCount += combine.mesh.vertexCount;
    23.          
    24.                 if (generateStrips)
    25.                 {
    26.                     // SUBOPTIMAL FOR PERFORMANCE
    27.                   int curStripCount = combine.mesh.GetTriangleStrip(combine.subMeshIndex).Length;
    28.                     if (curStripCount != 0)
    29.                     {
    30.                         if( stripCount != 0 )
    31.                         {
    32.                             if ((stripCount & 1) == 1 )
    33.                                 stripCount += 3;
    34.                             else
    35.                                 stripCount += 2;
    36.                         }
    37.                         stripCount += curStripCount;
    38.                     }
    39.                     else
    40.                     {
    41.                         generateStrips = false;
    42.                     }
    43.                 }
    44.             }
    45.         }
    46.  
    47.         // Precomputed how many triangles we need instead
    48.         if (!generateStrips)
    49.         {
    50.             foreach( MeshInstance combine in combines )
    51.             {
    52.                 if (combine.mesh)
    53.                 {
    54.                     triangleCount += combine.mesh.GetTriangles(combine.subMeshIndex).Length;
    55.                 }
    56.             }
    57.         }
    58.  
    59.         Vector3[] vertices = new Vector3[vertexCount] ;
    60.         Vector3[] normals = new Vector3[vertexCount] ;
    61.         Vector4[] tangents = new Vector4[vertexCount] ;
    62.         Vector2[] uv = new Vector2[vertexCount];
    63.         Vector2[] uv1 = new Vector2[vertexCount];
    64.         Color[] colors = new Color[vertexCount];
    65.  
    66.         int[] triangles = new int[triangleCount];
    67.         int[] strip = new int[stripCount];
    68.  
    69.         int offset;
    70.  
    71.         offset=0;
    72.         foreach( MeshInstance combine in combines )
    73.         {
    74.             if (combine.mesh)
    75.                 Copy(combine.mesh.vertexCount, combine.mesh.vertices, vertices, ref offset, combine.transform);
    76.         }
    77.  
    78.         offset=0;
    79.         foreach( MeshInstance combine in combines )
    80.         {
    81.             if (combine.mesh)
    82.             {
    83.                 Matrix4x4 invTranspose = combine.transform;
    84.                 invTranspose = invTranspose.inverse.transpose;
    85.                 CopyNormal(combine.mesh.vertexCount, combine.mesh.normals, normals, ref offset, invTranspose);
    86.             }
    87.          
    88.         }
    89.         offset=0;
    90.         foreach( MeshInstance combine in combines )
    91.         {
    92.             if (combine.mesh)
    93.             {
    94.                 Matrix4x4 invTranspose = combine.transform;
    95.                 invTranspose = invTranspose.inverse.transpose;
    96.                 CopyTangents(combine.mesh.vertexCount, combine.mesh.tangents, tangents, ref offset, invTranspose);
    97.             }
    98.          
    99.         }
    100.         offset=0;
    101.         foreach( MeshInstance combine in combines )
    102.         {
    103.             if (combine.mesh)
    104.                 Copy(combine.mesh.vertexCount, combine.mesh.uv, uv, ref offset);
    105.         }
    106.  
    107.         offset=0;
    108.         foreach( MeshInstance combine in combines )
    109.         {
    110.             if (combine.mesh)
    111.                 Copy(combine.mesh.vertexCount, combine.mesh.uv2, uv1, ref offset);
    112.         }
    113.  
    114.         offset=0;
    115.         foreach( MeshInstance combine in combines )
    116.         {
    117.             if (combine.mesh)
    118.                 CopyColors(combine.mesh.vertexCount, combine.mesh.colors, colors, ref offset);
    119.         }
    120.  
    121.         int triangleOffset=0;
    122.         int stripOffset=0;
    123.         int vertexOffset=0;
    124.         foreach( MeshInstance combine in combines )
    125.         {
    126.             if (combine.mesh)
    127.             {
    128.                 if (generateStrips)
    129.                 {
    130.                     int[] inputstrip = combine.mesh.GetTriangleStrip(combine.subMeshIndex);
    131.                     if (stripOffset != 0)
    132.                     {
    133.                         if ((stripOffset & 1) == 1)
    134.                         {
    135.                             strip[stripOffset+0] = strip[stripOffset-1];
    136.                             strip[stripOffset+1] = inputstrip[0] + vertexOffset;
    137.                             strip[stripOffset+2] = inputstrip[0] + vertexOffset;
    138.                             stripOffset+=3;
    139.                         }
    140.                         else
    141.                         {
    142.                             strip[stripOffset+0] = strip[stripOffset-1];
    143.                             strip[stripOffset+1] = inputstrip[0] + vertexOffset;
    144.                             stripOffset+=2;
    145.                         }
    146.                     }
    147.              
    148.                     for (int i=0;i<inputstrip.Length;i++)
    149.                     {
    150.                         strip[i+stripOffset] = inputstrip[i] + vertexOffset;
    151.                     }
    152.                     stripOffset += inputstrip.Length;
    153.                 }
    154.                 else
    155.                 {
    156.                     int[]  inputtriangles = combine.mesh.GetTriangles(combine.subMeshIndex);
    157.                     for (int i=0;i<inputtriangles.Length;i++)
    158.                     {
    159.                         triangles[i+triangleOffset] = inputtriangles[i] + vertexOffset;
    160.                     }
    161.                     triangleOffset += inputtriangles.Length;
    162.                 }
    163.          
    164.                 vertexOffset += combine.mesh.vertexCount;
    165.             }
    166.         }
    167.  
    168.         Mesh mesh = new Mesh();
    169.         mesh.name = "Combined Mesh";
    170.         mesh.vertices = vertices;
    171.         mesh.normals = normals;
    172.         mesh.colors = colors;
    173.         mesh.uv = uv;
    174.         mesh.uv2 = uv1;
    175.         mesh.tangents = tangents;
    176.         if (generateStrips)
    177.             mesh.SetTriangleStrip(strip, 0);
    178.         else
    179.             mesh.triangles = triangles;
    180.  
    181.         return mesh;
    182.     }
    183.  
    184.     static void Copy (int vertexcount, Vector3[] src, Vector3[] dst, ref int offset, Matrix4x4 transform)
    185.     {
    186.         for (int i=0;i<src.Length;i++)
    187.             dst[i+offset] = transform.MultiplyPoint(src[i]);
    188.         offset += vertexcount;
    189.     }
    190.  
    191.     static void CopyNormal (int vertexcount, Vector3[] src, Vector3[] dst, ref int offset, Matrix4x4 transform)
    192.     {
    193.         for (int i=0;i<src.Length;i++)
    194.             dst[i+offset] = transform.MultiplyVector(src[i]).normalized;
    195.         offset += vertexcount;
    196.     }
    197.  
    198.     static void Copy (int vertexcount, Vector2[] src, Vector2[] dst, ref int offset)
    199.     {
    200.         for (int i=0;i<src.Length;i++)
    201.             dst[i+offset] = src[i];
    202.         offset += vertexcount;
    203.     }
    204.  
    205.     static void CopyColors (int vertexcount, Color[] src, Color[] dst, ref int offset)
    206.     {
    207.         for (int i=0;i<src.Length;i++)
    208.             dst[i+offset] = src[i];
    209.         offset += vertexcount;
    210.     }
    211.  
    212.     static void CopyTangents (int vertexcount, Vector4[] src, Vector4[] dst, ref int offset, Matrix4x4 transform)
    213.     {
    214.         for (int i=0;i<src.Length;i++)
    215.         {
    216.             Vector4 p4 = src[i];
    217.             Vector3 p = new Vector3(p4.x, p4.y, p4.z);
    218.             p = transform.MultiplyVector(p).normalized;
    219.             dst[i+offset] = new Vector4(p.x, p.y, p.z, p4.w);
    220.         }
    221.      
    222.         offset += vertexcount;
    223.     }
    224. }
    225.  
     
    Last edited: Mar 6, 2015
    Extruder likes this.
  2. lsgheero

    lsgheero

    Joined:
    Mar 24, 2013
    Posts:
    60
    This will correct it... Change the GetTriangleStrip to GetTriangles and the same for the SetTriangleStrip, change that to SetTriangles
     
    Extruder likes this.
  3. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    688
    thank you very much
     
  4. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Also, see here.
     
  5. arcade1982

    arcade1982

    Joined:
    Dec 30, 2014
    Posts:
    3
    I've changed the line errors provided, but it now is telling me that the triangle indices aren't of a multiple of 3. The error says its trying to create triangles with 32 indices supplied. Anyone know anything about that?
     
  6. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Unless you are using a legacy system that relies on it, do not use this script.
    Secondly make sure you cannot use static or dynamic batching instead of combining meshes.

    You still here?

    - If possible use this API instead.
    - If you don't know how to use the above API, refer to this example
    - If none of the above works, write on this thread again and I will try to overcome my Saturday mood and reply here.

    If you still think the error you got is relevant, consider copy-pasting the error message, it may help.