Beispiel #1
0
        private void button4_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.GetCurrentProcess().MaxWorkingSet = new IntPtr(8L * 1024L * 1024L * 1024L);
            Media.SearchPath = "media";
            var meshes = Object3dManager.LoadSceneFromObj(textBox1.Text, textBox2.Text);
            var s      = GameScene.FromMesh3dList(meshes, textBox3.Text + "/", textBox4.Text, true);

            System.IO.File.WriteAllText(textBox3.Text + "/" + textBox5.Text + ".scene", s);
            button4.Text = "Done";
        }
Beispiel #2
0
        public DragonScene()
        {
            var scene = Game.World.Scene;

            var fiatmeshes = Object3dManager.LoadSceneFromObj("sponza2.obj", "sponza2.mtl", 1);

            fiatmeshes.ForEach((a) =>
            {
                a.GetLodLevel(0).DisableFaceCulling = true;
                scene.Add(a);
            });
        }
Beispiel #3
0
        private static void Main(string[] args)
        {
            Arguments = new Dictionary <string, string>();
            foreach (var a in args)
            {
                int i = a.IndexOf('=');
                if (i > 0)
                {
                    string k = a.Substring(0, i);
                    string v = a.Substring(i + 1);
                    Arguments.Add(k, v);
                }
            }
            mode             = args[0];
            infile           = args[1];
            outfile          = args[2];
            Media.SearchPath = "media";

            if (mode == "scene2assets")
            {
                string scenename = args[3];
                var    element   = new VEngine.FileFormats.GameScene(infile);
                element.Load();
                int           unnamed = 0;
                StringBuilder sceneb  = new StringBuilder();
                var           rand    = new Random();
                foreach (var m in element.Meshes)
                {
                    string name = "mesh" + (unnamed++);
                    SaveMeshToFile(m, name, outfile);
                    sceneb.AppendLine("mesh3d " + name + ".mesh3d");
                }
                File.WriteAllText(outfile + "/" + scenename, sceneb.ToString());
            }
            if (mode == "obj2raw")
            {
                var element = Object3dManager.LoadFromObjSingle(infile);
                element.SaveRaw(outfile);
            }
            if (mode == "obj2rawtangsmooth")
            {
                var element = Object3dManager.LoadFromObjSingle(infile);
                element.RecalulateNormals(Object3dManager.NormalRecalculationType.Smooth, 1);
                element.SaveRawWithTangents(outfile);
            }
            if (mode == "raw2rawtang")
            {
                var element = Object3dManager.LoadFromRaw(infile);
                element.SaveRawWithTangents(outfile);
            }
            if (mode == "obj2rawtang")
            {
                var element = Object3dManager.LoadFromObjSingle(infile);
                element.SaveRawWithTangents(outfile);
            }
            if (mode == "objscene2assets")
            {
                Console.WriteLine("Conversion started");
                var elements = Object3dManager.LoadSceneFromObj(infile + ".obj", infile + ".mtl");

                var           map       = new List <string>();
                var           r         = new Random();
                StringBuilder sceneb    = new StringBuilder();
                string        scenename = args[3];

                Console.WriteLine("Found elements " + elements.Count);
                foreach (var m in elements)
                {
                    string n = m.GetInstance(0).Name;
                    if (n == null || n.Length == 0 || map.Contains(n))
                    {
                        n = m.GetLodLevel(0).Info3d.Manager.Name;
                    }
                    if (n == null || n.Length == 0 || map.Contains(n))
                    {
                        n = m.GetLodLevel(0).Material.Name;
                    }
                    if (n == null || n.Length == 0 || map.Contains(n))
                    {
                        n = Path.GetFileNameWithoutExtension(m.GetLodLevel(0).Material.DiffuseTexture.FileName);
                    }
                    if (n == null || n.Length == 0 || map.Contains(n))
                    {
                        n = Path.GetFileNameWithoutExtension(m.GetLodLevel(0).Material.BumpTexture.FileName);
                    }
                    if (n == null || n.Length == 0 || map.Contains(n))
                    {
                        n = Path.GetFileNameWithoutExtension(m.GetLodLevel(0).Material.NormalsTexture.FileName);
                    }
                    while (n == null || n.Length == 0 || map.Contains(n))
                    {
                        n = "unknown_" + r.Next();
                    }
                    Console.WriteLine("Converting mesh " + n);

                    SaveMeshToFile(m, n, outfile);
                    sceneb.AppendLine("mesh3d " + n + ".mesh3d");
                }
                Console.WriteLine("Saving scene");
                File.WriteAllText(outfile + "/" + scenename, sceneb.ToString());
            }

            if (mode == "ply2raw")
            {
                var ai          = new AssimpContext();
                var vertexinfos = new List <VertexInfo>();
                var mi          = ai.ImportFile(infile, PostProcessSteps.Triangulate);
                foreach (var m in mi.Meshes)
                {
                    var indices = m.GetIndices();

                    for (int i = 0; i < indices.Length; i++)
                    {
                        int f  = indices[i];
                        var vp = m.Vertices[f];
                        var vn = m.Normals[f];
                        var vt = (m.TextureCoordinateChannels.Length == 0 || m.TextureCoordinateChannels[0].Count <= f) ? new Assimp.Vector3D(0) : m.TextureCoordinateChannels[0][f];
                        var vi = new VertexInfo()
                        {
                            Position = new Vector3(vp.X, vp.Y, vp.Z),
                            Normal   = new Vector3(vn.X, vn.Y, vn.Z),
                            UV       = new Vector2(vt.X, vt.Y)
                        };
                        vertexinfos.Add(vi);
                    }
                }

                var element = new Object3dManager(vertexinfos);
                element.SaveRaw(outfile);
            }
            if (mode == "generateterrain")
            {
                RequireArgs("resolution", "in", "out", "size", "uvscale", "height");
                var    img        = new System.Drawing.Bitmap(Arguments["in"]);
                int    resolution = int.Parse(Arguments["resolution"]);
                string ofile      = Arguments["out"];
                float  size       = float.Parse(Arguments["size"], System.Globalization.CultureInfo.InvariantCulture) / 2.0f;
                float  uvscale    = float.Parse(Arguments["uvscale"], System.Globalization.CultureInfo.InvariantCulture);
                float  height     = float.Parse(Arguments["height"], System.Globalization.CultureInfo.InvariantCulture);
                int    lx         = 1;
                var    t          = VEngine.Generators.Object3dGenerator.CreateTerrain(new Vector2(-size), new Vector2(size), new Vector2(uvscale), Vector3.UnitY, resolution, (x, y) =>
                {
                    int xpx = (int)(x * img.Size.Width);
                    int ypx = (int)(y * img.Size.Height);
                    if (xpx >= img.Size.Width)
                    {
                        xpx = img.Size.Width - 1;
                    }
                    if (ypx >= img.Size.Height)
                    {
                        ypx = img.Size.Height - 1;
                    }
                    var col  = img.GetPixel(xpx, ypx);
                    int zxzs = (int)(x * 100.0);
                    if (zxzs > lx)
                    {
                        Console.WriteLine(zxzs);
                    }
                    if (zxzs > lx)
                    {
                        lx = zxzs;
                    }
                    return(((float)(col.R) / 255.0f) * height);
                });
                Console.WriteLine("Starting saving");
                t.SaveRawWithTangents(ofile);
            }
            if (mode == "assimp2assets")
            {
                // convert.exe assimp2assets infile.dae outdir outname.scene
                var ai        = new AssimpContext();
                var usednames = new List <string>();
                var mi        = ai.ImportFile(infile, PostProcessSteps.Triangulate | PostProcessSteps.GenerateSmoothNormals);

                int    cnt       = 0;
                var    scenesb   = new StringBuilder();
                string scenename = args[3];
                doublefaced = args.Length == 5 && args[4] == "doublefaced";
                foreach (var m in mi.Materials)
                {
                    string name = usednames.Contains(m.Name) ? (m.Name + (unnamed++).ToString()) : m.Name;
                    var    sb   = new StringBuilder();
                    sb.AppendLine(string.Format("diffuse {0} {1} {2}", ftos(m.ColorDiffuse.R), ftos(m.ColorDiffuse.G), ftos(m.ColorDiffuse.B)));
                    sb.AppendLine(string.Format("roughness {0}", ftos(1.0f / (m.Shininess + 1.0f))));
                    sb.AppendLine(string.Format("metalness {0}", ftos(0.0f)));
                    sb.AppendLine();

                    if (m.HasTextureDiffuse)
                    {
                        sb.AppendLine("node");
                        sb.AppendLine(string.Format("texture {0}", Path.GetFileName(m.TextureDiffuse.FilePath)));
                        sb.AppendLine("mix REPLACE");
                        sb.AppendLine("target DIFFUSE");
                        sb.AppendLine("modifier LINEARIZE");
                        sb.AppendLine();
                    }
                    if (m.HasTextureReflection)
                    {
                        sb.AppendLine("node");
                        sb.AppendLine(string.Format("texture {0}", Path.GetFileName(m.TextureReflection.FilePath)));
                        sb.AppendLine("mix REPLACE");
                        sb.AppendLine("target ROUGHNESS");
                        sb.AppendLine();
                    }
                    if (m.HasTextureSpecular)
                    {
                        sb.AppendLine("node");
                        sb.AppendLine(string.Format("texture {0}", Path.GetFileName(m.TextureSpecular.FilePath)));
                        sb.AppendLine("mix REPLACE");
                        sb.AppendLine("target ROUGHNESS");
                        sb.AppendLine();
                    }
                    if (m.HasTextureNormal)
                    {
                        sb.AppendLine("node");
                        sb.AppendLine(string.Format("texture {0}", Path.GetFileName(m.TextureNormal.FilePath)));
                        sb.AppendLine("mix REPLACE");
                        sb.AppendLine("target NORMAL");
                        sb.AppendLine();
                    }
                    if (m.HasTextureDisplacement)
                    {
                        sb.AppendLine("node");
                        sb.AppendLine(string.Format("texture {0}", Path.GetFileName(m.TextureDisplacement.FilePath)));
                        sb.AppendLine("mix REPLACE");
                        sb.AppendLine("target BUMP");
                        sb.AppendLine();
                    }
                    Console.WriteLine("Saving " + outfile + "/" + name + ".material");
                    File.WriteAllText(outfile + "/" + name + ".material", sb.ToString());
                    matdict.Add(cnt, outfile + "/" + name + ".material");
                    cnt++;
                }
                recurseNode(mi, mi.RootNode, scenesb, Matrix4x4.Identity);
                Console.WriteLine("Saving " + outfile + "/" + scenename);
                File.WriteAllText(outfile + "/" + scenename, scenesb.ToString());
            }


            Console.WriteLine("Done");
        }