getTransformMatrix4 static method

Matrix4? getTransformMatrix4(
  1. VoltronArray? transformList
)

Implementation

static Matrix4? getTransformMatrix4(VoltronArray? transformList) {
  if (transformList == null) {
    return null;
  }

  final result = List.filled(16, 0.0);
  final helperMatrix = sHelperMatrix;
  MatrixUtil.resetIdentityMatrix(result);
  var size = transformList.size();
  for (var transformIdx = 0; transformIdx < size; transformIdx++) {
    VoltronMap? transform = transformList.get<VoltronMap>(transformIdx);
    if (transform != null && transform.size() > 0) {
      for (var transformType in transform.keySet()) {
        MatrixUtil.resetIdentityMatrix(helperMatrix);
        Object? value = transform.get<Object>(transformType);
        if (transformType == "matrix" && value is VoltronArray) {
          var matrix = value;
          for (var i = 0; i < 16; i++) {
            Object? matrixValue = matrix.get<Object>(i);
            if (matrixValue is num) {
              helperMatrix[i] = matrixValue.toDouble();
            } else {
              helperMatrix[i] = 0;
            }
          }
        } else if (transformType == "perspective" && value is num) {
          MatrixUtil.applyPerspective(helperMatrix, value.toDouble());
        } else if (transformType == "rotateX") {
          MatrixUtil.applyRotateX(
              helperMatrix, convertToRadians(transform, transformType));
        } else if (transformType == "rotateY") {
          MatrixUtil.applyRotateY(
              helperMatrix, convertToRadians(transform, transformType));
        } else if (transformType == "rotate" || transformType == "rotateZ") {
          MatrixUtil.applyRotateZ(
              helperMatrix, convertToRadians(transform, transformType));
        } else if (transformType == "scale" && value is num) {
          var scale = value.toDouble();
          MatrixUtil.applyScaleX(helperMatrix, scale);
          MatrixUtil.applyScaleY(helperMatrix, scale);
        } else if (transformType == "scaleX" && value is num) {
          MatrixUtil.applyScaleX(helperMatrix, value.toDouble());
        } else if (transformType == "scaleY" && value is num) {
          MatrixUtil.applyScaleY(helperMatrix, value.toDouble());
        } else if (transformType == "translate" && value is VoltronArray) {
          var x = 0.0;
          var y = 0.0;
          var z = 0.0;
          if (value.size() > 0) {
            Object? tranX = value.get<Object>(0);
            if (tranX is num) {
              x = tranX.toDouble();
            }
          }

          if (value.size() > 1) {
            Object? tranY = value.get<Object>(1);
            if (tranY is num) {
              y = tranY.toDouble();
            }
          }

          if (value.size() > 2) {
            Object? tranZ = value.get<Object>(1);
            if (tranZ is num) {
              z = tranZ.toDouble();
            }
          }
          MatrixUtil.applyTranslate3D(helperMatrix, x, y, z);
        } else if (transformType == "translateX" && value is num) {
          MatrixUtil.applyTranslate2D(helperMatrix, value.toDouble(), 0);
        } else if (transformType == "translateY" && value is num) {
          MatrixUtil.applyTranslate2D(helperMatrix, 0, value.toDouble());
        } else if (transformType == "skewX") {
          MatrixUtil.applySkewX(
              helperMatrix, convertToRadians(transform, transformType));
        } else if (transformType == "skewY") {
          MatrixUtil.applySkewY(
              helperMatrix, convertToRadians(transform, transformType));
        } else {
          LogUtils.e(
              "transform", "Unsupported transform type:$transformType");
        }
        MatrixUtil.multiplyInto(result, result, helperMatrix);
      }
    }
  }

  return Matrix4.fromList(result);
}