コード例 #1
0
        //public bool ConvertMorphFile(string srcMorphFilePath, string dstMorphFilePath, Dictionary<int,int>vertMap)
        //{

        //    bool compressed = srcMorphFilePath.Substring(-2, 2).Equals("gz");
        //    StreamingMorphs sm = new StreamingMorphs();
        //    //this is the "welded" or likely less vert count original
        //    MorphData srcMorphData = sm.GetMorphDataFromDisk(srcMorphFilePath, true, compressed, true);
        //    MorphData dstMorphData = ConvertMorph(srcMorphData, vertMap);

        //    sm.WriteMorphDataToFile(ref dstMorphData, dstMorphFilePath, true);

        //    return true;
        //}

        public MCS_Utilities.Morph.MorphData ConvertMorph(MCS_Utilities.Morph.MorphData srcMorphData, Dictionary <int, int> vertMap)
        {
            //this will be our "unwelded" or likely higher vert count new morph
            MCS_Utilities.Morph.MorphData dstMorphData = new MCS_Utilities.Morph.MorphData();

            dstMorphData.meshName = srcMorphData.meshName;
            dstMorphData.name     = srcMorphData.name;
            dstMorphData.jctData  = srcMorphData.jctData; //TODO: this is technically not supported as we don't do anything with this yet

            MCS_Utilities.Morph.BlendshapeData bd = new MCS_Utilities.Morph.BlendshapeData();

            bd.name       = srcMorphData.blendshapeData.name;
            bd.shapeIndex = srcMorphData.blendshapeData.shapeIndex;
            bd.frameIndex = srcMorphData.blendshapeData.frameIndex;

            bd.deltaVertices = new Vector3[vertMap.Count];
            bd.deltaNormals  = new Vector3[vertMap.Count]; //TODO: unsupported
            bd.deltaTangents = new Vector3[vertMap.Count]; //TODO: unsupported

            //rebuild this morph using the vertmap, we'll likely be adding duplicates
            foreach (int dstVertIndex in vertMap.Keys)
            {
                int srcVertIndex = vertMap[dstVertIndex];
                bd.deltaVertices[dstVertIndex] = srcMorphData.blendshapeData.deltaVertices[srcVertIndex];
            }

            dstMorphData.blendshapeData = bd;

            return(dstMorphData);
        }
