Example #1
0
        public void BuildPrefab(bool erase = false)
        {
            ToolBox.DirExNorCreate("Assets/Resources/Prefabs/Zones/");
            string zoneFolder = "Assets/Resources/Prefabs/Zones/Zone" + ZoneID + "/";

            ToolBox.DirExNorCreate(zoneFolder);
            string path = zoneFolder + "Unit" + UnitID + ".prefab";

            GameObject pref = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;

            if (pref != null && erase == false)
            {
                return;
            }
            else
            {
                if (erase)
                {
                    AssetDatabase.DeleteAsset(path);
                }
            }


#if UNITY_EDITOR
            GameObject shapeGO = null;
            GameObject weaponGO;
            GameObject shieldGO;

            if (zudShape != null)
            {
                shapeGO = zudShape.BuildGameObject();
                AssetDatabase.AddObjectToAsset(zudShape.mesh, path);
                AssetDatabase.AddObjectToAsset(zudShape.texture, path);
                AssetDatabase.AddObjectToAsset(zudShape.material, path);
            }
            if (zudWeapon != null)
            {
                weaponGO = zudWeapon.BuildGameObject();
                if (shapeGO)
                {
                    weaponGO.transform.parent = shapeGO.transform;
                }

                AssetDatabase.AddObjectToAsset(zudWeapon.mesh, path);
                AssetDatabase.AddObjectToAsset(zudWeapon.texture, path);
                AssetDatabase.AddObjectToAsset(zudWeapon.material, path);
            }
            if (zudShield != null)
            {
                shieldGO = zudShield.BuildGameObject();
                if (shapeGO)
                {
                    shieldGO.transform.parent = shapeGO.transform;
                }

                AssetDatabase.AddObjectToAsset(zudShield.mesh, path);
                AssetDatabase.AddObjectToAsset(zudShield.texture, path);
                AssetDatabase.AddObjectToAsset(zudShield.material, path);
            }
            AnimatorController ac;
            if ((zudComSeq != null || zudBatSeq != null) && shapeGO != null)
            {
                Avatar ava = AvatarBuilder.BuildGenericAvatar(shapeGO, "");
                //Avatar ava = AvatarBuilder.BuildHumanAvatar(shapeGO, new HumanDescription());
                ava.name = FileName + "_Ava";
                //ava.humanDescription.

                AssetDatabase.AddObjectToAsset(ava, path);
                Animator animator = shapeGO.GetComponent <Animator>();
                if (!animator)
                {
                    animator = shapeGO.AddComponent <Animator>();
                }

                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, path);
                int i = 0;
                if (zudComSeq != null && shapeGO != null)
                {
                    zudComSeq.FirstPoseModel(shapeGO);
                    AnimationClip[] clips = zudComSeq.BuildAnimationClips(shapeGO);
                    ac.AddLayer(FileName + "_COM");
                    i++;
                    foreach (AnimationClip clip in clips)
                    {
                        if (clip != null)
                        {
                            AssetDatabase.AddObjectToAsset(clip, path);
                            AnimatorState state = ac.layers[i].stateMachine.AddState(clip.name);
                            state.motion = clip;
                        }
                    }
                }
                if (zudBatSeq != null && shapeGO != null)
                {
                    if (zudComSeq == null)
                    {
                        zudBatSeq.FirstPoseModel(shapeGO);
                    }

                    AnimationClip[] clips = zudBatSeq.BuildAnimationClips(shapeGO);
                    ac.AddLayer(FileName + "_BAT");
                    i++;
                    foreach (AnimationClip clip in clips)
                    {
                        if (clip != null)
                        {
                            AssetDatabase.AddObjectToAsset(clip, path);
                            AnimatorState state = ac.layers[i].stateMachine.AddState(clip.name);
                            state.motion = clip;
                        }
                    }
                }
            }

            GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(shapeGO, path, InteractionMode.AutomatedAction);
            AssetDatabase.SaveAssets();
            GameObject.DestroyImmediate(shapeGO);
#endif
        }
Example #2
0
        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;
        }