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

GetBytesWithMime を VRMShaders に移動 #863

Merged
merged 1 commit into from
Apr 12, 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
3 changes: 1 addition & 2 deletions Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ private static void Export(GameObject go, string path, MeshExportSettings settin
using (var exporter = new gltfExporter(gltf, inverseAxis))
{
exporter.Prepare(go);
exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset);
exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
}


if (isGlb)
{
var bytes = gltf.ToGlbBytes();
Expand Down
64 changes: 3 additions & 61 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,6 @@ namespace UniGLTF
{
public static class GltfTextureExporter
{

/// <summary>
/// 画像のバイト列を得る
/// </summary>
/// <param name="bytes"></param>
/// <param name="texture"></param>
/// <returns></returns>
public static (byte[] bytes, string mine) GetBytesWithMime(Texture2D texture)
{
#if UNITY_EDITOR
Copy link
Contributor

Choose a reason for hiding this comment

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

これもともと Runtime のほうにあったんですね

var path = UnityPath.FromAsset(texture);
if (path.IsUnderAssetsFolder)
{
if (path.Extension == ".png")
{
return
(
System.IO.File.ReadAllBytes(path.FullPath),
"image/png"
);
}
if (path.Extension == ".jpg")
{
return
(
System.IO.File.ReadAllBytes(path.FullPath),
"image/jpeg"
);
}
}
#endif

try
{
var png = texture.EncodeToPNG();
if (png != null)
{
return (png, "image/png");
}
}
catch (Exception ex)
{
// fail to EncodeToPng
// System.ArgumentException: not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.

Debug.LogWarning(ex);
}

{
// try copy and EncodeToPng
var copy = TextureConverter.CopyTexture(texture, TextureImportTypes.sRGB, null);
var png = copy.EncodeToPNG();
UnityEngine.Object.DestroyImmediate(copy);

return (png, "image/png");
}
}

/// <summary>
/// gltf に texture を足す
///
Expand All @@ -79,9 +21,9 @@ public static (byte[] bytes, string mine) GetBytesWithMime(Texture2D texture)
/// <param name="bufferIndex"></param>
/// <param name="texture"></param>
/// <returns>gltf texture index</returns>
public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture)
public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture, Func<Texture2D, (byte[] bytes, string mime)> getTextureBytes)
{
var bytesWithMime = GetBytesWithMime(texture);
var bytesWithMime = getTextureBytes(texture);

// add view
var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE);
Expand All @@ -93,7 +35,7 @@ public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D tex
{
name = TextureImportName.RemoveSuffix(texture.name),
bufferView = viewIndex,
mimeType = bytesWithMime.mine,
mimeType = bytesWithMime.mime,
});

// add sampler
Expand Down
8 changes: 4 additions & 4 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ static glTFNode ExportNode(Transform x, List<Transform> nodes, List<Renderer> re
return node;
}

public virtual void ExportExtensions()
public virtual void ExportExtensions(Func<Texture2D, (byte[], string)> getTextureBytes)
{

}

public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture, bool> useAsset)
public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture, bool> useAsset, Func<Texture2D, (byte[], string)> getTextureBytes)
{
var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
var bufferIndex = glTF.AddBuffer(bytesBuffer);
Expand Down Expand Up @@ -302,13 +302,13 @@ public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture,
#endregion
#endif

ExportExtensions();
ExportExtensions(getTextureBytes);

// Extension で Texture が増える場合があるので最後に呼ぶ
for (int i = 0; i < TextureManager.Exported.Count; ++i)
{
var unityTexture = TextureManager.Exported[i];
glTF.PushGltfTexture(bufferIndex, unityTexture);
glTF.PushGltfTexture(bufferIndex, unityTexture, getTextureBytes);
}
}
#endregion
Expand Down
8 changes: 0 additions & 8 deletions Assets/UniGLTF/Tests/Resources.meta

This file was deleted.

88 changes: 0 additions & 88 deletions Assets/UniGLTF/Tests/Resources/4x4.png.meta

This file was deleted.

2 changes: 1 addition & 1 deletion 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);
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
return gltf.ToGlbBytes();
}
}
Expand Down
8 changes: 0 additions & 8 deletions Assets/UniGLTF/Tests/UniGLTF/Resources.meta

This file was deleted.

Binary file removed Assets/UniGLTF/Tests/UniGLTF/Resources/4x4.png
Binary file not shown.
21 changes: 1 addition & 20 deletions Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void TextureExportTest()
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Trilinear,
};
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset);

var material = new Material(Shader.Find("Standard"));
material.mainTexture = tex0;
Expand All @@ -31,24 +31,5 @@ public void TextureExportTest()
Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter);
Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter);
}


[Test]
public void NotReadable()
{
var readonlyTexture = Resources.Load<Texture2D>("4x4");
Assert.False(readonlyTexture.isReadable);
var (bytes, mime) = GltfTextureExporter.GetBytesWithMime(readonlyTexture);
Assert.NotNull(bytes);
}

[Test]
public void Compressed()
{
var readonlyTexture = Resources.Load<Texture2D>("4x4compressed");
Assert.False(readonlyTexture.isReadable);
var (bytes, mime) = GltfTextureExporter.GetBytesWithMime(readonlyTexture);
Assert.NotNull(bytes);
}
}
}
6 changes: 3 additions & 3 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 );
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);

// 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 );
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
}

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 );
exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);

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 );
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);

// TODO: Check contents in JSON
/*var exportJson = */
Expand Down
3 changes: 2 additions & 1 deletion Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;
using UnityEngine.UI;
using VRM;
using VRMShaders;

namespace VRM.Samples
{
Expand Down Expand Up @@ -95,7 +96,7 @@ void OnExportClicked()
return;
}

var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, _ => false);
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, (Texture _) => false, TextureExporter.GetTextureBytesWithMime);
var bytes = vrm.ToGlbBytes();
File.WriteAllBytes(path, bytes);
Debug.LogFormat("export to {0}", path);
Expand Down
5 changes: 2 additions & 3 deletions Assets/VRM.Samples/VRM.Samples.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
"UniGLTF",
"VRMShaders.GLTF.IO.Runtime"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
"defineConstraints": []
}
2 changes: 1 addition & 1 deletion Assets/VRM/Editor/Format/VRMEditorExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static void Export(string path, GameObject exportRoot, VRMMetaObject meta,
using (var exporter = new VRMExporter(gltf))
{
exporter.Prepare(target);
exporter.Export(settings.MeshExportSettings, AssetTextureUtil.IsTextureEditorAsset );
exporter.Export(settings.MeshExportSettings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
}
var bytes = gltf.ToGlbBytes();
File.WriteAllBytes(path, bytes);
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 @@ -14,13 +14,13 @@ protected override IMaterialExporter CreateMaterialExporter()
return new VRMMaterialExporter();
}

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

public override void ExportExtensions()
public override void ExportExtensions(Func<Texture2D, (byte[], string)> getTextureBytes)
{
// avatar
var animator = Copy.GetComponent<Animator>();
Expand Down Expand Up @@ -110,7 +110,7 @@ public override void ExportExtensions()
VRM.meta.title = meta.Title;
if (meta.Thumbnail != null)
{
VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail);
VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail, getTextureBytes);
}

VRM.meta.licenseType = meta.LicenseType;
Expand Down
Loading