Skip to content

Commit

Permalink
Merge pull request #842 from ousttrue/fix/update_importer_interface
Browse files Browse the repository at this point in the history
コード整理
  • Loading branch information
ousttrue authored Apr 5, 2021
2 parents 92fd4e9 + 002e8c6 commit 293ef81
Show file tree
Hide file tree
Showing 35 changed files with 234 additions and 142 deletions.
3 changes: 2 additions & 1 deletion Assets/UniGLTF/Editor/UniGLTF.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"UniGLTF",
"MeshUtility",
"MeshUtility.Editor",
"VRMShaders"
"VRMShaders",
"VRMShaders.Editor"
],
"optionalUnityReferences": [],
"includePlatforms": [
Expand Down
3 changes: 2 additions & 1 deletion Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using UniGLTF.Animation;
using UnityEditor;
using UnityEngine;
using VRMShaders;

namespace UniGLTF
{
Expand Down Expand Up @@ -35,7 +36,7 @@ private static void Export(GameObject go, string path, MeshExportSettings settin
using (var exporter = new gltfExporter(gltf, inverseAxis))
{
exporter.Prepare(go);
exporter.Export(settings);
exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset );
}


Expand Down
2 changes: 1 addition & 1 deletion Assets/UniGLTF/Runtime/UniGLTF/IO/IAnimationImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public virtual async Task LoadAsync(IAwaitCaller awaitCaller = null, Func<string

using (MeasureTime("AnimationImporter"))
{
AnimationClips.AddRange(AnimationImporter.Import(GLTF, Root, InvertAxis));
AnimationClips.AddRange(AnimationImporter.Import(GLTF, Root, null, null, InvertAxis));
}

await OnLoadHierarchy(awaitCaller, MeasureTime);
Expand Down Expand Up @@ -149,12 +149,12 @@ protected virtual async Task LoadGeometryAsync(IAwaitCaller awaitCaller, Func<st
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)
Expand Down Expand Up @@ -202,7 +202,7 @@ async Task<MeshWithMaterials> BuildMeshAsync(IAwaitCaller awaitCaller, Func<stri
{
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;
using System.Linq;
using UniGLTF.UniUnlit;
using UnityEngine;
using VRMShaders;


namespace UniGLTF
Expand Down
12 changes: 6 additions & 6 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void ImportMeshIndependentVertexBuffer(glTF gltf, glTFMesh gltfMesh, IAxi
for (int j = 0; j < jointsLength; ++j)
{
var bw = new BoneWeight();

var joints = joints0(j);
var weights = weights0(j);

Expand All @@ -240,7 +240,7 @@ public void ImportMeshIndependentVertexBuffer(glTF gltf, glTFMesh gltfMesh, IAxi

bw.boneIndex3 = joints.w;
bw.weight3 = weights.w;

bw = NormalizeBoneWeight(bw);

m_boneWeights.Add(bw);
Expand Down Expand Up @@ -389,7 +389,7 @@ public void ImportMeshSharingVertexBuffer(glTF gltf, glTFMesh gltfMesh, IAxisInv
for (int j = 0; j < jointsLength; ++j)
{
var bw = new BoneWeight();

var joints = joints0(j);
var weights = weights0(j);

Expand All @@ -406,7 +406,7 @@ public void ImportMeshSharingVertexBuffer(glTF gltf, glTFMesh gltfMesh, IAxisInv
bw.weight3 = weights.w;

bw = NormalizeBoneWeight(bw);

m_boneWeights.Add(bw);
}
}
Expand Down Expand Up @@ -648,7 +648,7 @@ static void BuildBlendShape(Mesh mesh, MeshContext meshContext, BlendShape blend
}
}

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);

Expand All @@ -663,7 +663,7 @@ public static async Task<MeshWithMaterials> BuildMeshAsync(IAwaitCaller awaitCal
var result = new MeshWithMaterials
{
Mesh = mesh,
Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray()
Materials = meshContext.MaterialIndices.Select(ctx).ToArray()
};

await awaitCaller.NextFrame();
Expand Down
14 changes: 7 additions & 7 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public static TransformWithSkin BuildHierarchy(glTF gltf, int i, List<Transform>
//
// 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;
}
Expand All @@ -142,7 +142,7 @@ public static void FixCoordinate(ImporterContext context, List<TransformWithSkin
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
Expand All @@ -158,7 +158,7 @@ public static void FixCoordinate(ImporterContext context, List<TransformWithSkin
}
}

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>();
Expand All @@ -170,12 +170,12 @@ public static void SetupSkinning(ImporterContext context, List<TransformWithSkin
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())
{
Expand All @@ -184,7 +184,7 @@ public static void SetupSkinning(ImporterContext context, List<TransformWithSkin

if (skin.inverseBindMatrices != -1)
{
var bindPoses = context.GLTF.GetArrayFromAccessor<Matrix4x4>(skin.inverseBindMatrices)
var bindPoses = gltf.GetArrayFromAccessor<Matrix4x4>(skin.inverseBindMatrices)
.Select(inverter.InvertMat4)
.ToArray()
;
Expand Down
2 changes: 1 addition & 1 deletion Assets/UniGLTF/Runtime/UniGLTF/IO/RootAnimationImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

using VRMShaders;

namespace UniGLTF
{
Expand Down Expand Up @@ -173,7 +173,7 @@ public virtual void ExportExtensions()

}

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);
Expand All @@ -185,7 +185,7 @@ public virtual void Export(MeshExportSettings meshExportSettings)
#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();
Expand Down
3 changes: 2 additions & 1 deletion Assets/UniGLTF/Tests/UniGLTF.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"references": [
"UniGLTF",
"UniGLTF.Editor",
"VRMShaders"
"VRMShaders",
"VRMShaders.Editor"
],
"optionalUnityReferences": [
"TestAssemblies"
Expand Down
4 changes: 2 additions & 2 deletions Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TextureTransformTest()
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);
Expand Down Expand Up @@ -242,7 +242,7 @@ public void MaterialExportTest()
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 });
Expand Down
25 changes: 1 addition & 24 deletions Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void TextureExportTest()
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;
Expand Down Expand Up @@ -50,28 +50,5 @@ public void Compressed()
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));
}
}
}
}
8 changes: 4 additions & 4 deletions Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using UniJSON;
using UnityEngine;

using VRMShaders;

namespace UniGLTF
{
Expand Down Expand Up @@ -106,7 +106,7 @@ public void UniGLTFSimpleSceneTest()
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();
Expand Down Expand Up @@ -298,7 +298,7 @@ public void GlTFToJsonTest()
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(CreateSimpleScene());
exporter.Export(MeshExportSettings.Default);
exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset );
}

var expected = gltf.ToJson().ParseAsJson();
Expand Down Expand Up @@ -534,7 +534,7 @@ public void SameMeshButDifferentMaterialExport()
using (var exporter = new gltfExporter(gltf))
{
exporter.Prepare(go);
exporter.Export(UniGLTF.MeshExportSettings.Default);
exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset );

json = gltf.ToJson();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"MeshUtility",
"UniGLTF",
"UniVRM.Editor",
"VRM.Tests"
"VRM.Tests",
"VRMShaders",
"VRMShaders.Editor"
],
"optionalUnityReferences": [
"TestAssemblies"
Expand Down
3 changes: 2 additions & 1 deletion Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using UnityEngine;
using MeshUtility;
using System;
using VRMShaders;

namespace VRM.Samples
{
Expand Down Expand Up @@ -169,7 +170,7 @@ public void ImportExportTest()
*/
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 = */
Expand Down
5 changes: 3 additions & 2 deletions Assets/VRM.Samples/Editor/Tests/VRMMaterialTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using UniGLTF;
using UnityEngine;

using VRMShaders;

namespace VRM.Samples
{
Expand All @@ -10,7 +11,7 @@ static UniGLTF.glTFMaterial ExportLoaded(string resourceName)
{
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
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void OnExportClicked()
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);
Expand Down
3 changes: 2 additions & 1 deletion Assets/VRM.Samples/VRM.Samples.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"references": [
"VRM",
"UniHumanoid",
"UniGLTF"
"UniGLTF",
"VRMShaders"
],
"optionalUnityReferences": [],
"includePlatforms": [],
Expand Down
Loading

0 comments on commit 293ef81

Please sign in to comment.