Beispiel #1
0
        /// <summary>
        /// コピー元のアバターのBlendShapeClipを基に、コピー先のアバターのBlendShapeClipを書き替えます。
        /// </summary>
        /// <param name="sourceClip"></param>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        private static void CopyBlendShapeClip(BlendShapeClip sourceClip, GameObject source, GameObject destination)
        {
            var destinationBlendShapeAvatar = destination.GetComponent <VRMBlendShapeProxy>().BlendShapeAvatar;

            var destinationClip = sourceClip.Preset != BlendShapePreset.Unknown
                ? destinationBlendShapeAvatar.GetClip(sourceClip.Preset)
                : destinationBlendShapeAvatar.GetClip(sourceClip.BlendShapeName);

            if (sourceClip == destinationClip)
            {
                return;
            }

            if (!destinationClip)
            {
                destinationClip = BlendShapeAvatar.CreateBlendShapeClip(
                    UnityPath.FromAsset(destinationBlendShapeAvatar)
                    .Parent.Child(Path.GetFileName(AssetDatabase.GetAssetPath(sourceClip))).Value
                    );
                destinationBlendShapeAvatar.Clips.Add(destinationClip);
                EditorUtility.SetDirty(destinationBlendShapeAvatar);
            }

            destinationClip.Values = sourceClip.Values
                                     .Select(binding => CopyVRMBlendShapes.CopyBlendShapeBinding(binding, source, destination)).ToArray();

            destinationClip.MaterialValues = sourceClip.MaterialValues.ToArray();

            EditorUtility.SetDirty(destinationClip);
        }
Beispiel #2
0
        /// <summary>
        /// アセットをプレハブが置かれているディレクトリの直下のフォルダへ複製します。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">複製元のオブジェクト。</param>
        /// <param name="prefabInstance">プレハブインスタンス。</param>
        /// <param name="fileName">ファイル名が複製元と異なる場合に指定。</param>
        /// <returns></returns>
        internal static T DuplicateAssetToFolder <T>(
            T source,
            GameObject prefabInstance,
            string fileName = ""
            ) where T : Object
        {
            string destinationFileName;

            if (string.IsNullOrEmpty(fileName))
            {
                var sourceUnityPath = UnityPath.FromAsset(source);
                if (!sourceUnityPath.IsUnderAssetsFolder || AssetDatabase.IsSubAsset(source))
                {
                    destinationFileName = source.name.EscapeFilePath() + ".asset";
                }
                else
                {
                    destinationFileName = Path.GetFileName(sourceUnityPath.Value);
                }
            }
            else
            {
                destinationFileName = fileName;
            }

            return(Duplicator.DuplicateAsset(
                       source,
                       destinationPath: Duplicator.DetermineAssetPath(prefabInstance, typeof(T), destinationFileName)
                       ));
        }
Beispiel #3
0
        void Restore()
        {
            var assetPath = UnityPath.FromAsset(this);

            if (assetPath.IsNull)
            {
                return;
            }


            foreach (var x in assetPath.Parent.ChildFiles)
            {
                var clip = UnityEditor.AssetDatabase.LoadAssetAtPath <BlendShapeClip>(x.Value);
                if (clip == null)
                {
                    continue;
                }

                if (!Clips.Contains(clip))
                {
                    Clips.Add(clip);
                }

                Debug.LogFormat("{0}", clip.name);
            }
            Clips = Clips.OrderBy(x => BlendShapeKey.CreateFrom(x)).ToList();
        }
        private static glTF_VCAST_vci_audio FromAudioClip(glTF gltf, AudioClip clip)
        {
#if UNITY_EDITOR
            if (Application.isPlaying)
#endif
            {
                var bytes     = WaveUtil.GetWaveBinary(clip);
                var viewIndex = gltf.ExtendBufferAndGetViewIndex(0, bytes);
                return(new glTF_VCAST_vci_audio
                {
                    name = clip.name,
                    mimeType = "audio/wav",
                    bufferView = viewIndex,
                });
            }
#if UNITY_EDITOR
            else
            {
                var path = UnityPath.FromAsset(clip);
                if (!path.IsUnderAssetsFolder)
                {
                    return(null);
                }
                if (path.Extension.ToLower() == ".wav")
                {
                    var bytes     = File.ReadAllBytes(path.FullPath);
                    var viewIndex = gltf.ExtendBufferAndGetViewIndex(0, bytes);
                    return(new glTF_VCAST_vci_audio
                    {
                        name = clip.name,
                        mimeType = "audio/wav",
                        bufferView = viewIndex,
                    });
                }
                else if (path.Extension.ToLower() == ".mp3")
                {
                    var bytes     = File.ReadAllBytes(path.FullPath);
                    var viewIndex = gltf.ExtendBufferAndGetViewIndex(0, bytes);
                    return(new glTF_VCAST_vci_audio
                    {
                        name = clip.name,
                        mimeType = "audio/mp3",
                        bufferView = viewIndex,
                    });
                }
                else
                {
                    // Convert to wav
                    var bytes     = WaveUtil.GetWaveBinary(clip);
                    var viewIndex = gltf.ExtendBufferAndGetViewIndex(0, bytes);
                    return(new glTF_VCAST_vci_audio
                    {
                        name = clip.name,
                        mimeType = "audio/wav",
                        bufferView = viewIndex,
                    });
                }
            }
#endif
        }
