private void DrawSelectedZone(Vector2 worldPosition)
        {
            //Calc zone under cursor
            if (_main.LandLayout != null && _main.LandLayout.Zones.Any() && _main.LandLayout.Bounds.Contains((Vector2i)worldPosition))
            {
                var selectedZone = _main.LandLayout.Zones.OrderBy(z => Vector2.SqrMagnitude(z.Center - worldPosition)).First();
                var zoneColor    = _target[selectedZone.Type].LandColor;

                Gizmos.color = zoneColor;

                //Draw zone bounds
                //foreach (var chunk in selectedZone.ChunkBounds)
                //DrawRectangle.ForGizmo(Chunk.GetBounds(chunk), zoneColor);

                foreach (var chunk in _main.LandLayout.GetChunks(selectedZone))
                {
                    DrawRectangle.ForGizmo(Chunk.GetBounds(chunk), zoneColor);
                }

                foreach (var block in selectedZone.GetBlocks2())
                {
                    var blockBounds = new Bounds2i(block, 1, 1);
                    DrawRectangle.ForGizmo(blockBounds, zoneColor);
                }
            }
        }
        protected override Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density)
        {
            //Prepare input data
            var infiniteLoopChecker = 0;

            //Generate zones center coords, check that only one zone occupies one chunk
            var zonesCoords = new List <Vector2>(count);

            for (var i = 0; i < count; i++)
            {
                var zoneCenterX = Random.Range((float)landBounds.Min.X, landBounds.Max.X);
                var zoneCenterY = Random.Range((float)landBounds.Min.Z, landBounds.Max.Z);
                var newCenter   = new Vector2(zoneCenterX, zoneCenterY);
                if (zonesCoords.All(zc => Vector2.SqrMagnitude(zc - newCenter) > density.x * density.x))
                {
                    zonesCoords.Add(new Vector2 {
                        x = zoneCenterX, y = zoneCenterY
                    });
                }
                else
                {
                    if (infiniteLoopChecker++ < 100)
                    {
                        i--;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(zonesCoords.ToArray());
        }
Example #3
0
        public Cell(Macro.Cell macro, MicroMap map)
        {
            Macro          = macro;
            _map           = map;
            BlockPositions = Rasterization.ConvexToBlocks(macro.Contains, macro.Bounds);

            Bounds = (Bounds2i)macro.Bounds;

            var vertices = new List <Vector2i>(BlockPositions);

            foreach (var blockPosition in BlockPositions)
            {
                if (!vertices.Contains(blockPosition + Vector2i.Forward))
                {
                    vertices.Add(blockPosition + Vector2i.Forward);
                }
                if (!vertices.Contains(blockPosition + Vector2i.Right))
                {
                    vertices.Add(blockPosition + Vector2i.Right);
                }
                if (!vertices.Contains(blockPosition + Vector2i.One))
                {
                    vertices.Add(blockPosition + Vector2i.One);
                }
            }

            VertexPositions = vertices.ToArray();
        }
Example #4
0
        public static void ForHandle(Bounds2i rectangle, Color color, uint width = 0, bool filled = false)
        {
            var corner1 = new Vector3(rectangle.Min.X, 0, rectangle.Min.Z);
            var corner2 = new Vector3(rectangle.Min.X, 0, rectangle.Max.Z + 1);
            var corner3 = new Vector3(rectangle.Max.X + 1, 0, rectangle.Max.Z + 1);
            var corner4 = new Vector3(rectangle.Max.X + 1, 0, rectangle.Min.Z);

            var points = new[] { corner1, corner2, corner3, corner4, corner1 };

            Handles.color = color;
            if (width == 0)
            {
                Handles.DrawPolyLine(points);
            }
            else
            {
                Handles.DrawAAPolyLine(width, points);
            }

            if (filled)
            {
                Handles.DrawLine(corner1, corner3);
                Handles.DrawLine(corner2, corner4);
            }
        }
        protected override Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density)
        {
            //Prepare input data
            var infiniteLoopChecker = 0;

            //Generate zones center coords, check that only one zone occupies one chunk
            var zonesCoords = new List<Vector2>(count);
            for (var i = 0; i < count; i++)
            {
                var zoneCenterX = Random.Range((float)landBounds.Min.X, landBounds.Max.X);
                var zoneCenterY = Random.Range((float)landBounds.Min.Z, landBounds.Max.Z);
                var newCenter = new Vector2(zoneCenterX, zoneCenterY);
                if (zonesCoords.All(zc => Vector2.SqrMagnitude(zc - newCenter) > density.x*density.x))
                    zonesCoords.Add(new Vector2 {x = zoneCenterX, y = zoneCenterY});
                else
                {
                    if (infiniteLoopChecker++ < 100)
                        i--;
                    else
                        break;
                }
            }

            return zonesCoords.ToArray();
        }
 public ZoneLayout(ZoneType type, Cell cell)
 {
     Cell        = cell;
     Center      = cell.Center;
     Type        = type;
     Bounds      = (Bounds2i)cell.Bounds;
     ChunkBounds = new Bounds2i(Chunk.GetPosition(Bounds.Min), Chunk.GetPosition(Bounds.Max));
     _neighbors  = new ZoneLayout[0];
 }
        /*
         * public void Render(MicroMap map, TriRunner renderSettings)
         * {
         *  //Break map to visualization chunks (rounded to ChunkSize)
         *  var microBounds = map.Bounds;
         *  var chunkMin = new Vector2i(SnapDown(microBounds.Min.X, ChunkSize), SnapDown(microBounds.Min.Z, ChunkSize));
         *  var chunkMax = new Vector2i(SnapUp(microBounds.Max.X, ChunkSize), SnapUp(microBounds.Max.Z, ChunkSize));
         *
         *  for (int x = chunkMin.X; x < chunkMax.X; x += ChunkSize)
         *  {
         *      for (int z = chunkMin.Z; z < chunkMax.Z; z += ChunkSize)
         *      {
         *          var meshGO = new GameObject("MicroMapChunk");
         *          var filter = meshGO.AddComponent<MeshFilter>();
         *          var renderer = meshGO.AddComponent<MeshRenderer>();
         *          meshGO.transform.SetParent(GetMeshRoot());
         *
         *          var chunkBound = new Bounds2i(new Vector2i(x, z), ChunkSize, ChunkSize );
         *          var chunkMesh = _mesher.CreateMesh(map, chunkBound, renderSettings);
         *
         *          filter.mesh = chunkMesh.Item1;
         *          var texturedMat = new Material(_textured);
         *          texturedMat.mainTexture = chunkMesh.Item2;
         *          renderer.material = texturedMat;
         *      }
         *  }
         * }
         */

        public void Render2(MicroMap map, TriRunner renderSettings)
        {
            Assert.IsTrue(renderSettings.RenderMode != TerrainRenderMode.Macro);

            //Break map to render chunks (rounded to ChunkSize)
            var microBounds = map.Bounds;
            var chunkMin    = new Vector2i(SnapDown(microBounds.Min.X, ChunkSize), SnapDown(microBounds.Min.Z, ChunkSize));
            var chunkMax    = new Vector2i(SnapUp(microBounds.Max.X, ChunkSize), SnapUp(microBounds.Max.Z, ChunkSize));

            //Iterate and generate render chunks
            for (int x = chunkMin.X; x < chunkMax.X; x += ChunkSize)
            {
                for (int z = chunkMin.Z; z < chunkMax.Z; z += ChunkSize)
                {
                    var meshGO = new GameObject("MicroMapChunk");
                    meshGO.transform.SetParent(GetMeshRoot());

                    var chunkBound  = new Bounds2i(new Vector2i(x, z), ChunkSize, ChunkSize);
                    var chunkMeshes = renderSettings.RenderMode == TerrainRenderMode.Blocks
                        ? _mesher.CreateMinecraftMesh(map, chunkBound, renderSettings)
                        : _mesher.CreateTerrainMesh(map, chunkBound, renderSettings);

                    //optimize todo implement single mesh mode for simple chunk (no caves)
                    {
                        var baseMeshGO   = new GameObject("BaseMesh");
                        var baseFilter   = baseMeshGO.AddComponent <MeshFilter>();
                        var baseRenderer = baseMeshGO.AddComponent <MeshRenderer>();
                        baseMeshGO.transform.SetParent(meshGO.transform);

                        var underMeshGO   = new GameObject("UnderMesh");
                        var underFilter   = underMeshGO.AddComponent <MeshFilter>();
                        var underRenderer = underMeshGO.AddComponent <MeshRenderer>();
                        underMeshGO.transform.SetParent(meshGO.transform);

                        var mainMeshGO   = new GameObject("MainMesh");
                        var mainFilter   = mainMeshGO.AddComponent <MeshFilter>();
                        var mainRenderer = mainMeshGO.AddComponent <MeshRenderer>();
                        mainMeshGO.transform.SetParent(meshGO.transform);

                        baseFilter.mesh = chunkMeshes.Base.Item1;
                        var baseTexturedMat = new Material(_textured);
                        baseTexturedMat.mainTexture = chunkMeshes.Base.Item2;
                        baseRenderer.material       = baseTexturedMat;

                        underFilter.mesh = chunkMeshes.Under.Item1;
                        var underTexturedMat = new Material(_textured);
                        underTexturedMat.mainTexture = chunkMeshes.Under.Item2;
                        underRenderer.material       = underTexturedMat;

                        mainFilter.mesh = chunkMeshes.Main.Item1;
                        var mainTexturedMat = new Material(_textured);
                        mainTexturedMat.mainTexture = chunkMeshes.Main.Item2;
                        mainRenderer.material       = mainTexturedMat;
                    }
                }
            }
        }
        public static void ForDebug(Bounds2i rectangle, Color color, float duration = 0)
        {
            var corner1 = new Vector3(rectangle.Min.X, 0, rectangle.Min.Z);
            var corner2 = new Vector3(rectangle.Min.X, 0, rectangle.Max.Z + 1);
            var corner3 = new Vector3(rectangle.Max.X + 1, 0, rectangle.Max.Z + 1);
            var corner4 = new Vector3(rectangle.Max.X + 1, 0, rectangle.Min.Z);

            Debug.DrawLine(corner1, corner2, color, duration);
            Debug.DrawLine(corner2, corner3, color, duration);
            Debug.DrawLine(corner3, corner4, color, duration);
            Debug.DrawLine(corner4, corner1, color, duration);
        }
Example #9
0
 public Land(LandLayout layout, ILandSettings settings)
 {
     _settings       = settings;
     _zones          = layout.Zones.Select(z => new Zone(z)).ToArray();
     Layout          = layout;
     _idwCoeff       = settings.IDWCoeff;
     _idwOffset      = settings.IDWOffset;
     _zoneMaxType    = settings.ZoneTypes.Max(z => z.Type);
     _zoneTypesCount = _zones.Where(z => z.Type != ZoneType.Empty).Distinct(Zone.TypeComparer).Count();
     _zoneSettings   = settings.ZoneTypes.ToArray();
     _chunksBounds   = new Bounds2i(settings.LandBounds.Min / (settings.BlocksCount * settings.BlockSize),
                                    settings.LandBounds.Max / (settings.BlocksCount * settings.BlockSize));
 }
Example #10
0
 public Land(LandLayout layout, ILandSettings settings)
 {
     _settings = settings;
     _zones = layout.Zones.Select(z => new Zone(z)).ToArray();
     Layout = layout;
     _idwCoeff = settings.IDWCoeff;
     _idwOffset = settings.IDWOffset;
     _zoneMaxType = settings.ZoneTypes.Max(z => z.Type);
     _zoneTypesCount = _zones.Where(z => z.Type != ZoneType.Empty).Distinct(Zone.TypeComparer).Count();
     _zoneSettings = settings.ZoneTypes.ToArray();
     _chunksBounds = new Bounds2i(settings.LandBounds.Min/(settings.BlocksCount*settings.BlockSize),
         settings.LandBounds.Max/(settings.BlocksCount*settings.BlockSize));
 }
 private void CopyBlocks(Chunk src, ChunkMaskBlock[,] dest, Bounds2i srcBounds, Vector2i destPosition)
 {
     for (int z = srcBounds.Min.Z; z <= srcBounds.Max.Z; z++)
     {
         var destPosZ = destPosition.Z + (z - srcBounds.Min.Z);
         for (int x = srcBounds.Min.X; x <= srcBounds.Max.X; x++)
         {
             var destPosX = destPosition.X + (x - srcBounds.Min.X);
             dest[destPosX, destPosZ].Block  = src.BlockType[x, z];
             dest[destPosX, destPosZ].Normal = src.NormalMap[x, z];
         }
     }
 }
        public static void ForGizmo(Bounds2i rectangle, Color color)
        {
            var corner1 = new Vector3(rectangle.Min.X, 0, rectangle.Min.Z);
            var corner2 = new Vector3(rectangle.Min.X, 0, rectangle.Max.Z + 1);
            var corner3 = new Vector3(rectangle.Max.X + 1, 0, rectangle.Max.Z + 1);
            var corner4 = new Vector3(rectangle.Max.X + 1, 0, rectangle.Min.Z);

            Gizmos.color = color;
            Gizmos.DrawLine(corner1, corner2);
            Gizmos.DrawLine(corner2, corner3);
            Gizmos.DrawLine(corner3, corner4);
            Gizmos.DrawLine(corner4, corner1);
        }
        /// <summary>
        /// Generate zone points using Poisson sampling
        /// </summary>
        /// <param name="count"></param>
        /// <param name="landBounds"></param>
        /// <param name="density"></param>
        /// <returns></returns>
        protected override Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density)
        {
            var checkedPoints   = new List <Vector2>();
            var uncheckedPoints = new List <Vector2>();

            //Generate start point
            var zoneCenterX = Random.Range((float)landBounds.Min.X, landBounds.Max.X);
            var zoneCenterY = Random.Range((float)landBounds.Min.Z, landBounds.Max.Z);
            var startPoint  = new Vector2(zoneCenterX, zoneCenterY);

            uncheckedPoints.Add(startPoint);

            //Generate point around first unchecked
            while (uncheckedPoints.Any())
            {
                var processedPoint = uncheckedPoints.First();
                uncheckedPoints.RemoveAt(0);

                for (int i = 0; i < 10; i++)
                {
                    var r        = Random.Range(density.x + 0.1f, density.y);
                    var a        = Random.Range(0, 2 * Mathf.PI);
                    var newPoint = processedPoint + new Vector2(r * Mathf.Cos(a), r * Mathf.Sin(a));

                    if (landBounds.Contains((Vector2i)newPoint))
                    {
                        if (checkedPoints.TrueForAll(p => Vector2.SqrMagnitude(p - newPoint) > density.x * density.x) &&
                            uncheckedPoints.TrueForAll(p => Vector2.SqrMagnitude(p - newPoint) > density.x * density.x))
                        {
                            uncheckedPoints.Add(newPoint);
                        }
                    }
                }

                checkedPoints.Add(processedPoint);
                if (checkedPoints.Count >= count)
                {
                    break;
                }
            }

            return(checkedPoints.ToArray());
        }
Example #14
0
        public MicroMap(MacroMap macromap, TriRunner settings)
        {
            Bounds = (Bounds2i)macromap.Bounds;

            _macromap = macromap;
            _settings = settings;

            Cells = new Cell[macromap.Cells.Count];
            for (var i = 0; i < macromap.Cells.Count; i++)
            {
                var macroCell = macromap.Cells[i];
                var microCell = new Cell(macroCell, this);
                Cells[i] = microCell;
            }

            _heightMap = new Heights[Bounds.Size.X + 1, Bounds.Size.Z + 1];
            _blocks    = new Blocks[Bounds.Size.X, Bounds.Size.Z];

            Debug.LogFormat("Generated micromap {0} x {1} = {2} blocks", Bounds.Size.X, Bounds.Size.Z, Bounds.Size.X * Bounds.Size.Z);
        }
        /// <summary>
        /// Generate zone points using Poisson sampling
        /// </summary>
        /// <param name="count"></param>
        /// <param name="landBounds"></param>
        /// <param name="density"></param>
        /// <returns></returns>
        protected override Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density)
        {
            var checkedPoints = new List<Vector2>();
            var uncheckedPoints = new List<Vector2>();

            //Generate start point
            var zoneCenterX = Random.Range((float)landBounds.Min.X, landBounds.Max.X);
            var zoneCenterY = Random.Range((float)landBounds.Min.Z, landBounds.Max.Z);
            var startPoint = new Vector2(zoneCenterX, zoneCenterY);

            uncheckedPoints.Add(startPoint);

            //Generate point around first unchecked
            while (uncheckedPoints.Any())
            {
                var processedPoint = uncheckedPoints.First();
                uncheckedPoints.RemoveAt(0);

                for (int i = 0; i < 10; i++)
                {
                    var r = Random.Range(density.x + 0.1f, density.y);
                    var a = Random.Range(0, 2*Mathf.PI);
                    var newPoint = processedPoint + new Vector2(r * Mathf.Cos(a), r*Mathf.Sin(a));

                    if (landBounds.Contains((Vector2i) newPoint))
                    {
                        if(checkedPoints.TrueForAll(p => Vector2.SqrMagnitude(p - newPoint) > density.x * density.x)
                            && uncheckedPoints.TrueForAll(p => Vector2.SqrMagnitude(p - newPoint) > density.x * density.x))
                            uncheckedPoints.Add(newPoint);
                    }
                }

                checkedPoints.Add(processedPoint);
                if (checkedPoints.Count >= count)
                    break;
            }

            return checkedPoints.ToArray();
        }
        /*
         * public void SetLand(LandLayout land)
         * {
         *  _land = land;
         * }
         */

        /*
         * public bool IsZoneVisible(ZoneLayout zone)
         * {
         *  //todo check Zone with Observer position and neighbour zones
         *
         *  var observerPos = new Vector2(Position.x, Position.z);
         *
         *  foreach (var zoneVert in zone.Cell.Vertices)
         *      if (Vector2.Distance(zoneVert, observerPos) < Range)
         *          return true;
         *
         *
         *  return false;
         * }
         */

        public bool IsBoundVisible(Bounds2i bounds)
        {
            var observerPos = new Vector2(Position.x, Position.z);

            if (Vector2.Distance((Vector2)bounds.Corner1, observerPos) < Range)
            {
                return(true);
            }
            if (Vector2.Distance((Vector2)bounds.Corner2, observerPos) < Range)
            {
                return(true);
            }
            if (Vector2.Distance((Vector2)bounds.Corner3, observerPos) < Range)
            {
                return(true);
            }
            if (Vector2.Distance((Vector2)bounds.Corner4, observerPos) < Range)
            {
                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Generate random centers of cells
 /// </summary>
 /// <param name="count"></param>
 /// <param name="landBounds"></param>
 /// <param name="density"></param>
 /// <returns></returns>
 protected abstract Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density);
Example #18
0
        void OnValidate()
        {
            if (LandSize < 1) LandSize = 1;

            //Update Land bounds
            var landMin = -LandSize / 2;
            var landMax = landMin + LandSize - 1;
            var minChunkBounds = Chunk.GetBounds(new Vector2i(landMin, landMin));
            var maxChunkBounds = Chunk.GetBounds(new Vector2i(landMax, landMax));

            LayoutBounds = new Bounds2i(minChunkBounds.Min, maxChunkBounds.Max);
        }
Example #19
0
 protected override Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density)
 {
     return(new[] { new Vector2(8, 9), new Vector2(-8, 24), new Vector2(10, -8), new Vector2(17, 18) });
 }
 protected override Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density)
 {
     return new[] {new Vector2(8, 9), new Vector2(-8, 24), new Vector2(10, -8), new Vector2(17, 18) };
 }
 private void CopyBlocks(Chunk src, ChunkMaskBlock[,] dest, Bounds2i srcBounds, Vector2i destPosition)
 {
     for (int z = srcBounds.Min.Z; z <= srcBounds.Max.Z; z++)
     {
         var destPosZ = destPosition.Z + (z - srcBounds.Min.Z);
         for (int x = srcBounds.Min.X; x <= srcBounds.Max.X; x++)
         {
             var destPosX = destPosition.X + (x - srcBounds.Min.X);
             dest[destPosX, destPosZ].Block = src.BlockType[x, z];
             dest[destPosX, destPosZ].Normal = src.NormalMap[x, z];
         }
     }
 }
 public static void ForGizmo(Bounds2i rectangle)
 {
     ForGizmo(rectangle, Gizmos.color);
 }
