コード例 #1
0
        // NavMesh generator
        private List <VertexPositionColor> LoadNavMeshTestData(H32Loader h32)
        {
            // TODO - configure nav dir - determine filename using worldid xml

            DateTime TIMER;
            var      floorLineVertices = new List <VertexPositionColor>();

            bool generate     = true;  //false;
            bool loadFromFile = false; //true;//true;
            bool saveToFile   = false; //true;
            //NavMeshBuilder.OPTION_PARALLEL_THREADS = 1;
            //NavMeshBuilder.OPTION_REMOVE_SMALL_GRAPHS = false;

            CompiledNavMeshSet compiledMeshSet = null;

            if (generate)
            {
                TIMER = DateTime.Now;
                h32.LoadIntoGeoSpace(m_geoSpace);
                Debug.WriteLine("h32.LoadIntoGeoSpace TIME: " + (DateTime.Now - TIMER));

                TIMER = DateTime.Now;
                m_geoSpace.BuildTree();
                m_geoSpace.Validate();
                Debug.WriteLine("BuildTree TIME: " + (DateTime.Now - TIMER));

                float startX = 90;       //133-120; //340 - 20;
                float endX   = 200;      //133+20; //360 + 20;
                float startY = 103 - 20; //220 - 20;
                float endY   = 120;      // 103+20; //240 + 20;

                int top = 400;
                int bot = 0;

                float step           = 1f;
                var   navMeshBuilder = new NavMeshBuilder(startX, startY, bot, endX, endY, top, step);

                TIMER           = DateTime.Now;
                compiledMeshSet = navMeshBuilder.ScanFloor(m_geoSpace);
                Debug.WriteLine("ScanFloor TIME: " + (DateTime.Now - TIMER));

                if (saveToFile)
                {
                    // File roundtrip test
                    using (var fs = File.OpenWrite("c:\\temp2\\300200000.nav"))
                        compiledMeshSet.Save(fs);
                }
            }

            if (loadFromFile)
            {
                using (var fs = File.OpenRead(@"c:\temp2\out\210020000.nav"))  //"c:\\temp2\\300200000.nav"))
                    compiledMeshSet = CompiledNavMeshSet.Load(fs);
            }

            Debug.WriteLine("Total estimated compiled mesh size in bytes: " + compiledMeshSet.GetEstimatedFileSizeInBytes());

            var compiledMesh = //compiledMeshSet.FindSubgraphUnderPointWithSnap(126, 66, 145+2, maxFall: 10);
                               //compiledMeshSet.FindSubgraphUnderPointWithSnap(2484, 2614, 300, maxFall: 20);
                               compiledMeshSet.FindSubgraphUnderPointWithSnap(110, 110, 300, maxFall: 2220);

            if (compiledMesh == null)
            {
                throw new InvalidOperationException("point was not over a floor");
            }

            m_tempCompiledNavMeshSet = compiledMeshSet;

            // DEBUG MARKERS

            //    floorLineVertices.Add(new VertexPositionColor(new Vector3(126, 65, 144.3f + 3), Color.Lime));
            //    floorLineVertices.Add(new VertexPositionColor(new Vector3(126, 65, 144.3f), Color.Lime));

            //    floorLineVertices.Add(new VertexPositionColor(new Vector3(127, 64, 145.09f + 3), Color.Red));
            //    floorLineVertices.Add(new VertexPositionColor(new Vector3(127, 64, 145.09f), Color.Red));

            //=============================================
            // TESTING COMPILED MESH GETNEIGHBOR

            //start...
            //    floorLineVertices.Add(new VertexPositionColor(new Vector3(133, 103, 150), Color.White));
            //  floorLineVertices.Add(new VertexPositionColor(new Vector3(133, 103, 144), Color.White));

            /*
             * var curNode = compiledMesh.FindFloorUnderPoint(132.9f, 102.9f, 149, 20);
             * Vector3 v = compiledMesh.WorldFromBlockIndex(curNode.blockIndex, 144);
             *
             * floorLineVertices.Add(new VertexPositionColor(v + new Vector3(0,0,5), Color.Lime));
             * floorLineVertices.Add(new VertexPositionColor(v, Color.Lime));
             *
             * curNode = compiledMesh.GetNeighbor(curNode, 0, 1);
             * v = compiledMesh.WorldFromBlockIndex(curNode.blockIndex, 144);
             * floorLineVertices.Add(new VertexPositionColor(v + new Vector3(0, 0, 5), Color.Cyan));
             * floorLineVertices.Add(new VertexPositionColor(v, Color.Lime));
             *
             * curNode = compiledMesh.GetNeighbor(curNode, 0, 1);
             * v = compiledMesh.WorldFromBlockIndex(curNode.blockIndex, 144);
             * floorLineVertices.Add(new VertexPositionColor(v + new Vector3(0, 0, 5), Color.Yellow));
             * floorLineVertices.Add(new VertexPositionColor(v, Color.Lime));
             *
             * curNode = compiledMesh.GetNeighbor(curNode, 1, 1);
             * v = compiledMesh.WorldFromBlockIndex(curNode.blockIndex, 144);
             * floorLineVertices.Add(new VertexPositionColor(v + new Vector3(0, 0, 5), Color.White));
             * floorLineVertices.Add(new VertexPositionColor(v, Color.Lime));
             */
            // END TEST
            //=============================================



            //=============================================
            // TEST A*

            //TestAStar(compiledMesh, new Vector3(141, 21, 145), new Vector3(140, 28, 144), 10, floorLineVertices);

            //TestAStar(compiledMesh, new Vector3(155, 56, 144), new Vector3(169, 23, 144), 10, floorLineVertices);

            //    TestAStar(compiledMesh, new Vector3(130, 101, 145), new Vector3(125, 106, 147), 10, floorLineVertices);
            // END A*
            //=============================================



            TIMER = DateTime.Now;
            for (int bY = 0; bY < compiledMesh.BlockHeight; bY++)
            {
                for (int bX = 0; bX < compiledMesh.BlockWidth; bX++)
                {
                    float x = compiledMesh.X1 + bX * compiledMesh.Step;
                    float y = compiledMesh.Y1 + bY * compiledMesh.Step;

                    compiledMesh.ForeachHeightAtXY(bX, bY,
                                                   (directionFlags, z) =>
                    {
                        /*Color xcolor = Color.Lime;
                         *
                         * //if (bX == 28 && bY == 0) Debugger.Break();
                         *
                         * if (y > 221 && y < 223 && x > 346 && x < 348)
                         *  xcolor = Color.White;
                         * else if (y > 221 && y < 223 && x > 347 && x < 349)
                         *  xcolor = Color.Blue;
                         * else if (y >= 221 && y <= 223 && x >= 346 && x <= 348)
                         *  xcolor = Color.Red;*/


                        // DRAW ELEVATION MARKER

                        /*    floorLineVertices.Add(new VertexPositionColor(new Vector3(x - 0.1f, y, z), xcolor));
                         *  floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y + 0.1f, z), xcolor));
                         *
                         *  floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y + 0.1f, z), xcolor));
                         *  floorLineVertices.Add(new VertexPositionColor(new Vector3(x + 0.1f, y, z), xcolor));
                         *
                         *  floorLineVertices.Add(new VertexPositionColor(new Vector3(x + 0.1f, y, z), xcolor));
                         *  floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y - 0.1f, z), xcolor));
                         *
                         *  floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y - 0.1f, z), xcolor));
                         *  floorLineVertices.Add(new VertexPositionColor(new Vector3(x - 0.1f, y, z), xcolor));
                         */

                        // EDGE CONNECTIONS
                        // draw lines through the compass points...

                        float halfstep = compiledMesh.Step;
                        float n;

                        n = compiledMesh.GetEdge(bX, bY, z, directionFlags, NavMeshUtil.DIRECTION_LEFT);
                        if (n >= 0)
                        {
                            var test = Color.Orange;
                            if (Math.Abs(z - n) >= 0.9f)
                            {
                                test = Color.Red;
                            }

                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y, z), test));
                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x - halfstep, y, n), test));
                        }
                        n = compiledMesh.GetEdge(bX, bY, z, directionFlags, NavMeshUtil.DIRECTION_TL);
                        if (n >= 0)
                        {
                            var test = Color.Orange;
                            if (Math.Abs(z - n) / (new Vector2(x, y) - new Vector2(x - halfstep, y + halfstep)).Length() >= 1.4f)
                            {
                                test = Color.Magenta;
                            }

                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y, z), test));
                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x - halfstep, y + halfstep, n), test));
                        }
                        n = compiledMesh.GetEdge(bX, bY, z, directionFlags, NavMeshUtil.DIRECTION_TOP);
                        if (n >= 0)
                        {
                            var test = Color.Orange;
                            if (Math.Abs(z - n) >= 0.9f)
                            {
                                test = Color.Red;
                            }

                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y, z), test));
                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y + halfstep, n), test));
                        }
                        n = compiledMesh.GetEdge(bX, bY, z, directionFlags, NavMeshUtil.DIRECTION_TR);
                        if (n >= 0)
                        {
                            var test = Color.Orange;
                            if (Math.Abs(z - n) / (new Vector2(x, y) - new Vector2(x + halfstep, y + halfstep)).Length() >= 1.4f)
                            {
                                test = Color.Purple;
                            }

                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x, y, z), test));
                            floorLineVertices.Add(new VertexPositionColor(new Vector3(x + halfstep, y + halfstep, n), test));
                        }
                    });
                }
            }
            Debug.WriteLine("MESH ASSEMBLY TIME: " + (DateTime.Now - TIMER));
            return(floorLineVertices);
        }
