remove ITextureConverter. static class OcclusionMetallicRoughnessConverter

This commit is contained in:
ousttrue
2021-03-12 20:35:07 +09:00
parent d2e560aca4
commit fd12e0d1f5
8 changed files with 36 additions and 96 deletions

View File

@@ -1,6 +1,4 @@
using System.Collections.Generic;
using UniGLTF.UniUnlit;
using UniJSON;
using UniGLTF.UniUnlit;
using UnityEngine;
@@ -67,15 +65,14 @@ namespace UniGLTF
int index = -1;
if (m.HasProperty("_MetallicGlossMap"))
{
float smoothness = 0.0f;
float smoothness = 1.0f;
if (m.HasProperty("_GlossMapScale"))
{
smoothness = m.GetFloat("_GlossMapScale");
}
// Bake smoothness values into a texture.
var converter = new OcclusionMetallicRoughnessConverter(smoothness);
index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), converter);
index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), x => OcclusionMetallicRoughnessConverter.GetExportTexture(x, smoothness));
if (index != -1)
{
material.pbrMetallicRoughness.metallicRoughnessTexture =
@@ -107,33 +104,12 @@ namespace UniGLTF
}
}
}
// static void Export_Occlusion(Material m, TextureExportManager textureManager, glTFMaterial material)
// {
// if (m.HasProperty("_OcclusionMap"))
// {
// var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter());
// if (index != -1)
// {
// material.occlusionTexture = new glTFMaterialOcclusionTextureInfo()
// {
// index = index,
// };
// Export_MainTextureTransform(m, material.occlusionTexture);
// }
// if (index != -1 && m.HasProperty("_OcclusionStrength"))
// {
// material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength");
// }
// }
// }
static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material)
{
if (m.HasProperty("_BumpMap"))
{
var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter());
var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter().GetExportTexture);
if (index != -1)
{
material.normalTexture = new glTFMaterialNormalTextureInfo()

View File

@@ -1,9 +0,0 @@
using UnityEngine;
namespace UniGLTF
{
public interface ITextureConverter
{
Texture2D GetExportTexture(Texture2D texture);
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 55c6531d46a98a845b19cbe1f938eab1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -2,7 +2,7 @@ using UnityEngine;
namespace UniGLTF
{
public class NormalConverter : ITextureConverter
public class NormalConverter
{
private Material m_decoder;
private Material GetDecoder()

View File

@@ -19,27 +19,18 @@ namespace UniGLTF
/// Metallic : glTF.b = unity.r
///
/// </summary>
public class OcclusionMetallicRoughnessConverter : ITextureConverter
public static class OcclusionMetallicRoughnessConverter
{
private readonly float _smoothnessOrRoughness;
public OcclusionMetallicRoughnessConverter(float smoothnessOrRoughness)
{
_smoothnessOrRoughness = smoothnessOrRoughness;
}
public delegate Color32 ColorConversion(Color32 metallicRoughness, Color32 occlusion);
public static Texture2D Convert(Texture2D metallicRoughnessTexture, Texture2D occlusionTexture, Material convertMaterial,
float metallicFactor, float roughnessFactor)
public static Texture2D Import(Texture2D metallicRoughnessTexture,
float metallicFactor, float roughnessFactor, Texture2D occlusionTexture)
{
if (metallicRoughnessTexture != null && occlusionTexture != null)
{
if (metallicRoughnessTexture != occlusionTexture)
{
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial);
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null);
var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32();
var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, RenderTextureReadWrite.Linear, convertMaterial);
var copyOcclusion = TextureConverter.CopyTexture(occlusionTexture, RenderTextureReadWrite.Linear, null);
var occlusionPixels = copyOcclusion.GetPixels32();
if (metallicRoughnessPixels.Length != occlusionPixels.Length)
{
@@ -47,7 +38,7 @@ namespace UniGLTF
}
for (int i = 0; i < metallicRoughnessPixels.Length; ++i)
{
metallicRoughnessPixels[i] = Import(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, occlusionPixels[i]);
metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, occlusionPixels[i]);
}
copyMetallicRoughness.SetPixels32(metallicRoughnessPixels);
copyMetallicRoughness.Apply();
@@ -56,11 +47,11 @@ namespace UniGLTF
}
else
{
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial);
var copyMetallicRoughness = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null);
var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32();
for (int i = 0; i < metallicRoughnessPixels.Length; ++i)
{
metallicRoughnessPixels[i] = Import(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]);
metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]);
}
copyMetallicRoughness.SetPixels32(metallicRoughnessPixels);
copyMetallicRoughness.Apply();
@@ -70,8 +61,8 @@ namespace UniGLTF
}
else if (metallicRoughnessTexture != null)
{
var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, convertMaterial);
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => Import(x, metallicFactor, roughnessFactor, default)).ToArray());
var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, RenderTextureReadWrite.Linear, null);
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(x, metallicFactor, roughnessFactor, default)).ToArray());
copyTexture.Apply();
copyTexture.name = metallicRoughnessTexture.name;
return copyTexture;
@@ -86,18 +77,7 @@ namespace UniGLTF
}
}
public static Texture2D GetImportTexture(Texture2D metallicRoughnessTexture, float metallicFactor, float roughnessFactor, Texture2D occlusionTexture)
{
return Convert(metallicRoughnessTexture, occlusionTexture, null, metallicFactor, roughnessFactor);
}
public Texture2D GetExportTexture(Texture2D texture)
{
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Export, null);
return converted;
}
public static Color32 Import(Color32 metallicRoughness, float metallicFactor, float roughnessFactor, Color32 occlusion)
public static Color32 ImportPixel(Color32 metallicRoughness, float metallicFactor, float roughnessFactor, Color32 occlusion)
{
var dst = new Color32
{
@@ -110,12 +90,18 @@ namespace UniGLTF
return dst;
}
public Color32 Export(Color32 src)
public static Texture2D GetExportTexture(Texture2D texture, float smoothness)
{
var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, x => Export(x, smoothness), null);
return converted;
}
public static Color32 Export(Color32 src, float smoothness)
{
var dst = new Color32
{
r = src.g, // Occlusion
g = (byte)(255 - src.a), // Roughness from Smoothness
g = (byte)(255 - src.a * smoothness), // Roughness from Smoothness
b = src.r, // Metallic
a = 255, // not used
};

View File

@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
@@ -70,7 +71,7 @@ namespace UniGLTF
return index;
}
public int ConvertAndGetIndex(Texture texture, ITextureConverter converter)
public int ConvertAndGetIndex(Texture texture, Func<Texture2D, Texture2D> converter)
{
if (texture == null)
{
@@ -84,7 +85,7 @@ namespace UniGLTF
return -1;
}
m_exportTextures[index] = converter.GetExportTexture(texture as Texture2D);
m_exportTextures[index] = converter(texture as Texture2D);
return index;
}

