Merge pull request #1765 from ousttrue/fix/use_optional_for_index

glTFTexture.source type to int?
This commit is contained in:
ousttrue
2022-09-05 19:05:26 +09:00
committed by GitHub
16 changed files with 253 additions and 161 deletions

View File

@@ -475,9 +475,9 @@ public static void Serialize_gltf_textures_ITEM(JsonFormatter f, glTFTexture val
f.Value(value.sampler);
}
if(value.source>=0){
if(value.source.HasValue){
f.Key("source");
f.Value(value.source);
f.Value(value.source.Value);
}
if(value.extensions!=null){

View File

@@ -58,8 +58,7 @@ namespace UniGLTF
[JsonSchema(Minimum = 0)]
public int sampler;
[JsonSchema(Minimum = 0)]
public int source;
public int? source;
// empty schemas
public glTFExtension extensions;

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;
@@ -70,13 +71,15 @@ namespace UniGLTF
var vectors = new Dictionary<string, Vector4>();
var actions = new List<Action<Material>>();
var standardTexDesc = default(TextureDescriptor);
TextureDescriptor? standardTexDesc = default;
if (src.pbrMetallicRoughness != null || src.occlusionTexture != null)
{
if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null)
{
SubAssetKey key;
(key, standardTexDesc) = GltfPbrTextureImporter.StandardTexture(data, src);
if (GltfPbrTextureImporter.TryStandardTexture(data, src, out var key, out var desc))
{
standardTexDesc = desc;
}
}
if (src.pbrMetallicRoughness.baseColorFactor != null &&
@@ -90,15 +93,18 @@ namespace UniGLTF
if (src.pbrMetallicRoughness.baseColorTexture != null &&
src.pbrMetallicRoughness.baseColorTexture.index != -1)
{
var (key, textureParam) = GltfPbrTextureImporter.BaseColorTexture(data, src);
textureSlots.Add("_MainTex", textureParam);
if (GltfPbrTextureImporter.TryBaseColorTexture(data, src, out var key, out var desc))
{
textureSlots.Add("_MainTex", desc);
}
}
if (src.pbrMetallicRoughness.metallicRoughnessTexture != null &&
src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1 &&
standardTexDesc.HasValue)
{
actions.Add(material => material.EnableKeyword("_METALLICGLOSSMAP"));
textureSlots.Add("_MetallicGlossMap", standardTexDesc);
textureSlots.Add("_MetallicGlossMap", standardTexDesc.Value);
// Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
floatValues.Add("_Metallic", 1.0f);
floatValues.Add("_GlossMapScale", 1.0f);
@@ -113,14 +119,16 @@ namespace UniGLTF
if (src.normalTexture != null && src.normalTexture.index != -1)
{
actions.Add(material => material.EnableKeyword("_NORMALMAP"));
var (_, textureParam) = GltfPbrTextureImporter.NormalTexture(data, src);
textureSlots.Add("_BumpMap", textureParam);
floatValues.Add("_BumpScale", src.normalTexture.scale);
if (GltfPbrTextureImporter.TryNormalTexture(data, src, out var key, out var desc))
{
textureSlots.Add("_BumpMap", desc);
floatValues.Add("_BumpScale", src.normalTexture.scale);
}
}
if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
if (src.occlusionTexture != null && src.occlusionTexture.index != -1 && standardTexDesc.HasValue)
{
textureSlots.Add("_OcclusionMap", standardTexDesc);
textureSlots.Add("_OcclusionMap", standardTexDesc.Value);
floatValues.Add("_OcclusionStrength", src.occlusionTexture.strength);
}
@@ -152,8 +160,10 @@ namespace UniGLTF
if (src.emissiveTexture != null && src.emissiveTexture.index != -1)
{
var (key, textureParam) = GltfPbrTextureImporter.EmissiveTexture(data, src);
textureSlots.Add("_EmissionMap", textureParam);
if (GltfPbrTextureImporter.TryEmissiveTexture(data, src, out var key, out var desc))
{
textureSlots.Add("_EmissionMap", desc);
}
}
}

View File

@@ -42,11 +42,11 @@ namespace UniGLTF
// texture
if (src.pbrMetallicRoughness.baseColorTexture != null)
{
var (offset, scale) =
GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
var (key, textureParam) = GltfTextureImporter.CreateSrgb(data,
src.pbrMetallicRoughness.baseColorTexture.index, offset, scale);
textureSlots.Add("_MainTex", textureParam);
var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
if (GltfTextureImporter.TryCreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale, out var key, out var desc))
{
textureSlots.Add("_MainTex", desc);
}
}
matDesc = new MaterialDescriptor(

View File

@@ -42,13 +42,19 @@ namespace UniGLTF
var actions = new List<Action<Material>>();
var src = data.GLTF.materials[i];
var standardTexDesc = default(TextureDescriptor);
TextureDescriptor? standardTexDesc = default;
if (src.pbrMetallicRoughness != null || src.occlusionTexture != null)
{
if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null)
{
SubAssetKey key;
(key, standardTexDesc) = GltfPbrTextureImporter.StandardTexture(data, src);
if (GltfPbrTextureImporter.TryStandardTexture(data, src, out var key, out var desc))
{
if (string.IsNullOrEmpty(desc.UnityObjectName))
{
throw new ArgumentNullException();
}
standardTexDesc = desc;
}
}
if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
@@ -61,15 +67,17 @@ namespace UniGLTF
if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1)
{
var (key, textureParam) = GltfPbrTextureImporter.BaseColorTexture(data, src);
// from _MainTex !
textureSlots.Add("_BaseMap", textureParam);
if (GltfPbrTextureImporter.TryBaseColorTexture(data, src, out var key, out var desc))
{
// from _MainTex !
textureSlots.Add("_BaseMap", desc);
}
}
if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1)
if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1 && standardTexDesc.HasValue)
{
actions.Add(material => material.EnableKeyword("_METALLICGLOSSMAP"));
textureSlots.Add("_MetallicGlossMap", standardTexDesc);
textureSlots.Add("_MetallicGlossMap", standardTexDesc.Value);
// Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
floatValues.Add("_Metallic", 1.0f);
floatValues.Add("_GlossMapScale", 1.0f);
@@ -87,14 +95,16 @@ namespace UniGLTF
if (src.normalTexture != null && src.normalTexture.index != -1)
{
actions.Add(material => material.EnableKeyword("_NORMALMAP"));
var (key, textureParam) = GltfPbrTextureImporter.NormalTexture(data, src);
textureSlots.Add("_BumpMap", textureParam);
floatValues.Add("_BumpScale", src.normalTexture.scale);
if (GltfPbrTextureImporter.TryNormalTexture(data, src, out var key, out var desc))
{
textureSlots.Add("_BumpMap", desc);
floatValues.Add("_BumpScale", src.normalTexture.scale);
}
}
if (src.occlusionTexture != null && src.occlusionTexture.index != -1)
if (src.occlusionTexture != null && src.occlusionTexture.index != -1 && standardTexDesc.HasValue)
{
textureSlots.Add("_OcclusionMap", standardTexDesc);
textureSlots.Add("_OcclusionMap", standardTexDesc.Value);
floatValues.Add("_OcclusionStrength", src.occlusionTexture.strength);
}
@@ -125,8 +135,10 @@ namespace UniGLTF
if (src.emissiveTexture != null && src.emissiveTexture.index != -1)
{
var (key, textureParam) = GltfPbrTextureImporter.EmissiveTexture(data, src);
textureSlots.Add("_EmissionMap", textureParam);
if (GltfPbrTextureImporter.TryEmissiveTexture(data, src, out var key, out var desc))
{
textureSlots.Add("_EmissionMap", desc);
}
}
}

