예제 #1
0
        public static void FlushObjects(Rect terrainRect, bool clearCache = true)
        {
            VegetationSystemPro system = GameObject.FindObjectOfType <VegetationSystemPro>();

            if (system == null)
            {
                return;
            }

            PersistentVegetationStorage storage = system.PersistentVegetationStorage;

            PersistentVegetationStoragePackage storagePackage = system.PersistentVegetationStorage.PersistentVegetationStoragePackage;

            if (storagePackage == null)
            {
                return;
            }

            List <VegetationCell> overlapCellList = new List <VegetationCell>();

            system.VegetationCellQuadTree.Query(terrainRect, overlapCellList);

            for (int i = 0; i < overlapCellList.Count; i++)
            {
                int cellIndex = overlapCellList[i].Index;

                //storagePackage.PersistentVegetationCellList[cellIndex].ClearCell();

                var infoList = storagePackage.PersistentVegetationCellList[cellIndex].PersistentVegetationInfoList;
                for (int j = 0; j < infoList.Count; j++)
                {
                    var itemList = infoList[j].VegetationItemList;

                    for (int k = itemList.Count - 1; k >= 0; k--)
                    {
                        Vector3 pos  = itemList[k].Position + system.VegetationSystemPosition;
                        Vector2 pos2 = pos.V2();

                        if (terrainRect.Contains(pos2))
                        {
                            itemList.RemoveAt(k);
                            //storage.RemoveVegetationItemInstance(infoList[j].VegetationItemID, pos, 1, clearCellCache:false);
                        }
                    }
                }

                //VegetationItemIndexes indexes = VegetationSystemPro.GetVegetationItemIndexes(vegetationItemID);
                //system.ClearCache(overlapCellList[i],indexes.VegetationPackageIndex,indexes.VegetationItemIndex);
            }

            //if (clearCache)
            //{
            //	for (int i=0; i<system.VegetationCellList.Count; i++)
            //		system.VegetationCellList[i].ClearCache();
            //}

            system.RefreshBillboards();
        }
예제 #2
0
        public static void SetObjects(List <ObjectPool.Transition>[] allTransitions, string[] allIds)
        {
            if (system == null)
            {
                system = GameObject.FindObjectOfType <VegetationSystemPro>();
            }
            if (system == null)
            {
                throw new System.Exception("MapMagic could not find Vegetation System in scene./n Add Window -> AwesomeTechnologies -> Vegetation System to scene");
            }

            PersistentVegetationStorage storage = system.PersistentVegetationStorage;

            PersistentVegetationStoragePackage storagePackage = system.PersistentVegetationStorage.PersistentVegetationStoragePackage;

            if (storagePackage == null)
            {
                throw new System.Exception("Vegetation System has no storage package assigned to be used with MapMagic./nAssign a Persistent Vegetation Storage Package to Persistent Vegetation Storage component and initialize it.");
            }

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

                var itemInfo = system.GetVegetationItemInfo(allIds[i]);
                if (itemInfo == null)
                {
                    throw new System.Exception("Item applied by MapMagic is not present in Vegetation System storage");
                }

                foreach (ObjectPool.Transition obj in allTransitions[i])
                {
                    storage.AddVegetationItemInstance(
                        allIds[i],
                        obj.pos,
                        obj.scale,
                        obj.rotation,
                        applyMeshRotation: true,
                        vegetationSourceID: VS_MM_id,
                        distanceFalloff: 1,
                        clearCellCache: true);
                }
            }

            //for (int i=0; i<system.VegetationCellList.Count; i++)
            //	system.VegetationCellList[i].ClearCache();
            //system.ClearCache();

            system.RefreshBillboards();

                                #if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(storagePackage);
            UnityEditor.EditorUtility.SetDirty(system);
                                #endif
        }
