mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-08-20 17:45:22 -05:00
@@ -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)
|
||||
@@ -25,43 +38,45 @@ namespace UniGLTF
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user