namespace UnityEngine.ProBuilder.MeshOperations { /// /// Functions for manipulating the transform of a mesh. /// public static class MeshTransform { /// /// Set the pivot point for a mesh to either the center, or a corner point of the bounding box. /// /// The to adjust vertices for a new pivot point. /// The new pivot point is either the center of the mesh bounding box, or /// the bounds center - extents. internal static void SetPivot(this ProBuilderMesh mesh, PivotLocation pivotLocation) { var bounds = mesh.GetBounds(); var pivot = pivotLocation == PivotLocation.Center ? bounds.center : bounds.center - bounds.extents; SetPivot(mesh, mesh.transform.TransformPoint(pivot)); } /// /// Centers the mesh pivot at the average of a set of vertex positions. /// /// This is the equivalent of the [Center Pivot](../manual/CenterPivot.html) action. /// /// The target mesh. /// The array of indices representing the positions to average in order to find the new pivot. public static void CenterPivot(this ProBuilderMesh mesh, int[] indexes) { if (mesh == null) throw new System.ArgumentNullException("mesh"); Vector3 center = Vector3.zero; if (indexes != null && indexes.Length > 0) { Vector3[] positions = mesh.positionsInternal; if (positions == null || positions.Length < 3) return; foreach (int i in indexes) center += positions[i]; center = mesh.transform.TransformPoint(center / (float)indexes.Length); } else { center = mesh.transform.TransformPoint(mesh.mesh.bounds.center); } Vector3 dir = (mesh.transform.position - center); mesh.transform.position = center; mesh.ToMesh(); mesh.TranslateVerticesInWorldSpace(mesh.mesh.triangles, dir); mesh.Refresh(); } /// /// Sets the pivot point of a mesh in world space. The Transform component position property is set to worldPosition, /// while the mesh geometry does not move. /// /// This is the equivalent of the [Set Pivot (Vertices)](../manual/Vert_SetPivot.html) action. /// /// The target mesh. /// The new pivot position in world space. public static void SetPivot(this ProBuilderMesh mesh, Vector3 worldPosition) { if (mesh == null) throw new System.ArgumentNullException("mesh"); var transform = mesh.transform; Vector3 offset = transform.position - worldPosition; transform.position = worldPosition; mesh.ToMesh(); mesh.TranslateVerticesInWorldSpace(mesh.mesh.triangles, offset); mesh.Refresh(); } /// /// Scales vertices and sets `transform.localScale` to `Vector3.one`. /// /// This is the equivalent of the [Freeze Transform](../manual/Freeze_Transform.html) action. /// /// The target mesh. public static void FreezeScaleTransform(this ProBuilderMesh mesh) { if (mesh == null) throw new System.ArgumentNullException("mesh"); Vector3[] v = mesh.positionsInternal; for (var i = 0; i < v.Length; i++) v[i] = Vector3.Scale(v[i], mesh.transform.localScale); mesh.transform.localScale = new Vector3(1f, 1f, 1f); } } }