public void Parse(BinaryReader buffer) { idCharacter = buffer.ReadByte(); idWeapon = buffer.ReadByte(); idWeaponCategory = buffer.ReadByte(); idWeaponMaterial = buffer.ReadByte(); idShield = buffer.ReadByte(); idShieldMaterial = buffer.ReadByte(); Unknown = buffer.ReadByte(); padding = buffer.ReadByte(); if (UseDebug) { Debug.Log(FileName); Debug.Log("idCharacter : " + idCharacter); Debug.Log("idWeapon : " + idWeapon); Debug.Log("idWeaponCategory : " + idWeaponCategory); Debug.Log("idWeaponMaterial : " + idWeaponMaterial); Debug.Log("idShield : " + idShield); Debug.Log("idShieldMaterial : " + idShieldMaterial); Debug.Log("Unknown : " + Unknown); Debug.Log("padding : " + padding); } // pointers long ptrCharacterSHP = buffer.ReadInt32(); long lenCharacterSHP = buffer.ReadInt32(); long ptrWeaponWEP = buffer.ReadInt32(); long lenWeaponWEP = buffer.ReadInt32(); long ptrShieldWEP = buffer.ReadInt32(); long lenShieldWEP = buffer.ReadInt32(); long ptrCommonSEQ = buffer.ReadInt32(); long lenCommonSEQ = buffer.ReadInt32(); long ptrBattleSEQ = buffer.ReadInt32(); long lenBattleSEQ = buffer.ReadInt32(); if (UseDebug) { Debug.Log("ptrCharacterSHP : " + ptrCharacterSHP); Debug.Log("lenCharacterSHP : " + lenCharacterSHP); Debug.Log("ptrWeaponWEP : " + ptrWeaponWEP); Debug.Log("lenWeaponWEP : " + lenWeaponWEP); Debug.Log("ptrShieldWEP : " + ptrShieldWEP); Debug.Log("lenShieldWEP : " + lenShieldWEP); Debug.Log("ptrCommonSEQ : " + ptrCommonSEQ); Debug.Log("lenCommonSEQ : " + lenCommonSEQ); Debug.Log("ptrBattleSEQ : " + ptrBattleSEQ); Debug.Log("lenBattleSEQ : " + lenBattleSEQ); } // shape section if (lenCharacterSHP > 0) { if (buffer.BaseStream.Position != ptrCharacterSHP) { Debug.LogWarning("le pointeur ptrCharacterSHP n'est pas à la bonne place : " + buffer.BaseStream.Position + " != " + ptrCharacterSHP); buffer.BaseStream.Position = ptrCharacterSHP; } zudShape = new SHP(); zudShape.excpFaces = excpFaces; zudShape.FileName = FileName + "_ZSHP"; zudShape.UseDebug = UseDebug; zudShape.Parse(buffer); } // weapon section if (lenWeaponWEP > 0) { if (buffer.BaseStream.Position != ptrWeaponWEP) { Debug.LogWarning("le pointeur ptrWeaponWEP n'est pas à la bonne place : " + buffer.BaseStream.Position + " != " + ptrWeaponWEP); buffer.BaseStream.Position = ptrWeaponWEP; } zudWeapon = new WEP(); zudWeapon.FileName = ((byte)idWeapon).ToString(); zudWeapon.SmithMaterial = idWeaponMaterial; zudWeapon.Parse(buffer); } // shield section if (lenShieldWEP > 0) { if (buffer.BaseStream.Position != ptrShieldWEP) { Debug.LogWarning("le pointeur ptrShieldWEP n'est pas à la bonne place : " + buffer.BaseStream.Position + " != " + ptrShieldWEP); buffer.BaseStream.Position = ptrShieldWEP; } zudShield = new WEP(); zudShield.FileName = ((byte)idShield).ToString(); zudShield.SmithMaterial = idShieldMaterial; zudShield.Parse(buffer); } // common anim section if (lenCommonSEQ > 0) { if (buffer.BaseStream.Position != ptrCommonSEQ) { Debug.LogWarning("le pointeur ptrCommonSEQ n'est pas à la bonne place : " + buffer.BaseStream.Position + " != " + ptrCommonSEQ); buffer.BaseStream.Position = ptrCommonSEQ; } zudComSeq = new SEQ(); zudComSeq.FileName = FileName + "_COM_SEQ"; zudComSeq.Parse(buffer); } // battle anim section if (lenBattleSEQ > 0) { if (buffer.BaseStream.Position != ptrBattleSEQ) { Debug.LogWarning("le pointeur ptrBattleSEQ n'est pas à la bonne place : " + buffer.BaseStream.Position + " != " + ptrBattleSEQ); buffer.BaseStream.Position = ptrBattleSEQ; } zudBatSeq = new SEQ(); zudBatSeq.FileName = FileName + "_BAT_SEQ"; zudBatSeq.Parse(buffer); } }
public void Build(bool buildPrefab = false) { // Building Model for (int i = 0; i < numFaces; i++) { for (int j = 0; j < faces[i].verticesCount; j++) { if (tim != null) { float u = faces[i].uv[j].x / tim.width; float v = faces[i].uv[j].y / tim.height; faces[i].uv[j] = (new Vector2(u, v)); } else { faces[i].uv[j] = Vector2.zero; } } } Mesh shapeMesh = new Mesh(); List <Vector3> meshVertices = new List <Vector3>(); List <int> meshTriangles = new List <int>(); List <Vector2> meshTrianglesUV = new List <Vector2>(); List <BoneWeight> meshWeights = new List <BoneWeight>(); List <Color32> meshColors = new List <Color32>(); for (int i = 0; i < faces.Count; i++) { //Debug.Log("faces["+i+"] .type : " + faces[i].type + " || .size : " + faces[i].size + " || .side : " + faces[i].side + " || .alpha : " + faces[i].alpha); if (faces[i].type == 0x2C || faces[i].type == 0x3C) { if (faces[i].side != 4) { meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[0]].position); meshWeights.Add(vertices[faces[i].vertices[0]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTrianglesUV.Add(faces[i].uv[0]); meshTrianglesUV.Add(faces[i].uv[1]); meshTrianglesUV.Add(faces[i].uv[2]); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[3]].position); meshWeights.Add(vertices[faces[i].vertices[3]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTrianglesUV.Add(faces[i].uv[3]); meshTrianglesUV.Add(faces[i].uv[2]); meshTrianglesUV.Add(faces[i].uv[1]); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[0]].position); meshWeights.Add(vertices[faces[i].vertices[0]].boneWeight); meshTrianglesUV.Add(faces[i].uv[2]); meshTrianglesUV.Add(faces[i].uv[1]); meshTrianglesUV.Add(faces[i].uv[0]); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[3]].position); meshWeights.Add(vertices[faces[i].vertices[3]].boneWeight); meshTrianglesUV.Add(faces[i].uv[1]); meshTrianglesUV.Add(faces[i].uv[2]); meshTrianglesUV.Add(faces[i].uv[3]); if (excpFaces) { // we have colored vertices meshColors.Add(faces[i].colors[0]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[3]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[0]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[3]); } } else { meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[0]].position); meshWeights.Add(vertices[faces[i].vertices[0]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTrianglesUV.Add(faces[i].uv[0]); meshTrianglesUV.Add(faces[i].uv[1]); meshTrianglesUV.Add(faces[i].uv[2]); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[3]].position); meshWeights.Add(vertices[faces[i].vertices[3]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTrianglesUV.Add(faces[i].uv[3]); meshTrianglesUV.Add(faces[i].uv[2]); meshTrianglesUV.Add(faces[i].uv[1]); if (excpFaces) { // we have colored vertices meshColors.Add(faces[i].colors[0]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[3]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[1]); } } } else if (faces[i].type == 0x24 || faces[i].type == 0x34) { if (faces[i].side != 4) { meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[0]].position); meshWeights.Add(vertices[faces[i].vertices[0]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTrianglesUV.Add(faces[i].uv[1]); meshTrianglesUV.Add(faces[i].uv[2]); meshTrianglesUV.Add(faces[i].uv[0]); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[0]].position); meshWeights.Add(vertices[faces[i].vertices[0]].boneWeight); meshTrianglesUV.Add(faces[i].uv[0]); meshTrianglesUV.Add(faces[i].uv[2]); meshTrianglesUV.Add(faces[i].uv[1]); if (excpFaces) { // we have colored vertices meshColors.Add(faces[i].colors[0]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[2]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[0]); } } else { meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[0]].position); meshWeights.Add(vertices[faces[i].vertices[0]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[1]].position); meshWeights.Add(vertices[faces[i].vertices[1]].boneWeight); meshTriangles.Add(meshVertices.Count); meshVertices.Add(vertices[faces[i].vertices[2]].position); meshWeights.Add(vertices[faces[i].vertices[2]].boneWeight); meshTrianglesUV.Add(faces[i].uv[1]); meshTrianglesUV.Add(faces[i].uv[2]); meshTrianglesUV.Add(faces[i].uv[0]); if (excpFaces) { // we have colored vertices meshColors.Add(faces[i].colors[0]); meshColors.Add(faces[i].colors[1]); meshColors.Add(faces[i].colors[2]); } } } } shapeMesh.name = FileName + "_mesh"; shapeMesh.vertices = meshVertices.ToArray(); shapeMesh.triangles = meshTriangles.ToArray(); shapeMesh.uv = meshTrianglesUV.ToArray(); shapeMesh.boneWeights = meshWeights.ToArray(); if (excpFaces) { shapeMesh.colors32 = meshColors.ToArray(); } if (tim != null) { texture = tim.textures[0]; texture.filterMode = FilterMode.Trilinear; texture.anisoLevel = 4; #if UNITY_EDITOR texture.alphaIsTransparency = true; #endif texture.wrapMode = TextureWrapMode.Repeat; texture.Compress(true); } else { texture = new Texture2D(128, 128); } Material mat = null; if (excpFaces) { Shader shader = Shader.Find("Particles/Standard Unlit"); mat = new Material(shader); mat.name = string.Concat("Material_SHP_", FileName); mat.SetTexture("_MainTex", texture); mat.SetFloat("_Mode", 0); mat.SetFloat("_ColorMode", 0); mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); mat.SetInt("_ZWrite", 1); mat.EnableKeyword("_ALPHATEST_ON"); mat.DisableKeyword("_ALPHABLEND_ON"); mat.DisableKeyword("_ALPHAPREMULTIPLY_ON"); mat.SetTextureScale("_MainTex", new Vector2(1, -1)); } else { Shader shader = Shader.Find("Standard"); mat = new Material(shader); mat.name = string.Concat("Material_SHP_", FileName); mat.SetTexture("_MainTex", texture); mat.SetFloat("_Mode", 1); mat.SetFloat("_Cutoff", 0.5f); mat.SetFloat("_Glossiness", 0.0f); mat.SetFloat("_SpecularHighlights", 0.0f); mat.SetFloat("_GlossyReflections", 0.0f); mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); mat.SetInt("_ZWrite", 1); mat.EnableKeyword("_ALPHATEST_ON"); mat.DisableKeyword("_ALPHABLEND_ON"); mat.DisableKeyword("_ALPHAPREMULTIPLY_ON"); mat.SetTextureScale("_MainTex", new Vector2(1, -1)); } material = mat; GameObject shapeGo = new GameObject(FileName); GameObject meshGo = new GameObject(FileName + "_mesh"); meshGo.transform.parent = shapeGo.transform; Transform[] meshBones = new Transform[numBones]; Matrix4x4[] bindPoses = new Matrix4x4[numBones]; for (int i = 0; i < numBones; i++) { meshBones[i] = new GameObject(bones[i].name).transform; meshBones[i].localRotation = Quaternion.identity; bindPoses[i] = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one); if (bones[i].parentIndex == -1) { meshBones[i].parent = shapeGo.transform; meshBones[i].localPosition = Vector3.zero; } else { meshBones[i].parent = meshBones[bones[i].parentIndex]; meshBones[i].localPosition = new Vector3((float)(bones[bones[i].parentIndex].length) / 100, 0, 0); } } SkinnedMeshRenderer mr = meshGo.AddComponent <SkinnedMeshRenderer>(); mr.material = mat; mr.bones = meshBones; shapeMesh.bindposes = bindPoses; mr.rootBone = meshBones[0]; mr.sharedMesh = shapeMesh; mesh = shapeMesh; this.shapeGo = shapeGo; string modFolder = "Assets/Resources/Prefabs/Models/"; string modFilename = string.Concat(modFolder, "SHP_", FileName, ".prefab"); #if UNITY_EDITOR if (buildPrefab == true) { GameObject shpBase = shapeGo; GameObject prefab = PrefabUtility.SaveAsPrefabAsset(shpBase, modFilename); string shpId = FileName; if (shpId == "06" || shpId == "99" || shpId == "C9" || shpId == "CA") { shpId = "00"; // Ashley Shapes } if (shpId == "7C") { shpId = "02"; // Rozencrantz } if (shpId == "9B") { shpId = "03"; // Merlose } if (shpId == "98") { shpId = "83"; // Sydney } // Animations seeker Avatar ava = AvatarBuilder.BuildGenericAvatar(shpBase, "bone_0"); ava.name = FileName + "_Ava"; AssetDatabase.AddObjectToAsset(ava, modFilename); Animator animator = shpBase.GetComponent <Animator>(); if (!animator) { animator = shpBase.AddComponent <Animator>(); } AnimatorController ac = AnimatorController.Instantiate(AssetDatabase.LoadAssetAtPath <AnimatorController>("Assets/Resources/Prefabs/DefaultAC.controller") as AnimatorController); ac.name = FileName + "_AC"; animator.runtimeAnimatorController = ac; animator.avatar = ava; AssetDatabase.AddObjectToAsset(ac, modFilename); string[] hash = FilePath.Split("/"[0]); hash[hash.Length - 1] = ""; string[] files = Directory.GetFiles(String.Join("/", hash), "*.SEQ"); bool posed = false; uint i = 0; foreach (string file in files) { List <string> topa = new List <string>(); topa.Add("_COM.SEQ"); topa.Add("_BT1.SEQ"); topa.Add("_BT2.SEQ"); topa.Add("_BT3.SEQ"); topa.Add("_BT4.SEQ"); topa.Add("_BT5.SEQ"); topa.Add("_BT6.SEQ"); topa.Add("_BT7.SEQ"); topa.Add("_BT8.SEQ"); topa.Add("_BT9.SEQ"); topa.Add("_BTA.SEQ"); hash = file.Split("/"[0]); if (hash[hash.Length - 1].StartsWith(shpId) && file.EndsWith(".SEQ")) { if (topa.Contains(hash[hash.Length - 1].Substring(2, 8))) { SEQ _seq = new SEQ(); _seq.Parse(file); if (!posed || file == shpId + "_COM.SEQ") { posed = true; _seq.FirstPoseModel(shpBase); Mesh baked = new Mesh(); baked.name = string.Concat("Baked_Mesh_SHP_", FileName); mr.BakeMesh(baked); //baked.bindposes = bindPoses; // need to recalculate this before using the baked mesh //baked.boneWeights = meshWeights.ToArray(); //mr.sharedMesh = baked; //shapeMesh.RecalculateNormals(); shapeMesh.RecalculateTangents(); shapeMesh.Optimize(); //AssetDatabase.RemoveObjectFromAsset(mesh); AssetDatabase.AddObjectToAsset(baked, modFilename); } AnimationClip[] clips = _seq.BuildAnimationClips(shpBase); AnimatorControllerLayer layer = new AnimatorControllerLayer(); layer.name = hash[hash.Length - 1].Substring(0, 6); layer.stateMachine = new AnimatorStateMachine(); layer.stateMachine.states = ac.layers[0].stateMachine.states; ac.AddLayer(hash[hash.Length - 1].Substring(0, 6)); i++; foreach (AnimationClip clip in clips) { if (clip != null) { AssetDatabase.AddObjectToAsset(clip, modFilename); AnimatorState state = ac.layers[i].stateMachine.AddState(clip.name); state.motion = clip; } } } } } if (shpBase != null) { mesh = shapeMesh; AssetDatabase.AddObjectToAsset(mesh, modFilename); AssetDatabase.AddObjectToAsset(material, modFilename); AssetDatabase.AddObjectToAsset(texture, modFilename); } //PrefabUtility.ReplacePrefab(shpBase, prefab); prefab = PrefabUtility.SaveAsPrefabAsset(shpBase, modFilename); AssetDatabase.SaveAssets(); GameObject.DestroyImmediate(shpBase); } #endif mesh = shapeMesh; }