Merge pull request #792 from ousttrue/fix/texture_enumerate

Fix/texture enumerate
This commit is contained in:
ousttrue
2021-03-17 12:09:22 +09:00
committed by GitHub
9 changed files with 134 additions and 64 deletions

View File

@@ -51,7 +51,7 @@ namespace UniGLTF
s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
if (s_foldTextures)
{
DrawRemapGUI<UnityEngine.Texture2D>(importer, parser.EnumerateTextures().Select(x => x.ConvertedName));
DrawRemapGUI<UnityEngine.Texture2D>(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x => x.ConvertedName));
}
if (GUILayout.Button("Clear"))
@@ -115,6 +115,7 @@ namespace UniGLTF
};
TextureExtractor.ExtractTextures(self.assetPath,
GltfTextureEnumerator.Enumerate,
self.GetSubAssets<UnityEngine.Texture2D>(self.assetPath).ToArray(),
addRemap,
onCompleted

View File

@@ -60,7 +60,7 @@ namespace UniGLTF
public static IEnumerable<(string, UnityEngine.Object)> EnumerateTexturesFromUri(Dictionary<AssetImporter.SourceAssetIdentifier, UnityEngine.Object> exclude,
GltfParser parser, UnityPath dir)
{
foreach (var texParam in parser.EnumerateTextures())
foreach (var texParam in GltfTextureEnumerator.Enumerate(parser.GLTF))
{
switch (texParam.TextureType)
{

View File

@@ -116,18 +116,16 @@ namespace UniGLTF
/// <param name="importer"></param>
/// <param name="dirName"></param>
/// <param name="onCompleted"></param>
public static void ExtractTextures(string assetPath, Texture2D[] subAssets, Action<Texture2D> addRemap, Action<IEnumerable<string>> onCompleted = null)
public static void ExtractTextures(string assetPath, TextureEnumerator textureEnumerator, Texture2D[] subAssets, Action<Texture2D> addRemap, Action<IEnumerable<string>> onCompleted = null)
{
var extractor = new TextureExtractor(assetPath, subAssets);
var normalMaps = new List<string>();
foreach (var material in extractor.GLTF.materials)
foreach (var x in textureEnumerator(extractor.GLTF))
{
foreach (var x in extractor.Parser.EnumerateTextures(material))
{
var gltfTexture = extractor.GLTF.textures[x.Index0.Value];
var gltfImage = extractor.GLTF.images[gltfTexture.source];
extractor.Extract(x, !string.IsNullOrEmpty(gltfImage.uri));
}
var gltfTexture = extractor.GLTF.textures[x.Index0.Value];
var gltfImage = extractor.GLTF.images[gltfTexture.source];
extractor.Extract(x, !string.IsNullOrEmpty(gltfImage.uri));
}
EditorApplication.delayCall += () =>

View File

@@ -334,7 +334,6 @@ namespace UniGLTF
}
#endregion
public static void AppendImageExtension(glTFImage texture, string extension)
{
if (!texture.name.EndsWith(extension))
@@ -342,57 +341,5 @@ namespace UniGLTF
texture.name = texture.name + extension;
}
}
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()
{
var used = new HashSet<string>();
for (int i = 0; i < GLTF.materials.Count; ++i)
{
var m = GLTF.materials[i];
foreach (var x in EnumerateTextures(m))
{
if (used.Add(x.ConvertedName))
{
yield return x;
}
}
}
}
}
}

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
namespace UniGLTF
{
public delegate IEnumerable<GetTextureParam> TextureEnumerator(glTF gltf);
public static class GltfTextureEnumerator
{
public static IEnumerable<GetTextureParam> EnumerateTextures(glTF gltf, 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 static IEnumerable<GetTextureParam> Enumerate(glTF gltf)
{
foreach (var material in gltf.materials)
{
foreach (var textureInfo in EnumerateTextures(gltf, material))
{
yield return textureInfo;
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 205a551be16aede40baa76e1f36e42a2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -152,7 +152,8 @@ namespace VRM
.ToArray();
var prefabParentDir = assetPath.Parent;
var folder = assetPath.GetAssetFolder(".Textures");
TextureExtractor.ExtractTextures(assetPath.Value, subAssets, _ => { }, onTextureReloaded);
var vrmTextures = new VRMTextureEnumerator(m_context.VRM);
TextureExtractor.ExtractTextures(assetPath.Value, vrmTextures.Enumerate, subAssets, _ => { }, onTextureReloaded);
}
bool SaveAsAsset(UnityEngine.Object o)

View File

@@ -0,0 +1,45 @@
using System.Collections.Generic;
using UniGLTF;
namespace VRM
{
public class VRMTextureEnumerator
{
readonly glTF_VRM_extensions m_vrm;
public VRMTextureEnumerator(glTF_VRM_extensions vrm)
{
m_vrm = vrm;
}
public IEnumerable<GetTextureParam> Enumerate(glTF gltf)
{
for (int i = 0; i < gltf.materials.Count; ++i)
{
var vrmMaterial = m_vrm.materialProperties[i];
if (vrmMaterial.shader == MToon.Utils.ShaderName)
{
// MToon
foreach (var kv in vrmMaterial.textureProperties)
{
// SRGB color or normalmap
yield return GetTextureParam.Create(gltf, kv.Value, kv.Key);
}
}
else
{
// PBR or Unlit
foreach (var textureInfo in GltfTextureEnumerator.EnumerateTextures(gltf, gltf.materials[i]))
{
yield return textureInfo;
}
}
}
// thumbnail
if (m_vrm.meta != null && m_vrm.meta.texture != -1)
{
yield return GetTextureParam.Create(gltf, m_vrm.meta.texture);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d9b840dcce2a5b94aae00d4bc4bf7e10
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: