mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-06-16 12:59:41 -05:00
static class TextureIO
This commit is contained in:
parent
be204ad6cb
commit
a8dcec0263
|
|
@ -1,16 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9968c71baa1b1c04c94b0d3191cf513a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -100,7 +100,11 @@ namespace UniGLTF
|
|||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
|
||||
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, x)).ToArray());
|
||||
copyTexture.Apply();
|
||||
copyTexture.name = metallicSmoothTexture.name;
|
||||
return copyTexture;
|
||||
}
|
||||
}
|
||||
else if (metallicSmoothTexture)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
using System.Reflection;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
|
|
@ -28,6 +31,34 @@ namespace UniGLTF
|
|||
Dictionary<ExportKey, int> m_exportMap = new Dictionary<ExportKey, int>();
|
||||
List<Texture2D> m_exported = new List<Texture2D>();
|
||||
|
||||
public IReadOnlyList<Texture2D> Exported => m_exported;
|
||||
|
||||
static bool CopyIfMaxTextureSizeIsSmaller(Texture src/*, glTFTextureTypes textureType, out Texture2D dst*/)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var textureImporter = AssetImporter.GetAtPath(UnityPath.FromAsset(src).Value) as TextureImporter;
|
||||
var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (textureImporter != null && getSizeMethod != null)
|
||||
{
|
||||
var args = new object[2] { 0, 0 };
|
||||
getSizeMethod.Invoke(textureImporter, args);
|
||||
var originalWidth = (int)args[0];
|
||||
var originalHeight = (int)args[1];
|
||||
var originalSize = Mathf.Max(originalWidth, originalHeight);
|
||||
if (textureImporter.maxTextureSize < originalSize)
|
||||
{
|
||||
// export resized texture.
|
||||
// this has textureImporter.maxTextureSize
|
||||
// dst = TextureConverter.CopyTexture(src, textureType, null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// dst = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Texture の export index を得る
|
||||
/// </summary>
|
||||
|
|
@ -59,7 +90,7 @@ namespace UniGLTF
|
|||
|
||||
// get Texture2D
|
||||
index = m_exported.Count;
|
||||
if (src is Texture2D texture2D)
|
||||
if (src is Texture2D texture2D && !CopyIfMaxTextureSizeIsSmaller(src))
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
|
@ -105,7 +136,10 @@ namespace UniGLTF
|
|||
|
||||
m_exported.Add(texture2D);
|
||||
m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index);
|
||||
m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index);
|
||||
if (occlusionTexture != metallicSmoothTexture && occlusionTexture != null)
|
||||
{
|
||||
m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index);
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
|
@ -117,6 +151,10 @@ namespace UniGLTF
|
|||
texture2D = src as Texture2D;
|
||||
if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(src)))
|
||||
{
|
||||
if (CopyIfMaxTextureSizeIsSmaller(src))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,17 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
#if UNITY_EDITOR
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
|
||||
public class TextureIO : ITextureExporter
|
||||
public class TextureIO
|
||||
{
|
||||
public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType)
|
||||
{
|
||||
|
|
@ -39,24 +34,6 @@ namespace UniGLTF
|
|||
}
|
||||
}
|
||||
|
||||
public static glTFTextureTypes GetglTFTextureType(string shaderName, string propName)
|
||||
{
|
||||
switch (propName)
|
||||
{
|
||||
case "_MetallicGlossMap":
|
||||
case "_OcclusionMap":
|
||||
return glTFTextureTypes.OcclusionMetallicRoughness;
|
||||
case "_BumpMap":
|
||||
return glTFTextureTypes.Normal;
|
||||
case "_Color":
|
||||
case "_EmissionMap":
|
||||
return glTFTextureTypes.SRGB;
|
||||
default:
|
||||
// Debug.LogWarning($"unknown texture property: {propName} as sRGB");
|
||||
return glTFTextureTypes.SRGB;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetglTFTextureType(glTF glTf, int textureIndex, out glTFTextureTypes textureType)
|
||||
{
|
||||
foreach (var material in glTf.materials)
|
||||
|
|
@ -74,74 +51,12 @@ namespace UniGLTF
|
|||
return false;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static void MarkTextureAssetAsNormalMap(string assetPath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(assetPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
|
||||
if (null == textureImporter)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//Debug.LogFormat("[MarkTextureAssetAsNormalMap] {0}", assetPath);
|
||||
textureImporter.textureType = TextureImporterType.NormalMap;
|
||||
textureImporter.SaveAndReimport();
|
||||
}
|
||||
#endif
|
||||
|
||||
public virtual IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m)
|
||||
{
|
||||
var props = ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
|
||||
if (props == null)
|
||||
{
|
||||
// unknown shader
|
||||
yield return (m.mainTexture, glTFTextureTypes.SRGB);
|
||||
}
|
||||
|
||||
foreach (var prop in props.Properties)
|
||||
{
|
||||
|
||||
if (prop.ShaderPropertyType == ShaderPropExporter.ShaderPropertyType.TexEnv)
|
||||
{
|
||||
yield return (m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
|
||||
static (Byte[] bytes, string mine) GetBytesWithMime(Texture2D texture)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var path = UnityPath.FromAsset(texture);
|
||||
if (path.IsUnderAssetsFolder)
|
||||
{
|
||||
var textureImporter = AssetImporter.GetAtPath(path.Value) as TextureImporter;
|
||||
var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (textureImporter != null && getSizeMethod != null)
|
||||
{
|
||||
var args = new object[2] { 0, 0 };
|
||||
getSizeMethod.Invoke(textureImporter, args);
|
||||
var originalWidth = (int)args[0];
|
||||
var originalHeight = (int)args[1];
|
||||
|
||||
var originalSize = Mathf.Max(originalWidth, originalHeight);
|
||||
var requiredMaxSize = textureImporter.maxTextureSize;
|
||||
|
||||
// Resized exporting if MaxSize setting value is smaller than original image size.
|
||||
if (originalSize > requiredMaxSize)
|
||||
{
|
||||
return
|
||||
(
|
||||
TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(),
|
||||
"image/png"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (path.Extension == ".png")
|
||||
{
|
||||
return
|
||||
|
|
@ -163,14 +78,14 @@ namespace UniGLTF
|
|||
|
||||
return
|
||||
(
|
||||
TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(),
|
||||
texture.EncodeToPNG(),
|
||||
"image/png"
|
||||
);
|
||||
}
|
||||
|
||||
public int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType)
|
||||
static public int ExportTexture(glTF gltf, int bufferIndex, Texture2D texture)
|
||||
{
|
||||
var bytesWithMime = GetBytesWithMime(texture, textureType); ;
|
||||
var bytesWithMime = GetBytesWithMime(texture);
|
||||
|
||||
// add view
|
||||
var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE);
|
||||
|
|
|
|||
|
|
@ -54,28 +54,6 @@ namespace UniGLTF
|
|||
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>
|
||||
|
|
@ -206,6 +184,12 @@ namespace UniGLTF
|
|||
|
||||
var materialExporter = CreateMaterialExporter();
|
||||
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList();
|
||||
|
||||
for (int i = 0; i < TextureManager.Exported.Count; ++i)
|
||||
{
|
||||
var unityTexture = TextureManager.Exported[i];
|
||||
TextureIO.ExportTexture(glTF, bufferIndex, unityTexture);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Meshes
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UniGLTF;
|
||||
using UniJSON;
|
||||
|
|
@ -113,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, glTFTextureTypes.SRGB);
|
||||
VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail);
|
||||
}
|
||||
|
||||
VRM.meta.licenseType = meta.LicenseType;
|
||||
|
|
@ -138,7 +137,7 @@ namespace VRM
|
|||
VRM.meta.title = meta.Title;
|
||||
if (meta.Thumbnail != null)
|
||||
{
|
||||
VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.SRGB);
|
||||
VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail);
|
||||
}
|
||||
|
||||
// ussage permission
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user