Пример #1
0
        /// <summary>
        /// Return combined batched meshes (support submeshes).
        /// </summary>
        /// <param name="batch">What MeshFilters to batch</param>
        /// <returns>the batched meshes</returns>
        public static BatchData CompileInitialBatchData(MeshFilter[] batch, bool value)
        {
            BatchData       batchData       = new BatchData();
            CombineInstance combineInstance = new CombineInstance();
            MeshFilter      current;

            BatchClass batchInstance;

            Material[] materials;

            Vector3 tempPos;

            for (int i = 0; i < batch.Length; i++)
            {
                current   = batch[i];
                materials = null;

                if (current != null)
                {
                    tempPos = current.transform.position; // assign this to avoid world space (using this method cause -> worldToLocal changes size, we dont want that.)

                    current.transform.position = current.transform.position - current.transform.root.position;

                    combineInstance.transform = current.transform.localToWorldMatrix;
                    combineInstance.mesh      = current.mesh;

                    current.transform.position = tempPos;

                    materials = HandleRenders(current, value);

                    batchInstance = batchData.Batchable(materials, current.mesh.vertexCount);
                    if (batchInstance != null)
                    {
                        batchInstance.AddFilter(current, combineInstance);
                    }
                    else
                    {
                        batchData.Add(new BatchClass(materials)).AddFilter(current, combineInstance);
                    }
                }
            }

            return(batchData);
        }
Пример #2
0
        /// <summary>
        /// Update our batch data
        /// </summary>
        /// <param name="filters">what filters you want to add / remove</param>
        /// <param name="Add">Are we adding an instance ? or removing it ?</param>
        /// <param name="batchData">returns the edited batch data</param>
        public static void UpdateBatchData(MeshFilter[] filters, bool Add, ref BatchData batchData)
        {
            BatchClass currentBatch;
            MeshFilter currentFilter;

            if (!Add)
            {
                for (int i = 0; i < filters.Length; i++)
                {
                    currentFilter = filters[i];

                    for (int b = 0; b < batchData.Count; b++)
                    {
                        currentBatch = batchData[b];

                        for (int c = 0; c < currentBatch.Filters.Count; c++)
                        {
                            if (currentBatch.Filters[c] == currentFilter)
                            {
                                currentBatch.RemoveFilter(c);
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                MeshRenderer    currentRenderer;
                CombineInstance currentCombineMesh = new CombineInstance();
                Vector3         tempPos;

                for (int i = 0; i < filters.Length; i++)
                {
                    if (filters[i] == null)
                    {
                        continue;
                    }

                    currentFilter   = filters[i];
                    currentRenderer = currentFilter.GetComponent <MeshRenderer>();

                    if (currentRenderer != null && currentFilter != null)
                    {
                        currentBatch = batchData.Batchable(currentRenderer.materials, currentFilter.mesh.vertexCount);

                        HandleRenders(currentFilter, Add);

                        tempPos = currentFilter.transform.position; // assign this to avoid world space (using this method cause -> worldToLocal changes size, we dont want that.)

                        currentFilter.transform.position = currentFilter.transform.position - currentFilter.transform.root.position;

                        currentCombineMesh.transform = currentFilter.transform.localToWorldMatrix;
                        currentCombineMesh.mesh      = currentFilter.mesh;

                        currentFilter.transform.position = tempPos;

                        if (currentBatch != null)
                        {
                            currentBatch.AddFilter(currentFilter, currentCombineMesh);
                        }
                        else
                        {
                            batchData.Add(new BatchClass(currentRenderer.materials)).AddFilter(currentFilter, currentCombineMesh);
                        }
                    }
                }
            }
        }