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

Refactoring Serializing Textures #961

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -36,7 +36,7 @@ static void SaveAsPng(bool sRGB)
return;
}

var (tex, mime) = TextureExporter.GetTextureBytesWithMime(texture, sRGB ? ColorSpace.sRGB : ColorSpace.Linear);
var (tex, mime) = new EditorTextureSerializer().ExportBytesWithMime(texture, sRGB ? ColorSpace.sRGB : ColorSpace.Linear);

File.WriteAllBytes(path, tex);
Debug.Log($"save: {path}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected override void ExportPath(string path)
UseSparseAccessorForMorphTarget = m_settings.Sparse,
DivideVertexBuffer = m_settings.DivideVertexBuffer,
};
exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(settings, new EditorTextureSerializer());
}

if (isGlb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ public static class GltfTextureExporter
{
/// <summary>
/// gltf に texture を足す
///
///
/// * textures
/// * samplers
/// * images
/// * bufferViews
///
///
/// を更新し、textures の index を返す
///
///
/// </summary>
/// <param name="gltf"></param>
/// <param name="bufferIndex"></param>
/// <param name="texture"></param>
/// <returns>gltf texture index</returns>
public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture, ColorSpace textureColorSpace, gltfExporter.GetBytesWithMimeFromTexture2D getTextureBytes)
public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture, ColorSpace textureColorSpace, ITextureSerializer textureSerializer)
{
var bytesWithMime = getTextureBytes(texture, textureColorSpace);
var bytesWithMime = textureSerializer.ExportBytesWithMime(texture, textureColorSpace);

// add view
var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE);
Expand Down
23 changes: 7 additions & 16 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public GameObject Copy

/// <summary>
/// Mesh毎に、元のBlendShapeIndex => ExportされたBlendShapeIndex の対応を記録する
///
///
/// BlendShape が空の場合にスキップするので
/// </summary>
/// <value></value>
Expand Down Expand Up @@ -148,7 +148,7 @@ static glTFNode ExportNode(Transform x, List<Transform> nodes, List<MeshWithRend
if (x.gameObject.activeInHierarchy)
{
var meshRenderer = x.GetComponent<MeshRenderer>();

if (meshRenderer != null)
{
var meshFilter = x.GetComponent<MeshFilter>();
Expand Down Expand Up @@ -214,21 +214,12 @@ private static bool TryGetSameMeshIndex(List<MeshWithRenderer> meshWithRenderers
return false;
}

public virtual void ExportExtensions(GetBytesWithMimeFromTexture2D getTextureBytes)
public virtual void ExportExtensions(ITextureSerializer textureSerializer)
{
// do nothing
}

/// <summary>
/// Texture2D から実際のバイト列を取得するデリゲート。
///
/// textureColorSpace は Texture2D をコピーする際に用いる。
/// Texture2D 単体では、色空間を知ることができないため。
/// 一般には、その Texture2D がアサインされる glTF のプロパティの仕様が定める色空間と一致する。
/// </summary>
public delegate (byte[] bytes, string mime) GetBytesWithMimeFromTexture2D(Texture2D texture, ColorSpace textureColorSpace);

public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture, bool> useAsset, GetBytesWithMimeFromTexture2D getTextureBytes)
public virtual void Export(MeshExportSettings meshExportSettings, ITextureSerializer textureSerializer)
{
var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
var bufferIndex = glTF.AddBuffer(bytesBuffer);
Expand All @@ -247,7 +238,7 @@ public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture,
#region Materials and Textures
Materials = uniqueUnityMeshes.SelectMany(x => x.Renderer.sharedMaterials).Where(x => x != null).Distinct().ToList();

TextureManager = new TextureExporter(useAsset);
TextureManager = new TextureExporter(textureSerializer);

var materialExporter = CreateMaterialExporter();
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList();
Expand Down Expand Up @@ -366,13 +357,13 @@ public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture,
#endregion
#endif

ExportExtensions(getTextureBytes);
ExportExtensions(textureSerializer);

// Extension で Texture が増える場合があるので最後に呼ぶ
for (int i = 0; i < TextureManager.Exported.Count; ++i)
{
var (unityTexture, colorSpace) = TextureManager.Exported[i];
glTF.PushGltfTexture(bufferIndex, unityTexture, colorSpace, getTextureBytes);
glTF.PushGltfTexture(bufferIndex, unityTexture, colorSpace, textureSerializer);
}
}
#endregion
Expand Down
4 changes: 2 additions & 2 deletions Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static Byte[] Export(GameObject root)
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(root);
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(MeshExportSettings.Default, new EditorTextureSerializer());
return gltf.ToGlbBytes();
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ static void RuntimeLoadExport(FileInfo gltf, int subStrStart)

if (Skip.Contains(gltf.Directory.Parent.Name))
{
// Export issue:
// Export issue:
// skip
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TextureTransformTest()
filterMode = FilterMode.Bilinear,
};

var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
var textureManager = new TextureExporter(new EditorTextureSerializer());
var srcMaterial = new Material(Shader.Find("Standard"));

var offset = new Vector2(0.3f, 0.2f);
Expand Down Expand Up @@ -242,7 +242,7 @@ public void MaterialExportTest()
material.SetColor("_EmissionColor", new Color(0, 1, 2, 1));
material.EnableKeyword("_EMISSION");
var materialExporter = new MaterialExporter();
var textureExportManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
var textureExportManager = new TextureExporter(new EditorTextureSerializer());
var gltfMaterial = materialExporter.ExportMaterial(material, textureExportManager);

Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static Texture2D AssignTextureToMaterialPropertyAndExportAndExtract(Text
UseSparseAccessorForMorphTarget = false,
DivideVertexBuffer = false,
};
exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(settings, new EditorTextureSerializer());
}
Assert.AreEqual(1, gltf.images.Count);
var exportedImage = gltf.images[0];
Expand Down
2 changes: 1 addition & 1 deletion Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TextureExportTest()
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Trilinear,
};
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset);
var textureManager = new TextureExporter(new EditorTextureSerializer());

