static void GetMaterial(string modelPath, FBXModelPtr model, ref List <Material> materials) { if (model.GetMaterialCount() > 0) { for (int i = 0; i < model.GetMaterialCount(); ++i) { FBXMaterialPtr mat = model.GetMaterial(i); if (!mat.IsNull()) { Material material = new Material(Shader.Find("Standard")); material.name = mat.GetName(); if (mat.Exist("Diffuse")) { material.SetColor("_Color", FBXVector3ToUnityColor(mat.GetVector3("Diffuse"))); } if (mat.Exist("DiffuseColor")) { material.name = Path.GetFileNameWithoutExtension(mat.GetString("DiffuseColor")); material.mainTexture = TryLoadTexture(modelPath, Path.GetFileName(mat.GetString("DiffuseColor")), TextureFormat.RGBA32, true); } if (mat.Exist("NormalMap")) { material.SetTexture("_BumpMap", TryLoadTexture(modelPath, Path.GetFileName(mat.GetString("NormalMap")), TextureFormat.RGBA32, true)); } materials.Add(material); } } } }
static Mesh GetMesh(FBXModelPtr model, float scaleFactor, Vector3 rotationOffset) { if (model.GetVertexCount() <= 0) { return(null); } // vertice List <Vector3> vertice = new List <Vector3>(); for (int i = 0; i < model.GetVertexCount(); ++i) { vertice.Add((FBXVector3ToUnityVector3(model.GetVertex(i)) - FBXVector3ToUnityVector3(model.GetPivotScale())) * scaleFactor); } // color List <Color> colors = new List <Color>(); for (int i = 0; i < model.GetColorCount(); ++i) { colors.Add(FBXColorToUnityColor(model.GetColor(i))); } // uv List <List <Vector2> > uvs = new List <List <Vector2> >(); for (int iUVLayer = 0; iUVLayer < model.GetUVLayerCount(); ++iUVLayer) { List <Vector2> uv = new List <Vector2>(); for (int iUV = 0; iUV < model.GetUVCount(iUVLayer); ++iUV) { uv.Add(FBXVector2ToUnityVector2(model.GetUV(iUVLayer, iUV))); } uvs.Add(uv); } // normal List <Vector3> normals = new List <Vector3>(); for (int i = 0; i < model.GetNormalCount(); ++i) { normals.Add(FBXVector3ToUnityVector3(model.GetNormal(i))); } // tangent List <Vector4> tangents = new List <Vector4>(); for (int i = 0; i < model.GetTangentCount(); ++i) { tangents.Add(FBXVector4ToUnityVector4(model.GetTangent(i))); } // indice, flip indice //Debug.LogFormat( "model has {0} submesh", model.GetMaterialCount() ); for (int i = 0; i < model.GetMaterialCount(); ++i) { int materialPolygonCount = model.GetMaterialPolygonCount(i); //Debug.LogFormat( "material {0} has {1} polygons", i, materialPolygonCount ); } List <List <int> > indice = new List <List <int> >(); for (int iMaterial = 0; iMaterial < model.GetMaterialCount(); ++iMaterial) { List <int> submesh = new List <int>(); for (int i = 0; i < model.GetIndiceCount(iMaterial); i += 3) { submesh.Add(model.GetIndex(iMaterial, i)); submesh.Add(model.GetIndex(iMaterial, i + 2)); submesh.Add(model.GetIndex(iMaterial, i + 1)); } indice.Add(submesh); } Mesh mesh = new Mesh(); mesh.SetVertices(vertice); //Debug.LogFormat( "Mesh {0} has {1} vertice", model.GetName(), vertice.Count ); if (colors.Count > 0) { mesh.SetColors(colors); //Debug.LogFormat( "Mesh {0} has {1} colors", model.GetName(), colors.Count ); } //else //{ // Debug.LogWarningFormat( "Mesh {0} has no colors", model.GetName() ); //} if (uvs.Count > 0) { for (int i = 0; i < uvs.Count; ++i) { mesh.SetUVs(i, uvs[i]); //Debug.LogFormat( "Mesh {0} layer {1} has {2} uv", model.GetName(), i, uvs[i].Count ); } } //else //{ // Debug.LogWarningFormat( "Mesh {0} has no uvs", model.GetName() ); //} if (normals.Count > 0) { mesh.SetNormals(normals); //Debug.LogFormat( "Mesh {0} has {1} normals", model.GetName(), normals.Count ); } //else //{ // Debug.LogWarningFormat( "Mesh {0} has no normals", model.GetName() ); //} if (tangents.Count > 0) { mesh.SetTangents(tangents); // Debug.LogFormat( "Mesh {0} has {1} tangents", model.GetName(), tangents.Count ); } //else //{ // Debug.LogWarningFormat( "Mesh {0} has no tangents", model.GetName() ); //} mesh.subMeshCount = indice.Count; for (int i = 0; i < indice.Count; ++i) { mesh.SetIndices(indice[i].ToArray(), MeshTopology.Triangles, i); //Debug.LogFormat( "submesh {0} has {1} indice", i, indice[i].Count ); //for( int j = 0; j < indice[i].Count; ++j ) //{ // Debug.LogFormat( "submesh indice[{0}] = {1}", i, indice[i][j] ); //} } // mesh.RecalculateNormals(); mesh.Optimize(); return(mesh); }