Ejemplo n.º 1
0
        public void Index3ShortestDistanceMethodenTest()
        {
            Index3 size = new Index3(20, 20, 20);
            Index3 i1   = new Index3(5, 7, 6);   // Startwert
            Index3 i2   = new Index3(12, 13, 8); // Destinations
            Index3 i3   = new Index3(7, 6, 2);   // Results

            Assert.AreEqual(i3.X, i1.ShortestDistanceX(i2.X, size.X));
            Assert.AreEqual(i3.Y, i1.ShortestDistanceY(i2.Y, size.Y));
            Assert.AreEqual(i3.Z, i1.ShortestDistanceZ(i2.Z, size.Z));

            Assert.AreEqual(new Index2(i3.X, i3.Y), i1.ShortestDistanceXY(new Index2(i2.X, i2.Y), new Index2(size.X, size.Y)));
            Assert.AreEqual(new Index3(i3.X, i3.Y, i2.Z - i1.Z), i1.ShortestDistanceXY(i2, new Index2(size.X, size.Y)));

            Assert.AreEqual(i3, i1.ShortestDistanceXYZ(i2, size));
        }
Ejemplo n.º 2
0
        public void Index3ShortestDistanceMethodenTest()
        {
            Index3 size = new Index3(20, 20, 20);
            Index3 i1 = new Index3(5, 7, 6); // Startwert
            Index3 i2 = new Index3(12, 13, 8); // Destinations
            Index3 i3 = new Index3(7, 6, 2); // Results

            Assert.AreEqual(i3.X, i1.ShortestDistanceX(i2.X, size.X));
            Assert.AreEqual(i3.Y, i1.ShortestDistanceY(i2.Y, size.Y));
            Assert.AreEqual(i3.Z, i1.ShortestDistanceZ(i2.Z, size.Z));

            Assert.AreEqual(new Index2(i3.X, i3.Y), i1.ShortestDistanceXY(new Index2(i2.X, i2.Y), new Index2(size.X, size.Y)));
            Assert.AreEqual(new Index3(i3.X, i3.Y, i2.Z - i1.Z), i1.ShortestDistanceXY(i2, new Index2(size.X, size.Y)));

            Assert.AreEqual(i3, i1.ShortestDistanceXYZ(i2, size));
        }
