private IEnumerator DownloadAndImportAll(string url) { string objString = null; string mtlString = null; Hashtable textures = null; GameObject importedObject = null; yield return(StartCoroutine(DownloadFile(url, retval => objString = retval))); yield return(StartCoroutine(DownloadFile(url.Substring(0, url.Length - 4) + ".mtl", retval => mtlString = retval))); if (mtlString != null && mtlString.Length > 0) { string path = url; int lastSlash = path.LastIndexOf('/', path.Length - 1); if (lastSlash >= 0) { path = path.Substring(0, lastSlash + 1); } Hashtable[] mtls = ObjImporter.ImportMaterialSpecs(mtlString); for (int i = 0; i < mtls.Length; i++) { if (mtls[i].ContainsKey("mainTexName")) { Texture2D texture = null; string texUrl = path + mtls[i]["mainTexName"]; yield return(StartCoroutine(DownloadTexture(texUrl, retval => texture = retval))); if (texture != null) { if (textures == null) { textures = new Hashtable(); } textures[mtls[i]["mainTexName"]] = texture; } } } } yield return(StartCoroutine(DownloadFile(url, retval => objString = retval))); if (objString != null && objString.Length > 0) { importedObject = ObjImporter.Import(objString, mtlString, textures); } }
public static GameObject LoadObj(string objPath, string mtlPath) { //ObjImporter.shader = Shader.Find(AssetManager.instance.defaultColorShaderName);//AssetManager.instance.defaultColorShader; //if (ObjImporter.shader == null) // ObjImporter.shader = AssetManager.instance.fallbackShader; ObjImporter.material = AssetManager.instance.objMaterial; //Debug.Log("*****ERR: can't find shader " + AssetManager.instance.defaultColorShaderName); GameObject result = null; Hashtable textures = new Hashtable(); Debug.Log("Loading obj " + objPath + " mtl " + mtlPath); if (mtlPath == null || !File.Exists(mtlPath)) { Debug.Log("Material path " + mtlPath + " doesn't exist, switching to " + objPath); mtlPath = objPath.Substring(0, objPath.Length - ".obj".Length) + ".mtl"; Debug.Log("New path " + mtlPath); } string objContents = ReadFile(objPath); string matContents = ReadFile(mtlPath);//resourcePath + ".mtl"); //bool hasTexture = false; Hashtable[] mtls = null; if (matContents != null) { mtls = ObjImporter.ImportMaterialSpecs(matContents); //Debug.Log("Material count " + mtls.Length); for (int i = 0; i < mtls.Length; i++) { if (mtls[i].ContainsKey("mainTexName")) { var resDir = Path.GetDirectoryName(mtlPath); string texName = ((string)mtls[i]["mainTexName"]).Replace(' ', '_'); var texPath = Path.Combine(resDir, texName); //Debug.Log("****************************************TEX PATH " + texPath); var texture = Utils.LoadTexture(texPath); if (texture != null) { //hasTexture = true; //ObjImporter.shader = Shader.Find(AssetManager.instance.defaultTextureShaderName);//AssetManager.instance.defaultTextureShader; //if (ObjImporter.shader == null) // ObjImporter.shader = AssetManager.instance.fallbackShader; //Debug.Log("*****ERR: can't find shader " + AssetManager.instance.defaultColorShaderName); textures[mtls[i]["mainTexName"]] = texture; } } } } result = ObjImporter.Import(objContents, matContents, textures); if (result == null) { Debug.LogError("Oops. GameObject not loaded from path: " + objPath + "||" + mtlPath); //throw new Exception("GameObject could not be loaded from " + resourcePath); } //Shader shader = Shader.Find("LightweightRenderPipeline/Unlit"); //Renderer[] renderers = result.GetComponentsInChildren<Renderer>(); //foreach (Renderer r in renderers) //{ // Material[] mats = r.materials; // for (int i = 0; i < mats.Length; i++) // { // if (hasTexture) // mats[i].shader = AssetManager.instance.defaultTextureShader; // else // mats[i].shader = AssetManager.instance.defaultColorShader; // /* // //mats[i] = AssetManager.Instance.testMaterial; // //break; // if (unlit) // { // mats[i].shader = AssetManager.Instance.unlitShader; // } // else if (translucid) // { // mats[i].shader = AssetManager.Instance.translucidShader; // } // else // { // mats[i].shader = AssetManager.Instance.defaultShader; // } // if (colorTweak != Color.white) // { // //Debug.Log("SETTING TINT!!!!!!!!!--------------------------------------------------------------------- " + mats[i].name + " " + template.name); // //mats[i].color = new Color((mats[i].color.r + colorTweak.r) / 2, (mats[i].color.g + colorTweak.g) / 2, (mats[i].color.b + colorTweak.b) / 2, (mats[i].color.a + colorTweak.a) / 2); // Color originalColor = mats[i].color; // float a = colorTweak.a; // float oneMinusA = 1 - colorTweak.a; // mats[i].color = new Color(mats[i].color.r * oneMinusA + colorTweak.r * a, mats[i].color.g * oneMinusA + colorTweak.g * a, mats[i].color.b * oneMinusA + colorTweak.b * a); // //mats[i].color = new Color((mats[i].color.r + colorTweak.r * colorTweak.a), (mats[i].color.g + colorTweak.g * colorTweak.a), (mats[i].color.b + colorTweak.b * colorTweak.a));//, (mats[i].color.a + colorTweak.a)); // } // * */ // } // r.materials = mats; //} return(result); }
/* ------------------------------------------------------------------------------------- */ /* ------------------------------- Downloading files ---------------------------------- */ private IEnumerator DownloadAndImportFile(string url, Quaternion rotate, Vector3 scale, Vector3 translate, bool gameObjectPerGrp, bool subMeshPerGrp, bool usesRightHanded) { string objString = null; string mtlString = null; Hashtable textures = null; yield return(StartCoroutine(DownloadFile(url, retval => objString = retval))); yield return(StartCoroutine(DownloadFile(url.Substring(0, url.Length - 4) + ".mtl", retval => mtlString = retval))); if (mtlString != null && mtlString.Length > 0) { string path = url; int lastSlash = path.LastIndexOf('/', path.Length - 1); if (lastSlash >= 0) { path = path.Substring(0, lastSlash + 1); } Hashtable[] mtls = ObjImporter.ImportMaterialSpecs(mtlString); for (int i = 0; i < mtls.Length; i++) { if (mtls[i].ContainsKey("mainTexName")) { Texture2D texture = null; string texUrl = path + mtls[i]["mainTexName"]; yield return(StartCoroutine(DownloadTexture(texUrl, retval => texture = retval))); if (texture != null) { if (textures == null) { textures = new Hashtable(); } textures[mtls[i]["mainTexName"]] = texture; } } } } if (objString != null && objString.Length > 0) { // yield return StartCoroutine(ObjImporter.ImportInBackground(objString, mtlString, textures, rotate, scale, translate, retval => targetObject = retval, gameObjectPerGrp, subMeshPerGrp, usesRightHanded)); targetObject = ObjImporter.Import(objString, mtlString, textures, rotate, scale, translate); AddToLog("Done importing model:" + targetObject); if (targetObject != null) { if (mtlString == null || mtlString.Length <= 0) { SetDftTextureInAllMaterials(targetObject, defaultTexture); SetDftColorInAllMaterials(targetObject, defaultTexture); } // rename the object if needed if (targetObject.name == "Imported OBJ file") { string[] path = url.Split(new char[] { '/', '.' }); if (path.Length > 1) { targetObject.name = path[path.Length - 2]; } } // place the bottom on the floor overallBounds = GetBounds(targetObject); targetObject.transform.position = new Vector3(0, overallBounds.min.y * -1f, 0); overallBounds = GetBounds(targetObject); modelInfoText.text = GetModelInfo(targetObject, overallBounds); ResetCameraPosition(); } } }
private IEnumerator DownloadAndImportAllInBackground(string url, Action <float> progressCallback, Action <GameObject> result) { string objString = null; string mtlString = null; Hashtable textures = null; GameObject importedObject = null; Texture2D tex = null; progress_bar2.SetActive(true); objekt.transform.position = Vector3.zero; yield return(StartCoroutine(DownloadFile(url, retval => objString = retval))); //yield return StartCoroutine(DownloadFile(url.Substring(0, url.Length - 4) + ".mtl", retval => mtlString = retval)); yield return(StartCoroutine(DownloadFile(url + ".mtl", retval => mtlString = retval))); if (mtlString != null && mtlString.Length > 0) { string path = url; int lastSlash = path.LastIndexOf('/', path.Length - 1); if (lastSlash >= 0) { path = path.Substring(0, lastSlash + 1); } Hashtable[] mtls = ObjImporter.ImportMaterialSpecs(mtlString); for (int i = 0; i < mtls.Length; i++) { if (mtls[i].ContainsKey("mainTexName")) { Texture2D texture = null; string texUrl = path + mtls[i]["mainTexName"]; yield return(StartCoroutine(DownloadTexture(texUrl, retval => texture = retval))); if (texture != null) { if (textures == null) { textures = new Hashtable(); } textures[mtls[i]["mainTexName"]] = texture; tex = texture; } } } } //yield return StartCoroutine(DownloadFile(url, retval => objString = retval)); if (objString != null && objString.Length > 0) { yield return(StartCoroutine(ObjImporter.ImportInBackground(objString, mtlString, textures, r => importedObject = r, progressCallback))); } if (importedObject.transform.childCount == 0) { importedObject.GetComponent <Renderer>().material.shader = Shader.Find("Standard"); importedObject.GetComponent <Renderer>().material.SetTexture("_MainTex", tex); result(importedObject); importedObject.AddComponent <MeshCollider>(); importedObject.transform.localScale = new Vector3(-1, 1, 1); importedObject.transform.parent = objekt; GameObject.Find("Scene").GetComponent <UseShader>().init(); } else { var count = importedObject.transform.childCount; for (int i = 0; i < count; i++) { var obj = importedObject.transform.GetChild(0); obj.GetComponent <Renderer>().material.shader = Shader.Find("Standard"); obj.GetComponent <Renderer>().material.SetTexture("_MainTex", tex); obj.transform.localScale = new Vector3(-1, 1, 1); // Spiegelt das importierte Objekt, geändert obj.gameObject.AddComponent <MeshCollider>(); obj.gameObject.transform.parent = objekt; result(obj.gameObject); GameObject.Find("Scene").GetComponent <UseShader>().init(); } } Debug.Log("Done"); progress_bar.SetActive(false); progress_bar2.SetActive(false); progress_bar.GetComponent <Slider>().value = 0; progress_bar2.GetComponent <Slider>().value = 0; script.resetFront(); script.zentrum(); dist.setSize(); }