Beispiel #1
0
        /// <summary>
        /// Generate icon image for brick using MeshProjector.
        /// </summary>
        /// <param name="brick">Brick.</param>
        /// <param name="filePath">File path.</param>
        /// <param name="size">Image size.</param>
        public static void GenerateIcon(Brick brick, string filePath, int size)
        {
            //generate texture
            MeshProjector projector = new MeshProjector(new Vector3(-1000, 0, 0), 31);
            var           texture   = projector.Project(brick, size);

            //convert texture
            RenderTexture.active = texture;
            Texture2D texture2D = new Texture2D(texture.width, texture.height);

            texture2D.ReadPixels(new Rect(0, 0, texture.width, texture.height), 0, 0);
            RenderTexture.active = null;

            //save texture as png image
            var image = ImageConversion.EncodeToPNG(texture2D);

            System.IO.File.WriteAllBytes(filePath, image);
        }
        public void TestHeightmapCircle()
        {
            OBJ heightmap, circle;

            using (var file = File.OpenRead(@"C:\Users\Rhys\Documents\planetest.obj"))
            {
                heightmap = new OBJ(file);
            }
            using (var file = File.OpenRead(@"C:\Users\Rhys\Documents\circletest.obj"))
            {
                circle = new OBJ(file);
            }

            var baseMesh = new List <BaseMeshTriangle>();
            var projMesh = new List <MeshTriangle>();

            foreach (var group in heightmap.FacesByObjgroup)
            {
                var otri = new List <Vector3>();
                foreach (var tri in group.Value)
                {
                    otri.Add(heightmap.Vertices[tri[0] - 1]);
                    if (otri.Count == 3)
                    {
                        baseMesh.Add(new BaseMeshTriangle()
                        {
                            Vertices = otri.ToArray()
                        });
                        otri = new List <Vector3>();
                    }
                }
            }

            foreach (var group in circle.FacesByObjgroup)
            {
                var otri = new List <Vector3>();
                var otc  = new List <float[]>();
                foreach (var tri in group.Value)
                {
                    otri.Add(circle.Vertices[tri[0] - 1]);
                    otc.Add(new float[] { circle.TextureCoords[tri[1] - 1].X, circle.TextureCoords[tri[1] - 1].Y });
                    if (otri.Count == 3)
                    {
                        projMesh.Add(new MeshTriangle()
                        {
                            Vertices  = otri.ToArray(),
                            TexCoords = otc.ToArray()
                        });
                        otri = new List <Vector3>();
                        otc  = new List <float[]>();
                    }
                }
            }

            var proj = new MeshProjector(baseMesh, projMesh);

            proj.Project();

            //write result
            using (var file = File.Open(@"C:\Users\Rhys\Documents\test.obj", FileMode.Create))
            {
                SaveResult(proj, file);
            }
        }
        public void TestCombo()
        {
            var map = BasicMap();

            var timer = new System.Diagnostics.Stopwatch();

            timer.Start();

            var heightmap = new SimplifiedHeightmap(512, map);

            heightmap.BuildSecondDerivative();
            heightmap.GenerateFullTree();
            heightmap.GenerateMesh();

            timer.Stop();
            Console.WriteLine("!!! Heightmap took " + timer.ElapsedMilliseconds + "ms.");
            timer.Restart();

            var svg   = new SVGParser(File.ReadAllText(@"C:\Users\Rhys\Documents\roads.svg"));
            var paths = svg.ToLinePaths();

            var geom = new RoadGeometry(paths, TS1RoadTemplates.OLD_TOWN_DEFAULT_TEMPLATES);

            geom.GenerateIntersections();
            geom.GenerateRoadGeometry();

            timer.Stop();
            Console.WriteLine("!!! Road took " + timer.ElapsedMilliseconds + "ms.");
            timer.Restart();

            List <MeshProjector> projectors = new List <MeshProjector>();

            foreach (var pair in geom.Meshes)
            {
                var mesh = pair.Value;

                var baseTris = new List <BaseMeshTriangle>();
                for (int i = 0; i < heightmap.Indices.Count; i += 3)
                {
                    baseTris.Add(new BaseMeshTriangle()
                    {
                        Vertices = new Vector3[] {
                            heightmap.Vertices[heightmap.Indices[i]],
                            heightmap.Vertices[heightmap.Indices[i + 1]],
                            heightmap.Vertices[heightmap.Indices[i + 2]],
                        }
                    });
                }

                var projTris = new List <MeshTriangle>();
                for (int i = 0; i < mesh.Indices.Count; i += 3)
                {
                    projTris.Add(new MeshTriangle()
                    {
                        Vertices = new Vector3[] {
                            mesh.Vertices[mesh.Indices[i]].Position,
                            mesh.Vertices[mesh.Indices[i + 1]].Position,
                            mesh.Vertices[mesh.Indices[i + 2]].Position,
                        },
                        TexCoords = new float[][]
                        {
                            mesh.Vertices[mesh.Indices[i]].TexCoords,
                            mesh.Vertices[mesh.Indices[i + 1]].TexCoords,
                            mesh.Vertices[mesh.Indices[i + 2]].TexCoords,
                        }
                    });
                }

                var proj = new MeshProjector(baseTris, projTris);
                proj.Project();
                projectors.Add(proj);
            }

            timer.Stop();
            Console.WriteLine("!!! Projection took " + timer.ElapsedMilliseconds + "ms.");

            //write result
            using (var file = File.Open(@"C:\Users\Rhys\Documents\combined.obj", FileMode.Create))
            {
                SaveResults(projectors, file);
            }
        }