mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-11 21:14:19 -05:00
Bake smoothness and roughnessFactor into textures. Treat smoothness and roughnessFactor as hardcoded-value 1.0 in Unity world (Resolve #212)
This commit is contained in:
parent
8dbc13f1b6
commit
c95753fc8e
|
|
@ -61,33 +61,44 @@ namespace UniGLTF
|
|||
int index = -1;
|
||||
if (m.HasProperty("_MetallicGlossMap"))
|
||||
{
|
||||
index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), new MetallicRoughnessConverter());
|
||||
float smoothness = 0.0f;
|
||||
if (m.HasProperty("_GlossMapScale"))
|
||||
{
|
||||
smoothness = m.GetFloat("_GlossMapScale");
|
||||
}
|
||||
|
||||
// Bake smoothness values into a texture.
|
||||
var converter = new MetallicRoughnessConverter(smoothness);
|
||||
index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), converter);
|
||||
if (index != -1)
|
||||
{
|
||||
material.pbrMetallicRoughness.metallicRoughnessTexture = new glTFMaterialMetallicRoughnessTextureInfo()
|
||||
{
|
||||
index = index,
|
||||
};
|
||||
material.pbrMetallicRoughness.metallicRoughnessTexture =
|
||||
new glTFMaterialMetallicRoughnessTextureInfo()
|
||||
{
|
||||
index = index,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (index != -1 && m.HasProperty("_GlossMapScale"))
|
||||
if (index != -1)
|
||||
{
|
||||
material.pbrMetallicRoughness.metallicFactor = 1.0f;
|
||||
material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_GlossMapScale");
|
||||
// Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
|
||||
material.pbrMetallicRoughness.roughnessFactor = 1.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
if (m.HasProperty("_Metallic"))
|
||||
{
|
||||
material.pbrMetallicRoughness.metallicFactor = m.GetFloat("_Metallic");
|
||||
}
|
||||
|
||||
if (m.HasProperty("_Glossiness"))
|
||||
{
|
||||
material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_Glossiness");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material)
|
||||
|
|
|
|||
|
|
@ -164,11 +164,14 @@ namespace UniGLTF
|
|||
if (texture != null)
|
||||
{
|
||||
var prop = "_MetallicGlossMap";
|
||||
material.SetTexture(prop, texture.ConvertTexture(prop));
|
||||
var smoothness = 1.0f - x.pbrMetallicRoughness.roughnessFactor;
|
||||
// Bake smoothness values into a texture.
|
||||
material.SetTexture(prop, texture.ConvertTexture(prop, smoothness));
|
||||
}
|
||||
|
||||
material.SetFloat("_Metallic", 1.0f);
|
||||
material.SetFloat("_GlossMapScale", 1.0f - x.pbrMetallicRoughness.roughnessFactor);
|
||||
// Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212.
|
||||
material.SetFloat("_GlossMapScale", 1.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
#if UNITY_EDITOR
|
||||
|
|
@ -50,6 +51,13 @@ namespace UniGLTF
|
|||
{
|
||||
private const string m_extension = ".metallicRoughness";
|
||||
|
||||
private float _smoothness;
|
||||
|
||||
public MetallicRoughnessConverter(float smoothness)
|
||||
{
|
||||
_smoothness = smoothness;
|
||||
}
|
||||
|
||||
public Texture2D GetImportTexture(Texture2D texture)
|
||||
{
|
||||
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Import, null);
|
||||
|
|
@ -66,21 +74,29 @@ 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
|
||||
return new Color32
|
||||
{
|
||||
r = src.b,
|
||||
g = 0,
|
||||
b = 0,
|
||||
a = (byte)(255 - src.g),
|
||||
// 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)),
|
||||
};
|
||||
}
|
||||
|
||||
public Color32 Export(Color32 src)
|
||||
{
|
||||
// Smoothness(Unity): src.a -> Roughness(glTF): dst.g (with conversion)
|
||||
// Metallic(Unity) : src.r -> Metallic(glTF) : dst.b
|
||||
return new Color32
|
||||
{
|
||||
r = 0,
|
||||
g = (byte)(255 - src.a),
|
||||
// Bake smoothness values into a texture.
|
||||
// See: https://github.com/dwango/UniVRM/issues/212.
|
||||
g = (byte)(255 - Math.Min(src.a * _smoothness, 255)),
|
||||
b = src.r,
|
||||
a = 255,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -30,7 +30,13 @@ namespace UniGLTF
|
|||
get { return m_converts; }
|
||||
}
|
||||
|
||||
public Texture2D ConvertTexture(string prop)
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
var convertedTexture = Converts.FirstOrDefault(x => x.Key == prop);
|
||||
if (convertedTexture.Value != null)
|
||||
|
|
@ -63,7 +69,7 @@ namespace UniGLTF
|
|||
|
||||
if (prop == "_MetallicGlossMap")
|
||||
{
|
||||
var converted = new MetallicRoughnessConverter().GetImportTexture(Texture);
|
||||
var converted = new MetallicRoughnessConverter(smoothness).GetImportTexture(Texture);
|
||||
m_converts.Add(prop, converted);
|
||||
return converted;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user