Skip to content

Commit

Permalink
Merge pull request #780 from ousttrue/fix/roughness_smooth_conversion
Browse files Browse the repository at this point in the history
MetallicRoughnessOcclusion テクスチャーの取り廻しを中心に改修
  • Loading branch information
ousttrue authored Mar 17, 2021
2 parents 0b3c348 + 25b70b9 commit f55cd3e
Show file tree
Hide file tree
Showing 68 changed files with 1,243 additions and 1,101 deletions.
8 changes: 3 additions & 5 deletions Assets/UniGLTF/Editor/UniGLTF/AssetTextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public static class AssetTextureLoader
public static Task<Texture2D> LoadTaskAsync(UnityPath m_assetPath,
glTF gltf, int textureIndex)
{
var textureType = TextureIO.GetglTFTextureType(gltf, textureIndex);
var colorSpace = TextureIO.GetColorSpace(textureType);
var isLinear = colorSpace == RenderTextureReadWrite.Linear;
var sampler = gltf.GetSamplerFromTextureIndex(textureIndex);
var colorSpace = gltf.GetColorSpace(textureIndex);

//
// texture from assets
Expand All @@ -25,7 +22,7 @@ public static Task<Texture2D> LoadTaskAsync(UnityPath m_assetPath,
else
{
importer.maxTextureSize = 8192;
importer.sRGBTexture = !isLinear;
importer.sRGBTexture = colorSpace == RenderTextureReadWrite.sRGB;
importer.SaveAndReimport();
}

Expand All @@ -50,6 +47,7 @@ public static Task<Texture2D> LoadTaskAsync(UnityPath m_assetPath,
importer.SaveAndReimport();
}

var sampler = gltf.GetSamplerFromTextureIndex(textureIndex);
if (sampler != null)
{
TextureSamplerUtil.SetSampler(Texture, sampler);
Expand Down
16 changes: 13 additions & 3 deletions Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void Dispose()
}
}

static bool s_foldMaterials;
static bool s_foldTextures;
static bool s_foldMaterials = true;
static bool s_foldTextures = true;

public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser)
{
Expand All @@ -56,7 +56,17 @@ public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser)
s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
if (s_foldTextures)
{
DrawRemapGUI<UnityEngine.Texture2D>(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x => x.ConvertedName));
DrawRemapGUI<UnityEngine.Texture2D>(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x =>
{
switch (x.TextureType)
{
case GetTextureParam.TextureTypes.NormalMap:
return x.GltflName;

default:
return x.ConvertedName;
}
}));
}

if (GUILayout.Button("Clear"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ public class GlbScriptedImporter : ScriptedImporter

public override void OnImportAsset(AssetImportContext ctx)
{
try
{
ScriptedImporterImpl.Import(this, ctx, m_reverseAxis);
}
catch (System.Exception ex)
{
Debug.LogError(ex);
}
ScriptedImporterImpl.Import(this, ctx, m_reverseAxis);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ public class GltfScriptedImporter : ScriptedImporter

public override void OnImportAsset(AssetImportContext ctx)
{
try
{
ScriptedImporterImpl.Import(this, ctx, m_reverseAxis);
}
catch (System.Exception ex)
{
Debug.LogError(ex);
}
ScriptedImporterImpl.Import(this, ctx, m_reverseAxis);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
Expand Down Expand Up @@ -40,6 +41,12 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat(
EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent))))
{
// settings TextureImporters
foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser.GLTF))
{
TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap);
}

