Skip to content

Commit

Permalink
Fix calculation of smoothness and roughnessFactor conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
yutopp committed Mar 15, 2019
1 parent 92f501e commit c8489af
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 2 additions & 3 deletions Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs
Expand Up @@ -164,9 +164,8 @@ public virtual Material CreateMaterial(int i, glTFMaterial x)
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);
Expand Down
19 changes: 14 additions & 5 deletions Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs
Expand Up @@ -51,11 +51,11 @@ class MetallicRoughnessConverter : ITextureConverter
{
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)
Expand All @@ -76,27 +76,36 @@ public Color32 Import(Color32 src)
{
// 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,
g = 0,
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),
};
}

public Color32 Export(Color32 src)
{
// 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,
};
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRM/UniGLTF/Scripts/IO/TextureItem.cs
Expand Up @@ -36,7 +36,7 @@ public Texture2D Texture
/// <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)
Expand Down Expand Up @@ -69,7 +69,7 @@ public Texture2D ConvertTexture(string prop, float smoothness = 1.0f)

if (prop == "_MetallicGlossMap")
{
var converted = new MetallicRoughnessConverter(smoothness).GetImportTexture(Texture);
var converted = new MetallicRoughnessConverter(smoothnessOrRoughness).GetImportTexture(Texture);
m_converts.Add(prop, converted);
return converted;
}
Expand Down

0 comments on commit c8489af

Please sign in to comment.