예제 #1
0
        public void Draw(GraphicsDevice gd, Effect VertexShader, Effect PixelShader, CityContent content)
        {
            VertexShader.CurrentTechnique = VertexShader.Techniques[1];
            PixelShader.CurrentTechnique  = PixelShader.Techniques[4];

            VertexShader.Parameters["ObjModel"].SetValue(Matrix.Identity);
            VertexShader.Parameters["DepthBias"].SetValue(0f);

            for (int i = 0; i < Cells.Count; i++)
            {
                var cell  = Cells[i];
                var nhood = Data[cell.Ind];
                FillCell(gd, VertexShader, PixelShader, content, Cells[i], (nhood.Color ?? Color.White) * 0.2f);
            }


            VertexShader.CurrentTechnique = VertexShader.Techniques[2];
            PixelShader.CurrentTechnique  = PixelShader.Techniques[2];
        }
예제 #2
0
        public void Draw(Terrain terrain, GraphicsDevice gd, CityContent content, Effect VertexShader, Effect PixelShader, int passIndex, int size, BoundingFrustum frustrum)
        {
            var camPos = terrain.Camera.CalculateR();

            var cx = (int)Math.Round(camPos.X / 16);
            var cy = (int)Math.Round(camPos.Y / 16);

            var invalid = Chunks.Keys.Where(i =>
            {
                var x = i % 32;
                var y = i / 32;
                return((x < cx - 2) || (x > cx + 2) || (y < cy - 2) || (y > cy + 2));
            }).ToList();

            foreach (var c in invalid)
            {
                var chunk = Chunks[c];
                chunk.Dispose();
                Chunks.Remove(c);
            }

            gd.RasterizerState = RasterizerState.CullNone;
            gd.BlendState      = BlendState.NonPremultiplied;
            var genScale = 1 / ((terrain.Camera.LotSquish - 1) / 2 + 1);

            VertexShader.Parameters["ObjModel"].SetValue(Matrix.CreateScale(genScale, genScale * terrain.Camera.LotSquish, genScale));
            VertexShader.Parameters["DepthBias"].SetValue(-0.12f * terrain.Camera.DepthBiasScale);
            VertexShader.Parameters["HeightVScale"].SetValue(1f);// 1f / terrain.Camera.LotSquish);

            PixelShader.CurrentTechnique = PixelShader.Techniques[1];
            PixelShader.Parameters["ObjTex"].SetValue(content.TreeTex);
            PixelShader.CurrentTechnique.Passes[passIndex].Apply();

            gd.SamplerStates[1] = SamplerState.AnisotropicClamp;
            gd.BlendState       = BlendState.AlphaBlend;

            VertexShader.CurrentTechnique = VertexShader.Techniques[1];
            VertexShader.CurrentTechnique.Passes[5].Apply();

            var copy = new HashSet <int>(terrain.LotTileLookup.Keys.Select(i => (int)i.Y * 512 + (int)i.X));

            for (int y = Math.Max(0, cy - size); y <= Math.Min(31, cy + size); y++)
            {
                for (int x = Math.Max(0, cx - size); x <= Math.Min(31, cx + size); x++)
                {
                    var ind = y * 32 + x;
                    CityFoliageChunk chunk;
                    if (!Chunks.TryGetValue(ind, out chunk))
                    {
                        chunk = GenerateChunk(gd, x, y, copy);
                        Chunks.Add(chunk.Ind, chunk);
                    }

                    if (chunk.Indices != null && chunk.Bounds.Intersects(frustrum))
                    {
                        //var col = (new Vector4(m_TintColor.R / 255.0f, m_TintColor.G / 255.0f, m_TintColor.B / 255.0f, 1) * 1.25f) / fsof.NightLightColor.ToVector4();
                        //PixelShader.Parameters["LightCol"].SetValue(col);


                        gd.SetVertexBuffer(chunk.Vertices);
                        gd.Indices = chunk.Indices;

                        gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, chunk.Indices.IndexCount / 3);
                    }
                }
            }
        }
