Esempio n. 1
0
        override public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Shader effect, bool renderingForWater)
        {
            base.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderingForWater);

            if (Primitive == null)
            {
                var bounds = Vector4.Zero;
                var uvs    = Sheet.GenerateTileUVs(new Point(2, 0), out bounds);

                Primitive = new RawPrimitive();

                Primitive.AddQuad(Matrix.Identity, Color.White, Color.White, uvs, bounds);
            }

            if (Primitive.VertexCount == 0)
            {
                return;
            }

            var under = new VoxelHandle(chunks, GlobalVoxelCoordinate.FromVector3(Position));

            if (under.IsValid)
            {
                Color color = new Color(under.Sunlight ? 255 : 0, 255, 0);
                LightRamp = color;
            }
            else
            {
                LightRamp = new Color(200, 255, 0);
            }

            Color origTint = effect.VertexColorTint;

            effect.VertexColorTint = VertexColor;
            effect.LightRamp       = LightRamp;
            effect.World           = GlobalTransform;

            effect.MainTexture = Sheet.GetTexture();

            effect.EnableWind = false;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                Primitive.Render(graphicsDevice);
            }

            effect.VertexColorTint = origTint;
        }
Esempio n. 2
0
        override public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Shader effect, bool renderingForWater)
        {
            base.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderingForWater);

            if (Debugger.Switches.DrawRailNetwork)
            {
                DrawNeighborConnection(TrackAbove);
                DrawNeighborConnection(TrackBelow);
            }

            if (Primitive == null)
            {
                Primitive = new RawPrimitive();
                var voxel = GetContainingVoxel();
                AddSideQuad(voxel, new GlobalVoxelOffset(1, 0, 0), (float)Math.PI * 0.5f, new Vector3(0.45f, 0.0f, 0.0f));
                AddSideQuad(voxel, new GlobalVoxelOffset(0, 0, 1), 0.0f, new Vector3(0.0f, 0.0f, 0.45f));
                AddSideQuad(voxel, new GlobalVoxelOffset(-1, 0, 0), (float)Math.PI * 0.5f, new Vector3(-0.45f, 0.0f, 0.0f));
                AddSideQuad(voxel, new GlobalVoxelOffset(0, 0, -1), 0.0f, new Vector3(0.0f, 0.0f, -0.45f));
            }

            if (Primitive.VertexCount == 0)
            {
                return;
            }

            var under = new VoxelHandle(chunks, GlobalVoxelCoordinate.FromVector3(Position));

            if (under.IsValid)
            {
                Color color = new Color(under.Sunlight ? 255 : 0, 255, 0);
                LightRamp = color;
            }
            else
            {
                LightRamp = new Color(200, 255, 0);
            }

            Color origTint = effect.VertexColorTint;

            if (!Active)
            {
                DoStipple(effect);
            }
            effect.VertexColorTint = VertexColor;
            effect.LightRamp       = LightRamp;
            effect.World           = GlobalTransform;

            effect.MainTexture = Sheet.GetTexture();


            effect.EnableWind = false;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                Primitive.Render(graphicsDevice);
            }

            effect.VertexColorTint = origTint;
            if (!Active && !String.IsNullOrEmpty(previousEffect))
            {
                effect.CurrentTechnique = effect.Techniques[previousEffect];
            }
        }
