Skip to content

Commit

Permalink
Merge pull request #21 from sketchfab/feature/update-exporter-206
Browse files Browse the repository at this point in the history
Feature/update exporter 206
  • Loading branch information
AurL authored May 18, 2017
2 parents 0dccd4b + 4fde65b commit 52bc939
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
41 changes: 35 additions & 6 deletions GlTF_Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@
public class GlTF_Animation : GlTF_Writer {
public List<GlTF_Channel> channels = new List<GlTF_Channel>();
public List<GlTF_AnimSampler> animSamplers = new List<GlTF_AnimSampler>();
//bool gotTranslation = false;
//bool gotRotation = false;
//bool gotScale = false;

public enum ROTATION_TYPE
{
UNKNOWN,
QUATERNION,
EULER
};

int bakingFramerate = 30; // FPS

public GlTF_Animation (string n, string target) {
public GlTF_Animation (string n) {
name = n;
}

private struct TargetCurveSet
{
public AnimationCurve[] translationCurves;
public AnimationCurve[] rotationCurves;
//Additional curve types
public AnimationCurve[] localEulerAnglesRaw;
public AnimationCurve[] m_LocalEuler;
public AnimationCurve[] scaleCurves;
public ROTATION_TYPE rotationType;
public void Init()
{
translationCurves = new AnimationCurve[3];
Expand Down Expand Up @@ -153,8 +161,9 @@ private void collectClipCurves(AnimationClip clip, ref Dictionary<string, Target
else if (binding.propertyName.Contains(".z"))
current.scaleCurves[2] = curve;
}
else if (binding.propertyName.Contains("m_LocalRotation"))
else if (binding.propertyName.ToLower().Contains("localrotation"))
{
current.rotationType = ROTATION_TYPE.QUATERNION;
if (binding.propertyName.Contains(".x"))
current.rotationCurves[0] = curve;
else if (binding.propertyName.Contains(".y"))
Expand All @@ -164,6 +173,18 @@ private void collectClipCurves(AnimationClip clip, ref Dictionary<string, Target
else if (binding.propertyName.Contains(".w"))
current.rotationCurves[3] = curve;
}
// Takes into account 'localEuler', 'localEulerAnglesBaked' and 'localEulerAnglesRaw'
else if (binding.propertyName.ToLower().Contains("localeuler"))
{
current.rotationType = ROTATION_TYPE.EULER;
if (binding.propertyName.Contains(".x"))
current.rotationCurves[0] = curve;
else if (binding.propertyName.Contains(".y"))
current.rotationCurves[1] = curve;
else if (binding.propertyName.Contains(".z"))
current.rotationCurves[2] = curve;
}
targetCurves[binding.path] = current;
}
}

Expand Down Expand Up @@ -218,7 +239,15 @@ private void bakeCurveSet(TargetCurveSet curveSet, float length, int bakingFrame
times[i] = currentTime;
positions[i] = new Vector3(curveSet.translationCurves[0].Evaluate(currentTime), curveSet.translationCurves[1].Evaluate(currentTime), curveSet.translationCurves[2].Evaluate(currentTime));
scales[i] = new Vector3(curveSet.scaleCurves[0].Evaluate(currentTime), curveSet.scaleCurves[1].Evaluate(currentTime), curveSet.scaleCurves[2].Evaluate(currentTime));
rotations[i] = new Vector4(curveSet.rotationCurves[0].Evaluate(currentTime), curveSet.rotationCurves[1].Evaluate(currentTime), curveSet.rotationCurves[2].Evaluate(currentTime), curveSet.rotationCurves[3].Evaluate(currentTime));
if(curveSet.rotationType == ROTATION_TYPE.EULER)
{
Quaternion eulerToQuat = Quaternion.Euler(curveSet.rotationCurves[0].Evaluate(currentTime), curveSet.rotationCurves[1].Evaluate(currentTime), curveSet.rotationCurves[2].Evaluate(currentTime));
rotations[i] = new Vector4(eulerToQuat.x, eulerToQuat.y, eulerToQuat.z, eulerToQuat.w);
}
else
{
rotations[i] = new Vector4(curveSet.rotationCurves[0].Evaluate(currentTime), curveSet.rotationCurves[1].Evaluate(currentTime), curveSet.rotationCurves[2].Evaluate(currentTime), curveSet.rotationCurves[3].Evaluate(currentTime));
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions GlTF_Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ public class GlTF_Writer {
public static bool exportPBRMaterials;
public static bool hasSpecularMaterials = false;
public static bool convertRightHanded = true;
public static string exporterVersion = "2.0.5";
public static Regex rgx = new Regex("[^a-zA-Z0-9 -]");
public static string exporterVersion = "2.0.6";
public static Regex rgx = new Regex("[^a-zA-Z0-9 -_.]");

static public string cleanNonAlphanumeric(string s)
{
return rgx.Replace(s, "");
}
static public string GetNameFromObject(Object o, bool useId = false)
{
var ret = rgx.Replace(o.name, "");
ret = ret.Replace(" ", "_");
ret = ret.Replace("/", "_");
ret = ret.Replace("\\", "_");

var ret = cleanNonAlphanumeric(o.name);
if (useId)
{
ret += "_" + o.GetInstanceID();
Expand Down
28 changes: 14 additions & 14 deletions SceneToGlTFWiz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void parseUnityCamera(Transform tr)
cam.type = "orthographic";
cam.zfar = tr.GetComponent<Camera>().farClipPlane;
cam.znear = tr.GetComponent<Camera>().nearClipPlane;
cam.name = tr.name;
cam.name = GlTF_Writer.cleanNonAlphanumeric(tr.name);
//cam.orthographic.xmag = tr.camera.
GlTF_Writer.cameras.Add(cam);
}
Expand All @@ -60,7 +60,7 @@ public static void parseUnityCamera(Transform tr)
cam.znear = tr.GetComponent<Camera>().nearClipPlane;
cam.aspect_ratio = tr.GetComponent<Camera>().aspect;
cam.yfov = tr.GetComponent<Camera>().fieldOfView;
cam.name = tr.name;
cam.name = GlTF_Writer.cleanNonAlphanumeric(tr.name);
GlTF_Writer.cameras.Add(cam);
}
}
Expand All @@ -82,28 +82,28 @@ public static void parseUnityLight(Transform tr)
case LightType.Point:
GlTF_PointLight pl = new GlTF_PointLight();
pl.color = new GlTF_ColorRGB(tr.GetComponent<Light>().color);
pl.name = tr.name;
pl.name = GlTF_Writer.cleanNonAlphanumeric(tr.name);
GlTF_Writer.lights.Add(pl);
break;

case LightType.Spot:
GlTF_SpotLight sl = new GlTF_SpotLight();
sl.color = new GlTF_ColorRGB(tr.GetComponent<Light>().color);
sl.name = tr.name;
sl.name = GlTF_Writer.cleanNonAlphanumeric(tr.name);
GlTF_Writer.lights.Add(sl);
break;

case LightType.Directional:
GlTF_DirectionalLight dl = new GlTF_DirectionalLight();
dl.color = new GlTF_ColorRGB(tr.GetComponent<Light>().color);
dl.name = tr.name;
dl.name = GlTF_Writer.cleanNonAlphanumeric(tr.name);
GlTF_Writer.lights.Add(dl);
break;

case LightType.Area:
GlTF_AmbientLight al = new GlTF_AmbientLight();
al.color = new GlTF_ColorRGB(tr.GetComponent<Light>().color);
al.name = tr.name;
al.name = GlTF_Writer.cleanNonAlphanumeric(tr.name);
GlTF_Writer.lights.Add(al);
break;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public IEnumerator Export(string path, Preset presetAsset, bool buildZip, bool e
// Initialize the node
GlTF_Node node = new GlTF_Node();
node.id = GlTF_Node.GetNameFromObject(tr);
node.name = tr.name;
node.name = GlTF_Writer.cleanNonAlphanumeric(tr.name);

if (tr.GetComponent<Camera>() != null)
parseUnityCamera(tr);
Expand All @@ -204,7 +204,7 @@ public IEnumerator Export(string path, Preset presetAsset, bool buildZip, bool e
if (m != null)
{
GlTF_Mesh mesh = new GlTF_Mesh();
mesh.name = GlTF_Mesh.GetNameFromObject(m) + tr.name;
mesh.name = GlTF_Writer.cleanNonAlphanumeric(GlTF_Mesh.GetNameFromObject(m) + tr.name);

GlTF_Accessor positionAccessor = new GlTF_Accessor(GlTF_Accessor.GetNameFromObject(m, "position"), GlTF_Accessor.Type.VEC3, GlTF_Accessor.ComponentType.FLOAT);
positionAccessor.bufferView = GlTF_Writer.vec3BufferView;
Expand Down Expand Up @@ -314,7 +314,7 @@ public IEnumerator Export(string path, Preset presetAsset, bool buildZip, bool e
else
{
GlTF_Material material = new GlTF_Material();
material.name = mat.name;
material.name = GlTF_Writer.cleanNonAlphanumeric(mat.name);
primitive.materialIndex = GlTF_Writer.materials.Count;
GlTF_Writer.materialNames.Add(matName);
GlTF_Writer.materials.Add (material);
Expand Down Expand Up @@ -490,7 +490,7 @@ public IEnumerator Export(string path, Preset presetAsset, bool buildZip, bool e
for (int i = 0; i < clips.Length; i++)
{
//FIXME It seems not good to generate one animation per animator.
GlTF_Animation anim = new GlTF_Animation(a.name, node.name);
GlTF_Animation anim = new GlTF_Animation(GlTF_Writer.cleanNonAlphanumeric(a.name));
anim.Populate(clips[i], tr, GlTF_Writer.bakeAnimation);
if(anim.channels.Count > 0)
GlTF_Writer.animations.Add(anim);
Expand All @@ -502,7 +502,7 @@ public IEnumerator Export(string path, Preset presetAsset, bool buildZip, bool e
{
AnimationClip clip = animation.clip;
//FIXME It seems not good to generate one animation per animator.
GlTF_Animation anim = new GlTF_Animation(animation.name, node.name);
GlTF_Animation anim = new GlTF_Animation(GlTF_Writer.cleanNonAlphanumeric(animation.name));
anim.Populate(clip, tr, GlTF_Writer.bakeAnimation);
if (anim.channels.Count > 0)
GlTF_Writer.animations.Add(anim);
Expand Down Expand Up @@ -544,10 +544,10 @@ public IEnumerator Export(string path, Preset presetAsset, bool buildZip, bool e

if (tr.GetComponent<Camera>() != null)
{
node.cameraName = tr.name;
node.cameraName = GlTF_Writer.cleanNonAlphanumeric(tr.name);
}
else if (tr.GetComponent<Light>() != null)
node.lightName = tr.name;
node.lightName = GlTF_Writer.cleanNonAlphanumeric(tr.name);

// Parse node's skin data
GlTF_Accessor invBindMatrixAccessor = null;
Expand All @@ -557,7 +557,7 @@ public IEnumerator Export(string path, Preset presetAsset, bool buildZip, bool e
node.skeletons = GlTF_Skin.findRootSkeletons(skinMesh);
GlTF_Skin skin = new GlTF_Skin();
skin.setBindShapeMatrix(tr);
skin.name = skinMesh.rootBone.name + "_skeleton_" + node.name + tr.GetInstanceID();
skin.name = GlTF_Writer.cleanNonAlphanumeric(skinMesh.rootBone.name) + "_skeleton_" + GlTF_Writer.cleanNonAlphanumeric(node.name) + tr.GetInstanceID();

// Create invBindMatrices accessor
invBindMatrixAccessor = new GlTF_Accessor(skin.name + "invBindMatrices", GlTF_Accessor.Type.MAT4, GlTF_Accessor.ComponentType.FLOAT);
Expand Down

0 comments on commit 52bc939

Please sign in to comment.