Search Unity

Failed setting triangles. Some indices are referencing out of bounds vertices.

Discussion in 'Scripting' started by Deleted User, Oct 2, 2010.

  1. Deleted User

    Deleted User

    Guest

    Hi there
    I've ran into this error while writing a runtime loader for custom 3d model format. Actually the loader is pretty much done, but I get the error when I'm testing the script.
    Code (csharp):
    1. Failed setting triangles. Some indices are referencing out of bounds vertices.
    I know it's probably common sense, but I'm not very good programmer so I don't get it lol. Could someone point out what I'm doing wrong, please? Here's the source:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ModelLoader {
    6.    
    7.     public ModelLoader(int model_id, string objectname) {
    8.         this.model_id = model_id;
    9.         this.objectname = objectname;
    10.         if(!cached_models.ContainsKey(model_id))
    11.             parse_footer();
    12.     }
    13.    
    14.     private void parse_footer() {
    15.         data = DataBuffer.load("/New Unity Project 6/modelz/"+ model_id + ".rsm");
    16.         data.set_caret(data.get_length() - 23);
    17.        
    18.         num_vertices = data.read_short();
    19.         num_triangles = data.read_short();
    20.         num_tex_triangles = data.read_byte();
    21.        
    22.         int use_tex = data.read_byte();
    23.         bool bools = (0x1  use_tex ^ 0xffffffff) == -2;
    24.         bool bool_78_ = (use_tex  0x2 ^ 0xffffffff) == -3;
    25.         int use_tri_priority = data.read_byte();
    26.         int use_transparency = data.read_byte();
    27.         int use_tri_skinning = data.read_byte();
    28.         int numtrilen2 = data.read_byte();
    29.         int use_vert_skinning = data.read_byte();
    30.        
    31.         int x_data_len = data.read_short();
    32.         int y_data_len = data.read_short();
    33.         int z_data_len = data.read_short();
    34.         int tri_data_len = data.read_short();
    35.         int unknown = data.read_short();        
    36.         Debug.Log("vertices: " + num_vertices + " //triangles: " + num_triangles);
    37.        
    38.    
    39.        
    40. int tmp = 0;
    41.         vert_dir_offset = tmp;
    42.         tmp += num_vertices;
    43.         tex_pointer_offset = tmp;
    44.         if(use_tex == 1)
    45.             tmp += num_triangles;
    46.         tri_type_offset = tmp;
    47.         tmp += num_triangles;
    48.         tri_priority_offset = tmp;
    49.         if(use_tri_priority == 255)
    50.             tmp += num_triangles;
    51.         tri_skin_offset = tmp;
    52.         if(use_tri_skinning == 1)
    53.             tmp += num_triangles;
    54.         vert_skin_offset = tmp;
    55.         if(use_vert_skinning == 1)
    56.             tmp += num_vertices;
    57.         tri_alpha_offset = tmp;
    58.         if(use_transparency == 1)
    59.             tmp += num_triangles;
    60.         tri_data_offset = tmp;
    61.         tmp += tri_data_len;
    62.         numtrilen2_offset = tmp;
    63.         if(numtrilen2 == 1)
    64.             tmp += num_triangles * 2;
    65.         unknown_offset = tmp;
    66.         tmp += unknown;
    67.         color_data_offset = tmp;
    68.         tmp += num_triangles * 2;
    69.         x_data_offset = tmp;
    70.         tmp += x_data_len;
    71.         y_data_offset = tmp;
    72.         tmp += y_data_len;
    73.         z_data_offset = tmp;
    74.         tmp += z_data_len;
    75.         uv_map_offset = tmp;
    76.         tmp += k4 * 6;
    77.         k9 = tmp;
    78.         tmp += l4 * 6;
    79.         l9 = tmp;
    80.         tmp += l4 * 6;
    81.         i10 = tmp;
    82.         tmp += l4;
    83.         j10 = tmp;
    84.         tmp += l4;
    85.         k10 = tmp;
    86.         tmp += l4 + i5 * 2;
    87.     }
    88.    
    89.     public Mesh generate_mesh() {
    90.         if(cached_models == null)
    91.             cached_models = new Dictionary<int, Mesh>();
    92.        
    93.         if(cached_models.ContainsKey(model_id))
    94.             return cached_models[model_id];
    95.        
    96.         Mesh _mesh = new Mesh();
    97.         Vector3[] _vertices = new Vector3[num_vertices];
    98.         Vector2[] _uv = new Vector2[num_vertices];
    99.        
    100.         int[] vertexX = new int[num_vertices];
    101.         int[] vertexY = new int[num_vertices];
    102.         int[] vertexZ = new int[num_vertices];
    103.         int[] facePoint1 = new int[num_triangles];
    104.         int[] facePoint2 = new int[num_triangles];
    105.         int[] facePoint3 = new int[num_triangles];
    106.        
    107.  
    108.        
    109.  
    110.         int[] _triangles = new int[num_triangles * 3];
    111.        
    112.         data.set_caret(vert_dir_offset);
    113.        
    114.         DataBuffer stream_1 = new DataBuffer(data.get_buffer());
    115.         DataBuffer stream_2 = new DataBuffer(data.get_buffer());
    116.         DataBuffer stream_3 = new DataBuffer(data.get_buffer());
    117.        
    118.                 DataBuffer nc5 = new DataBuffer(data.get_buffer());
    119.         DataBuffer nc6 = new DataBuffer(data.get_buffer());
    120.         DataBuffer nc7 = new DataBuffer(data.get_buffer());
    121.        
    122.        
    123.         stream_1.set_caret(x_data_offset);
    124.         stream_2.set_caret(y_data_offset);
    125.         stream_3.set_caret(z_data_offset);
    126.         nc5.caret = vert_skin_offset;
    127.        
    128.         int x_off = 0;
    129.         int y_off = 0;
    130.         int z_off = 0;
    131.         for(int i = 0; i < num_vertices; i++) {
    132.             int flag = data.read_byte();
    133.            
    134.             int cur_x_off = 0;
    135.             if((flag  1) != 0)
    136.                 cur_x_off = stream_1.read_smart();
    137.            
    138.             int cur_y_off = 0;
    139.             if((flag  2) != 0)
    140.                 cur_y_off = stream_2.read_smart();
    141.            
    142.             int cur_z_off = 0;
    143.             if((flag  4) != 0)
    144.                 cur_z_off = stream_3.read_smart();
    145.            
    146.    
    147.             _uv[i] = new Vector2(0, 0);
    148.  
    149.             vertexX[i] = x_off + cur_x_off;
    150.             vertexY[i] = y_off + cur_y_off;
    151.             vertexZ[i] = z_off + cur_z_off;
    152.             x_off = vertexX[i];
    153.             y_off = vertexY[i];
    154.             z_off = vertexZ[i];
    155.            
    156.            
    157.             _vertices[i] = new Vector3(vertexX[i], vertexY[i], vertexZ[i]);
    158.            
    159.         }
    160.        
    161.         //data.set_caret(tri_data_offset);
    162.         data.caret = color_data_offset;
    163.         stream_1.caret = tex_pointer_offset;
    164.         stream_2.caret = tri_priority_offset;
    165.         stream_3.caret = tri_alpha_offset;
    166.        
    167.                 nc5.caret = tri_skin_offset;
    168.         nc6.caret = numtrilen2_offset;
    169.         nc7.caret = unknown_offset;
    170.        
    171.         int offset_1 = 0;
    172.         int offset_2 = 0;
    173.         int offset_3 = 0;
    174.         int start_val = 0;
    175.         int tri_idx = 0;
    176.        
    177.         for(int i = 0; i < num_triangles; i++) {
    178.             int tri_type = stream_1.read_byte();
    179.             if(tri_type == 1) {
    180.                 offset_1 = data.read_smart()+ start_val;
    181.                 start_val = offset_1;
    182.                 offset_2 = data.read_smart()+ start_val;
    183.                 start_val = offset_2;
    184.                 offset_3 = data.read_smart()+ start_val;
    185.                 start_val = offset_3;
    186.                
    187.                 facePoint1[i] = offset_1;
    188.                 facePoint2[i] = offset_2;
    189.                 facePoint3[i] = offset_3;
    190.                
    191.             } else if(tri_type == 2) {
    192.                 offset_2 = offset_3;
    193.                 offset_3 = data.read_smart()+ start_val;
    194.                 start_val = offset_3;
    195.                
    196.                 facePoint1[i] = offset_1;
    197.                 facePoint2[i] = offset_2;
    198.                 facePoint3[i] = offset_3;
    199.                
    200.             } else if(tri_type == 3) {
    201.                 offset_1 = offset_3;
    202.                 offset_3 = data.read_smart()+ start_val;
    203.                 start_val = offset_3;
    204.                
    205.                 facePoint1[i] = offset_1;
    206.                 facePoint2[i] = offset_2;
    207.                 facePoint3[i] = offset_3;
    208.                
    209.             } else if(tri_type == 4) {
    210.                 int tmp = offset_1;
    211.                 offset_1 = offset_2;
    212.                 offset_2 = tmp;
    213.                 offset_3 = data.read_smart()+ start_val;
    214.                 start_val = offset_3;
    215.                
    216.                 facePoint1[i] = offset_1;
    217.                 facePoint2[i] = offset_2;
    218.                 facePoint3[i] = offset_3;
    219.             }
    220.  
    221.            
    222.            
    223. _triangles[tri_idx++] = facePoint1[i] + facePoint2[i] + facePoint3[i];
    224.  
    225.            
    226.  
    227.         }
    228.        
    229.         data.caret = uv_map_offset;
    230.         stream_1.caret = k9;
    231.         stream_2.caret = l9;
    232.         stream_3.caret = i10;
    233.         nc5.caret = j10;
    234.         nc6.caret = k10;
    235.        
    236.        
    237.         _mesh.vertices = _vertices;
    238.         _mesh.triangles = _triangles;
    239.         //_mesh.uv = _uv;
    240.         //_mesh.colors = _colors;
    241.         _mesh.RecalculateNormals();
    242.         _mesh.RecalculateBounds();
    243.        
    244.         cached_models[model_id] = _mesh;
    245.         return _mesh;
    246.     }
    247.    
    248.             public int k9;
    249.         public int l9;
    250.         public int i10;
    251.         public int j10;
    252.         public int k10;
    253.    
    254.     byte[] x = null;
    255.         byte[] O = null;
    256.         byte[] J = null;
    257.         byte[] F = null;
    258.         byte[] cb = null;
    259.         byte[] gb = null;
    260.         byte[] lb = null;
    261.         int[] ab = null;
    262.         int[] kb = null;
    263.         int[] y = null;
    264.         int[] N = null;
    265.         short[] D = null;
    266.  
    267.    
    268.                 int k4 = 0;
    269.         int l4 = 0;
    270.         int i5 = 0;
    271.         int v = 0;
    272.         int hb = 0;
    273.         int P = 0;
    274.         byte G = 0;
    275.    
    276.             int[] texTrianglesPoint1 = null;
    277.         int[] texTrianglesPoint2 = null;
    278.         int[] texTrianglesPoint3 = null;
    279.    
    280.     private string objectname;
    281.     private int model_id;
    282.     private DataBuffer data;
    283.     private int num_vertices;
    284.     private int num_triangles;
    285.     private int num_tex_triangles;
    286.     private int vert_dir_offset;
    287.     private int tri_type_offset;
    288.     private int tri_priority_offset;
    289.     private int tri_skin_offset;
    290.     private int tex_pointer_offset;
    291.     private int vert_skin_offset;
    292.     private int tri_alpha_offset;
    293.     private int tri_data_offset;
    294.     private int color_data_offset;
    295.     private int uv_map_offset;
    296.     private int x_data_offset;
    297.     private int y_data_offset;
    298.     private int z_data_offset;
    299.     private int unknown_offset;
    300.     private int numtrilen2_offset;
    301.    
    302.     private static Dictionary<int, Mesh> cached_models = new Dictionary<int, Mesh>();
    303. }
    Thanks
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    The error most likely means that for a mesh with N vertices, you have one or more indices with values >= N.

    As to why, it's probably due to an error in your code. If you're using Unity 3, you might be able to make use of the new debugging features to track down the cause of the error. Otherwise, you may need to add some debug output to try to narrow things down. You could check all the indices manually, and for any that are out of bounds, print out their values (and the number of vertices for comparison) using Debug.Log(). From there, you can try to determine where and why the invalid indices are being generated.
     
  3. Deleted User

    Deleted User

    Guest

    Thanks for the answer, but I've already figured it out and finished the loader ;)
     
  4. Matt_001

    Matt_001

    Joined:
    Sep 17, 2010
    Posts:
    101
    If you already figured out, you should have posted about it.

    It is so frustrating when you are searching for a solution to your problem and the thread starter don't post back to explain the actual Solution!
     
    Last edited: Mar 8, 2011
    yahlimem and Madsen87 like this.
  5. kablammyman

    kablammyman

    Joined:
    Nov 22, 2010
    Posts:
    507
    agreed! I'm trying to write a custom loader as well and could use a lil help with this error!
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Simple, stop making your triangles refer to vertices that don't exist. ;) Actually it is that simple, but why that's happening depends on the logic of your code. Basically, what Jesse said.

    --Eric
     
    haroonalam likes this.
  7. Matt_001

    Matt_001

    Joined:
    Sep 17, 2010
    Posts:
    101
    Maybe I should have stated my problem before in case someone could use it, I eventually found the solution. I was copying arrays of Vertices and Indices in order to modify part of the mesh. What happened is when I copied back my triangles inside the Unity object it was referencing to outbounds index because they had been changed in the modifying process.

    So be careful when you are doing modification in those stuff. You shall ensure that your index are changed everywhere before reloading them.

    A detailed explanation can be found here.
     
  8. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    one way to recreate this problem is , when setting the mesh triangles at the end of the procedural code, to set them before you have set the vertices etc. write the triangles array into the mesh after having written the vertices in.
     
  9. Gapp

    Gapp

    Joined:
    Oct 30, 2013
    Posts:
    2
    Quick note, don't set triangles before you set vertices in the mesh.
     
    Eagle-Bound likes this.
  10. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    I get this error using only Unity 5.0.2f1 and the default blender import script. It happens when my blender mesh has Shading set to Smooth in Object mode.
     
  11. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    smooth shading perhaps merges some vertices.
     
  12. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    Some meshes I have imported from Blender bring this up in play mode if they are on static objects. Unfortunately the error doesn't tell me which mesh is causing the problem. Also, I have no idea what to do to 'fix' the mesh - mucking about with smoothing etc in Blender doesn't seem to help.
     
    Last edited: Feb 16, 2016
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Please make a new post - it's unlikely your problem is the same problem as this very, very old question.
     
  14. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    Sure, I'll do that. But it is the same error message - "Failed Setting Traingles, some indices are referencing out of bounds vertices", so I surmise that though perhaps the cause may be different, the problem is the same.
     
  15. Jilbarkus

    Jilbarkus

    Joined:
    Oct 6, 2022
    Posts:
    6
    anyone still running into this problem? try this:
    after creating a new Mesh, do Mesh.Clear() before setting any vertices, normals, UVs or triangles.
    for some reason this works....
    Mesh mesh = new Mesh();
    mesh.Clear();
    mesh.SetVertices(vertices);
    etc...