fix ExtractTextures

This commit is contained in:
ousttrue 2021-02-24 23:18:15 +09:00
parent 471daaf5bf
commit 09c79cb8eb
7 changed files with 171 additions and 112 deletions

View File

@ -76,19 +76,19 @@ namespace UniGLTF
}
}
public void ExtractTextures()
{
// extract textures to files
this.ExtractTextures(TextureDirName);
// reimport
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
}
// public void ExtractTextures()
// {
// // extract textures to files
// this.ExtractTextures(TextureDirName);
// // reimport
// AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
// }
public void ExtractMaterials()
{
this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
}
// public void ExtractMaterials()
// {
// this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
// AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
// }
public void ExtractMaterialsAndTextures()
{
@ -99,10 +99,10 @@ namespace UniGLTF
});
}
public Dictionary<string, T> GetExternalUnityObjects<T>() where T : UnityEngine.Object
{
return this.GetExternalObjectMap().Where(x => x.Key.type == typeof(T)).ToDictionary(x => x.Key.name, x => (T)x.Value);
}
// public Dictionary<string, T> GetExternalUnityObjects<T>() where T : UnityEngine.Object
// {
// return this.GetExternalObjectMap().Where(x => x.Key.type == typeof(T)).ToDictionary(x => x.Key.name, x => (T)x.Value);
// }
public void SetExternalUnityObject<T>(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object
{

View File

@ -85,10 +85,14 @@ namespace UniGLTF
{
throw new System.ArgumentNullException();
}
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel(name);
if (map.TryGetValue(name, out T value))
map.TryGetValue(name, out T value);
if (map.Any() && value == null)
{
var a = 0;
// Debug.Log($"{name}: {value}");
}
var asset = EditorGUILayout.ObjectField(value, typeof(T), true) as T;
if (asset != value)

View File

@ -96,6 +96,7 @@ namespace UniGLTF
class TextureExtractor
{
GltfParser m_parser;
public GltfParser Parser => m_parser;
public glTF GLTF => m_parser.GLTF;
@ -115,59 +116,17 @@ namespace UniGLTF
static Regex s_mimeTypeReg = new Regex("image/(?<mime>.*)$");
public void Extract(int? index, string prop = default)
public void Extract(GetTextureParam param)
{
if (!index.HasValue)
{
return;
}
var gltfTexture = GLTF.textures[index.Value];
var gltfImage = GLTF.images[gltfTexture.source];
var mimeType = s_mimeTypeReg.Match(gltfImage.mimeType);
var ext = "";
switch (mimeType.Groups["mime"].Value)
{
case "jpeg":
ext = ".jpg";
break;
case "png":
ext = ".png";
break;
default:
throw new NotImplementedException();
}
var assetName = gltfImage.name;
string targetPath = default;
switch (prop)
{
case GetTextureParam.NORMAL_PROP:
case GetTextureParam.METALLIC_GLOSS_PROP:
case GetTextureParam.OCCLUSION_PROP:
// File.WriteAllBytes(targetPath, subAsset.EncodeToPNG());
throw new NotImplementedException();
default:
{
var name = "";
var bytes = GLTF.GetImageBytes(m_parser.Storage, gltfTexture.source, out name);
targetPath = string.Format("{0}/{1}{2}",
m_path,
assetName,
ext
);
File.WriteAllBytes(targetPath, bytes.ToArray());
}
break;
}
var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.Name);
var targetPath = string.Format("{0}/{1}{2}",
m_path,
param.Name,
".png"
);
File.WriteAllBytes(targetPath, subAsset.EncodeToPNG().ToArray());
AssetDatabase.ImportAsset(targetPath);
var subAsset = m_subAssets.FirstOrDefault(x => x.name == assetName);
Textures.Add(new TextureInfo
{
Path = targetPath,
@ -195,11 +154,9 @@ namespace UniGLTF
foreach (var material in extractor.GLTF.materials)
{
// standard or unlit
extractor.Extract(material.pbrMetallicRoughness?.baseColorTexture?.index);
if (!glTF_KHR_materials_unlit.IsEnable(material))
foreach (var x in extractor.Parser.EnumerateTextures(material))
{
// standard
extractor.Extract(x);
}
}

View File

@ -257,6 +257,12 @@ namespace UniGLTF
}
used.Add(lower);
}
var ext = GetTextureExtension(i);
if (!string.IsNullOrEmpty(ext))
{
AppendImageExtension(image, ext);
}
}
}
@ -313,14 +319,117 @@ namespace UniGLTF
}
#endregion
public IEnumerable<GetTextureParam> EnumerateTextures()
public static void AppendImageExtension(glTFImage texture, string extension)
{
for (int i = 0; i < GLTF.textures.Count; ++i)
if (!texture.name.EndsWith(extension))
{
yield return GetTextureParam.Create(GLTF, i);
texture.name = texture.name + extension;
}
}
public string GetTextureExtension(int imageIndex)
{
foreach (var m in GLTF.materials)
{
if (m.pbrMetallicRoughness != null)
{
// base color
if (m.pbrMetallicRoughness?.baseColorTexture != null)
{
if (m.pbrMetallicRoughness.baseColorTexture.index == imageIndex)
{
return "";
}
}
// metallic roughness
if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null)
{
if (m.pbrMetallicRoughness.metallicRoughnessTexture.index == imageIndex)
{
return ".metallicRoughness";
}
}
}
// emission
if (m.emissiveTexture != null)
{
if (m.emissiveTexture.index == imageIndex)
{
return "";
}
}
// normal
if (m.normalTexture != null)
{
if (m.normalTexture.index == imageIndex)
{
return "";
}
}
// occlusion
if (m.occlusionTexture != null)
{
if (m.occlusionTexture.index == imageIndex)
{
return ".occlusion";
}
}
}
// TODO: converted textures
return "";
}
public IEnumerable<GetTextureParam> EnumerateTextures(glTFMaterial m)
{
if (m.pbrMetallicRoughness != null)
{
// base color
if (m.pbrMetallicRoughness?.baseColorTexture != null)
{
yield return PBRMaterialItem.BaseColorTexture(GLTF, m);
}
// metallic roughness
if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null)
{
yield return PBRMaterialItem.MetallicRoughnessTexture(GLTF, m);
}
}
// emission
if (m.emissiveTexture != null)
{
yield return GetTextureParam.Create(GLTF, m.emissiveTexture.index);
}
// normal
if (m.normalTexture != null)
{
yield return PBRMaterialItem.NormalTexture(GLTF, m);
}
// occlusion
if (m.occlusionTexture != null)
{
yield return PBRMaterialItem.OcclusionTexture(GLTF, m);
}
}
public IEnumerable<GetTextureParam> EnumerateTextures()
{
for (int i = 0; i < GLTF.materials.Count; ++i)
{
var m = GLTF.materials[i];
foreach (var x in EnumerateTextures(m))
{
yield return x;
}
}
}
}
}