Beispiel #5
0
        /// <param name="src">GameObject instance in scene or prefab</param>
        public static bool Execute(GameObject src, bool onlyBlendShapeRenderers)
        {
            var results = new List <MeshIntegrationResult>();

            // instance or prefab => copy
            var copy = GameObject.Instantiate(src);

            copy.name = copy.name + "_mesh_integration";

            // integrate
            if (onlyBlendShapeRenderers)
            {
                results.Add(MeshIntegratorUtility.Integrate(copy, onlyBlendShapeRenderers: MeshEnumerateOption.OnlyWithBlendShape));
                results.Add(MeshIntegratorUtility.Integrate(copy, onlyBlendShapeRenderers: MeshEnumerateOption.OnlyWithoutBlendShape));
            }
            else
            {
                results.Add(MeshIntegratorUtility.Integrate(copy, onlyBlendShapeRenderers: MeshEnumerateOption.All));
            }

            // replace
            MeshIntegratorUtility.ReplaceMeshWithResults(copy, results);

            // write mesh asset.
            foreach (var result in results)
            {
                var mesh      = result.IntegratedRenderer.sharedMesh;
                var assetPath = GetMeshWritePath(mesh);
                Debug.LogFormat("CreateAsset: {0}", assetPath);
                AssetDatabase.CreateAsset(mesh, assetPath);
            }

            if (src.GetGameObjectType() == GameObjectType.AssetPrefab)
            {
                // write prefab.
                {
                    var prefabPath = UnityPath.FromAsset(src);
                    prefabPath = prefabPath.Parent.Child($"{prefabPath.FileNameWithoutExtension}_integrated.prefab");
                    Debug.LogFormat("WritePrefab: {0}", prefabPath);
                    PrefabUtility.SaveAsPrefabAsset(copy, prefabPath.Value);
                }

                // destroy copy in scene.
                GameObject.DestroyImmediate(copy);
            }
            else
            {
                // do nothing. keep copy.
            }

            return(true);
        }
        private static T BackupAsset <T>(T asset, GameObject rootPrefab) where T : UnityEngine.Object
        {
            var srcAssetPath = UnityPath.FromAsset(asset);
            var assetName    = srcAssetPath.FileName;

            var backupDir  = "MeshIntegratorBackup";
            var backupPath = UnityPath.FromAsset(rootPrefab).Parent.Child(backupDir);

            backupPath.EnsureFolder();
            var dstAssetPath = backupPath.Child(assetName);

            AssetDatabase.CopyAsset(srcAssetPath.Value, dstAssetPath.Value);
            return(dstAssetPath.LoadAsset <T>());
        }
Beispiel #7
0
        /// <summary>
        /// アセットを複製します。
        /// </summary>
        /// <remarks>
        /// 複製先にすでにアセットが存在していれば上書きし、metaファイルは複製しません。
        /// </remarks>
        /// <param name="source"></param>
        /// <param name="duplicatedPath">「Assets/」から始まりファイル名で終わる複製先のパス。</param>
        private static T DuplicateAsset <T>(T source, string destinationPath) where T : Object
        {
            if (source is AnimatorController controller)
            {
                return(Duplicator.DuplicateAnimatorControllerAsset(controller, destinationPath) as T);
            }

            var sourceUnityPath = UnityPath.FromAsset(source);
            var destination     = AssetDatabase.LoadMainAssetAtPath(destinationPath);
            var copied          = false;

            if (destination)
            {
                if (AssetDatabase.IsNativeAsset(source) || !sourceUnityPath.IsUnderAssetsFolder)
                {
                    EditorUtility.CopySerialized(source, destination);
                }
                else
                {
                    var sourceFullPath      = sourceUnityPath.FullPath;
                    var destinationFullPath = destinationPath.AssetPathToFullPath();
                    if (File.GetLastWriteTime(sourceFullPath) != File.GetLastWriteTime(destinationFullPath))
                    {
                        File.Copy(sourceFullPath, destinationFullPath, overwrite: true);
                        AssetDatabase.ImportAsset(destinationPath);
                        copied = true;
                    }
                }
            }
            else
            {
                if (AssetDatabase.IsSubAsset(source) || !sourceUnityPath.IsUnderAssetsFolder)
                {
                    AssetDatabase.CreateAsset(Duplicator.DuplicateAssetInstance(source), destinationPath);
                }
                else
                {
                    AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(source), destinationPath);
                }
            }

            var destinationAsset = AssetDatabase.LoadAssetAtPath <T>(destinationPath);

            if (copied)
            {
                EditorUtility.SetDirty(destinationAsset);
                AssetDatabase.SaveAssets();
            }
            return(destinationAsset);
        }