Esempio n. 3
0
        override public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Shader effect, bool renderingForWater)
        {
            base.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderingForWater);

            if (Debugger.Switches.DrawRailNetwork)
            {
                //Drawer3D.DrawBox(GetContainingVoxel().GetBoundingBox(), Color.White, 0.01f, true);
                //Drawer3D.DrawLine(GetContainingVoxel().GetBoundingBox().Center(), GlobalTransform.Translation, Color.White, 0.01f);
                var transform = Matrix.CreateRotationY((float)Math.PI * 0.5f * (float)Piece.Orientation) * GlobalTransform;
                if (Library.GetRailPiece(Piece.RailPiece).HasValue(out var piece))
                {
                    foreach (var spline in piece.SplinePoints)
                    {
                        for (var i = 1; i < spline.Count; ++i)
                        {
                            Drawer3D.DrawLine(Vector3.Transform(spline[i - 1], transform),
                                              Vector3.Transform(spline[i], transform), Color.Purple, 0.1f);
                        }
                    }

                    foreach (var connection in piece.EnumerateConnections())
                    {
                        Drawer3D.DrawLine(Vector3.Transform(connection.Item1, transform) + new Vector3(0.0f, 0.2f, 0.0f),
                                          Vector3.Transform(connection.Item2, transform) + new Vector3(0.0f, 0.2f, 0.0f),
                                          Color.Brown, 0.1f);
                    }


                    //foreach (var neighborConnection in NeighborRails)
                    //{
                    //    var neighbor = Manager.FindComponent(neighborConnection.NeighborID);
                    //    if (neighbor == null)
                    //        Drawer3D.DrawLine(Position, Position + Vector3.UnitY, Color.CornflowerBlue, 0.1f);
                    //    else
                    //        Drawer3D.DrawLine(Position + new Vector3(0.0f, 0.5f, 0.0f), (neighbor as Body).Position + new Vector3(0.0f, 0.5f, 0.0f), Color.Teal, 0.1f);
                    //}
                }
            }

            if (!IsVisible)
            {
                return;
            }

            if (Primitive == null)
            {
                var bounds = Vector4.Zero;
                var uvs    = Sheet.GenerateTileUVs(Frame, out bounds);
                if (Library.GetRailPiece(Piece.RailPiece).HasValue(out var rawPiece))
                {
                    var transform = Matrix.CreateRotationY((float)Math.PI * 0.5f * (float)Piece.Orientation);

                    var realShape = 0;
                    if (rawPiece.AutoSlope)
                    {
                        var transformedConnections = GetTransformedConnections();
                        var matchingNeighbor1      = NeighborRails.FirstOrDefault(n => (n.Position - transformedConnections[0].Item1 - new Vector3(0.0f, 1.0f, 0.0f)).LengthSquared() < 0.001f);
                        var matchingNeighbor2      = NeighborRails.FirstOrDefault(n => (n.Position - transformedConnections[1].Item1 - new Vector3(0.0f, 1.0f, 0.0f)).LengthSquared() < 0.001f);

                        if (matchingNeighbor1 != null && matchingNeighbor2 != null)
                        {
                            realShape = 3;
                        }
                        else if (matchingNeighbor1 != null)
                        {
                            realShape = 1;
                        }
                        else if (matchingNeighbor2 != null)
                        {
                            realShape = 2;
                        }
                    }

                    Primitive = new RawPrimitive();
                    Primitive.AddVertex(new ExtendedVertex(Vector3.Transform(new Vector3(-0.5f, VertexHeightOffsets[realShape, 0], 0.5f), transform), Color.White, Color.White, uvs[0], bounds));
                    Primitive.AddVertex(new ExtendedVertex(Vector3.Transform(new Vector3(0.5f, VertexHeightOffsets[realShape, 1], 0.5f), transform), Color.White, Color.White, uvs[1], bounds));
                    Primitive.AddVertex(new ExtendedVertex(Vector3.Transform(new Vector3(0.5f, VertexHeightOffsets[realShape, 2], -0.5f), transform), Color.White, Color.White, uvs[2], bounds));
                    Primitive.AddVertex(new ExtendedVertex(Vector3.Transform(new Vector3(-0.5f, VertexHeightOffsets[realShape, 3], -0.5f), transform), Color.White, Color.White, uvs[3], bounds));
                    Primitive.AddIndicies(new short[] { 0, 1, 3, 1, 2, 3 });

                    var       sideBounds = Vector4.Zero;
                    Vector2[] sideUvs    = null;

                    sideUvs = Sheet.GenerateTileUVs(new Point(3, 4), out sideBounds);

                    AddScaffoldGeometry(transform, sideBounds, sideUvs, -1.0f, false);

                    if (realShape == 3)
                    {
                        AddScaffoldGeometry(transform, sideBounds, sideUvs, 0.0f, false);
                    }
                    else if (realShape == 1)
                    {
                        sideUvs = Sheet.GenerateTileUVs(new Point(0, 4), out sideBounds);
                        AddScaffoldGeometry(transform, sideBounds, sideUvs, 0.0f, true);
                    }
                    else if (realShape == 2)
                    {
                        sideUvs = Sheet.GenerateTileUVs(new Point(0, 4), out sideBounds);
                        AddScaffoldGeometry(transform, sideBounds, sideUvs, 0.0f, false);
                    }

                    // Todo: Make these static and avoid recalculating them constantly.
                    var bumperBackBounds  = Vector4.Zero;
                    var bumperBackUvs     = Sheet.GenerateTileUVs(new Point(0, 5), out bumperBackBounds);
                    var bumperFrontBounds = Vector4.Zero;
                    var bumperFrontUvs    = Sheet.GenerateTileUVs(new Point(1, 5), out bumperFrontBounds);
                    var bumperSideBounds  = Vector4.Zero;
                    var bumperSideUvs     = Sheet.GenerateTileUVs(new Point(2, 5), out bumperSideBounds);

                    foreach (var connection in GetTransformedConnections())
                    {
                        var matchingNeighbor = NeighborRails.FirstOrDefault(n => (n.Position - connection.Item1).LengthSquared() < 0.001f);
                        if (matchingNeighbor == null && rawPiece.AutoSlope)
                        {
                            matchingNeighbor = NeighborRails.FirstOrDefault(n => (n.Position - connection.Item1 - new Vector3(0.0f, 1.0f, 0.0f)).LengthSquared() < 0.001f);
                        }

                        if (matchingNeighbor == null)
                        {
                            var bumperOffset = connection.Item1 - GlobalTransform.Translation;
                            var bumperGap    = Vector3.Normalize(bumperOffset) * 0.1f;
                            var bumperAngle  = AngleBetweenVectors(new Vector2(bumperOffset.X, bumperOffset.Z), new Vector2(0, 0.5f));

                            var xDiag = bumperOffset.X <-0.001f || bumperOffset.X> 0.001f;
                            var zDiag = bumperOffset.Z <-0.001f || bumperOffset.Z> 0.001f;

                            if (xDiag && zDiag)
                            {
                                var y = bumperOffset.Y;
                                bumperOffset  *= sqrt2;
                                bumperOffset.Y = y;

                                var endBounds = Vector4.Zero;
                                var endUvs    = Sheet.GenerateTileUVs(new Point(6, 2), out endBounds);
                                Primitive.AddQuad(
                                    Matrix.CreateRotationY((float)Math.PI * 1.25f)
                                    * Matrix.CreateRotationY(bumperAngle)
                                    // This offset would not be correct if diagonals could slope.
                                    * Matrix.CreateTranslation(new Vector3(Sign(bumperOffset.X), 0.0f, Sign(bumperOffset.Z))),
                                    Color.White, Color.White, endUvs, endBounds);
                            }

                            Primitive.AddQuad(
                                Matrix.CreateRotationX(-(float)Math.PI * 0.5f)
                                * Matrix.CreateTranslation(0.0f, 0.3f, -0.2f)
                                * Matrix.CreateRotationY(bumperAngle)
                                * Matrix.CreateTranslation(bumperOffset + bumperGap),
                                Color.White, Color.White, bumperBackUvs, bumperBackBounds);

                            Primitive.AddQuad(
                                Matrix.CreateRotationX(-(float)Math.PI * 0.5f)
                                * Matrix.CreateTranslation(0.0f, 0.3f, -0.2f)
                                * Matrix.CreateRotationY(bumperAngle)
                                * Matrix.CreateTranslation(bumperOffset),
                                Color.White, Color.White, bumperFrontUvs, bumperFrontBounds);

                            if (VoxelHelpers.FindFirstVoxelBelow(GetContainingVoxel()).RampType == RampType.None)
                            {
                                Primitive.AddQuad(
                                    Matrix.CreateRotationX(-(float)Math.PI * 0.5f)
                                    * Matrix.CreateRotationY(-(float)Math.PI * 0.5f)
                                    * Matrix.CreateTranslation(0.3f, 0.3f, 0.18f)
                                    * Matrix.CreateRotationY(bumperAngle)
                                    * Matrix.CreateTranslation(bumperOffset),
                                    Color.White, Color.White, bumperSideUvs, bumperSideBounds);

                                Primitive.AddQuad(
                                    Matrix.CreateRotationX(-(float)Math.PI * 0.5f)
                                    * Matrix.CreateRotationY(-(float)Math.PI * 0.5f)
                                    * Matrix.CreateTranslation(-0.3f, 0.3f, 0.18f)
                                    * Matrix.CreateRotationY(bumperAngle)
                                    * Matrix.CreateTranslation(bumperOffset),
                                    Color.White, Color.White, bumperSideUvs, bumperSideBounds);
                            }
                        }
                    }
                }
            }

            // Everything that draws should set it's tint, making this pointless.

            var under = new VoxelHandle(chunks, GlobalVoxelCoordinate.FromVector3(Position));

            if (under.IsValid)
            {
                Color color = new Color(under.Sunlight ? 255 : 0, 255, 0);
                LightRamp = color;
            }
            else
            {
                LightRamp = new Color(200, 255, 0);
            }

            Color origTint = effect.VertexColorTint;

            if (!Active)
            {
                DoStipple(effect);
            }
            effect.VertexColorTint = VertexColor;
            effect.LightRamp       = LightRamp;
            effect.World           = GlobalTransform;

            effect.MainTexture = Sheet.GetTexture();


            effect.EnableWind = false;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                Primitive.Render(graphicsDevice);
            }

            effect.VertexColorTint = origTint;
            if (!Active)
            {
                EndDraw(effect);
            }
        }