예제 #3
0
        public void FillEdges(GraphicsDevice gd, Effect VertexShader, Effect PixelShader, CityContent content, Color color)
        {
            VertexShader.CurrentTechnique.Passes[2].Apply();

            var frontDS = FrontDepthStencil;

            var bs = NoColor;

            gd.RasterizerState   = RasterizerState.CullNone;
            gd.BlendState        = bs;
            gd.DepthStencilState = frontDS;

            PixelShader.Parameters["ObjTex"].SetValue(TextureGenerator.GetPxWhite(gd));
            PixelShader.CurrentTechnique.Passes[0].Apply();

            foreach (var cell in Cells)
            {
                gd.SetVertexBuffer(cell.Vertices);
                gd.Indices = cell.Indices;

                gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, cell.Indices.IndexCount / 3);
            }

            var so = StencilCompareLine;

            gd.DepthStencilState = so;
            gd.BlendState        = BlendState.AlphaBlend;

            var screen = UIScreen.Current;
            var aspect = screen.ScreenWidth / (float)(screen.ScreenHeight);
            var dat    = new DGRP3DVert[]
            {
                new DGRP3DVert(new Vector3(-1, -1, 0f), Vector3.Zero, new Vector2(0, 0)),
                new DGRP3DVert(new Vector3(1, -1, 0f), Vector3.Zero, new Vector2(20 * aspect, 0)),
                new DGRP3DVert(new Vector3(-1, 1, 0f), Vector3.Zero, new Vector2(0, 20)),
                new DGRP3DVert(new Vector3(1, 1, 0f), Vector3.Zero, new Vector2(20 * aspect, 20)),
            };

            PixelShader.Parameters["ObjTex"].SetValue(TextureGenerator.GetPxWhite(gd));// content.NeighTextures[i%3]);
            PixelShader.Parameters["HighlightColor"].SetValue(color.ToVector4());
            var bM = VertexShader.Parameters["BaseMatrix"].GetValueMatrix();

            VertexShader.Parameters["BaseMatrix"].SetValue(Matrix.CreateOrthographic(2, 2, -1, 0.0001f));
            VertexShader.CurrentTechnique.Passes[2].Apply();
            PixelShader.CurrentTechnique.Passes[0].Apply();
            gd.SamplerStates[0] = SamplerState.LinearWrap;
            gd.DrawUserPrimitives(PrimitiveType.TriangleStrip, dat, 0, 2);
            VertexShader.Parameters["BaseMatrix"].SetValue(bM);
        }
예제 #4
0
        private void EdgeCell(GraphicsDevice gd, Effect VertexShader, Effect PixelShader, CityContent content, CompleteVCell cell, Color color)
        {
            VertexShader.CurrentTechnique.Passes[2].Apply();

            gd.RasterizerState   = RasterizerState.CullNone;
            gd.BlendState        = BlendState.Additive;
            gd.DepthStencilState = DepthStencilState.DepthRead;

            PixelShader.Parameters["HighlightColor"].SetValue(color.ToVector4());
            PixelShader.Parameters["ObjTex"].SetValue(NhoodGrad);
            PixelShader.CurrentTechnique.Passes[0].Apply();

            gd.SetVertexBuffer(cell.Vertices);
            gd.Indices = cell.Indices;

            gd.SamplerStates[0] = SamplerState.LinearClamp;

            gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, cell.Indices.IndexCount / 3);
        }
예제 #5
0
        public void DrawHover(GraphicsDevice gd, SpriteBatch batch, Effect VertexShader, Effect PixelShader, CityContent content)
        {
            VertexShader.CurrentTechnique = VertexShader.Techniques[1];
            PixelShader.CurrentTechnique  = PixelShader.Techniques[4];

            VertexShader.Parameters["ObjModel"].SetValue(Matrix.Identity);
            VertexShader.Parameters["DepthBias"].SetValue(0f);

            try
            {
                batch.Begin();
            }
            catch
            {
            }

            var toDraw          = new HashSet <UINeighBanner>();
            var bannerContainer = (UIScreen.Current as UI.Screens.CoreGameScreen).CityFloatingContainer;

            foreach (var hover in HoverPct)
            {
                var id = hover.Key;
                var f  = hover.Value;

                int cid;
                if (NHoodToCell.TryGetValue(id, out cid))
                {
                    var cell  = Cells[cid];
                    var nhood = Data[id];
                    EdgeCell(gd, VertexShader, PixelShader, content, cell, (nhood.Color ?? Color.White) * f * 0.6f);
                    FillCell(gd, VertexShader, PixelShader, content, cell, (nhood.Color ?? Color.White) * f * 0.15f);
                    DrawCellBanner(cell, toDraw, bannerContainer, f);
                }
            }

            if (BannerPct > 0)
            {
                foreach (var cell in Cells)
                {
                    float oldOpacity = 0f;
                    HoverPct.TryGetValue(cell.Ind, out oldOpacity);
                    if (cell.Ind >= Data.Count)
                    {
                        continue;
                    }
                    var nhood = Data[cell.Ind];
                    FillCell(gd, VertexShader, PixelShader, content, cell, (nhood.Color ?? Color.White) * BannerPct * 0.15f);
                    if (BannerPct > oldOpacity)
                    {
                        DrawCellBanner(cell, toDraw, bannerContainer, BannerPct);
                    }
                }
                FillEdges(gd, VertexShader, PixelShader, content, Color.Black * 0.5f * BannerPct);
            }


            var toDelete = Banners.Except(toDraw).ToList();

            foreach (var del in toDelete)
            {
                Banners.Remove(del);
                bannerContainer.Remove(del);
            }

            try
            {
                batch.End();
            }
            catch
            {
            }

            /*
             * for (int i = 0; i < Cells.Count; i++)
             * {
             *  EdgeCell(gd, VertexShader, PixelShader, content, Cells[i], Colours[i % Colours.Length]);
             * }
             */
        }