Ejemplo n.º 3
0
        protected override void OnPreDraw(GameTime gameTime)
        {
            if (player.ActorHost == null)
            {
                return;
            }

            if (ControlTexture == null)
            {
                ControlTexture = new RenderTarget2D(Manager.GraphicsDevice, ActualClientArea.Width, ActualClientArea.Height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
            }

            float octoDaysPerEarthDay = 360f;
            float inclinationVariance = MathHelper.Pi / 3f;

            float playerPosX = ((float)player.ActorHost.Player.Position.GlobalPosition.X / (planet.Size.X * Chunk.CHUNKSIZE_X)) * MathHelper.TwoPi;
            float playerPosY = ((float)player.ActorHost.Player.Position.GlobalPosition.Y / (planet.Size.Y * Chunk.CHUNKSIZE_Y)) * MathHelper.TwoPi;

            TimeSpan diff = DateTime.UtcNow - new DateTime(1888, 8, 8);

            float inclination = ((float)Math.Sin(playerPosY) * inclinationVariance) + MathHelper.Pi / 6f;
            //Console.WriteLine("Stand: " + (MathHelper.Pi + playerPosX) + " Neigung: " + inclination);
            Matrix sunMovement =
                Matrix.CreateRotationX(inclination) *
                //Matrix.CreateRotationY((((float)gameTime.TotalGameTime.TotalMinutes * MathHelper.TwoPi) + playerPosX) * -1);
                Matrix.CreateRotationY((float)(MathHelper.TwoPi - ((diff.TotalDays * octoDaysPerEarthDay * MathHelper.TwoPi) % MathHelper.TwoPi)));

            Vector3 sunDirection = Vector3.Transform(new Vector3(0, 0, 1), sunMovement);

            simpleShader.Parameters["DiffuseColor"].SetValue(new Microsoft.Xna.Framework.Color(190, 190, 190).ToVector4());
            simpleShader.Parameters["DiffuseIntensity"].SetValue(0.6f);
            simpleShader.Parameters["DiffuseDirection"].SetValue(sunDirection);

            // Console.WriteLine(sunDirection);

            // Index3 chunkOffset = player.ActorHost.Position.ChunkIndex;
            Index3 chunkOffset = camera.CameraChunk;

            Microsoft.Xna.Framework.Color background =
                new Microsoft.Xna.Framework.Color(181, 224, 255);

            Manager.GraphicsDevice.SetRenderTarget(MiniMapTexture);
            Manager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            Manager.GraphicsDevice.Clear(background);

            foreach (var renderer in chunkRenderer)
            {
                if (!renderer.ChunkPosition.HasValue)
                {
                    continue;
                }

                Index3 shift = chunkOffset.ShortestDistanceXY(
                    renderer.ChunkPosition.Value, new Index2(
                        planet.Size.X,
                        planet.Size.Y));

                BoundingBox chunkBox = new BoundingBox(
                    new Vector3(
                        shift.X * Chunk.CHUNKSIZE_X,
                        shift.Y * Chunk.CHUNKSIZE_Y,
                        shift.Z * Chunk.CHUNKSIZE_Z),
                    new Vector3(
                        (shift.X + 1) * Chunk.CHUNKSIZE_X,
                        (shift.Y + 1) * Chunk.CHUNKSIZE_Y,
                        (shift.Z + 1) * Chunk.CHUNKSIZE_Z));

                int range = 3;
                if (shift.X >= -range && shift.X <= range &&
                    shift.Y >= -range && shift.Y <= range)
                {
                    renderer.Draw(camera.MinimapView, miniMapProjectionMatrix, shift);
                }
            }

            Manager.GraphicsDevice.SetRenderTarget(ControlTexture);
            Manager.GraphicsDevice.Clear(background);

            Manager.GraphicsDevice.BlendState        = BlendState.AlphaBlend;
            Manager.GraphicsDevice.DepthStencilState = DepthStencilState.None;

            // Draw Sun
            // GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            sunEffect.Texture = sunTexture;
            Matrix billboard = Matrix.Invert(camera.View);

            billboard.Translation = player.ActorHost.Position.LocalPosition + (sunDirection * -10);
            sunEffect.World       = billboard;
            sunEffect.View        = camera.View;
            sunEffect.Projection  = camera.Projection;
            sunEffect.CurrentTechnique.Passes[0].Apply();
            Manager.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, billboardVertices, 0, 2);

            Manager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            foreach (var renderer in chunkRenderer)
            {
                if (!renderer.ChunkPosition.HasValue)
                {
                    continue;
                }

                Index3 shift = chunkOffset.ShortestDistanceXY(
                    renderer.ChunkPosition.Value, new Index2(
                        planet.Size.X,
                        planet.Size.Y));

                BoundingBox chunkBox = new BoundingBox(
                    new Vector3(
                        shift.X * Chunk.CHUNKSIZE_X,
                        shift.Y * Chunk.CHUNKSIZE_Y,
                        shift.Z * Chunk.CHUNKSIZE_Z),
                    new Vector3(
                        (shift.X + 1) * Chunk.CHUNKSIZE_X,
                        (shift.Y + 1) * Chunk.CHUNKSIZE_Y,
                        (shift.Z + 1) * Chunk.CHUNKSIZE_Z));

                if (camera.Frustum.Intersects(chunkBox))
                {
                    renderer.Draw(camera.View, camera.Projection, shift);
                }
            }

            if (player.SelectedBox.HasValue)
            {
                // Index3 offset = player.ActorHost.Position.ChunkIndex * Chunk.CHUNKSIZE;
                Index3 offset           = camera.CameraChunk * Chunk.CHUNKSIZE;
                Index3 planetSize       = planet.Size * Chunk.CHUNKSIZE;
                Index3 relativePosition = new Index3(
                    Index2.ShortestDistanceOnAxis(offset.X, player.SelectedBox.Value.X, planetSize.X),
                    Index2.ShortestDistanceOnAxis(offset.Y, player.SelectedBox.Value.Y, planetSize.Y),
                    player.SelectedBox.Value.Z - offset.Z);

                Vector3 selectedBoxPosition = new Vector3(
                    player.SelectedBox.Value.X - (chunkOffset.X * Chunk.CHUNKSIZE_X),
                    player.SelectedBox.Value.Y - (chunkOffset.Y * Chunk.CHUNKSIZE_Y),
                    player.SelectedBox.Value.Z - (chunkOffset.Z * Chunk.CHUNKSIZE_Z));
                // selectionEffect.World = Matrix.CreateTranslation(selectedBoxPosition);
                selectionEffect.World      = Matrix.CreateTranslation(relativePosition);
                selectionEffect.View       = camera.View;
                selectionEffect.Projection = camera.Projection;
                foreach (var pass in selectionEffect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    Manager.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, selectionLines, 0, 8, selectionIndeces, 0, 12);
                }
            }

            Manager.GraphicsDevice.SetRenderTarget(null);
        }
