Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sampler param #966

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 16 additions & 71 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static (SubAssetKey, TextureImportParam Param) CreateSRGB(GltfParser pars
var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default);
return (key, param);
}

public static (SubAssetKey, TextureImportParam Param) CreateLinear(GltfParser parser, int textureIndex, Vector2 offset, Vector2 scale)
{
var gltfTexture = parser.GLTF.textures[textureIndex];
Expand Down Expand Up @@ -102,91 +102,36 @@ public static SamplerParam CreateSampler(glTF gltf, int index)
if (gltfTexture.sampler < 0 || gltfTexture.sampler >= gltf.samplers.Count)
{
// default
return new SamplerParam
{
FilterMode = FilterMode.Bilinear,
WrapModes = new (SamplerWrapType, TextureWrapMode)[] { },
};
return SamplerParam.Default;
}

var gltfSampler = gltf.samplers[gltfTexture.sampler];
return new SamplerParam
{
WrapModes = GetUnityWrapMode(gltfSampler).ToArray(),
WrapModesU = GetUnityWrapMode(gltfSampler.wrapS),
WrapModesV = GetUnityWrapMode(gltfSampler.wrapT),
FilterMode = ImportFilterMode(gltfSampler.minFilter),
};
}

public static IEnumerable<(SamplerWrapType, TextureWrapMode)> GetUnityWrapMode(glTFTextureSampler sampler)
public static TextureWrapMode GetUnityWrapMode(glWrap wrap)
{
if (sampler.wrapS == sampler.wrapT)
switch (wrap)
{
switch (sampler.wrapS)
{
case glWrap.NONE: // default
yield return (SamplerWrapType.All, TextureWrapMode.Repeat);
break;

case glWrap.CLAMP_TO_EDGE:
yield return (SamplerWrapType.All, TextureWrapMode.Clamp);
break;

case glWrap.REPEAT:
yield return (SamplerWrapType.All, TextureWrapMode.Repeat);
break;

case glWrap.MIRRORED_REPEAT:
yield return (SamplerWrapType.All, TextureWrapMode.Mirror);
break;
case glWrap.NONE: // default
return TextureWrapMode.Repeat;

default:
throw new NotImplementedException();
}
}
else
{
switch (sampler.wrapS)
{
case glWrap.NONE: // default
yield return (SamplerWrapType.U, TextureWrapMode.Repeat);
break;
case glWrap.CLAMP_TO_EDGE:
return TextureWrapMode.Clamp;

case glWrap.CLAMP_TO_EDGE:
yield return (SamplerWrapType.U, TextureWrapMode.Clamp);
break;
case glWrap.REPEAT:
return TextureWrapMode.Repeat;

case glWrap.REPEAT:
yield return (SamplerWrapType.U, TextureWrapMode.Repeat);
break;
case glWrap.MIRRORED_REPEAT:
return TextureWrapMode.Mirror;

case glWrap.MIRRORED_REPEAT:
yield return (SamplerWrapType.U, TextureWrapMode.Mirror);
break;

default:
throw new NotImplementedException();
}
switch (sampler.wrapT)
{
case glWrap.NONE: // default
yield return (SamplerWrapType.V, TextureWrapMode.Repeat);
break;

case glWrap.CLAMP_TO_EDGE:
yield return (SamplerWrapType.V, TextureWrapMode.Clamp);
break;

case glWrap.REPEAT:
yield return (SamplerWrapType.V, TextureWrapMode.Repeat);
break;

case glWrap.MIRRORED_REPEAT:
yield return (SamplerWrapType.V, TextureWrapMode.Mirror);
break;

default:
throw new NotImplementedException();
}
default:
throw new NotImplementedException();
}
}

Expand Down
32 changes: 5 additions & 27 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureSamplerUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,52 +43,30 @@ public static glFilter ExportMinFilter(Texture texture)
}
}

public static TextureWrapMode GetWrapS(Texture texture)
{
#if UNITY_2017_1_OR_NEWER
return texture.wrapModeU;
#else
return texture.wrapMode;
#endif
}

