mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-09 12:29:23 -05:00
Fix calculation of smoothness and roughnessFactor conversion
This commit is contained in:
@@ -164,9 +164,8 @@ namespace UniGLTF
|
||||
if (texture != null)
|
||||
{
|
||||
var prop = "_MetallicGlossMap";
|
||||
var smoothness = 1.0f - x.pbrMetallicRoughness.roughnessFactor;
|
||||
// Bake smoothness values into a texture.
|
||||
material.SetTexture(prop, texture.ConvertTexture(prop, smoothness));
|
||||
// Bake roughnessFactor values into a texture.
|
||||
material.SetTexture(prop, texture.ConvertTexture(prop, x.pbrMetallicRoughness.roughnessFactor));
|
||||
}
|
||||
|
||||
material.SetFloat("_Metallic", 1.0f);
|
||||
|
||||
@@ -51,11 +51,11 @@ namespace UniGLTF
|
||||
{
|
||||
private const string m_extension = ".metallicRoughness";
|
||||
|
||||
private float _smoothness;
|
||||
private float _smoothnessOrRoughness;
|
||||
|
||||
public MetallicRoughnessConverter(float smoothness)
|
||||
public MetallicRoughnessConverter(float smoothnessOrRoughness)
|
||||
{
|
||||
_smoothness = smoothness;
|
||||
_smoothnessOrRoughness = smoothnessOrRoughness;
|
||||
}
|
||||
|
||||
public Texture2D GetImportTexture(Texture2D texture)
|
||||
@@ -76,6 +76,10 @@ namespace UniGLTF
|
||||
{
|
||||
// Roughness(glTF): dst.g -> Smoothness(Unity): src.a (with conversion)
|
||||
// Metallic(glTF) : dst.b -> Metallic(Unity) : src.r
|
||||
|
||||
var pixelRoughnessFactor = src.g * _smoothnessOrRoughness; // roughness
|
||||
var pixelSmoothness = 1.0f - Mathf.Sqrt(pixelRoughnessFactor);
|
||||
|
||||
return new Color32
|
||||
{
|
||||
r = src.b,
|
||||
@@ -83,7 +87,7 @@ namespace UniGLTF
|
||||
b = 0,
|
||||
// Bake roughness values into a texture.
|
||||
// See: https://github.com/dwango/UniVRM/issues/212.
|
||||
a = (byte)(255 - Math.Min(src.g * (1.0f - _smoothness), 255)),
|
||||
a = (byte)Mathf.Clamp(pixelSmoothness * 255, 0, 255),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -91,12 +95,17 @@ namespace UniGLTF
|
||||
{
|
||||
// Smoothness(Unity): src.a -> Roughness(glTF): dst.g (with conversion)
|
||||
// Metallic(Unity) : src.r -> Metallic(glTF) : dst.b
|
||||
|
||||
var pixelSmoothness = src.a * _smoothnessOrRoughness; // smoothness
|
||||
// https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/
|
||||
var pixelRoughnessFactor = (1.0f - pixelSmoothness) * (1.0f - pixelSmoothness);
|
||||
|
||||
return new Color32
|
||||
{
|
||||
r = 0,
|
||||
// Bake smoothness values into a texture.
|
||||
// See: https://github.com/dwango/UniVRM/issues/212.
|
||||
g = (byte)(255 - Math.Min(src.a * _smoothness, 255)),
|
||||
g = (byte)Mathf.Clamp(pixelRoughnessFactor * 255, 0, 255),
|
||||
b = src.r,
|
||||
a = 255,
|
||||
};
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace UniGLTF
|
||||
/// <param name="prop"></param>
|
||||
/// <param name="smoothness">used only when converting MetallicRoughness maps</param>
|
||||
/// <returns></returns>
|
||||
public Texture2D ConvertTexture(string prop, float smoothness = 1.0f)
|
||||
public Texture2D ConvertTexture(string prop, float smoothnessOrRoughness = 1.0f)
|
||||
{
|
||||
var convertedTexture = Converts.FirstOrDefault(x => x.Key == prop);
|
||||
if (convertedTexture.Value != null)
|
||||
@@ -69,7 +69,7 @@ namespace UniGLTF
|
||||
|
||||
if (prop == "_MetallicGlossMap")
|
||||
{
|
||||
var converted = new MetallicRoughnessConverter(smoothness).GetImportTexture(Texture);
|
||||
var converted = new MetallicRoughnessConverter(smoothnessOrRoughness).GetImportTexture(Texture);
|
||||
m_converts.Add(prop, converted);
|
||||
return converted;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user