예제 #6
0
        public void DrawSlice(GraphicsDevice gd, CityContent content, Effect vs, Effect ps, int vsn, int psn, int ind, int chunkSize)
        {
            //assume a lot of the parameters have already been set
            //we just need to switch the textures and draw all the different buffers
            if (ind == -1)
            {
                DrawAll(gd, content, vs, ps, vsn, psn);
                return;
            }
            gd.BlendState = BlendState.AlphaBlend;

            ps.Parameters["VertexColorTex"].SetValue(content.VertexColor);
            ps.Parameters["UseVertexColor"].SetValue(true);
            vs.Parameters["DepthBias"].SetValue(0f);

            var chunkWidth = 512 / chunkSize;

            for (int i = 0; i < 5; i++)
            {
                if (LayerVertices[i] == null)
                {
                    continue;
                }
                ps.Parameters["TextureAtlasTex"].SetValue(content.TerrainTextures[i]);
                var trans = (1 - (i - 1) / 2) * 2 + ((4 - i) % 2);
                ps.Parameters["TransAtlasTex"].SetValue((i == 0) ? null : content.TransAtlas[trans]);

                if (i == 4)
                {
                    ps.CurrentTechnique = ps.Techniques[3];
                    ps.Parameters["BigWTex"].SetValue(content.BigWNormal);
                    ps.Parameters["SmallWTex"].SetValue(content.SmallWNormal);
                }

                ps.CurrentTechnique.Passes[psn].Apply();
                vs.CurrentTechnique.Passes[vsn].Apply();

                gd.SamplerStates[0] = SamplerState.LinearWrap;
                gd.SamplerStates[1] = SamplerState.LinearClamp;

                gd.SetVertexBuffer(LayerVertices[i]);
                gd.Indices = LayerIndices[i];

                var ranges = LayerSubPrims[i];

                DrawChunk(gd, ind, chunkWidth, ranges, LayerPrims[i]);
                if (i == 4)
                {
                    //HACK HACK HACK HACK
                    //Monogame OpenGL DOES NOT like these textures staying in samplers 3 and 4.
                    //for some reason they cause textures to randomly black out. null them immediately.
                    ps.Parameters["BigWTex"].SetValue((Texture2D)null);
                    ps.Parameters["SmallWTex"].SetValue((Texture2D)null);
                    ps.CurrentTechnique.Passes[0].Apply();

                    ps.CurrentTechnique = ps.Techniques[2];
                }
            }

            //draw road verts

            ps.Parameters["TextureAtlasTex"].SetValue(content.RoadAtlas);
            vs.Parameters["DepthBias"].SetValue(0f);
            ps.Parameters["UseVertexColor"].SetValue(false);

            ps.CurrentTechnique.Passes[psn].Apply();
            vs.CurrentTechnique.Passes[vsn].Apply();
            gd.SamplerStates[0] = RoadSampler;

            gd.SetVertexBuffer(RoadVertices);
            gd.Indices = RoadIndices;

            DrawChunk(gd, ind, chunkWidth, RoadSubPrims, RoadPrims);
        }
