private static void AddMeshToGameObject(Mesh[] meshes, GameObject obj) { Renderer objRenderer = obj.GetComponent <Renderer>(); Material[] mats = new Material[meshes.Length]; MaterialHandler mtlHandler = MaterialHandler.GetInstance(); for (int i = 0; i < meshes.Length; i++) { Material mat = mtlHandler.GetMaterialForMesh(meshes[i].name); if (mat == null) { Debug.LogFormat("Could not find material for mesh {0}", meshes[i].name); mats[i] = mtlHandler.GetMaterialForMesh("default"); } else { mats[i] = mat; } } objRenderer.materials = mats; Mesh main = new Mesh(); main.subMeshCount = meshes.Length; if (meshes.Length == 0) { obj.GetComponent <MeshFilter>().mesh = main; } else { CombineInstance[] combine = new CombineInstance[meshes.Length]; for (int i = 0; i < meshes.Length; i++) { combine[i].mesh = meshes[i]; combine[i].transform = Matrix4x4.identity; } main.CombineMeshes(combine); for (int i = 0; i < main.subMeshCount; i++) { int[] tri = new int[0]; tri = meshes[i].triangles; int offset = 0; if (i > 0) { for (int j = 0; j < i; j++) { offset += meshes[j].vertexCount; } } main.SetTriangles(tri, i, false, offset); //Don't ask? if (main.subMeshCount != meshes.Length) { main.subMeshCount = meshes.Length; } } obj.GetComponent <MeshFilter>().mesh = main; } }
private static GameObject AddMeshToNewGameObject(Mesh[] meshes, bool addMeshCollider = false, string name = null) { //Set up our gameobject and add a renderer and filter GameObject obj = new GameObject(); obj.AddComponent <MeshRenderer>(); obj.AddComponent <MeshFilter>(); Renderer objRenderer = obj.GetComponent <Renderer>(); Material[] mats = new Material[meshes.Length]; MaterialHandler mtlHandler = MaterialHandler.GetInstance(); for (int i = 0; i < meshes.Length; i++) { Material mat = mtlHandler.GetMaterialForMesh(meshes[i].name); if (mat == null) { Debug.LogFormat("Could not find material for mesh {0}", meshes[i].name); mats[i] = mtlHandler.GetMaterialForMesh("default"); } else { mats[i] = mat; } } objRenderer.materials = mats; Mesh main = new Mesh(); main.subMeshCount = meshes.Length; if (meshes.Length == 0) { obj.GetComponent <MeshFilter>().mesh = main; } else { CombineInstance[] combine = new CombineInstance[meshes.Length]; for (int i = 0; i < meshes.Length; i++) { combine[i].mesh = meshes[i]; combine[i].transform = Matrix4x4.identity; } main.CombineMeshes(combine); for (int i = 0; i < main.subMeshCount; i++) { int[] tri = meshes[i].triangles; int offset = 0; if (i > 0) { for (int j = 0; j < i; j++) { offset += meshes[j].vertexCount; } } main.SetTriangles(tri, i, false, offset); //Don't ask? if (main.subMeshCount != meshes.Length) { main.subMeshCount = meshes.Length; } } obj.GetComponent <MeshFilter>().mesh = main; } if (addMeshCollider) { obj.AddComponent <MeshCollider>(); } string newName = ""; //Redo this if (name != null) { newName = name; } else { newName = meshes.Length > 0 ? meshes[0].name.Substring(0, meshes[0].name.Length - 2) : "Unknown"; } obj.name = newName; obj.SetActive(false); return(obj); }
private void LoadMeshData(string fileName) { StringBuilder sb = new StringBuilder(); string text = File.ReadAllText(fileName); int start = 0; string objectName = null; bool hasColor = false; int faceDataCount = 0; StringBuilder sbFloat = new StringBuilder(); StringBuilder mtlFile = new StringBuilder(); StringBuilder mtlName = new StringBuilder(); for (int i = 0; i < text.Length; i++) { if (text[i] == '\n')// || text[i] == '#') { sb.Remove(0, sb.Length); // Start +1 for whitespace '\n' sb.Append(text, start + 1, i - start); start = i; if (sb[0] == 'o' && sb[1] == ' ') { sbFloat.Remove(0, sbFloat.Length); int j = 2; while (j < sb.Length) { objectName += sb[j]; j++; } } else if (sb[0] == 'm' && sb[1] == 't' && sb[2] == 'l' && sb[3] == 'l' && sb[4] == 'i' && sb[5] == 'b') { int splitStart = 6; sbFloat.Remove(0, sbFloat.Length); while (splitStart < sb.Length) { mtlFile.Append(sb[splitStart]); splitStart++; } } else if (sb[0] == 'u' && sb[1] == 's' && sb[2] == 'e' && sb[3] == 'm' && sb[4] == 't' && sb[5] == 'l') { int splitStart = 6; sbFloat.Remove(0, sbFloat.Length); while (splitStart < sb.Length) { mtlName.Append(sb[splitStart]); splitStart++; } } else if (sb[0] == 'v' && sb[1] == ' ') // Vertices { int splitStart = 2; vertices.Add(new Vector3(GetFloat(sb, ref splitStart, ref sbFloat), GetFloat(sb, ref splitStart, ref sbFloat), GetFloat(sb, ref splitStart, ref sbFloat))); } else if (sb[0] == 'v' && sb[1] == 't' && sb[2] == ' ') // UV { int splitStart = 3; uv.Add(new Vector4(GetFloat(sb, ref splitStart, ref sbFloat), GetFloat(sb, ref splitStart, ref sbFloat), GetFloat(sb, ref splitStart, ref sbFloat), GetFloat(sb, ref splitStart, ref sbFloat))); } else if (sb[0] == 'v' && sb[1] == 'n' && sb[2] == ' ') // Normals { int splitStart = 3; normals.Add(new Vector3(GetFloat(sb, ref splitStart, ref sbFloat), GetFloat(sb, ref splitStart, ref sbFloat), GetFloat(sb, ref splitStart, ref sbFloat))); } else if (sb[0] == 'v' && sb[1] == 'c' && sb[2] == ' ') // Vertex colors { int splitStart = 3; //Colors are written a, r, g, b byte a = Scale(GetFloat(sb, ref splitStart, ref sbFloat)); byte r = Scale(GetFloat(sb, ref splitStart, ref sbFloat)); byte g = Scale(GetFloat(sb, ref splitStart, ref sbFloat)); byte b = Scale(GetFloat(sb, ref splitStart, ref sbFloat)); if (!hasColor && r + g + b + a > 0) { hasColor = true; } colors.Add(new Color32(r, g, b, a)); } else if (sb[0] == 'f' && sb[1] == ' ') { int splitStart = 2; int j = 1; intArray.Clear(); int info = 0; // Add faceData, a face can contain multiple triangles, facedata is stored in following order vert, uv, normal. If uv or normal are / set it to a 0 while (splitStart < sb.Length && char.IsDigit(sb[splitStart])) { faceData.Add(new Vector3Int(GetInt(sb, ref splitStart, ref sbFloat), GetInt(sb, ref splitStart, ref sbFloat), GetInt(sb, ref splitStart, ref sbFloat))); j++; intArray.Add(faceDataCount); faceDataCount++; } info += j; j = 1; while (j + 2 < info) //Create triangles out of the face data. There will generally be more than 1 triangle per face. { triangles.Add(intArray[0]); triangles.Add(intArray[j]); triangles.Add(intArray[j + 1]); j++; } } } } MaterialHandler mtlHandler = MaterialHandler.GetInstance(); mtlHandler.LoadMaterial(Path.Combine(fileName.Substring(0, fileName.LastIndexOf(@"\")), mtlFile.ToString().Trim())); mtlHandler.RegisterMaterialForMesh(meshName, mtlName.ToString().Trim()); if (hasColor) { mtlHandler.SetMaterialShader(mtlName.ToString(), "texblend"); } }