public BigTerrainScene() { var whiteboxInfo = Object3dManager.LoadFromObjSingle(Media.Get("whiteroom.obj")); var whitebox = Mesh3d.Create(new Object3dInfo(whiteboxInfo.Vertices), new GenericMaterial(new Vector3(1000, 1000, 1000))); whitebox.GetInstance(0).Scale(3000); whitebox.GetInstance(0).Translate(0, -1500, 0); Game.World.Scene.Add(whitebox); }
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"; }
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); }); }
private void ApplyVboIbo(Mesh3d mesh, string vbo) { if (vbo.Length == 0) { return; } var obj = Object3dManager.LoadFromRaw(Media.Get(vbo)); mesh.GetLodLevel(0).Info3d = new Object3dInfo(obj.Vertices); mesh.GetLodLevel(0).Info3d.Manager = obj; mesh.GetLodLevel(0).Info3d.Manager.Name = vbo; }
Mesh3d CreateDiffuseModelFromRaw(string obj, Vector3 color) { var terrain3dManager = Object3dManager.LoadFromRaw(Media.Get(obj)); var terrain3dInfo = new Object3dInfo(terrain3dManager.Vertices); var terrainMaterial = new GenericMaterial(); terrainMaterial.DiffuseColor = new Vector3(0); terrainMaterial.SpecularColor = new Vector3(1); terrainMaterial.Roughness = 0.5f; var terrainMesh = Mesh3d.Create(terrain3dInfo, terrainMaterial); return(terrainMesh); }
public static void Create() { var lucyobj = new Object3dInfo(Object3dManager.LoadFromObjSingle(Media.Get("sph1.obj")).Vertices); var bbmaterial = new GenericMaterial(); bbmaterial.Roughness = 0; bbmaterial.DiffuseColor = Vector3.Zero; bbmaterial.SpecularColor = Vector3.One; var lucy = Mesh3d.Create(lucyobj, bbmaterial); lucy.ClearInstances(); lucy.AutoRecalculateMatrixForOver16Instances = true; Game.World.Scene.Add(lucy); Commons.PickedMesh = lucy; Commons.Picked = null; int cnt = 0, din = 256; Renderer pp = new Renderer(din, din, 1); pp.GraphicsSettings.UseDeferred = true; pp.GraphicsSettings.UseRSM = false; pp.GraphicsSettings.UseVDAO = true; pp.GraphicsSettings.UseFog = false; pp.GraphicsSettings.UseBloom = false; pp.GraphicsSettings.UseCubeMapGI = false; Game.DisplayAdapter.MainRenderer.CubeMaps.Clear(); var internalMapping = new Dictionary <Renderer.CubeMapInfo, CubeMapFramebuffer>(); /* * for(float x = -10; x < 13; x += 4.0f) * { * for(float y = 0.2f; y < 15.3f; y += 6.0f) * { * for(float z = -3; z < 3.5; z += 3.0f) * { * CubeMapFramebuffer cubens = new CubeMapFramebuffer(din, din); * * cubens.SetPosition(x*0.98f - 0.35f, y, z - 0.25f); * * lucy.AddInstance(new Mesh3dInstance(new TransformationManager(cubens.GetPosition(), Quaternion.Identity, 0.1f), "cubemap-marker-" + cnt.ToString())); * var info = new Renderer.CubeMapInfo() * { * FalloffScale = 5.0f, * Texture = cubens.Texture, * Position = cubens.GetPosition() * }; * Game.DisplayAdapter.MainRenderer.CubeMaps.Add(info); * internalMapping.Add(info, cubens); * cnt++; * } * } * }*/ for (int i = 0; i < 100; i++) { CubeMapFramebuffer cubens = new CubeMapFramebuffer(din, din); cubens.SetPosition(0, i, 0); lucy.AddInstance(new Mesh3dInstance(new TransformationManager(cubens.GetPosition(), Quaternion.Identity, 0.1f), "cubemap-marker-" + cnt.ToString())); var info = new Renderer.CubeMapInfo() { FalloffScale = 7.0f, Texture = cubens.Texture, Position = cubens.GetPosition() }; Game.DisplayAdapter.MainRenderer.CubeMaps.Add(info); internalMapping.Add(info, cubens); cnt++; } int index = 0; bool livemode = false; Game.OnKeyUp += (xa, eargs) => { if (eargs.Key == OpenTK.Input.Key.PageUp) { index++; } if (eargs.Key == OpenTK.Input.Key.PageDown) { index--; } if (index > 9) { index = 0; } if (index < 0) { index = 9; } if (Commons.Picked == null) { index = 0; } Commons.Picked = lucy.GetInstance(index); TitleOutput.Message = "Picked cubemap ID " + index.ToString(); if (eargs.Key == OpenTK.Input.Key.End) { livemode = !livemode; } if (eargs.Key == OpenTK.Input.Key.Insert) { for (int i = 0; i < cnt; i++) { Game.DisplayAdapter.MainRenderer.CubeMaps[i].Position *= -1; internalMapping[Game.DisplayAdapter.MainRenderer.CubeMaps[i]].Clear(); } } if (eargs.Key == OpenTK.Input.Key.Home) { var cpos = Camera.MainDisplayCamera.GetPosition(); /* * float angle = 0; * for(int i = 0; i < cnt; i++) * { * float prc = (float)i / (float)cnt; * angle += 2.39996322f; * var disp = new Vector3(prc * 15.0f * (float)Math.Sin(angle), 1.0f - prc * 3.0f, prc * 15.0f * (float)Math.Cos(angle)); * lucy.GetInstance(i).SetPosition(cpos + disp); * }*/ for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { int i = y + x * 10; lucy.GetInstance(i).SetPosition(cpos + new Vector3(x - 5, 0, y - 5) * 2); } } } if (eargs.Key == OpenTK.Input.Key.Up) { Game.DisplayAdapter.MainRenderer.CubeMaps[index].FalloffScale += 0.1f; } if (eargs.Key == OpenTK.Input.Key.Down) { Game.DisplayAdapter.MainRenderer.CubeMaps[index].FalloffScale -= 0.1f; if (Game.DisplayAdapter.MainRenderer.CubeMaps[index].FalloffScale < 0.02) { Game.DisplayAdapter.MainRenderer.CubeMaps[index].FalloffScale = 0.02f; } } }; int ix = 0; Game.OnBeforeDraw += (od, dsd) => { // for(int i = 0; i < 23; i++) // { // if((lucy.GetInstance(i).GetPosition() - Game.DisplayAdapter.MainRenderer.CubeMaps[i].Position).Length > 0.01f) // { var cubes = Game.DisplayAdapter.MainRenderer.CubeMaps; if (!livemode) { for (int iz = 0; iz < cnt; iz++) { if ((lucy.GetInstance(iz).GetPosition() - cubes[iz].Position).Length > 0.01f) { //internalMapping[cubes[iz]].Clear(); } } for (int iz = 0; iz < cnt; iz++) { if ((lucy.GetInstance(iz).GetPosition() - cubes[iz].Position).Length > 0.01f) { Game.World.CurrentlyRenderedCubeMap = iz; pp.CubeMaps = cubes; cubes[iz].Position = lucy.GetInstance(iz).GetPosition(); internalMapping[cubes[iz]].SetPosition(lucy.GetInstance(iz).GetPosition()); pp.RenderToCubeMapFramebuffer(internalMapping[cubes[iz]]); internalMapping[cubes[iz]].GenerateMipMaps(); Game.World.CurrentlyRenderedCubeMap = -1; } } return; } int i = ix++; if (ix >= cnt) { ix = 0; } Game.World.CurrentlyRenderedCubeMap = i; pp.CubeMaps = cubes; cubes[i].Position = lucy.GetInstance(i).GetPosition(); internalMapping[cubes[i]].SetPosition(lucy.GetInstance(i).GetPosition()); pp.RenderToCubeMapFramebuffer(internalMapping[cubes[i]]); internalMapping[cubes[i]].GenerateMipMaps(); Game.World.CurrentlyRenderedCubeMap = -1; // } // } }; }
public HotelScene() { var scene = Game.World.Scene; Game.Invoke(() => { var unknownMaterial = new GenericMaterial(new Vector3(1)); var lines = System.IO.File.ReadAllLines(Media.Get("scene.scn")); int linec = lines.Length; var texmap = new Dictionary <string, TexInfo>(); var matermap = new Dictionary <string, GenericMaterial>(); var mat = new GenericMaterial(); var trans = Matrix4.Identity; var glossyNtex = new Texture(Media.Get("200_norm.jpg")); var fussyNtex = new Texture(Media.Get("174_norm.jpg")); for (int i = 0; i < linec; i++) { var line = lines[i]; if (line.Contains("scene.textures.texture")) { var sdot = line.Split('.'); string keyname = sdot[2]; if (!texmap.ContainsKey(keyname)) { texmap.Add(keyname, new TexInfo()); } if (line.Contains(keyname + ".gain")) { var vals = line.Split('=')[1]; vals.Trim(); texmap[keyname].Gain = StringToFloat(vals); } if (line.Contains(keyname + ".value")) { var vals = line.Split('=')[1].Trim().Split(' '); if (vals.Length >= 3) { texmap[keyname].Value = new Vector3(StringToFloat(vals[0]), StringToFloat(vals[1]), StringToFloat(vals[2])); } else if (vals.Length == 1) { texmap[keyname].Value = new Vector3(StringToFloat(vals[0])); } } if (line.Contains(keyname + ".uvscale")) { var vals = line.Split('=')[1].Trim().Split(' '); if (vals.Length >= 2) { texmap[keyname].UVScale = new Vector2(StringToFloat(vals[0]), StringToFloat(vals[1])); } else if (vals.Length == 1) { texmap[keyname].UVScale = new Vector2(StringToFloat(vals[0])); } } if (line.Contains(keyname + ".file")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); texmap[keyname].TexFile = vals.Replace(".exr", ".png"); } if (line.Contains(keyname + ".type")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); texmap[keyname].Type = vals; } } else if (line.Contains("scene.materials.material")) { var sdot = line.Split('.'); string keyname = sdot[2]; if (!matermap.ContainsKey(keyname)) { matermap.Add(keyname, new GenericMaterial()); } if (line.Contains(keyname + ".d ") || line.Contains(keyname + ".kd ")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); if (texmap.ContainsKey(vals)) { //matermap[keyname]. //matermap[keyname].if(texmap.ContainsKey(vals)) { if (texmap[vals].TexFile.Length > 1) { matermap[keyname].DiffuseTexture = new Texture(Media.Get(System.IO.Path.GetFileName(texmap[vals].TexFile))); } else { matermap[keyname].DiffuseColor = texmap[vals].Value; } // matermap[keyname].InvertUVYAxis = texmap[vals].InvertUVX || texmap[vals].InvertUVY; } } } if (line.Contains(keyname + ".uroughness")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); if (texmap.ContainsKey(vals)) { if (texmap[vals].TexFile.Length > 1) { matermap[keyname].RoughnessTexture = new Texture(Media.Get(System.IO.Path.GetFileName(texmap[vals].TexFile))); } else { matermap[keyname].Roughness = Math.Max(1f, texmap[vals].Value.X); } } } if (line.Contains(keyname + ".vroughness")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); if (texmap.ContainsKey(vals)) { if (texmap[vals].TexFile.Length > 1) { matermap[keyname].RoughnessTexture = new Texture(Media.Get(System.IO.Path.GetFileName(texmap[vals].TexFile))); } else { matermap[keyname].DiffuseColor = texmap[vals].Value; } } } if (line.Contains(keyname + ".n ")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); if (texmap.ContainsKey(vals)) { if (texmap[vals].TexFile.Length > 1) { matermap[keyname].NormalsTexture = new Texture(Media.Get(System.IO.Path.GetFileName(texmap[vals].TexFile))); } } } if (line.Contains(keyname + ".k ") || line.Contains(keyname + ".ks ")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); if (texmap.ContainsKey(vals)) { if (texmap[vals].TexFile.Length > 1) { matermap[keyname].SpecularTexture = new Texture(Media.Get(System.IO.Path.GetFileName(texmap[vals].TexFile))); } else { matermap[keyname].SpecularColor = texmap[vals].Value; } } } } else if (line.Contains(".ply")) { var s = line.Split('=')[1]; s = s.Trim(' ', '"'); var ply = Object3dManager.LoadFromRaw(Media.Get(System.IO.Path.GetFileName(s) + ".raw")); if (mat.NormalsTexture == null && mat.RoughnessTexture == null) { // mat.NormalsTexture = mat.Roughness < 0.3 ? glossyNtex : fussyNtex; // mat.NormalMapScale = 100.0f; } var mesh = Mesh3d.Create(new Object3dInfo(ply.Vertices), mat); trans.Transpose(); trans = trans * Matrix4.CreateScale(0.6f) * Matrix4.CreateRotationX(MathHelper.DegreesToRadians(-90.0f)); mesh.GetInstance(0).Transformation.SetPosition(trans.ExtractTranslation()); mesh.GetInstance(0).Transformation.SetOrientation(trans.ExtractRotation()); mesh.GetInstance(0).Transformation.SetScale(trans.ExtractScale()); scene.Add(mesh); mat = new GenericMaterial(); trans = Matrix4.Identity; } else if (line.Contains(".material")) { var vals = line.Split('=')[1].Trim(new char[] { ' ', '"' }); if (matermap.ContainsKey(vals)) { mat = matermap[vals]; } } if (line.Contains(".transformation")) { var s = line.Split('=')[1]; s = s.Trim(' '); var numbers = s.Split(' '); for (int f = 0; f < numbers.Length; f++) { trans[f % 4, (int)Math.Floor((float)f / 4)] = StringToFloat(numbers[f]); } } } //DynamicCubeMapController.Create(); DynamicCubeMapController.Create(); }); }
static void recurseNode(Assimp.Scene scn, Node node, StringBuilder scenesb, Matrix4x4 matrix) { Console.WriteLine("Scanning node " + node.Name); foreach (var mindex in node.MeshIndices) { var m = scn.Meshes[mindex]; string name = usednames.Contains(m.Name) ? (m.Name + (unnamed++).ToString()) : m.Name; var sb = new StringBuilder(); if (!File.Exists(outfile + "/" + name + ".mesh3d")) { var vertexinfos = new List <VertexInfo>(); 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); } if (doublefaced) { for (int i = indices.Length - 1; i >= 0; 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); Console.WriteLine("Saving " + outfile + "/" + name + ".raw"); element.SaveRawWithTangents(outfile + "/" + name + ".raw"); string matname = matdict[m.MaterialIndex]; scenesb.AppendLine("mesh3d " + outfile + "/" + name + ".mesh3d"); sb.AppendLine("lodlevel"); sb.AppendLine("start 0.0"); sb.AppendLine("end 99999.0"); sb.AppendLine("info3d " + outfile + "/" + name + ".raw"); sb.AppendLine("material " + matname); sb.AppendLine(); } sb.AppendLine("instance"); Assimp.Vector3D pvec, pscl; Assimp.Quaternion pquat; var a = new Assimp.Quaternion(new Vector3D(1, 0, 0), MathHelper.DegreesToRadians(-90)).GetMatrix(); var a2 = Matrix4x4.FromScaling(new Vector3D(0.01f)); (matrix * node.Transform * new Assimp.Matrix4x4(a) * a2).Decompose(out pscl, out pquat, out pvec); var q1 = new OpenTK.Quaternion(pquat.X, pquat.Y, pquat.Z, pquat.W).Inverted(); var m1 = Matrix4.CreateFromQuaternion(q1); m1[2, 0] = -m1[2, 0]; m1[2, 1] = -m1[2, 1]; m1[0, 2] = -m1[0, 2]; m1[1, 2] = -m1[1, 2]; var q2 = m1.ExtractRotation(true); sb.AppendLine(string.Format("translate {0} {1} {2}", ftos(pvec.X), ftos(pvec.Y), ftos(pvec.Z))); sb.AppendLine(string.Format("rotate {0} {1} {2} {3}", ftos(q1.X), ftos(q1.Y), ftos(q1.Z), ftos(q1.W))); sb.AppendLine(string.Format("scale {0} {1} {2}", ftos(pscl.X), ftos(pscl.Y), ftos(pscl.Z))); if (!File.Exists(outfile + "/" + name + ".mesh3d")) { Console.WriteLine("Saving " + outfile + "/" + name + ".mesh3d"); File.WriteAllText(outfile + "/" + name + ".mesh3d", sb.ToString()); } else { Console.WriteLine("Extending " + outfile + "/" + name + ".mesh3d"); File.AppendAllText(outfile + "/" + name + ".mesh3d", sb.ToString()); } } foreach (var c in node.Children) { if (c != node) { recurseNode(scn, c, scenesb, matrix * node.Transform); } } }
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"); }
public LightningTestScene() { var scene = Game.World.Scene; Game.Invoke(() => { var ground = CreateWall(new Vector2(-1000), new Vector2(1000), Quaternion.Identity, Vector3.Zero, new Vector3(0.1f, 0.4f, 1)); // scene.Add(ground); var obj = Object3dManager.LoadFromObjSingle(Media.Get("emily.obj")); obj.RecalulateNormals(Object3dManager.NormalRecalculationType.Smooth, 1); var t1 = CreateModel(obj.AsObject3dInfo(), new Vector3(0.8f), new Vector3(0.2f), 0.1f); t1.GetLodLevel(0).Material.SetDiffuseTexture("00_diffuse_unlit_unpainted.png"); t1.GetLodLevel(0).Material.SetSpecularTexture("00_specular_unlit_unpainted.png"); t1.GetLodLevel(0).Material.SetBumpTexture("00_displacement_misdfcro.png"); scene.Add(t1); var obj2 = Object3dManager.LoadFromObjSingle(Media.Get("emilylashes.obj")); var t2 = CreateModel(obj2.AsObject3dInfo(), new Vector3(0.8f), new Vector3(0.2f), 0.1f); t2.GetLodLevel(0).Material.DiffuseColor = Vector3.Zero; t2.GetLodLevel(0).Material.SpecularColor = Vector3.Zero; scene.Add(t2); // var obj2 = Object3dManager.LoadFromObjSingle(Media.Get("emilyeyes.obj")); // var t2 = CreateModel(obj2.AsObject3dInfo(), new Vector3(0.8f), new Vector3(0.2f), 0.1f); // t2.GetLodLevel(0).Material.DiffuseColor = Vector3.Zero; // t2.GetLodLevel(0).Material.SpecularColor = Vector3.Zero; // scene.Add(t2); /* * var m = CreateDiffuseModelFromObj("hipolysphere.obj", new Vector3(1)); * m.GetLodLevel(0).Material.SpecularColor = new Vector3(1.0f); * m.GetLodLevel(0).Material.DiffuseColor= new Vector3(0.0f); * m.GetLodLevel(0).Material.Roughness = 0.9f; * scene.Add(m);*/ /* var green = CreateWall(new Vector2(-100), new Vector2(100), Quaternion.FromAxisAngle(Vector3.UnitX, MathHelper.DegreesToRadians(90)), Vector3.Zero, new Vector3(0.2f, 1, 0.3f)); * var red = CreateWall(new Vector2(-100), new Vector2(100), Quaternion.FromAxisAngle(Vector3.UnitZ, MathHelper.DegreesToRadians(90)), Vector3.Zero, new Vector3(1, 0.2f, 0.2f)); * green.GetInstance(0).Translate(0, 0, -15); * red.GetInstance(0).Translate(15, 0, 0); * * var lucy = CreateDiffuseModelFromRaw("lucy.vbo.raw", new Vector3(1)); * //scene.Add(green); * //scene.Add(red); * * Game.CascadeShadowMaps.SetDirection(Quaternion.FromAxisAngle(Vector3.UnitX, MathHelper.DegreesToRadians(-25))); * * scene.Add(lucy); * * float[] billboardfloats = { * -1.0f, 0, 0, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, * 1.0f, 0, 0, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, * -1.0f, 1.0f, 0, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, * 1.0f, 1.0f, 0, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f * }; * * var billboardObj = new Object3dManager(VertexInfo.FromFloatArray(billboardfloats)).AsObject3dInfo(); * billboardObj.DrawMode = OpenTK.Graphics.OpenGL4.PrimitiveType.TriangleStrip; * string[] trees = new string[] { "vurt_PineSnowy.dds" }; * for(int id = 0; id < 1; id++) * { * var billboardMaterial = new GenericMaterial(); * billboardMaterial.UseForwardRenderer = true; * billboardMaterial.IsBillboard = true; * billboardMaterial.SetDiffuseTexture(trees[id]); * //billboardMaterial.SetNormalsTexture("alphatest_n.png"); * billboardMaterial.Roughness = 0.8f; * billboardMaterial.InvertUVy = true; * * var billboardMesh = Mesh3d.Create(billboardObj, billboardMaterial); * billboardMesh.ClearInstances(); * * for(int i = 0; i < 30000; i++) * { * var pos = new Vector3(rand(-1000, 1000), 0, rand(-1000, 1000)); * float uniscale = rand(1.7f, 2.5f); * var scale = new Vector3(uniscale, rand(12.3f, 12.4f), uniscale); * billboardMesh.AddInstance(new TransformationManager(pos, scale)); * } * billboardMesh.UpdateMatrix(); * * Game.CreateThread(() => * { * while(true) * { * //billboardMesh.IterationSortInstancesByDistanceFrom(Camera.MainDisplayCamera.Transformation.Position, 50); * billboardMesh.FullSortInstancesByDistanceFrom(Camera.MainDisplayCamera.Transformation.Position); * billboardMesh.UpdateMatrix(false); * } * }); * * * * scene.Add(billboardMesh); * } * string[] vegs = new string[] { "fieldgrassobj01.dds", "vurt_brownplants.dds" }; * for(int id = 0; id < 1; id++) * { * var billboardMaterial = new GenericMaterial(); * billboardMaterial.UseForwardRenderer = true; * billboardMaterial.IsBillboard = true; * billboardMaterial.SetDiffuseTexture(vegs[id]); * //billboardMaterial.SetNormalsTexture("alphatest_n.png"); * billboardMaterial.Roughness = 0.8f; * billboardMaterial.InvertUVy = true; * billboardMaterial.Blending = GenericMaterial.BlendingEnum.Alpha; * * var billboardMesh = Mesh3d.Create(billboardObj, billboardMaterial); * billboardMesh.ClearInstances(); * * for(int i = 0; i < 3000000; i++) * { * var pos = new Vector3(rand(-1000, 1000), 0, rand(-1000, 1000)); * float uniscale = rand(1.7f, 2.5f); * var scale = new Vector3(uniscale, rand(2.3f, 2.4f), uniscale); * billboardMesh.AddInstance(new TransformationManager(pos, scale)); * } * billboardMesh.UpdateMatrix(); * * Game.CreateThread(() => * { * while(true) * { * //billboardMesh.IterationSortInstancesByDistanceFrom(Camera.MainDisplayCamera.Transformation.Position, 50); * billboardMesh.FullSortInstancesByDistanceFrom(Camera.MainDisplayCamera.Transformation.Position); * billboardMesh.UpdateMatrix(false); * } * }); * * * * scene.Add(billboardMesh); * }*/ /* * var trootobj = new Object3dInfo(Object3dManager.LoadFromObjSingle(Media.Get("tree2r.obj")).Vertices); * var tleavobj = new Object3dInfo(Object3dManager.LoadFromObjSingle(Media.Get("tree2l.obj")).Vertices); * var rootmaterial = new GenericMaterial() * { * Roughness = 0.9f, * DiffuseColor = Vector3.One, * SpecularColor = Vector3.Zero * }; * var leavmaterial = new GenericMaterial() * { * Roughness = 0.9f, * DiffuseColor = Vector3.One, * SpecularColor = Vector3.Zero * }; * // leavmaterial.SetAlphaTexture("Branches0018_1_S_mask.png"); * leavmaterial.SetDiffuseTexture("Hedge 00 seamless.jpg"); * * var trm = Mesh3d.Create(trootobj, rootmaterial); * var tlm = Mesh3d.Create(tleavobj, leavmaterial); * scene.Add(trm); * scene.Add(tlm); * * trm.ClearInstances(); * tlm.ClearInstances(); * * * * for(int i = 0; i < 100; i++) * { * var pos = new Vector3(rand(-100, 100), 0, rand(-100, 100)); * float uniscale = rand(0.7f, 1.5f); * var scale = new Vector3(uniscale, rand(0.7f, 3.0f), uniscale); * trm.AddInstance(new TransformationManager(pos, scale)); * tlm.AddInstance(new TransformationManager(pos, scale)); * } * trm.UpdateMatrix(); * tlm.UpdateMatrix(); */ /* * var cubeMaterial = new GenericMaterial(new Vector3(1, 0, 0)); * var cubeObj3d = new Object3dInfo(Object3dGenerator.CreateCube(new Vector3(1), new Vector2(1)).Vertices); * //var cubeObj3d = new Object3dInfo(Object3dGenerator.CreateGround(new Vector2(-1), new Vector2(1), new Vector2(1), Vector3.UnitY).Vertices); * * var cubes = Mesh3d.Create(cubeObj3d, cubeMaterial); * cubes.ClearInstances(); * * for(int x = 0; x < 100; x++) * for(int y = 0; y < 100; y++) * for(int z = 0; z < 100; z++) * { * cubes.AddInstance(new TransformationManager(new Vector3(x, y, z) * 4)); * } * cubes.UpdateMatrix(); * * scene.Add(cubes);*/ /* * var cityMgr = Object3dManager.LoadFromObjSingle(Media.Get("somecity.obj")); * cityMgr.TryToFixVertexWinding(); * cityMgr.RecalulateNormals(Object3dManager.NormalRecalculationType.Flat); * var cityObj = cityMgr.AsObject3dInfo(); * var cityMat = new GenericMaterial() * { * DiffuseColor = new Vector3(1, 0.89f, 0.97f), * Roughness = 0.2f * }; * var cityMesh = Mesh3d.Create(cityObj, cityMat); * cityMesh.GetInstance(0).Translate(0, 0.1f, 0); * scene.Add(cityMesh);*/ // // var citimul = Object3dManager.LoadSceneFromObj("somecity.obj", "somecity.mtl"); // foreach(var m in citimul) // scene.Add(m); /* * var terrain = new Object3dInfo( Object3dGenerator.CreateTerrain(new Vector2(-10000, -10000), new Vector2(10000, 10000), new Vector2(1, -1), Vector3.UnitY, 512, (x,y) => 0).Vertices ); * var terrainMaterial = new GenericMaterial(); * terrainMaterial.Type = GenericMaterial.MaterialType.TessellatedTerrain; * terrainMaterial.TessellationMultiplier = 1.0f; * terrainMaterial.ParallaxHeightMultiplier = 200.0f; * terrainMaterial.SetBumpTexture("ee.png"); * var terrainMesh = Mesh3d.Create(terrain, terrainMaterial); * scene.Add(terrainMesh);*/ // DynamicCubeMapController.Create(); }); }
public static Object3dManager CreateTerrain(Vector2 start, Vector2 end, Vector2 uvScale, Vector3 normal, int subdivisions, Func <float, float, float> heightGenerator) { var VBO = new List <float>(); var VBOParts = new List <float[]>(); var indices = new List <uint>(); uint count = 0, x, y; float partx1, partx2, party1, party2; float[] vertex1, vertex2, vertex3, vertex4; uint xp1, yp1; Vector3 normal1; for (x = 0; x < subdivisions; x++) { for (y = 0; y < subdivisions; y++) { xp1 = x + 1; yp1 = y + 1; partx1 = (float)x / subdivisions; party1 = (float)y / subdivisions; partx2 = (float)(xp1) / subdivisions; party2 = (float)(yp1) / subdivisions; vertex1 = GetTerrainVertex(start, end, uvScale, normal, partx1, party1, heightGenerator); vertex2 = GetTerrainVertex(start, end, uvScale, normal, partx2, party1, heightGenerator); vertex3 = GetTerrainVertex(start, end, uvScale, normal, partx1, party2, heightGenerator); vertex4 = GetTerrainVertex(start, end, uvScale, normal, partx2, party2, heightGenerator); normal1 = -Vector3.Cross(GetVector(vertex2) - GetVector(vertex1), GetVector(vertex3) - GetVector(vertex1)).Normalized(); uint[] indicesPart = { count, count + 1, count + 2, count + 3, count + 2, count + 1 }; VBOParts.Add(new float[] { vertex3[0], vertex3[1], vertex3[2], vertex3[3], vertex3[4], normal1.X, normal1.Y, normal1.Z }); VBOParts.Add(new float[] { vertex4[0], vertex4[1], vertex4[2], vertex4[3], vertex4[4], normal1.X, normal1.Y, normal1.Z }); VBOParts.Add(new float[] { vertex1[0], vertex1[1], vertex1[2], vertex1[3], vertex1[4], normal1.X, normal1.Y, normal1.Z }); VBOParts.Add(new float[] { vertex2[0], vertex2[1], vertex2[2], vertex2[3], vertex2[4], normal1.X, normal1.Y, normal1.Z }); indices.AddRange(indicesPart); count += 4; } //GC.Collect(); } //GC.Collect(); for (int i = 0; i < VBOParts.Count; i++) { for (int g = i < 8 ? 0 : i - 8; g < (i + 8 > VBOParts.Count ? VBOParts.Count : i + 8); g++) { if (i != g) { if (VBOParts[i][0] == VBOParts[g][0] && VBOParts[i][1] == VBOParts[g][1] && VBOParts[i][2] == VBOParts[g][2]) //if(GetVector(VBOParts[i]) == GetVector(VBOParts[g])) { Vector3 n = Vector3.Lerp(GetNormal(VBOParts[i]), GetNormal(VBOParts[g]), 0.5f); VBOParts[i][5] = n.X; VBOParts[i][6] = n.Y; VBOParts[i][7] = n.Z; VBOParts[g][5] = n.X; VBOParts[g][6] = n.Y; VBOParts[g][7] = n.Z; } } } if (i > subdivisions * 4 + 8) { for (int g = i - subdivisions * 4 - 8; g < (i - subdivisions * 4 + 8 > VBOParts.Count ? VBOParts.Count : i - subdivisions * 4 + 8); g++) { if (i != g) { if (VBOParts[i][0] == VBOParts[g][0] && VBOParts[i][1] == VBOParts[g][1] && VBOParts[i][2] == VBOParts[g][2]) //if(GetVector(VBOParts[i]) == GetVector(VBOParts[g])) { Vector3 n = Vector3.Lerp(GetNormal(VBOParts[i]), GetNormal(VBOParts[g]), 0.5f); VBOParts[i][5] = n.X; VBOParts[i][6] = n.Y; VBOParts[i][7] = n.Z; VBOParts[g][5] = n.X; VBOParts[g][6] = n.Y; VBOParts[g][7] = n.Z; } } } } if (i < VBOParts.Count - subdivisions * 4 - 8) { for (int g = i + subdivisions * 4 - 8; g < (i + subdivisions * 4 + 8 > VBOParts.Count ? VBOParts.Count : i + subdivisions * 4 + 8); g++) { if (i != g) { if (VBOParts[i][0] == VBOParts[g][0] && VBOParts[i][1] == VBOParts[g][1] && VBOParts[i][2] == VBOParts[g][2]) //if(GetVector(VBOParts[i]) == GetVector(VBOParts[g])) { Vector3 n = Vector3.Lerp(GetNormal(VBOParts[i]), GetNormal(VBOParts[g]), 0.5f); VBOParts[i][5] = n.X; VBOParts[i][6] = n.Y; VBOParts[i][7] = n.Z; VBOParts[g][5] = n.X; VBOParts[g][6] = n.Y; VBOParts[g][7] = n.Z; } } } } } for (int i = 0; i < VBOParts.Count; i += 4) { VBO.AddRange(VBOParts[i]); VBO.AddRange(VBOParts[i + 1]); VBO.AddRange(VBOParts[i + 2]); VBO.AddRange(VBOParts[i + 3]); VBO.AddRange(VBOParts[i + 2]); VBO.AddRange(VBOParts[i + 1]); } var finalObject = new Object3dManager(VertexInfo.FromFloatArray(VBO.ToArray())); //SaveCache(start, end, uvScale, normal, subdivisions, finalObject); return(finalObject); }