var material = new Material(Shader.Find("Standard"));
material.mainTexture = tex0;
Expand Down
8 changes: 4 additions & 4 deletions Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void UniGLTFSimpleSceneTest()
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(MeshExportSettings.Default, new EditorTextureSerializer());

// remove empty buffer
gltf.buffers.Clear();
Expand Down Expand Up @@ -298,7 +298,7 @@ public void GlTFToJsonTest()
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(CreateSimpleScene());
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(MeshExportSettings.Default, new EditorTextureSerializer());
}

var expected = gltf.ToJson().ParseAsJson();
Expand Down Expand Up @@ -534,7 +534,7 @@ public void SameMeshButDifferentMaterialExport()
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(UniGLTF.MeshExportSettings.Default, new EditorTextureSerializer());

json = gltf.ToJson();
}
Expand Down Expand Up @@ -618,7 +618,7 @@ public void MeshHasNoRendererTest()
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(UniGLTF.MeshExportSettings.Default, new EditorTextureSerializer());

json = gltf.ToJson();
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void ImportExportTest()
*/
importedJson.RemoveValue(Utf8String.From("/bufferViews/*/byteStride"));

var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, new EditorTextureSerializer());

// TODO: Check contents in JSON
/*var exportJson = */
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static UniGLTF.glTFMaterial ExportLoaded(string resourceName)
{
var material = Resources.Load<Material>(resourceName);
var exporter = new VRMMaterialExporter();
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
var textureManager = new TextureExporter(new EditorTextureSerializer());
var exported = exporter.ExportMaterial(material, textureManager);

// parse glTFExtensionExport to glTFExtensionImport
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void OnExportClicked()
return;
}

var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, (Texture _) => false, TextureExporter.GetTextureBytesWithMime);
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, new RuntimeTextureSerializer());
var bytes = vrm.ToGlbBytes();
File.WriteAllBytes(path, bytes);
Debug.LogFormat("export to {0}", path);
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRM/Editor/Format/VRMEditorExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static void ForceUniqueName(Transform transform, Dictionary<string, int> nameCou
}