View File

@@ -172,16 +172,19 @@ namespace UniGLTF
for (var textureIdx = 0; textureIdx < GLTF.textures.Count; ++textureIdx)
{
var gltfTexture = GLTF.textures[textureIdx];
var gltfImage = GLTF.images[gltfTexture.source];
if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:"))
if (gltfTexture.source.HasValidIndex())
{
// from image uri
gltfTexture.name = Path.GetFileNameWithoutExtension(gltfImage.uri);
}
if (string.IsNullOrEmpty(gltfTexture.name))
{
// use image name
gltfTexture.name = gltfImage.name;
var gltfImage = GLTF.images[gltfTexture.source.Value];
if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:"))
{
// from image uri
gltfTexture.name = Path.GetFileNameWithoutExtension(gltfImage.uri);
}
if (string.IsNullOrEmpty(gltfTexture.name))
{
// use image name
gltfTexture.name = gltfImage.name;
}
}
if (string.IsNullOrEmpty(gltfTexture.name))
{

View File

@@ -15,7 +15,10 @@ namespace UniGLTF
// base color
if (m.pbrMetallicRoughness?.baseColorTexture != null)
{
yield return BaseColorTexture(data, m);
if (TryBaseColorTexture(data, m, out var key, out var desc))
{
yield return (key, desc);
}
}
// metallic roughness
@@ -28,13 +31,19 @@ namespace UniGLTF
// emission
if (m.emissiveTexture != null)
{
yield return EmissiveTexture(data, m);
if (TryEmissiveTexture(data, m, out var key, out var desc))
{
yield return (key, desc);
}
}
// normal
if (m.normalTexture != null)
{
yield return NormalTexture(data, m);
if (TryNormalTexture(data, m, out var key, out var desc))
{
yield return (key, desc);
}
}
// occlusion
@@ -47,17 +56,20 @@ namespace UniGLTF
// metallicSmooth and occlusion
if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue)
{
yield return StandardTexture(data, m);
if (TryStandardTexture(data, m, out var key, out var desc))
{
yield return (key, desc);
}
}
}
public static (SubAssetKey, TextureDescriptor) BaseColorTexture(GltfData data, glTFMaterial src)
public static bool TryBaseColorTexture(GltfData data, glTFMaterial src, out SubAssetKey key, out TextureDescriptor desc)
{
var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture);
return GltfTextureImporter.CreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale);
return GltfTextureImporter.TryCreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale, out key, out desc);
}
public static (SubAssetKey, TextureDescriptor) StandardTexture(GltfData data, glTFMaterial src)
public static bool TryStandardTexture(GltfData data, glTFMaterial src, out SubAssetKey key, out TextureDescriptor desc)
{
var metallicFactor = 1.0f;
var roughnessFactor = 1.0f;
@@ -67,25 +79,24 @@ namespace UniGLTF
roughnessFactor = src.pbrMetallicRoughness.roughnessFactor;
}
var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture);
return GltfTextureImporter.CreateStandard(data,
return GltfTextureImporter.TryCreateStandard(data,
src.pbrMetallicRoughness?.metallicRoughnessTexture?.index,
src.occlusionTexture?.index,
offset, scale,
metallicFactor,
roughnessFactor);
roughnessFactor, out key, out desc);
}
public static (SubAssetKey, TextureDescriptor) NormalTexture(GltfData data, glTFMaterial src)
public static bool TryNormalTexture(GltfData data, glTFMaterial src, out SubAssetKey key, out TextureDescriptor desc)
{
var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.normalTexture);
return GltfTextureImporter.CreateNormal(data, src.normalTexture.index, offset, scale);
return GltfTextureImporter.TryCreateNormal(data, src.normalTexture.index, offset, scale, out key, out desc);
}
public static (SubAssetKey, TextureDescriptor) EmissiveTexture(GltfData data, glTFMaterial src)
public static bool TryEmissiveTexture(GltfData data, glTFMaterial src, out SubAssetKey key, out TextureDescriptor desc)
{
var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.emissiveTexture);
return GltfTextureImporter.CreateSrgb(data, src.emissiveTexture.index, offset, scale);
return GltfTextureImporter.TryCreateSrgb(data, src.emissiveTexture.index, offset, scale, out key, out desc);
}
}
}

