def compute_offsets(size = 65535)
      # WebGL limits type of index buffer values to 16-bit.
      # TODO: check what the limit is for OpenGL, as we aren't using WebGL here

      indices = self[:index].array
      vertices = self[:position].array

      faces_count = indices.length / 3

      # puts "Computing buffers in offsets of #{size} -> indices:#{indices.length} vertices:#{vertices.length}"
      # puts "Faces to process: #{(indices.length/3)}"
      # puts "Reordering #{verticesCount} vertices."

      sorted_indices = Array.new(indices.length) # 16-bit (Uint16Array in THREE.js)
      index_ptr = 0
      vertex_ptr = 0

      offsets = [DrawCall.new(0, 0, 0)]
      offset = offsets.first

      duplicated_vertices = 0
      new_vertice_maps = 0
      face_vertices = Array.new(6) # (Int32Array)
      vertex_map = Array.new(vertices.length) # (Int32Array)
      rev_vertex_map = Array.new(vertices.length) # (Int32Array)
      vertices.each_index do |j|
        vertex_map[j] = -1
        rev_vertex_map[j] = -1
      end

      # Traverse every face and reorder vertices in the proper offsets of 65k.
      # We can have more than 65k entries in the index buffer per offset, but only reference 65k values.
      faces_count.times do |findex|
        new_vertice_maps = 0

        3.times do |vo|
          vid = indices[findex * 3 * vo]
          if vertex_map[vid] == -1
            # unmapped vertice
            face_vertices[vo * 2] = vid
            face_vertices[vo * 2 + 1] = -1
            new_vertice_maps += 1
          elsif vertex_map[vid] < offset.index
            # reused vertices from previous block (duplicate)
            face_vertices[vo * 2] = vid
            face_vertices[vo * 2 + 1] = -1
            duplicated_vertices += 1
          else
            # reused vertice in the current block
            face_vertices[vo * 2] =vid
            face_vertices[vo * 2 + 1] = vertec_map[vid]
          end
        end

        face_max = vertex_ptr + new_vertex_maps
        if face_max > offset.index + size
          new_offset = DrawCall.new(index_ptr, 0, vertex_ptr)
          offsets << new_offset
          offset = new_offset

          # Re-evaluate reused vertices in light of new offset.
          (0...6).step(2) do |v|
            new_vid = face_vertices[v + 1]
            if (new_vid > -1 && new_vid < offset.index)
              faceVertices[v + 1] = -1
            end
          end

          # Reindex the face.
          (0...6).step(2) do |v|
            vid = face_vertices[v]
            new_vid = face_vertices[v + 1]

            if new_vid == -1
              new_vid = vertex_ptr
              vertex_ptr += 1
            end

            vertex_map[vid] = new_vid
            rev_vertex_map[new_vid] = vid
            sorted_indices [index_ptr] = new_vid - offset.index # XXX: overflows at 16bit
            index_ptr += 1
            offset.count += 1
          end
        end

        # Move all attribute values to map to the new computed indices , also expand the vertice stack to match our new vertexPtr.
        self.reorder_buffers(sorted_indices, rev_vertex_map, vertex_ptr)
        @draw_calls = offsets

        # order_time = Time.now
        # puts "Reorder time: #{(order_time - s)}ms"
        # puts "Duplicated #{duplicated_vertices} vertices."
        # puts "Compute Buffers time: #{(Time.now - s)}ms"
        # puts "Draw offsets: #{offsets.length}"

        offsets
      end
    end
def compute_offsets(size = 65535)
      # WebGL limits type of index buffer values to 16-bit.
      # TODO: check what the limit is for OpenGL, as we aren't using WebGL here

      indices = self[:index].array
      vertices = self[:position].array

      faces_count = indices.length / 3

      # puts "Computing buffers in offsets of #{size} -> indices:#{indices.length} vertices:#{vertices.length}"
      # puts "Faces to process: #{(indices.length/3)}"
      # puts "Reordering #{verticesCount} vertices."

      sorted_indices = Array.new(indices.length) # 16-bit (Uint16Array in THREE.js)
      index_ptr = 0
      vertex_ptr = 0

      offsets = [DrawCall.new(0, 0, 0)]
      offset = offsets.first

      duplicated_vertices = 0
      new_vertice_maps = 0
      face_vertices = Array.new(6) # (Int32Array)
      vertex_map = Array.new(vertices.length) # (Int32Array)
      rev_vertex_map = Array.new(vertices.length) # (Int32Array)
      vertices.each_index do |j|
        vertex_map[j] = -1
        rev_vertex_map[j] = -1
      end

      # Traverse every face and reorder vertices in the proper offsets of 65k.
      # We can have more than 65k entries in the index buffer per offset, but only reference 65k values.
      faces_count.times do |findex|
        new_vertice_maps = 0

        3.times do |vo|
          vid = indices[findex * 3 * vo]
          if vertex_map[vid] == -1
            # unmapped vertice
            face_vertices[vo * 2] = vid
            face_vertices[vo * 2 + 1] = -1
            new_vertice_maps += 1
          elsif vertex_map[vid] < offset.index
            # reused vertices from previous block (duplicate)
            face_vertices[vo * 2] = vid
            face_vertices[vo * 2 + 1] = -1
            duplicated_vertices += 1
          else
            # reused vertice in the current block
            face_vertices[vo * 2] =vid
            face_vertices[vo * 2 + 1] = vertec_map[vid]
          end
        end

        face_max = vertex_ptr + new_vertex_maps
        if face_max > offset.index + size
          new_offset = DrawCall.new(index_ptr, 0, vertex_ptr)
          offsets << new_offset
          offset = new_offset

          # Re-evaluate reused vertices in light of new offset.
          (0...6).step(2) do |v|
            new_vid = face_vertices[v + 1]
            if (new_vid > -1 && new_vid < offset.index)
              faceVertices[v + 1] = -1
            end
          end

          # Reindex the face.
          (0...6).step(2) do |v|
            vid = face_vertices[v]
            new_vid = face_vertices[v + 1]

            if new_vid == -1
              new_vid = vertex_ptr
              vertex_ptr += 1
            end

            vertex_map[vid] = new_vid
            rev_vertex_map[new_vid] = vid
            sorted_indices [index_ptr] = new_vid - offset.index # XXX: overflows at 16bit
            index_ptr += 1
            offset.count += 1
          end
        end

        # Move all attribute values to map to the new computed indices , also expand the vertice stack to match our new vertexPtr.
        self.reorder_buffers(sorted_indices, rev_vertex_map, vertex_ptr)
        @draw_calls = offsets

        # order_time = Time.now
        # puts "Reorder time: #{(order_time - s)}ms"
        # puts "Duplicated #{duplicated_vertices} vertices."
        # puts "Compute Buffers time: #{(Time.now - s)}ms"
        # puts "Draw offsets: #{offsets.length}"

        offsets
      end
    end
Compute the draw offset for large models by chunking the index buffer into chunks of 65k addressable vertices.
 This method will effectively rewrite the index buffer and remap all attributes to match the new indices.
 WARNING: This method will also expand the vertex count to prevent sprawled triangles across draw offsets.
 size - Defaults to 65535, but allows for larger or smaller chunks.