Mesh ConvertInstanceToMesh(AMF_Permutations perm) { Quaternion q = Quaternion.AngleAxis(90f, Vector3.up); Mesh temp = new Mesh(); temp.name = perm.pName; List <Vector3> verts = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); //List<int> badIndex = new List<int>(); //int[] identity=new int[perm.vertices.Count]; List <BoneWeight> bones = new List <BoneWeight>(); //Matrix4x4 texMatrix=Matrix4x4.identity; for (int i = 0; i < perm.vertices.Count; i++) { Vector3 pos = perm.vertices[i].pos; Matrix4x4 flip = Matrix4x4.identity; flip.SetRow(0, new Vector4(-1, 0)); if (float.IsNaN(perm.mult)) { flip.SetRow(1, new Vector4(0, 0, 1)); flip.SetRow(2, new Vector4(0, -1)); } verts.Add((flip.MultiplyPoint3x4(pos)));//q* uvs.Add(perm.vertices[i].tex); //texMatrix=perm.vertices[i].tmat; if (perm.vertices[i].HasBoneWeight()) { bones.Add(perm.vertices[i].GetBoneWeight()); } } temp.SetVertices(verts); temp.SetUVs(0, uvs); temp.boneWeights = bones.ToArray(); //Triangles List <int> tris; int faceTotal = 0; temp.subMeshCount = perm.meshes.Count; for (int s = 0; s < perm.meshes.Count; s++) { AMF_Mesh sinfo = perm.meshes[s]; faceTotal += sinfo.faceCount; tris = new List <int>(); for (int f = 0; f < sinfo.faceCount; f++) { Vector3Int face = perm.faces[f + sinfo.startingFace]; tris.Add(face.x); tris.Add(face.y); tris.Add(face.z); } temp.SetTriangles(tris, s); } return(temp); }
List <Mesh> ConvertInstanceToMesh(AMF_Permutations perm, bool splitSubmeshes) { List <Mesh> meshes = new List <Mesh>(); List <Vector3> verts = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); //List<int> badIndex = new List<int>(); //int[] identity=new int[perm.vertices.Count]; List <BoneWeight> bones = new List <BoneWeight>(); //Matrix4x4 texMatrix=Matrix4x4.identity; for (int i = 0; i < perm.vertices.Count; i++) { Vector3 pos = perm.vertices[i].pos; Matrix4x4 flip = Matrix4x4.identity; flip.SetRow(0, new Vector4(-1, 0)); if (float.IsNaN(perm.mult)) { flip.SetRow(1, new Vector4(0, 0, 1)); flip.SetRow(2, new Vector4(0, -1)); } verts.Add(flip.MultiplyPoint3x4(pos)); uvs.Add(perm.vertices[i].tex); //texMatrix=perm.vertices[i].tmat; if (perm.vertices[i].HasBoneWeight()) { bones.Add(perm.vertices[i].GetBoneWeight()); } } Mesh temp; Dictionary <int, int> remap = new Dictionary <int, int>(); int[] tempfaces; List <Vector3> tempverts; List <Vector2> tempuvs; for (int s = 0; s < perm.meshes.Count; s++) { temp = new Mesh(); remap.Clear(); temp.name = perm.pName + ":" + s; meshes.Add(temp); AMF_Mesh sinfo = perm.meshes[s]; //sinfo.faceCount tempfaces = new int[sinfo.faceCount * 3]; tempverts = new List <Vector3>(); tempuvs = new List <Vector2>(); //populate vertex array and remap for (int f = 0; f < sinfo.faceCount; f++) { Vector3Int face = perm.faces[f + sinfo.startingFace]; if (!remap.ContainsKey(face.x)) { tempverts.Add(verts[face.x]); tempuvs.Add(uvs[face.x]); remap.Add(face.x, tempverts.Count - 1); } tempfaces[f / 3] = remap[face.x]; if (!remap.ContainsKey(face.y)) { tempverts.Add(verts[face.y]); tempuvs.Add(uvs[face.y]); remap.Add(face.y, tempverts.Count - 1); } tempfaces[f / 3 + 1] = remap[face.y]; if (!remap.ContainsKey(face.z)) { tempverts.Add(verts[face.z]); tempuvs.Add(uvs[face.z]); remap.Add(face.z, tempverts.Count - 1); } tempfaces[f / 3 + 2] = remap[face.z]; } temp.vertices = tempverts.ToArray(); temp.uv = tempuvs.ToArray(); temp.SetTriangles(tempfaces, 0); } return(meshes); }
/* * * Convert Meshes */ public Dictionary <long, Mesh> ConvertMeshes(AMF amf, Dictionary <string, Material> mats, Dictionary <string, AMFShaderInfo> matHelpers, GameObject root) { //List<Mesh> meshList = new List<Mesh>(); Dictionary <long, Mesh> meshCache = new Dictionary <long, Mesh>(); float meshComplete = 0; float totalMeshCount = 0; foreach (AMF_RegionInfo ri in amf.regionInfo) { totalMeshCount += ri.permutations.Count; } for (int ri = 0; ri < amf.regionInfo.Count; ri++) { GameObject riNode = new GameObject(amf.regionInfo[ri].name); GameObjectUtility.SetParentAndAlign(riNode, root); foreach (AMF_Permutations perm in amf.regionInfo[ri].permutations) { EditorUtility.DisplayProgressBar("Creating Meshes", perm.pName, (meshComplete / totalMeshCount)); Mesh temp; if (createDuplicateInstances || !meshCache.ContainsKey(perm.vAddress)) { temp = new Mesh(); temp.name = perm.pName; List <Vector3> verts = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <int> badIndex = new List <int>(); int[] identity = new int[perm.vertices.Count]; //matr=matr.ConvertHandedness(); Matrix4x4 texMatrix = Matrix4x4.identity; for (int i = 0; i < perm.vertices.Count; i++) { Vector3 pos = perm.vertices[i].pos; identity[i] = i; //pos=perm.matrix4x4*pos; if (pos.IsBad()) { Debug.LogErrorFormat("Invalid vertex found: [{0}]" + perm.vertices[i].pos.ToString(), i); badIndex.Add(i); verts.Add(Vector3.zero); uvs.Add(Vector2.zero); } else { if (!float.IsNaN(perm.mult)) { Matrix4x4 flip = Matrix4x4.identity; flip.SetRow(0, new Vector4(-1, 0)); verts.Add(flip.MultiplyPoint3x4(pos)); } else { //verts.Add(Matrix4x4.identity.Convert3DSMatrixToUnity().MultiplyPoint3x4(pos)); Matrix4x4 flip = Matrix4x4.identity; flip.SetRow(0, new Vector4(-1, 0)); flip.SetRow(1, new Vector4(0, 0, 1)); flip.SetRow(2, new Vector4(0, -1)); verts.Add(flip.MultiplyPoint3x4(pos)); } uvs.Add(perm.vertices[i].tex); texMatrix = perm.vertices[i].tmat; } } temp.SetVertices(verts); temp.SetUVs(0, uvs); List <int> tris; int faceTotal = 0; temp.subMeshCount = perm.meshes.Count; /* temp.SetIndices(identity,MeshTopology.Points,0); */ // Debug.LogFormat("{0} adding submeshes",perm.pName); for (int s = 0; s < perm.meshes.Count; s++) { AMF_Mesh sinfo = perm.meshes[s]; faceTotal += sinfo.faceCount; tris = new List <int>(); // Debug.LogFormat("{0}:{1}-{2}",s,sinfo.startingFace,sinfo.faceCount); for (int f = 0; f < sinfo.faceCount; f++) { Vector3Int face = perm.faces[f + sinfo.startingFace]; if (badIndex.Contains(face.x) || badIndex.Contains(face.y) || badIndex.Contains(face.z)) { // Debug.LogWarning("Dumping face due to invalid vertex"); } else { tris.Add(face.x); tris.Add(face.y); tris.Add(face.z); } } //Debug.LogFormat("{0} Setting {1} triangles",s,tris.Count); temp.SetTriangles(tris, s); } if (perm.faces.Count != faceTotal) { Debug.LogErrorFormat("Faces mistmatch: {0}vs{1} {2}", perm.faces.Count, faceTotal, perm.pName); } //if(!float.IsNaN(perm.mult)){ temp.FlipNormals(); //} temp.RecalculateNormals(); if (GenerateLightmapUVs) { Unwrapping.GenerateSecondaryUVSet(temp); } meshCache.Add(perm.vAddress, temp); } else { temp = meshCache[perm.vAddress]; } GameObject meshNode = new GameObject(perm.pName); Matrix4x4 matr = Matrix4x4.identity; if (!float.IsNaN(perm.mult)) { Matrix4x4 scalerM = new Matrix4x4(); scalerM.SetRow(0, new Vector4(100f * importScale, 0)); scalerM.SetRow(1, new Vector4(0, 100f * importScale)); scalerM.SetRow(2, new Vector4(0, 0, 100f * importScale)); scalerM.SetRow(3, new Vector4(0, 0, 0, 1)); matr.SetRow(0, new Vector4(perm.mult, 0)); matr.SetRow(1, new Vector4(0, perm.mult)); matr.SetRow(2, new Vector4(0, 0, perm.mult)); matr.SetRow(3, new Vector4(0, 0, 0, 1)); matr *= perm.matrix4x4; matr *= scalerM; Matrix4x4 unityMatr = matr.Convert3DSMatrixToUnity(); meshNode.transform.localScale = unityMatr.ExtractScale(); meshNode.transform.localRotation = unityMatr.GetRotation(); meshNode.transform.localPosition = unityMatr.ExtractPosition(); } else { meshNode.transform.localScale = new Vector3(importScale, importScale, importScale); } //GameObjectUtility.SetParentAndAlign(meshNode,riNode); //meshNode.transform.localToWorldMatrix=matr; MeshFilter mf = meshNode.AddComponent <MeshFilter>(); mf.sharedMesh = temp; MeshRenderer mr = meshNode.AddComponent <MeshRenderer>(); meshNode.transform.parent = riNode.transform; if (GenerateMeshCollidersOnClusters && amf.regionInfo[ri].name.Equals("Clusters")) { MeshCollider mc = meshNode.AddComponent <MeshCollider>(); mc.sharedMesh = mf.sharedMesh; } Material[] materials = new Material[temp.subMeshCount]; List <AMFShaderInfo> si = new List <AMFShaderInfo>(); for (int i = 0; i < materials.Length; i++) { materials[i] = mats[amf.shaderInfos[perm.meshes[i].shaderIndex].sName]; si.Add(matHelpers[amf.shaderInfos[perm.meshes[i].shaderIndex].sName]); //si[i].SaveData(amf.shaderInfos[perm.meshes[i].shaderIndex]); } mr.sharedMaterials = materials; AMFMaterialHelper mh = meshNode.AddComponent <AMFMaterialHelper>(); mh.shaderSettings = si; //mh.SaveData(amf.shaderInfos[perm.meshes[0].shaderIndex]); //Debug.LogFormat("Transform: Pos:{0} Rot:{1} Scale:{2}",perm.matrix4x4.ExtractPosition(),perm.matrix4x4.ExtractRotation(),perm.matrix4x4.ExtractScale()); meshComplete++; } } return(meshCache); }