Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Added TextureExportManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Nov 7, 2018
1 parent b15f3df commit 9c355c9
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 44 deletions.
56 changes: 23 additions & 33 deletions Core/Scripts/IO/MaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@ public enum glTFBlendMode

public interface IMaterialExporter
{
glTFMaterial ExportMaterial(Material m, List<Texture> textures, List<Texture> exportTextures);
glTFMaterial ExportMaterial(Material m, TextureExportManager textureManager);
}

public class MaterialExporter : IMaterialExporter
{
public virtual glTFMaterial ExportMaterial(Material m, List<Texture> textures, List<Texture> exportTextures)
public virtual glTFMaterial ExportMaterial(Material m, TextureExportManager textureManager)
{
if (textures == null)
{
throw new System.ArgumentNullException();
}

var material = CreateMaterial(m);

// common params
material.name = m.name;
Export_Color(m, textures, exportTextures, material);
Export_Metallic(m, textures, exportTextures, material);
Export_Normal(m, textures, exportTextures, material);
Export_Occlusion(m, textures, exportTextures, material);
Export_Emission(m, textures, exportTextures, material);
Export_Color(m, textureManager, material);
Export_Metallic(m, textureManager, material);
Export_Normal(m, textureManager, material);
Export_Occlusion(m, textureManager, material);
Export_Emission(m, textureManager, material);

return material;
}

static void Export_Color(Material m, List<Texture> textures, List<Texture> exportTextures, glTFMaterial material)
static void Export_Color(Material m, TextureExportManager textureManager, glTFMaterial material)
{
if (m.HasProperty("_Color"))
{
Expand All @@ -50,10 +45,9 @@ static void Export_Color(Material m, List<Texture> textures, List<Texture> expor

if (m.HasProperty("_MainTex"))
{
var index = textures.IndexOf(m.GetTexture("_MainTex"));
if (index != -1 && m.mainTexture != null)
var index = textureManager.CopyAndGetIndex(m.GetTexture("_MainTex"), RenderTextureReadWrite.sRGB);
if (index != -1)
{
exportTextures[index] = TextureItem.CopyTexture(m.mainTexture, RenderTextureReadWrite.sRGB, null);
material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
{
index = index,
Expand All @@ -62,15 +56,14 @@ static void Export_Color(Material m, List<Texture> textures, List<Texture> expor
}
}

static void Export_Metallic(Material m, List<Texture> textures, List<Texture> exportTextures, glTFMaterial material)
static void Export_Metallic(Material m, TextureExportManager textureManager, glTFMaterial material)
{
int index = -1;
if (m.HasProperty("_MetallicGlossMap"))
{
index = textures.IndexOf(m.GetTexture("_MetallicGlossMap"));
if (index != -1 && m.HasProperty("_MetallicGlossMap"))
index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), new MetallicRoughnessConverter());
if (index != -1)
{
exportTextures[index] = (new MetallicRoughnessConverter()).GetExportTexture(textures[index] as Texture2D);
material.pbrMetallicRoughness.metallicRoughnessTexture = new glTFMaterialMetallicRoughnessTextureInfo()
{
index = index,
Expand All @@ -97,14 +90,13 @@ static void Export_Metallic(Material m, List<Texture> textures, List<Texture> ex
}
}

static void Export_Normal(Material m, List<Texture> textures, List<Texture> exportTextures, glTFMaterial material)
static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material)
{
if (m.HasProperty("_BumpMap"))
{
var index = textures.IndexOf(m.GetTexture("_BumpMap"));
if (index != -1 && m.HasProperty("_BumpMap"))
var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter());
if (index != -1)
{
exportTextures[index] = (new NormalConverter()).GetExportTexture(textures[index] as Texture2D);
material.normalTexture = new glTFMaterialNormalTextureInfo()
{
index = index,
Expand All @@ -118,14 +110,13 @@ static void Export_Normal(Material m, List<Texture> textures, List<Texture> expo
}
}

static void Export_Occlusion(Material m, List<Texture> textures, List<Texture> exportTextures, glTFMaterial material)
static void Export_Occlusion(Material m, TextureExportManager textureManager, glTFMaterial material)
{
if (m.HasProperty("_OcclusionMap"))
{
var index = textures.IndexOf(m.GetTexture("_OcclusionMap"));
if (index != -1 && m.HasProperty("_OcclusionMap"))
var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter());
if (index != -1)
{
exportTextures[index] = (new OcclusionConverter()).GetExportTexture(textures[index] as Texture2D);
material.occlusionTexture = new glTFMaterialOcclusionTextureInfo()
{
index = index,
Expand All @@ -139,7 +130,7 @@ static void Export_Occlusion(Material m, List<Texture> textures, List<Texture> e
}
}

static void Export_Emission(Material m, List<Texture> textures, List<Texture> exportTextures, glTFMaterial material)
static void Export_Emission(Material m, TextureExportManager textureManager, glTFMaterial material)
{
if (m.HasProperty("_EmissionColor"))
{
Expand All @@ -149,10 +140,9 @@ static void Export_Emission(Material m, List<Texture> textures, List<Texture> ex

if (m.HasProperty("_EmissionMap"))
{
var index = textures.IndexOf(m.GetTexture("_EmissionMap"));
if (index != -1 && m.HasProperty("_EmissionMap"))
var index = textureManager.CopyAndGetIndex(m.GetTexture("_EmissionMap"), RenderTextureReadWrite.sRGB);
if (index != -1)
{
exportTextures[index] = TextureItem.CopyTexture(textures[index], RenderTextureReadWrite.sRGB, null);
material.emissiveTexture = new glTFMaterialEmissiveTextureInfo()
{
index = index,
Expand Down Expand Up @@ -256,7 +246,7 @@ static glTFMaterial Export_Standard(Material m)
pbrMetallicRoughness = new glTFPbrMetallicRoughness(),
};

switch(m.GetTag("RenderType", true))
switch (m.GetTag("RenderType", true))
{
case "Transparent":
material.alphaMode = glTFBlendMode.BLEND.ToString();
Expand Down
2 changes: 1 addition & 1 deletion Core/Scripts/IO/TextureConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace UniGLTF
{
interface ITextureConverter
public interface ITextureConverter
{
Texture2D GetImportTexture(Texture2D texture);
Texture2D GetExportTexture(Texture2D texture);
Expand Down
85 changes: 85 additions & 0 deletions Core/Scripts/IO/TextureExportManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;


namespace UniGLTF
{
public class TextureExportManager
{
List<Texture> m_textures;
public List<Texture> Textures
{
get { return m_textures; }
}

List<Texture> m_exportTextures;
public Texture GetExportTexture(int index)
{
if (index < 0 || index >= m_exportTextures.Count)
{
return null;
}
if (m_exportTextures[index] != null)
{
// コピー変換済み
return m_exportTextures[index];
}

// オリジナル
return m_textures[index];
}

public TextureExportManager(IEnumerable<Texture> textures)
{
/*
if (textures == null)
{
throw new System.ArgumentNullException();
}
*/
m_textures = textures.ToList();
m_exportTextures = new List<Texture>(Enumerable.Repeat<Texture>(null, m_textures.Count));
}

public int CopyAndGetIndex(Texture texture, RenderTextureReadWrite readWrite)
{
if (texture == null)
{
return -1;
}

var index = m_textures.IndexOf(texture);
if (index == -1)
{
// ありえない?
return -1;
}

// ToDo: may already exists
m_exportTextures[index] = TextureItem.CopyTexture(texture, readWrite, null);

return index;
}

public int ConvertAndGetIndex(Texture texture, ITextureConverter converter)
{
if (texture == null)
{
return -1;
}

var index = m_textures.IndexOf(texture);
if (index == -1)
{
// ありえない?
return -1;
}

m_exportTextures[index] = converter.GetExportTexture(texture as Texture2D);

return index;
}
}
}
12 changes: 12 additions & 0 deletions Core/Scripts/IO/TextureExportManager.cs.meta

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

14 changes: 4 additions & 10 deletions Core/Scripts/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ public List<Material> Materials
private set;
}

public List<Texture> Textures
{
get;
private set;
}
public TextureExportManager TextureManager;

protected virtual IMaterialExporter CreateMaterialExporter()
{
Expand Down Expand Up @@ -445,17 +441,15 @@ public void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMo
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();


Textures = unityTextures.Select(y => y.Texture).ToList();
List<Texture> exportTextures = new List<Texture>(Textures);
TextureManager = new TextureExportManager(unityTextures.Select(x => x.Texture));

var materialExporter = CreateMaterialExporter();
gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, Textures, exportTextures)).ToList();
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, exportTextures[i], unityTexture.TextureType);
TextureIO.ExportTexture(gltf, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.TextureType);
}
#endregion

Expand Down

0 comments on commit 9c355c9

Please sign in to comment.