TextureExporter.ExportLinear place holder

This commit is contained in:
ousttrue 2021-03-17 17:50:47 +09:00
parent 6d211f2ec5
commit 25b70b97d2
7 changed files with 122 additions and 83 deletions

View File

@ -8,6 +8,7 @@ namespace UniGLTF
OcclusionMetallicRoughness,
Normal,
SRGB,
Linear,
}
public interface IglTFTextureinfo

View File

@ -20,6 +20,7 @@ namespace UniGLTF
NormalMap,
// Occlusion + Metallic + Smoothness
StandardMap,
Linear,
}
public static string RemoveSuffix(string src)

View File

@ -0,0 +1,94 @@
using UnityEngine;
namespace UniGLTF
{
public static class GltfTextureExporter
{
/// <summary>
/// 画像のバイト列を得る
/// </summary>
/// <param name="bytes"></param>
/// <param name="texture"></param>
/// <returns></returns>
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"
);
}
/// <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)
{
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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: edc800243b783ea4392e1d916789c803
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -100,7 +100,7 @@ namespace UniGLTF
}
/// <summary>
/// sRGBなテクスチャーを処理
/// sRGBなテクスチャーを処理し、index を確定させ
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
@ -135,7 +135,17 @@ namespace UniGLTF
}
/// <summary>
/// Standard の Metallic, Smoothness, Occlusion をまとめる
/// Linearなテクスチャーを処理し、index を確定させる
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
public int ExportLinear(Texture src)
{
throw new NotImplementedException();
}
/// <summary>
/// Standard の Metallic, Smoothness, Occlusion をまとめ、index を確定させる
/// </summary>
/// <param name="metallicSmoothTexture"></param>
/// <param name="smoothness"></param>
@ -175,7 +185,7 @@ namespace UniGLTF
}
/// <summary>
/// Normal のテクスチャを変換
/// Normal のテクスチャを変換し index を確定させ
/// </summary>
/// <param name="normalTexture"></param>
/// <returns></returns>
@ -210,83 +220,5 @@ namespace UniGLTF
return index;
}
/// <summary>
/// 画像のバイト列を得る
/// </summary>
/// <param name="bytes"></param>
/// <param name="texture"></param>
/// <returns></returns>
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"
);
}
/// <summary>
///
/// </summary>
/// <param name="gltf"></param>
/// <param name="bufferIndex"></param>
/// <param name="texture"></param>
/// <returns></returns>
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;
}
}
}

View File

@ -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

View File

@ -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;