Beispiel #8
0
        /// <summary>
        /// アセットを複製します。
        /// </summary>
        /// <remarks>
        /// 複製先にすでにアセットが存在していれば上書きし、metaファイルは複製しません。
        /// </remarks>
        /// <param name="source"></param>
        /// <param name="duplicatedPath">「Assets/」から始まりファイル名で終わる複製先のパス。</param>
        private static void DuplicateAsset(UnityEngine.Object source, string destinationPath)
        {
            var sourceUnityPath = UnityPath.FromAsset(source);

            UnityEngine.Object destination = AssetDatabase.LoadMainAssetAtPath(destinationPath);
            if (destination)
            {
                if (AssetDatabase.IsNativeAsset(source) && !(source is AnimatorController) ||
                    !sourceUnityPath.IsUnderAssetsFolder)
                {
                    EditorUtility.CopySerialized(source, destination);
                }
                else
                {
                    var sourceFullPath      = sourceUnityPath.FullPath;
                    var destinationFullPath = destinationPath.AssetPathToFullPath();
                    if (File.GetLastWriteTime(sourceFullPath) != File.GetLastWriteTime(destinationFullPath))
                    {
                        File.Copy(
                            sourceFileName: sourceFullPath,
                            destFileName: destinationFullPath,
                            overwrite: true
                            );
                        AssetDatabase.ImportAsset(destinationPath);
                    }
                }
            }
            else
            {
                if (AssetDatabase.IsSubAsset(source) || !sourceUnityPath.IsUnderAssetsFolder)
                {
                    AssetDatabase.CreateAsset(Duplicator.DuplicateAssetInstance(source), destinationPath);
                }
                else
                {
                    AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(source), destinationPath);
                }
            }
        }
        /// <summary>
        /// 情報をコピーします。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        private static void CopyInformation(GameObject source, GameObject destination)
        {
            var sourceMeta      = source.GetComponent <VRMMeta>().Meta;
            var destinationMeta = destination.GetComponent <VRMMeta>().Meta;

            destinationMeta.Title              = sourceMeta.Title;
            destinationMeta.Version            = sourceMeta.Version;
            destinationMeta.Author             = sourceMeta.Author;
            destinationMeta.ContactInformation = sourceMeta.ContactInformation;
            destinationMeta.Reference          = sourceMeta.Reference;

            destinationMeta.Thumbnail = sourceMeta.Thumbnail;
            if (!destinationMeta.Thumbnail)
            {
                return;
            }

            var sourceThumbnailPath = AssetDatabase.GetAssetPath(destinationMeta.Thumbnail);

            if (UnityPath.FromUnityPath(sourceThumbnailPath).Parent.Value
                != UnityPath.FromAsset(source).GetAssetFolder(suffix: ".Textures").Value)
            {
                return;
            }

            var destinationPrefabPath = CopyVRMSettings.GetPrefabAssetPath(destination);

            if (string.IsNullOrEmpty(destinationPrefabPath))
            {
                return;
            }

            var destinationThumbnailPath = UnityPath.FromUnityPath(destinationPrefabPath)
                                           .GetAssetFolder(suffix: ".Textures")
                                           .Child(Path.GetFileName(sourceThumbnailPath)).GenerateUniqueAssetPath().Value;

            AssetDatabase.CopyAsset(sourceThumbnailPath, destinationThumbnailPath);
            destinationMeta.Thumbnail = AssetDatabase.LoadAssetAtPath <Texture2D>(destinationThumbnailPath);
        }
        /// <summary>
        /// プレハブのパスを取得します。
        /// </summary>
        /// <param name="vrm"></param>
        /// <returns></returns>
        private string GetAssetsPath(GameObject vrm)
        {
            var path = "";

            var unityPath = UnityPath.FromAsset(asset: vrm);

            if (unityPath.IsUnderAssetsFolder)
            {
                path = unityPath.Value;
            }

            if (string.IsNullOrEmpty(path))
            {
                path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(vrm);
            }

            if (string.IsNullOrEmpty(path))
            {
                path = "Assets/" + avatar.name;
            }

            return(path);
        }