private Node CreateNode(Box3D bounds, int level, EntityManager entityManager)
        {
            var mesh       = MeshCreator.CreateXZGrid(10, 10);
            var staticMesh = new StaticMesh
            {
                Color       = new Vector4(0f, 0f, 1f, 1f),
                ModelMatrix = Matrix4.Identity,
            };

            var size                = bounds.Max - bounds.Min;
            var mesh3V3N            = mesh.Transformed(Matrix4.CreateScale((float)size.X, 1, (float)size.Z) * Matrix4.CreateTranslation((Vector3)bounds.Center));
            var improvedPerlinNoise = new ImprovedPerlinNoise(4711);

            for (int i = 0; i < mesh3V3N.Vertices.Length; i++)
            {
                var vertex = mesh3V3N.Vertices[i];
                var height = improvedPerlinNoise.Noise(vertex.Position.X, vertex.Position.Z) * 0.2;
                mesh3V3N.Vertices[i] = new Vertex3V3N
                {
                    Normal   = new Vector3(0, 1, 0),
                    Position = new Vector3(vertex.Position.X, (float)height, vertex.Position.Z)
                };
            }

            staticMesh.Update(mesh3V3N);

            var entity = new Entity(Guid.NewGuid().ToString());

            entityManager.Add(entity);
            entityManager.AddComponentToEntity(entity, staticMesh);

            if (level == 0)
            {
                return(new Node(bounds, new Node[] { }, entity, 1));
            }

            var min    = bounds.Min;
            var max    = bounds.Max;
            var center = bounds.Center;

            return(new Node(bounds,
                            new[]
            {
                CreateNode(new Box3D(bounds.Min, center), level - 1, entityManager),
                CreateNode(new Box3D(new Vector3d(center.X, 0, min.Z), new Vector3d(max.X, 0, center.Z)), level - 1, entityManager),
                CreateNode(new Box3D(new Vector3d(min.X, 0, center.Z), new Vector3d(center.X, 0, max.Z)), level - 1, entityManager),
                CreateNode(new Box3D(center, max), level - 1, entityManager)
            }, entity, Math.Pow(2, level)));
        }
        public void Update(double elapsedTime, EntityManager entityManager)
        {
            foreach (var entity in entityManager.GetEntitiesWithComponent <CubeMeshComponent>())
            {
                var mesh = entityManager.GetComponent <StaticMesh>(entity);
                if (mesh == null)
                {
                    var component = entityManager.GetComponent <CubeMeshComponent>(entity);
                    mesh             = new StaticMesh();
                    mesh.ModelMatrix = component.Transform;
                    mesh.Color       = new Vector4(0f, 0f, 1f, 1f);
                    mesh.Update(MeshCreator.CreateXZGrid(25, 25));

                    entityManager.AddComponentToEntity(entity, mesh);
                }
            }
        }
        private Node CreateNode(Box3D bounds, int level, EntityManager entityManager)
        {
            var mesh = MeshCreator.CreateXZGrid(10, 10);
            var staticMesh = new StaticMesh
            {
                Color = new Vector4(0f, 0f, 1f, 1f),
                ModelMatrix = Matrix4.Identity,
            };

            var size = bounds.Max - bounds.Min;
            var mesh3V3N = mesh.Transformed(Matrix4.CreateScale((float)size.X, 1, (float)size.Z) * Matrix4.CreateTranslation((Vector3)bounds.Center));
            var improvedPerlinNoise = new ImprovedPerlinNoise(4711);
            for (int i = 0; i < mesh3V3N.Vertices.Length; i++)
            {
                var vertex = mesh3V3N.Vertices[i];
                var height = improvedPerlinNoise.Noise(vertex.Position.X, vertex.Position.Z) * 0.2;
                mesh3V3N.Vertices[i] = new Vertex3V3N
                {
                    Normal = new Vector3(0, 1, 0),
                    Position = new Vector3(vertex.Position.X, (float)height, vertex.Position.Z)
                };
            }

            staticMesh.Update(mesh3V3N);

            var entity = new Entity(Guid.NewGuid().ToString());
            entityManager.Add(entity);
            entityManager.AddComponentToEntity(entity, staticMesh);

            if (level == 0)
            {
                return new Node(bounds, new Node[] { }, entity, 1);
            }

            var min = bounds.Min;
            var max = bounds.Max;
            var center = bounds.Center;

            return new Node(bounds,
                new[]
                {
                    CreateNode(new Box3D(bounds.Min, center), level -1, entityManager),
                    CreateNode(new Box3D(new Vector3d(center.X, 0, min.Z), new Vector3d(max.X, 0, center.Z)), level -1, entityManager),
                    CreateNode(new Box3D(new Vector3d(min.X, 0, center.Z), new Vector3d(center.X, 0, max.Z)), level - 1, entityManager),
                    CreateNode(new Box3D(center, max), level - 1, entityManager)
                }, entity, Math.Pow(2, level));
        }
        public void GenerateMesh(StaticMesh staticMesh, int x, int y, float numberOfChunksX, float numberOfChunksY)
        {
            var offset = new Vector2(x, y);

            var noiseGenerator = new ChunkNoiseGenerator(offset, _noise);

            var heightMapGenerator = new HeightMapGenerator(noiseGenerator);
            var mesh = heightMapGenerator.GenerateMesh();

            var translationMatrix = Matrix4.CreateTranslation(offset.X - numberOfChunksX / 2.0f, 0, offset.Y - numberOfChunksY / 2.0f);
            staticMesh.ModelMatrix = translationMatrix;
            staticMesh.Color = _colorCycle.GetNext();
            staticMesh.Update(mesh);
        }