public static TextureWrapMode GetWrapT(Texture texture)
{
#if UNITY_2017_1_OR_NEWER
return texture.wrapModeV;
#else
return texture.wrapMode;
#endif
}

public static glWrap ExportWrapMode(TextureWrapMode wrapMode)
{
switch (wrapMode)
{
case TextureWrapMode.Clamp:
return glWrap.CLAMP_TO_EDGE;

case (TextureWrapMode)(-1):
case TextureWrapMode.Repeat:
return glWrap.REPEAT;

#if UNITY_2017_1_OR_NEWER
case TextureWrapMode.Mirror:
case TextureWrapMode.MirrorOnce:
return glWrap.MIRRORED_REPEAT;
#endif

// case (TextureWrapMode)(-1):
// case TextureWrapMode.Repeat:
default:
throw new NotImplementedException();
return glWrap.REPEAT;
}
}

public static glTFTextureSampler Export(Texture texture)
{
var magFilter = ExportMagFilter(texture);
var minFilter = ExportMinFilter(texture);
var wrapS = ExportWrapMode(GetWrapS(texture));
var wrapT = ExportWrapMode(GetWrapT(texture));
var wrapS = ExportWrapMode(texture.wrapModeU);
var wrapT = ExportWrapMode(texture.wrapModeV);
return new glTFTextureSampler
{
magFilter = magFilter,
Expand Down
28 changes: 26 additions & 2 deletions Assets/VRMShaders/GLTF/IO/Runtime/SamplerParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,31 @@ namespace VRMShaders
{
public struct SamplerParam
{
public (SamplerWrapType, TextureWrapMode)[] WrapModes;
public FilterMode FilterMode;
public TextureWrapMode WrapModesU;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unity-2017 から wrapmode が u, v, w に分離された様子。
最近のUniVRMは、サポートバージョンが Unity-2018 以降なので変更。


public TextureWrapMode WrapModesV;

public FilterMode FilterMode;

public static SamplerParam Default => new SamplerParam
{
FilterMode = FilterMode.Bilinear,
WrapModesU = TextureWrapMode.Repeat,
WrapModesV = TextureWrapMode.Repeat,
};
}

public static class SamplerParamExtensions
{
public static void SetSampler(this Texture2D texture, in SamplerParam param)
{
if (texture == null)
{
return;
}
texture.wrapModeU = param.WrapModesU;
texture.wrapModeV = param.WrapModesV;
texture.filterMode = param.FilterMode;
}
}
}
10 changes: 0 additions & 10 deletions Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/VRMShaders/GLTF/IO/Runtime/SamplerWrapTypes.cs.meta

This file was deleted.

40 changes: 1 addition & 39 deletions Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,51 +138,13 @@ async Task<TextureLoadInfo> GetOrCreateBaseTexture(string name, TextureImportPar
texture.LoadImage(imageBytes);
}

SetSampler(texture, param);
texture.SetSampler(param.Sampler);

cacheInfo = new TextureLoadInfo(texture, used, false);
m_textureCache.Add(name, cacheInfo);
return cacheInfo;
}

public static void SetSampler(Texture2D texture, TextureImportParam param)
{
if (texture == null)
{
return;
}

if (param.Sampler.WrapModes != null)
{
foreach (var (key, value) in param.Sampler.WrapModes)
{
switch (key)
{
case SamplerWrapType.All:
texture.wrapMode = value;
break;

case SamplerWrapType.U:
texture.wrapModeU = value;
break;

case SamplerWrapType.V:
texture.wrapModeV = value;
break;

case SamplerWrapType.W:
texture.wrapModeW = value;
break;

default:
throw new NotImplementedException();
}
}
}

texture.filterMode = param.Sampler.FilterMode;
}

/// <summary>
/// テクスチャーをロード、必要であれば変換して返す。
/// 同じものはキャッシュを返す
Expand Down