private void WriteSkelAnimation(AnimationClip clipAnimation, GameObject root, BinaryWriter writer) { var trackBones = CloneTree(root).Select(_ => new BoneTrack(_)).ToList(); var cloneRoot = trackBones[0].gameObject; ISampler sampler; if (clipAnimation.legacy) { sampler = new LegacySampler(cloneRoot, clipAnimation); } else { sampler = new AnimatorSampler(cloneRoot, clipAnimation); } using (sampler) { var timeStep = 1.0f / clipAnimation.frameRate; var numKeyFrames = 1 + (int)(clipAnimation.length * clipAnimation.frameRate); for (var frame = 0; frame < numKeyFrames; ++frame) { var t = frame * timeStep; sampler.Sample(t); //clipAnimation.SampleAnimation(cloneRoot, t); //foreach (var trackBone in trackBones) //{ // clipAnimation.SampleAnimation(trackBone.gameObject, t); //} foreach (var trackBone in trackBones) { trackBone.Sample(t); } } } writer.Write(trackBones.Count); foreach (var bone in trackBones) { WriteStringSZ(writer, _engine.DecorateName(bone.gameObject.name)); writer.Write((byte)7); writer.Write(bone.translation.Count); for (var frame = 0; frame < bone.translation.Count; ++frame) { writer.Write(bone.keys[frame]); Write(writer, bone.translation[frame]); Write(writer, bone.rotation[frame]); Write(writer, bone.scale[frame]); } } //foreach (var bone in trackBones) //{ // bone.Reset(); //} Object.DestroyImmediate(trackBones[0].gameObject); }
private void WriteSkelAnimation(AnimationClip clipAnimation, GameObject root, BinaryWriter writer) { var trackBones = CloneTree(root).Select(_ => new BoneTrack(_)).ToList(); var cloneRoot = trackBones[0].gameObject; ISampler sampler; if (!clipAnimation.isHumanMotion) { sampler = new LegacySampler(cloneRoot, clipAnimation); } else { sampler = new AnimatorSampler(cloneRoot, clipAnimation); } using (sampler) { var timeStep = 1.0f / clipAnimation.frameRate; var numKeyFrames = 1 + (int)(clipAnimation.length * clipAnimation.frameRate); for (var frame = 0; frame < numKeyFrames; ++frame) { var t = frame * timeStep; sampler.Sample(t); //clipAnimation.SampleAnimation(cloneRoot, t); //foreach (var trackBone in trackBones) //{ // clipAnimation.SampleAnimation(trackBone.gameObject, t); //} foreach (var trackBone in trackBones) { trackBone.Sample(t); } } } writer.Write(trackBones.Count); foreach (var bone in trackBones) { bone.Optimize(); } foreach (var bone in trackBones) { var mask = 0; bool hasTranslation = false; bool hasRotation = false; bool hasScale = false; if (bone.translation != null && bone.translation.Count > 0) { mask |= 1; hasTranslation = true; } if (bone.rotation != null && bone.rotation.Count > 0) { mask |= 2; hasRotation = true; } if (bone.scale != null && bone.scale.Count > 0) { mask |= 4; hasScale = true; } if (mask == 0) { continue; } WriteStringSZ(writer, _engine.DecorateName(bone.gameObject.name)); writer.Write((byte)mask); writer.Write(bone.keys.Count); for (var frame = 0; frame < bone.keys.Count; ++frame) { writer.Write(bone.keys[frame]); if (hasTranslation) { Write(writer, bone.translation[frame]); } if (hasRotation) { Write(writer, bone.rotation[frame]); } if (hasScale) { Write(writer, bone.scale[frame]); } } } //foreach (var bone in trackBones) //{ // bone.Reset(); //} Object.DestroyImmediate(trackBones[0].gameObject); }