View File

@ -43,6 +43,28 @@ namespace UniGLTF
Transparent
}
public static GetTextureParam BaseColorTexture(glTF gltf, glTFMaterial src)
{
return GetTextureParam.Create(gltf, src.pbrMetallicRoughness.baseColorTexture.index);
}
public static GetTextureParam MetallicRoughnessTexture(glTF gltf, glTFMaterial src)
{
return GetTextureParam.CreateMetallic(gltf,
src.pbrMetallicRoughness.metallicRoughnessTexture.index,
src.pbrMetallicRoughness.metallicFactor);
}
public static GetTextureParam OcclusionTexture(glTF gltf, glTFMaterial src)
{
return GetTextureParam.CreateOcclusion(gltf, src.occlusionTexture.index);
}
public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src)
{
return GetTextureParam.CreateNormal(gltf, src.normalTexture.index);
}
public static async Awaitable<Material> CreateAsync(glTF gltf, int i, GetTextureAsyncFunc getTexture)
{
if (getTexture == null)
@ -66,7 +88,7 @@ namespace UniGLTF
if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1)
{
material.mainTexture = await getTexture(gltf, GetTextureParam.Create(gltf, src.pbrMetallicRoughness.baseColorTexture.index));
material.mainTexture = await getTexture(gltf, BaseColorTexture(gltf, src));
// Texture Offset and Scale
MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
@ -76,9 +98,7 @@ namespace UniGLTF
{
material.EnableKeyword("_METALLICGLOSSMAP");
var texture = await getTexture(gltf, GetTextureParam.CreateMetallic(gltf,
src.pbrMetallicRoughness.metallicRoughnessTexture.index,
src.pbrMetallicRoughness.metallicFactor));
var texture = await getTexture(gltf, MetallicRoughnessTexture(gltf, src));
if (texture != null)
{
material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture);
@ -101,7 +121,7 @@ namespace UniGLTF
if (src.normalTexture != null && src.normalTexture.index != -1)
{
material.EnableKeyword("_NORMALMAP");
var texture = await getTexture(gltf, GetTextureParam.CreateNormal(gltf, src.normalTexture.index));
var texture = await getTexture(gltf, NormalTexture(gltf, src));
if (texture != null)
{
material.SetTexture(GetTextureParam.NORMAL_PROP, texture);
@ -114,7 +134,7 @@ namespace UniGLTF
if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
{
var texture = await getTexture(gltf, GetTextureParam.CreateOcclusion(gltf, src.occlusionTexture.index));
var texture = await getTexture(gltf, OcclusionTexture(gltf, src));
if (texture != null)
{
material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture);

View File

@ -31,22 +31,6 @@ namespace UniGLTF
return copyTexture;
}
public static void AppendTextureExtension(Texture texture, string extension)
{
if (!texture.name.EndsWith(extension))
{
texture.name = texture.name + extension;
}
}
public static void RemoveTextureExtension(Texture texture, string extension)
{
if (texture.name.EndsWith(extension))
{
texture.name = texture.name.Replace(extension, "");
}
}
struct ColorSpaceScope : IDisposable
{
bool m_sRGBWrite;
@ -194,8 +178,6 @@ namespace UniGLTF
public class MetallicRoughnessConverter : ITextureConverter
{
private const string m_extension = ".metallicRoughness";
private float _smoothnessOrRoughness;
public MetallicRoughnessConverter(float smoothnessOrRoughness)
@ -206,14 +188,12 @@ namespace UniGLTF
public Texture2D GetImportTexture(Texture2D texture)
{
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Import, null);
TextureConverter.AppendTextureExtension(converted, m_extension);
return converted;
}
public Texture2D GetExportTexture(Texture2D texture)
{
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Export, null);
TextureConverter.RemoveTextureExtension(converted, m_extension);
return converted;
}
@ -260,8 +240,6 @@ namespace UniGLTF
public class NormalConverter : ITextureConverter
{
private const string m_extension = ".normal";
private Material m_decoder;
private Material GetDecoder()
{
@ -288,7 +266,6 @@ namespace UniGLTF
{
var mat = GetEncoder();
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, mat);
TextureConverter.AppendTextureExtension(converted, m_extension);
return converted;
}
@ -298,26 +275,22 @@ namespace UniGLTF
{
var mat = GetDecoder();
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, mat);
TextureConverter.RemoveTextureExtension(converted, m_extension);
return converted;
}
}
public class OcclusionConverter : ITextureConverter
{
private const string m_extension = ".occlusion";
public Texture2D GetImportTexture(Texture2D texture)
{
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Occlusion, Import, null);
TextureConverter.AppendTextureExtension(converted, m_extension);
return converted;
}
public Texture2D GetExportTexture(Texture2D texture)
{
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Occlusion, Export, null);
TextureConverter.RemoveTextureExtension(converted, m_extension);
return converted;
}

View File

@ -148,7 +148,6 @@ namespace UniGLTF
{
var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false);
var converted = new NormalConverter().GetImportTexture(baseTexture);
converted.name = $"{converted.name}.{GetTextureParam.NORMAL_PROP}";
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(param, info);
return info.Texture;
@ -161,7 +160,6 @@ namespace UniGLTF
var textureAssetPath = AssetDatabase.GetAssetPath(info.Texture);
TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath);
info.Texture.name = $"{info.Texture.name}.{GetTextureParam.NORMAL_PROP}";
#endif
return info.Texture;
}
@ -172,7 +170,6 @@ namespace UniGLTF
// Bake roughnessFactor values into a texture.
var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false);
var converted = new MetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(baseTexture);
converted.name = $"{converted.name}.{GetTextureParam.METALLIC_GLOSS_PROP}";
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(param, info);
return info.Texture;
@ -182,7 +179,6 @@ namespace UniGLTF
{
var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false);
var converted = new OcclusionConverter().GetImportTexture(baseTexture);
converted.name = $"{converted.name}.{GetTextureParam.OCCLUSION_PROP}";
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(param, info);
return info.Texture;