View File

@@ -35,13 +35,20 @@ namespace UniGLTF
return (texDesc.SubAssetKey, texDesc);
}
public static (SubAssetKey, TextureDescriptor) CreateSrgb(GltfData data, int textureIndex, Vector2 offset, Vector2 scale)
public static bool TryCreateSrgb(GltfData data, int textureIndex, Vector2 offset, Vector2 scale, out SubAssetKey key, out TextureDescriptor desc)
{
var gltfTexture = data.GLTF.textures[textureIndex];
var gltfImage = data.GLTF.images[gltfTexture.source];
if (!gltfTexture.source.HasValidIndex())
{
key = default;
desc = default;
return false;
}
var gltfImage = data.GLTF.images[gltfTexture.source.Value];
var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfTexture.name, gltfImage.uri);
var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex);
var param = new TextureDescriptor(
desc = new TextureDescriptor(
name,
offset, scale,
sampler,
@@ -50,16 +57,24 @@ namespace UniGLTF
default,
() => Task.FromResult(GetImageBytesFromTextureIndex(data, textureIndex)),
default, default, default, default, default);
return (param.SubAssetKey, param);
key = desc.SubAssetKey;
return true;
}
public static (SubAssetKey, TextureDescriptor) CreateLinear(GltfData data, int textureIndex, Vector2 offset, Vector2 scale)
public static bool TryCreateLinear(GltfData data, int textureIndex, Vector2 offset, Vector2 scale, out SubAssetKey key, out TextureDescriptor desc)
{
var gltfTexture = data.GLTF.textures[textureIndex];
var gltfImage = data.GLTF.images[gltfTexture.source];
if (!gltfTexture.source.HasValidIndex())
{
key = default;
desc = default;
return false;
}
var gltfImage = data.GLTF.images[gltfTexture.source.Value];
var name = TextureImportName.GetUnityObjectName(TextureImportTypes.Linear, gltfTexture.name, gltfImage.uri);
var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex);
var param = new TextureDescriptor(
desc = new TextureDescriptor(
name,
offset,
scale,
@@ -69,16 +84,24 @@ namespace UniGLTF
default,
() => Task.FromResult(GetImageBytesFromTextureIndex(data, textureIndex)),
default, default, default, default, default);
return (param.SubAssetKey, param);
key = desc.SubAssetKey;
return true;
}
public static (SubAssetKey, TextureDescriptor) CreateNormal(GltfData data, int textureIndex, Vector2 offset, Vector2 scale)
public static bool TryCreateNormal(GltfData data, int textureIndex, Vector2 offset, Vector2 scale, out SubAssetKey key, out TextureDescriptor desc)
{
var gltfTexture = data.GLTF.textures[textureIndex];
var gltfImage = data.GLTF.images[gltfTexture.source];
if (!gltfTexture.source.HasValidIndex())
{
key = default;
desc = default;
return false;
}
var gltfImage = data.GLTF.images[gltfTexture.source.Value];
var name = TextureImportName.GetUnityObjectName(TextureImportTypes.NormalMap, gltfTexture.name, gltfImage.uri);
var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex);
var param = new TextureDescriptor(
desc = new TextureDescriptor(
name,
offset,
scale,
@@ -88,10 +111,11 @@ namespace UniGLTF
default,
() => Task.FromResult(GetImageBytesFromTextureIndex(data, textureIndex)),
default, default, default, default, default);
return (param.SubAssetKey, param);
key = desc.SubAssetKey;
return true;
}
public static (SubAssetKey, TextureDescriptor) CreateStandard(GltfData data, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, Vector2 offset, Vector2 scale, float metallicFactor, float roughnessFactor)
public static bool TryCreateStandard(GltfData data, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, Vector2 offset, Vector2 scale, float metallicFactor, float roughnessFactor, out SubAssetKey key, out TextureDescriptor desc)
{
string name = default;
@@ -100,24 +124,37 @@ namespace UniGLTF
if (metallicRoughnessTextureIndex.HasValue)
{
var gltfTexture = data.GLTF.textures[metallicRoughnessTextureIndex.Value];
name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source].uri);
sampler = TextureSamplerUtil.CreateSampler(data.GLTF, metallicRoughnessTextureIndex.Value);
getMetallicRoughnessAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, metallicRoughnessTextureIndex.Value));
if (gltfTexture.source.HasValidIndex())
{
name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source.Value].uri);
sampler = TextureSamplerUtil.CreateSampler(data.GLTF, metallicRoughnessTextureIndex.Value);
getMetallicRoughnessAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, metallicRoughnessTextureIndex.Value));
}
}
GetTextureBytesAsync getOcclusionAsync = default;
if (occlusionTextureIndex.HasValue)
{
var gltfTexture = data.GLTF.textures[occlusionTextureIndex.Value];
if (string.IsNullOrEmpty(name))
if (gltfTexture.source.HasValidIndex())
{
name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source].uri);
if (string.IsNullOrEmpty(name))
{
name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source.Value].uri);
}
sampler = TextureSamplerUtil.CreateSampler(data.GLTF, occlusionTextureIndex.Value);
getOcclusionAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, occlusionTextureIndex.Value));
}
sampler = TextureSamplerUtil.CreateSampler(data.GLTF, occlusionTextureIndex.Value);
getOcclusionAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, occlusionTextureIndex.Value));
}
var texDesc = new TextureDescriptor(
if (string.IsNullOrEmpty(name))
{
key = default;
desc = default;
return false;
}
desc = new TextureDescriptor(
name,
offset,
scale,
@@ -128,7 +165,13 @@ namespace UniGLTF
getMetallicRoughnessAsync,
getOcclusionAsync,
default, default, default, default);
return (texDesc.SubAssetKey, texDesc);
key = desc.SubAssetKey;
if (string.IsNullOrEmpty(desc.UnityObjectName))
{
throw new ArgumentNullException();
}
Debug.Log("${name}");
return true;
}
public static (Vector2, Vector2) GetTextureOffsetAndScale(glTFTextureInfo textureInfo)

