/// <summary> /// Scale mesh vertices to fit within a bounds size. /// </summary> /// <param name="mesh">The mesh to apply scaling to.</param> /// <param name="currentSize">The bounding size of the original shape we want to fit.</param> /// <param name="sizeToFit">The size to fit mesh contents within.</param> public static void FitToSize(ProBuilderMesh mesh, Bounds currentSize, Vector3 sizeToFit) { if (mesh.vertexCount < 1) { return; } var scale = Math.Abs(sizeToFit).DivideBy(currentSize.size); if (scale == Vector3.one || scale == Vector3.zero) { return; } var positions = mesh.positionsInternal; if (System.Math.Abs(currentSize.size.x) < 0.001f) { scale.x = 0; } if (System.Math.Abs(currentSize.size.y) < 0.001f) { scale.y = 0; } if (System.Math.Abs(currentSize.size.z) < 0.001f) { scale.z = 0; } for (int i = 0, c = mesh.vertexCount; i < c; i++) { positions[i] -= currentSize.center; positions[i].Scale(scale); positions[i] += currentSize.center; } mesh.Rebuild(); }