Example #23
0
 /// <summary>
 /// Generate random centers of cells
 /// </summary>
 /// <param name="count"></param>
 /// <param name="landBounds"></param>
 /// <param name="density"></param>
 /// <returns></returns>
 protected abstract Vector2[] GeneratePoints(int count, Bounds2i landBounds, Vector2 density);
        private void DrawSelectedZone(Vector2 worldPosition)
        {
            //Calc zone under cursor
            if (_main.LandLayout != null && _main.LandLayout.Zones.Any() && _main.LandLayout.Bounds.Contains((Vector2i)worldPosition))
            {
                var selectedZone = _main.LandLayout.Zones.OrderBy(z => Vector2.SqrMagnitude(z.Center - worldPosition)).First();
                var zoneColor = _target[selectedZone.Type].LandColor;

                Gizmos.color = zoneColor;

                //Draw zone bounds
                //foreach (var chunk in selectedZone.ChunkBounds)
                //DrawRectangle.ForGizmo(Chunk.GetBounds(chunk), zoneColor);

                foreach (var chunk in _main.LandLayout.GetChunks(selectedZone))
                    DrawRectangle.ForGizmo(Chunk.GetBounds(chunk), zoneColor);

                foreach (var block in selectedZone.GetBlocks2())
                {
                    var blockBounds = new Bounds2i(block, 1, 1);
                    DrawRectangle.ForGizmo(blockBounds, zoneColor);
                }
            }
        }