From 25b70b97d2ba7cede163da509553ea6cd27bd656 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 17 Mar 2021 17:50:47 +0900 Subject: [PATCH] TextureExporter.ExportLinear place holder --- .../Runtime/UniGLTF/Format/glTFMaterial.cs | 1 + .../UniGLTF/IO/TextureIO/GetTextureParam.cs | 1 + .../IO/TextureIO/GltfTextureExporter.cs | 94 +++++++++++++++++++ .../IO/TextureIO/GltfTextureExporter.cs.meta | 11 +++ .../UniGLTF/IO/TextureIO/TextureExporter.cs | 94 +++---------------- .../Runtime/UniGLTF/IO/gltfExporter.cs | 2 +- Assets/VRM/Runtime/IO/VRMExporter.cs | 2 +- 7 files changed, 122 insertions(+), 83 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs index f91dd9d41..4fe760250 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFMaterial.cs @@ -8,6 +8,7 @@ namespace UniGLTF OcclusionMetallicRoughness, Normal, SRGB, + Linear, } public interface IglTFTextureinfo diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs index eb4e5264a..8bfcc14a3 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GetTextureParam.cs @@ -20,6 +20,7 @@ namespace UniGLTF NormalMap, // Occlusion + Metallic + Smoothness StandardMap, + Linear, } public static string RemoveSuffix(string src) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs new file mode 100644 index 000000000..66c7664ab --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs @@ -0,0 +1,94 @@ +using UnityEngine; + +namespace UniGLTF +{ + public static class GltfTextureExporter + { + + /// + /// 画像のバイト列を得る + /// + /// + /// + /// + static (byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) + { +#if UNITY_EDITOR + 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 + + return + ( + texture.EncodeToPNG(), + "image/png" + ); + } + + /// + /// gltf に texture を足す + /// + /// * textures + /// * samplers + /// * images + /// * bufferViews + /// + /// を更新し、textures の index を返す + /// + /// + /// + /// + /// + /// gltf texture index + public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture) + { + var bytesWithMime = GetBytesWithMime(texture); + + // add view + var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); + var viewIndex = gltf.AddBufferView(view); + + // add image + var imageIndex = gltf.images.Count; + gltf.images.Add(new glTFImage + { + name = GetTextureParam.RemoveSuffix(texture.name), + bufferView = viewIndex, + mimeType = bytesWithMime.mine, + }); + + // add sampler + var samplerIndex = gltf.samplers.Count; + var sampler = TextureSamplerUtil.Export(texture); + gltf.samplers.Add(sampler); + + // add texture + var textureIndex = gltf.textures.Count; + gltf.textures.Add(new glTFTexture + { + sampler = samplerIndex, + source = imageIndex, + }); + + return textureIndex; + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta new file mode 100644 index 000000000..df9f76019 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: edc800243b783ea4392e1d916789c803 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs index 486bdece0..4cf0c3f5e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureExporter.cs @@ -100,7 +100,7 @@ namespace UniGLTF } /// - /// sRGBなテクスチャーを処理する + /// sRGBなテクスチャーを処理し、index を確定させる /// /// /// @@ -135,7 +135,17 @@ namespace UniGLTF } /// - /// Standard の Metallic, Smoothness, Occlusion をまとめる + /// Linearなテクスチャーを処理し、index を確定させる + /// + /// + /// + public int ExportLinear(Texture src) + { + throw new NotImplementedException(); + } + + /// + /// Standard の Metallic, Smoothness, Occlusion をまとめ、index を確定させる /// /// /// @@ -175,7 +185,7 @@ namespace UniGLTF } /// - /// Normal のテクスチャを変換する + /// Normal のテクスチャを変換し index を確定させる /// /// /// @@ -210,83 +220,5 @@ namespace UniGLTF return index; } - - /// - /// 画像のバイト列を得る - /// - /// - /// - /// - static (Byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) - { -#if UNITY_EDITOR - 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 - - return - ( - texture.EncodeToPNG(), - "image/png" - ); - } - - /// - /// - /// - /// - /// - /// - /// - static public int ExportTexture(glTF gltf, int bufferIndex, Texture2D texture) - { - var bytesWithMime = GetBytesWithMime(texture); - - // add view - var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); - var viewIndex = gltf.AddBufferView(view); - - // add image - var imageIndex = gltf.images.Count; - gltf.images.Add(new glTFImage - { - name = GetTextureParam.RemoveSuffix(texture.name), - bufferView = viewIndex, - mimeType = bytesWithMime.mine, - }); - - // add sampler - var samplerIndex = gltf.samplers.Count; - var sampler = TextureSamplerUtil.Export(texture); - gltf.samplers.Add(sampler); - - // add texture - var textureIndex = gltf.textures.Count; - gltf.textures.Add(new glTFTexture - { - sampler = samplerIndex, - source = imageIndex, - }); - - return textureIndex; - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 5ac336716..424b02de0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -188,7 +188,7 @@ namespace UniGLTF for (int i = 0; i < TextureManager.Exported.Count; ++i) { var unityTexture = TextureManager.Exported[i]; - TextureExporter.ExportTexture(glTF, bufferIndex, unityTexture); + glTF.PushGltfTexture(bufferIndex, unityTexture); } #endregion diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index a24c3e240..1245c9196 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -112,7 +112,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); + VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail); } VRM.meta.licenseType = meta.LicenseType;