Example #1
0
        /** \param removeOld: Remove terrains that are not in the collector. */
        private void AddTerrains(Collector collector, bool removeOld = false)
        {
            // Get new nodes
            HashSet <Vector2Int> toRemove = new HashSet <Vector2Int>(_terrains.Keys);

            foreach (var nodeName in collector.GetNewNodes())
            {
                var    node          = collector.GetNode(nodeName);
                string terrainName   = node.Mesh;
                IntPtr terrainHandle = collector.GetTerrainHandle(terrainName);

                if (terrainHandle != IntPtr.Zero)
                {
                    // get bbox
                    BBox   bbox   = Terrains.terrainGetBBox(terrainHandle);
                    double size   = bbox.xmax - bbox.xmin;
                    var    coords = new Vector2Int((int)(bbox.xmin / size), (int)(bbox.ymin / size));

                    if (!_terrains.ContainsKey(coords))
                    {
                        GameObject terrainGO = new GameObject("Terrain " + coords);
#if UNITY_EDITOR
                        Undo.RegisterCreatedObjectUndo(terrainGO, "Added terrain");
#endif
                        terrainGO.transform.SetParent(transform);
                        var worldTerrain = terrainGO.AddComponent <WorldTerrain>();
                        worldTerrain.terrainSystem = this;
                        worldTerrain.coords        = coords;
                        var terrain = terrainGO.AddComponent <Terrain>();
                        terrain.materialTemplate = materialTemplate;
                        terrain.terrainData      = new TerrainData();
                        terrainGO.AddComponent <TerrainCollider>().terrainData = terrain.terrainData;

                        Terrains.ReadTerrainData(terrain.terrainData, terrainHandle);
                        Terrains.ReadTerrainTextures(terrain, terrainHandle, collector, _cacheLocation);
                        Terrains.UpdateTerrainBBox(terrain, bbox);

                        _terrainGOs.Add(coords, terrainGO);
                        _terrains.Add(coords, terrain);
                    }
                    else
                    {
                        toRemove.Remove(coords);
                    }
                }
            }

            if (removeOld)
            {
                foreach (var key in toRemove)
                {
                    DeleteTerrain(key);
                }
            }

            UpdateNeighbours();
        }
Example #2
0
        internal static void UpdateTerrainBBox(Terrain terrain, BBox bbox)
        {
            TerrainData tData = terrain.terrainData;

            tData.size = new Vector3(
                (float)(bbox.xmax - bbox.xmin),
                (float)(bbox.zmax - bbox.zmin),
                (float)(bbox.ymax - bbox.ymin)
                );
            terrain.transform.position = new Vector3((float)bbox.xmin, (float)bbox.zmin, (float)bbox.ymin);
        }