public static void ImportCharMesh(string meshPath, string zmsPath) { if (!File.Exists(zmsPath)) { Debug.LogWarning("Failed to find referenced ZMS."); return; } var mesh = new Mesh(); var zms = new Revise.Files.ZMS.ModelFile(); zms.Load(zmsPath); var verts = new Vector3[zms.Vertices.Count]; var uvs = new Vector2[zms.Vertices.Count]; var bones = new BoneWeight[zms.Vertices.Count]; for (int k = 0; k < zms.Vertices.Count; ++k) { var v = zms.Vertices[k]; v.TextureCoordinates[0].y = 1 - v.TextureCoordinates[0].y; verts[k] = rtuPosition(v.Position); uvs[k] = v.TextureCoordinates[0]; bones[k] = new BoneWeight(); bones[k].boneIndex0 = zms.BoneTable[v.BoneIndices.X]; bones[k].boneIndex1 = zms.BoneTable[v.BoneIndices.Y]; bones[k].boneIndex2 = zms.BoneTable[v.BoneIndices.Z]; bones[k].boneIndex3 = zms.BoneTable[v.BoneIndices.W]; bones[k].weight0 = v.BoneWeights.x; bones[k].weight1 = v.BoneWeights.y; bones[k].weight2 = v.BoneWeights.z; bones[k].weight3 = v.BoneWeights.w; } mesh.vertices = verts; mesh.uv = uvs; mesh.boneWeights = bones; int[] indices = new int[zms.Indices.Count * 3]; for (int k = 0; k < zms.Indices.Count; ++k) { indices[k * 3 + 0] = zms.Indices[k].X; indices[k * 3 + 2] = zms.Indices[k].Y; indices[k * 3 + 1] = zms.Indices[k].Z; } mesh.triangles = indices; mesh.RecalculateNormals(); AssetDatabase.CreateAsset(mesh, meshPath); }
public void ImportModels() { _meshLookup = new Mesh[_f.ModelFiles.Count]; for (int i = 0; i < _f.ModelFiles.Count; ++i) { var zmsPath = rootPath + _f.ModelFiles[i]; if (!File.Exists(zmsPath)) { Debug.LogWarning("Failed to find referenced ZMS."); continue; } var mesh = new Mesh(); var zms = new Revise.Files.ZMS.ModelFile(); zms.Load(zmsPath); var verts = new Vector3[zms.Vertices.Count]; var uvs = new Vector2[zms.Vertices.Count]; for (int k = 0; k < zms.Vertices.Count; ++k) { var v = zms.Vertices[k]; v.TextureCoordinates[0].y = 1 - v.TextureCoordinates[0].y; verts[k] = rtuPosition(v.Position); uvs[k] = v.TextureCoordinates[0]; } mesh.vertices = verts; mesh.uv = uvs; int[] indices = new int[zms.Indices.Count * 3]; for (int k = 0; k < zms.Indices.Count; ++k) { indices[k * 3 + 0] = zms.Indices[k].X; indices[k * 3 + 2] = zms.Indices[k].Y; indices[k * 3 + 1] = zms.Indices[k].Z; } mesh.triangles = indices; mesh.RecalculateNormals(); Unwrapping.GenerateSecondaryUVSet(mesh); var meshPath = _basePath + "Mesh_" + i.ToString() + ".asset"; AssetDatabase.CreateAsset(mesh, meshPath); _meshLookup[i] = mesh; } _matLookup = new Material[_f.TextureFiles.Count]; for (int i = 0; i < _f.TextureFiles.Count; ++i) { var tex = _f.TextureFiles[i]; var texPath = _basePath + "Tex_" + texLookup[i].ToString() + ".DDS"; if (!File.Exists(texPath)) { continue; } var tex2d = AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)) as Texture2D; Shader shader = null; if (tex.TwoSided) { if (tex.AlphaTestEnabled) shader = Shader.Find("Transparent/Cutout/DoubleSided"); else if (tex.AlphaEnabled) shader = Shader.Find("Transparent/DoubleSided"); else { Debug.LogWarning("Two-sided non-alpha material encountered."); } } else if (tex.AlphaTestEnabled) shader = Shader.Find("Transparent/Cutout/Diffuse"); else if (tex.AlphaEnabled) shader = Shader.Find("Transparent/Diffuse"); else shader = Shader.Find("Diffuse"); if (!shader) { Debug.LogWarning("Failed to find appropriate shader for material."); continue; } var mat = new Material(shader); if (tex.AlphaTestEnabled) mat.SetFloat("_Cutoff", (float)tex.AlphaReference / 256); mat.mainTexture = tex2d; var matPath = _basePath + "Mat_" + i.ToString() + ".mat"; AssetDatabase.CreateAsset(mat, matPath); _matLookup[i] = mat; } for (int i = 0; i < _f.Objects.Count; ++i) { var obj = _f.Objects[i]; var mdl = ScriptableObject.CreateInstance<RoseMapObjectData>(); for (int j = 0; j < obj.Parts.Count; ++j) { var part = obj.Parts[j]; var subObj = new RoseMapObjectData.SubObject(); subObj.mesh = _meshLookup[part.Model]; subObj.material = _matLookup[part.Texture]; subObj.animation = null; subObj.parent = part.Parent; subObj.position = rtuPosition(part.Position) / 100; subObj.rotation = rtuRotation(part.Rotation); subObj.scale = rtuScale(part.Scale); if (part.Collision == Revise.Files.ZSC.CollisionType.None) { subObj.colMode = 0; } else { subObj.colMode = 1; } if (part.AnimationFilePath != "") { var animPath = _basePath + "Anim_" + i.ToString() + "_" + j.ToString() + ".asset"; var clip = ImportNodeAnimation(animPath, part.AnimationFilePath); subObj.animation = clip; } mdl.subObjects.Add(subObj); } var mdlPath = _basePath + "Model_" + i.ToString() + ".asset"; AssetDatabase.CreateAsset(mdl, mdlPath); EditorUtility.SetDirty(mdl); } }
/// <summary> /// Tests the load method. /// </summary> private void TestLoadMethod(string filePath) { Stream stream = File.OpenRead(filePath); stream.Seek(0, SeekOrigin.End); long fileSize = stream.Position; stream.Seek(0, SeekOrigin.Begin); ModelFile modelFile = new ModelFile(); modelFile.Load(stream); long streamPosition = stream.Position; stream.Close(); Assert.AreEqual(fileSize, streamPosition, "Not all of the file was read"); }
/// <summary> /// Tests the save method. /// </summary> private void TestSaveMethod(string filePath) { ModelFile modelFile = new ModelFile(); modelFile.Load(filePath); MemoryStream savedStream = new MemoryStream(); modelFile.Save(savedStream); savedStream.Seek(0, SeekOrigin.Begin); ModelFile savedModelFile = new ModelFile(); savedModelFile.Load(savedStream); savedStream.Close(); Assert.AreEqual(modelFile.Pool, savedModelFile.Pool, "Pool values do not match"); Assert.AreEqual(modelFile.BoneTable.Count, savedModelFile.BoneTable.Count, "Bone table counts do not match"); for (int i = 0; i < modelFile.BoneTable.Count; i++) { Assert.AreEqual(modelFile.BoneTable[i], savedModelFile.BoneTable[i], "Bone table values do not match"); } Assert.AreEqual(modelFile.Vertices.Count, savedModelFile.Vertices.Count, "Vertex counts do not match"); for (int i = 0; i < modelFile.Vertices.Count; i++) { Assert.AreEqual(modelFile.Vertices[i].Position, savedModelFile.Vertices[i].Position, "Vertex position values do not match"); Assert.AreEqual(modelFile.Vertices[i].Normal, savedModelFile.Vertices[i].Normal, "Vertex normal values do not match"); Assert.AreEqual(modelFile.Vertices[i].Colour, savedModelFile.Vertices[i].Colour, "Vertex colour values do not match"); Assert.AreEqual(modelFile.Vertices[i].BoneWeights, savedModelFile.Vertices[i].BoneWeights, "Vertex bone weight values do not match"); Assert.AreEqual(modelFile.Vertices[i].BoneIndices, savedModelFile.Vertices[i].BoneIndices, "Vertex bone index values do not match"); Assert.AreEqual(modelFile.Vertices[i].TextureCoordinates[0], savedModelFile.Vertices[i].TextureCoordinates[0], "Vertex texture coordinate values do not match"); Assert.AreEqual(modelFile.Vertices[i].TextureCoordinates[1], savedModelFile.Vertices[i].TextureCoordinates[1], "Vertex texture coordinate values do not match"); Assert.AreEqual(modelFile.Vertices[i].TextureCoordinates[2], savedModelFile.Vertices[i].TextureCoordinates[2], "Vertex texture coordinate values do not match"); Assert.AreEqual(modelFile.Vertices[i].TextureCoordinates[3], savedModelFile.Vertices[i].TextureCoordinates[3], "Vertex texture coordinate values do not match"); Assert.AreEqual(modelFile.Vertices[i].Tangent, savedModelFile.Vertices[i].Tangent, "Vertex tangent values do not match"); } Assert.AreEqual(modelFile.Indices.Count, savedModelFile.Indices.Count, "Index counts do not match"); for (int i = 0; i < modelFile.Indices.Count; i++) { Assert.AreEqual(modelFile.Indices[i], savedModelFile.Indices[i], "Index values do not match"); } Assert.AreEqual(modelFile.Materials.Count, savedModelFile.Materials.Count, "Material counts do not match"); for (int i = 0; i < modelFile.Materials.Count; i++) { Assert.AreEqual(modelFile.Materials[i], savedModelFile.Materials[i], "Material values do not match"); } Assert.AreEqual(modelFile.Strips.Count, savedModelFile.Strips.Count, "Strip counts do not match"); for (int i = 0; i < modelFile.Strips.Count; i++) { Assert.AreEqual(modelFile.Strips[i], savedModelFile.Strips[i], "Strip values do not match"); } }