generateRotationTrack method

dynamic generateRotationTrack(
  1. dynamic modelName,
  2. Map curves,
  3. dynamic initialValue,
  4. dynamic preRotation,
  5. dynamic postRotation,
  6. dynamic eulerOrder,
)

Implementation

generateRotationTrack(modelName, Map curves, initialValue, preRotation,
    postRotation, eulerOrder) {
  if (curves["x"] != null) {
    this.interpolateRotations(curves["x"]);
    curves["x"]["values"] = curves["x"]["values"]
        .map((v) => MathUtils.degToRad(v).toDouble())
        .toList();
  }

  if (curves["y"] != null) {
    this.interpolateRotations(curves["y"]);
    curves["y"]["values"] = curves["y"]["values"]
        .map((v) => MathUtils.degToRad(v).toDouble())
        .toList();
  }

  if (curves["z"] != null) {
    this.interpolateRotations(curves["z"]);
    curves["z"]["values"] = curves["z"]["values"]
        .map((v) => MathUtils.degToRad(v).toDouble())
        .toList();
  }

  var times = this.getTimesForAllAxes(curves);
  var values = this.getKeyframeTrackValues(times, curves, initialValue);

  Quaternion? preRotationQuaternion;

  if (preRotation != null) {
    preRotation =
        preRotation.map((v) => MathUtils.degToRad(v).toDouble()).toList();
    preRotation.add(eulerOrder);

    if (preRotation.length == 4 &&
        Euler.RotationOrders.indexOf(preRotation[3]) >= 0) {
      preRotation[3] =
          Euler.RotationOrders.indexOf(preRotation[3]).toDouble();
    }

    var preRotationEuler =
        new Euler().fromArray(List<double>.from(preRotation));
    preRotationQuaternion = new Quaternion().setFromEuler(preRotationEuler);
  }

  if (postRotation != null) {
    postRotation =
        postRotation.map((v) => MathUtils.degToRad(v).toDouble()).toList();
    postRotation.push(eulerOrder);

    postRotation = new Euler().fromArray(postRotation);
    postRotation = new Quaternion().setFromEuler(postRotation).invert();
  }

  var quaternion = new Quaternion();
  var euler = new Euler();

  List<num> quaternionValues =
      List<num>.filled(((values.length / 3) * 4).toInt(), 0.0);

  for (var i = 0; i < values.length; i += 3) {
    euler.set(values[i], values[i + 1], values[i + 2], eulerOrder);

    quaternion.setFromEuler(euler);

    if (preRotationQuaternion != null)
      quaternion.premultiply(preRotationQuaternion);
    if (postRotation != null) quaternion.multiply(postRotation);

    quaternion.toArray(quaternionValues, ((i / 3) * 4).toInt());
  }

  return new QuaternionKeyframeTrack(
      '${modelName}.quaternion', times, quaternionValues);
}