private TransformAnimationCurves CreateTransformAnimationCurves(AnimationClip anim, EditorCurveBinding[] bindings) { var curves = new TransformAnimationCurves(); var positionX = bindings.Where(b => b.propertyName == "m_LocalPosition.x").FirstOrDefault(); var positionY = bindings.Where(b => b.propertyName == "m_LocalPosition.y").FirstOrDefault(); var positionZ = bindings.Where(b => b.propertyName == "m_LocalPosition.z").FirstOrDefault(); var rotationX = bindings.Where(b => b.propertyName == "m_LocalRotation.x").FirstOrDefault(); var rotationY = bindings.Where(b => b.propertyName == "m_LocalRotation.y").FirstOrDefault(); var rotationZ = bindings.Where(b => b.propertyName == "m_LocalRotation.z").FirstOrDefault(); var rotationW = bindings.Where(b => b.propertyName == "m_LocalRotation.w").FirstOrDefault(); var scaleX = bindings.Where(b => b.propertyName == "m_LocalScale.x").FirstOrDefault(); var scaleY = bindings.Where(b => b.propertyName == "m_LocalScale.y").FirstOrDefault(); var scaleZ = bindings.Where(b => b.propertyName == "m_LocalScale.z").FirstOrDefault(); curves.positionX = AnimationUtility.GetEditorCurve(anim, positionX); curves.positionY = AnimationUtility.GetEditorCurve(anim, positionY); curves.positionZ = AnimationUtility.GetEditorCurve(anim, positionZ); curves.rotationX = AnimationUtility.GetEditorCurve(anim, rotationX); curves.rotationY = AnimationUtility.GetEditorCurve(anim, rotationY); curves.rotationZ = AnimationUtility.GetEditorCurve(anim, rotationZ); curves.rotationW = AnimationUtility.GetEditorCurve(anim, rotationW); curves.scaleX = AnimationUtility.GetEditorCurve(anim, scaleX); curves.scaleY = AnimationUtility.GetEditorCurve(anim, scaleY); curves.scaleZ = AnimationUtility.GetEditorCurve(anim, scaleZ); return(curves); }
private XmlNode AddAnimationCurve(TransformAnimationCurves curves, string curveId, string jointId) { XmlNode curveNode = this.animations.AppendChild(this.xml.CreateElement("animation", COLLADA)); SetIdAttribute(curveNode, curveId); int keyCount = curves.positionX.length; // input string inputID = string.Format("{0}-input", curveId); { XmlNode inputNode = curveNode.AppendChild(this.xml.CreateElement("source", COLLADA)); SetIdAttribute(inputNode, inputID); StringBuilder sb = new StringBuilder(); foreach (var k in curves.positionX.keys) { sb.AppendFormat("{0:F6} ", k.time); } string arrayId = string.Format("{0}-array", inputID); XmlNode flArrayNode = inputNode.AppendChild(this.xml.CreateElement("float_array", COLLADA)); SetIdAttribute(flArrayNode, arrayId); SetAttribute(flArrayNode, "count", keyCount.ToString()); flArrayNode.AppendChild(this.xml.CreateTextNode(sb.ToString())); AddAnimationCurveTechniqueNode(inputNode, keyCount, 1, arrayId, "TIME", "float"); } //output string outputID = string.Format("{0}-output", curveId); { XmlNode outputNode = curveNode.AppendChild(this.xml.CreateElement("source", COLLADA)); SetIdAttribute(outputNode, outputID); StringBuilder sb = new StringBuilder(); for (int i = 0; i < curves.positionX.length; ++i) { var mat = curves.CreateTransformMatrix(i); // already in right-handed coordinate var matStr = FormtMatrix4x4ForCollada(ref mat); sb.Append(matStr); } string arrayId = string.Format("{0}-array", outputID); XmlNode flArrayNode = outputNode.AppendChild(this.xml.CreateElement("float_array", COLLADA)); SetIdAttribute(flArrayNode, arrayId); SetAttribute(flArrayNode, "count", (keyCount * 16).ToString()); flArrayNode.AppendChild(this.xml.CreateTextNode(sb.ToString())); AddAnimationCurveTechniqueNode(outputNode, keyCount, 16, arrayId, "TRANSFORM", "float4x4"); } //interpolation string interpolationID = string.Format("{0}-interpolation", curveId); { XmlNode interpolationNode = curveNode.AppendChild(this.xml.CreateElement("source", COLLADA)); SetIdAttribute(interpolationNode, interpolationID); StringBuilder sb = new StringBuilder(); foreach (var k in curves.positionX.keys) { sb.Append("LINEAR "); } string arrayId = string.Format("{0}-array", outputID); XmlNode flArrayNode = interpolationNode.AppendChild(this.xml.CreateElement("Name_array", COLLADA)); SetIdAttribute(flArrayNode, arrayId); SetAttribute(flArrayNode, "count", keyCount.ToString()); flArrayNode.AppendChild(this.xml.CreateTextNode(sb.ToString())); AddAnimationCurveTechniqueNode(interpolationNode, keyCount, 1, arrayId, "INTERPOLATION", "name"); } //sampler string samplerID = string.Format("{0}-sampler", curveId); { var samplerNode = curveNode.AppendChild(this.xml.CreateElement("sampler", COLLADA)); SetIdAttribute(samplerNode, samplerID); var sin = samplerNode.AppendChild(this.xml.CreateElement("input", COLLADA)); SetAttribute(sin, "semantic", "INPUT"); SetAttribute(sin, "source", string.Format("#{0}", inputID)); var sout = samplerNode.AppendChild(this.xml.CreateElement("input", COLLADA)); SetAttribute(sout, "semantic", "OUTPUT"); SetAttribute(sout, "source", string.Format("#{0}", outputID)); var sinterpol = samplerNode.AppendChild(this.xml.CreateElement("input", COLLADA)); SetAttribute(sinterpol, "semantic", "INTERPOLATION"); SetAttribute(sinterpol, "source", string.Format("#{0}", interpolationID)); } //channel { var channelNode = curveNode.AppendChild(this.xml.CreateElement("channel", COLLADA)); SetAttribute(channelNode, "source", string.Format("#{0}", samplerID)); SetAttribute(channelNode, "target", string.Format("{0}/matrix", jointId)); } return(curveNode); }