mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-08-23 11:05:29 -05:00
Merge pull request #842 from ousttrue/fix/update_importer_interface
コード整理
This commit is contained in:
@@ -4,7 +4,8 @@
|
||||
"UniGLTF",
|
||||
"MeshUtility",
|
||||
"MeshUtility.Editor",
|
||||
"VRMShaders"
|
||||
"VRMShaders",
|
||||
"VRMShaders.Editor"
|
||||
],
|
||||
"optionalUnityReferences": [],
|
||||
"includePlatforms": [
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Reflection;
|
||||
using UniGLTF.Animation;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
@@ -35,7 +36,7 @@ namespace UniGLTF
|
||||
using (var exporter = new gltfExporter(gltf, inverseAxis))
|
||||
{
|
||||
exporter.Prepare(go);
|
||||
exporter.Export(settings);
|
||||
exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,6 @@ namespace UniGLTF
|
||||
{
|
||||
public interface IAnimationImporter
|
||||
{
|
||||
List<AnimationClip> Import(glTF gltf, GameObject root, Axises invertAxis);
|
||||
List<AnimationClip> Import(glTF gltf, GameObject root, List<Transform> nodes, List<AnimationClip> clips, Axises invertAxis);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ namespace UniGLTF
|
||||
|
||||
using (MeasureTime("AnimationImporter"))
|
||||
{
|
||||
AnimationClips.AddRange(AnimationImporter.Import(GLTF, Root, InvertAxis));
|
||||
AnimationClips.AddRange(AnimationImporter.Import(GLTF, Root, null, null, InvertAxis));
|
||||
}
|
||||
|
||||
await OnLoadHierarchy(awaitCaller, MeasureTime);
|
||||
@@ -149,12 +149,12 @@ namespace UniGLTF
|
||||
nodes.Add(NodeImporter.BuildHierarchy(GLTF, i, Nodes, Meshes));
|
||||
}
|
||||
|
||||
NodeImporter.FixCoordinate(this, nodes, inverter);
|
||||
NodeImporter.FixCoordinate(GLTF, nodes, inverter);
|
||||
|
||||
// skinning
|
||||
for (int i = 0; i < nodes.Count; ++i)
|
||||
{
|
||||
NodeImporter.SetupSkinning(this, nodes, i, inverter);
|
||||
NodeImporter.SetupSkinning(GLTF, nodes, i, inverter);
|
||||
}
|
||||
|
||||
if (Root == null)
|
||||
@@ -202,7 +202,7 @@ namespace UniGLTF
|
||||
{
|
||||
using (MeasureTime("BuildMesh"))
|
||||
{
|
||||
var meshWithMaterials = await MeshImporter.BuildMeshAsync(awaitCaller, MaterialFactory, x);
|
||||
var meshWithMaterials = await MeshImporter.BuildMeshAsync(awaitCaller, MaterialFactory.GetMaterial, x);
|
||||
var mesh = meshWithMaterials.Mesh;
|
||||
|
||||
// mesh name
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using UniGLTF.UniUnlit;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
|
||||
namespace UniGLTF
|
||||
|
||||
@@ -225,7 +225,7 @@ namespace UniGLTF
|
||||
for (int j = 0; j < jointsLength; ++j)
|
||||
{
|
||||
var bw = new BoneWeight();
|
||||
|
||||
|
||||
var joints = joints0(j);
|
||||
var weights = weights0(j);
|
||||
|
||||
@@ -240,7 +240,7 @@ namespace UniGLTF
|
||||
|
||||
bw.boneIndex3 = joints.w;
|
||||
bw.weight3 = weights.w;
|
||||
|
||||
|
||||
bw = NormalizeBoneWeight(bw);
|
||||
|
||||
m_boneWeights.Add(bw);
|
||||
@@ -389,7 +389,7 @@ namespace UniGLTF
|
||||
for (int j = 0; j < jointsLength; ++j)
|
||||
{
|
||||
var bw = new BoneWeight();
|
||||
|
||||
|
||||
var joints = joints0(j);
|
||||
var weights = weights0(j);
|
||||
|
||||
@@ -406,7 +406,7 @@ namespace UniGLTF
|
||||
bw.weight3 = weights.w;
|
||||
|
||||
bw = NormalizeBoneWeight(bw);
|
||||
|
||||
|
||||
m_boneWeights.Add(bw);
|
||||
}
|
||||
}
|
||||
@@ -648,7 +648,7 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<MeshWithMaterials> BuildMeshAsync(IAwaitCaller awaitCaller, MaterialFactory ctx, MeshImporter.MeshContext meshContext)
|
||||
public static async Task<MeshWithMaterials> BuildMeshAsync(IAwaitCaller awaitCaller, Func<int, Material> ctx, MeshImporter.MeshContext meshContext)
|
||||
{
|
||||
var (mesh, recalculateTangents) = _BuildMesh(meshContext);
|
||||
|
||||
@@ -663,7 +663,7 @@ namespace UniGLTF
|
||||
var result = new MeshWithMaterials
|
||||
{
|
||||
Mesh = mesh,
|
||||
Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray()
|
||||
Materials = meshContext.MaterialIndices.Select(ctx).ToArray()
|
||||
};
|
||||
|
||||
await awaitCaller.NextFrame();
|
||||
|
||||
@@ -131,9 +131,9 @@ namespace UniGLTF
|
||||
//
|
||||
// fix node's coordinate. z-back to z-forward
|
||||
//
|
||||
public static void FixCoordinate(ImporterContext context, List<TransformWithSkin> nodes, IAxisInverter inverter)
|
||||
public static void FixCoordinate(glTF gltf, List<TransformWithSkin> nodes, IAxisInverter inverter)
|
||||
{
|
||||
if (context.GLTF.rootnodes == null)
|
||||
if (gltf.rootnodes == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -142,7 +142,7 @@ namespace UniGLTF
|
||||
Position = x.Transform.position,
|
||||
Rotation = x.Transform.rotation,
|
||||
});
|
||||
foreach (var x in context.GLTF.rootnodes)
|
||||
foreach (var x in gltf.rootnodes)
|
||||
{
|
||||
// fix nodes coordinate
|
||||
// reverse Z in global
|
||||
@@ -158,7 +158,7 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetupSkinning(ImporterContext context, List<TransformWithSkin> nodes, int i, IAxisInverter inverter)
|
||||
public static void SetupSkinning(glTF gltf, List<TransformWithSkin> nodes, int i, IAxisInverter inverter)
|
||||
{
|
||||
var x = nodes[i];
|
||||
var skinnedMeshRenderer = x.Transform.GetComponent<SkinnedMeshRenderer>();
|
||||
@@ -170,12 +170,12 @@ namespace UniGLTF
|
||||
if (mesh == null) throw new Exception();
|
||||
if (skinnedMeshRenderer == null) throw new Exception();
|
||||
|
||||
if (x.SkinIndex.Value < context.GLTF.skins.Count)
|
||||
if (x.SkinIndex.Value < gltf.skins.Count)
|
||||
{
|
||||
// calculate internal values(boundingBox etc...) when sharedMesh assigned ?
|
||||
skinnedMeshRenderer.sharedMesh = null;
|
||||
|
||||
var skin = context.GLTF.skins[x.SkinIndex.Value];
|
||||
var skin = gltf.skins[x.SkinIndex.Value];
|
||||
var joints = skin.joints.Select(y => nodes[y].Transform).ToArray();
|
||||
if (joints.Any())
|
||||
{
|
||||
@@ -184,7 +184,7 @@ namespace UniGLTF
|
||||
|
||||
if (skin.inverseBindMatrices != -1)
|
||||
{
|
||||
var bindPoses = context.GLTF.GetArrayFromAccessor<Matrix4x4>(skin.inverseBindMatrices)
|
||||
var bindPoses = gltf.GetArrayFromAccessor<Matrix4x4>(skin.inverseBindMatrices)
|
||||
.Select(inverter.InvertMat4)
|
||||
.ToArray()
|
||||
;
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace UniGLTF
|
||||
{
|
||||
public sealed class RootAnimationImporter : IAnimationImporter
|
||||
{
|
||||
public List<AnimationClip> Import(glTF gltf, GameObject root, Axises invertAxis)
|
||||
public List<AnimationClip> Import(glTF gltf, GameObject root, List<Transform> _nodes, List<AnimationClip> _clips, Axises invertAxis)
|
||||
{
|
||||
var animationClips = new List<AnimationClip>();
|
||||
if (gltf.animations != null && gltf.animations.Any())
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
using VRMShaders;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
@@ -173,7 +173,7 @@ namespace UniGLTF
|
||||
|
||||
}
|
||||
|
||||
public virtual void Export(MeshExportSettings meshExportSettings)
|
||||
public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture, bool> useAsset)
|
||||
{
|
||||
var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
|
||||
var bufferIndex = glTF.AddBuffer(bytesBuffer);
|
||||
@@ -185,7 +185,7 @@ namespace UniGLTF
|
||||
#region Materials and Textures
|
||||
Materials = Nodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList();
|
||||
|
||||
TextureManager = new TextureExporter();
|
||||
TextureManager = new TextureExporter(useAsset);
|
||||
|
||||
var materialExporter = CreateMaterialExporter();
|
||||
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList();
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
"references": [
|
||||
"UniGLTF",
|
||||
"UniGLTF.Editor",
|
||||
"VRMShaders"
|
||||
"VRMShaders",
|
||||
"VRMShaders.Editor"
|
||||
],
|
||||
"optionalUnityReferences": [
|
||||
"TestAssemblies"
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace UniGLTF
|
||||
filterMode = FilterMode.Bilinear,
|
||||
};
|
||||
|
||||
var textureManager = new TextureExporter();
|
||||
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
var srcMaterial = new Material(Shader.Find("Standard"));
|
||||
|
||||
var offset = new Vector2(0.3f, 0.2f);
|
||||
@@ -242,7 +242,7 @@ namespace UniGLTF
|
||||
material.SetColor("_EmissionColor", new Color(0, 1, 2, 1));
|
||||
material.EnableKeyword("_EMISSION");
|
||||
var materialExporter = new MaterialExporter();
|
||||
var textureExportManager = new TextureExporter();
|
||||
var textureExportManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
var gltfMaterial = materialExporter.ExportMaterial(material, textureExportManager);
|
||||
|
||||
Assert.AreEqual(gltfMaterial.emissiveFactor, new float[] { 0, 0.5f, 1 });
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace UniGLTF
|
||||
wrapMode = TextureWrapMode.Clamp,
|
||||
filterMode = FilterMode.Trilinear,
|
||||
};
|
||||
var textureManager = new TextureExporter();
|
||||
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
|
||||
var material = new Material(Shader.Find("Standard"));
|
||||
material.mainTexture = tex0;
|
||||
@@ -50,28 +50,5 @@ namespace UniGLTF
|
||||
var (bytes, mime) = GltfTextureExporter.GetBytesWithMime(readonlyTexture);
|
||||
Assert.NotNull(bytes);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExportMetallicSmoothnessOcclusion_Test()
|
||||
{
|
||||
var metallic = new Texture2D(4, 4, TextureFormat.ARGB32, false, true);
|
||||
var occlusion = new Texture2D(4, 4, TextureFormat.ARGB32, false, true);
|
||||
|
||||
{
|
||||
var exporter = new TextureExporter();
|
||||
Assert.AreEqual(-1, exporter.ExportMetallicSmoothnessOcclusion(null, 0, null));
|
||||
}
|
||||
{
|
||||
var exporter = new TextureExporter();
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion));
|
||||
Assert.AreEqual(1, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null));
|
||||
}
|
||||
{
|
||||
var exporter = new TextureExporter();
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, occlusion));
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion));
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UniJSON;
|
||||
using UnityEngine;
|
||||
|
||||
using VRMShaders;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
@@ -106,7 +106,7 @@ namespace UniGLTF
|
||||
using (var exporter = new gltfExporter(gltf))
|
||||
{
|
||||
exporter.Prepare(go);
|
||||
exporter.Export(MeshExportSettings.Default);
|
||||
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset );
|
||||
|
||||
// remove empty buffer
|
||||
gltf.buffers.Clear();
|
||||
@@ -298,7 +298,7 @@ namespace UniGLTF
|
||||
using (var exporter = new gltfExporter(gltf))
|
||||
{
|
||||
exporter.Prepare(CreateSimpleScene());
|
||||
exporter.Export(MeshExportSettings.Default);
|
||||
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset );
|
||||
}
|
||||
|
||||
var expected = gltf.ToJson().ParseAsJson();
|
||||
@@ -534,7 +534,7 @@ namespace UniGLTF
|
||||
using (var exporter = new gltfExporter(gltf))
|
||||
{
|
||||
exporter.Prepare(go);
|
||||
exporter.Export(UniGLTF.MeshExportSettings.Default);
|
||||
exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset );
|
||||
|
||||
json = gltf.ToJson();
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
"MeshUtility",
|
||||
"UniGLTF",
|
||||
"UniVRM.Editor",
|
||||
"VRM.Tests"
|
||||
"VRM.Tests",
|
||||
"VRMShaders",
|
||||
"VRMShaders.Editor"
|
||||
],
|
||||
"optionalUnityReferences": [
|
||||
"TestAssemblies"
|
||||
|
||||
@@ -5,6 +5,7 @@ using UniJSON;
|
||||
using UnityEngine;
|
||||
using MeshUtility;
|
||||
using System;
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRM.Samples
|
||||
{
|
||||
@@ -169,7 +170,7 @@ namespace VRM.Samples
|
||||
*/
|
||||
importedJson.RemoveValue(Utf8String.From("/bufferViews/*/byteStride"));
|
||||
|
||||
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root);
|
||||
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.IsTextureEditorAsset );
|
||||
|
||||
// TODO: Check contents in JSON
|
||||
/*var exportJson = */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using NUnit.Framework;
|
||||
using UniGLTF;
|
||||
using UnityEngine;
|
||||
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRM.Samples
|
||||
{
|
||||
@@ -10,7 +11,7 @@ namespace VRM.Samples
|
||||
{
|
||||
var material = Resources.Load<Material>(resourceName);
|
||||
var exporter = new VRMMaterialExporter();
|
||||
var textureManager = new UniGLTF.TextureExporter();
|
||||
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
var exported = exporter.ExportMaterial(material, textureManager);
|
||||
|
||||
// parse glTFExtensionExport to glTFExtensionImport
|
||||
|
||||
@@ -95,7 +95,7 @@ namespace VRM.Samples
|
||||
return;
|
||||
}
|
||||
|
||||
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model);
|
||||
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, _ => false);
|
||||
var bytes = vrm.ToGlbBytes();
|
||||
File.WriteAllBytes(path, bytes);
|
||||
Debug.LogFormat("export to {0}", path);
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
"references": [
|
||||
"VRM",
|
||||
"UniHumanoid",
|
||||
"UniGLTF"
|
||||
"UniGLTF",
|
||||
"VRMShaders"
|
||||
],
|
||||
"optionalUnityReferences": [],
|
||||
"includePlatforms": [],
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Text;
|
||||
using UniGLTF;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRM
|
||||
{
|
||||
@@ -224,7 +225,7 @@ namespace VRM
|
||||
using (var exporter = new VRMExporter(gltf))
|
||||
{
|
||||
exporter.Prepare(target);
|
||||
exporter.Export(settings.MeshExportSettings);
|
||||
exporter.Export(settings.MeshExportSettings, AssetTextureUtil.IsTextureEditorAsset );
|
||||
}
|
||||
var bytes = gltf.ToGlbBytes();
|
||||
File.WriteAllBytes(path, bytes);
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
"UniUnlit",
|
||||
"UniGLTF",
|
||||
"UniGLTF.Editor",
|
||||
"VRMShaders"
|
||||
"VRMShaders",
|
||||
"VRMShaders.Editor"
|
||||
],
|
||||
"optionalUnityReferences": [],
|
||||
"includePlatforms": [
|
||||
|
||||
@@ -14,13 +14,13 @@ namespace VRM
|
||||
return new VRMMaterialExporter();
|
||||
}
|
||||
|
||||
public static glTF Export(MeshExportSettings configuration, GameObject go)
|
||||
public static glTF Export(MeshExportSettings configuration, GameObject go, Func<Texture, bool> useAsset)
|
||||
{
|
||||
var gltf = new glTF();
|
||||
using (var exporter = new VRMExporter(gltf))
|
||||
{
|
||||
exporter.Prepare(go);
|
||||
exporter.Export(configuration);
|
||||
exporter.Export(configuration, useAsset);
|
||||
}
|
||||
return gltf;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Linq;
|
||||
using UniGLTF;
|
||||
using UniGLTF.ShaderPropExporter;
|
||||
using UnityEngine;
|
||||
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRM
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using NUnit.Framework;
|
||||
using UniGLTF;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRM
|
||||
{
|
||||
@@ -15,7 +16,7 @@ namespace VRM
|
||||
filterMode = FilterMode.Bilinear,
|
||||
};
|
||||
|
||||
var textureManager = new TextureExporter();
|
||||
var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
var srcMaterial = new Material(Shader.Find("VRM/MToon"));
|
||||
|
||||
var offset = new Vector2(0.3f, 0.2f);
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
"UniGLTF",
|
||||
"MeshUtility",
|
||||
"MeshUtility.Editor",
|
||||
"VRMShaders"
|
||||
"VRMShaders",
|
||||
"VRMShaders.Editor"
|
||||
],
|
||||
"optionalUnityReferences": [
|
||||
"TestAssemblies"
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using NUnit.Framework;
|
||||
using UniGLTF;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRM
|
||||
{
|
||||
@@ -105,7 +106,7 @@ namespace VRM
|
||||
try
|
||||
{
|
||||
// export
|
||||
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go);
|
||||
var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, AssetTextureUtil.IsTextureEditorAsset );
|
||||
|
||||
// re import
|
||||
if (vrm != null)
|
||||
|
||||
@@ -124,7 +124,10 @@ namespace UniVRM10
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException($"{kv.Key}: {kv.Value}");
|
||||
#if VRM_DEVELOP
|
||||
Debug.LogWarning($"vectorProperties: {kv.Key}: {kv.Value}");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,7 +228,10 @@ namespace UniVRM10
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException($"floatProperties: {kv.Key} is unknown");
|
||||
#if VRM_DEVELOP
|
||||
Debug.LogWarning($"floatProperties: {kv.Key} is unknown");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +253,10 @@ namespace UniVRM10
|
||||
case "_OutlineWidthTexture": map.OutlineWidthTexture = index; break;
|
||||
case "_UvAnimMaskTexture": map.UvAnimMaskTexture = index; break;
|
||||
default:
|
||||
throw new NotImplementedException($"textureProperties: {kv.Key} is unknown");
|
||||
#if VRM_DEVELOP
|
||||
Debug.LogWarning($"textureProperties: {kv.Key} is unknown");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,7 +322,10 @@ namespace UniVRM10
|
||||
{
|
||||
index = mtoon.TextureIndexMap.MainTex.Value
|
||||
};
|
||||
var value = mtoon.OffsetScale["_MainTex"];
|
||||
if (!mtoon.OffsetScale.TryGetValue("_MainTex", out float[] value))
|
||||
{
|
||||
value = new float[] { 0, 0, 1, 1 };
|
||||
}
|
||||
glTF_KHR_texture_transform.Serialize(
|
||||
gltfMaterial.pbrMetallicRoughness.baseColorTexture,
|
||||
(value[0], value[1]),
|
||||
|
||||
8
Assets/VRMShaders/Editor.meta
Normal file
8
Assets/VRMShaders/Editor.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58204522f08a6fe4f846a5817a37f902
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
71
Assets/VRMShaders/Editor/AssetTextureUtil.cs
Normal file
71
Assets/VRMShaders/Editor/AssetTextureUtil.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VRMShaders
|
||||
{
|
||||
public static class AssetTextureUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// TextureImporter.maxTextureSize が オリジナルの画像Sizeより小さいか
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsMaxTextureSizeSmallerThanOriginalTextureSize(Texture2D src)
|
||||
{
|
||||
var path = AssetDatabase.GetAssetPath(src);
|
||||
var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
|
||||
|
||||
// private メソッド TextureImporter.GetWidthAndHeight を無理やり呼ぶ
|
||||
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)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Export するときに オリジナルのテクスチャーアセット(png/jpg)を使用するか否か。
|
||||
/// 条件は、
|
||||
///
|
||||
/// * TextureAsset が存在する
|
||||
/// * TextureImporter の maxSize
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="texture2D"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsTextureEditorAsset(Texture texture)
|
||||
{
|
||||
if (texture is Texture2D texture2D && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture2D)))
|
||||
{
|
||||
// exists Texture2D asset
|
||||
if (IsMaxTextureSizeSmallerThanOriginalTextureSize(texture2D))
|
||||
{
|
||||
// Texture Inspector の MaxSize 設定で、テクスチャをオリジナルサイズよりも小さいサイズで Texture 化する指示を行っているため
|
||||
// glTF Exporter もそれにしたがって、解釈をする
|
||||
//
|
||||
// 4096x4096 のような巨大なテクスチャーがそのまま出力されることを、Unityの TextureImporter.maxSize により防止する
|
||||
//
|
||||
return false;
|
||||
}
|
||||
|
||||
// use Texture2D asset. EncodeToPng
|
||||
return true;
|
||||
}
|
||||
|
||||
// not Texture2D or not exists Texture2D asset. EncodeToPng
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/VRMShaders/Editor/AssetTextureUtil.cs.meta
Normal file
11
Assets/VRMShaders/Editor/AssetTextureUtil.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6efbb66c1ac168848beead1fe3b16263
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
3
Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef
Normal file
3
Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "VRMShaders.Editor"
|
||||
}
|
||||
7
Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef.meta
Normal file
7
Assets/VRMShaders/Editor/VRMShaders.Editor.asmdef.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7da8a75dcade2144aab699032d7d7987
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,13 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Reflection;
|
||||
using VRMShaders;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace UniGLTF
|
||||
|
||||
namespace VRMShaders
|
||||
{
|
||||
/// <summary>
|
||||
/// glTF にエクスポートする Texture2D を蓄えて index を確定させる。
|
||||
@@ -15,12 +11,29 @@ namespace UniGLTF
|
||||
/// </summary>
|
||||
public class TextureExporter
|
||||
{
|
||||
Func<Texture, bool> m_useAsset;
|
||||
|
||||
public TextureExporter(Func<Texture, bool> useAsset)
|
||||
{
|
||||
m_useAsset = useAsset;
|
||||
}
|
||||
|
||||
public enum ConvertTypes
|
||||
{
|
||||
// 無変換
|
||||
None,
|
||||
// Unity Standard様式 から glTF PBR様式への変換
|
||||
OcclusionMetallicRoughness,
|
||||
// Assetを使うときはそのバイト列を無変換で、それ以外は DXT5nm 形式からのデコードを行う
|
||||
Normal,
|
||||
}
|
||||
|
||||
struct ExportKey
|
||||
{
|
||||
public readonly Texture Src;
|
||||
public readonly glTFTextureTypes TextureType;
|
||||
public readonly ConvertTypes TextureType;
|
||||
|
||||
public ExportKey(Texture src, glTFTextureTypes type)
|
||||
public ExportKey(Texture src, ConvertTypes type)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
@@ -45,7 +58,7 @@ namespace UniGLTF
|
||||
/// <param name="src"></param>
|
||||
/// <param name="textureType"></param>
|
||||
/// <returns></returns>
|
||||
public int GetTextureIndex(Texture src, glTFTextureTypes textureType)
|
||||
public int GetTextureIndex(Texture src, ConvertTypes textureType)
|
||||
{
|
||||
if (src == null)
|
||||
{
|
||||
@@ -54,53 +67,6 @@ namespace UniGLTF
|
||||
return m_exportMap[new ExportKey(src, textureType)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TextureImporter.maxTextureSize が元のテクスチャーより小さいか否かの判定
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <returns></returns>
|
||||
static bool CopyIfMaxTextureSizeIsSmaller(Texture src)
|
||||
{
|
||||
#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)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 元の Asset が存在して、 TextureImporter に設定された画像サイズが小さくない
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="texture2D"></param>
|
||||
/// <returns></returns>
|
||||
static bool UseAsset(Texture2D texture2D)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture2D)))
|
||||
{
|
||||
if (CopyIfMaxTextureSizeIsSmaller(texture2D))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// sRGBなテクスチャーを処理し、index を確定させる
|
||||
/// </summary>
|
||||
@@ -114,7 +80,7 @@ namespace UniGLTF
|
||||
}
|
||||
|
||||
// cache
|
||||
if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.SRGB), out var index))
|
||||
if (m_exportMap.TryGetValue(new ExportKey(src, ConvertTypes.None), out var index))
|
||||
{
|
||||
return index;
|
||||
}
|
||||
@@ -122,7 +88,7 @@ namespace UniGLTF
|
||||
// get Texture2D
|
||||
index = Exported.Count;
|
||||
var texture2D = src as Texture2D;
|
||||
if (UseAsset(texture2D))
|
||||
if (m_useAsset(texture2D))
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
@@ -131,7 +97,7 @@ namespace UniGLTF
|
||||
texture2D = TextureConverter.CopyTexture(src, TextureImportTypes.sRGB, null);
|
||||
}
|
||||
Exported.Add(texture2D);
|
||||
m_exportMap.Add(new ExportKey(src, glTFTextureTypes.SRGB), index);
|
||||
m_exportMap.Add(new ExportKey(src, ConvertTypes.None), index);
|
||||
|
||||
return index;
|
||||
}
|
||||
@@ -161,11 +127,11 @@ namespace UniGLTF
|
||||
}
|
||||
|
||||
// cache
|
||||
if (metallicSmoothTexture != null && m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), out var index))
|
||||
if (metallicSmoothTexture != null && m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, ConvertTypes.OcclusionMetallicRoughness), out var index))
|
||||
{
|
||||
return index;
|
||||
}
|
||||
if (occlusionTexture != null && m_exportMap.TryGetValue(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), out index))
|
||||
if (occlusionTexture != null && m_exportMap.TryGetValue(new ExportKey(occlusionTexture, ConvertTypes.OcclusionMetallicRoughness), out index))
|
||||
{
|
||||
return index;
|
||||
}
|
||||
@@ -179,11 +145,11 @@ namespace UniGLTF
|
||||
Exported.Add(texture2D);
|
||||
if (metallicSmoothTexture != null)
|
||||
{
|
||||
m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index);
|
||||
m_exportMap.Add(new ExportKey(metallicSmoothTexture, ConvertTypes.OcclusionMetallicRoughness), index);
|
||||
}
|
||||
if (occlusionTexture != null && occlusionTexture != metallicSmoothTexture)
|
||||
{
|
||||
m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index);
|
||||
m_exportMap.Add(new ExportKey(occlusionTexture, ConvertTypes.OcclusionMetallicRoughness), index);
|
||||
}
|
||||
|
||||
return index;
|
||||
@@ -202,7 +168,7 @@ namespace UniGLTF
|
||||
}
|
||||
|
||||
// cache
|
||||
if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.Normal), out var index))
|
||||
if (m_exportMap.TryGetValue(new ExportKey(src, ConvertTypes.Normal), out var index))
|
||||
{
|
||||
return index;
|
||||
}
|
||||
@@ -210,7 +176,7 @@ namespace UniGLTF
|
||||
// get Texture2D
|
||||
index = Exported.Count;
|
||||
var texture2D = src as Texture2D;
|
||||
if (UseAsset(texture2D))
|
||||
if (m_useAsset(texture2D))
|
||||
{
|
||||
// EditorAsset を使うので変換不要
|
||||
}
|
||||
@@ -221,7 +187,7 @@ namespace UniGLTF
|
||||
}
|
||||
|
||||
Exported.Add(texture2D);
|
||||
m_exportMap.Add(new ExportKey(src, glTFTextureTypes.Normal), index);
|
||||
m_exportMap.Add(new ExportKey(src, ConvertTypes.Normal), index);
|
||||
|
||||
return index;
|
||||
}
|
||||
@@ -89,5 +89,28 @@ namespace VRMShaders
|
||||
Is.EqualTo(new Color32(255, 0, 0, 255)));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExportMetallicSmoothnessOcclusion_Test()
|
||||
{
|
||||
var metallic = new Texture2D(4, 4, TextureFormat.ARGB32, false, true);
|
||||
var occlusion = new Texture2D(4, 4, TextureFormat.ARGB32, false, true);
|
||||
|
||||
{
|
||||
var exporter = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
Assert.AreEqual(-1, exporter.ExportMetallicSmoothnessOcclusion(null, 0, null));
|
||||
}
|
||||
{
|
||||
var exporter = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion));
|
||||
Assert.AreEqual(1, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null));
|
||||
}
|
||||
{
|
||||
var exporter = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset );
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, occlusion));
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(null, 0, occlusion));
|
||||
Assert.AreEqual(0, exporter.ExportMetallicSmoothnessOcclusion(metallic, 0, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"name": "VRMShaders.Tests",
|
||||
"references": [
|
||||
"VRMShaders"
|
||||
"VRMShaders",
|
||||
"VRMShaders.Editor"
|
||||
],
|
||||
"optionalUnityReferences": [
|
||||
"TestAssemblies"
|
||||
|
||||
Reference in New Issue
Block a user