예제 #7
0
        public void DrawAll(GraphicsDevice gd, CityContent content, Effect vs, Effect ps, int vsn, int psn)
        {
            //assume a lot of the parameters have already been set
            //we just need to switch the textures and draw all the different buffers
            gd.BlendState = BlendState.AlphaBlend;

            ps.Parameters["VertexColorTex"].SetValue(content.VertexColor);
            ps.Parameters["UseVertexColor"].SetValue(true);
            vs.Parameters["DepthBias"].SetValue(0f);

            for (int i = 0; i < 5; i++)
            {
                ps.Parameters["TextureAtlasTex"].SetValue(content.TerrainTextures[i]);
                var trans = (1 - (i - 1) / 2) * 2 + ((4 - i) % 2);
                ps.Parameters["TransAtlasTex"].SetValue((i == 0) ? null : content.TransAtlas[trans]);

                if (i == 4)
                {
                    ps.CurrentTechnique = ps.Techniques[3];
                    ps.Parameters["BigWTex"].SetValue(content.BigWNormal);
                    ps.Parameters["SmallWTex"].SetValue(content.SmallWNormal);
                }

                vs.CurrentTechnique.Passes[vsn].Apply();
                ps.CurrentTechnique.Passes[psn].Apply();

                gd.SamplerStates[0] = SamplerState.LinearWrap;
                gd.SamplerStates[1] = SamplerState.LinearClamp;

                gd.SetVertexBuffer(LayerVertices[i]);
                gd.Indices = LayerIndices[i];
                if (LayerPrims[i] > 0)
                {
                    gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, LayerPrims[i]);
                }
                if (i == 4)
                {
                    //HACK HACK HACK HACK
                    //Monogame OpenGL DOES NOT like these textures staying in samplers 3 and 4.
                    //for some reason they cause textures to randomly black out. null them immediately.
                    ps.Parameters["BigWTex"].SetValue((Texture2D)null);
                    ps.Parameters["SmallWTex"].SetValue((Texture2D)null);
                    ps.CurrentTechnique.Passes[0].Apply();

                    ps.CurrentTechnique = ps.Techniques[2];
                }
            }
            //draw road verts

            ps.Parameters["TextureAtlasTex"].SetValue(content.RoadAtlas);
            vs.Parameters["DepthBias"].SetValue(0f);
            ps.Parameters["UseVertexColor"].SetValue(false);

            vs.CurrentTechnique.Passes[vsn].Apply();
            ps.CurrentTechnique.Passes[psn].Apply();
            gd.SamplerStates[0] = RoadSampler;

            gd.SetVertexBuffer(RoadVertices);
            gd.Indices = RoadIndices;
            if (RoadPrims > 0)
            {
                gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, RoadPrims);
            }

            var rts = gd.GetRenderTargets();

            gd.SetRenderTargets(rts);
            //gd.SetRenderTarget((RenderTarget2D)rts.FirstOrDefault()?.RenderTarget);
        }
예제 #8
0
        public void Draw(GraphicsDevice gd, Effect VertexShader, Effect PixelShader, int passIndex, CityContent content)
        {
            VertexShader.CurrentTechnique = VertexShader.Techniques[1];
            PixelShader.CurrentTechnique  = PixelShader.Techniques[4];

            VertexShader.Parameters["ObjModel"].SetValue(Matrix.Identity);
            VertexShader.Parameters["DepthBias"].SetValue(0f);

            var bM = VertexShader.Parameters["BaseMatrix"].GetValueMatrix();

            for (int i = 0; i < Cells.Count; i++)
            {
                //draw each cell
                VertexShader.CurrentTechnique.Passes[2].Apply();

                //var col = (new Vector4(m_TintColor.R / 255.0f, m_TintColor.G / 255.0f, m_TintColor.B / 255.0f, 1) * 1.25f) / fsof.NightLightColor.ToVector4();
                //PixelShader.Parameters["LightCol"].SetValue(col);

                var frontDS = FrontDepthStencil;

                var bs = NoColor;

                gd.RasterizerState   = RasterizerState.CullNone;
                gd.BlendState        = bs;
                gd.DepthStencilState = frontDS;

                PixelShader.Parameters["ObjTex"].SetValue(TextureGenerator.GetPxWhite(gd));
                PixelShader.CurrentTechnique.Passes[0].Apply();

                gd.SetVertexBuffer(Cells[i].Vertices);
                gd.Indices = Cells[i].Indices;

                gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, Cells[i].Indices.IndexCount / 3);

                var so = StencilCompare;

                gd.DepthStencilState = so;
                gd.BlendState        = BlendState.AlphaBlend;

                var screen = UIScreen.Current;
                var aspect = screen.ScreenWidth / (float)screen.ScreenHeight;
                var dat    = new DGRP3DVert[]
                {
                    new DGRP3DVert(new Vector3(-1, -1, 0f), Vector3.Zero, new Vector2(0, 0)),
                    new DGRP3DVert(new Vector3(1, -1, 0f), Vector3.Zero, new Vector2(20 * aspect, 0)),
                    new DGRP3DVert(new Vector3(-1, 1, 0f), Vector3.Zero, new Vector2(0, 20)),
                    new DGRP3DVert(new Vector3(1, 1, 0f), Vector3.Zero, new Vector2(20 * aspect, 20)),
                };

                PixelShader.Parameters["ObjTex"].SetValue(content.NeighTextures[i % 3]);
                PixelShader.Parameters["HighlightColor"].SetValue((Colours[i % Colours.Length] * 0.5f).ToVector4());
                VertexShader.Parameters["BaseMatrix"].SetValue(Matrix.CreateOrthographic(2, 2, -1, 1));
                VertexShader.CurrentTechnique.Passes[2].Apply();
                PixelShader.CurrentTechnique.Passes[0].Apply();
                gd.SamplerStates[0] = SamplerState.LinearWrap;
                gd.DrawUserPrimitives(PrimitiveType.TriangleStrip, dat, 0, 2);
                VertexShader.Parameters["BaseMatrix"].SetValue(bM);
            }

            VertexShader.CurrentTechnique = VertexShader.Techniques[2];
            PixelShader.CurrentTechnique  = PixelShader.Techniques[2];
        }