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

Commit

Permalink
Obsolete gltfImporter class #34
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Sep 21, 2018
1 parent 93ffd21 commit ae46109
Show file tree
Hide file tree
Showing 9 changed files with 657 additions and 645 deletions.
2 changes: 1 addition & 1 deletion Core/Editor/UniGLTFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void UniGLTFSimpleSceneTest()
// import
context.ParseJson(gltf.ToJson(), new SimpleStorage(new ArraySegment<byte>()));
//Debug.LogFormat("{0}", context.Json);
gltfImporter.Load(context);
context.Load();

AssertAreEqual(go.transform, context.Root.transform);
}
Expand Down
6 changes: 3 additions & 3 deletions Core/Scripts/Editor/ImporterMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void ImportMenu()
if (Application.isPlaying)
{
// load into scene
var context = gltfImporter.Load(path);
var context = ImporterContext.Load(path);
context.ShowMeshes();
Selection.activeGameObject = context.Root;
}
Expand Down Expand Up @@ -51,14 +51,14 @@ public static void ImportMenu()
static void Import(string readPath, UnityPath prefabPath)
{
var bytes = File.ReadAllBytes(readPath);
var context = gltfImporter.Parse(readPath, bytes);
var context = ImporterContext.Parse(readPath, bytes);

context.SaveTexturesAsPng(prefabPath);

EditorApplication.delayCall += () =>
{
// delay and can import png texture
gltfImporter.Load(context);
context.Load();
context.SaveAsAsset(prefabPath);
context.Destroy(false);
};
Expand Down
4 changes: 2 additions & 2 deletions Core/Scripts/Editor/gltfAssetPostprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void Import(UnityPath gltfPath)
{
context.ParseJson(File.ReadAllText(gltfPath.FullPath, System.Text.Encoding.UTF8),
new FileSystemStorage(gltfPath.Parent.FullPath));
gltfImporter.Load(context);
context.Load();
context.SaveAsAsset(prefabPath);
context.Destroy(false);
}
Expand All @@ -53,7 +53,7 @@ public static void Import(UnityPath gltfPath)
EditorApplication.delayCall += () =>
{
// delay and can import png texture
gltfImporter.Load(context);
context.Load();
context.SaveAsAsset(prefabPath);
context.Destroy(false);
};
Expand Down
133 changes: 132 additions & 1 deletion Core/Scripts/IO/AnimationImporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;

Expand Down Expand Up @@ -152,5 +153,135 @@ public static void SetAnimationCurve(
targetClip.SetCurve(relativePath, typeof(Transform), propertyNames[i], curves[i]);
}
}

public static void ImportAnimation(ImporterContext ctx, AnimationClip clip)
{
for (int i = 0; i < ctx.GLTF.animations.Count; ++i)
{
var animation = ctx.GLTF.animations[i];
if (string.IsNullOrEmpty(animation.name))
{
animation.name = string.Format("animation:{0}", i);
}

foreach (var channel in animation.channels)
{
var targetTransform = ctx.Nodes[channel.target.node];
var relativePath = targetTransform.RelativePathFrom(ctx.Root.transform);
switch (channel.target.path)
{
case glTFAnimationTarget.PATH_TRANSLATION:
{
var sampler = animation.samplers[channel.sampler];
var input = ctx.GLTF.GetArrayFromAccessor<float>(sampler.input);
var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output);

AnimationImporter.SetAnimationCurve(
clip,
relativePath,
new string[] { "localPosition.x", "localPosition.y", "localPosition.z" },
input,
output,
sampler.interpolation,
(values, last) =>
{
Vector3 temp = new Vector3(values[0], values[1], values[2]);
return temp.ReverseZ().ToArray();
}
);
}
break;

case glTFAnimationTarget.PATH_ROTATION:
{
var sampler = animation.samplers[channel.sampler];
var input = ctx.GLTF.GetArrayFromAccessor<float>(sampler.input);
var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output);

AnimationImporter.SetAnimationCurve(
clip,
relativePath,
new string[] { "localRotation.x", "localRotation.y", "localRotation.z", "localRotation.w" },
input,
output,
sampler.interpolation,
(values, last) =>
{
Quaternion currentQuaternion = new Quaternion(values[0], values[1], values[2], values[3]);
Quaternion lastQuaternion = new Quaternion(last[0], last[1], last[2], last[3]);
return AnimationImporter.GetShortest(lastQuaternion, currentQuaternion.ReverseZ()).ToArray();
}
);

clip.EnsureQuaternionContinuity();
}
break;

case glTFAnimationTarget.PATH_SCALE:
{
var sampler = animation.samplers[channel.sampler];
var input = ctx.GLTF.GetArrayFromAccessor<float>(sampler.input);
var output = ctx.GLTF.GetArrayFromAccessorAsFloat(sampler.output);

AnimationImporter.SetAnimationCurve(
clip,
relativePath,
new string[] { "localScale.x", "localScale.y", "localScale.z" },
input,
output,
sampler.interpolation,
(values, last) => values);
}
break;

case glTFAnimationTarget.PATH_WEIGHT:
{
var node = ctx.GLTF.nodes[channel.target.node];
var mesh = ctx.GLTF.meshes[node.mesh];
for (int k = 0; k < mesh.weights.Length; ++k)
{
//var weight = mesh.weights[k];
var curve = new AnimationCurve();
var sampler = animation.samplers[channel.sampler];
var input = ctx.GLTF.GetArrayFromAccessor<float>(sampler.input);
var output = ctx.GLTF.GetArrayFromAccessor<float>(sampler.output);
for (int j = 0, l = k; j < input.Length; ++j, l += mesh.weights.Length)
{
curve.AddKey(input[j], output[l] * 100);
}

clip.SetCurve(relativePath, typeof(SkinnedMeshRenderer), "blendShape." + k, curve);
}
}
break;

default:
Debug.LogWarningFormat("unknown path: {0}", channel.target.path);
break;
}
}
}
}

public static void ImportAnimation(ImporterContext ctx)
{
// animation
if (ctx.GLTF.animations != null && ctx.GLTF.animations.Any())
{
ctx.Animation = new AnimationClip();
//ctx.Animation.name = ANIMATION_NAME;
ctx.Animation.name = "legacy";
ctx.Animation.ClearCurves();
ctx.Animation.legacy = true;

ImportAnimation(ctx, ctx.Animation);

ctx.Animation.wrapMode = WrapMode.Loop;
var animation = ctx.Root.AddComponent<Animation>();
animation.AddClip(ctx.Animation, ctx.Animation.name);
animation.clip = ctx.Animation;
}
}

}
}
}
Loading

0 comments on commit ae46109

Please sign in to comment.