/// <summary> /// SubAssetを外部ファイルに展開する /// </summary> public static void Extract(ScriptedImporter importer, GltfParser parser) { if (string.IsNullOrEmpty(importer.assetPath)) { return; } var path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Extracted"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } // meta { foreach (var asset in importer.GetSubAssets <VRM10MetaObject>(importer.assetPath)) { asset.ExtractSubAsset($"{path}/{asset.name}.asset", false); } } { // expressions foreach (var asset in importer.GetSubAssets <VRM10Expression>(importer.assetPath)) { asset.ExtractSubAsset($"{path}/{asset.name}.asset", false); } // expressions foreach (var asset in importer.GetSubAssets <VRM10ExpressionAvatar>(importer.assetPath)) { asset.ExtractSubAsset($"{path}/{asset.name}.asset", false); } // external な expressionAvatar.Clips に 再代入する var expressionAvatar = importer.GetExternalObjectMap().Select(x => x.Value as VRM10ExpressionAvatar).FirstOrDefault(x => x != null); var expressions = importer.GetExternalObjectMap().Select(x => x.Value as VRM10Expression).Where(x => x != null).ToList(); expressionAvatar.Clips = expressions; var avatarPath = AssetDatabase.GetAssetPath(expressionAvatar); if (!string.IsNullOrEmpty(avatarPath)) { EditorUtility.SetDirty(expressionAvatar); AssetDatabase.WriteImportSettingsIfDirty(avatarPath); } } AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); }
static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <Texture2D> addRemap = externalObject => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); }; Action <IEnumerable <UnityPath> > onCompleted = _ => { AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); self.ExtractMaterials(); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }; var assetPath = UnityPath.FromFullpath(parser.TargetPath); var dirName = $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures(parser, assetPath.Parent.Child(dirName), GltfTextureEnumerator.Enumerate, self.GetSubAssets <UnityEngine.Texture2D>(self.assetPath).ToArray(), addRemap, onCompleted ); }
static void ExtractMaterialsAndTextures(ScriptedImporter self) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <Texture2D> addRemap = externalObject => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject); }; Action <IEnumerable <string> > onCompleted = _ => { AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); self.ExtractMaterials(); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }; TextureExtractor.ExtractTextures(self.assetPath, GltfTextureEnumerator.Enumerate, self.GetSubAssets <UnityEngine.Texture2D>(self.assetPath).ToArray(), addRemap, onCompleted ); }
static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures, Func <string, string> textureDir, Func <string, string> materialDir) { if (string.IsNullOrEmpty(self.assetPath)) { return; } Action <SubAssetKey, Texture2D> addRemap = (key, externalObject) => { self.AddRemap(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), externalObject); }; Action <IEnumerable <UnityPath> > onCompleted = _ => { AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); self.ExtractMaterials(materialDir); AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); }; var assetPath = UnityPath.FromFullpath(parser.TargetPath); var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures(parser, assetPath.Parent.Child(dirName), enumTextures, self.GetSubAssets <UnityEngine.Texture2D>(self.assetPath).ToArray(), addRemap, onCompleted ); }
public TextureExtractor(ScriptedImporter importer) { // parse GLTF m_parser = new GltfParser(); m_parser.ParsePath(importer.assetPath); m_path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Textures"; m_subAssets = importer.GetSubAssets <UnityEngine.Texture2D>(importer.assetPath).ToArray(); }
public static void ExtractMaterials(this ScriptedImporter importer) { if (string.IsNullOrEmpty(importer.assetPath)) { return; } var path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Materials"; var info = TextureExtractor.SafeCreateDirectory(path); foreach (var asset in importer.GetSubAssets <Material>(importer.assetPath)) { ExtractSubAsset(asset, $"{path}/{asset.name}.mat", false); } }
public static void ExtractAssets <T>(this ScriptedImporter importer, string dirName, string extension) where T : UnityEngine.Object { if (string.IsNullOrEmpty(importer.assetPath)) { return; } var subAssets = importer.GetSubAssets <T>(importer.assetPath); var path = string.Format("{0}/{1}.{2}", Path.GetDirectoryName(importer.assetPath), Path.GetFileNameWithoutExtension(importer.assetPath), dirName ); var info = importer.SafeCreateDirectory(path); foreach (var asset in subAssets) { ExtractFromAsset(asset, string.Format("{0}/{1}{2}", path, asset.name, extension), false); } }
public static void ExtractTextures(this ScriptedImporter importer, string dirName, Func <string, VrmLib.Model> CreateModel, Action onComplited = null) { if (string.IsNullOrEmpty(importer.assetPath)) { return; } var subAssets = importer.GetSubAssets <UnityEngine.Texture2D>(importer.assetPath); var path = string.Format("{0}/{1}.{2}", Path.GetDirectoryName(importer.assetPath), Path.GetFileNameWithoutExtension(importer.assetPath), dirName ); importer.SafeCreateDirectory(path); Dictionary <VrmLib.ImageTexture, string> targetPaths = new Dictionary <VrmLib.ImageTexture, string>(); // Reload Model var model = CreateModel(importer.assetPath); var mimeTypeReg = new System.Text.RegularExpressions.Regex("image/(?<mime>.*)$"); int count = 0; foreach (var texture in model.Textures) { var imageTexture = texture as VrmLib.ImageTexture; if (imageTexture == null) { continue; } var mimeType = mimeTypeReg.Match(imageTexture.Image.MimeType); var assetName = !string.IsNullOrEmpty(imageTexture.Name) ? imageTexture.Name : string.Format("{0}_img{1}", model.Root.Name, count); var targetPath = string.Format("{0}/{1}.{2}", path, assetName, mimeType.Groups["mime"].Value); imageTexture.Name = assetName; if (imageTexture.TextureType == VrmLib.Texture.TextureTypes.MetallicRoughness || imageTexture.TextureType == VrmLib.Texture.TextureTypes.Occlusion) { var subAssetTexture = subAssets.Where(x => x.name == imageTexture.Name).FirstOrDefault(); File.WriteAllBytes(targetPath, subAssetTexture.EncodeToPNG()); } else { File.WriteAllBytes(targetPath, imageTexture.Image.Bytes.ToArray()); } AssetDatabase.ImportAsset(targetPath); targetPaths.Add(imageTexture, targetPath); count++; } EditorApplication.delayCall += () => { foreach (var targetPath in targetPaths) { var imageTexture = targetPath.Key; var targetTextureImporter = AssetImporter.GetAtPath(targetPath.Value) as TextureImporter; targetTextureImporter.sRGBTexture = (imageTexture.ColorSpace == VrmLib.Texture.ColorSpaceTypes.Srgb); if (imageTexture.TextureType == VrmLib.Texture.TextureTypes.NormalMap) { targetTextureImporter.textureType = TextureImporterType.NormalMap; } targetTextureImporter.SaveAndReimport(); var externalObject = AssetDatabase.LoadAssetAtPath(targetPath.Value, typeof(UnityEngine.Texture2D)); importer.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), imageTexture.Name), externalObject); } //AssetDatabase.WriteImportSettingsIfDirty(assetPath); AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); if (onComplited != null) { onComplited(); } }; }
private static T GetSubAsset <T>(this ScriptedImporter importer, string assetPath) where T : UnityEngine.Object { return(importer.GetSubAssets <T>(assetPath) .FirstOrDefault()); }