public GPUInstanceCellItem RemoveAt(int index) { if (index >= m_Size || index < 0) { throw new IndexOutOfRangeException(); } GPUInstanceCellItem cellItem = m_Items[index]; GPUInstanceCellItem[] new_arr = new GPUInstanceCellItem[m_Size - 1]; for (int i = 0, j = 0; i < m_Size; i++, j++) { if (i == index) { j--; // Skip over rm_index by fixing j temporarily } else { new_arr[j] = m_Items[i]; } } m_Items = new_arr; m_Capacity = --m_Size; return(cellItem); }
public void Set(int index, GPUInstanceCellItem element) { if (index >= m_Size || index < 0) { throw new System.IndexOutOfRangeException(); } m_Items[index] = element; }
public bool Remove(GPUInstanceCellItem element) { int index = IndexOf(element); if (index == -1) { return(false); } RemoveAt(index); return(true); }
public bool RemoveCellItem(GPUInstanceCellItem cellItem) { if (cellItem == null) { return(true); } int cellIndex = cellItem.cellIndex; int index = m_Cells[cellIndex].IndexOf(cellItem); if (index == -1) { return(false); } // Debug.LogError("Remove:" + index); m_Cells[cellIndex].Set(index, null); // m_DrawCount--; return(true); }
public void Add(GPUInstanceCellItem element) { if (m_Size + 1 >= GPUInstanceDefine.MAX_CAPACITY) { Debug.LogError("MAX_CAPACITY Cant add new element :" + element.cellIndex); // return false; } if (m_Size + 1 >= m_Capacity) //注意扩容的消耗 { // Debug.LogError("Try to double capacity"); if (m_Capacity == 0) { m_Capacity = 1; } else { m_Capacity *= 2; } // Debug.LogError(m_Capacity); m_Capacity = Mathf.Min(GPUInstanceDefine.MAX_CAPACITY, m_Capacity); GPUInstanceCellItem[] new_arr = new GPUInstanceCellItem[m_Capacity]; for (int i = 0; i < m_Size; i++) { new_arr[i] = m_Items[i]; } m_Items = new_arr; Matrix4x4[] new_matrixArr = new Matrix4x4[m_Capacity]; for (int i = 0; i < m_Size; i++) { new_matrixArr[i] = m_TRSMatrices[i]; } m_TRSMatrices = new_matrixArr; OnCapacityChange(); } m_Items[m_Size++] = element; // return true; }
public int IndexOf(GPUInstanceCellItem element) { for (int i = 0; i < m_Size; i++) { if (element == null) { if (m_Items[i] == null) { return(i); } } else { if (element.Equals(m_Items[i])) { return(i); } } } return(-1); }
public void AddCellItem(GPUInstanceCellItem cellItem) { for (int i = 0; i < m_Cells.Count; i++) { int index = m_Cells[i].IndexOf(null); if (index != -1) { m_Cells[i].Set(index, cellItem); if (cellItem != null) { cellItem.cellIndex = m_Cells[i].CellIndex; } // Debug.LogError("找到null,直接set:" + m_Cells[i].CellIndex); return; } } GPUInstanceCell cell; //寻找合适的 if (m_DrawCount + 1 >= m_DrawCapacity - m_Cells.Count + 1) { //创建新的Cell cell = CreateCell(); } else { cell = m_Cells[m_Cells.Count - 1]; } if (cellItem != null) { cellItem.cellIndex = cell.CellIndex; // Debug.LogError(cellItem.cellIndex); } cell.Add(cellItem); m_DrawCount++; }