public static MeshSelection ExpandSelection(MeshSelection selection) { selection = selection.ToFaces(false); foreach (PBMesh pbMesh in selection.GetSelectedMeshes().ToArray()) { ProBuilderMesh mesh = pbMesh.ProBuilderMesh; IList <Face> selectedFaces = new List <Face>(); mesh.GetFaces(selection.GetFaces(pbMesh).ToArray(), selectedFaces); HashSet <int> groupsHs = new HashSet <int>(); for (int i = 0; i < selectedFaces.Count; ++i) { Face face = selectedFaces[i]; if (!groupsHs.Contains(face.smoothingGroup)) { groupsHs.Add(face.smoothingGroup); } } IList <int> selectedIndices = new List <int>(); IList <Face> faces = mesh.faces; for (int i = 0; i < faces.Count; ++i) { Face face = faces[i]; if (groupsHs.Contains(face.smoothingGroup)) { selectedIndices.Add(i); } } selection.SelectedFaces[pbMesh.gameObject] = selectedIndices; } return(selection); }
public override void Drag(Vector3 position, Quaternion rotation, Vector3 scale) { base.Drag(position, rotation, scale); Vector3 lp = MatrixInv.MultiplyPoint(position); Vector2 delta = new Vector2(-lp.x, -lp.y); List <Face> faces = new List <Face>(); for (int m = 0; m < Meshes.Length; ++m) { ProBuilderMesh mesh = Meshes[m]; Vector2[] origins = Origins[m]; var uvTransforms = UVTransforms[m]; int[] indexes = Indexes[m]; Vector2[] textures = mesh.textures.ToArray(); IList <Vector4> tangents = mesh.tangents; // Account for object scale delta *= k_vector3Magnitude / mesh.transform.lossyScale.magnitude; for (int i = 0; i < indexes.Length; ++i) { int index = indexes[i]; var uvTransform = uvTransforms[i]; textures[index] = origins[i] + new Vector2(tangents[index].w * delta.x / uvTransform.scale.x, delta.y / uvTransform.scale.y); } mesh.textures = textures; mesh.GetFaces(Selection.SelectedFaces[mesh.gameObject], faces); mesh.RefreshUV(faces); faces.Clear(); } }
public void ResetUV(MeshSelection selection) { if (selection == null) { return; } if (selection.HasVertices) { selection.VerticesToFaces(false); } else if (selection.HasEdges) { selection.EdgesToFaces(false); } List<Face> faces = new List<Face>(); foreach (KeyValuePair<ProBuilderMesh, IList<int>> kvp in selection.SelectedFaces) { ProBuilderMesh mesh = kvp.Key; faces.Clear(); mesh.GetFaces(kvp.Value, faces); for(int i = 0; i < faces.Count; ++i) { faces[i].uv = AutoUnwrapSettings.defaultAutoUnwrapSettings; } mesh.RefreshUV(faces); mesh.ToMesh(); mesh.Refresh(); } }
public virtual void BeginDrag(MeshSelection selection, Vector3 initialPosition, Quaternion initialRotation) { Selection = selection; if (Selection.HasEdges) { Selection.EdgesToFaces(false, false); } if (Selection.HasVertices) { Selection.VerticesToFaces(false, false); } InitialPosition = initialPosition; InitialRotation = initialRotation; Matrix = Matrix4x4.TRS(InitialPosition, InitialRotation, Vector3.one); MatrixInv = Matrix.inverse; List <ProBuilderMesh> allMeshes = new List <ProBuilderMesh>(); List <Vector2[]> allOrigins = new List <Vector2[]>(); List <Vector2[]> allFaceCenters = new List <Vector2[]>(); List <PBAutoUVConversion.UVTransform[]> allUVTransforms = new List <PBAutoUVConversion.UVTransform[]>(); List <bool[]> allIsManualUv = new List <bool[]>(); List <int[]> allIndexes = new List <int[]>(); HashSet <int> indexes = new HashSet <int>(); List <Vector2> origins = new List <Vector2>(); List <Vector2> faceCenters = new List <Vector2>(); List <PBAutoUVConversion.UVTransform> uvTransforms = new List <PBAutoUVConversion.UVTransform>(); List <bool> isManualUv = new List <bool>(); List <Face> faces = new List <Face>(); foreach (KeyValuePair <ProBuilderMesh, IList <int> > kvp in Selection.SelectedFaces) { ProBuilderMesh mesh = kvp.Key; mesh.GetFaces(kvp.Value, faces); IList <Vector2> textures = mesh.textures; for (int f = 0; f < faces.Count; ++f) { Face face = faces[f]; IList <int> faceIndexes = face.indexes; for (int i = 0; i < faceIndexes.Count; ++i) { int faceIndex = faceIndexes[i]; if (!indexes.Contains(faceIndex)) { indexes.Add(faceIndex); origins.Add(textures[faceIndex]); faceCenters.Add(GetCenterOfMass(textures, face)); PBAutoUVConversion.UVTransform transform = PBAutoUVConversion.GetUVTransform(mesh, face); if (!face.manualUV) { Vector2 scale = transform.scale; scale.x = -scale.x; transform.scale = scale; } uvTransforms.Add(transform); } } isManualUv.Add(face.manualUV); face.manualUV = true; } allIndexes.Add(indexes.ToArray()); allOrigins.Add(origins.ToArray()); allFaceCenters.Add(faceCenters.ToArray()); allUVTransforms.Add(uvTransforms.ToArray()); allIsManualUv.Add(isManualUv.ToArray()); allMeshes.Add(mesh); indexes.Clear(); origins.Clear(); faceCenters.Clear(); uvTransforms.Clear(); isManualUv.Clear(); faces.Clear(); } Indexes = allIndexes.ToArray(); Origins = allOrigins.ToArray(); FaceCenters = allFaceCenters.ToArray(); UVTransforms = allUVTransforms.ToArray(); IsManualUv = allIsManualUv.ToArray(); Meshes = allMeshes.ToArray(); }