TextureLoader.cs を class 別に分解

This commit is contained in:
ousttrue
2021-02-10 21:59:52 +09:00
parent 2edee1e7b5
commit d2fd56ef51
10 changed files with 224 additions and 161 deletions

View File

@@ -76,7 +76,7 @@ namespace UniGLTF
#if UNIGLTF_USE_WEBREQUEST_TEXTURELOADER
return new UnityWebRequestTextureLoader(index);
#else
return new TextureLoader(index);
return new GltfTextureLoader(index);
#endif
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 54a405d31cce94643be8f76f805da8c8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,77 @@
using System.Collections;
using UnityEngine;
namespace UniGLTF
{
#if UNITY_EDITOR
public class AssetTextureLoader : ITextureLoader
{
public Texture2D Texture
{
private set;
get;
}
UnityPath m_assetPath;
public AssetTextureLoader(UnityPath assetPath, string _)
{
m_assetPath = assetPath;
}
public void Dispose()
{
}
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
}
public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
{
//
// texture from assets
//
m_assetPath.ImportAsset();
var importer = m_assetPath.GetImporter<UnityEditor.TextureImporter>();
if (importer == null)
{
Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath);
}
importer.maxTextureSize = 8192;
importer.sRGBTexture = !isLinear;
importer.SaveAndReimport();
Texture = m_assetPath.LoadAsset<Texture2D>();
//Texture.name = m_textureName;
if (Texture == null)
{
Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath);
}
else
{
var maxSize = Mathf.Max(Texture.width, Texture.height);
importer.maxTextureSize
= maxSize > 4096 ? 8192 :
maxSize > 2048 ? 4096 :
maxSize > 1024 ? 2048 :
maxSize > 512 ? 1024 :
512;
importer.SaveAndReimport();
}
if (sampler != null)
{
TextureSamplerUtil.SetSampler(Texture, sampler);
}
yield break;
}
}
#endif
}

View File

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

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections;
using UnityEngine;
namespace UniGLTF
{
public class GltfTextureLoader : ITextureLoader
{
int m_textureIndex;
public GltfTextureLoader(int textureIndex)
{
m_textureIndex = textureIndex;
}
public Texture2D Texture
{
private set;
get;
}
public void Dispose()
{
}
static Byte[] ToArray(ArraySegment<byte> bytes)
{
if (bytes.Array == null)
{
return new byte[] { };
}
else if (bytes.Offset == 0 && bytes.Count == bytes.Array.Length)
{
return bytes.Array;
}
else
{
Byte[] result = new byte[bytes.Count];
Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length);
return result;
}
}
Byte[] m_imageBytes;
string m_textureName;
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
m_imageBytes = ToArray(segments);
}
public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
{
//
// texture from image(png etc) bytes
//
Texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear);
Texture.name = m_textureName;
if (m_imageBytes != null)
{
Texture.LoadImage(m_imageBytes);
}
if (sampler != null)
{
TextureSamplerUtil.SetSampler(Texture, sampler);
}
yield break;
}
}
}

View File

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

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF
{
public interface ITextureLoader : IDisposable
{
Texture2D Texture { get; }
/// <summary>
/// Call from any thread
/// </summary>
/// <param name="gltf"></param>
/// <param name="storage"></param>
void ProcessOnAnyThread(glTF gltf, IStorage storage);
/// <summary>
/// Call from unity main thread
/// </summary>
/// <param name="isLinear"></param>
/// <param name="sampler"></param>
/// <returns></returns>
IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler);
}
}

View File

@@ -3,169 +3,9 @@ using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF
{
public interface ITextureLoader : IDisposable
{
Texture2D Texture { get; }
/// <summary>
/// Call from any thread
/// </summary>
/// <param name="gltf"></param>
/// <param name="storage"></param>
void ProcessOnAnyThread(glTF gltf, IStorage storage);
/// <summary>
/// Call from unity main thread
/// </summary>
/// <param name="isLinear"></param>
/// <param name="sampler"></param>
/// <returns></returns>
IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler);
}
#if UNITY_EDITOR
public class AssetTextureLoader : ITextureLoader
{
public Texture2D Texture
{
private set;
get;
}
UnityPath m_assetPath;
public AssetTextureLoader(UnityPath assetPath, string _)
{
m_assetPath = assetPath;
}
public void Dispose()
{
}
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
}
public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
{
//
// texture from assets
//
m_assetPath.ImportAsset();
var importer = m_assetPath.GetImporter<TextureImporter>();
if (importer == null)
{
Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath);
}
importer.maxTextureSize = 8192;
importer.sRGBTexture = !isLinear;
importer.SaveAndReimport();
Texture = m_assetPath.LoadAsset<Texture2D>();
//Texture.name = m_textureName;
if (Texture == null)
{
Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath);
}
else
{
var maxSize = Mathf.Max(Texture.width, Texture.height);
importer.maxTextureSize
= maxSize > 4096 ? 8192 :
maxSize > 2048 ? 4096 :
maxSize > 1024 ? 2048 :
maxSize > 512 ? 1024 :
512;
importer.SaveAndReimport();
}
if (sampler != null)
{
TextureSamplerUtil.SetSampler(Texture, sampler);
}
yield break;
}
}
#endif
public class TextureLoader : ITextureLoader
{
int m_textureIndex;
public TextureLoader(int textureIndex)
{
m_textureIndex = textureIndex;
}
public Texture2D Texture
{
private set;
get;
}
public void Dispose()
{
}
static Byte[] ToArray(ArraySegment<byte> bytes)
{
if (bytes.Array == null)
{
return new byte[] { };
}
else if (bytes.Offset == 0 && bytes.Count == bytes.Array.Length)
{
return bytes.Array;
}
else
{
Byte[] result = new byte[bytes.Count];
Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length);
return result;
}
}
Byte[] m_imageBytes;
string m_textureName;
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
m_imageBytes = ToArray(segments);
}
public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
{
//
// texture from image(png etc) bytes
//
Texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear);
Texture.name = m_textureName;
if (m_imageBytes != null)
{
Texture.LoadImage(m_imageBytes);
}
if (sampler != null)
{
TextureSamplerUtil.SetSampler(Texture, sampler);
}
yield break;
}
}
public class UnityWebRequestTextureLoader : ITextureLoader
{
public Texture2D Texture

View File

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