View File

@@ -301,8 +301,10 @@ namespace VRM
meta.Title = gltfMeta.title;
if (gltfMeta.texture >= 0)
{
var (key, param) = GltfTextureImporter.CreateSrgb(Data, gltfMeta.texture, Vector2.zero, Vector2.one);
meta.Thumbnail = await TextureFactory.GetTextureAsync(param, awaitCaller) as Texture2D;
if (GltfTextureImporter.TryCreateSrgb(Data, gltfMeta.texture, Vector2.zero, Vector2.one, out var key, out var desc))
{
meta.Thumbnail = await TextureFactory.GetTextureAsync(desc, awaitCaller) as Texture2D;
}
}
meta.AllowedUser = gltfMeta.allowedUser;
meta.ViolentUssage = gltfMeta.violentUssage;

View File

@@ -85,9 +85,9 @@ namespace VRM
foreach (var kv in vrmMaterial.textureProperties)
{
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key,
out var texture))
out var key, out var desc))
{
textureSlots.Add(kv.Key, texture.Item2);
textureSlots.Add(kv.Key, desc);
}
}

View File

@@ -12,13 +12,13 @@ namespace VRM
var vrmMaterial = vrm.materialProperties[materialIdx];
foreach (var kv in vrmMaterial.textureProperties)
{
if (TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key, out var texture))
if (TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key, out var key, out var desc))
{
yield return texture;
yield return (key, desc);
}
}
}
public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_Material vrmMaterial, string textureKey, out (SubAssetKey, TextureDescriptor) texture)
public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_Material vrmMaterial, string textureKey, out SubAssetKey key, out TextureDescriptor desc)
{
// 任意の shader の import を許容する
if (/*vrmMaterial.shader == MToon.Utils.ShaderName &&*/ vrmMaterial.textureProperties.TryGetValue(textureKey, out var textureIdx))
@@ -33,16 +33,14 @@ namespace VRM
switch (textureKey)
{
case MToon.Utils.PropBumpMap:
texture = GltfTextureImporter.CreateNormal(data, textureIdx, offset, scale);
break;
return GltfTextureImporter.TryCreateNormal(data, textureIdx, offset, scale, out key, out desc);
default:
texture = GltfTextureImporter.CreateSrgb(data, textureIdx, offset, scale);
break;
return GltfTextureImporter.TryCreateSrgb(data, textureIdx, offset, scale, out key, out desc);
}
return true;
}
texture = default;
key = default;
desc = default;
return false;
}