View File

@@ -183,7 +183,7 @@ namespace UniGLTF
{
occlusionBaseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index1.Value, false);
}
var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture);
var converted = OcclusionMetallicRoughnessConverter.Import(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture);
converted.name = param.ConvertedName;
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(converted.name, info);

View File

@@ -39,9 +39,8 @@ namespace UniGLTF
{
{
var smoothness = 1.0f;
var conv = new OcclusionMetallicRoughnessConverter(smoothness);
Assert.That(
conv.Export(new Color32(255, 255, 255, 255)),
OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness),
// r <- 0 : (Unused)
// g <- 0 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8)
// b <- 255 : Same metallic (src.r)
@@ -51,9 +50,8 @@ namespace UniGLTF
{
var smoothness = 0.5f;
var conv = new OcclusionMetallicRoughnessConverter(smoothness);
Assert.That(
conv.Export(new Color32(255, 255, 255, 255)),
OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness),
// r <- 0 : (Unused)
// g <- 63 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8)
// b <- 255 : Same metallic (src.r)
@@ -63,9 +61,8 @@ namespace UniGLTF
{
var smoothness = 0.0f;
var conv = new OcclusionMetallicRoughnessConverter(smoothness);
Assert.That(
conv.Export(new Color32(255, 255, 255, 255)),
OcclusionMetallicRoughnessConverter.Export(new Color32(255, 255, 255, 255), smoothness),
// r <- 0 : (Unused)
// g <- 255 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8)
// b <- 255 : Same metallic (src.r)
@@ -80,7 +77,7 @@ namespace UniGLTF
{
var roughnessFactor = 1.0f;
Assert.That(
OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default),
OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default),
// r <- 255 : Same metallic (src.r)
// g <- 0 : (Unused)
// b <- 0 : (Unused)
@@ -91,7 +88,7 @@ namespace UniGLTF
{
var roughnessFactor = 1.0f;
Assert.That(
OcclusionMetallicRoughnessConverter.Import(new Color32(255, 63, 255, 255), 1.0f, roughnessFactor, default),
OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 63, 255, 255), 1.0f, roughnessFactor, default),
// r <- 255 : Same metallic (src.r)
// g <- 0 : (Unused)
// b <- 0 : (Unused)
@@ -102,7 +99,7 @@ namespace UniGLTF
{
var roughnessFactor = 0.5f;
Assert.That(
OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default),
OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default),
// r <- 255 : Same metallic (src.r)
// g <- 0 : (Unused)
// b <- 0 : (Unused)
@@ -113,7 +110,7 @@ namespace UniGLTF
{
var roughnessFactor = 0.0f;
Assert.That(
OcclusionMetallicRoughnessConverter.Import(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default),
OcclusionMetallicRoughnessConverter.ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default),
// r <- 255 : Same metallic (src.r)
// g <- 0 : (Unused)
// b <- 0 : (Unused)