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

add ITextureExporter #686

Merged
merged 1 commit into from
Jan 25, 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
16 changes: 16 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
#endif


namespace UniGLTF
{
public interface ITextureExporter
{
(Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType);
IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m);
int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType);
}
}
11 changes: 11 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta

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

76 changes: 29 additions & 47 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace UniGLTF
{
public static class TextureIO

public class TextureIO : ITextureExporter
{
public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType)
{
Expand Down Expand Up @@ -80,44 +81,25 @@ public static void MarkTextureAssetAsNormalMap(string assetPath)
}
#endif

public struct TextureExportItem
{
public Texture Texture;
public glTFTextureTypes TextureType;

public TextureExportItem(Texture texture, glTFTextureTypes textureType)
{
Texture = texture;
TextureType = textureType;
}
}

public static IEnumerable<TextureExportItem> GetTextures(Material m)
public virtual IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m)
{
var props = ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
if (props == null)
{
yield return new TextureExportItem(m.mainTexture, glTFTextureTypes.BaseColor);
yield return (m.mainTexture, glTFTextureTypes.BaseColor);
}

foreach (var prop in props.Properties)
{

if (prop.ShaderPropertyType == ShaderPropExporter.ShaderPropertyType.TexEnv)
{
yield return new TextureExportItem(m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key));
yield return (m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key));
}
}
}


struct BytesWithMime
{
public Byte[] Bytes;
public string Mime;
}

static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
{
#if UNITY_EDITOR
var path = UnityPath.FromAsset(texture);
Expand All @@ -138,46 +120,46 @@ static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureT
// Resized exporting if MaxSize setting value is smaller than original image size.
if (originalSize > requiredMaxSize)
{
return new BytesWithMime
{
Bytes = TextureItem.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(),
Mime = "image/png",
};
return
(
TextureItem.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(),
"image/png"
);
}
}

if (path.Extension == ".png")
{
return new BytesWithMime
{
Bytes = System.IO.File.ReadAllBytes(path.FullPath),
Mime = "image/png",
};
return
(
System.IO.File.ReadAllBytes(path.FullPath),
"image/png"
);
}
if (path.Extension == ".jpg")
{
return new BytesWithMime
{
Bytes = System.IO.File.ReadAllBytes(path.FullPath),
Mime = "image/jpeg",
};
return
(
System.IO.File.ReadAllBytes(path.FullPath),
"image/jpeg"
);
}
}
#endif

return new BytesWithMime
{
Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
Mime = "image/png",
};
return
(
TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
"image/png"
);
}

public static int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType)
public virtual int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType)
{
var bytesWithMime = GetBytesWithMime(texture, textureType); ;

// add view
var view = gltf.buffers[bufferIndex].Append(bytesWithMime.Bytes, glBufferTarget.NONE);
var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE);
var viewIndex = gltf.AddBufferView(view);

// add image
Expand All @@ -186,7 +168,7 @@ public static int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glT
{
name = texture.name,
bufferView = viewIndex,
mimeType = bytesWithMime.Mime,
mimeType = bytesWithMime.mine,
});

// add sampler
Expand Down
28 changes: 25 additions & 3 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ protected virtual IMaterialExporter CreateMaterialExporter()
return new MaterialExporter();
}

private ITextureExporter _textureExporter;
public ITextureExporter TextureExporter
{
get
{
if (_textureExporter != null)
{
return _textureExporter;
}
else
{
_textureExporter = new TextureIO();
return _textureExporter;
}
}
set
{
_textureExporter = value;
}
}


/// <summary>
/// このエクスポーターがサポートするExtension
/// </summary>
Expand Down Expand Up @@ -237,17 +259,17 @@ public virtual void Export(MeshExportSettings meshExportSettings)

#region Materials and Textures
Materials = Nodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList();
var unityTextures = Materials.SelectMany(x => TextureIO.GetTextures(x)).Where(x => x.Texture != null).Distinct().ToList();
var unityTextures = Materials.SelectMany(x => TextureExporter.GetTextures(x)).Where(x => x.texture != null).Distinct().ToList();

TextureManager = new TextureExportManager(unityTextures.Select(x => x.Texture));
TextureManager = new TextureExportManager(unityTextures.Select(x => x.texture));

var materialExporter = CreateMaterialExporter();
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList();

for (int i = 0; i < unityTextures.Count; ++i)
{
var unityTexture = unityTextures[i];
TextureIO.ExportTexture(glTF, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.TextureType);
TextureExporter.ExportTexture(glTF, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.textureType);
}
#endregion

Expand Down
4 changes: 2 additions & 2 deletions Assets/VRM/Runtime/IO/VRMExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public override void Export(MeshExportSettings configuration)
VRM.meta.title = meta.Title;
if (meta.Thumbnail != null)
{
VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
}

VRM.meta.licenseType = meta.LicenseType;
Expand All @@ -138,7 +138,7 @@ public override void Export(MeshExportSettings configuration)
VRM.meta.title = meta.Title;
if (meta.Thumbnail != null)
{
VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
}

// ussage permission
Expand Down