View File

@@ -48,9 +48,9 @@ namespace VRM
if (vrmMaterial.textureProperties.ContainsKey(UnlitTransparentZWriteMainTexturePropName))
{
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial,
UnlitTransparentZWriteMainTexturePropName, out var texture))
UnlitTransparentZWriteMainTexturePropName, out var key, out var desc))
{
textureSlots.Add(MToon.Utils.PropMainTex, texture.Item2);
textureSlots.Add(MToon.Utils.PropMainTex, desc);
}
}

View File

@@ -59,21 +59,21 @@ namespace VRM
}
// Thumbnail
if (TryGetThumbnailTexture(data, vrm, out var thumbnail))
if (TryGetThumbnailTexture(data, vrm, out var key, out var desc))
{
yield return thumbnail;
yield return (key, desc);
}
}
private static bool TryGetThumbnailTexture(GltfData data, glTF_VRM_extensions vrm, out (SubAssetKey, TextureDescriptor) texture)
private static bool TryGetThumbnailTexture(GltfData data, glTF_VRM_extensions vrm, out SubAssetKey key, out TextureDescriptor desc)
{
if (vrm.meta.texture > -1)
{
texture = GltfTextureImporter.CreateSrgb(data, vrm.meta.texture, Vector2.zero, Vector2.one);
return true;
return GltfTextureImporter.TryCreateSrgb(data, vrm.meta.texture, Vector2.zero, Vector2.one, out key, out desc);
}
texture = default;
key = default;
desc = default;
return false;
}
}

