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; }
static void SaveMeshToFile(Mesh3d m, string name, string outfile) { StringBuilder materialb = new StringBuilder(); var mat = m.GetLodLevel(0).Material; var i3d = m.GetLodLevel(0).Info3d; var inst = m.GetInstance(0); string meshname = name + ".mesh3d"; string matname = name + ".material"; string rawfile = name + ".raw"; i3d.Manager.ReverseYUV(1); i3d.Manager.SaveRawWithTangents(outfile + "/" + rawfile); materialb.AppendLine(string.Format("diffuse {0} {1} {2}", ftos(mat.DiffuseColor.X), ftos(mat.DiffuseColor.Y), ftos(mat.DiffuseColor.Z))); materialb.AppendLine(string.Format("roughness {0}", ftos(mat.Roughness))); materialb.AppendLine(string.Format("metalness {0}", 0.2f)); materialb.AppendLine(); if (mat.NormalsTexture != null) { materialb.AppendLine("node"); materialb.AppendLine(string.Format("texture {0}", mat.NormalsTexture.FileName)); materialb.AppendLine("mix REPLACE"); materialb.AppendLine("target NORMAL"); materialb.AppendLine(); } if (mat.DiffuseTexture != null) { materialb.AppendLine("node"); materialb.AppendLine(string.Format("texture {0}", mat.DiffuseTexture.FileName)); materialb.AppendLine("mix REPLACE"); materialb.AppendLine("target DIFFUSE"); materialb.AppendLine("modifier LINEARIZE"); materialb.AppendLine(); } if (mat.BumpTexture != null) { materialb.AppendLine("node"); materialb.AppendLine(string.Format("texture {0}", mat.BumpTexture.FileName)); materialb.AppendLine("mix REPLACE"); materialb.AppendLine("target BUMP"); materialb.AppendLine(); } if (mat.RoughnessTexture != null) { materialb.AppendLine("node"); materialb.AppendLine(string.Format("texture {0}", mat.RoughnessTexture.FileName)); materialb.AppendLine("mix REPLACE"); materialb.AppendLine("target ROUGHNESS"); materialb.AppendLine(); } File.WriteAllText(outfile + "/" + matname, materialb.ToString()); StringBuilder meshb = new StringBuilder(); meshb.AppendLine("lodlevel"); meshb.AppendLine("start 0"); meshb.AppendLine("end 99999"); meshb.AppendLine(string.Format("info3d {0}", rawfile)); meshb.AppendLine(string.Format("material {0}", matname)); meshb.AppendLine(); meshb.AppendLine("instance"); meshb.AppendLine(string.Format("translate {0} {1} {2}", ftos(inst.Transformation.Position.R.X), ftos(inst.Transformation.Position.R.Y), ftos(inst.Transformation.Position.R.Z))); meshb.AppendLine(string.Format("scale {0} {1} {2}", ftos(inst.Transformation.ScaleValue.R.X), ftos(inst.Transformation.ScaleValue.R.Y), ftos(inst.Transformation.ScaleValue.R.Z))); meshb.AppendLine(string.Format("rotate {0} {1} {2} {3}", ftos(inst.Transformation.Orientation.R.X), ftos(inst.Transformation.Orientation.R.Y), ftos(inst.Transformation.Orientation.R.Z), ftos(inst.Transformation.Orientation.R.W))); File.WriteAllText(outfile + "/" + meshname, meshb.ToString()); }
public static void SetUpInputBehaviours() { Game.OnKeyUp += (o, e) => { if (e.Key == OpenTK.Input.Key.F1 && !e.Shift) { InterpolateCameraFromSaved(0); } if (e.Key == OpenTK.Input.Key.F2 && !e.Shift) { InterpolateCameraFromSaved(1); } if (e.Key == OpenTK.Input.Key.F3 && !e.Shift) { InterpolateCameraFromSaved(2); } if (e.Key == OpenTK.Input.Key.F4 && !e.Shift) { InterpolateCameraFromSaved(3); } if (e.Key == OpenTK.Input.Key.F5 && !e.Shift) { InterpolateCameraFromSaved(4); } if (e.Key == OpenTK.Input.Key.F6 && !e.Shift) { InterpolateCameraFromSaved(5); } if (e.Key == OpenTK.Input.Key.F7 && !e.Shift) { InterpolateCameraFromSaved(6); } if (e.Key == OpenTK.Input.Key.F1 && e.Shift) { SaveCamera(0); } if (e.Key == OpenTK.Input.Key.F2 && e.Shift) { SaveCamera(1); } if (e.Key == OpenTK.Input.Key.F3 && e.Shift) { SaveCamera(2); } if (e.Key == OpenTK.Input.Key.F4 && e.Shift) { SaveCamera(3); } if (e.Key == OpenTK.Input.Key.F5 && e.Shift) { SaveCamera(4); } if (e.Key == OpenTK.Input.Key.F6 && e.Shift) { SaveCamera(5); } if (e.Key == OpenTK.Input.Key.F7 && e.Shift) { SaveCamera(6); } if (e.Key == OpenTK.Input.Key.Tab) { Game.DisplayAdapter.IsCursorVisible = !Game.DisplayAdapter.IsCursorVisible; FreeCam.Freeze = Game.DisplayAdapter.IsCursorVisible; } if (e.Key == OpenTK.Input.Key.Comma) { if (PickedMesh.GetLodLevel(0) != null) { PickedMesh.GetLodLevel(0).Material.Roughness -= 0.05f; if (PickedMesh.GetLodLevel(0).Material.Roughness < 0) { PickedMesh.GetLodLevel(0).Material.Roughness = 0; } } } if (e.Key == OpenTK.Input.Key.Period) { if (PickedMesh.GetLodLevel(0) != null) { PickedMesh.GetLodLevel(0).Material.Roughness += 0.05f; if (PickedMesh.GetLodLevel(0).Material.Roughness > 1) { PickedMesh.GetLodLevel(0).Material.Roughness = 1; } } } if (e.Key == OpenTK.Input.Key.T) { /*if(PickedMesh != null) * { * PickedMesh.GetLodLevel(0).Material.ParallaxHeightMultiplier -= 0.1f; * if(PickedMesh.GetLodLevel(0).Material.ParallaxHeightMultiplier <= 0.01f) * PickedMesh.GetLodLevel(0).Material.ParallaxHeightMultiplier = 0.01f; * }*/ } if (e.Key == OpenTK.Input.Key.Y) { if (PickedMesh.GetLodLevel(0) != null) { PickedMesh.GetLodLevel(0).Material.ParallaxHeightMultiplier += 0.1f; if (PickedMesh.GetLodLevel(0).Material.ParallaxHeightMultiplier >= 24) { PickedMesh.GetLodLevel(0).Material.ParallaxHeightMultiplier = 24; } } } if (e.Key == OpenTK.Input.Key.Pause) { ShaderProgram.RecompileAll(); ComputeShader.RecompileAll(); } if (e.Key == OpenTK.Input.Key.R) { // Game.DisplayAdapter.Pipeline.PostProcessor.UnbiasedIntegrateRenderMode = !Game.DisplayAdapter.Pipeline.PostProcessor.UnbiasedIntegrateRenderMode; } if (e.Key == OpenTK.Input.Key.LBracket) { FreeCam.Cam.Brightness -= 0.1f; } if (e.Key == OpenTK.Input.Key.RBracket) { FreeCam.Cam.Brightness += 0.1f; } if (e.Key == OpenTK.Input.Key.Number1) { RedLight[0].GetTransformationManager().SetPosition(FreeCam.Cam.Transformation.GetPosition() + new Vector3((float)rand.NextDouble() * 2 - 1, (float)rand.NextDouble() * 2 - 1, (float)rand.NextDouble() * 2 - 1) * 0.1f); RedLight[0].GetTransformationManager().SetOrientation(FreeCam.Cam.Transformation.GetOrientation()); Picked = RedLight[0]; } if (e.Key == OpenTK.Input.Key.Tilde) { // Interpolator.Interpolate<Vector3>(RedLight.GetTransformationManager().Position, RedLight.GetTransformationManager().Position.R, FreeCam.Cam.GetPosition(), 8.0f, Interpolator.Easing.EaseInOut); } var settings = Game.DisplayAdapter.MainRenderer.GraphicsSettings; if (e.Key == OpenTK.Input.Key.Number0) { settings.UseVDAO = !settings.UseVDAO; } if (e.Key == OpenTK.Input.Key.Number9) { settings.UseBloom = !settings.UseBloom; } if (e.Key == OpenTK.Input.Key.Number8) { settings.UseDeferred = !settings.UseDeferred; } if (e.Key == OpenTK.Input.Key.Number7) { settings.UseDepth = !settings.UseDepth; } if (e.Key == OpenTK.Input.Key.Number6) { settings.UseFog = !settings.UseFog; } if (e.Key == OpenTK.Input.Key.Number5) { settings.UseCubeMapGI = !settings.UseCubeMapGI; } if (e.Key == OpenTK.Input.Key.Number4) { settings.UseRSM = !settings.UseRSM; } if (e.Key == OpenTK.Input.Key.Number3) { settings.UseVXGI = !settings.UseVXGI; } if (e.Key == OpenTK.Input.Key.Number2) { settings.UseHBAO = !settings.UseHBAO; } }; }
private void LoadFromString(string[] lines) { Materials = new List <GenericMaterial>(); Meshes = new List <Mesh3d>(); Lights = new List <Light>(); Cameras = new List <Camera>(); var regx = new Regex("(.+?)[ ]+(.+)"); var currentMaterial = new GenericMaterial(Vector3.One); Light tempLight = null; GenericMaterial tempMaterial = null; Mesh3d tempMesh = null; Camera tempCamera = null; Action flush = () => { if (tempLight != null) { Lights.Add(tempLight); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempLight); } } if (tempMaterial != null) { Materials.Add(tempMaterial); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempMaterial); } } if (tempMesh != null) { Meshes.Add(tempMesh); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempMesh); } } if (tempCamera != null) { Cameras.Add(tempCamera); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempCamera); } } tempLight = null; tempMaterial = null; tempMesh = null; tempCamera = null; }; string vertexbuffer = ""; PhysicalBody physob = null; foreach (var l in lines) { if (l.StartsWith("//") || l.Trim().Length == 0) { continue; } var regout = regx.Match(l); if (!regout.Success) { throw new ArgumentException("Invalid line in scene string: " + l); } string command = regout.Groups[1].Value.Trim(); string data = regout.Groups[2].Value.Trim(); if (command.Length == 0 || data.Length == 0) { throw new ArgumentException("Invalid line in scene string: " + l); } switch (command) { // Mesh3d start case "mesh": { flush(); tempMesh = Mesh3d.Empty; tempMesh.AddInstance(new Mesh3dInstance(new TransformationManager(Vector3.Zero), data)); //tempMesh.Name = data; break; } case "usematerial": { tempMesh.AddLodLevel(null, Materials.First((a) => a.Name == data), 0, 999999); break; } case "scaleuv": { string[] literals = data.Split(' '); float x, y; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMesh.GetLodLevel(0).Info3d.Manager.ScaleUV(x, y); var obj = tempMesh.GetLodLevel(0).Info3d.Manager; tempMesh.GetLodLevel(0).SetInfo3d(new Object3dInfo(tempMesh.GetLodLevel(0).Info3d.Manager.Vertices)); tempMesh.GetLodLevel(0).Info3d.Manager = obj; break; } case "vbo": { vertexbuffer = data; ApplyVboIbo(tempMesh, vertexbuffer); vertexbuffer = ""; break; } case "physics": { var datas = data.Split(' '); if (datas[0] == "convexhull") { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } var shape = tempMesh.GetLodLevel(0).Info3d.Manager.GetConvexHull(); float mass; if (!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass)) { throw new ArgumentException("Invalid line in scene string: " + l); } physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape); } if (datas[0] == "boundingbox") { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } var shape = Physics.CreateBoundingBoxShape(tempMesh.GetLodLevel(0).Info3d.Manager); float mass; if (!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass)) { throw new ArgumentException("Invalid line in scene string: " + l); } physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape); } if (datas[0] == "accurate") { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } var shape = tempMesh.GetLodLevel(0).Info3d.Manager.GetAccurateCollisionShape(); float mass; if (!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass)) { throw new ArgumentException("Invalid line in scene string: " + l); } physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape); } if (datas[0] == "enable") { if (physob == null) { throw new ArgumentException("Invalid line in scene string: " + l); } physob.Enable(); } break; } case "translate": { string[] literals = data.Split(' '); if (literals.Length != 3) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (tempMesh != null) { tempMesh.GetInstance(0).Transformation.Translate(x, y, z); } if (tempCamera != null) { tempCamera.Transformation.Translate(x, y, z); } if (tempLight != null) { tempLight.Transformation.Translate(x, y, z); } break; } case "scale": { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } string[] literals = data.Split(' '); if (literals.Length != 3) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMesh.GetInstance(0).Transformation.Scale(x, y, z); break; } case "rotate": { string[] literals = data.Split(' '); if (literals.Length < 3 || literals.Length > 4) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } Quaternion rot = Quaternion.Identity; if (literals.Length == 3) { var rotx = Matrix3.CreateRotationX(MathHelper.DegreesToRadians(x)); var roty = Matrix3.CreateRotationY(MathHelper.DegreesToRadians(y)); var rotz = Matrix3.CreateRotationZ(MathHelper.DegreesToRadians(z)); rot = Quaternion.FromMatrix(rotx * roty * rotz); } if (literals.Length == 4) { float w; if (!float.TryParse(literals[3], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out w)) { throw new ArgumentException("Invalid line in scene string: " + l); } rot = new Quaternion(x, y, z, w); } if (tempMesh != null) { tempMesh.GetInstance(0).Transformation.Rotate(rot); } if (tempCamera != null) { tempCamera.Transformation.Rotate(rot); } if (tempLight != null) { tempLight.Transformation.Rotate(rot); } break; } // Mesh3d end Material start case "material": { flush(); tempMaterial = new GenericMaterial(Vector3.One); tempMaterial.Name = data; break; } case "type": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.Type = (GenericMaterial.MaterialType)Enum.Parse(typeof(GenericMaterial.MaterialType), data, true); break; } case "invertnormalmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } // tempMaterial.InvertNormalMap = data == "true" ? true : false; break; } case "transparency": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } //tempMaterial.SupportTransparency = data == "true" ? true : false; break; } case "normalmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.NormalsTexture = new Texture(Media.Get(data)); break; } case "bumpmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.BumpTexture = new Texture(Media.Get(data)); break; } case "roughnessmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.RoughnessTexture = new Texture(Media.Get(data)); break; } case "metalnessmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } // tempMaterial.MetalnessMap = new Texture(Media.Get(data)); break; } case "roughness": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } float f; if (!float.TryParse(data, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.Roughness = f; break; } case "metalness": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } float f; if (!float.TryParse(data, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f)) { throw new ArgumentException("Invalid line in scene string: " + l); } //tempMaterial.Metalness = f; break; } case "color": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } string[] literals = data.Split(' '); if (literals.Length != 3) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.DiffuseColor = new Vector3(x, y, z); break; } case "texture": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.DiffuseTexture = new Texture(Media.Get(data)); tempMaterial.SpecularTexture = tempMaterial.DiffuseTexture; //tempMaterial.Mode = GenericMaterial.DrawMode.TextureMultipleColor; break; } // Material end default: throw new ArgumentException("Invalid line in scene string: " + l); } } flush(); }
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; }