예제 #3
0
        public VegetationSystem CopyVegetationSystem(GameObject srcGameObj, Terrain terrain)
        {
            GameObject vegetationSystemObject = GameObject.Instantiate(srcGameObj);

            vegetationSystemObject.transform.SetParent(terrain.transform);

            VegetationSystem vegetationSystem = vegetationSystemObject.GetComponent <VegetationSystem>();

            if (vegetationSystem == null)
            {
                vegetationSystem = vegetationSystemObject.AddComponent <VegetationSystem>();
            }

            if (package != null)
            {
                vegetationSystem.AddVegetationPackage(package, true);
            }

            if (vegetationSystemObject.GetComponent <TerrainSystem>() == null)
            {
                vegetationSystemObject.AddComponent <TerrainSystem>();
            }

            BillboardSystem billboardSystem = vegetationSystemObject.GetComponent <BillboardSystem>();

            if (billboardSystem == null)
            {
                billboardSystem = vegetationSystemObject.AddComponent <BillboardSystem>();
            }
            billboardSystem.VegetationSystem = vegetationSystem;

            PersistentVegetationStorage srcStorage = vegetationSystemObject.GetComponent <PersistentVegetationStorage>();

            if (srcStorage != null)
            {
                GameObject.DestroyImmediate(srcStorage);
            }

            PersistentVegetationStorage persistentVegetationStorage = vegetationSystemObject.AddComponent <PersistentVegetationStorage>();

            persistentVegetationStorage.VegetationSystem = vegetationSystem;
            vegetationSystem.AutoselectTerrain           = false;
            vegetationSystem.currentTerrain = terrain;
            //vegetationSystem.AutomaticWakeup = true;

            PersistentVegetationStoragePackage persistentVegetationStoragePackage = ScriptableObject.CreateInstance <PersistentVegetationStoragePackage>();

            persistentVegetationStorage.PersistentVegetationStoragePackage         = persistentVegetationStoragePackage;
            persistentVegetationStorage.AutoInitPersistentVegetationStoragePackage = true;

            vegetationSystem.SetSleepMode(false);

            return(vegetationSystem);
        }
예제 #4
0
    void DoAddPersistentStorage(VegetationSystem _vegetationSystem)
    {
        _vegetationSystem.AutomaticWakeup = true;
        // NB seems redundant to recurse into parent object - but this is needed to get the terrain name
        GameObject parentTerrainGo = _vegetationSystem.gameObject.transform.parent.gameObject;
        PersistentVegetationStorage _persistentVegetationStorage = parentTerrainGo.GetComponentInChildren <PersistentVegetationStorage>();

        PersistentVegetationStoragePackage newPersistentVegetationStoragePackage = ScriptableObject.CreateInstance <PersistentVegetationStoragePackage>();

        AssetDatabase.CreateAsset(newPersistentVegetationStoragePackage, _assetsPath + "/" + parentTerrainGo.name + _DEFAULT_SUFFIX + ".asset");
        _persistentVegetationStorage.PersistentVegetationStoragePackage = newPersistentVegetationStoragePackage;

        // initialize
        _persistentVegetationStorage.InitializePersistentStorage();

        if (_debugToLog)
        {
            Debug.Log("Added storage asset for " + parentTerrainGo.name);
        }

        if (_autoBake)
        {
            if (_debugToLog)
            {
                Debug.Log("Auto Baking " + parentTerrainGo.name);
            }
            //if (_vegetationSystem.GetSleepMode())
            //{
            _vegetationSystem.SetSleepMode(false);     // wake up VegetationSystem, needed for prefab
            //}
            // now bake
            List <string> vegetationItemIdList =
                VegetationPackageEditorTools.CreateVegetationInfoIdList(_persistentVegetationStorage.VegetationSystem.CurrentVegetationPackage);


            for (int i = 0; i <= vegetationItemIdList.Count - 1; i++)
            {
                _persistentVegetationStorage.BakeVegetationItem(vegetationItemIdList[i]);
            }
            _persistentVegetationStorage.VegetationSystem.DelayedClearVegetationCellCache();
            EditorUtility.SetDirty(_persistentVegetationStorage.PersistentVegetationStoragePackage);
            EditorUtility.SetDirty(_persistentVegetationStorage.VegetationSystem.CurrentVegetationPackage);
            if (_debugToLog)
            {
                Debug.Log("Finished Baking for " + parentTerrainGo.name);
            }
        }
    }
    void AssignTerrainToVS(Terrain terrain, VegetationSystem vs, VegetationPackage vegetationPackage, PersistentVegetationStoragePackage storagePackage)
    {
        vs.AutoselectTerrain = false;

        vs.CurrentVegetationPackage = vegetationPackage;
        PersistentVegetationStorage storage = vs.GetComponent <PersistentVegetationStorage>();

        if (storage != null)
        {
            storage.SetPersistentVegetationStoragePackage(storagePackage);
        }
        if (vs.InitDone)
        {
            vs.HotswapTerrain(terrain, new UnityTerrainData(terrain));
        }
        else
        {
            vs.SetTerrain(terrain);
        }
    }