View File

@@ -12,175 +12,180 @@ namespace UniVRM10
{
public static IEnumerable<(string key, (SubAssetKey, TextureDescriptor))> EnumerateAllTextures(GltfData data, glTFMaterial material, VRMC_materials_mtoon mToon)
{
if (TryGetBaseColorTexture(data, material, out var litTex))
if (TryGetBaseColorTexture(data, material, out var key, out var desc))
{
yield return (MToon10Prop.BaseColorTexture.ToUnityShaderLabName(), litTex);
yield return (MToon10Prop.BaseColorTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetEmissiveTexture(data, material, out var emissiveTex))
if (TryGetEmissiveTexture(data, material, out key, out desc))
{
yield return (MToon10Prop.EmissiveTexture.ToUnityShaderLabName(), emissiveTex);
yield return (MToon10Prop.EmissiveTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetNormalTexture(data, material, out var normalTex))
if (TryGetNormalTexture(data, material, out key, out desc))
{
yield return (MToon10Prop.NormalTexture.ToUnityShaderLabName(), normalTex);
yield return (MToon10Prop.NormalTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetShadeMultiplyTexture(data, mToon, out var shadeTex))
if (TryGetShadeMultiplyTexture(data, mToon, out key, out desc))
{
yield return (MToon10Prop.ShadeColorTexture.ToUnityShaderLabName(), shadeTex);
yield return (MToon10Prop.ShadeColorTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetShadingShiftTexture(data, mToon, out var shadeShiftTex))
if (TryGetShadingShiftTexture(data, mToon, out key, out desc))
{
yield return (MToon10Prop.ShadingShiftTexture.ToUnityShaderLabName(), shadeShiftTex);
yield return (MToon10Prop.ShadingShiftTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetMatcapTexture(data, mToon, out var matcapTex))
if (TryGetMatcapTexture(data, mToon, out key, out desc))
{
yield return (MToon10Prop.MatcapTexture.ToUnityShaderLabName(), matcapTex);
yield return (MToon10Prop.MatcapTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetRimMultiplyTexture(data, mToon, out var rimTex))
if (TryGetRimMultiplyTexture(data, mToon, out key, out desc))
{
yield return (MToon10Prop.RimMultiplyTexture.ToUnityShaderLabName(), rimTex);
yield return (MToon10Prop.RimMultiplyTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetOutlineWidthMultiplyTexture(data, mToon, out var outlineTex))
if (TryGetOutlineWidthMultiplyTexture(data, mToon, out key, out desc))
{
yield return (MToon10Prop.OutlineWidthMultiplyTexture.ToUnityShaderLabName(), outlineTex);
yield return (MToon10Prop.OutlineWidthMultiplyTexture.ToUnityShaderLabName(), (key, desc));
}
if (TryGetUvAnimationMaskTexture(data, mToon, out var uvAnimMaskTex))
if (TryGetUvAnimationMaskTexture(data, mToon, out key, out desc))
{
yield return (MToon10Prop.UvAnimationMaskTexture.ToUnityShaderLabName(), uvAnimMaskTex);
yield return (MToon10Prop.UvAnimationMaskTexture.ToUnityShaderLabName(), (key, desc));
}
}
private static bool TryGetBaseColorTexture(GltfData data, glTFMaterial src, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetBaseColorTexture(GltfData data, glTFMaterial src, out SubAssetKey key, out TextureDescriptor desc)
{
try
{
pair = GltfPbrTextureImporter.BaseColorTexture(data, src);
return true;
return GltfPbrTextureImporter.TryBaseColorTexture(data, src, out key, out desc);
}
catch (NullReferenceException)
{
pair = default;
key = default;
desc = default;
return false;
}
catch (ArgumentOutOfRangeException)
{
pair = default;
key = default;
desc = default;
return false;
}
}
private static bool TryGetEmissiveTexture(GltfData data, glTFMaterial src, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetEmissiveTexture(GltfData data, glTFMaterial src, out SubAssetKey key, out TextureDescriptor desc)
{
try
{
pair = GltfPbrTextureImporter.EmissiveTexture(data, src);
return true;
return GltfPbrTextureImporter.TryEmissiveTexture(data, src, out key, out desc);
}
catch (NullReferenceException)
{
pair = default;
key = default;
desc = default;
return false;
}
catch (ArgumentOutOfRangeException)
{
pair = default;
key = default;
desc = default;
return false;
}
}
private static bool TryGetNormalTexture(GltfData data, glTFMaterial src, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetNormalTexture(GltfData data, glTFMaterial src, out SubAssetKey key, out TextureDescriptor desc)
{
try
{
pair = GltfPbrTextureImporter.NormalTexture(data, src);
return true;
return GltfPbrTextureImporter.TryNormalTexture(data, src, out key, out desc);
}
catch (NullReferenceException)
{
pair = default;
key = default;
desc = default;
return false;
}
catch (ArgumentOutOfRangeException)
{
pair = default;
key = default;
desc = default;
return false;
}
}
private static bool TryGetShadeMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetShadeMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out SubAssetKey key, out TextureDescriptor desc)
{
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.ShadeMultiplyTexture), out pair);
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.ShadeMultiplyTexture), out key, out desc);
}
private static bool TryGetShadingShiftTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetShadingShiftTexture(GltfData data, VRMC_materials_mtoon mToon, out SubAssetKey key, out TextureDescriptor desc)
{
return TryGetLinearTexture(data, new Vrm10TextureInfo(mToon.ShadingShiftTexture), out pair);
return TryGetLinearTexture(data, new Vrm10TextureInfo(mToon.ShadingShiftTexture), out key, out desc);
}
private static bool TryGetMatcapTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetMatcapTexture(GltfData data, VRMC_materials_mtoon mToon, out SubAssetKey key, out TextureDescriptor desc)
{
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.MatcapTexture), out pair);
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.MatcapTexture), out key, out desc);
}
private static bool TryGetRimMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetRimMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out SubAssetKey key, out TextureDescriptor desc)
{
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.RimMultiplyTexture), out pair);
return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.RimMultiplyTexture), out key, out desc);
}
private static bool TryGetOutlineWidthMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetOutlineWidthMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out SubAssetKey key, out TextureDescriptor desc)
{
return TryGetLinearTexture(data, new Vrm10TextureInfo(mToon.OutlineWidthMultiplyTexture), out pair);
return TryGetLinearTexture(data, new Vrm10TextureInfo(mToon.OutlineWidthMultiplyTexture), out key, out desc);
}
private static bool TryGetUvAnimationMaskTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetUvAnimationMaskTexture(GltfData data, VRMC_materials_mtoon mToon, out SubAssetKey key, out TextureDescriptor desc)
{
return TryGetLinearTexture(data, new Vrm10TextureInfo(mToon.UvAnimationMaskTexture), out pair);
return TryGetLinearTexture(data, new Vrm10TextureInfo(mToon.UvAnimationMaskTexture), out key, out desc);
}
private static bool TryGetSRGBTexture(GltfData data, Vrm10TextureInfo info, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetSRGBTexture(GltfData data, Vrm10TextureInfo info, out SubAssetKey key, out TextureDescriptor desc)
{
try
{
var (offset, scale) = GetTextureOffsetAndScale(info);
pair = GltfTextureImporter.CreateSrgb(data, info.index, offset, scale);
return true;
return GltfTextureImporter.TryCreateSrgb(data, info.index, offset, scale, out key, out desc);
}
catch (NullReferenceException)
{
pair = default;
key = default;
desc = default;
return false;
}
catch (ArgumentOutOfRangeException)
{
pair = default;
key = default;
desc = default;
return false;
}
}
private static bool TryGetLinearTexture(GltfData data, Vrm10TextureInfo info, out (SubAssetKey, TextureDescriptor) pair)
private static bool TryGetLinearTexture(GltfData data, Vrm10TextureInfo info, out SubAssetKey key, out TextureDescriptor desc)
{
try
{
var (offset, scale) = GetTextureOffsetAndScale(info);
pair = GltfTextureImporter.CreateLinear(data, info.index, offset, scale);
return true;
return GltfTextureImporter.TryCreateLinear(data, info.index, offset, scale, out key, out desc);
}
catch (NullReferenceException)
{
pair = default;
key = default;
desc = default;
return false;
}
catch (ArgumentOutOfRangeException)
{
pair = default;
key = default;
desc = default;
return false;
}
}

View File

@@ -55,9 +55,14 @@ namespace VRMShaders
public SubAssetKey(Type type, string name)
{
if (type == null || string.IsNullOrEmpty(name))
if (type == null)
{
throw new System.ArgumentNullException();
throw new System.ArgumentNullException("type");
}
if (string.IsNullOrEmpty(name))
{
throw new System.ArgumentNullException("name");
}
if (!type.IsSubclassOf(typeof(UnityEngine.Object)))

View File

@@ -52,6 +52,10 @@ namespace VRMShaders
GetTextureBytesAsync i4,
GetTextureBytesAsync i5)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
UnityObjectName = name;
Offset = offset;
Scale = scale;