コード例 #2
0
        private void LoadContentHelper(DirManager levelDir, DirManager meshesDir)
        {
            var TIMER = DateTime.Now;

            //=========

            var vertices               = new List <VertexPositionColor>();
            var vegVertices            = new List <VertexPositionColor>();
            var collisionVertices      = new List <VertexPositionColor>();
            var doorVertices1          = new List <VertexPositionColor>();
            var doorVertices2          = new List <VertexPositionColor>();
            var doorCollisionVertices1 = new List <VertexPositionColor>();
            var doorCollisionVertices2 = new List <VertexPositionColor>();
            var lineVertices           = new List <VertexPositionColor>();

            //==========
            //Window.Title = "Loading brushes...";
            LoadBrushes(meshesDir, levelDir, vertices, collisionVertices, lineVertices);

            //============
            //Window.Title = "Loading vegetation...";
            LoadVegetation(meshesDir, levelDir, vegVertices, collisionVertices, lineVertices);

            //============
            // doors have 2 states - start and end. doors can start open or closed, so the end state is the opposite.
            LoadDoors(meshesDir, levelDir, 0, doorVertices1, doorCollisionVertices1, lineVertices);
            LoadDoors(meshesDir, levelDir, 999999, doorVertices2, doorCollisionVertices2, lineVertices);

            // ===========
            // terrain
            H32Loader h32;

            using (var landMapStream = levelDir.OpenFile(@"terrain\land_map.h32"))
                h32 = new H32Loader(landMapStream);

            Debug.WriteLine("Data load TIME: " + (DateTime.Now - TIMER));



            // ============== NAVMESH TEST =====================
            var floorLineVertices =
                new List <VertexPositionColor>(); // disable navmesh

            //LoadNavMeshTestData(h32);



            //============
            m_renderData.vertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, vertices);

            m_renderData.vegetationVertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, vegVertices);

            m_renderData.collisionVertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, collisionVertices);

            m_renderData.doorVertexBuffer1          = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorVertices1);
            m_renderData.doorVertexBuffer2          = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorVertices2);
            m_renderData.doorCollisionVertexBuffer1 = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorCollisionVertices1);
            m_renderData.doorCollisionVertexBuffer2 = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorCollisionVertices2);

            if (lineVertices.Count > 0)
            {
                m_renderData.lineVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), lineVertices.Count, BufferUsage.WriteOnly);
                m_renderData.lineVertexBuffer.SetData <VertexPositionColor>(lineVertices.ToArray());
            }

            if (floorLineVertices.Count > 0)
            {
                m_renderData.floorLineVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), floorLineVertices.Count, BufferUsage.WriteOnly);
                m_renderData.floorLineVertexBuffer.SetData <VertexPositionColor>(floorLineVertices.ToArray());
            }

            vertices               = null;
            vegVertices            = null;
            collisionVertices      = null;
            doorVertices1          = null;
            doorVertices2          = null;
            doorCollisionVertices1 = null;
            doorCollisionVertices2 = null;
            lineVertices           = null;
            GC.Collect();

            //============

            //Window.Title = "Loading terrain...";
            using (var levelDataXml = levelDir.OpenFile("leveldata.xml"))
            {
                m_renderData.terrainVertexBuffer =
                    LoadH32Terrain(GraphicsDevice, h32, new LevelDataXmlLoader(levelDataXml).WaterLevel);
            }
            GC.Collect();
        }
