Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension implementation can inject texture loading strategy to ImporterContext #986

Merged
merged 6 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Assets/UniGLTF/Editor/MeshUtility/TextureSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEditor;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;

namespace MeshUtility
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using UnityEngine;
using ColorSpace = VRMShaders.ColorSpace;

namespace UniGLTF
{
Expand All @@ -10,13 +11,13 @@ public static float[] ToFloat4(this Color src, ColorSpace srcColorSpace, ColorSp
var dst = src.ConvertColorSpace(srcColorSpace, dstColorSpace);
return new float[] {dst.r, dst.g, dst.b, dst.a};
}

public static float[] ToFloat3(this Color src, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
var dst = src.ConvertColorSpace(srcColorSpace, dstColorSpace);
return new float[] {dst.r, dst.g, dst.b};
}

public static Color ToColor4(this float[] src, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
if (src == null || src.Length < 4)
Expand All @@ -27,7 +28,7 @@ public static Color ToColor4(this float[] src, ColorSpace srcColorSpace, ColorSp

return new Color(src[0], src[1], src[2], src[3]).ConvertColorSpace(srcColorSpace, dstColorSpace);
}

public static Color ToColor3(this float[] src, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
if (src == null || src.Length < 3)
Expand Down Expand Up @@ -64,4 +65,4 @@ private static Color ConvertColorSpace(this Color srcColor, ColorSpace srcColorS
}
}
}
}
}
9 changes: 7 additions & 2 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ public IAnimationImporter AnimationImporter
public TextureFactory TextureFactory { get; }
public MaterialFactory MaterialFactory { get; }

public ImporterContext(GltfParser parser, IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> externalObjectMap = null)
public ImporterContext(
GltfParser parser,
IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> externalObjectMap = null,
ITextureDeserializer textureDeserializer = null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

{
Parser = parser;
TextureDescriptorGenerator = new GltfTextureDescriptorGenerator(Parser);
MaterialDescriptorGenerator = new GltfMaterialDescriptorGenerator();

externalObjectMap = externalObjectMap ?? new Dictionary<SubAssetKey, UnityEngine.Object>();
TextureFactory = new TextureFactory(externalObjectMap
textureDeserializer = textureDeserializer ?? new UnityTextureDeserializer();

TextureFactory = new TextureFactory(textureDeserializer, externalObjectMap
.Where(x => x.Value is Texture)
.ToDictionary(x => x.Key, x => (Texture) x.Value));
MaterialFactory = new MaterialFactory(externalObjectMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;

namespace UniGLTF
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine;
using VRMShaders;

using ColorSpace = VRMShaders.ColorSpace;

namespace UniGLTF
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UniGLTF.UniUnlit;
using UnityEngine;
using VRMShaders;

using ColorSpace = VRMShaders.ColorSpace;

namespace UniGLTF
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;

namespace UniGLTF
{
Expand Down

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions Assets/VRM/Runtime/IO/VRMExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using UniGLTF;
using UniJSON;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;

using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;

namespace VRM
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/Runtime/IO/VRMMaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using UniGLTF.ShaderPropExporter;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;

namespace VRM
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UniGLTF.Extensions.VRMC_materials_mtoon;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
using OutlineWidthMode = MToon.OutlineWidthMode;
using RenderMode = MToon.RenderMode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UniGLTF.Extensions.VRMC_materials_mtoon;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
using OutlineWidthMode = UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode;

namespace UniVRM10
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM10/Runtime/Migration/MigrationMToon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using UniGLTF.Extensions.VRMC_materials_mtoon;
using UniJSON;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
using RenderMode = MToon.RenderMode;

namespace UniVRM10
Expand Down
2 changes: 0 additions & 2 deletions Assets/VRMShaders/GLTF/IO/Editor/EditorTextureSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.IO;
using System.Reflection;
using UniGLTF;
using UnityEditor;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;

namespace VRMShaders
{
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRMShaders/GLTF/IO/Runtime/ColorSpace.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace UniGLTF
namespace VRMShaders
{
public enum ColorSpace
{
sRGB,
Linear,
}
}
}
21 changes: 21 additions & 0 deletions Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Threading.Tasks;
using UnityEngine;

namespace VRMShaders
{
/// <summary>
/// 単純に Texture2D アセットを生成する機能
/// </summary>
public interface ITextureDeserializer
{
/// <summary>
/// imageData をもとに Texture2D を生成する
/// </summary>
/// <param name="imageData">データ</param>
/// <param name="useMipmap">Texture2D の mipmap が生成されるべきか否か</param>
/// <param name="colorSpace">Texture2D の色空間</param>
/// <returns></returns>
Task<Texture2D> LoadTextureAsync(byte[] imageData, bool useMipmap, ColorSpace colorSpace);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

}
}

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

2 changes: 1 addition & 1 deletion Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface ITextureExporter
/// <summary>
/// Export する Texture2D のリスト。これが gltf.textures になる
/// </summary>
IReadOnlyList<(Texture2D, UniGLTF.ColorSpace)> Exported { get; }
IReadOnlyList<(Texture2D, ColorSpace)> Exported { get; }

/// <summary>
/// 指定の Texture を、 sRGB 色空間の値を持つ Texture に出力するように指示する。
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRMShaders/GLTF/IO/Runtime/ITextureSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine;

namespace UniGLTF
namespace VRMShaders
{
/// <summary>
/// Texture2D を入力として byte[] を得る機能
Expand Down
19 changes: 1 addition & 18 deletions Assets/VRMShaders/GLTF/IO/Runtime/NormalConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;


namespace VRMShaders
{
Expand All @@ -19,28 +17,13 @@ private static Material Decoder
}
}

private static Material m_encoder;
private static Material Encoder
{
get
{
if (m_encoder == null)
{
m_encoder = new Material(Shader.Find("UniGLTF/NormalMapEncoder"));
}
return m_encoder;
}
}

// GLTF data to Unity texture
// ConvertToNormalValueFromRawColorWhenCompressionIsRequired
public static Texture2D Import(Texture2D texture)
{
return TextureConverter.CopyTexture(texture, ColorSpace.Linear, false, Encoder);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

return texture;
}

// Unity texture to GLTF data
// ConvertToRawColorWhenNormalValueIsCompressed
public static Texture2D Export(Texture texture)
{
return TextureConverter.CopyTexture(texture, ColorSpace.Linear, false, Decoder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Linq;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;


namespace VRMShaders
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEngine;
using VRMShaders;

namespace UniGLTF
namespace VRMShaders
{
public sealed class RuntimeTextureSerializer : ITextureSerializer
{
Expand Down
3 changes: 1 addition & 2 deletions Assets/VRMShaders/GLTF/IO/Runtime/TextureConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Linq;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;


namespace VRMShaders
{
Expand All @@ -11,6 +9,7 @@ public static class TextureConverter
public static Texture2D CreateEmptyTextureWithSettings(Texture src, ColorSpace dstColorSpace, bool dstNeedsAlpha)
{
var texFormat = dstNeedsAlpha ? TextureFormat.ARGB32 : TextureFormat.RGB24;
texFormat = TextureFormat.ARGB32;
var dst = new Texture2D(src.width, src.height, texFormat, src.HasMipMap(), dstColorSpace == ColorSpace.Linear);
dst.name = src.name;
dst.anisoLevel = src.anisoLevel;
Expand Down
3 changes: 0 additions & 3 deletions Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using UniGLTF;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;


namespace VRMShaders
{
Expand Down
Loading