public static void Flip(CSGBrush[] brushes, Matrix4x4 flipMatrix, string undoDescription = "Flip brushes") { var fail = false; Undo.IncrementCurrentGroup(); Undo.RegisterCompleteObjectUndo(brushes.ToArray <UnityEngine.Object>(), undoDescription); var isGlobal = Tools.pivotRotation == PivotRotation.Global; var centerAll = BoundsUtilities.GetCenter(brushes); for (var t = 0; t < brushes.Length; t++) { var brush = brushes[t]; var position = brush.transform.position; Matrix4x4 brushFlip; Vector3 brushCenter; if (isGlobal) { brushFlip = brush.transform.localToWorldMatrix * flipMatrix * brush.transform.worldToLocalMatrix; brushCenter = brush.transform.InverseTransformPoint(centerAll) - position; } else { brushFlip = flipMatrix; brushCenter = brush.transform.InverseTransformPoint(centerAll); } brushFlip = Matrix4x4.TRS(brushCenter, Quaternion.identity, Vector3.one) * brushFlip * Matrix4x4.TRS(-brushCenter, Quaternion.identity, Vector3.one); brush.EnsureInitialized(); var shape = brush.Shape; for (var s = 0; s < shape.Surfaces.Length; s++) { var plane = shape.Surfaces[s].Plane; var normal = brushFlip.MultiplyVector(plane.normal); var biNormal = brushFlip.MultiplyVector(shape.Surfaces[s].BiNormal); var tangent = brushFlip.MultiplyVector(shape.Surfaces[s].Tangent); var pointOnPlane = plane.pointOnPlane; pointOnPlane = brushFlip.MultiplyPoint(pointOnPlane); shape.Surfaces[s].Plane = new CSGPlane(normal, pointOnPlane); shape.Surfaces[s].BiNormal = biNormal; shape.Surfaces[s].Tangent = tangent; } var controlMesh = brush.ControlMesh; var vertices = controlMesh.Vertices; for (var v = 0; v < vertices.Length; v++) { vertices[v] = brushFlip.MultiplyPoint(vertices[v]); } var polygons = controlMesh.Polygons; for (var p = 0; p < polygons.Length; p++) { Array.Reverse(polygons[p].EdgeIndices); } var edges = controlMesh.Edges; var twinVertices = new short[edges.Length]; for (var e = 0; e < edges.Length; e++) { twinVertices[e] = edges[edges[e].TwinIndex].VertexIndex; } for (var e = 0; e < edges.Length; e++) { edges[e].VertexIndex = twinVertices[e]; } brush.ControlMesh.SetDirty(); EditorUtility.SetDirty(brush); InternalCSGModelManager.CheckSurfaceModifications(brush, true); ControlMeshUtility.RebuildShape(brush); } if (fail) { Debug.LogWarning("Failed to perform operation"); Undo.RevertAllInCurrentGroup(); } InternalCSGModelManager.Refresh(); }
public static void Flip(CSGBrush[] brushes, Matrix4x4 flipMatrix, string undoDescription = "Flip brushes") { var fail = false; Undo.IncrementCurrentGroup(); Undo.RegisterCompleteObjectUndo(brushes.ToArray <UnityEngine.Object>(), undoDescription); var isGlobal = Tools.pivotRotation == PivotRotation.Global; var centerAll = BoundsUtilities.GetCenter(brushes); for (var t = 0; t < brushes.Length; t++) { var brush = brushes[t]; var shape = brush.Shape; var position = brush.transform.position; Matrix4x4 brushFlip; Vector3 brushCenter; if (isGlobal) { brushFlip = brush.transform.localToWorldMatrix * flipMatrix * brush.transform.worldToLocalMatrix; brushCenter = brush.transform.InverseTransformPoint(centerAll) - position; } else { brushFlip = flipMatrix; brushCenter = brush.transform.InverseTransformPoint(centerAll); } for (var s = 0; s < shape.Surfaces.Length; s++) { var plane = shape.Surfaces[s].Plane; var normal = brushFlip.MultiplyVector(plane.normal); var biNormal = brushFlip.MultiplyVector(shape.Surfaces[s].BiNormal); var tangent = brushFlip.MultiplyVector(shape.Surfaces[s].Tangent); var pointOnPlane = plane.pointOnPlane; pointOnPlane -= brushCenter; pointOnPlane = brushFlip.MultiplyPoint(pointOnPlane); pointOnPlane += brushCenter; shape.Surfaces[s].Plane = new CSGPlane(normal, pointOnPlane); shape.Surfaces[s].BiNormal = biNormal; shape.Surfaces[s].Tangent = tangent; } var controlMesh = brush.ControlMesh; brush.Shape = shape; brush.ControlMesh = ControlMeshUtility.CreateFromShape(shape); if (brush.ControlMesh == null) { brush.ControlMesh = controlMesh; fail = true; break; } ShapeUtility.CreateCutter(shape, brush.ControlMesh); brush.EnsureInitialized(); brush.ControlMesh.SetDirty(); EditorUtility.SetDirty(brush); InternalCSGModelManager.CheckSurfaceModifications(brush, true); ControlMeshUtility.RebuildShape(brush); } if (fail) { Debug.Log("Failed to perform operation"); Undo.RevertAllInCurrentGroup(); } InternalCSGModelManager.Refresh(); }