public PBAutoUnwrapSettings GetSettings(MeshSelection selection) { if (selection == null) { return PBAutoUnwrapSettings.defaultAutoUnwrapSettings; } if (selection.HasVertices) { selection.VerticesToFaces(false); } else if (selection.HasEdges) { selection.EdgesToFaces(false); } PBAutoUnwrapSettings unwrapSettings = PBAutoUnwrapSettings.defaultAutoUnwrapSettings; IList<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;) { Face face = faces[i]; unwrapSettings = face.uv; break; } } return unwrapSettings; }
private void SetTextureGroup(MeshSelection selection, int textureGroup = 0) { if (selection.HasVertices) { selection.VerticesToFaces(false); } else if (selection.HasEdges) { selection.EdgesToFaces(false); } List<Face> faces = new List<Face>(); Dictionary<ProBuilderMesh, IList<int>> selectedFaces = selection.SelectedFaces; foreach (KeyValuePair<ProBuilderMesh, IList<int>> kvp in selectedFaces) { faces.Clear(); ProBuilderMesh mesh = kvp.Key; mesh.GetFaces(kvp.Value, faces); int texGroup = textureGroup >= 0 ? GetUnusedTextureGroup(mesh) : -1; for (int i = 0; i < faces.Count; ++i) { Face face = faces[i]; face.textureGroup = texGroup; } mesh.RefreshUV(faces); mesh.ToMesh(); mesh.Refresh(); } }
public void ApplySettings(PBAutoUnwrapSettings settings, MeshSelection selection) { if (selection == null) { return; } if (selection.HasVertices) { selection.VerticesToFaces(false); } else if (selection.HasEdges) { selection.EdgesToFaces(false); } AutoUnwrapSettings unwrapSettings = settings; foreach (KeyValuePair <ProBuilderMesh, IList <Face> > kvp in selection.SelectedFaces) { ProBuilderMesh mesh = kvp.Key; IList <Face> faces = kvp.Value; for (int i = 0; i < faces.Count; ++i) { Face face = faces[i]; face.uv = unwrapSettings; } mesh.RefreshUV(faces); } }
public void SetAutoUV(MeshSelection selection, bool auto) { 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); PBAutoUVConversion.SetAutoUV(mesh, faces.ToArray(), auto); mesh.ToMesh(); mesh.Refresh(); } }
public override void Delete() { MeshSelection selection = GetSelection(); selection.VerticesToFaces(false, true); foreach (KeyValuePair <ProBuilderMesh, IList <int> > kvp in selection.SelectedFaces) { kvp.Key.DeleteFaces(kvp.Value); kvp.Key.ToMesh(); kvp.Key.Refresh(); } }
public ApplyMaterialResult ApplyMaterial(Material material, MeshSelection selection) { if (selection == null) { return(null); } if (selection.HasVertices) { selection.VerticesToFaces(false, false); } else if (selection.HasEdges) { selection.EdgesToFaces(false, false); } MeshMaterialsState oldState = new MeshMaterialsState(); MeshMaterialsState newState = new MeshMaterialsState(); List <Face> faces = new List <Face>(); foreach (KeyValuePair <ProBuilderMesh, IList <int> > kvp in selection.SelectedFaces) { ProBuilderMesh mesh = kvp.Key; AddAllFacesToState(oldState, mesh); AddMaterialsToState(oldState, mesh); faces.Clear(); mesh.GetFaces(kvp.Value, faces); mesh.SetMaterial(faces, material); mesh.Refresh(); mesh.ToMesh(); RemoveUnusedMaterials(mesh); mesh.Refresh(); mesh.ToMesh(); GetFaceToSubmeshIndexes(newState.FaceToSubmeshIndex, kvp, mesh); AddMaterialsToState(newState, mesh); } if (oldState.FaceToSubmeshIndex.Count == 0 && newState.FaceToSubmeshIndex.Count == 0) { return(null); } return(new ApplyMaterialResult(oldState, newState)); }
public bool HasAutoUV(MeshSelection selection, bool auto) { if (selection == null) { return(false); } if (selection.HasVertices) { selection.VerticesToFaces(false, false); } else if (selection.HasEdges) { selection.EdgesToFaces(false, false); } IList <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) { Face face = faces[i]; if (auto) { if (!face.manualUV) { return(true); } } else { if (face.manualUV) { return(true); } } } } return(false); }
public MeshSelection ToFaces(bool invert, bool partial = false) { MeshSelection selection = new MeshSelection(this); if (selection.HasEdges) { selection.EdgesToFaces(invert, partial); } else if (selection.HasVertices) { selection.VerticesToFaces(invert, partial); } else { selection.SelectedEdges.Clear(); selection.UnselectedEdges.Clear(); selection.SelectedIndices.Clear(); selection.UnselectedIndices.Clear(); } return(selection); }
public void ResetUV(MeshSelection selection) { if (selection == null) { return; } if (selection.HasVertices) { selection.VerticesToFaces(false, false); } else if (selection.HasEdges) { selection.EdgesToFaces(false, 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 void Merge(MeshSelection selection, bool partial) { if (HasFaces) { if (selection.HasEdges) { selection.EdgesToFaces(false, partial); } else if (selection.HasVertices) { selection.VerticesToFaces(false, partial); } foreach (KeyValuePair <ProBuilderMesh, IList <int> > kvp in selection.SelectedFaces) { ProBuilderMesh mesh = kvp.Key; IList <int> faces = kvp.Value; if (!SelectedFaces.ContainsKey(mesh)) { SelectedFaces.Add(mesh, faces); } else { IList <int> existingFaces = SelectedFaces[mesh].ToList(); MergeLists(faces, existingFaces); SelectedFaces[mesh] = existingFaces; } } foreach (KeyValuePair <ProBuilderMesh, IList <int> > kvp in selection.UnselectedFaces) { ProBuilderMesh mesh = kvp.Key; IList <int> faces = kvp.Value; if (!UnselectedFaces.ContainsKey(mesh)) { UnselectedFaces.Add(mesh, faces); } else { IList <int> existingFaces = UnselectedFaces[mesh].ToList(); MergeLists(faces, existingFaces); UnselectedFaces[mesh] = existingFaces; } } } else if (HasEdges) { if (selection.HasFaces) { selection.FacesToEdges(false); } else if (selection.HasVertices) { selection.VerticesToEdges(false, partial); } foreach (KeyValuePair <ProBuilderMesh, IList <Edge> > kvp in selection.SelectedEdges) { ProBuilderMesh mesh = kvp.Key; IList <Edge> edges = kvp.Value; if (!SelectedEdges.ContainsKey(mesh)) { SelectedEdges.Add(mesh, edges); } else { IList <Edge> existingEdges = SelectedEdges[mesh].ToList(); MergeLists(edges, existingEdges); SelectedEdges[mesh] = existingEdges; } } foreach (KeyValuePair <ProBuilderMesh, IList <Edge> > kvp in selection.UnselectedEdges) { ProBuilderMesh mesh = kvp.Key; IList <Edge> edges = kvp.Value; if (!UnselectedEdges.ContainsKey(mesh)) { UnselectedEdges.Add(mesh, edges); } else { IList <Edge> existingEdges = UnselectedEdges[mesh].ToList(); MergeLists(edges, existingEdges); UnselectedEdges[mesh] = existingEdges; } } } else if (HasVertices) { if (selection.HasFaces) { selection.FacesToVertices(false); } else if (selection.HasEdges) { selection.EdgesToVertices(false); } foreach (KeyValuePair <ProBuilderMesh, IList <int> > kvp in selection.SelectedIndices) { ProBuilderMesh mesh = kvp.Key; IList <int> indexes = kvp.Value; if (!SelectedIndices.ContainsKey(mesh)) { SelectedIndices.Add(mesh, indexes); } else { IList <int> existingIndexes = SelectedIndices[mesh].ToList(); MergeLists(indexes, existingIndexes); SelectedIndices[mesh] = existingIndexes; } } foreach (KeyValuePair <ProBuilderMesh, IList <int> > kvp in selection.SelectedIndices) { ProBuilderMesh mesh = kvp.Key; IList <int> indexes = kvp.Value; if (!UnselectedIndices.ContainsKey(mesh)) { UnselectedIndices.Add(mesh, indexes); } else { IList <int> existingIndexes = UnselectedIndices[mesh].ToList(); MergeLists(indexes, existingIndexes); UnselectedIndices[mesh] = existingIndexes; } } } }
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(); }