예제 #6
0
        public static void FlushObjects(Rect terrainRect, bool clearCache = true)
        {
            if (system == null)
            {
                system = GameObject.FindObjectOfType <VegetationSystemPro>();
            }
            if (system == null)
            {
                return;
            }
            //throw new System.Exception("MapMagic could not find Vegetation System in scene./n Add Window -> AwesomeTechnologies -> Vegetation System to scene");

            PersistentVegetationStorage storage = system.PersistentVegetationStorage;

            PersistentVegetationStoragePackage storagePackage = system.PersistentVegetationStorage.PersistentVegetationStoragePackage;

            if (storagePackage == null)
            {
                return;
            }
            //throw new System.Exception("Vegetation System has no storage package assigned to be used with MapMagic./nAssign a Persistent Vegetation Storage Package to Persistent Vegetation Storage component and initialize it.");

            List <VegetationCell> overlapCellList = new List <VegetationCell>();

            system.VegetationCellQuadTree.Query(terrainRect, overlapCellList);

            if (system.VegetationCellQuadTree.Count != storagePackage.PersistentVegetationCellList.Count)
            {
                throw new System.Exception("Vegetation System cells number has a different count rather than Persistent Storage./nPlease re-initialize Persistent Storage.");
            }

            for (int i = 0; i < overlapCellList.Count; i++)
            {
                int cellIndex = overlapCellList[i].Index;

                //storagePackage.PersistentVegetationCellList[cellIndex].ClearCell();

                var infoList = storagePackage.PersistentVegetationCellList[cellIndex].PersistentVegetationInfoList;
                for (int j = 0; j < infoList.Count; j++)
                {
                    var itemList = infoList[j].VegetationItemList;

                    for (int k = itemList.Count - 1; k >= 0; k--)
                    {
                        Vector3 pos  = itemList[k].Position + system.VegetationSystemPosition;
                        Vector2 pos2 = pos.V2();

                        if (terrainRect.Contains(pos2))
                        {
                            itemList.RemoveAt(k);
                            //storage.RemoveVegetationItemInstance(infoList[j].VegetationItemID, pos, 1, clearCellCache:false);
                        }
                    }
                }

                //VegetationItemIndexes indexes = VegetationSystemPro.GetVegetationItemIndexes(vegetationItemID);
                //system.ClearCache(overlapCellList[i],indexes.VegetationPackageIndex,indexes.VegetationItemIndex);
            }

            //if (clearCache)
            //{
            //	for (int i=0; i<system.VegetationCellList.Count; i++)
            //		system.VegetationCellList[i].ClearCache();
            //}
            system.ClearCache(new Bounds(terrainRect.center.V3(), terrainRect.size.V3()));

            system.RefreshBillboards();
        }