TextureExtractor

This commit is contained in:
ousttrue
2021-03-03 16:21:30 +09:00
parent 24f5079945
commit d13755c079
3 changed files with 52 additions and 53 deletions

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;
@@ -12,7 +13,6 @@ namespace UniGLTF
[SerializeField]
Axises m_reverseAxis = default;
const string TextureDirName = "Textures";
const string MaterialDirName = "Materials";
public override void OnImportAsset(AssetImportContext ctx)
@@ -83,11 +83,23 @@ namespace UniGLTF
public void ExtractMaterialsAndTextures()
{
TextureExtractor.ExtractTextures(this, TextureDirName, () =>
if (string.IsNullOrEmpty(assetPath))
{
this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
});
return;
}
TextureExtractor.ExtractTextures(assetPath,
this.GetSubAssets<UnityEngine.Texture2D>(this.assetPath).ToArray(),
externalObject =>
{
this.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject);
},
() =>
{
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
});
}
public void SetExternalUnityObject<T>(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object

View File

@@ -75,22 +75,12 @@ namespace UniGLTF
dirName
);
var info = importer.SafeCreateDirectory(path);
var info = TextureExtractor.SafeCreateDirectory(path);
foreach (var asset in subAssets)
{
ExtractFromAsset(asset, string.Format("{0}/{1}{2}", path, asset.name, extension), false);
}
}
public static DirectoryInfo SafeCreateDirectory(this ScriptedImporter importer, string path)
{
if (Directory.Exists(path))
{
return null;
}
return Directory.CreateDirectory(path);
}
}
}

View File

@@ -1,16 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using System.Linq;
namespace UniGLTF
{
class TextureExtractor
{
const string TextureDirName = "Textures";
GltfParser m_parser;
public GltfParser Parser => m_parser;
@@ -21,17 +21,27 @@ namespace UniGLTF
UnityEngine.Texture2D[] m_subAssets;
string m_path;
public TextureExtractor(ScriptedImporter importer)
public TextureExtractor(string assetPath, UnityEngine.Texture2D[] subAssets)
{
// parse GLTF
m_parser = new GltfParser();
m_parser.ParsePath(importer.assetPath);
m_parser.ParsePath(assetPath);
m_path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Textures";
m_subAssets = importer.GetSubAssets<UnityEngine.Texture2D>(importer.assetPath).ToArray();
m_path = $"{Path.GetDirectoryName(assetPath)}/{Path.GetFileNameWithoutExtension(assetPath)}.Textures";
SafeCreateDirectory(m_path);
m_subAssets = subAssets;
}
static Regex s_mimeTypeReg = new Regex("image/(?<mime>.*)$");
static string GetExt(string mime)
{
switch (mime)
{
case "image/png": return ".png";
case "image/jpeg": return ".jpg";
}
throw new NotImplementedException();
}
public void Extract(GetTextureParam param)
{
@@ -44,11 +54,7 @@ namespace UniGLTF
case GetTextureParam.OCCLUSION_PROP:
{
// write converted texture
targetPath = string.Format("{0}/{1}{2}",
m_path,
param.Name,
".png"
);
targetPath = $"{m_path}/{param.Name}.png";
File.WriteAllBytes(targetPath, subAsset.EncodeToPNG().ToArray());
break;
}
@@ -56,12 +62,10 @@ namespace UniGLTF
default:
{
// write original bytes
targetPath = string.Format("{0}/{1}{2}",
m_path,
param.Name,
".png"
);
var gltfTexture = GLTF.textures[param.Index0.Value];
var gltfImage = GLTF.images[gltfTexture.source];
var ext = GetExt(gltfImage.mimeType);
targetPath = $"{m_path}/{param.Name}{ext}";
File.WriteAllBytes(targetPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray());
break;
}
@@ -70,6 +74,15 @@ namespace UniGLTF
Textures.Add(targetPath, param);
}
public static DirectoryInfo SafeCreateDirectory(string path)
{
if (Directory.Exists(path))
{
return null;
}
return Directory.CreateDirectory(path);
}
/// <summary>
///
/// * Texture(.png etc...)をディスクに書き出す
@@ -80,23 +93,9 @@ namespace UniGLTF
/// <param name="importer"></param>
/// <param name="dirName"></param>
/// <param name="onCompleted"></param>
public static void ExtractTextures(ScriptedImporter importer, string dirName, Action onCompleted = null)
public static void ExtractTextures(string assetPath, Texture2D[] subAssets, Action<Texture2D> addRemap, Action onCompleted = null)
{
if (string.IsNullOrEmpty(importer.assetPath))
{
return;
}
var path = string.Format("{0}/{1}.{2}",
Path.GetDirectoryName(importer.assetPath),
Path.GetFileNameWithoutExtension(importer.assetPath),
dirName
);
importer.SafeCreateDirectory(path);
// Reload Model
var extractor = new TextureExtractor(importer);
var extractor = new TextureExtractor(assetPath, subAssets);
foreach (var material in extractor.GLTF.materials)
{
foreach (var x in extractor.Parser.EnumerateTextures(material))
@@ -131,11 +130,9 @@ namespace UniGLTF
// remap
var externalObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Texture2D>(targetPath);
importer.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject);
addRemap(externalObject);
}
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
if (onCompleted != null)
{
onCompleted();