MetallicFactor and RoughnessFactor

This commit is contained in:
ousttrue 2021-03-12 18:57:23 +09:00
parent 4413128f28
commit 3d85986f2e
5 changed files with 31 additions and 26 deletions

View File

@ -52,7 +52,8 @@ namespace UniGLTF
{
return GetTextureParam.CreateStandard(gltf,
src.pbrMetallicRoughness.metallicRoughnessTexture.index,
src.pbrMetallicRoughness.metallicFactor);
src.pbrMetallicRoughness.metallicFactor,
src.pbrMetallicRoughness.roughnessFactor);
}
public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src)

View File

@ -4,14 +4,18 @@ namespace UniGLTF
{
/// <summary>
///
/// * https://github.com/dwango/UniVRM/issues/212.
/// * https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/
/// * https://github.com/vrm-c/UniVRM/issues/388
/// * https://github.com/vrm-c/UniVRM/issues/781
///
/// Unity = glTF
/// Occlusion: unity.g = glTF.r
/// Roughness: unity.a = 1 - glTF.g * roughnessFactor
/// Metallic : unity.r = glTF.b * metallicFactor
///
/// glTF = Unity
/// Occlusion: src.r -> dst.g
/// Roughness: src.g -> dst.a (bake smoothnessOrRoughness)
/// Metallic : src.b -> dst.r
/// Occlusion: glTF.r = unity.g
/// Roughness: glTF.g = 1 - unity.a * smoothness
/// Metallic : glTF.b = unity.r
///
/// </summary>
public class OcclusionMetallicRoughnessConverter : ITextureConverter
{
@ -22,11 +26,11 @@ namespace UniGLTF
_smoothnessOrRoughness = smoothnessOrRoughness;
}
public static Texture2D GetImportTexture(Texture2D texture, float smoothnessOrRoughness)
public static Texture2D GetImportTexture(Texture2D texture, float metallicFactor, float roughnessFactor)
{
TextureConverter.ColorConversion convert = src =>
{
return Import(src, smoothnessOrRoughness);
return Import(src, metallicFactor, roughnessFactor);
};
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, convert, null);
return converted;
@ -38,15 +42,14 @@ namespace UniGLTF
return converted;
}
public static Color32 Import(Color32 src, float _smoothnessOrRoughness)
public static Color32 Import(Color32 src, float metallicFactor, float roughnessFactor)
{
var dst = new Color32
{
r = src.b, // Metallic
r = (byte)(src.b * metallicFactor), // Metallic
g = src.r, // Occlusion
b = 0, // not used
// Roughness to Smoothness. Bake _smoothnessOrRoughness into a texture.
a = (byte)(255 - src.g * _smoothnessOrRoughness),
b = 0, // not used
a = (byte)(255 - src.g * roughnessFactor), // Roughness to Smoothness
};
return dst;
@ -56,9 +59,8 @@ namespace UniGLTF
{
var dst = new Color32
{
r = src.g, // Occlusion
// Roughness from Smoothness. Bake divide _smoothnessOrRoughness from a texture.
g = (byte)(255 - src.a),
r = src.g, // Occlusion
g = (byte)(255 - src.a), // Roughness from Smoothness
b = src.r, // Metallic
a = 255, // not used
};

View File

@ -57,6 +57,7 @@ namespace UniGLTF
public readonly TextureTypes TextureType;
public readonly float MetallicFactor;
public readonly float RoughnessFactor;
public readonly ushort? Index0;
public readonly ushort? Index1;
public readonly ushort? Index2;
@ -69,7 +70,7 @@ namespace UniGLTF
/// </summary>
public bool ExtractConverted => TextureType == TextureTypes.StandardMap;
public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, int i0, int i1, int i2, int i3, int i4, int i5)
public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, float roughnessFactor, int i0, int i1, int i2, int i3, int i4, int i5)
{
if (string.IsNullOrEmpty(name))
{
@ -79,6 +80,7 @@ namespace UniGLTF
TextureType = textureType;
MetallicFactor = metallicFactor;
RoughnessFactor = roughnessFactor;
Index0 = (ushort)i0;
Index1 = (ushort)i1;
Index2 = (ushort)i2;
@ -90,10 +92,10 @@ namespace UniGLTF
public static GetTextureParam CreateSRGB(glTF gltf, int textureIndex)
{
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, TextureTypes.sRGB, default, textureIndex, default, default, default, default, default);
return new GetTextureParam(name, TextureTypes.sRGB, default, default, textureIndex, default, default, default, default, default);
}
public static GetTextureParam Create(glTF gltf, int index, string prop)
public static GetTextureParam Create(glTF gltf, int index, string prop, float metallicFactor, float roughnessFactor)
{
switch (prop)
{
@ -102,7 +104,7 @@ namespace UniGLTF
case OCCLUSION_PROP:
case METALLIC_GLOSS_PROP:
return CreateStandard(gltf, index, 1);
return CreateStandard(gltf, index, metallicFactor, roughnessFactor);
default:
return CreateSRGB(gltf, index);
@ -112,13 +114,13 @@ namespace UniGLTF
public static GetTextureParam CreateNormal(glTF gltf, int textureIndex)
{
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, TextureTypes.NormalMap, default, textureIndex, default, default, default, default, default);
return new GetTextureParam(name, TextureTypes.NormalMap, default, default, textureIndex, default, default, default, default, default);
}
public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor)
public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor, float roughnessFactor)
{
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, textureIndex, default, default, default, default, default);
return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, roughnessFactor, textureIndex, default, default, default, default, default);
}
}
}

View File

@ -178,7 +178,7 @@ namespace UniGLTF
case GetTextureParam.TextureTypes.StandardMap:
{
var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false);
var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor);
var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor);
converted.name = param.ConvertedName;
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(converted.name, info);

View File

@ -73,7 +73,7 @@ namespace VRM
}
foreach (var kv in item.textureProperties)
{
var param = GetTextureParam.Create(gltf, kv.Value, kv.Key);
var param = GetTextureParam.Create(gltf, kv.Value, kv.Key, 1, 1);
var texture = await getTexture(awaitCaller, gltf, param);
if (texture != null)
{