/// <summary>
///
///
/// </summary>
/// <param name="path"></param>
/// <param name="settings"></param>
Expand Down Expand Up @@ -225,7 +225,7 @@ static byte[] Export(GameObject exportRoot, VRMMetaObject meta,
using (var exporter = new VRMExporter(gltf))
{
exporter.Prepare(target);
exporter.Export(settings.MeshExportSettings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
exporter.Export(settings.MeshExportSettings, new EditorTextureSerializer());
}
var bytes = gltf.ToGlbBytes();
Debug.LogFormat("Export elapsed {0}", sw.Elapsed);
Expand Down
8 changes: 4 additions & 4 deletions Assets/VRM/Runtime/IO/VRMExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ protected override IMaterialExporter CreateMaterialExporter()
return new VRMMaterialExporter();
}

public static glTF Export(MeshExportSettings configuration, GameObject go, Func<Texture, bool> useAsset, GetBytesWithMimeFromTexture2D getTextureBytes)
public static glTF Export(MeshExportSettings configuration, GameObject go, ITextureSerializer textureSerializer)
{
var gltf = new glTF();
using (var exporter = new VRMExporter(gltf))
{
exporter.Prepare(go);
exporter.Export(configuration, useAsset, getTextureBytes);
exporter.Export(configuration, textureSerializer);
}
return gltf;
}
Expand All @@ -33,7 +33,7 @@ public VRMExporter(glTF gltf) : base(gltf, Axises.Z)
gltf.extensionsUsed.Add(glTF_VRM_extensions.ExtensionName);
}

public override void ExportExtensions(GetBytesWithMimeFromTexture2D getTextureBytes)
public override void ExportExtensions(ITextureSerializer textureSerializer)
{
// avatar
var animator = Copy.GetComponent<Animator>();
Expand Down Expand Up @@ -111,7 +111,7 @@ public override void ExportExtensions(GetBytesWithMimeFromTexture2D getTextureBy
VRM.meta.title = meta.Title;
if (meta.Thumbnail != null)
{
VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail, ColorSpace.sRGB, getTextureBytes);
VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail, ColorSpace.sRGB, textureSerializer);
}

VRM.meta.licenseType = meta.LicenseType;
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/Tests/MToonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TextureTransformTest()
filterMode = FilterMode.Bilinear,
};

var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset);
var textureManager = new TextureExporter(new EditorTextureSerializer());
var srcMaterial = new Material(Shader.Find("VRM/MToon"));

var offset = new Vector2(0.3f, 0.2f);
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/Tests/VRMLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void VrmTestModelsTests()
try
{
// export
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, new EditorTextureSerializer());

// re import
if (vrm != null)
Expand Down
14 changes: 7 additions & 7 deletions Assets/VRM/Tests/VrmDividedMeshTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ static IEnumerable<Mesh> GetMeshes(GameObject gameObject)

/// <summary>
/// positions: [
/// {1, 1, 0}
/// {1, 1, 1}
/// {1, 1, 2}
/// {1, 1, 3}
/// {1, 1, 4}
/// {1, 1, 5}
/// {1, 1, 0}
/// {1, 1, 1}
/// {1, 1, 2}
/// {1, 1, 3}
/// {1, 1, 4}
/// {1, 1, 5}
/// ]
/// submesh
/// 0 1 2
Expand All @@ -73,7 +73,7 @@ public void ExportDividedMeshTest()
ExportOnlyBlendShapePosition = true,
ExportTangents = false,
UseSparseAccessorForMorphTarget = true,
}, loaded, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
}, loaded, new EditorTextureSerializer());
var bytes = exported.ToGlbBytes();
var divided = Load(bytes, path);

Expand Down
6 changes: 3 additions & 3 deletions Assets/VRM10/Editor/Vrm10ExportDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected override IEnumerable<Validator> ValidatorFactory()

protected override void OnLayout()
{
// m_settings, m_meshes.Meshes
// m_settings, m_meshes.Meshes
m_meshes.SetRoot(State.ExportRoot, m_settings.MeshExportSettings);
}

Expand Down Expand Up @@ -297,9 +297,9 @@ protected override void ExportPath(string path)
model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);

// export vrm-1.0
var exporter = new UniVRM10.Vrm10Exporter(AssetTextureUtil.IsTextureEditorAsset);
var exporter = new UniVRM10.Vrm10Exporter(new EditorTextureSerializer());
var option = new VrmLib.ExportArgs();
exporter.Export(root, model, converter, option, AssetTextureUtil.GetTextureBytesWithMime, Meta ? Meta : m_tmpMeta);
exporter.Export(root, model, converter, option, Meta ? Meta : m_tmpMeta);

var exportedBytes = exporter.Storage.ToBytes();

Expand Down
Loading