Esempio n. 4
0
        override public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Shader effect, bool renderingForWater)
        {
            base.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderingForWater);

            if (Debugger.Switches.DrawPipeNetwork)
            {
                foreach (var neighborConnection in NeighborPipes)
                {
                    var neighbor = Manager.FindComponent(neighborConnection);
                    if (neighbor == null)
                    {
                        Drawer3D.DrawLine(Position, Position + Vector3.UnitY, Color.CornflowerBlue, 0.1f);
                    }
                    else
                    {
                        Drawer3D.DrawLine(Position + new Vector3(0.0f, 0.5f, 0.0f), (neighbor as GameComponent).Position + new Vector3(0.0f, 0.5f, 0.0f), new Color(SteamPressure, 0.0f, 0.0f, 1.0f), 0.1f);
                    }
                }

                Drawer3D.DrawBox(GetBoundingBox(), Color.Red, 0.01f, false);
                Drawer3D.DrawLine(Position, Position + Vector3.Transform(new Vector3(1, 0, 0), Matrix.CreateRotationY((float)Math.PI / 2 * (float)Orientation)), new Color(0.0f, 1.0f, 1.0f), 0.03f);
            }

            if (!DrawPipes)
            {
                return;
            }

            if (Primitive == null)
            {
                var bounds = Vector4.Zero;
                var uvs    = Sheet.GenerateTileUVs(new Point(0, 0), out bounds);

                Primitive = new RawPrimitive();

                foreach (var connection in NeighborPipes)
                {
                    var neighbor = Manager.FindComponent(connection) as GameComponent;
                    if (neighbor == null)
                    {
                        continue;
                    }

                    var orientationToNeighbor = OrientationHelper.DetectOrientationFromVector(new Vector3(neighbor.Position.X - this.Position.X, 0.0f, neighbor.Position.Z - this.Position.Z));
                    var pipeAngle             = Math.PI * 0.5f * ((float)Orientation - (float)orientationToNeighbor);

                    Primitive.AddQuad(
                        Matrix.CreateTranslation(0.5f, 0.0f, 0.0f)
                        * Matrix.CreateScale(0.5f, 0.5f, 0.5f)
                        * Matrix.CreateRotationX((float)Math.PI * 0.5f)
                        //* Matrix.CreateTranslation(0.0f, 0.3f, -0.2f)
                        * Matrix.CreateRotationY((float)pipeAngle),
                        //* Matrix.CreateTranslation(bumperOffset + bumperGap),
                        Color.White, Color.White, uvs, bounds);
                }
            }

            if (Primitive.VertexCount == 0)
            {
                return;
            }

            var under = new VoxelHandle(chunks, GlobalVoxelCoordinate.FromVector3(Position));

            if (under.IsValid)
            {
                Color color = new Color(under.Sunlight ? 255 : 0, 255, 0);
                LightRamp = color;
            }
            else
            {
                LightRamp = new Color(200, 255, 0);
            }

            Color origTint = effect.VertexColorTint;

            if (!Active)
            {
                DoStipple(effect);
            }
            effect.VertexColorTint = VertexColor;
            effect.LightRamp       = LightRamp;
            effect.World           = GlobalTransform;

            effect.MainTexture = Sheet.GetTexture();


            effect.EnableWind = false;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                Primitive.Render(graphicsDevice);
            }

            effect.VertexColorTint = origTint;
            if (!Active)
            {
                EndDraw(effect);
            }
        }
