public void ImportOBJFromNetwork() { // This function downloads and imports an obj file named onion.obj from the url below // This also loads the associated textures and materials given by the absolute URLs GameObject importedObject; isImportingFromNetwork = true; OBJImportOptions importOptions = new OBJImportOptions(); importOptions.zUp = false; importOptions.localPosition = new Vector3(0.87815f, 1.4417f, -4.4708f); importOptions.localScale = new Vector3(0.0042f, 0.0042f, 0.0042f); string objURL = "https://dl.dropbox.com/s/v09bh0hiivja10e/onion.obj?dl=1"; string objName = "onion"; string diffuseTexURL = "https://dl.dropbox.com/s/0u4ij6sddi7a3gc/onion.jpg?dl=1"; string bumpTexURL = ""; string specularTexURL = ""; string opacityTexURL = ""; string materialURL = "https://dl.dropbox.com/s/fuzryqigs4gxwvv/onion.mtl?dl=1"; progressSlider.value = 0; uninteractivePanel.SetActive(true); downloadProgress = new ReferencedNumeric <float>(0); StartCoroutine(UpdateProgress()); PolyfewRuntime.ImportOBJFromNetwork(objURL, objName, diffuseTexURL, bumpTexURL, specularTexURL, opacityTexURL, materialURL, downloadProgress, (GameObject imp) => { AssignMeshesFromPairs(); isImportingFromNetwork = false; importedObject = imp; barabarianRef.SetActive(false); targetObject = importedObject; ResetSettings(); objectMeshPairs = PolyfewRuntime.GetObjectMeshPairs(targetObject, true); trianglesCount.text = PolyfewRuntime.CountTriangles(true, targetObject) + ""; exportButton.interactable = true; uninteractivePanel.SetActive(false); importFromWeb.interactable = false; importFromFileSystem.interactable = false; preserveFace.interactable = false; preservationStrength.interactable = false; disableTemporary = true; preservationSphere.gameObject.SetActive(false); disableTemporary = false; }, (Exception ex) => { uninteractivePanel.SetActive(false); isImportingFromNetwork = false; Debug.LogError("Failed to download and import OBJ file. " + ex.Message); }, importOptions); }
public async void ImportFromNetworkWebGL(string objURL, string objName, string diffuseTexURL, string bumpTexURL, string specularTexURL, string opacityTexURL, string materialURL, ReferencedNumeric <float> downloadProgress, Action <GameObject> OnSuccess, Action <Exception> OnError, OBJImportOptions importOptions = null) { if (String.IsNullOrWhiteSpace(objURL)) { OnError(new InvalidOperationException("Cannot download from empty URL. Please provide a direct URL to the obj file")); return; } if (String.IsNullOrWhiteSpace(diffuseTexURL)) { Debug.LogWarning("Cannot download from empty URL. Please provide a direct URL to the accompanying texture file."); } if (String.IsNullOrWhiteSpace(materialURL)) { Debug.LogWarning("Cannot download from empty URL. Please provide a direct URL to the accompanying material file."); } if (downloadProgress == null) { OnError(new ArgumentNullException("downloadProgress", "You must pass a reference to the Download Progress object.")); return; } GameObject objectToPopulate = new GameObject(); objectToPopulate.AddComponent <ObjectImporter>(); ObjectImporter objImporter = objectToPopulate.GetComponent <ObjectImporter>(); if (importOptions == null) { importOptions = new OBJImportOptions(); } objImporter.ImportModelFromNetworkWebGL(objURL, objName, diffuseTexURL, bumpTexURL, specularTexURL, opacityTexURL, materialURL, downloadProgress, importOptions, (GameObject imported) => { Destroy(objImporter); OnSuccess(imported); }, (exception) => { DestroyImmediate(objectToPopulate); OnError(exception); }); }
public void ImportOBJ() { // This function loads an abj file named Meat.obj from the project's asset directory // This also loads the associated textures and materials GameObject importedObject; OBJImportOptions importOptions = new OBJImportOptions(); importOptions.zUp = false; importOptions.localPosition = new Vector3(-2.199f, -1, -1.7349f); importOptions.localScale = new Vector3(0.045f, 0.045f, 0.045f); string objPath = Application.dataPath + "/PolyFew/demo/TestModels/Meat.obj"; string texturesFolderPath = Application.dataPath + "/PolyFew/demo/TestModels/textures"; string materialsFolderPath = Application.dataPath + "/PolyFew/demo/TestModels/materials"; PolyfewRuntime.ImportOBJFromFileSystem(objPath, texturesFolderPath, materialsFolderPath, (GameObject imp) => { importedObject = imp; Debug.Log("Successfully imported GameObject: " + importedObject.name); barabarianRef.SetActive(false); targetObject = importedObject; ResetSettings(); objectMeshPairs = PolyfewRuntime.GetObjectMeshPairs(targetObject, true); trianglesCount.text = PolyfewRuntime.CountTriangles(true, targetObject) + ""; exportButton.interactable = true; importFromWeb.interactable = false; importFromFileSystem.interactable = false; preserveFace.interactable = false; preservationStrength.interactable = false; disableTemporary = true; preservationSphere.gameObject.SetActive(false); disableTemporary = false; }, (Exception ex) => { Debug.LogError("Failed to load OBJ file. " + ex.ToString()); }, importOptions); }
public async Task ImportFromLocalFileSystem(string objPath, string texturesFolderPath, string materialsFolderPath, Action <GameObject> Callback, OBJImportOptions importOptions = null) { if (Application.platform == RuntimePlatform.WebGLPlayer) { Debug.LogWarning("The function cannot run on WebGL player. As web apps cannot read from or write to local file system."); return; } if (!String.IsNullOrWhiteSpace(objPath)) { objPath = Path.GetFullPath(objPath); if (objPath[objPath.Length - 1] == '\\') { objPath = objPath.Remove(objPath.Length - 1); } else if (objPath[objPath.Length - 1] == '/') { objPath = objPath.Remove(objPath.Length - 1); } } if (!String.IsNullOrWhiteSpace(texturesFolderPath)) { texturesFolderPath = Path.GetFullPath(texturesFolderPath); if (texturesFolderPath[texturesFolderPath.Length - 1] == '\\') { texturesFolderPath = texturesFolderPath.Remove(texturesFolderPath.Length - 1); } else if (texturesFolderPath[texturesFolderPath.Length - 1] == '/') { texturesFolderPath = texturesFolderPath.Remove(texturesFolderPath.Length - 1); } } if (!String.IsNullOrWhiteSpace(materialsFolderPath)) { materialsFolderPath = Path.GetFullPath(materialsFolderPath); if (materialsFolderPath[materialsFolderPath.Length - 1] == '\\') { materialsFolderPath = materialsFolderPath.Remove(materialsFolderPath.Length - 1); } else if (materialsFolderPath[materialsFolderPath.Length - 1] == '/') { materialsFolderPath = materialsFolderPath.Remove(materialsFolderPath.Length - 1); } } if (!System.IO.File.Exists(objPath)) { throw new FileNotFoundException("The path provided doesn't point to a file. The path might be invalid or the file is non-existant."); } if (!string.IsNullOrWhiteSpace(texturesFolderPath) && !System.IO.Directory.Exists(texturesFolderPath)) { Debug.LogWarning("The directory pointed to by the given path for textures is non-existant."); } if (!string.IsNullOrWhiteSpace(materialsFolderPath) && !System.IO.Directory.Exists(materialsFolderPath)) { Debug.LogWarning("The directory pointed to by the given path for materials is non-existant."); } string fileNameWithExt = System.IO.Path.GetFileName(objPath); string dirPath = System.IO.Path.GetDirectoryName(objPath); string objName = fileNameWithExt.Split('.')[0]; #pragma warning disable bool didFail = false; GameObject objectToPopulate = new GameObject(); objectToPopulate.AddComponent <ObjectImporter>(); ObjectImporter objImporter = objectToPopulate.GetComponent <ObjectImporter>(); if (dirPath.Contains("/") && !dirPath.EndsWith("/")) { dirPath += "/"; } else if (!dirPath.EndsWith("\\")) { dirPath += "\\"; } var split = fileNameWithExt.Split('.'); if (split[1].ToLower() != "obj") { DestroyImmediate(objectToPopulate); throw new System.InvalidOperationException("The path provided must point to a wavefront obj file."); } if (importOptions == null) { importOptions = new OBJImportOptions(); } try { GameObject toReturn = await objImporter.ImportModelAsync(objName, objPath, null, importOptions, texturesFolderPath, materialsFolderPath); Destroy(objImporter); Callback(toReturn); } catch (Exception ex) { DestroyImmediate(objectToPopulate); throw ex; } }