Skip to content

Commit

Permalink
fix roughnessConversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Mar 16, 2021
1 parent 151e8cb commit 7c07adb
Showing 1 changed file with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
#define SIMPLE_CONV
using UnityEngine;

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
///
/// Occlusion(glTF): src.r
/// Roughness(glTF): src.g -> Smoothness(Unity): dst.a (bake smoothnessOrRoughness)
/// Metallic(glTF) : src.b -> Metallic(Unity) : dst.r
/// </summary>
public class MetallicRoughnessConverter : ITextureConverter
{
private float _smoothnessOrRoughness;
private readonly float _smoothnessOrRoughness;
private readonly float _smoothnessOrRoughnessInverse;

public MetallicRoughnessConverter(float smoothnessOrRoughness)
{
_smoothnessOrRoughness = smoothnessOrRoughness;
_smoothnessOrRoughnessInverse = 1.0f / _smoothnessOrRoughness;
}

public Texture2D GetImportTexture(Texture2D texture)
Expand All @@ -25,43 +38,45 @@ public Texture2D GetExportTexture(Texture2D texture)

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) / 255.0f; // roughness
var pixelSmoothness = 1.0f - Mathf.Sqrt(pixelRoughnessFactor);

return new Color32
var dst = 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)Mathf.Clamp(pixelSmoothness * 255, 0, 255),
};

// Bake _smoothnessOrRoughness into a texture.
#if SIMPLE_CONV
dst.a = (byte)(255 - src.g * _smoothnessOrRoughness);
#else
var pixelRoughnessFactor = (src.g * _smoothnessOrRoughness) / 255.0f; // roughness
var pixelSmoothness = 1.0f - Mathf.Sqrt(pixelRoughnessFactor);
dst.a = (byte)Mathf.Clamp(pixelSmoothness * 255, 0, 255);
#endif
return dst;
}

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) / 255.0f; // smoothness
// https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/
var pixelRoughnessFactorSqrt = (1.0f - pixelSmoothness);
var pixelRoughnessFactor = pixelRoughnessFactorSqrt * pixelRoughnessFactorSqrt;

return new Color32
var dst = new Color32
{
r = 0,
// Bake smoothness values into a texture.
// See: https://github.com/dwango/UniVRM/issues/212.
g = (byte)Mathf.Clamp(pixelRoughnessFactor * 255, 0, 255),
b = src.r,
a = 255,
};

// Bake divide _smoothnessOrRoughness from a texture.
#if SIMPLE_CONV
dst.g = (byte)(255 - src.a);
#else
var pixelSmoothness = (src.a * _smoothnessOrRoughness) / 255.0f; // smoothness
var pixelRoughnessFactorSqrt = (1.0f - pixelSmoothness);
var pixelRoughnessFactor = pixelRoughnessFactorSqrt * pixelRoughnessFactorSqrt;
dst.g = (byte)Mathf.Clamp(pixelRoughnessFactor * 255, 0, 255);
#endif

return dst;
}
}

}

0 comments on commit 7c07adb

Please sign in to comment.