Ejemplo n.º 4
0
        public void Draw(Matrix view, Matrix projection, Index3 chunkOffset, Index2 planetSize)
        {
            effect.Projection              = projection;
            effect.View                    = view;
            effect.TextureEnabled          = true;
            graphicsDevice.RasterizerState = RasterizerState.CullClockwise;
            using (var writer = File.AppendText(Path.Combine(".", "render.log")))
                foreach (var pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    i++;
                    foreach (var entity in Entities)
                    {
                        if (!entity.Components.ContainsComponent <RenderComponent>())
                        {
                            continue;
                        }

                        var rendercomp = entity.Components.GetComponent <RenderComponent>();


                        if (!models.TryGetValue(rendercomp.Name, out ModelInfo modelinfo))
                        {
                            modelinfo = new ModelInfo()
                            {
                                render  = true,
                                model   = Game.Content.Load <Model>(rendercomp.ModelName),
                                texture = Game.Content.Load <Texture2D>(rendercomp.TextureName),
                            };
                        }

                        if (!modelinfo.render)
                        {
                            continue;
                        }

                        var positioncomp = entity.Components.GetComponent <PositionComponent>();
                        var position     = positioncomp.Position;
                        var body         = entity.Components.GetComponent <BodyComponent>();

                        HeadComponent head = new HeadComponent();
                        if (entity.Components.ContainsComponent <HeadComponent>())
                        {
                            head = entity.Components.GetComponent <HeadComponent>();
                        }

                        Index3 shift = chunkOffset.ShortestDistanceXY(
                            position.ChunkIndex, planetSize);

                        var rotation = MathHelper.WrapAngle(positioncomp.Direction + MathHelper.ToRadians(rendercomp.BaseZRotation));

                        Matrix world = Matrix.CreateTranslation(
                            shift.X * Chunk.CHUNKSIZE_X + position.LocalPosition.X,
                            shift.Y * Chunk.CHUNKSIZE_Y + position.LocalPosition.Y,
                            shift.Z * Chunk.CHUNKSIZE_Z + position.LocalPosition.Z) * Matrix.CreateScaling(body.Radius * 2, body.Radius * 2, body.Height) * Matrix.CreateRotationZ(rotation);
                        effect.World = world;
                        modelinfo.model.Transform = world;

                        modelinfo.model.Draw(effect, modelinfo.texture);
                    }
                }
        }