public FBXModelPtr(FBXModelPtr other) : this(FBXImporterUnmanagedPINVOKE.new_FBXModelPtr__SWIG_2(FBXModelPtr.getCPtr(other)), true) { if (FBXImporterUnmanagedPINVOKE.SWIGPendingException.Pending) { throw FBXImporterUnmanagedPINVOKE.SWIGPendingException.Retrieve(); } }
static GameObject CreateGameObject(string modelPath, FBXScenePtr scene) { // get unity scale factor and scale to meter for Unity float unitScaleFactor = 1.0f / scene.GetGlobalSettings().GetUnitScaleFactor(); FBXModelPtr model = scene.GetModel(); if (model == null) { return(null); } // skip scene root if only one node under root, same as Unity does if (model.GetChildCount() == 1) { model = model.GetChild(0); } GameObject ret = CreateGameObject(modelPath, model, unitScaleFactor, null, Vector3.zero); if (ret != null) { ret.gameObject.name = Path.GetFileNameWithoutExtension(modelPath); ret.gameObject.transform.localPosition /= unitScaleFactor; ret.gameObject.transform.localScale /= unitScaleFactor; } return(ret); }
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); } } } }
public void SetModel(FBXModelPtr value) { FBXImporterUnmanagedPINVOKE.FBXScenePtr_SetModel(swigCPtr, FBXModelPtr.getCPtr(value)); if (FBXImporterUnmanagedPINVOKE.SWIGPendingException.Pending) { throw FBXImporterUnmanagedPINVOKE.SWIGPendingException.Retrieve(); } }
public void AddChild(FBXModelPtr child) { FBXImporterUnmanagedPINVOKE.FBXModelPtr_AddChild(swigCPtr, FBXModelPtr.getCPtr(child)); if (FBXImporterUnmanagedPINVOKE.SWIGPendingException.Pending) { throw FBXImporterUnmanagedPINVOKE.SWIGPendingException.Retrieve(); } }
public FBXModelPtr GetChild(int index) { FBXModelPtr ret = new FBXModelPtr(FBXImporterUnmanagedPINVOKE.FBXModel_GetChild(swigCPtr, index), true); return ret; }
public static FBXModelPtr Create() { FBXModelPtr ret = new FBXModelPtr(FBXImporterUnmanagedPINVOKE.FBXModel_Create(), true); return ret; }
public FBXModelPtr GetModel() { FBXModelPtr ret = new FBXModelPtr(FBXImporterUnmanagedPINVOKE.FBXScenePtr_GetModel(swigCPtr), true); return(ret); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FBXModelPtr obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public FBXModelPtr Create() { FBXModelPtr ret = new FBXModelPtr(FBXImporterUnmanagedPINVOKE.FBXModelPtr_Create(swigCPtr), true); return(ret); }
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); }
static void GetPivotTransform(FBXModelPtr model, out Vector3 position, out Vector3 rotation, out Vector3 scale) { position = FBXVector3ToUnityVector3(model.GetPivotRotation()) + FBXVector3ToUnityVector3(model.GetRotationOffset()); rotation = FBXVector3ToUnityVector3(model.GetPivotRotation()); scale = FBXVector3ToUnityVector3(model.GetPivotScale()); }
static void GetTrasform(FBXModelPtr model, out Vector3 position, out Quaternion rotation, out Vector3 scale) { position = FBXVector3ToUnityVector3(model.GetTranslation()); rotation = FBXVector3ToUnityQuaternion(model.GetRotation()); scale = FBXVector3ToUnityVector3Abs(model.GetScale()); }
static GameObject CreateGameObject(string modelPath, FBXModelPtr model, float unitScaleFactor, Transform parent, Vector3 parentScalePivot) { GameObject obj = new GameObject(); // GameObject obj = GameObject.Instantiate<GameObject>(); if (obj == null) { return(null); } // set parent obj.transform.SetParent(parent, false); // set name obj.name = model.GetName(); // set transform Vector3 pivotP = Vector3.zero; Vector3 pivotR = Vector3.zero; Vector3 pivotS = Vector3.one; Vector3 modelP = Vector3.zero; Quaternion modelR = Quaternion.identity; Vector3 modelS = Vector3.one; GetPivotTransform(model, out pivotP, out pivotR, out pivotS); GetTrasform(model, out modelP, out modelR, out modelS); obj.transform.localPosition = ( FBXVector3ToUnityVector3(model.GetTranslation()) + FBXVector3ToUnityVector3(model.GetPivotRotation()) + FBXVector3ToUnityVector3(model.GetRotationOffset()) - parentScalePivot ) * unitScaleFactor; obj.transform.localRotation = FBXVector3ToUnityQuaternion(model.GetPreRotation()) * modelR * Quaternion.Inverse(FBXVector3ToUnityQuaternion(model.GetPostRotation())); obj.transform.localScale = modelS; // set mesh Mesh mesh = GetMesh(model, unitScaleFactor, FBXVector3ToUnityVector3(model.GetRotationOffset())); if (mesh != null) { mesh.name = model.GetName(); MeshFilter meshFilter = obj.AddComponent <MeshFilter>(); meshFilter.mesh = mesh; } // set material List <Material> materials = new List <Material>(); GetMaterial(modelPath, model, ref materials); if (materials.Count > 0 && mesh != null) { MeshRenderer meshRenderer = obj.AddComponent <MeshRenderer>(); if (materials.Count == 1) { if (meshRenderer.material != null) { GameObject.DestroyImmediate(meshRenderer.material); } meshRenderer.material = materials[0]; } else { for (int i = 0; i < materials.Count; ++i) { if (i < meshRenderer.materials.Length) { if (meshRenderer.materials[i] != null) { GameObject.DestroyImmediate(meshRenderer.materials[i]); } } meshRenderer.materials = materials.ToArray(); } } } // create childs for (int i = 0; i < model.GetChildCount(); ++i) { CreateGameObject(modelPath, model.GetChild(i), unitScaleFactor, obj.transform, FBXVector3ToUnityVector3(model.GetPivotScale())); } return(obj); }