Esempio n. 5
0
        public static Texture2D RenderPatternIcons(GraphicsDevice device, Microsoft.Xna.Framework.Content.ContentManager Content, Gui.JsonTileSheet Sheet)
        {
            Initialize();

            var shader = new Shader(Content.Load <Effect>(ContentPaths.Shaders.TexturedShaders), true);

            var sqrt  = (int)(Math.Ceiling(Math.Sqrt(Patterns.Count)));
            var width = MathFunctions.NearestPowerOf2(sqrt * Sheet.TileWidth);

            var fitHorizontal = width / Sheet.TileWidth;
            var rowCount      = (int)Math.Ceiling((float)Patterns.Count / (float)fitHorizontal);
            var height        = MathFunctions.NearestPowerOf2(rowCount * Sheet.TileHeight);

            RenderTarget2D toReturn  = new RenderTarget2D(device, width, height, false, SurfaceFormat.Color, DepthFormat.Depth16, 16, RenderTargetUsage.PreserveContents);
            var            tileSheet = new SpriteSheet(ContentPaths.rail_tiles, 32);

            device.SetRenderTarget(toReturn);
            device.Clear(Color.Transparent);
            shader.SetTexturedTechnique();
            shader.MainTexture             = AssetManager.GetContentTexture(ContentPaths.rail_tiles);
            shader.SelfIlluminationEnabled = true;
            shader.SelfIlluminationTexture = AssetManager.GetContentTexture(ContentPaths.Terrain.terrain_illumination);
            shader.EnableShadows           = false;
            shader.EnableLighting          = false;
            shader.ClippingEnabled         = false;
            shader.CameraPosition          = new Vector3(-0.5f, 0.5f, 0.5f);
            shader.VertexColorTint         = Color.White;
            shader.LightRamp                = Color.White;
            shader.SunlightGradient         = AssetManager.GetContentTexture(ContentPaths.Gradients.sungradient);
            shader.AmbientOcclusionGradient = AssetManager.GetContentTexture(ContentPaths.Gradients.ambientgradient);
            shader.TorchlightGradient       = AssetManager.GetContentTexture(ContentPaths.Gradients.torchgradient);

            Viewport oldview = device.Viewport;
            int      rows    = height / Sheet.TileWidth;
            int      cols    = width / Sheet.TileWidth;

            device.ScissorRectangle  = new Rectangle(0, 0, Sheet.TileWidth, Sheet.TileHeight);
            device.RasterizerState   = RasterizerState.CullNone;
            device.DepthStencilState = DepthStencilState.Default;
            Vector3 half = Vector3.One * 0.5f;

            half = new Vector3(half.X, half.Y, half.Z);

            foreach (EffectPass pass in shader.CurrentTechnique.Passes)
            {
                int ID = 0;

                foreach (var type in Patterns)
                {
                    int row = ID / cols;
                    int col = ID % cols;

                    var xboundsMin = 0;
                    var xboundsMax = 0;
                    var yboundsMin = 0;
                    var yboundsMax = 0;

                    var primitive = new RawPrimitive();
                    foreach (var piece in type.Pieces)
                    {
                        var rawPiece = RailLibrary.GetRailPiece(piece.RailPiece);
                        var bounds   = Vector4.Zero;
                        var uvs      = tileSheet.GenerateTileUVs(rawPiece.Tile, out bounds);
                        primitive.AddQuad(
                            Matrix.CreateRotationY((float)Math.PI * 0.5f * (float)piece.Orientation)
                            * Matrix.CreateTranslation(new Vector3(piece.Offset.X, 0.0f, piece.Offset.Y)),
                            Color.White, Color.White, uvs, bounds);

                        xboundsMin = Math.Min(xboundsMin, piece.Offset.X);
                        xboundsMax = Math.Max(xboundsMax, piece.Offset.X);
                        yboundsMin = Math.Min(yboundsMin, piece.Offset.Y);
                        yboundsMax = Math.Max(yboundsMax, piece.Offset.Y);
                    }

                    float xSize = xboundsMax - xboundsMin + 1;
                    float ySize = yboundsMax - yboundsMin + 1;

                    var cameraPos = new Vector3(xboundsMin + (xSize / 2), 2.0f, yboundsMax + 1.0f);

                    device.Viewport = new Viewport(col * Sheet.TileWidth, row * Sheet.TileHeight, Sheet.TileWidth, Sheet.TileHeight);
                    shader.View     = Matrix.CreateLookAt(cameraPos,
                                                          new Vector3((xboundsMin + (xSize / 2)), 0.0f, yboundsMin),
                                                          Vector3.UnitY);
                    shader.Projection     = Matrix.CreatePerspectiveFieldOfView(1.0f, 1.0f, 0.1f, 10);
                    shader.World          = Matrix.Identity;
                    shader.CameraPosition = cameraPos;
                    pass.Apply();
                    primitive.Render(device);

                    ++ID;
                }
            }
            device.Viewport = oldview;
            device.SetRenderTarget(null);
            return((Texture2D)toReturn);
        }