private void ProcessCellGrass(FoliageCellDataRuntime runtimeCell)
        {
            for (int i = 0, len = runtimeCell.m_FoliageDataSubdivided.Length; i < len; i++)
            {
                FoliageCellSubdividedDataRuntime runtimeCellSubdivided = runtimeCell.m_FoliageDataSubdivided[i].Value;
                float subdivDistance = runtimeCellSubdivided.m_Bounds.SqrDistance(m_CurrentFrameCameraPosition);

                if (subdivDistance <= m_MaxDistanceGrassSqr && GeometryUtility.TestPlanesAABB(m_CameraPlanes, runtimeCellSubdivided.m_Bounds))
                {
                    ProcessSubdividedCell(runtimeCell, runtimeCellSubdivided, subdivDistance);
                    m_DrawStats.m_ProcessedCellsSubdiv++;
                }
            }
        }
        private void ProcessSubdividedCell(FoliageCellDataRuntime cell, FoliageCellSubdividedDataRuntime cellSubdivided, float distance)
        {
            for (int foliageType = 0, foliageTypeCount = cellSubdivided.m_TypeHashLocationsRuntime.Length; foliageType < foliageTypeCount; foliageType++)
            {
                FoliageType type = m_FoliageTypes[cellSubdivided.m_TypeHashLocationsRuntime[foliageType].Key];

                float maxTypeDist = type.m_RenderInfo.m_MaxDistance * type.m_RenderInfo.m_MaxDistance;

                if (distance <= maxTypeDist)
                {
                    var batches = cellSubdivided.m_TypeHashLocationsRuntime[foliageType].Value.m_EditTime;

                    // Set the MPB values that are universal for all grass types
                    MaterialPropertyBlock mpb = type.m_RuntimeData.m_TypeMPB;

                    mpb.SetFloat(m_ShaderIDCritiasFoliageDistance, type.m_RenderInfo.m_MaxDistance);
                    mpb.SetFloat(m_ShaderIDCritiasFoliageDistanceSqr, maxTypeDist);

                    // TODO: Set bend data if we have it for this type
                    if (type.m_EnableBend)
                    {
                        mpb.SetFloat(m_ShaderIDCritiasBendDistance, type.m_BendDistance);
                        mpb.SetFloat(m_ShaderIDCritiasBendScale, type.m_BendPower);
                        mpb.SetVector(m_ShaderIDCritiasBendPosition, m_CurrentFrameBendPosition);
                    }

                    // Get data from the type
                    Mesh     mesh = type.m_RuntimeData.m_LODDataGrass.m_Mesh;
                    Material mat  = type.m_RuntimeData.m_LODDataGrass.m_Material;

                    // Only if we have the type for rendering indirect
                    if (type.RenderIndirect && m_CurrentFrameAllowIndirect)
                    {
                        long indirectCachedDataKey = ((((long)cell.m_Position.GetHashCode()) << 32) | ((long)cellSubdivided.m_Position.GetHashCode())) + type.m_Hash; // * 0xF01226E02D41B

                        if (m_CachedGPUBufferData.ContainsKey(indirectCachedDataKey) == false)
                        {
                            GPUBufferCellCachedData data = new GPUBufferCellCachedData();

                            // Merge the buffers
                            Matrix4x4[] allInstances;

                            // Build all the data
                            if (batches.Length > 1)
                            {
                                int totalCount = 0;
                                for (int batchIdx = 0; batchIdx < batches.Length; batchIdx++)
                                {
                                    totalCount += batches[batchIdx].Length;
                                }

                                List <Matrix4x4> concat = new List <Matrix4x4>(totalCount);

                                for (int batchIdx = 0; batchIdx < batches.Length; batchIdx++)
                                {
                                    concat.AddRange(batches[batchIdx]);
                                }

                                allInstances = concat.ToArray();
                            }
                            else
                            {
                                allInstances = batches[0];
                            }

                            // Set the position data
                            data.m_BufferPositions = new ComputeBuffer(allInstances.Length, 64);
                            data.m_BufferPositions.SetData(allInstances);

                            // Set the arguments
                            data.m_BufferArguments = new ComputeBuffer(1, m_TempDrawArgs.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
                            data.m_IndexCount      = mesh.GetIndexCount(0);

                            // Set the instance count
                            data.m_InstanceCount = (uint)allInstances.Length;

                            // Add the data. The cache will take care of clearing the data that is in excess
                            m_CachedGPUBufferData.Add(indirectCachedDataKey, data);
                        }

                        GPUBufferCellCachedData indirectData = m_CachedGPUBufferData[indirectCachedDataKey];

                        // Set the buffer positions
                        mpb.SetBuffer(m_ShaderIDCritiasInstanceBuffer, indirectData.m_BufferPositions);

                        // Set the draw count and send it
                        m_TempDrawArgs[0] = indirectData.m_IndexCount;
                        m_TempDrawArgs[1] = (uint)(indirectData.m_InstanceCount * m_Settings.m_GrassDensity);
                        indirectData.m_BufferArguments.SetData(m_TempDrawArgs);

                        // Draw without shadows
                        Graphics.DrawMeshInstancedIndirect(mesh, 0, mat, cellSubdivided.m_Bounds, indirectData.m_BufferArguments, 0, mpb, ShadowCastingMode.Off, true, m_CurrentFrameLayer, m_CurrentFrameCameraDraw);
                    }
                    else
                    {
                        // Cast shadow only if the type allows
                        ShadowCastingMode castShadow = type.m_RenderInfo.m_CastShadow ? ShadowCastingMode.On : ShadowCastingMode.Off;

                        // Pass with the MPB the data related to per-type distance
                        for (int i = 0, batchCount = batches.Length; i < batchCount; i++)
                        {
                            Graphics.DrawMeshInstanced(mesh, 0, mat, batches[i], (int)(batches[i].Length * m_Settings.m_GrassDensity), mpb, castShadow, true, m_CurrentFrameLayer, m_CurrentFrameCameraDraw);
                            m_DrawStats.m_ProcessedDrawCalls++;
                        }
                    }
                }
            }
        }
        /**
         * Load from file the runtime version of the data
         */
        public static FoliageDataRuntime LoadFromFileRuntime(string filename)
        {
            FoliageData data = LoadFromFileEditTime(filename);

            // Build the runtime data from the edit time data
            FoliageDataRuntime runtime = new FoliageDataRuntime();

            foreach (var hashedCell in data.m_FoliageData)
            {
                FoliageCellData        editCell    = hashedCell.Value;
                FoliageCellDataRuntime runtimeCell = new FoliageCellDataRuntime();

                // Set the data. Note that we only need the extended bounds
                runtimeCell.m_Bounds   = editCell.m_BoundsExtended;
                runtimeCell.m_Position = editCell.m_Position;

                // Build the tree instance data
                int idx = -1;
                runtimeCell.m_TypeHashLocationsRuntime = new FoliageKeyValuePair <int, FoliageTuple <FoliageInstance[]> > [editCell.m_TypeHashLocationsEditor.Count];
                foreach (var instances in editCell.m_TypeHashLocationsEditor)
                {
                    idx++;

                    List <FoliageInstance> allTreeInstances = new List <FoliageInstance>();
                    var labeledInstances = instances.Value;

                    // Build all the data from the labeled data
                    foreach (List <FoliageInstance> inst in labeledInstances.Values)
                    {
                        allTreeInstances.AddRange(inst);
                    }

                    // We will build the world matrix for trees
                    for (int i = 0; i < allTreeInstances.Count; i++)
                    {
                        FoliageInstance inst = allTreeInstances[i];
                        inst.BuildWorldMatrix();
                        allTreeInstances[i] = inst;
                    }

                    // Don't forget to trim all excess instances!
                    allTreeInstances.TrimExcess();

                    #if UNITY_EDITOR
                    if (allTreeInstances.Count == 0)
                    {
                        Debug.Assert(false, "Count 0!");
                    }
                    #endif

                    runtimeCell.m_TypeHashLocationsRuntime[idx] = new FoliageKeyValuePair <int, FoliageTuple <FoliageInstance[]> >(instances.Key, new FoliageTuple <FoliageInstance[]>(allTreeInstances.ToArray()));
                }

                // Build the grass instance data from the subdivided cells
                List <FoliageKeyValuePair <int, FoliageCellSubdividedDataRuntime> > foliageCellDataSubdivided = new List <FoliageKeyValuePair <int, FoliageCellSubdividedDataRuntime> >(editCell.m_FoliageDataSubdivided.Count);

                foreach (var hashedSubdividedCell in editCell.m_FoliageDataSubdivided)
                {
                    FoliageCellSubdividedData        editSubdividedCell    = hashedSubdividedCell.Value;
                    FoliageCellSubdividedDataRuntime runtimeSubdividedCell = new FoliageCellSubdividedDataRuntime();

                    // Set the data
                    runtimeSubdividedCell.m_Bounds   = editSubdividedCell.m_Bounds;
                    runtimeSubdividedCell.m_Position = editSubdividedCell.m_Position;

                    idx = -1;
                    runtimeSubdividedCell.m_TypeHashLocationsRuntime = new FoliageKeyValuePair <int, FoliageTuple <Matrix4x4[][]> > [editSubdividedCell.m_TypeHashLocationsEditor.Count];
                    foreach (var instances in editSubdividedCell.m_TypeHashLocationsEditor)
                    {
                        idx++;

                        List <FoliageInstance> allGrassInstances = new List <FoliageInstance>();
                        var labeledInstances = instances.Value;

                        foreach (List <FoliageInstance> inst in labeledInstances.Values)
                        {
                            allGrassInstances.AddRange(inst);
                        }

                        #if UNITY_EDITOR
                        if (allGrassInstances.Count == 0)
                        {
                            Debug.Assert(false, "Count 0!");
                        }
                        #endif

                        // Build the multi-array data
                        int           ranges  = Mathf.CeilToInt(allGrassInstances.Count / (float)FoliageGlobals.RENDER_BATCH_SIZE);
                        Matrix4x4[][] batches = new Matrix4x4[ranges][];

                        for (int i = 0; i < ranges; i++)
                        {
                            List <FoliageInstance> range = allGrassInstances.GetRange(i * FoliageGlobals.RENDER_BATCH_SIZE,
                                                                                      i * FoliageGlobals.RENDER_BATCH_SIZE + FoliageGlobals.RENDER_BATCH_SIZE > allGrassInstances.Count
                                    ? allGrassInstances.Count - i * FoliageGlobals.RENDER_BATCH_SIZE
                                    : FoliageGlobals.RENDER_BATCH_SIZE);

                            batches[i] = range.ConvertAll <Matrix4x4>((x) => x.GetWorldTransform()).ToArray();
                        }

                        // Set the data
                        runtimeSubdividedCell.m_TypeHashLocationsRuntime[idx] = new FoliageKeyValuePair <int, FoliageTuple <Matrix4x4[][]> >(instances.Key, new FoliageTuple <Matrix4x4[][]>(batches));
                    }

                    // Add the subdivided runtime cell
                    foliageCellDataSubdivided.Add(new FoliageKeyValuePair <int, FoliageCellSubdividedDataRuntime>(hashedSubdividedCell.Key, runtimeSubdividedCell));
                }

                // Build the subdivided data
                runtimeCell.m_FoliageDataSubdivided = foliageCellDataSubdivided.ToArray();


                // Add the runtime cell
                runtime.m_FoliageData.Add(hashedCell.Key, runtimeCell);
            }

            // Good for GC
            data = null;

            return(runtime);
        }