// 27 Slicer // Copyright 2021 Deftly Games // https://slicer.deftly.games/ using System.Collections.Generic; using UnityEngine; namespace Slicer.Core { /// /// Contains useful methods for working with UnityEngine.Vector3. /// public static class VectorUtility { /// /// Transforms the supplied vector by the supplied matrix /// /// The vector to transform /// The matrix to multiply by /// Returns the vectors transformed by the matrix public static Vector3 TransformVector(Vector3 v, Matrix4x4 matrix) { v = matrix.MultiplyPoint3x4(v); return v; } /// /// Calculates the bounds that encapsulates all of the vectors after they have been transformed by the supplied matrix. /// /// The vectors to encapsulate into the returned bounds. /// The matrix to transform all of the vectors by. /// The bounds that contains all of the transformed vectors. public static Bounds CalculateBounds(List vectors, Matrix4x4 matrix) { var min = TransformVector(vectors[0], matrix); var max = min; // Iterate through all verts except first one for (var i = 1; i < vectors.Count; i++) { var v = TransformVector(vectors[i], matrix); max = Vector3.Max(v, max); min = Vector3.Min(v, min); } var bounds = new Bounds(); bounds.SetMinMax(min, max); return bounds; } } }