コード例 #2
0
ファイル: ImportUtilities.cs プロジェクト: xysverma/mcs
        public static bool RemapMorphsIfRequired(GameObject go)
        {
            SkinnedMeshRenderer[] smrArray = go.GetComponentsInChildren <SkinnedMeshRenderer>();
            StreamingMorphs       sb       = new StreamingMorphs();

            StreamingMorphs.LoadMainThreadAssetPaths();

            bool remapped = false;


            //we'll refresh this folder when we're done if we need to
            //string refreshDir = null;

            foreach (SkinnedMeshRenderer smr in smrArray)
            {
                Dictionary <int, int> tsMap = new Dictionary <int, int>();
                bool isTargetMapGenerated   = false;
                //InferredMeta meta = new InferredMeta(assetPath, go, smr);
                CoreMesh coreMesh = smr.GetComponent <CoreMesh>();
                if (coreMesh == null)
                {
                    Debug.LogWarning("Skipping: " + smr.name + ", it does not contain a CoreMesh Component");
                    continue;
                }

                //ConvertBlendshapeFromMap
                #region Remapping
                ProjectionMeshMap pmm = new ProjectionMeshMap();
                string            incompatMorphPath = coreMesh.runtimeMorphPath + "_incompat";
                string            compatMorphPath   = coreMesh.runtimeMorphPath;

                string incompatMorphPathAbsolute = Path.Combine(Application.streamingAssetsPath, incompatMorphPath);
                string compatMorphPathAbsolute   = Path.Combine(Application.streamingAssetsPath, compatMorphPath);

                string incompatMorphPathAbsoluteMeta = incompatMorphPathAbsolute + ".meta";
                string compatMorphPathAbsoluteMeta   = compatMorphPathAbsolute + ".meta";

                string mrPath = compatMorphPathAbsolute + ".morphs.mr";
                mrPath = mrPath.Replace(@"\", "/");

                bool incompatExists = Directory.Exists(incompatMorphPathAbsolute);
                bool compatExists   = Directory.Exists(compatMorphPathAbsolute);

                if (compatExists)
                {
                    string compatProjectionFilePath   = Path.Combine(compatMorphPathAbsolute, "projectionmap.bin");
                    string incompatProjectionFilePath = Path.Combine(incompatMorphPathAbsolute, "projectionmap.bin");
                    bool   didFindProjectionFile      = File.Exists(compatProjectionFilePath);

                    if (didFindProjectionFile)
                    {
                        var manifest = MCS_Utilities.MorphExtraction.MorphExtraction.GenerateManifestByCrawling(compatMorphPathAbsolute, compatMorphPathAbsolute.Substring(compatMorphPathAbsolute.LastIndexOf("/")));

                        if (manifest.names.Length <= 0)
                        {
                            UnityEngine.Debug.LogError("Unable to generate proper manifest in: " + compatMorphPathAbsolute + " => " + compatMorphPathAbsolute.Substring(compatMorphPathAbsolute.LastIndexOf("/")));
                        }

                        MCS_Utilities.Morph.MorphData sourceMD = sb.GetMorphDataFromResources(compatMorphPathAbsolute, manifest.names[0]);
                        pmm.Read(compatProjectionFilePath);

                        if (pmm.IsReMappingNeeded(smr, sourceMD, false))
                        {
                            try
                            {
                                int count = 0;
                                int total = manifest.names.Length;
                                if (incompatExists)
                                {
                                    //Delete the incompat directory.
                                    MCS_Utilities.Paths.TryDirectoryDelete(incompatMorphPathAbsolute);
                                }
                                //Move contents to incompat directory
                                Directory.Move(compatMorphPathAbsolute, incompatMorphPathAbsolute);
                                //Delete compat directory - clean up
                                //Directory.Delete(compatMorphPathAbsolute);
                                //Create compat directory.
                                Directory.CreateDirectory(compatMorphPathAbsolute);

                                foreach (string morph in manifest.names)
                                {
                                    EditorUtility.DisplayProgressBar("Processing Morphs...", morph, ((float)count / (float)total));
                                    try
                                    {
                                        MCS_Utilities.Morph.MorphData targetMD;
                                        //does this blendshape already exist, if so just continue and ignore it, and don't lookup from disk
                                        int blendshapeIndex = smr.sharedMesh.GetBlendShapeIndex(morph);
                                        if (blendshapeIndex >= 0)
                                        {
                                            continue;
                                        }

                                        sourceMD = sb.GetMorphDataFromResources(incompatMorphPathAbsolute, morph);

                                        //"Assets/MCS/Content/RRMale/Morph/Resources/LaidBack/LaidBackPants/projectionmap.bin");
                                        if (!isTargetMapGenerated)
                                        {
                                            tsMap = pmm.GenerateTargetToSourceMap(smr);
                                            isTargetMapGenerated = true;
                                        }
                                        targetMD = pmm.ConvertMorphDataFromMap(smr, sourceMD, tsMap);
                                        //MorphData mData = new MorphData { name = bsNew.name, meshName = smr.name, blendshapeState = bsNew };
                                        //var morphPath = meta.morphPathKey + "_NEW";

                                        MCS_Utilities.MorphExtraction.MorphExtraction.WriteMorphDataToFile(targetMD, compatMorphPathAbsolute + "/" + targetMD.name + ".morph", false, false);
                                        sourceMD = targetMD;
                                        //UnityEngine.Debug.Log("Converted");
                                    } catch (Exception e)
                                    {
                                        UnityEngine.Debug.LogException(e);
                                    }
                                    count++;
                                }
                                EditorUtility.ClearProgressBar();
                                //Copy the projectionmap.bin file to new folder.
                                if (isTargetMapGenerated)
                                {
                                    string saPath = Application.streamingAssetsPath;
                                    File.Copy(incompatProjectionFilePath, compatProjectionFilePath);
                                    //coreMesh.runtimeMorphPath = regeneratedMorphPath;
                                }

                                //generate an mr file
                                UnityEngine.Debug.Log("Creating MR: " + mrPath);
                                string baseMRPath = compatMorphPathAbsolute.Replace(@"\", @"/");
                                MCS_Utilities.MorphExtraction.MorphExtraction.MergeMorphsIntoMR(baseMRPath, mrPath);

                                if (Directory.Exists(compatMorphPathAbsolute))
                                {
                                    MCS_Utilities.Paths.TryDirectoryDelete(compatMorphPathAbsolute);
                                }
                                if (Directory.Exists(incompatMorphPathAbsolute))
                                {
                                    MCS_Utilities.Paths.TryDirectoryDelete(incompatMorphPathAbsolute);
                                }
                                if (File.Exists(incompatMorphPathAbsoluteMeta))
                                {
                                    File.Delete(incompatMorphPathAbsoluteMeta);
                                }
                                if (File.Exists(compatMorphPathAbsoluteMeta))
                                {
                                    File.Delete(compatMorphPathAbsoluteMeta);
                                }

                                remapped = true;
                            }
                            catch (Exception e)
                            {
                                Debug.LogException(e);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Missing projection file. Can't remap!");
                    }
                }
                else
                {
                    //Debug.LogWarning("Directory containing morphs does not exist!");
                }
                #endregion
            }

            //NOTE: this does not work

            /*
             * UnityEngine.Debug.LogWarning("Refresh Dir: " + refreshDir);
             *
             * if (!String.IsNullOrEmpty(refreshDir))
             * {
             *  if (Directory.Exists(refreshDir))
             *  {
             *      UnityEngine.Debug.Log("Refreshing: " + refreshDir);
             *      AssetDatabase.ImportAsset(refreshDir);
             *  }
             * }
             */
            return(remapped);
        }