示例#1
0
  SkeletonJoint getJoint(Element jointNode, boolean isRootNode) {
    SkeletonJoint joint = new SkeletonJoint();
    joint.name = jointNode.getAttribute("id");
    if (!isRootNode) {
      Element matrixElement = jointNode.getChildByName("matrix");
      if (matrixElement != null) {
        Matrix4 m = getMatrix(matrixElement);
        m.getTranslation(joint.position);
        m.getRotation(joint.rotation);
        // TODO: get scale from matrix
      }
    }

    Array<Element> nodes = jointNode.getChildrenByName("node");
    for (int i = 0; i < nodes.size; i++) {
      SkeletonJoint child = getJoint(nodes.get(i), false);
      if (!isRootNode) {
        child.parent = joint;
      }
      joint.children.add(child);
    }

    return joint;
  }
  private Skeleton generateSkeleton(
      com.badlogic.gdx.graphics.g3d.loaders.ogre.skeleton.Skeleton ogreSkel) {
    List<Bone> bones = ogreSkel.getBones().getBone();
    List<SkeletonJoint> joints = new ArrayList<SkeletonJoint>();
    Map<String, SkeletonJoint> nameToJoint = new HashMap<String, SkeletonJoint>();
    for (int i = 0; i < bones.size(); i++) {
      Bone bone = bones.get(i);
      SkeletonJoint joint = new SkeletonJoint();

      joint.name = bone.name;
      joint.position.set(bone.position.x, bone.position.y, bone.position.z);
      joint.rotation.setFromAxis(
          bone.rotation.axis.x,
          bone.rotation.axis.y,
          bone.rotation.axis.z,
          MathUtils.radiansToDegrees * bone.rotation.angle);
      if (bone.scale != null) {
        if (bone.scale.factor == 0) joint.scale.set(bone.scale.x, bone.scale.y, bone.scale.z);
        else joint.scale.set(bone.scale.factor, bone.scale.factor, bone.scale.factor);
      }
      joints.add(joint);
      nameToJoint.put(joint.name, joint);
    }

    List<Boneparent> hierarchy = ogreSkel.getBonehierarchy().getBoneparent();
    for (int i = 0; i < hierarchy.size(); i++) {
      Boneparent link = hierarchy.get(i);
      SkeletonJoint joint = nameToJoint.get(link.getBone());
      SkeletonJoint parent = nameToJoint.get(link.getParent());
      parent.children.add(joint);
      joint.parent = parent;
    }

    Skeleton skel = new Skeleton();
    for (int i = 0; i < joints.size(); i++) {
      SkeletonJoint joint = joints.get(i);
      if (joint.parent == null) skel.hierarchy.add(joint);
    }

    skel.buildFromHierarchy();

    List<Animation> animations = ogreSkel.getAnimations().getAnimation();
    for (int i = 0; i < animations.size(); i++) {
      Animation animation = animations.get(i);
      SkeletonKeyframe[][] perJointkeyFrames = new SkeletonKeyframe[skel.bindPoseJoints.size][];

      List<Track> tracks = animation.getTracks().getTrack();
      if (tracks.size() != perJointkeyFrames.length)
        throw new IllegalArgumentException("Number of tracks does not equal number of joints");

      Matrix4 rotation = new Matrix4();
      Matrix4 transform = new Matrix4();

      for (int j = 0; j < tracks.size(); j++) {
        Track track = tracks.get(j);
        String jointName = track.getBone();
        int jointIndex = skel.namesToIndices.get(jointName);
        if (perJointkeyFrames[jointIndex] != null)
          throw new IllegalArgumentException(
              "Track for bone "
                  + jointName
                  + " in animation "
                  + animation.name
                  + " already defined!");
        SkeletonKeyframe[] jointKeyFrames =
            new SkeletonKeyframe[track.getKeyframes().getKeyframe().size()];
        perJointkeyFrames[jointIndex] = jointKeyFrames;

        for (int k = 0; k < track.getKeyframes().getKeyframe().size(); k++) {
          Keyframe keyFrame = track.getKeyframes().getKeyframe().get(k);
          SkeletonKeyframe jointKeyframe = new SkeletonKeyframe();
          jointKeyframe.timeStamp = keyFrame.time;
          jointKeyframe.position.set(
              keyFrame.translate.x, keyFrame.translate.y, keyFrame.translate.z);
          if (keyFrame.scale != null) {
            if (keyFrame.scale.factor == 0)
              jointKeyframe.scale.set(keyFrame.scale.x, keyFrame.scale.y, keyFrame.scale.z);
            else
              jointKeyframe.scale.set(
                  keyFrame.scale.factor, keyFrame.scale.factor, keyFrame.scale.factor);
          }
          jointKeyframe
              .rotation
              .setFromAxis(
                  keyFrame.rotate.axis.x,
                  keyFrame.rotate.axis.y,
                  keyFrame.rotate.axis.z,
                  MathUtils.radiansToDegrees * keyFrame.rotate.angle)
              .nor();
          jointKeyframe.parentIndex = skel.bindPoseJoints.get(jointIndex).parentIndex;
          jointKeyFrames[k] = jointKeyframe;

          rotation.set(jointKeyframe.rotation);
          rotation.trn(jointKeyframe.position);
          transform.set(skel.sceneMatrices.get(jointIndex));
          transform.mul(rotation);
          if (jointKeyframe.parentIndex != -1) {
            rotation.set(skel.offsetMatrices.get(jointKeyframe.parentIndex)).mul(transform);
            transform.set(rotation);
          }

          transform.getTranslation(jointKeyframe.position);
          transform.getRotation(jointKeyframe.rotation);
        }
      }

      for (int j = 0; j < perJointkeyFrames.length; j++) {
        if (perJointkeyFrames[j] == null)
          throw new IllegalArgumentException("No track for bone " + skel.jointNames.get(j));
      }

      skel.animations.put(
          animation.name,
          new SkeletonAnimation(animation.name, animation.length, perJointkeyFrames));
    }

    return skel;
  }