コード例 #3
0
        // load terrain vertices for rendering
        private List <VertexBuffer> LoadH32Terrain(GraphicsDevice GraphicsDevice, H32Loader h32, float waterLevel)
        {
            if (h32.isEmpty)
            {
                return(null);
            }

            bool createBothTriangles = true;
            int  triCount            = (createBothTriangles ? 2 : 1);

            var terrainVertices = new VertexPositionColorNormal[h32.width * h32.width * 3 * triCount];
            int dst             = 0;

            for (int y = 0; y < h32.width - 1; y++)
            {
                for (int x = 0; x < h32.width - 1; x++)
                {
                    var p1 = h32.VertexLookup(x, y);
                    var p2 = h32.VertexLookup(x, y + 1);
                    var p3 = h32.VertexLookup(x + 1, y);
                    var p4 = h32.VertexLookup(x + 1, y + 1);

                    Color color;
                    if (h32.IsCutout(x, y))
                    {
                        color = Color.Cyan;
                    }
                    else if (p1.Z < waterLevel)
                    {
                        color = Color.Blue;
                    }
                    else if (p1.Z <= waterLevel + 2)
                    {
                        color = Color.SandyBrown;
                    }
                    else if (Math.Abs(p1.Z - p2.Z) >= 2 || Math.Abs(p1.Z - p3.Z) >= 2)
                    {
                        color = Color.White;//Red;
                    }
                    else
                    {
                        color = Color.Green;
                    }

                    // Tri 1/2
                    var normal1 = MathUtil.CalculateNormal(p1, p2, p3);
                    terrainVertices[dst++] = new VertexPositionColorNormal(p1, color, normal1);
                    terrainVertices[dst++] = new VertexPositionColorNormal(p3, color, normal1);
                    terrainVertices[dst++] = new VertexPositionColorNormal(p2, color, normal1);

                    // Tri 2/2
                    if (createBothTriangles)
                    {
                        var normal2 = MathUtil.CalculateNormal(p2, p4, p3);
                        terrainVertices[dst++] = new VertexPositionColorNormal(p2, color, normal2);
                        terrainVertices[dst++] = new VertexPositionColorNormal(p3, color, normal2);
                        terrainVertices[dst++] = new VertexPositionColorNormal(p4, color, normal2);
                    }
                }
            }

            return(VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, terrainVertices));
        }
