Ejemplo n.º 1
0
        /// <summary>
        /// Get foliage chunks in range [9]
        /// </summary>
        /// <param name="pos"></param>
        public static FoliageCore_Chunk[] GetFoliageChunksNeighbors(Vector3 pos, FoliageCore_Chunk[] cache)
        {
            if (cache == null)
            {
                cache = new FoliageCore_Chunk[9];
            }

            if (cache.Length != 9)
            {
                Debug.LogError("UNATURE ERROR! CACHE SIZE IS BIGGER THAN NINE!");
                return(null);
            }

            FoliageCore_MainManager mManager = FoliageCore_MainManager.instance;

            if (mManager == null)
            {
                Debug.LogError("UNATURE ERROR! Foliage Manager can't be found!");
                return(null);
            }

            int topDownAdjuster   = FoliageCore_MainManager.FOLIAGE_MAIN_AREA_RESOLUTION;
            int rightLeftAdjuster = 1;

            int centerMiddleID = FoliageCore_MainManager.instance.GetChunkID(pos.x, pos.z);
            int centerRightID  = centerMiddleID - rightLeftAdjuster;
            int centerLeftID   = centerMiddleID + rightLeftAdjuster;

            int topMiddleID = centerMiddleID + topDownAdjuster;
            int topRightID  = topMiddleID + rightLeftAdjuster;
            int topLeftID   = topMiddleID - rightLeftAdjuster;

            int bottomMiddleID = centerMiddleID - topDownAdjuster;
            int bottomRightID  = bottomMiddleID + rightLeftAdjuster;
            int bottomLeftID   = bottomMiddleID - rightLeftAdjuster;

            cache[0] = mManager.CheckChunkInBounds(bottomRightID) ? mManager.sector.foliageChunks[bottomRightID] : null;   // bottom right
            cache[1] = mManager.CheckChunkInBounds(bottomMiddleID) ? mManager.sector.foliageChunks[bottomMiddleID] : null; // bottom center
            cache[2] = mManager.CheckChunkInBounds(bottomLeftID) ? mManager.sector.foliageChunks[bottomLeftID] : null;     // bottom left
            cache[3] = mManager.CheckChunkInBounds(centerRightID) ? mManager.sector.foliageChunks[centerRightID] : null;   // middle right
            cache[4] = mManager.CheckChunkInBounds(centerMiddleID) ? mManager.sector.foliageChunks[centerMiddleID] : null; // middle center
            cache[5] = mManager.CheckChunkInBounds(centerLeftID) ? mManager.sector.foliageChunks[centerLeftID] : null;     // middle left
            cache[6] = mManager.CheckChunkInBounds(topRightID) ? mManager.sector.foliageChunks[topRightID] : null;         // top right
            cache[7] = mManager.CheckChunkInBounds(topMiddleID) ? mManager.sector.foliageChunks[topMiddleID] : null;       // top center
            cache[8] = mManager.CheckChunkInBounds(topLeftID) ? mManager.sector.foliageChunks[topLeftID] : null;           // top left

            return(cache);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// On terrain changed.
        /// </summary>
        /// <param name="changedFlags"></param>
        protected virtual void OnTerrainChanged(int changedFlags)
        {
            TerrainChangedFlags flag = (TerrainChangedFlags)changedFlags;

            if (flag == TerrainChangedFlags.TreeInstances)
            {
                if (sector == null)
                {
                    GenerateSector(sectorResolution);
                }

                #if UNITY_EDITOR
                UNEditorUtility.StartSceneScrollbar("Fetching tree instances...", 1);
                #endif

                sector.FetchTreeInstances(true, () =>
                {
                    #if UNITY_EDITOR
                    UNSettings.Log("Trees Updated.");

                    UNEditorUtility.currentProgressIndex = 1;
                    #endif
                });
            }
            else if (flag == TerrainChangedFlags.TreePrototypesChanged)
            {
                CreatePool(PoolItemType);
            }
            else if (flag == TerrainChangedFlags.DelayedHeightmapUpdate)
            {
                if (FoliageCore_MainManager.instance == null || !updateGrassOnHeightsChange || !manageGrass)
                {
                    return;
                }

                TerrainData tData = terrain.terrainData;

                float[,] heights = tData.GetHeights(0, 0, tData.heightmapWidth, tData.heightmapHeight);
                bool changed = false;

                float cordX;
                float cordY;

                FoliageCore_MainManager manager = FoliageCore_MainManager.instance;

                int mChunkID;
                FoliageCore_Chunk      mChunk;
                FoliageManagerInstance mInstance;

                Vector3 position        = transform.position;
                Vector3 managerPosition = manager.transform.position;

                FoliageWorldMap worldMap;

                int transformedSize;

                for (int x = 0; x < tData.heightmapWidth; x++)
                {
                    for (int y = 0; y < tData.heightmapHeight; y++)
                    {
                        if (heights[y, x] != terrainData.multiThreaded_terrainHeights[y, x]) // change on cord.
                        {
                            changed = true;

                            cordX = ((x * tData.size.x) / tData.heightmapWidth) + position.x;
                            cordY = ((y * tData.size.z) / tData.heightmapHeight) + position.z;

                            mChunkID = manager.GetChunkID(cordX - managerPosition.x, cordY - managerPosition.z);

                            if (manager.CheckChunkInBounds(mChunkID))
                            {
                                mChunk = manager.sector.foliageChunks[mChunkID];

                                if (!mChunk.isFoliageInstanceAttached)
                                {
                                    continue;
                                }

                                mInstance = mChunk.GetOrCreateFoliageManagerInstance();

                                worldMap = mInstance.worldMap;

                                cordX = mInstance.TransformCord(cordX, mInstance.transform.position.x);
                                cordY = mInstance.TransformCord(cordY, mInstance.transform.position.z);

                                transformedSize = mInstance.TransformCord(2, 0);

                                worldMap.UpdateHeight_RANGE(cordX, cordY, transformedSize, transformedSize, false);

                                worldMap.SetPixels32Delayed();
                            }
                        }
                    }
                }

                if (changed)
                {
                    FoliageCore_MainManager.SaveDelayedMaps();
                }

                terrainData.multiThreaded_terrainHeights = heights;
            }
        }