private static Ai.Material ConvertAiMaterialFromMaterial(Material material, TextureSet textures) { var aiMaterial = new Ai.Material { Name = material.Name, ColorDiffuse = material.DiffuseColor.ToAssimp(), ColorAmbient = material.AmbientColor.ToAssimp(), ColorSpecular = material.SpecularColor.ToAssimp(), ColorEmissive = material.EmissionColor.ToAssimp(), Shininess = material.Shininess, ShadingMode = Ai.ShadingMode.Phong, }; ConvertMaterialTexture(material.Diffuse, Ai.TextureType.Diffuse); ConvertMaterialTexture(material.Ambient, Ai.TextureType.Ambient); ConvertMaterialTexture(material.Normal, Ai.TextureType.Normals); ConvertMaterialTexture(material.Specular, Ai.TextureType.Specular); ConvertMaterialTexture(material.Reflection, Ai.TextureType.Reflection); ConvertMaterialTexture(material.SpecularPower, Ai.TextureType.Shininess); void ConvertMaterialTexture(MaterialTexture materialTexture, Ai.TextureType textureType) { if (materialTexture.IsActive) { var texture = ConvertTextureSlotFromTextureId(materialTexture.TextureId, textureType, textures); aiMaterial.AddMaterialTexture(ref texture); } } return(aiMaterial); }
public void ExportMesh(GameObject go, string path) { Vector3 lastPos = go.transform.position; go.transform.position = Vector3.zero; Scene scene = new Scene(); List <string> sameNames = new List <string>(); List <Transform> children = new List <Transform>(); Dictionary <Transform, Node> nodesByTransform = new Dictionary <Transform, Node>(); GetAllChildren(children, go.transform); foreach (Transform child in children) { string nname = child.name; if (sameNames.Contains(nname)) { nname += "_"; } else { sameNames.Add(nname); } Node node = new Node(nname); UnityEngine.Matrix4x4 m = child.localToWorldMatrix.inverse; node.Transform = new Assimp.Matrix4x4( m.m00, m.m01, m.m02, m.m03, m.m10, m.m11, m.m12, m.m13, m.m20, m.m21, m.m22, m.m23, m.m30, m.m31, m.m32, m.m33 ); nodesByTransform.Add(child, node); if (child == go.transform) { Node rootNode = new Node(Path.GetFileNameWithoutExtension(path)); rootNode.Children.Add(node); scene.RootNode = rootNode; } } foreach (Transform child in children) { foreach (Transform c in child) { if (child == go.transform) { scene.RootNode.Children.Add(nodesByTransform[c]); } else { nodesByTransform[child].Children.Add(nodesByTransform[c]); } } } sameNames.Clear(); MeshFilter[] mfs = go.GetComponentsInChildren <MeshFilter>(); SkinnedMeshRenderer[] smrs = go.GetComponentsInChildren <SkinnedMeshRenderer>(); int meshIndex = 0; foreach (var mf in mfs) { PdxShape shape = mf.GetComponent <PdxShape>(); Assimp.Material mat = new Assimp.Material(); mat.Name = shape.shader; TextureSlot diff = new TextureSlot(); diff.FilePath = shape.diffuse; diff.TextureType = TextureType.Diffuse; diff.UVIndex = 0; //mat.TextureDiffuse = diff; mat.AddMaterialTexture(ref diff); TextureSlot norm = new TextureSlot(); norm.FilePath = shape.normal; norm.TextureType = TextureType.Normals; norm.UVIndex = 0; //mat.TextureNormal = norm; mat.AddMaterialTexture(ref norm); TextureSlot spec = new TextureSlot(); spec.FilePath = shape.specular; spec.TextureType = TextureType.Specular; spec.UVIndex = 0; //mat.TextureSpecular = spec; mat.AddMaterialTexture(ref spec); scene.Materials.Add(mat); Assimp.Mesh am = null; am = FromUnityMesh(mf.mesh, "pShape" + meshIndex, go.transform); if (sameNames.Contains(am.Name)) { am.Name += "_"; } else { sameNames.Add(am.Name); } am.MaterialIndex = meshIndex; scene.Meshes.Add(am); nodesByTransform[mf.transform].MeshIndices.Add(meshIndex); meshIndex++; } foreach (var smr in smrs) { PdxShape shape = smr.GetComponent <PdxShape>(); Assimp.Material mat = new Assimp.Material(); mat.Name = shape.shader; TextureSlot diff = new TextureSlot(); diff.FilePath = shape.diffuse; diff.TextureType = TextureType.Diffuse; diff.UVIndex = 0; //mat.TextureDiffuse = diff; mat.AddMaterialTexture(ref diff); TextureSlot norm = new TextureSlot(); norm.FilePath = shape.normal; norm.TextureType = TextureType.Normals; norm.UVIndex = 0; //mat.TextureNormal = norm; mat.AddMaterialTexture(ref norm); TextureSlot spec = new TextureSlot(); spec.FilePath = shape.specular; spec.TextureType = TextureType.Specular; spec.UVIndex = 0; //mat.TextureSpecular = spec; mat.AddMaterialTexture(ref spec); scene.Materials.Add(mat); Assimp.Mesh am = null; UnityEngine.Mesh baked = new UnityEngine.Mesh(); smr.BakeMesh(baked); am = FromUnityMesh(baked /*smr.sharedMesh*/, "pShape" + meshIndex, go.transform); if (sameNames.Contains(am.Name)) { am.Name += "_"; } else { sameNames.Add(am.Name); } am.MaterialIndex = meshIndex; scene.Meshes.Add(am); nodesByTransform[smr.transform].MeshIndices.Add(meshIndex); meshIndex++; } AssimpContext context = new AssimpContext(); bool result = context.ExportFile(scene, path, "obj", PostProcessSteps.MakeLeftHanded | PostProcessSteps.FlipWindingOrder); context.Dispose(); go.transform.position = lastPos; if (result) { EditorController.instance.Status("Object saved as " + path); } else { EditorController.instance.Status("Export failed :(", 2); } }