private BabylonNode ExportMesh(IIGameScene scene, IIGameNode meshNode, BabylonScene babylonScene) { if (IsMeshExportable(meshNode) == false) { return(null); } logger?.RaiseMessage(meshNode.Name, 1); if (!exportParameters.keepInstances) { return(ExportMasterMesh(scene, meshNode, babylonScene)); } else { // Instances #if MAX2020 || MAX2021 || MAX2022 var tabs = Loader.Global.INodeTab.Create(); #else var tabs = Loader.Global.NodeTab.Create(); #endif Loader.Global.IInstanceMgr.InstanceMgr.GetInstances(meshNode.MaxNode, tabs); if (tabs.Count > 1) { IINode Master = TabToList <IINode>(tabs)[tabs.Count - 1]; List <IINode> Instances = TabToList <IINode>(tabs).FindAll(x => x.Handle != Master.Handle); foreach (IINode instanceNode in tabs.ToIEnumerable()) { //this make sure every instance node is indexed in guid dictionary Tools.GetGuid(instanceNode); } BabylonMesh babylonMasterMesh = babylonScene.MeshesList.Find(mesh => mesh.id == Master.GetGuid().ToString()); if (babylonMasterMesh == null) { return(ExportMasterMesh(scene, meshNode, babylonScene)); } else { return(ExportInstanceMesh(scene, meshNode, babylonScene, babylonMasterMesh)); } } return(ExportMasterMesh(scene, meshNode, babylonScene)); } }
private BabylonNode ExportMesh(IIGameScene scene, IIGameNode meshNode, BabylonScene babylonScene) { if (IsMeshExportable(meshNode) == false) { return(null); } RaiseMessage(meshNode.Name, 1); // Instances #if MAX2020 var tabs = Loader.Global.INodeTab.Create(); #else var tabs = Loader.Global.NodeTab.Create(); #endif Loader.Global.IInstanceMgr.InstanceMgr.GetInstances(meshNode.MaxNode, tabs); if (tabs.Count > 1) { foreach (IINode instanceNode in Tools.ITabToIEnumerable(tabs)) { //this make sure every instance node is indexed in guid dictionary Tools.GetGuid(instanceNode); } // For a mesh with instances, we distinguish between master and instance meshes: // - a master mesh stores all the info of the mesh (transform, hierarchy, animations + vertices, indices, materials, bones...) // - an instance mesh only stores the info of the node (transform, hierarchy, animations) // Check if this mesh has already been exported List <BabylonMesh> babylonMasterMeshes = new List <BabylonMesh>(); var index = 0; while (index < tabs.Count) { #if MAX2017 || MAX2018 || MAX2019 || MAX2020 var tab = tabs[index]; #else var tab = tabs[new IntPtr(index)]; #endif Guid nodeGuid = Tools.guids.FirstOrDefault(x => x.Value == tab).Key; babylonMasterMeshes.AddRange(babylonScene.MeshesList.FindAll(_babylonMesh => { // Same id return(_babylonMesh.id == nodeGuid.ToString() && // Mesh is not a dummy _babylonMesh.isDummy == false); })); index++; } if (babylonMasterMeshes.Count > 0) { // Mesh already exported // Export this node as instance // Check if we need to export this instance as an instance mesh. // If there is already an exported mesh in the scene that shares this mesh's material, then export it as an instance. BabylonMesh babylonMasterMesh = null; foreach (var mesh in babylonMasterMeshes) { if (mesh.materialId == null || (meshNode.NodeMaterial != null && meshNode.NodeMaterial.MaxMaterial.GetGuid().ToString().Equals(mesh.materialId))) { babylonMasterMesh = mesh; } } if (babylonMasterMesh != null) { return(ExportInstanceMesh(scene, meshNode, babylonScene, babylonMasterMesh)); } return(ExportMasterMesh(scene, meshNode, babylonScene)); } } return(ExportMasterMesh(scene, meshNode, babylonScene)); }