public void Update(double elapsedTime, EntityManager entityManager)
        {
            foreach (var water in entityManager.GetEntitiesWithComponent <OceanComponent>())
            {
                var waterComponent = entityManager.GetComponent <OceanComponent>(water);
                var waterMesh      = entityManager.GetComponent <StaticMesh>(water);
                if (waterMesh == null)
                {
                    waterMesh = new StaticMesh
                    {
                        Color       = new Vector4(0f, 0f, 1f, 0f),
                        ModelMatrix = Matrix4.Identity
                    };
                    entityManager.AddComponentToEntity(water, waterMesh);
                }

                var mesh = CreateMesh(waterComponent);
                waterMesh.Update(mesh);
                for (var i = 0; i < waterMesh.Mesh.Vertices.Length; i++)
                {
                    var position = waterMesh.Mesh.Vertices[i].Position;
                    var vector3D = Gerstner2.CalculateWave((Vector3d)position, elapsedTime, _waveSettings);

                    waterMesh.Mesh.Vertices[i] = new Vertex3V3N
                    {
                        Position = (Vector3)vector3D
                    };
                }
                waterMesh.Mesh.CalculateNormals();
                waterMesh.Update(waterMesh.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)));
        }
        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);
                }
            }
        }
Beispiel #5
0
        private RenderableMesh EnsureResources(StaticMesh component)
        {
            if (!_allocatedResources.ContainsKey(component))
            {
                _allocatedResources[component] = _resourceAllocator.AllocateResourceFor(component.Mesh);
            }
            else
            {
                if (component.IsDirty)
                {
                    var oldResources = _allocatedResources[component];
                    oldResources.Delete();

                    _allocatedResources[component] = _resourceAllocator.AllocateResourceFor(component.Mesh);

                    component.IsDirty = false;
                }
            }

            return(_allocatedResources[component]);
        }
        private RenderableMesh EnsureResources(StaticMesh component)
        {
            if (!_allocatedResources.ContainsKey(component))
            {
                _allocatedResources[component] = _resourceAllocator.AllocateResourceFor(component.Mesh);
            }
            else
            {
                if (component.IsDirty)
                {
                    var oldResources = _allocatedResources[component];
                    oldResources.Delete();

                    _allocatedResources[component] = _resourceAllocator.AllocateResourceFor(component.Mesh);

                    component.IsDirty = false;
                }
            }

            return _allocatedResources[component];
        }
        public void Update(double elapsedTime, EntityManager entityManager)
        {
            foreach (var water in entityManager.GetEntitiesWithComponent<OceanComponent>())
            {
                var waterComponent = entityManager.GetComponent<OceanComponent>(water);
                var waterMesh = entityManager.GetComponent<StaticMesh>(water);
                if (waterMesh == null)
                {
                    waterMesh = new StaticMesh
                    {
                        Color = new Vector4(0f, 0f, 1f, 0f),
                        ModelMatrix = Matrix4.Identity
                    };
                    entityManager.AddComponentToEntity(water, waterMesh);
                }

                var mesh = CreateMesh(waterComponent);
                waterMesh.Update(mesh);
                for (var i = 0; i < waterMesh.Mesh.Vertices.Length; i++)
                {
                    var position = waterMesh.Mesh.Vertices[i].Position;
                    var vector3D = Gerstner2.CalculateWave((Vector3d)position, elapsedTime, _waveSettings);

                    waterMesh.Mesh.Vertices[i] = new Vertex3V3N
                    {
                        Position = (Vector3)vector3D
                    };
                }
                waterMesh.Mesh.CalculateNormals();
                waterMesh.Update(waterMesh.Mesh);
            }
        }
        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);
        }