mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-07 19:38:13 -05:00
GetTextureParam
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public static class AssetTextureLoader
|
||||
{
|
||||
public static Task<Texture2D> LoadTaskAsync(UnityPath m_assetPath,
|
||||
glTF gltf, int textureIndex)
|
||||
{
|
||||
var colorSpace = gltf.GetColorSpace(textureIndex);
|
||||
|
||||
//
|
||||
// texture from assets
|
||||
//
|
||||
m_assetPath.ImportAsset();
|
||||
var importer = m_assetPath.GetImporter<UnityEditor.TextureImporter>();
|
||||
if (importer == null)
|
||||
{
|
||||
Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
importer.maxTextureSize = 8192;
|
||||
importer.sRGBTexture = colorSpace == RenderTextureReadWrite.sRGB;
|
||||
importer.SaveAndReimport();
|
||||
}
|
||||
|
||||
var Texture = m_assetPath.LoadAsset<Texture2D>();
|
||||
|
||||
if (Texture == null)
|
||||
{
|
||||
Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
var maxSize = Mathf.Max(Texture.width, Texture.height);
|
||||
|
||||
importer.maxTextureSize
|
||||
= maxSize > 4096 ? 8192 :
|
||||
maxSize > 2048 ? 4096 :
|
||||
maxSize > 1024 ? 2048 :
|
||||
maxSize > 512 ? 1024 :
|
||||
512;
|
||||
|
||||
importer.SaveAndReimport();
|
||||
}
|
||||
|
||||
var sampler = gltf.GetSamplerFromTextureIndex(textureIndex);
|
||||
if (sampler != null)
|
||||
{
|
||||
TextureSamplerUtil.SetSampler(Texture, sampler);
|
||||
}
|
||||
|
||||
return Task.FromResult(Texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 748452d292d79304da4c271f584ab985
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -56,12 +56,12 @@ namespace UniGLTF
|
||||
s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
|
||||
if (s_foldTextures)
|
||||
{
|
||||
DrawRemapGUI<UnityEngine.Texture2D>(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x =>
|
||||
DrawRemapGUI<UnityEngine.Texture2D>(importer, GltfTextureEnumerator.Enumerate(parser).Select(x =>
|
||||
{
|
||||
switch (x.TextureType)
|
||||
{
|
||||
case GetTextureParam.TextureTypes.NormalMap:
|
||||
return x.GltflName;
|
||||
return x.GltfName;
|
||||
|
||||
default:
|
||||
return x.ConvertedName;
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace UniGLTF
|
||||
using (var loaded = new ImporterContext(parser, externalObjectMap.Concat(externalTextures)))
|
||||
{
|
||||
// settings TextureImporters
|
||||
foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser.GLTF))
|
||||
foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser))
|
||||
{
|
||||
TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap);
|
||||
}
|
||||
@@ -73,7 +73,7 @@ namespace UniGLTF
|
||||
GltfParser parser, UnityPath dir)
|
||||
{
|
||||
var used = new HashSet<Texture2D>();
|
||||
foreach (var texParam in GltfTextureEnumerator.Enumerate(parser.GLTF))
|
||||
foreach (var texParam in GltfTextureEnumerator.Enumerate(parser))
|
||||
{
|
||||
switch (texParam.TextureType)
|
||||
{
|
||||
@@ -82,11 +82,9 @@ namespace UniGLTF
|
||||
|
||||
default:
|
||||
{
|
||||
var gltfTexture = parser.GLTF.textures.First(y => y.name == texParam.GltflName);
|
||||
var gltfImage = parser.GLTF.images[gltfTexture.source];
|
||||
if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:"))
|
||||
if (!string.IsNullOrEmpty(texParam.Uri) && !texParam.Uri.StartsWith("data:"))
|
||||
{
|
||||
var child = dir.Child(gltfImage.uri);
|
||||
var child = dir.Child(texParam.Uri);
|
||||
var asset = AssetDatabase.LoadAssetAtPath<Texture2D>(child.Value);
|
||||
if (asset == null)
|
||||
{
|
||||
|
||||
@@ -40,22 +40,17 @@ namespace UniGLTF
|
||||
return Path.GetExtension(uri).ToLower();
|
||||
}
|
||||
|
||||
public void Extract(GetTextureParam param, bool hasUri)
|
||||
public void Extract(GetTextureParam param)
|
||||
{
|
||||
if (Textures.Values.Contains(param))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName);
|
||||
UnityPath targetPath = default;
|
||||
|
||||
if (hasUri && !param.ExtractConverted)
|
||||
if (!string.IsNullOrEmpty(param.Uri) && !param.ExtractConverted)
|
||||
{
|
||||
var gltfTexture = GLTF.textures[param.Index0.Value];
|
||||
var gltfImage = GLTF.images[gltfTexture.source];
|
||||
var ext = GetExt(gltfImage.mimeType, gltfImage.uri);
|
||||
targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}");
|
||||
targetPath = m_textureDirectory.Child(param.GltfFileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -65,7 +60,8 @@ namespace UniGLTF
|
||||
case GetTextureParam.TextureTypes.StandardMap:
|
||||
{
|
||||
// write converted texture
|
||||
targetPath = m_textureDirectory.Child($"{param.ConvertedName}.png");
|
||||
var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName);
|
||||
targetPath = m_textureDirectory.Child(param.ConvertedFileName);
|
||||
File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray());
|
||||
targetPath.ImportAsset();
|
||||
break;
|
||||
@@ -74,11 +70,8 @@ namespace UniGLTF
|
||||
default:
|
||||
{
|
||||
// write original bytes
|
||||
var gltfTexture = GLTF.textures[param.Index0.Value];
|
||||
var gltfImage = GLTF.images[gltfTexture.source];
|
||||
var ext = GetExt(gltfImage.mimeType, gltfImage.uri);
|
||||
targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}");
|
||||
File.WriteAllBytes(targetPath.FullPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray());
|
||||
targetPath = m_textureDirectory.Child(param.GltfFileName);
|
||||
File.WriteAllBytes(targetPath.FullPath, param.Index0().Result.ToArray());
|
||||
targetPath.ImportAsset();
|
||||
break;
|
||||
}
|
||||
@@ -112,11 +105,9 @@ namespace UniGLTF
|
||||
Action<IEnumerable<UnityPath>> onCompleted = null)
|
||||
{
|
||||
var extractor = new TextureExtractor(parser, textureDirectory, subAssets);
|
||||
foreach (var x in textureEnumerator(extractor.GLTF))
|
||||
foreach (var x in textureEnumerator(parser))
|
||||
{
|
||||
var gltfTexture = extractor.GLTF.textures[x.Index0.Value];
|
||||
var gltfImage = extractor.GLTF.images[gltfTexture.source];
|
||||
extractor.Extract(x, !string.IsNullOrEmpty(gltfImage.uri));
|
||||
extractor.Extract(x);
|
||||
}
|
||||
|
||||
EditorApplication.delayCall += () =>
|
||||
|
||||
@@ -7,6 +7,26 @@ namespace UniGLTF
|
||||
{
|
||||
public static class TextureImporterConfigurator
|
||||
{
|
||||
public static void ConfigureSize(Texture2D texture)
|
||||
{
|
||||
var path = UnityPath.FromAsset(texture);
|
||||
if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter)
|
||||
{
|
||||
var maxSize = Mathf.Max(texture.width, texture.height);
|
||||
textureImporter.maxTextureSize
|
||||
= maxSize > 4096 ? 8192 :
|
||||
maxSize > 2048 ? 4096 :
|
||||
maxSize > 1024 ? 2048 :
|
||||
maxSize > 512 ? 1024 :
|
||||
512;
|
||||
textureImporter.SaveAndReimport();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.IO.FileNotFoundException($"{path}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void ConfigureNormalMap(Texture2D texture)
|
||||
{
|
||||
var path = UnityPath.FromAsset(texture);
|
||||
@@ -47,8 +67,9 @@ namespace UniGLTF
|
||||
{
|
||||
case GetTextureParam.TextureTypes.NormalMap:
|
||||
{
|
||||
if (ExternalMap.TryGetValue(textureInfo.GltflName, out Texture2D external))
|
||||
if (ExternalMap.TryGetValue(textureInfo.GltfName, out Texture2D external))
|
||||
{
|
||||
ConfigureSize(external);
|
||||
ConfigureNormalMap(external);
|
||||
}
|
||||
}
|
||||
@@ -58,12 +79,19 @@ namespace UniGLTF
|
||||
{
|
||||
if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external))
|
||||
{
|
||||
ConfigureSize(external);
|
||||
ConfigureLinear(external);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case GetTextureParam.TextureTypes.sRGB:
|
||||
{
|
||||
if (ExternalMap.TryGetValue(textureInfo.GltfName, out Texture2D external))
|
||||
{
|
||||
ConfigureSize(external);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -372,6 +372,12 @@ namespace UniGLTF
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ArraySegment<Byte> GetImageBytesFromTextureIndex(this glTF self, IStorage storage, int textureIndex)
|
||||
{
|
||||
var imageIndex = self.textures[textureIndex].source;
|
||||
return self.GetImageBytes(storage, imageIndex);
|
||||
}
|
||||
|
||||
public static ArraySegment<Byte> GetImageBytes(this glTF self, IStorage storage, int imageIndex)
|
||||
{
|
||||
var image = self.images[imageIndex];
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace UniGLTF
|
||||
{
|
||||
m_parser = parser;
|
||||
m_textureFactory = new TextureFactory(GLTF, Storage, externalObjectMap);
|
||||
m_materialFactory = new MaterialFactory(GLTF, Storage, externalObjectMap);
|
||||
m_materialFactory = new MaterialFactory(m_parser, externalObjectMap);
|
||||
}
|
||||
|
||||
#region Source
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public delegate IEnumerable<GetTextureParam> TextureEnumerator(glTF gltf);
|
||||
public delegate IEnumerable<GetTextureParam> TextureEnumerator(GltfParser parser);
|
||||
|
||||
public static class GltfTextureEnumerator
|
||||
{
|
||||
public static IEnumerable<GetTextureParam> EnumerateTextures(glTF gltf, glTFMaterial m)
|
||||
public static IEnumerable<GetTextureParam> EnumerateTextures(GltfParser parser, glTFMaterial m)
|
||||
{
|
||||
int? metallicRoughnessTexture = default;
|
||||
if (m.pbrMetallicRoughness != null)
|
||||
@@ -14,7 +14,7 @@ namespace UniGLTF
|
||||
// base color
|
||||
if (m.pbrMetallicRoughness?.baseColorTexture != null)
|
||||
{
|
||||
yield return PBRMaterialItem.BaseColorTexture(gltf, m);
|
||||
yield return PBRMaterialItem.BaseColorTexture(parser, m);
|
||||
}
|
||||
|
||||
// metallic roughness
|
||||
@@ -27,13 +27,13 @@ namespace UniGLTF
|
||||
// emission
|
||||
if (m.emissiveTexture != null)
|
||||
{
|
||||
yield return GetTextureParam.CreateSRGB(gltf, m.emissiveTexture.index);
|
||||
yield return GetTextureParam.CreateSRGB(parser, m.emissiveTexture.index);
|
||||
}
|
||||
|
||||
// normal
|
||||
if (m.normalTexture != null)
|
||||
{
|
||||
yield return PBRMaterialItem.NormalTexture(gltf, m);
|
||||
yield return PBRMaterialItem.NormalTexture(parser, m);
|
||||
}
|
||||
|
||||
// occlusion
|
||||
@@ -46,16 +46,16 @@ namespace UniGLTF
|
||||
// metallicSmooth and occlusion
|
||||
if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue)
|
||||
{
|
||||
yield return PBRMaterialItem.StandardTexture(gltf, m);
|
||||
yield return PBRMaterialItem.StandardTexture(parser, m);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<GetTextureParam> Enumerate(glTF gltf)
|
||||
public static IEnumerable<GetTextureParam> Enumerate(GltfParser parser)
|
||||
{
|
||||
var used = new HashSet<GetTextureParam>();
|
||||
foreach (var material in gltf.materials)
|
||||
foreach (var material in parser.GLTF.materials)
|
||||
{
|
||||
foreach (var textureInfo in EnumerateTextures(gltf, material))
|
||||
foreach (var textureInfo in EnumerateTextures(parser, material))
|
||||
{
|
||||
if(used.Add(textureInfo)){
|
||||
yield return textureInfo;
|
||||
|
||||
@@ -8,14 +8,13 @@ namespace UniGLTF
|
||||
{
|
||||
public class MaterialFactory : IDisposable
|
||||
{
|
||||
glTF m_gltf;
|
||||
IStorage m_storage;
|
||||
GltfParser m_parser;
|
||||
Dictionary<string, Material> m_externalMap;
|
||||
public bool TryGetExternal(int index, out Material external)
|
||||
{
|
||||
if (m_externalMap != null)
|
||||
{
|
||||
var gltfMaterial = m_gltf.materials[index];
|
||||
var gltfMaterial = m_parser.GLTF.materials[index];
|
||||
if (m_externalMap.TryGetValue(gltfMaterial.name, out external))
|
||||
{
|
||||
return true;
|
||||
@@ -26,11 +25,9 @@ namespace UniGLTF
|
||||
return false;
|
||||
}
|
||||
|
||||
public MaterialFactory(glTF gltf, IStorage storage,
|
||||
IEnumerable<(string, UnityEngine.Object)> externalMap)
|
||||
public MaterialFactory(GltfParser parser, IEnumerable<(string, UnityEngine.Object)> externalMap)
|
||||
{
|
||||
m_gltf = gltf;
|
||||
m_storage = storage;
|
||||
m_parser = parser;
|
||||
if (externalMap != null)
|
||||
{
|
||||
m_externalMap = externalMap
|
||||
@@ -41,7 +38,7 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
public delegate Task<Material> CreateMaterialAsyncFunc(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture);
|
||||
public delegate Task<Material> CreateMaterialAsyncFunc(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture);
|
||||
CreateMaterialAsyncFunc m_createMaterialAsync;
|
||||
public CreateMaterialAsyncFunc CreateMaterialAsync
|
||||
{
|
||||
@@ -138,15 +135,15 @@ namespace UniGLTF
|
||||
/// <returns></returns>
|
||||
public async Task LoadMaterialsAsync(IAwaitCaller awaitCaller, GetTextureAsyncFunc getTexture)
|
||||
{
|
||||
if (m_gltf.materials == null || m_gltf.materials.Count == 0)
|
||||
if (m_parser.GLTF.materials == null || m_parser.GLTF.materials.Count == 0)
|
||||
{
|
||||
// no material. work around.
|
||||
var material = await CreateMaterialAsync(awaitCaller, m_gltf, 0, getTexture);
|
||||
var material = await CreateMaterialAsync(awaitCaller, m_parser, 0, getTexture);
|
||||
m_materials.Add(new MaterialLoadInfo(material, false));
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_gltf.materials.Count; ++i)
|
||||
for (int i = 0; i < m_parser.GLTF.materials.Count; ++i)
|
||||
{
|
||||
if (TryGetExternal(i, out Material material))
|
||||
{
|
||||
@@ -154,7 +151,7 @@ namespace UniGLTF
|
||||
continue;
|
||||
}
|
||||
|
||||
material = await CreateMaterialAsync(awaitCaller, m_gltf, i, getTexture);
|
||||
material = await CreateMaterialAsync(awaitCaller, m_parser, i, getTexture);
|
||||
m_materials.Add(new MaterialLoadInfo(material, false));
|
||||
}
|
||||
}
|
||||
@@ -196,22 +193,22 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
public static Task<Material> DefaultCreateMaterialAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture)
|
||||
public static Task<Material> DefaultCreateMaterialAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture)
|
||||
{
|
||||
if (i < 0 || i >= gltf.materials.Count)
|
||||
if (i < 0 || i >= parser.GLTF.materials.Count)
|
||||
{
|
||||
UnityEngine.Debug.LogWarning("glTFMaterial is empty");
|
||||
return PBRMaterialItem.CreateAsync(awaitCaller, gltf, i, getTexture);
|
||||
return PBRMaterialItem.CreateAsync(awaitCaller, parser, i, getTexture);
|
||||
}
|
||||
var x = gltf.materials[i];
|
||||
var x = parser.GLTF.materials[i];
|
||||
|
||||
if (glTF_KHR_materials_unlit.IsEnable(x))
|
||||
{
|
||||
var hasVertexColor = gltf.MaterialHasVertexColor(i);
|
||||
return UnlitMaterialItem.CreateAsync(awaitCaller, gltf, i, getTexture, hasVertexColor);
|
||||
var hasVertexColor = parser.GLTF.MaterialHasVertexColor(i);
|
||||
return UnlitMaterialItem.CreateAsync(awaitCaller, parser, i, getTexture, hasVertexColor);
|
||||
}
|
||||
|
||||
return PBRMaterialItem.CreateAsync(awaitCaller, gltf, i, getTexture);
|
||||
return PBRMaterialItem.CreateAsync(awaitCaller, parser, i, getTexture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -231,8 +228,14 @@ namespace UniGLTF
|
||||
name = "texture_0"
|
||||
}
|
||||
},
|
||||
images = new List<glTFImage>{
|
||||
new glTFImage{
|
||||
name = "image_0",
|
||||
mimeType = "image/png",
|
||||
}
|
||||
},
|
||||
};
|
||||
var task = DefaultCreateMaterialAsync(default(ImmediateCaller), gltf, i, null);
|
||||
var task = DefaultCreateMaterialAsync(default(ImmediateCaller), new GltfParser{GLTF = gltf}, i, null);
|
||||
return task.Result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,12 +43,12 @@ namespace UniGLTF
|
||||
Transparent
|
||||
}
|
||||
|
||||
public static GetTextureParam BaseColorTexture(glTF gltf, glTFMaterial src)
|
||||
public static GetTextureParam BaseColorTexture(GltfParser parser, glTFMaterial src)
|
||||
{
|
||||
return GetTextureParam.CreateSRGB(gltf, src.pbrMetallicRoughness.baseColorTexture.index);
|
||||
return GetTextureParam.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index);
|
||||
}
|
||||
|
||||
public static GetTextureParam StandardTexture(glTF gltf, glTFMaterial src)
|
||||
public static GetTextureParam StandardTexture(GltfParser parser, glTFMaterial src)
|
||||
{
|
||||
var metallicFactor = 1.0f;
|
||||
var roughnessFactor = 1.0f;
|
||||
@@ -57,38 +57,38 @@ namespace UniGLTF
|
||||
metallicFactor = src.pbrMetallicRoughness.metallicFactor;
|
||||
roughnessFactor = src.pbrMetallicRoughness.roughnessFactor;
|
||||
}
|
||||
return GetTextureParam.CreateStandard(gltf,
|
||||
return GetTextureParam.CreateStandard(parser,
|
||||
src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
|
||||
src.occlusionTexture?.index,
|
||||
metallicFactor,
|
||||
roughnessFactor);
|
||||
}
|
||||
|
||||
public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src)
|
||||
public static GetTextureParam NormalTexture(GltfParser parser, glTFMaterial src)
|
||||
{
|
||||
return GetTextureParam.CreateNormal(gltf, src.normalTexture.index);
|
||||
return GetTextureParam.CreateNormal(parser, src.normalTexture.index);
|
||||
}
|
||||
|
||||
public static async Task<Material> CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture)
|
||||
public static async Task<Material> CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture)
|
||||
{
|
||||
if (getTexture == null)
|
||||
{
|
||||
getTexture = (IAwaitCaller _awaitCaller, glTF _gltf, GetTextureParam _param) => Task.FromResult<Texture2D>(null);
|
||||
}
|
||||
|
||||
if (i < 0 || i >= gltf.materials.Count)
|
||||
if (i < 0 || i >= parser.GLTF.materials.Count)
|
||||
{
|
||||
return MaterialFactory.CreateMaterial(i, null, ShaderName);
|
||||
}
|
||||
|
||||
var src = gltf.materials[i];
|
||||
var src = parser.GLTF.materials[i];
|
||||
var material = MaterialFactory.CreateMaterial(i, src, ShaderName);
|
||||
var standardParam = default(GetTextureParam);
|
||||
if (src.pbrMetallicRoughness != null || src.occlusionTexture != null)
|
||||
{
|
||||
if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null)
|
||||
{
|
||||
standardParam = StandardTexture(gltf, src);
|
||||
standardParam = StandardTexture(parser, src);
|
||||
}
|
||||
if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
|
||||
{
|
||||
@@ -98,7 +98,7 @@ namespace UniGLTF
|
||||
|
||||
if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1)
|
||||
{
|
||||
material.mainTexture = await getTexture(awaitCaller, gltf, BaseColorTexture(gltf, src));
|
||||
material.mainTexture = await getTexture(awaitCaller, parser.GLTF, BaseColorTexture(parser, src));
|
||||
|
||||
// Texture Offset and Scale
|
||||
MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
|
||||
@@ -108,7 +108,7 @@ namespace UniGLTF
|
||||
{
|
||||
material.EnableKeyword("_METALLICGLOSSMAP");
|
||||
|
||||
var texture = await getTexture(awaitCaller, gltf, standardParam);
|
||||
var texture = await getTexture(awaitCaller, parser.GLTF, standardParam);
|
||||
if (texture != null)
|
||||
{
|
||||
material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture);
|
||||
@@ -131,7 +131,7 @@ namespace UniGLTF
|
||||
if (src.normalTexture != null && src.normalTexture.index != -1)
|
||||
{
|
||||
material.EnableKeyword("_NORMALMAP");
|
||||
var texture = await getTexture(awaitCaller, gltf, NormalTexture(gltf, src));
|
||||
var texture = await getTexture(awaitCaller, parser.GLTF, NormalTexture(parser, src));
|
||||
if (texture != null)
|
||||
{
|
||||
material.SetTexture(GetTextureParam.NORMAL_PROP, texture);
|
||||
@@ -144,7 +144,7 @@ namespace UniGLTF
|
||||
|
||||
if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
|
||||
{
|
||||
var texture = await getTexture(awaitCaller, gltf, standardParam);
|
||||
var texture = await getTexture(awaitCaller, parser.GLTF, standardParam);
|
||||
if (texture != null)
|
||||
{
|
||||
material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture);
|
||||
@@ -168,7 +168,7 @@ namespace UniGLTF
|
||||
|
||||
if (src.emissiveTexture != null && src.emissiveTexture.index != -1)
|
||||
{
|
||||
var texture = await getTexture(awaitCaller, gltf, GetTextureParam.CreateSRGB(gltf, src.emissiveTexture.index));
|
||||
var texture = await getTexture(awaitCaller, parser.GLTF, GetTextureParam.CreateSRGB(parser, src.emissiveTexture.index));
|
||||
if (texture != null)
|
||||
{
|
||||
material.SetTexture("_EmissionMap", texture);
|
||||
|
||||
@@ -8,20 +8,20 @@ namespace UniGLTF
|
||||
{
|
||||
public const string ShaderName = "UniGLTF/UniUnlit";
|
||||
|
||||
public static async Task<Material> CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture, bool hasVertexColor)
|
||||
public static async Task<Material> CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture, bool hasVertexColor)
|
||||
{
|
||||
if (getTexture == null)
|
||||
{
|
||||
getTexture = (_x, _y, _z) => Task.FromResult<Texture2D>(default);
|
||||
}
|
||||
|
||||
var src = gltf.materials[i];
|
||||
var src = parser.GLTF.materials[i];
|
||||
var material = MaterialFactory.CreateMaterial(i, src, ShaderName);
|
||||
|
||||
// texture
|
||||
if (src.pbrMetallicRoughness.baseColorTexture != null)
|
||||
{
|
||||
material.mainTexture = await getTexture(awaitCaller, gltf, GetTextureParam.CreateSRGB(gltf, src.pbrMetallicRoughness.baseColorTexture.index));
|
||||
material.mainTexture = await getTexture(awaitCaller, parser.GLTF, GetTextureParam.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index));
|
||||
|
||||
// Texture Offset and Scale
|
||||
MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex");
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public delegate Task<ArraySegment<byte>> GetTextureBytesAsync();
|
||||
|
||||
/// <summary>
|
||||
/// STANDARD(Pbr) texture = occlusion + metallic + smoothness
|
||||
/// </summary>
|
||||
@@ -38,98 +44,285 @@ namespace UniGLTF
|
||||
return src;
|
||||
}
|
||||
}
|
||||
|
||||
readonly string m_name;
|
||||
|
||||
public string GltflName => m_name;
|
||||
|
||||
public string ConvertedName
|
||||
|
||||
public struct NameExt
|
||||
{
|
||||
get
|
||||
public readonly string GltfName;
|
||||
public readonly string ConvertedName;
|
||||
|
||||
public readonly string Ext;
|
||||
public string Uri;
|
||||
|
||||
public NameExt(string gltfName, string convertedName, string ext, string uri)
|
||||
{
|
||||
switch (TextureType)
|
||||
GltfName = gltfName;
|
||||
ConvertedName = convertedName;
|
||||
Ext = ext;
|
||||
Uri = uri;
|
||||
}
|
||||
|
||||
public string GltfFileName => $"{GltfName}{Ext}";
|
||||
|
||||
public string ConvertedFileName => $"{ConvertedName}.png";
|
||||
|
||||
public static NameExt Create(glTF gltf, int textureIndex, TextureTypes textureType)
|
||||
{
|
||||
if (textureIndex < 0 || textureIndex >= gltf.textures.Count)
|
||||
{
|
||||
case TextureTypes.StandardMap: return $"{m_name}{STANDARD_SUFFIX}";
|
||||
case TextureTypes.NormalMap: return $"{m_name}{NORMAL_SUFFIX}";
|
||||
default: return m_name;
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
var gltfTexture = gltf.textures[textureIndex];
|
||||
if(gltfTexture.source < 0 || gltfTexture.source >=gltf.images.Count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
var gltfImage = gltf.images[gltfTexture.source];
|
||||
return new NameExt(gltfTexture.name, Convert(gltfTexture.name, textureType), gltfImage.GetExt(), gltfImage.uri);
|
||||
}
|
||||
|
||||
static string Convert(string name, TextureTypes textureType)
|
||||
{
|
||||
switch (textureType)
|
||||
{
|
||||
case TextureTypes.StandardMap: return $"{name}{STANDARD_SUFFIX}";
|
||||
case TextureTypes.NormalMap: return $"{name}{NORMAL_SUFFIX}";
|
||||
default: return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public readonly NameExt Name;
|
||||
public string GltfName => Name.GltfName;
|
||||
public string GltfFileName => Name.GltfFileName;
|
||||
public string ConvertedName => Name.ConvertedName;
|
||||
public string ConvertedFileName => Name.ConvertedFileName;
|
||||
public string Uri => Name.Uri;
|
||||
|
||||
public struct TextureSamplerParam
|
||||
{
|
||||
public enum TextureWrapType
|
||||
{
|
||||
All,
|
||||
U,
|
||||
V,
|
||||
W,
|
||||
}
|
||||
|
||||
public (TextureWrapType, TextureWrapMode)[] WrapModes;
|
||||
public FilterMode FilterMode;
|
||||
|
||||
public static TextureSamplerParam Create(glTF gltf, int index)
|
||||
{
|
||||
var gltfTexture = gltf.textures[index];
|
||||
if(gltfTexture.sampler<0 || gltfTexture.sampler>= gltf.samplers.Count)
|
||||
{
|
||||
// default
|
||||
return new TextureSamplerParam
|
||||
{
|
||||
FilterMode = FilterMode.Bilinear,
|
||||
WrapModes = new (TextureWrapType, TextureWrapMode)[]{},
|
||||
};
|
||||
}
|
||||
|
||||
var gltfSampler = gltf.samplers[gltfTexture.sampler];
|
||||
return new TextureSamplerParam
|
||||
{
|
||||
WrapModes = GetUnityWrapMode(gltfSampler).ToArray(),
|
||||
FilterMode = ImportFilterMode(gltfSampler.minFilter),
|
||||
};
|
||||
}
|
||||
|
||||
public static IEnumerable<(TextureWrapType, TextureWrapMode)> GetUnityWrapMode(glTFTextureSampler sampler)
|
||||
{
|
||||
if (sampler.wrapS == sampler.wrapT)
|
||||
{
|
||||
switch (sampler.wrapS)
|
||||
{
|
||||
case glWrap.NONE: // default
|
||||
yield return (TextureWrapType.All, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.CLAMP_TO_EDGE:
|
||||
yield return (TextureWrapType.All, TextureWrapMode.Clamp);
|
||||
break;
|
||||
|
||||
case glWrap.REPEAT:
|
||||
yield return (TextureWrapType.All, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.MIRRORED_REPEAT:
|
||||
yield return (TextureWrapType.All, TextureWrapMode.Mirror);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (sampler.wrapS)
|
||||
{
|
||||
case glWrap.NONE: // default
|
||||
yield return (TextureWrapType.U, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.CLAMP_TO_EDGE:
|
||||
yield return (TextureWrapType.U, TextureWrapMode.Clamp);
|
||||
break;
|
||||
|
||||
case glWrap.REPEAT:
|
||||
yield return (TextureWrapType.U, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.MIRRORED_REPEAT:
|
||||
yield return (TextureWrapType.U, TextureWrapMode.Mirror);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
switch (sampler.wrapT)
|
||||
{
|
||||
case glWrap.NONE: // default
|
||||
yield return (TextureWrapType.V, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.CLAMP_TO_EDGE:
|
||||
yield return (TextureWrapType.V, TextureWrapMode.Clamp);
|
||||
break;
|
||||
|
||||
case glWrap.REPEAT:
|
||||
yield return (TextureWrapType.V, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.MIRRORED_REPEAT:
|
||||
yield return (TextureWrapType.V, TextureWrapMode.Mirror);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static FilterMode ImportFilterMode(glFilter filterMode)
|
||||
{
|
||||
switch (filterMode)
|
||||
{
|
||||
case glFilter.NEAREST:
|
||||
case glFilter.NEAREST_MIPMAP_LINEAR:
|
||||
case glFilter.NEAREST_MIPMAP_NEAREST:
|
||||
return FilterMode.Point;
|
||||
|
||||
case glFilter.NONE:
|
||||
case glFilter.LINEAR:
|
||||
case glFilter.LINEAR_MIPMAP_NEAREST:
|
||||
return FilterMode.Bilinear;
|
||||
|
||||
case glFilter.LINEAR_MIPMAP_LINEAR:
|
||||
return FilterMode.Trilinear;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TextureSamplerParam Sampler;
|
||||
|
||||
public readonly TextureTypes TextureType;
|
||||
public readonly float MetallicFactor;
|
||||
public readonly float RoughnessFactor;
|
||||
public readonly ushort? Index0;
|
||||
public readonly ushort? Index1;
|
||||
public readonly ushort? Index2;
|
||||
public readonly ushort? Index3;
|
||||
public readonly ushort? Index4;
|
||||
public readonly ushort? Index5;
|
||||
|
||||
public readonly GetTextureBytesAsync Index0;
|
||||
public readonly GetTextureBytesAsync Index1;
|
||||
public readonly GetTextureBytesAsync Index2;
|
||||
public readonly GetTextureBytesAsync Index3;
|
||||
public readonly GetTextureBytesAsync Index4;
|
||||
public readonly GetTextureBytesAsync Index5;
|
||||
|
||||
/// <summary>
|
||||
/// この種類は RGB チャンネルの組み換えが必用
|
||||
/// </summary>
|
||||
public bool ExtractConverted => TextureType == TextureTypes.StandardMap;
|
||||
|
||||
public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, float roughnessFactor, int? i0, int? i1, int? i2, int? i3, int? i4, int? i5)
|
||||
public GetTextureParam(NameExt name, TextureSamplerParam sampler, TextureTypes textureType, float metallicFactor, float roughnessFactor,
|
||||
GetTextureBytesAsync i0,
|
||||
GetTextureBytesAsync i1,
|
||||
GetTextureBytesAsync i2,
|
||||
GetTextureBytesAsync i3,
|
||||
GetTextureBytesAsync i4,
|
||||
GetTextureBytesAsync i5)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
m_name = name;
|
||||
|
||||
Name = name;
|
||||
Sampler = sampler;
|
||||
TextureType = textureType;
|
||||
MetallicFactor = metallicFactor;
|
||||
RoughnessFactor = roughnessFactor;
|
||||
Index0 = (ushort?)i0;
|
||||
Index1 = (ushort?)i1;
|
||||
Index2 = (ushort?)i2;
|
||||
Index3 = (ushort?)i3;
|
||||
Index4 = (ushort?)i4;
|
||||
Index5 = (ushort?)i5;
|
||||
Index0 = i0;
|
||||
Index1 = i1;
|
||||
Index2 = i2;
|
||||
Index3 = i3;
|
||||
Index4 = i4;
|
||||
Index5 = i5;
|
||||
}
|
||||
|
||||
public static GetTextureParam CreateSRGB(glTF gltf, int textureIndex)
|
||||
public static GetTextureParam CreateSRGB(GltfParser parser, int textureIndex)
|
||||
{
|
||||
var name = gltf.textures[textureIndex].name;
|
||||
return new GetTextureParam(name, TextureTypes.sRGB, default, default, textureIndex, default, default, default, default, default);
|
||||
var name = NameExt.Create(parser.GLTF, textureIndex, TextureTypes.sRGB);
|
||||
var sampler = TextureSamplerParam.Create(parser.GLTF, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex);
|
||||
return new GetTextureParam(name, sampler, TextureTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
}
|
||||
|
||||
public static GetTextureParam Create(glTF gltf, int index, string prop, float metallicFactor, float roughnessFactor)
|
||||
public static GetTextureParam CreateNormal(GltfParser parser, int textureIndex)
|
||||
{
|
||||
var name = NameExt.Create(parser.GLTF, textureIndex, TextureTypes.NormalMap);
|
||||
var sampler = TextureSamplerParam.Create(parser.GLTF, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex);
|
||||
return new GetTextureParam(name, sampler, TextureTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
}
|
||||
|
||||
public static GetTextureParam CreateStandard(GltfParser parser, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, float metallicFactor, float roughnessFactor)
|
||||
{
|
||||
NameExt name = default;
|
||||
|
||||
GetTextureBytesAsync getMetallicRoughnessAsync = default;
|
||||
TextureSamplerParam sampler = default;
|
||||
if (metallicRoughnessTextureIndex.HasValue)
|
||||
{
|
||||
name = NameExt.Create(parser.GLTF, metallicRoughnessTextureIndex.Value, TextureTypes.StandardMap);
|
||||
sampler = TextureSamplerParam.Create(parser.GLTF, metallicRoughnessTextureIndex.Value);
|
||||
getMetallicRoughnessAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value);
|
||||
}
|
||||
|
||||
GetTextureBytesAsync getOcclusionAsync = default;
|
||||
if (occlusionTextureIndex.HasValue)
|
||||
{
|
||||
if(string.IsNullOrEmpty(name.GltfName)){
|
||||
name = NameExt.Create(parser.GLTF, occlusionTextureIndex.Value, TextureTypes.StandardMap);
|
||||
}
|
||||
sampler = TextureSamplerParam.Create(parser.GLTF, occlusionTextureIndex.Value);
|
||||
getOcclusionAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value);
|
||||
}
|
||||
|
||||
return new GetTextureParam(name, sampler, TextureTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default);
|
||||
}
|
||||
|
||||
public static GetTextureParam Create(GltfParser parser, int index, string prop, float metallicFactor, float roughnessFactor)
|
||||
{
|
||||
switch (prop)
|
||||
{
|
||||
case NORMAL_PROP:
|
||||
return CreateNormal(gltf, index);
|
||||
return CreateNormal(parser, index);
|
||||
|
||||
case OCCLUSION_PROP:
|
||||
case METALLIC_GLOSS_PROP:
|
||||
return CreateStandard(gltf, index, default, metallicFactor, roughnessFactor);
|
||||
return CreateStandard(parser, index, default, metallicFactor, roughnessFactor);
|
||||
|
||||
default:
|
||||
return CreateSRGB(gltf, index);
|
||||
return CreateSRGB(parser, index);
|
||||
}
|
||||
}
|
||||
|
||||
public static GetTextureParam CreateNormal(glTF gltf, int textureIndex)
|
||||
{
|
||||
var name = gltf.textures[textureIndex].name;
|
||||
return new GetTextureParam(name, TextureTypes.NormalMap, default, default, textureIndex, default, default, default, default, default);
|
||||
}
|
||||
|
||||
public static GetTextureParam CreateStandard(glTF gltf, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, float metallicFactor, float roughnessFactor)
|
||||
{
|
||||
string name = default;
|
||||
if (metallicRoughnessTextureIndex.HasValue)
|
||||
{
|
||||
name = gltf.textures[metallicRoughnessTextureIndex.Value].name;
|
||||
}
|
||||
else if (occlusionTextureIndex.HasValue)
|
||||
{
|
||||
name = gltf.textures[occlusionTextureIndex.Value].name;
|
||||
}
|
||||
return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, roughnessFactor, metallicRoughnessTextureIndex, occlusionTextureIndex, default, default, default, default);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public static class GltfTextureLoader
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c541795ac7ae51d468dfff535e5333cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -52,12 +52,12 @@ namespace UniGLTF
|
||||
|
||||
public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external)
|
||||
{
|
||||
if (param.Index0.HasValue && ExternalMap != null)
|
||||
if (param.Index0!=null && ExternalMap != null)
|
||||
{
|
||||
var cacheName = param.ConvertedName;
|
||||
if (param.TextureType == GetTextureParam.TextureTypes.NormalMap)
|
||||
{
|
||||
cacheName = param.GltflName;
|
||||
cacheName = param.GltfName;
|
||||
if (m_textureCache.TryGetValue(cacheName, out TextureLoadInfo normalInfo))
|
||||
{
|
||||
external = normalInfo.Texture;
|
||||
@@ -151,44 +151,69 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
async Task<TextureLoadInfo> GetOrCreateBaseTexture(IAwaitCaller awaitCaller, glTF gltf, int textureIndex, bool used)
|
||||
async Task<TextureLoadInfo> GetOrCreateBaseTexture(IAwaitCaller awaitCaller, GetTextureParam param, GetTextureBytesAsync getTextureBytesAsync, RenderTextureReadWrite colorSpace, bool used)
|
||||
{
|
||||
var name = gltf.textures[textureIndex].name;
|
||||
var name = param.GltfName;
|
||||
if (m_textureCache.TryGetValue(name, out TextureLoadInfo cacheInfo))
|
||||
{
|
||||
return cacheInfo;
|
||||
}
|
||||
|
||||
// not found. load new
|
||||
var imageBytes = await awaitCaller.Run(() =>
|
||||
{
|
||||
var imageIndex = gltf.textures[textureIndex].source;
|
||||
var segments = gltf.GetImageBytes(m_storage, imageIndex);
|
||||
return ToArray(segments);
|
||||
});
|
||||
var imageBytes = await getTextureBytesAsync();
|
||||
|
||||
//
|
||||
// texture from image(png etc) bytes
|
||||
//
|
||||
var colorSpace = gltf.GetColorSpace(textureIndex);
|
||||
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear);
|
||||
texture.name = gltf.textures[textureIndex].name;
|
||||
texture.name = name;
|
||||
if (imageBytes != null)
|
||||
{
|
||||
texture.LoadImage(imageBytes);
|
||||
texture.LoadImage(ToArray(imageBytes));
|
||||
}
|
||||
|
||||
var sampler = gltf.GetSamplerFromTextureIndex(textureIndex);
|
||||
if (sampler != null)
|
||||
{
|
||||
TextureSamplerUtil.SetSampler(texture, sampler);
|
||||
}
|
||||
SetSampler(texture, param);
|
||||
|
||||
cacheInfo = new TextureLoadInfo(texture, used, false);
|
||||
m_textureCache.Add(name, cacheInfo);
|
||||
return cacheInfo;
|
||||
}
|
||||
|
||||
public static void SetSampler(Texture2D texture, GetTextureParam param)
|
||||
{
|
||||
if (texture == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var (key, value) in param.Sampler.WrapModes)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case GetTextureParam.TextureSamplerParam.TextureWrapType.All:
|
||||
texture.wrapMode = value;
|
||||
break;
|
||||
|
||||
case GetTextureParam.TextureSamplerParam.TextureWrapType.U:
|
||||
texture.wrapModeU = value;
|
||||
break;
|
||||
|
||||
case GetTextureParam.TextureSamplerParam.TextureWrapType.V:
|
||||
texture.wrapModeV = value;
|
||||
break;
|
||||
|
||||
case GetTextureParam.TextureSamplerParam.TextureWrapType.W:
|
||||
texture.wrapModeW = value;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
texture.filterMode = param.Sampler.FilterMode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// テクスチャーをロード、必要であれば変換して返す。
|
||||
/// 同じものはキャッシュを返す
|
||||
@@ -212,7 +237,7 @@ namespace UniGLTF
|
||||
{
|
||||
case GetTextureParam.TextureTypes.NormalMap:
|
||||
{
|
||||
var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false);
|
||||
var baseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index0, RenderTextureReadWrite.Linear, false);
|
||||
var converted = NormalConverter.Import(baseTexture.Texture);
|
||||
converted.name = param.ConvertedName;
|
||||
var info = new TextureLoadInfo(converted, true, false);
|
||||
@@ -223,14 +248,14 @@ namespace UniGLTF
|
||||
case GetTextureParam.TextureTypes.StandardMap:
|
||||
{
|
||||
TextureLoadInfo baseTexture = default;
|
||||
if (param.Index0.HasValue)
|
||||
if (param.Index0!=null)
|
||||
{
|
||||
baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false);
|
||||
baseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index0, RenderTextureReadWrite.Linear, false);
|
||||
}
|
||||
TextureLoadInfo occlusionBaseTexture = default;
|
||||
if (param.Index1.HasValue)
|
||||
if (param.Index1!=null)
|
||||
{
|
||||
occlusionBaseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index1.Value, false);
|
||||
occlusionBaseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index1, RenderTextureReadWrite.Linear, false);
|
||||
}
|
||||
var converted = OcclusionMetallicRoughnessConverter.Import(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture);
|
||||
converted.name = param.ConvertedName;
|
||||
@@ -241,7 +266,7 @@ namespace UniGLTF
|
||||
|
||||
default:
|
||||
{
|
||||
var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, true);
|
||||
var baseTexture = await GetOrCreateBaseTexture(awaitCaller, param, param.Index0, RenderTextureReadWrite.sRGB, true);
|
||||
return baseTexture.Texture;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,180 +7,6 @@ namespace UniGLTF
|
||||
{
|
||||
public static class TextureSamplerUtil
|
||||
{
|
||||
#region WrapMode
|
||||
public enum TextureWrapType
|
||||
{
|
||||
All,
|
||||
#if UNITY_2017_1_OR_NEWER
|
||||
U,
|
||||
V,
|
||||
W,
|
||||
#endif
|
||||
}
|
||||
|
||||
public static KeyValuePair<TextureWrapType, TextureWrapMode> TypeWithMode(TextureWrapType type, TextureWrapMode mode)
|
||||
{
|
||||
return new KeyValuePair<TextureWrapType, TextureWrapMode>(type, mode);
|
||||
}
|
||||
|
||||
public static IEnumerable<KeyValuePair<TextureWrapType, TextureWrapMode>> GetUnityWrapMode(glTFTextureSampler sampler)
|
||||
{
|
||||
#if UNITY_2017_1_OR_NEWER
|
||||
if (sampler.wrapS == sampler.wrapT)
|
||||
{
|
||||
switch (sampler.wrapS)
|
||||
{
|
||||
case glWrap.NONE: // default
|
||||
yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.CLAMP_TO_EDGE:
|
||||
yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Clamp);
|
||||
break;
|
||||
|
||||
case glWrap.REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.MIRRORED_REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Mirror);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (sampler.wrapS)
|
||||
{
|
||||
case glWrap.NONE: // default
|
||||
yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.CLAMP_TO_EDGE:
|
||||
yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Clamp);
|
||||
break;
|
||||
|
||||
case glWrap.REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.MIRRORED_REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.U, TextureWrapMode.Mirror);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
switch (sampler.wrapT)
|
||||
{
|
||||
case glWrap.NONE: // default
|
||||
yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.CLAMP_TO_EDGE:
|
||||
yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Clamp);
|
||||
break;
|
||||
|
||||
case glWrap.REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.MIRRORED_REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.V, TextureWrapMode.Mirror);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#else
|
||||
// Unity2017.1より前
|
||||
// * wrapSとwrapTの区別が無くてwrapしかない
|
||||
// * Mirrorが無い
|
||||
|
||||
switch (sampler.wrapS)
|
||||
{
|
||||
case glWrap.NONE: // default
|
||||
yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
case glWrap.CLAMP_TO_EDGE:
|
||||
case glWrap.MIRRORED_REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Clamp);
|
||||
break;
|
||||
|
||||
case glWrap.REPEAT:
|
||||
yield return TypeWithMode(TextureWrapType.All, TextureWrapMode.Repeat);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static FilterMode ImportFilterMode(glFilter filterMode)
|
||||
{
|
||||
switch (filterMode)
|
||||
{
|
||||
case glFilter.NEAREST:
|
||||
case glFilter.NEAREST_MIPMAP_LINEAR:
|
||||
case glFilter.NEAREST_MIPMAP_NEAREST:
|
||||
return FilterMode.Point;
|
||||
|
||||
case glFilter.NONE:
|
||||
case glFilter.LINEAR:
|
||||
case glFilter.LINEAR_MIPMAP_NEAREST:
|
||||
return FilterMode.Bilinear;
|
||||
|
||||
case glFilter.LINEAR_MIPMAP_LINEAR:
|
||||
return FilterMode.Trilinear;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetSampler(Texture2D texture, glTFTextureSampler sampler)
|
||||
{
|
||||
if (texture == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var kv in GetUnityWrapMode(sampler))
|
||||
{
|
||||
switch (kv.Key)
|
||||
{
|
||||
case TextureWrapType.All:
|
||||
texture.wrapMode = kv.Value;
|
||||
break;
|
||||
|
||||
#if UNITY_2017_1_OR_NEWER
|
||||
case TextureWrapType.U:
|
||||
texture.wrapModeU = kv.Value;
|
||||
break;
|
||||
|
||||
case TextureWrapType.V:
|
||||
texture.wrapModeV = kv.Value;
|
||||
break;
|
||||
|
||||
case TextureWrapType.W:
|
||||
texture.wrapModeW = kv.Value;
|
||||
break;
|
||||
#endif
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
texture.filterMode = ImportFilterMode(sampler.minFilter);
|
||||
}
|
||||
|
||||
#region Export
|
||||
public static glFilter ExportFilterMode(Texture texture)
|
||||
{
|
||||
|
||||
@@ -99,7 +99,7 @@ namespace UniGLTF
|
||||
}
|
||||
|
||||
// should unique
|
||||
var gltfTextures = GltfTextureEnumerator.Enumerate(parser.GLTF);
|
||||
var gltfTextures = GltfTextureEnumerator.Enumerate(parser).ToArray();
|
||||
var distinct = gltfTextures.Distinct().ToArray();
|
||||
Assert.True(gltfTextures.SequenceEqual(distinct));
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UniJSON;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
|
||||
@@ -83,7 +83,7 @@ namespace VRM
|
||||
using (var context = new VRMImporterContext(parser, map))
|
||||
{
|
||||
var editor = new VRMEditorImporterContext(context, prefabPath);
|
||||
foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF))
|
||||
foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser))
|
||||
{
|
||||
TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D));
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace VRM
|
||||
using (var context = new VRMImporterContext(parser, map))
|
||||
{
|
||||
var editor = new VRMEditorImporterContext(context, prefabPath);
|
||||
foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF))
|
||||
foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser))
|
||||
{
|
||||
TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D));
|
||||
}
|
||||
|
||||
@@ -297,7 +297,7 @@ namespace VRM
|
||||
meta.Title = gltfMeta.title;
|
||||
if (gltfMeta.texture >= 0)
|
||||
{
|
||||
meta.Thumbnail = await TextureFactory.GetTextureAsync(awaitCaller, GLTF, GetTextureParam.CreateSRGB(GLTF, gltfMeta.texture));
|
||||
meta.Thumbnail = await TextureFactory.GetTextureAsync(awaitCaller, GLTF, GetTextureParam.CreateSRGB(Parser, gltfMeta.texture));
|
||||
}
|
||||
meta.AllowedUser = gltfMeta.allowedUser;
|
||||
meta.ViolentUssage = gltfMeta.violentUssage;
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace VRM
|
||||
"VRM/UnlitTransparentZWrite",
|
||||
};
|
||||
|
||||
public static async Task<Material> CreateAsync(IAwaitCaller awaitCaller, glTF gltf, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture)
|
||||
public static async Task<Material> CreateAsync(IAwaitCaller awaitCaller, GltfParser parser, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture)
|
||||
{
|
||||
var item = vrmMaterial;
|
||||
var shaderName = item.shader;
|
||||
@@ -41,7 +41,7 @@ namespace VRM
|
||||
// Debug.LogWarningFormat("unknown shader {0}.", shaderName);
|
||||
// #endif
|
||||
}
|
||||
return await MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, gltf, m_index, getTexture);
|
||||
return await MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, parser, m_index, getTexture);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -49,7 +49,7 @@ namespace VRM
|
||||
//
|
||||
var material = new Material(shader);
|
||||
// use material.name, because material name may renamed in GltfParser.
|
||||
material.name = gltf.materials[m_index].name;
|
||||
material.name = parser.GLTF.materials[m_index].name;
|
||||
material.renderQueue = item.renderQueue;
|
||||
|
||||
foreach (var kv in item.floatProperties)
|
||||
@@ -73,8 +73,8 @@ namespace VRM
|
||||
}
|
||||
foreach (var kv in item.textureProperties)
|
||||
{
|
||||
var param = GetTextureParam.Create(gltf, kv.Value, kv.Key, 1, 1);
|
||||
var texture = await getTexture(awaitCaller, gltf, param);
|
||||
var param = GetTextureParam.Create(parser, kv.Value, kv.Key, 1, 1);
|
||||
var texture = await getTexture(awaitCaller, parser.GLTF, param);
|
||||
if (texture != null)
|
||||
{
|
||||
material.SetTexture(kv.Key, texture);
|
||||
@@ -115,15 +115,15 @@ namespace VRM
|
||||
m_materials = materials;
|
||||
}
|
||||
|
||||
public Task<Material> CreateMaterialAsync(IAwaitCaller awaitCaller, glTF gltf, int i, GetTextureAsyncFunc getTexture)
|
||||
public Task<Material> CreateMaterialAsync(IAwaitCaller awaitCaller, GltfParser parser, int i, GetTextureAsyncFunc getTexture)
|
||||
{
|
||||
if (i == 0 && m_materials.Count == 0)
|
||||
{
|
||||
// dummy
|
||||
return MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, gltf, i, getTexture);
|
||||
return MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, parser, i, getTexture);
|
||||
}
|
||||
|
||||
return MToonMaterialItem.CreateAsync(awaitCaller, gltf, i, m_materials[i], getTexture);
|
||||
return MToonMaterialItem.CreateAsync(awaitCaller, parser, i, m_materials[i], getTexture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace VRM
|
||||
m_vrm = vrm;
|
||||
}
|
||||
|
||||
public IEnumerable<GetTextureParam> Enumerate(glTF gltf)
|
||||
public IEnumerable<GetTextureParam> Enumerate(GltfParser parser)
|
||||
{
|
||||
for (int i = 0; i < gltf.materials.Count; ++i)
|
||||
for (int i = 0; i < parser.GLTF.materials.Count; ++i)
|
||||
{
|
||||
var vrmMaterial = m_vrm.materialProperties[i];
|
||||
if (vrmMaterial.shader == MToon.Utils.ShaderName)
|
||||
@@ -22,13 +22,13 @@ namespace VRM
|
||||
foreach (var kv in vrmMaterial.textureProperties)
|
||||
{
|
||||
// SRGB color or normalmap
|
||||
yield return GetTextureParam.Create(gltf, kv.Value, kv.Key, default, default);
|
||||
yield return GetTextureParam.Create(parser, kv.Value, kv.Key, default, default);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// PBR or Unlit
|
||||
foreach (var textureInfo in GltfTextureEnumerator.EnumerateTextures(gltf, gltf.materials[i]))
|
||||
foreach (var textureInfo in GltfTextureEnumerator.EnumerateTextures(parser, parser.GLTF.materials[i]))
|
||||
{
|
||||
yield return textureInfo;
|
||||
}
|
||||
@@ -38,7 +38,7 @@ namespace VRM
|
||||
// thumbnail
|
||||
if (m_vrm.meta != null && m_vrm.meta.texture != -1)
|
||||
{
|
||||
yield return GetTextureParam.CreateSRGB(gltf, m_vrm.meta.texture);
|
||||
yield return GetTextureParam.CreateSRGB(parser, m_vrm.meta.texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user