/* ------------------------------------------------------------------------------------- */ /* ------------------------------- Downloading files ---------------------------------- */ private IEnumerator DownloadAndImportFile(string url, Quaternion rotate, Vector3 scale, Vector3 translate) { fileContentString = null; if (targetObject) { Destroy(targetObject); targetObject = null; } ResetCameraPosition(); modelInfo = ""; yield return(StartCoroutine(DownloadFile(url, fileContents => fileContentString = fileContents))); if (fileContentString != null && fileContentString.Length > 0) { targetObject = ColladaImporter.Import(fileContentString, rotate, scale, translate, importEmptyNodes); yield return(StartCoroutine(DownloadTextures(targetObject, url))); // place the bottom on the floor overallBounds = GetBounds(targetObject); targetObject.transform.position = new Vector3(0, overallBounds.min.y * -1f, 0); overallBounds = GetBounds(targetObject); modelInfo = GetModelInfo(targetObject, overallBounds); ResetCameraPosition(); } }
public static Root LoadModel(string inputPath, ExportFormat format) { switch (format) { case ExportFormat.GR2: { using (var fs = new FileStream(inputPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var root = new Root(); var gr2 = new GR2Reader(fs); gr2.Read(root); root.PostLoad(); return(root); } } case ExportFormat.DAE: { var importer = new ColladaImporter(); Root root = importer.Import(inputPath); return(root); } default: throw new ArgumentException("Invalid model format"); } }
private Root LoadDAE(string inPath) { var importer = new ColladaImporter(); importer.Options = Options; return(importer.Import(inPath)); }
static void Main() { if (!SharpDevice.IsDirectX11Supported()) { System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported"); return; } //render form RenderForm form = new RenderForm(); form.Text = "Tutorial 18: Skin Animation"; SharpFPS fpsCounter = new SharpFPS(); //number of cube int count = 1000; using (SharpDevice device = new SharpDevice(form)) { SharpBatch font = new SharpBatch(device, "textfont.dds"); //Input layout for Skinning Mesh InputElement[] description = new InputElement[] { new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0), new InputElement("NORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, 12, 0), new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 24, 0), new InputElement("BINORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, 32, 0), new InputElement("TANGENT", 0, SharpDX.DXGI.Format.R32G32B32_Float, 44, 0), new InputElement("JOINT", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 56, 0), new InputElement("WEIGHT", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 72, 0), }; SharpShader staticShader = new SharpShader(device, "../../Basic.hlsl", new SharpShaderDescription() { VertexShaderFunction = "VSMain", PixelShaderFunction = "PSMain" }, description); SharpShader skinShader = new SharpShader(device, "../../BasicSkin.hlsl", new SharpShaderDescription() { VertexShaderFunction = "VSMain", PixelShaderFunction = "PSMain" }, description); Buffer11 lightBuffer = skinShader.CreateBuffer <Vector4>(); string path = @"../../../Models/Troll/"; SharpModel model = new SharpModel(device, ColladaImporter.Import(path + "troll.dae")); foreach (Geometry g in model.Geometries) { if (g.IsAnimated) { g.Shader = skinShader; } else { g.Shader = staticShader; } if (!string.IsNullOrEmpty(g.Material.DiffuseTextureName)) { g.Material.DiffuseTexture = ShaderResourceView.FromFile(device.Device, path + g.Material.DiffuseTextureName); g.Material.NormalTextureName = Path.GetFileNameWithoutExtension(g.Material.DiffuseTextureName) + "N.dds"; g.Material.NormalTexture = ShaderResourceView.FromFile(device.Device, path + g.Material.NormalTextureName); } } fpsCounter.Reset(); form.KeyDown += (sender, e) => { switch (e.KeyCode) { case Keys.Up: if (count < 1000) { count++; } break; case Keys.Down: if (count > 0) { count--; } break; } }; int lastTick = Environment.TickCount; //main loop RenderLoop.Run(form, () => { //Resizing if (device.MustResize) { device.Resize(); font.Resize(); } //apply state device.UpdateAllStates(); //clear color device.Clear(Color.CornflowerBlue); //set transformation matrix float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height; Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 10000); Matrix view = Matrix.LookAtLH(new Vector3(0, -100, 50), new Vector3(0, 0, 50), Vector3.UnitZ); Matrix world = Matrix.Identity; float angle = Environment.TickCount / 2000.0F; Vector3 light = new Vector3((float)Math.Sin(angle), (float)Math.Cos(angle), 0); light.Normalize(); device.UpdateData <Vector4>(lightBuffer, new Vector4(light, 1)); device.DeviceContext.VertexShader.SetConstantBuffer(2, lightBuffer); float animationTime = (Environment.TickCount - lastTick) / 1000.0F; if (animationTime >= model.Animations.First().Duration) { lastTick = Environment.TickCount; animationTime = 0; } model.SetTime(animationTime); model.Draw(device, new SkinShaderInformation() { Trasform = world * view * projection, World = world }); font.Begin(); //draw string fpsCounter.Update(); font.DrawString("FPS: " + fpsCounter.FPS, 0, 0, Color.White); font.DrawString("Skinning Animation With Collada", 0, 30, Color.White); //flush text to view font.End(); //present device.Present(); }); //release resource font.Dispose(); } }
protected override IEnumerator LoadCoroutine() { SerializableModel modelData = siteData as SerializableModel; GameObject pivot = new GameObject("Position Pivot"); pivot.transform.parent = this.transform; Debug.Log("Loading Model"); yield return(null); string path = modelData.file; if (!File.Exists(path)) { Debug.LogError("Failed to find file: " + path); failed = true; yield break; } string fileExtension = Path.GetExtension(path); if (fileExtension == ".dae") { string colladaString = File.ReadAllText(path); model = ColladaImporter.Import(colladaString); LoadTextures(model, colladaString); if (model == null) { Debug.LogError("Failed to load model from " + path); } yield return(null); } else if (fileExtension == ".obj" || fileExtension == ".txt") { Debug.LogWarning("LOADING OBJ"); yield return(null); Debug.LogWarning("Preprocessing Model"); yield return(StartCoroutine(PreprocessObjFile(path))); Debug.LogWarning("DONE"); Debug.LogWarning("Loading Model..."); yield return(null); List <GameObject> loadedObjects = new List <GameObject>(); yield return(StartCoroutine(LoadObjModel(path, loadedObjects))); Debug.LogWarning("DONE"); model = new GameObject(modelData.name); model.transform.parent = this.transform; pivot.transform.parent = model.transform; pivot.transform.localPosition = Vector3.zero; pivot.transform.localRotation = Quaternion.identity; foreach (GameObject obj in loadedObjects) { obj.transform.parent = pivot.transform; } CenterModel(pivot); ScaleModelToFit(); } else { Debug.LogError("Failed to load model, unsupported file extension: " + path); yield break; } if (model == null) { yield break; } model.SetActive(false); }