mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-11 05:19:14 -05:00
TextureImportParam
This commit is contained in:
@@ -170,9 +170,10 @@ namespace UniGLTF
|
||||
await OnLoadModel(m_awaitCaller, MeasureTime);
|
||||
}
|
||||
|
||||
protected virtual async Task OnLoadModel(IAwaitCaller awaitCaller, Func<string, IDisposable> MeasureTime)
|
||||
protected virtual Task OnLoadModel(IAwaitCaller awaitCaller, Func<string, IDisposable> MeasureTime)
|
||||
{
|
||||
// do nothing
|
||||
return Task.FromResult<object>(null);
|
||||
}
|
||||
|
||||
async Task<MeshWithMaterials> BuildMeshAsync(Func<string, IDisposable> MeasureTime, MeshImporter.MeshContext x, int i)
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace UniGLTF
|
||||
var imageIndex = gltf.images.Count;
|
||||
gltf.images.Add(new glTFImage
|
||||
{
|
||||
name = GetTextureParam.RemoveSuffix(texture.name),
|
||||
name = TextureImportName.RemoveSuffix(texture.name),
|
||||
bufferView = viewIndex,
|
||||
mimeType = bytesWithMime.mine,
|
||||
});
|
||||
|
||||
@@ -4,9 +4,7 @@ using UnityEngine;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using VRMShaders;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
@@ -170,7 +168,7 @@ namespace UniGLTF
|
||||
texture.name = name;
|
||||
if (imageBytes != null)
|
||||
{
|
||||
texture.LoadImage(ToArray(imageBytes));
|
||||
texture.LoadImage(imageBytes);
|
||||
}
|
||||
|
||||
SetSampler(texture, param);
|
||||
@@ -279,7 +277,7 @@ namespace UniGLTF
|
||||
{
|
||||
var name = CreateNameExt(parser.GLTF, textureIndex, TextureImportTypes.sRGB);
|
||||
var sampler = CreateSampler(parser.GLTF, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex)));
|
||||
return new GetTextureParam(name, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
}
|
||||
|
||||
@@ -287,13 +285,13 @@ namespace UniGLTF
|
||||
{
|
||||
var name = CreateNameExt(parser.GLTF, textureIndex, TextureImportTypes.NormalMap);
|
||||
var sampler = CreateSampler(parser.GLTF, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex)));
|
||||
return new GetTextureParam(name, sampler, TextureImportTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
}
|
||||
|
||||
public static GetTextureParam CreateStandard(GltfParser parser, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, float metallicFactor, float roughnessFactor)
|
||||
{
|
||||
GetTextureParam.NameExt name = default;
|
||||
TextureImportName name = default;
|
||||
|
||||
GetTextureBytesAsync getMetallicRoughnessAsync = default;
|
||||
SamplerParam sampler = default;
|
||||
@@ -301,7 +299,7 @@ namespace UniGLTF
|
||||
{
|
||||
name = CreateNameExt(parser.GLTF, metallicRoughnessTextureIndex.Value, TextureImportTypes.StandardMap);
|
||||
sampler = CreateSampler(parser.GLTF, metallicRoughnessTextureIndex.Value);
|
||||
getMetallicRoughnessAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value);
|
||||
getMetallicRoughnessAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value)));
|
||||
}
|
||||
|
||||
GetTextureBytesAsync getOcclusionAsync = default;
|
||||
@@ -311,7 +309,7 @@ namespace UniGLTF
|
||||
name = CreateNameExt(parser.GLTF, occlusionTextureIndex.Value, TextureImportTypes.StandardMap);
|
||||
}
|
||||
sampler = CreateSampler(parser.GLTF, occlusionTextureIndex.Value);
|
||||
getOcclusionAsync = async () => parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value);
|
||||
getOcclusionAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value)));
|
||||
}
|
||||
|
||||
return new GetTextureParam(name, sampler, TextureImportTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default);
|
||||
@@ -333,7 +331,7 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
public static GetTextureParam.NameExt CreateNameExt(glTF gltf, int textureIndex, TextureImportTypes textureType)
|
||||
public static TextureImportName CreateNameExt(glTF gltf, int textureIndex, TextureImportTypes textureType)
|
||||
{
|
||||
if (textureIndex < 0 || textureIndex >= gltf.textures.Count)
|
||||
{
|
||||
@@ -345,8 +343,8 @@ namespace UniGLTF
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
var gltfImage = gltf.images[gltfTexture.source];
|
||||
var convertedName = GetTextureParam.NameExt.Convert(gltfTexture.name, textureType);
|
||||
return new GetTextureParam.NameExt(gltfTexture.name, convertedName, gltfImage.GetExt(), gltfImage.uri);
|
||||
var convertedName = TextureImportName.Convert(gltfTexture.name, textureType);
|
||||
return new TextureImportName(gltfTexture.name, convertedName, gltfImage.GetExt(), gltfImage.uri);
|
||||
}
|
||||
|
||||
public static SamplerParam CreateSampler(glTF gltf, int index)
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace VRMShaders
|
||||
{
|
||||
public delegate Task<ArraySegment<byte>> GetTextureBytesAsync();
|
||||
|
||||
/// <summary>
|
||||
/// STANDARD(Pbr) texture = occlusion + metallic + smoothness
|
||||
/// </summary>
|
||||
public struct GetTextureParam
|
||||
{
|
||||
public const string NORMAL_PROP = "_BumpMap";
|
||||
public const string NORMAL_SUFFIX = ".normal";
|
||||
|
||||
public const string METALLIC_GLOSS_PROP = "_MetallicGlossMap";
|
||||
public const string OCCLUSION_PROP = "_OcclusionMap";
|
||||
public const string STANDARD_SUFFIX = ".standard";
|
||||
|
||||
public static string RemoveSuffix(string src)
|
||||
{
|
||||
if (src.EndsWith(NORMAL_SUFFIX))
|
||||
{
|
||||
return src.Substring(0, src.Length - NORMAL_SUFFIX.Length);
|
||||
}
|
||||
else if (src.EndsWith(STANDARD_SUFFIX))
|
||||
{
|
||||
return src.Substring(0, src.Length - STANDARD_SUFFIX.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return src;
|
||||
}
|
||||
}
|
||||
|
||||
public struct NameExt
|
||||
{
|
||||
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)
|
||||
{
|
||||
GltfName = gltfName;
|
||||
ConvertedName = convertedName;
|
||||
Ext = ext;
|
||||
Uri = uri;
|
||||
}
|
||||
|
||||
public string GltfFileName => $"{GltfName}{Ext}";
|
||||
|
||||
public string ConvertedFileName => $"{ConvertedName}.png";
|
||||
|
||||
public static string Convert(string name, TextureImportTypes textureType)
|
||||
{
|
||||
switch (textureType)
|
||||
{
|
||||
case TextureImportTypes.StandardMap: return $"{name}{STANDARD_SUFFIX}";
|
||||
case TextureImportTypes.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 SamplerParam Sampler;
|
||||
|
||||
public readonly TextureImportTypes TextureType;
|
||||
public readonly float MetallicFactor;
|
||||
public readonly float RoughnessFactor;
|
||||
|
||||
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 == TextureImportTypes.StandardMap;
|
||||
|
||||
public GetTextureParam(NameExt name, SamplerParam sampler, TextureImportTypes textureType, float metallicFactor, float roughnessFactor,
|
||||
GetTextureBytesAsync i0,
|
||||
GetTextureBytesAsync i1,
|
||||
GetTextureBytesAsync i2,
|
||||
GetTextureBytesAsync i3,
|
||||
GetTextureBytesAsync i4,
|
||||
GetTextureBytesAsync i5)
|
||||
{
|
||||
Name = name;
|
||||
Sampler = sampler;
|
||||
TextureType = textureType;
|
||||
MetallicFactor = metallicFactor;
|
||||
RoughnessFactor = roughnessFactor;
|
||||
Index0 = i0;
|
||||
Index1 = i1;
|
||||
Index2 = i2;
|
||||
Index3 = i3;
|
||||
Index4 = i4;
|
||||
Index5 = i5;
|
||||
}
|
||||
}
|
||||
}
|
||||
50
Assets/VRMShaders/Runtime/TextureImportName.cs
Normal file
50
Assets/VRMShaders/Runtime/TextureImportName.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
namespace VRMShaders
|
||||
{
|
||||
public struct TextureImportName
|
||||
{
|
||||
public readonly string GltfName;
|
||||
public readonly string ConvertedName;
|
||||
|
||||
public readonly string Ext;
|
||||
public string Uri;
|
||||
|
||||
public TextureImportName(string gltfName, string convertedName, string ext, string uri)
|
||||
{
|
||||
GltfName = gltfName;
|
||||
ConvertedName = convertedName;
|
||||
Ext = ext;
|
||||
Uri = uri;
|
||||
}
|
||||
|
||||
public string GltfFileName => $"{GltfName}{Ext}";
|
||||
|
||||
public string ConvertedFileName => $"{ConvertedName}.png";
|
||||
|
||||
public const string NORMAL_SUFFIX = ".normal";
|
||||
public const string STANDARD_SUFFIX = ".standard";
|
||||
public static string Convert(string name, TextureImportTypes textureType)
|
||||
{
|
||||
switch (textureType)
|
||||
{
|
||||
case TextureImportTypes.StandardMap: return $"{name}{STANDARD_SUFFIX}";
|
||||
case TextureImportTypes.NormalMap: return $"{name}{NORMAL_SUFFIX}";
|
||||
default: return name;
|
||||
}
|
||||
}
|
||||
public static string RemoveSuffix(string src)
|
||||
{
|
||||
if (src.EndsWith(NORMAL_SUFFIX))
|
||||
{
|
||||
return src.Substring(0, src.Length - NORMAL_SUFFIX.Length);
|
||||
}
|
||||
else if (src.EndsWith(STANDARD_SUFFIX))
|
||||
{
|
||||
return src.Substring(0, src.Length - STANDARD_SUFFIX.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return src;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/VRMShaders/Runtime/TextureImportName.cs.meta
Normal file
11
Assets/VRMShaders/Runtime/TextureImportName.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d005850c7abbb24582e9788657afd35
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
72
Assets/VRMShaders/Runtime/TextureImportParam.cs
Normal file
72
Assets/VRMShaders/Runtime/TextureImportParam.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace VRMShaders
|
||||
{
|
||||
/// <summary>
|
||||
/// get bytes for
|
||||
///
|
||||
/// runtime:
|
||||
/// Texture2D.LoadImage
|
||||
/// extact:
|
||||
/// File.WriteAllBytes
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public delegate Task<byte[]> GetTextureBytesAsync();
|
||||
|
||||
public struct GetTextureParam
|
||||
{
|
||||
public const string NORMAL_PROP = "_BumpMap";
|
||||
|
||||
public const string METALLIC_GLOSS_PROP = "_MetallicGlossMap";
|
||||
public const string OCCLUSION_PROP = "_OcclusionMap";
|
||||
|
||||
|
||||
public readonly TextureImportName 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 SamplerParam Sampler;
|
||||
|
||||
public readonly TextureImportTypes TextureType;
|
||||
public readonly float MetallicFactor;
|
||||
public readonly float RoughnessFactor;
|
||||
|
||||
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 == TextureImportTypes.StandardMap;
|
||||
|
||||
public GetTextureParam(TextureImportName name, SamplerParam sampler, TextureImportTypes textureType, float metallicFactor, float roughnessFactor,
|
||||
GetTextureBytesAsync i0,
|
||||
GetTextureBytesAsync i1,
|
||||
GetTextureBytesAsync i2,
|
||||
GetTextureBytesAsync i3,
|
||||
GetTextureBytesAsync i4,
|
||||
GetTextureBytesAsync i5)
|
||||
{
|
||||
Name = name;
|
||||
Sampler = sampler;
|
||||
TextureType = textureType;
|
||||
MetallicFactor = metallicFactor;
|
||||
RoughnessFactor = roughnessFactor;
|
||||
Index0 = i0;
|
||||
Index1 = i1;
|
||||
Index2 = i2;
|
||||
Index3 = i3;
|
||||
Index4 = i4;
|
||||
Index5 = i5;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user