loaded.InvertAxis = reverseAxis;
loaded.Load();
loaded.ShowMeshes();
Expand Down Expand Up @@ -69,8 +76,7 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
{
switch (texParam.TextureType)
{
case GetTextureParam.METALLIC_GLOSS_PROP:
case GetTextureParam.OCCLUSION_PROP:
case GetTextureParam.TextureTypes.StandardMap:
break;

default:
Expand Down
34 changes: 3 additions & 31 deletions Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public void Extract(GetTextureParam param, bool hasUri)

switch (param.TextureType)
{
case GetTextureParam.METALLIC_GLOSS_PROP:
case GetTextureParam.OCCLUSION_PROP:
case GetTextureParam.TextureTypes.StandardMap:
{
// write converted texture
targetPath = $"{m_path}/{param.ConvertedName}.png";
Expand Down Expand Up @@ -130,40 +129,13 @@ public static void ExtractTextures(string assetPath, TextureEnumerator textureEn

EditorApplication.delayCall += () =>
{
// Wait for the texture assets to be imported

foreach (var kv in extractor.Textures)
{
var targetPath = kv.Key;
var param = kv.Value;

// TextureImporter
var targetTextureImporter = AssetImporter.GetAtPath(targetPath) as TextureImporter;
if (targetTextureImporter != null)
{
switch (param.TextureType)
{
case GetTextureParam.OCCLUSION_PROP:
case GetTextureParam.METALLIC_GLOSS_PROP:
#if VRM_DEVELOP
Debug.Log($"{targetPath} => linear");
#endif
targetTextureImporter.sRGBTexture = false;
targetTextureImporter.SaveAndReimport();
break;

case GetTextureParam.NORMAL_PROP:
#if VRM_DEVELOP
Debug.Log($"{targetPath} => normalmap");
#endif
targetTextureImporter.textureType = TextureImporterType.NormalMap;
targetTextureImporter.SaveAndReimport();
break;
}
}
else
{
throw new FileNotFoundException(targetPath);
}

// remap
var externalObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Texture2D>(targetPath);
if (externalObject != null)
Expand Down
74 changes: 74 additions & 0 deletions Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace UniGLTF
{
public static class TextureImporterConfigurator
{
public static void ConfigureNormalMap(Texture2D texture)
{
var path = UnityPath.FromAsset(texture);
if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter)
{
#if VRM_DEVELOP
Debug.Log($"{path} => normalmap");
#endif
textureImporter.textureType = TextureImporterType.NormalMap;
textureImporter.SaveAndReimport();
}
else
{
throw new System.IO.FileNotFoundException($"{path}");
}
}

public static void ConfigureLinear(Texture2D texture)
{
var path = UnityPath.FromAsset(texture);
if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter)
{
#if VRM_DEVELOP
Debug.Log($"{path} => linear");
#endif
textureImporter.sRGBTexture = false;
textureImporter.SaveAndReimport();
}
else
{
throw new System.IO.FileNotFoundException($"{path}");
}
}

public static void Configure(GetTextureParam textureInfo, IDictionary<string, Texture2D> ExternalMap)
{
switch (textureInfo.TextureType)
{
case GetTextureParam.TextureTypes.NormalMap:
{
if (ExternalMap.TryGetValue(textureInfo.GltflName, out Texture2D external))
{
ConfigureNormalMap(external);
}
}
break;

case GetTextureParam.TextureTypes.StandardMap:
{
if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external))
{
ConfigureLinear(external);
}
}
break;

case GetTextureParam.TextureTypes.sRGB:
break;

default:
throw new NotImplementedException();
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 7 additions & 21 deletions Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ namespace UniGLTF
{
public enum glTFTextureTypes
{
BaseColor,
Metallic,
OcclusionMetallicRoughness,
Normal,
Occlusion,
Emissive,
Unknown
SRGB,
Linear,
}

public interface IglTFTextureinfo
Expand Down Expand Up @@ -38,19 +36,13 @@ public abstract class glTFTextureInfo : IglTFTextureinfo
[Serializable]
public class glTFMaterialBaseColorTextureInfo : glTFTextureInfo
{
public override glTFTextureTypes TextureType
{
get { return glTFTextureTypes.BaseColor; }
}
public override glTFTextureTypes TextureType => glTFTextureTypes.SRGB;
}

[Serializable]
public class glTFMaterialMetallicRoughnessTextureInfo : glTFTextureInfo
{
public override glTFTextureTypes TextureType
{
get { return glTFTextureTypes.Metallic; }
}
public override glTFTextureTypes TextureType => glTFTextureTypes.OcclusionMetallicRoughness;
}

[Serializable]
Expand All @@ -70,19 +62,13 @@ public class glTFMaterialOcclusionTextureInfo : glTFTextureInfo
[JsonSchema(Minimum = 0.0, Maximum = 1.0)]
public float strength = 1.0f;

public override glTFTextureTypes TextureType
{
get { return glTFTextureTypes.Occlusion; }
}
public override glTFTextureTypes TextureType => glTFTextureTypes.OcclusionMetallicRoughness;
}

[Serializable]
public class glTFMaterialEmissiveTextureInfo : glTFTextureInfo
{
public override glTFTextureTypes TextureType
{
get { return glTFTextureTypes.Emissive; }
}
public override glTFTextureTypes TextureType => glTFTextureTypes.SRGB;
}

[Serializable]
Expand Down
16 changes: 0 additions & 16 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs

This file was deleted.

1 change: 1 addition & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public ImporterContext(GltfParser parser,
};
#endif
}

m_textureFactory = new TextureFactory(loadTextureAsync, externalObjectMap);
m_materialFactory = new MaterialFactory(GLTF, Storage, externalObjectMap);
}
Expand Down
12 changes: 0 additions & 12 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialExporter.cs.meta

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f55cd3e

Please sign in to comment.