public static ModelResourceNode Convert(LmMsgPck msg) { var aabb = LmAABB.Unpack(msg); var boneTable = LmBoneTable.Unpack(msg); var i = 0u; var modelResourceNode = new ModelResourceNode(); foreach (var boneInfo in boneTable.BoneInfos) { modelResourceNode.MeshBoneInformation.Bones.Add(new MeshBoneInformation.Bone(boneInfo.BoneName, boneInfo.LodBoneIdx, boneInfo.UniqueBoneIdx)); // FIXME modelResourceNode.MeshBoneInformation.BoneLut.Add(boneInfo.UniqueBoneIdx, i); i++; } modelResourceNode.NodeTable = LmNodeTable.Unpack(msg); if (msg.Version >= 20141113) { var assetHash = (ulong)msg.Read(); // TODO msg.UserData.GpuBuffer = msg.UserData.DependencyTable.Get(assetHash); } var meshContainerCount = (int)msg.Read(); for (var _ = 0; _ < meshContainerCount; _++) { var meshContainer = MeshContainer.Convert(msg); modelResourceNode.MeshContainers.Add(meshContainer); } if (msg.Version >= 20140623) { var unknown = msg.ReadBool(); modelResourceNode.Name = msg.ReadString(); } if (msg.Version >= 20140722 && msg.Version < 20140929) { var hasPsdData = msg.ReadBool(); var psdDataHash = msg.ReadUint64(); } if (msg.Version < 20140815) { return(modelResourceNode); } var gmdlPartsDatasCount = msg.ReadUint(); for (var _ = 0; _ < gmdlPartsDatasCount; _++) { var gmdlPartsData = GmdlPartsData.Unpack(msg); modelResourceNode.GmdlPartsDatas.Add(gmdlPartsData); } return(modelResourceNode); }
public static void Convert(ModelResourceNode gmdl, string inputPath, string outputPath) { // Create a game object and helper objects to recreate the mesh // from the source file. Get file name without .gmdl.gfxbin // extension. GameObject mainObject = new GameObject((Path.GetFileNameWithoutExtension(inputPath)).Split('.')[0]); GameObject meshObject = new GameObject("Mesh"); meshObject.transform.parent = mainObject.transform; GameObject parts_Base = new GameObject("Parts_Base"); parts_Base.transform.parent = meshObject.transform; foreach (MeshContainer meshContainer in gmdl.MeshContainers) { // I'm pretty sure the Count variable is optimized such that // this won't damage performance, but if the script becomes // slow try storing count in a variable before entering the // loop. foreach (SQEX.Luminous.Renderer.Mesh luminMesh in meshContainer.Meshes) { GameObject gameObject = new GameObject(luminMesh.Name); gameObject.transform.parent = parts_Base.transform; UnityEngine.Mesh mesh = new UnityEngine.Mesh(); UnityEngine.SkinnedMeshRenderer smr = gameObject.AddComponent <SkinnedMeshRenderer>(); List <Vector3> luminVertices = new List <Vector3>(); List <Vector3> normalsList = new List <Vector3>(); Vector3[] normalsArray; List <Vector4> tangentList = new List <Vector4>(); List <int> triangles = new List <int>(); // Read the vertex information from the input file and // convert the list (vertex information) at each position // to an array of doubles List <float> positions = luminMesh.VertexElementArrays["POSITION0"] as List <float>; float[] floatPositions = (from Position in positions select Position).ToArray(); int[] indices = (from index in luminMesh.GmdlGeometry.IdxBuffer select(int) index).ToArray(); // Convert triangles to proper order for unity normals for (var j = 0; j < indices.Length; j += 3) { var a = indices[j]; indices[j] = indices[j + 2]; indices[j + 2] = a; } // Load the vertices into a vector3 list for (uint j = 0; j < luminMesh.VertexCount * 3; j += 3) { luminVertices.Add(new Vector3(floatPositions[j], floatPositions[j + 1], floatPositions[j + 2])); } foreach (int triangle in indices) { triangles.Add(triangle); } // TODO: Figure out why I am passing the mesh to these // functions. It doesn't look like I'm using them. if (luminMesh.VertexElementArrays.ContainsKey("NORMAL0")) { AddLayerNormal(luminMesh.VertexElementArrays["NORMAL0"], ref normalsList, mesh); normalsArray = normalsList.ToArray(); } if (luminMesh.VertexElementArrays.ContainsKey("TANGENT0")) { AddLayerTangent(luminMesh.VertexElementArrays["TANGENT0"], ref tangentList, normalsList.ToArray()); } // Set mesh parts mesh.vertices = luminVertices.ToArray(); mesh.normals = normalsList.ToArray(); mesh.triangles = triangles.ToArray(); mesh.tangents = tangentList.ToArray(); smr.sharedMesh = mesh; // This temp hack causes the script to only write the // first mesh which reduces the amount of time each test // takes. // break; } } }