예제 #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);
                        }
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Batch buildings
        /// </summary>
        /// <param name="value">Batch the building ?</param>
        public virtual void Batch(bool value, BaseBuilding updatedBuilding, bool added)
        {
            if (UCSavingManager.IsLoading || !UCSettings.instance.UCBatchingEnabled)
            {
                return;
            }

            if (value)
            {
                Batch(false, updatedBuilding, added); // clear the old data

                if (initializedBatch && updatedBuilding != null)
                {
                    BatchUtility.UpdateBatchData(GetUpdatedBatchData(updatedBuilding, added), added, ref batchData);
                }
                else if (!initializedBatch)
                {
                    batchData = BatchUtility.CompileInitialBatchData(batchedFilters.ToArray(), true);
                }

                Mesh mesh;

                for (int i = 0; i < batchData.Count; i++)
                {
                    var go = new GameObject();
                    go.name               = "Group Batched Collider";
                    go.gameObject.layer   = LayersData.DefaultBuildingLayer;
                    go.transform.parent   = transform;
                    go.transform.position = transform.position;

                    var buildingBatch = go.AddComponent <BaseBuildingBatcher>();
                    buildingBatch.group = this;

                    mesh = new Mesh();

                    mesh.CombineMeshes(batchData[i].combineInstances.ToArray());

                    /* COLLIDER BATCHING CURRENTLY DISABLED.
                     * var meshCollider = go.AddComponent<MeshCollider>();
                     * meshCollider.sharedMesh = mesh;
                     */

                    if (batchData[i].Materials != null)
                    {
                        var renderer = go.AddComponent <MeshRenderer>();
                        var filter   = go.AddComponent <MeshFilter>();

                        filter.mesh        = mesh;
                        renderer.materials = batchData[i].Materials;
                    }

                    batchInstances.Add(go.transform);

                    if (OnBatchDoneEvent != null)
                    {
                        OnBatchDoneEvent(go, mesh);
                    }
                }

                initializedBatch = true;
            }
            else
            {
                for (int i = 0; i < batchInstances.Count; i++)
                {
                    if (batchInstances[i] != null)
                    {
                        Destroy(batchInstances[i].gameObject);
                    }
                }
                batchInstances.Clear();
            }

            if (OnGroupBatchedEvent != null)
            {
                OnGroupBatchedEvent(value);
            }
        }