コード例 #4
0
        // NavMesh generator
        private List <VertexPositionColor> LoadNavMeshTestData(H32Loader h32)
        {
            // TODO - configure nav dir - determine filename using worldid xml

            DateTime TIMER;
            bool     generate     = false;
            bool     loadFromFile = true;
            bool     saveToFile   = false;//true;
            //NavMeshBuilder.OPTION_PARALLEL_THREADS = 1;
            //NavMeshBuilder.OPTION_REMOVE_SMALL_GRAPHS = false;

            CompiledNavMeshSet compiledMeshSet = null;

            if (generate)
            {
                TIMER = DateTime.Now;
                h32.LoadIntoGeoSpace(m_geoSpace);
                Debug.WriteLine("h32.LoadIntoGeoSpace TIME: " + (DateTime.Now - TIMER));

                TIMER = DateTime.Now;
                m_geoSpace.BuildTree();
                m_geoSpace.Validate();
                Debug.WriteLine("BuildTree TIME: " + (DateTime.Now - TIMER));

                float startX = 90;       //133-120; //340 - 20;
                float endX   = 200;      //133+20; //360 + 20;
                float startY = 103 - 20; //220 - 20;
                float endY   = 120;      // 103+20; //240 + 20;

                int top = 400;
                int bot = 0;

                float step           = 1f;
                var   navMeshBuilder = new NavMeshBuilder(startX, startY, bot, endX, endY, top, step);

                TIMER           = DateTime.Now;
                compiledMeshSet = navMeshBuilder.ScanFloor(m_geoSpace);
                Debug.WriteLine("ScanFloor TIME: " + (DateTime.Now - TIMER));

                if (saveToFile)
                {
                    // File roundtrip test
                    using (var fs = File.OpenWrite("c:\\temp2\\300200000.nav"))
                        compiledMeshSet.Save(fs);
                }
            }

            if (loadFromFile)
            {
                using (var fs = File.OpenRead(@"c:\temp2\out\210020000.nav"))  //"c:\\temp2\\300200000.nav"))
                    compiledMeshSet = CompiledNavMeshSet.Load(fs);
            }

            Debug.WriteLine("Total estimated compiled mesh size in bytes: " + compiledMeshSet.GetEstimatedFileSizeInBytes());

            var compiledMesh = //compiledMeshSet.FindSubgraphUnderPointWithSnap(126, 66, 145+2, maxFall: 10);
                               //compiledMeshSet.FindSubgraphUnderPointWithSnap(2484, 2614, 300, maxFall: 20);
                               compiledMeshSet.FindSubgraphUnderPointWithSnap(110, 110, 300, maxFall: 2220);

            if (compiledMesh == null)
            {
                throw new InvalidOperationException("point was not over a floor");
            }

            m_tempCompiledNavMeshSet = compiledMeshSet;

            TIMER = DateTime.Now;
            var floorLineVertices = RenderFloorLines(compiledMesh, Vector3.Zero, 0);

            Debug.WriteLine("MESH ASSEMBLY TIME: " + (DateTime.Now - TIMER));

            return(floorLineVertices);
        }