// 27 Slicer
// Copyright 2021 Deftly Games
// https://slicer.deftly.games/
using UnityEngine;
namespace Slicer.Core
{
///
/// Contains useful methods for working with UnityEngine.Matrix4x4.
///
public static class MatrixUtility
{
///
/// Builds a transform matrix that transforms childTransform into the Local Object Space of rootTransform.
///
/// The transform used to build the Matrix
/// The transform to use as the Local Object Space
/// Returns a transform matrix in Local Object Space of the GameObject the rootTransform.
public static Matrix4x4 BuildTransformMatrix(Transform childTransform, Transform rootTransform)
{
var modelMatrix = childTransform.localToWorldMatrix;
var inversedRootMatrix = rootTransform.worldToLocalMatrix;
var matrix = inversedRootMatrix * modelMatrix;
return matrix;
}
///
/// Builds an inverse transform matrix that transforms childTransform into the Local Object Space of rootTransform.
///
/// The transform used to build the Matrix
/// The transform to use as the Local Object Space
/// Returns a transform matrix in Local Object Space of the GameObject the rootTransform.
public static Matrix4x4 BuildInverseTransformMatrix(Transform childTransform, Transform rootTransform)
{
var matrix = BuildTransformMatrix(rootTransform, childTransform);
return matrix;
}
}
}