public static void Init(PBMesh mesh, Vector2 scale) { if (mesh.m_pbMesh != null) { return; } mesh.m_meshFilter = mesh.GetComponent <MeshFilter>(); mesh.m_pbMesh = mesh.GetComponent <ProBuilderMesh>(); if (mesh.m_pbMesh == null) { mesh.m_pbMesh = mesh.gameObject.AddComponent <ProBuilderMesh>(); if (mesh.m_positions != null) { Face[] faces = mesh.m_faces.Select(f => f.ToFace()).ToArray(); mesh.m_pbMesh.Rebuild(mesh.m_positions, faces, mesh.m_textures); IList <Face> actualFaces = mesh.m_pbMesh.faces; for (int i = 0; i < actualFaces.Count; ++i) { actualFaces[i].submeshIndex = mesh.m_faces[i].SubmeshIndex; } mesh.m_pbMesh.Refresh(); mesh.m_pbMesh.ToMesh(); } else { ImportMesh(mesh.m_meshFilter, mesh.m_pbMesh, scale); } } }
public PBSmoothGroupData(PBMesh pb) { CreateMaterialsIfRequired(); m_groups = new Dictionary <int, List <Face> >(); //selected = new HashSet<int>(); groupColors = new Dictionary <int, Color>(); m_previewMesh = new Mesh() { hideFlags = HideFlags.HideAndDontSave, name = pb.name + "_SmoothingPreview" }; m_normalsMesh = new Mesh() { hideFlags = HideFlags.HideAndDontSave, name = pb.name + "_SmoothingNormals" }; m_preview = new GameObject("SmoothingPreview"); m_preview.AddComponent <MeshFilter>().sharedMesh = m_previewMesh; m_preview.AddComponent <MeshRenderer>().sharedMaterial = m_previewMaterial; m_normals = new GameObject("SmoothingNormals"); m_normals.AddComponent <MeshFilter>().sharedMesh = m_normalsMesh; m_normals.AddComponent <MeshRenderer>().sharedMaterial = m_normalsMaterial; m_preview.transform.SetParent(pb.transform, false); m_normals.transform.SetParent(pb.transform, false); PBMesh = pb; Rebuild(pb); }
public override void SetSelection(MeshSelection selection) { if (m_faceSelection != null) { m_faceSelection.Clear(); } if (selection != null) { selection = selection.ToFaces(false); m_faceSelection.BeginChange(); foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedFaces) { ProBuilderMesh mesh = kvp.Key.GetComponent <ProBuilderMesh>(); PBMesh pbMesh = mesh.GetComponent <PBMesh>(); if (pbMesh.IsMarkedAsDestroyed) { continue; } foreach (int face in kvp.Value) { m_faceSelection.Add(mesh, face); } } m_faceSelection.EndChange(); } }
public void Clear() { for (int i = 0; i < m_meshes.Count; ++i) { ProBuilderMesh mesh = m_meshes[i]; MeshFilter filter = m_meshToSelection[mesh]; Destroy(filter.gameObject); PBMesh pbMesh = m_pbMeshes[i]; if (pbMesh != null) { pbMesh.RaiseUnselected(); } } m_meshToSelection.Clear(); m_meshToEdges.Clear(); m_meshToEdgesList.Clear(); m_meshes.Clear(); m_pbMeshes.Clear(); m_edgeToSelection.Clear(); m_coincidentEdges.Clear(); m_selectedEdgesCount = 0; m_centerOfMass = Vector3.zero; m_lastNormal = Vector3.forward; m_lastPosition = Vector3.zero; }
public void RebuildNormalsMesh(PBMesh pb) { m_normalsMesh.Clear(); Mesh mesh = pb.Mesh; Vector3[] srcPositions = mesh.vertices; Vector3[] srcNormals = mesh.normals; int vertexCount = System.Math.Min(ushort.MaxValue / 2, mesh.vertexCount); Vector3[] positions = new Vector3[vertexCount * 2]; Vector4[] tangents = new Vector4[vertexCount * 2]; int[] indexes = new int[vertexCount * 2]; for (int i = 0; i < vertexCount; i++) { int a = i * 2, b = i * 2 + 1; positions[a] = srcPositions[i]; positions[b] = srcPositions[i]; tangents[a] = new Vector4(srcNormals[i].x, srcNormals[i].y, srcNormals[i].z, 0f); tangents[b] = new Vector4(srcNormals[i].x, srcNormals[i].y, srcNormals[i].z, 1f); indexes[a] = a; indexes[b] = b; } m_normalsMesh.vertices = positions; m_normalsMesh.tangents = tangents; m_normalsMesh.subMeshCount = 1; m_normalsMesh.SetIndices(indexes, MeshTopology.Lines, 0); }
public void Add(ProBuilderMesh mesh, IEnumerable <Edge> edges) { HashSet <Edge> edgesHs; List <Edge> edgesList; MeshFilter edgesSelection; m_meshToSelection.TryGetValue(mesh, out edgesSelection); m_meshToEdgesList.TryGetValue(mesh, out edgesList); if (!m_meshToEdges.TryGetValue(mesh, out edgesHs)) { edgesSelection = CreateEdgesGameObject(mesh); edgesSelection.transform.SetParent(mesh.transform, false); edgesHs = new HashSet <Edge>(); edgesList = new List <Edge>(); m_meshToSelection.Add(mesh, edgesSelection); m_meshToEdges.Add(mesh, edgesHs); m_meshToEdgesList.Add(mesh, edgesList); m_meshes.Add(mesh); PBMesh pbMesh = mesh.GetComponent <PBMesh>(); if (pbMesh != null) { pbMesh.RaiseSelected(true); } m_pbMeshes.Add(pbMesh); } int vertexCount = mesh.vertexCount; Edge[] notSelectedEdges = edges.Where(edge => !edgesHs.Contains(edge) && vertexCount > edge.a && vertexCount > edge.b).ToArray(); SetEdgesColor(mesh, edgesSelection, m_selectedColor, notSelectedEdges); for (int i = 0; i < notSelectedEdges.Length; ++i) { edgesHs.Add(notSelectedEdges[i]); edgesList.Add(notSelectedEdges[i]); } if (notSelectedEdges.Length > 0) { m_lastPosition = GetPosition(mesh, notSelectedEdges.Last()); m_lastNormal = GetNormal(mesh, notSelectedEdges.Last()); for (int i = 0; i < notSelectedEdges.Length; i++) { m_selectedEdgesCount++; if (m_selectedEdgesCount == 1) { m_centerOfMass = mesh.transform.TransformPoint(GetPosition(mesh, notSelectedEdges[i])); } else { m_centerOfMass *= (m_selectedEdgesCount - 1) / (float)m_selectedEdgesCount; m_centerOfMass += mesh.transform.TransformPoint(GetPosition(mesh, notSelectedEdges[i])) / m_selectedEdgesCount; } } } }
public void Add(ProBuilderMesh mesh, IEnumerable <int> indices) { HashSet <int> indicesHs; List <int> indicesList; MeshFilter vertices; m_meshToSelection.TryGetValue(mesh, out vertices); m_meshToIndicesList.TryGetValue(mesh, out indicesList); if (!m_meshToIndices.TryGetValue(mesh, out indicesHs)) { vertices = CreateVerticesGameObject(mesh, null); //vertices.transform.SetParent(transform, false); vertices.transform.SetParent(mesh.transform, false); indicesHs = new HashSet <int>(); indicesList = new List <int>(); m_meshToSelection.Add(mesh, vertices); m_meshToIndices.Add(mesh, indicesHs); m_meshToIndicesList.Add(mesh, indicesList); m_meshes.Add(mesh); PBMesh pbMesh = mesh.GetComponent <PBMesh>(); if (pbMesh != null) { pbMesh.RaiseSelected(false); } m_pbMeshes.Add(pbMesh); } int[] notSelectedIndices = indices.Where(i => !indicesHs.Contains(i)).ToArray(); SetVerticesColor(mesh, vertices, m_selectedColor, notSelectedIndices); for (int i = 0; i < notSelectedIndices.Length; ++i) { indicesHs.Add(notSelectedIndices[i]); indicesList.Add(notSelectedIndices[i]); } if (notSelectedIndices.Length > 0) { Vertex[] notSelectedVertices = mesh.GetVertices(notSelectedIndices); m_lastPosition = notSelectedVertices.Last().position; m_lastNormal = notSelectedVertices.Last().normal; for (int i = 0; i < notSelectedIndices.Length; i++) { m_selectedVerticesCount++; if (m_selectedVerticesCount == 1) { m_centerOfMass = mesh.transform.TransformPoint(notSelectedVertices[i].position); } else { m_centerOfMass *= (m_selectedVerticesCount - 1) / (float)m_selectedVerticesCount; m_centerOfMass += mesh.transform.TransformPoint(notSelectedVertices[i].position) / m_selectedVerticesCount; } } } }
private void Awake() { m_target = GetComponent <PBMesh>(); if (!m_target) { m_target = gameObject.AddComponent <PBMesh>(); } m_targetMesh = m_target.GetComponent <ProBuilderMesh>(); }
public static void ApplySmoothingGroup(PBMesh pbMesh, MeshSelection selection, float angleThreshold) { selection = selection.ToFaces(false); ProBuilderMesh mesh = pbMesh.ProBuilderMesh; IList <Face> faces = new List <Face>(); mesh.GetFaces(selection.GetFaces(pbMesh).ToArray(), faces); Smoothing.ApplySmoothingGroups(mesh, faces, angleThreshold); }
public static PBMesh ProBuilderize(GameObject gameObject) { PBMesh mesh = gameObject.GetComponent <PBMesh>(); if (mesh != null) { return(mesh); } return(gameObject.AddComponent <PBMesh>()); }
public static PBMesh ProBuilderize(GameObject gameObject, bool hierarchy, Vector2 uvScale) { bool wasActive = false; if (uvScale != Vector2.one) { wasActive = gameObject.activeSelf; gameObject.SetActive(false); } if (hierarchy) { MeshFilter[] meshFilters = gameObject.GetComponentsInChildren <MeshFilter>(true); for (int i = 0; i < meshFilters.Length; ++i) { if (meshFilters[i].GetComponent <PBMesh>() == null) { PBMesh pbMesh = meshFilters[i].gameObject.AddComponent <PBMesh>(); Init(pbMesh, uvScale); } } if (uvScale != Vector2.one) { gameObject.SetActive(wasActive); } return(gameObject.GetComponent <PBMesh>()); } else { PBMesh mesh = gameObject.GetComponent <PBMesh>(); if (mesh != null) { if (uvScale != Vector2.one) { gameObject.SetActive(wasActive); } return(mesh); } mesh = gameObject.AddComponent <PBMesh>(); Init(mesh, uvScale); if (uvScale != Vector2.one) { gameObject.SetActive(wasActive); } return(mesh); } }
/// <summary> /// Provided two faces, this method will attempt to project @f2 and align its size, rotation, and position to match /// the shared edge on f1. /// </summary> public static void AutoStitch(PBMesh mesh, int f1, int f2, int channel) { if (m_methodInfo == null) { return; } ProBuilderMesh pbMesh = mesh.ProBuilderMesh; IList <Face> faces = new List <Face>(2); pbMesh.GetFaces(new List <int> { f1, f2 }, faces); m_methodInfo.Invoke(null, new object[] { pbMesh, faces[0], faces[1], channel }); }
public static void SetGroup(PBMesh pbMesh, MeshSelection selection, int index) { selection = selection.ToFaces(false); ProBuilderMesh mesh = pbMesh.ProBuilderMesh; IList <Face> faces = new List <Face>(); mesh.GetFaces(selection.GetFaces(pbMesh).ToArray(), faces); foreach (Face face in faces) { face.smoothingGroup = index; } mesh.ToMesh(); mesh.Refresh(); }
public void Clear() { for (int i = 0; i < m_pbMeshes.Count; ++i) { PBMesh pbMesh = m_pbMeshes[i]; if (pbMesh != null) { pbMesh.RaiseUnselected(); } } m_selectionVertices.Clear(); m_selectionFaces.Clear(); m_faceToSelectionFace.Clear(); m_selectionFaceToMesh.Clear(); m_meshToFaces.Clear(); m_pbMeshes.Clear(); RebuildSelectionMesh(); }
private void Awake() { foreach (Transform child in transform) { Destroy(child.gameObject); } m_target = GetComponent <PBMesh>(); if (!m_target) { m_target = gameObject.AddComponent <PBMesh>(); } m_targetMesh = m_target.GetComponent <ProBuilderMesh>(); if (IsEditing) { BeginEdit(); } }
public override void SetSelection(MeshSelection selection) { m_vertexSelection.Clear(); m_selection.Clear(); if (selection != null) { selection = selection.ToVertices(false); foreach (KeyValuePair <GameObject, IList <int> > kvp in selection.SelectedIndices) { PBMesh pbMesh = kvp.Key.GetComponent <PBMesh>(); if (pbMesh.IsMarkedAsDestroyed) { continue; } m_vertexSelection.Add(kvp.Key.GetComponent <ProBuilderMesh>(), kvp.Value); } } }
private void OnDestroy() { if (m_material != null) { Destroy(m_material); } for (int i = 0; i < m_pbMeshes.Count; ++i) { PBMesh pbMesh = m_pbMeshes[i]; if (pbMesh != null) { pbMesh.RaiseUnselected(); } } m_meshToSelection.Clear(); m_meshToIndices.Clear(); m_meshToIndicesList.Clear(); m_meshes.Clear(); m_pbMeshes.Clear(); }
public static void ClearGroup(PBMesh pbMesh, MeshSelection selection) { SetGroup(pbMesh, selection, smoothingGroupNone); }
public void Rebuild(PBMesh pb) { CacheGroups(pb.ProBuilderMesh); RebuildPreviewMesh(pb.ProBuilderMesh); RebuildNormalsMesh(pb); }
public void Remove(ProBuilderMesh mesh, IEnumerable <Edge> edges = null) { HashSet <Edge> edgesHs; if (m_meshToEdges.TryGetValue(mesh, out edgesHs)) { MeshFilter edgesSelection = m_meshToSelection[mesh]; List <Edge> edgesList = m_meshToEdgesList[mesh]; if (edges != null) { Edge[] selectedEdges = edges.Where(i => edgesHs.Contains(i)).ToArray(); SetEdgesColor(mesh, edgesSelection, m_color, selectedEdges); for (int i = 0; i < selectedEdges.Length; ++i) { edgesHs.Remove(selectedEdges[i]); edgesList.Remove(selectedEdges[i]); } UpdateCenterOfMassOnRemove(mesh, selectedEdges); } else { UpdateCenterOfMassOnRemove(mesh, edgesList); edgesHs.Clear(); edgesList.Clear(); } if (edgesHs.Count == 0) { m_meshToEdges.Remove(mesh); m_meshToEdgesList.Remove(mesh); m_meshToSelection.Remove(mesh); Destroy(edgesSelection.gameObject); int meshIndex = m_meshes.IndexOf(mesh); if (meshIndex != -1) { m_meshes.RemoveAt(meshIndex); PBMesh pbMesh = m_pbMeshes[meshIndex]; if (pbMesh != null) { pbMesh.RaiseUnselected(); } m_pbMeshes.RemoveAt(meshIndex); } } if (edgesList.Count > 0) { m_lastPosition = GetPosition(mesh, edgesList.Last()); m_lastNormal = GetNormal(mesh, edgesList.Last()); } else { if (LastMesh != null) { m_lastPosition = GetPosition(LastMesh, m_meshToEdgesList[LastMesh].Last()); m_lastNormal = GetNormal(LastMesh, m_meshToEdgesList[LastMesh].Last()); } else { m_lastPosition = Vector3.zero; m_lastPosition = Vector3.forward; } } } }
public IEnumerable <int> GetEdges(PBMesh mesh) { PBEdge[] edges = mesh.Edges; return(SelectedEdges[mesh.gameObject].Select(e => Array.IndexOf(edges, new PBEdge(e, -1)))); }
public void Remove(ProBuilderMesh mesh, int faceIndex) { Face selectionFace; Dictionary <int, Face> faceToSelection; if (m_faceToSelectionFace.TryGetValue(mesh, out faceToSelection)) { if (!faceToSelection.TryGetValue(faceIndex, out selectionFace)) { return; } } else { return; } Face face = mesh.faces[faceIndex]; faceToSelection.Remove(faceIndex); if (faceToSelection.Count == 0) { m_faceToSelectionFace.Remove(mesh); } m_selectionFaceToMesh.Remove(selectionFace); FaceList faceList = m_meshToFaces[mesh]; IList <int> indexes = face.indexes; for (int i = 0; i < indexes.Count; ++i) { int index = indexes[i]; if (faceList.Indexes.ContainsKey(index)) { faceList.Indexes[index]--; if (faceList.Indexes[index] == 0) { faceList.Indexes.Remove(index); } } } if (faceList.Faces.Count == 0) { m_meshToFaces.Remove(mesh); PBMesh pbMesh = mesh.GetComponent <PBMesh>(); if (pbMesh != null) { pbMesh.RaiseUnselected(); } m_pbMeshes.Remove(pbMesh); } int flidx = faceList.Faces.IndexOf(faceIndex); faceList.Faces.RemoveAt(flidx); faceList.FaceIndexes.RemoveAt(flidx); faceList.SelectionFaces.Remove(selectionFace); Vector3 removedFaceCenterOfMass = GetCenterOfMass(selectionFace); int[] indices = selectionFace.distinctIndexes.OrderByDescending(i => i).ToArray(); for (int i = 0; i < indices.Length; ++i) { m_selectionVertices.RemoveAt(indices[i]); } int selectionFaceIndex = m_selectionFaces.IndexOf(selectionFace); int count = selectionFace.indexes.Count; m_selectionFaces.RemoveAt(selectionFaceIndex); for (int i = selectionFaceIndex; i < m_selectionFaces.Count; ++i) { m_selectionFaces[i].ShiftIndexes(-count); } if (!m_isChanging) { RebuildSelectionMesh(); } if (m_selectionFaces.Count == 0) { m_centerOfMass = Vector3.zero; m_lastPosition = Vector3.zero; m_lastNormal = Vector3.forward; m_lastMesh = null; } else if (m_selectionFaces.Count == 1) { m_centerOfMass = GetCenterOfMass(m_selectionFaces[0]); m_lastPosition = m_centerOfMass; m_lastNormal = GetNormal(m_selectionFaces[0]); m_lastMesh = m_selectionFaceToMesh[m_selectionFaces[0]]; } else { m_centerOfMass -= removedFaceCenterOfMass / (m_selectionFaces.Count + 1); m_centerOfMass *= (m_selectionFaces.Count + 1) / (float)m_selectionFaces.Count; m_lastPosition = GetCenterOfMass(m_selectionFaces.Last()); m_lastNormal = GetNormal(m_selectionFaces.Last()); m_lastMesh = m_selectionFaceToMesh[m_selectionFaces.Last()]; } }
public void Add(ProBuilderMesh mesh, int faceIndex) { Face face = mesh.faces[faceIndex]; Dictionary <int, Face> faceToSelection; if (m_faceToSelectionFace.TryGetValue(mesh, out faceToSelection)) { if (faceToSelection.ContainsKey(faceIndex)) { return; } } else { faceToSelection = new Dictionary <int, Face>(); m_faceToSelectionFace.Add(mesh, faceToSelection); } int[] indices = new int[face.indexes.Count]; for (int i = 0; i < indices.Length; ++i) { indices[i] = m_selectionVertices.Count + i; } Face selectionFace = new Face(indices); faceToSelection.Add(faceIndex, selectionFace); m_selectionFaceToMesh.Add(selectionFace, mesh); IList <int> indexes = face.indexes; Vertex[] vertices = mesh.GetVertices(indexes); for (int i = 0; i < vertices.Length; ++i) { m_selectionVertices.Add(transform.InverseTransformPoint(mesh.transform.TransformPoint(vertices[i].position))); } m_selectionFaces.Add(selectionFace); if (!m_isChanging) { RebuildSelectionMesh(); } FaceList faceList; if (!m_meshToFaces.TryGetValue(mesh, out faceList)) { faceList = new FaceList(); m_meshToFaces.Add(mesh, faceList); PBMesh pbMesh = mesh.GetComponent <PBMesh>(); if (pbMesh != null) { pbMesh.RaiseSelected(false); } m_pbMeshes.Add(pbMesh); } for (int i = 0; i < indexes.Count; ++i) { int index = indexes[i]; if (!faceList.Indexes.ContainsKey(index)) { faceList.Indexes.Add(index, 1); } else { faceList.Indexes[index]++; } } faceList.Faces.Add(faceIndex); faceList.FaceIndexes.Add(face.indexes.ToArray()); faceList.SelectionFaces.Add(selectionFace); m_lastMesh = mesh; m_lastPosition = GetCenterOfMass(selectionFace); m_lastNormal = GetNormal(selectionFace); if (m_selectionFaces.Count == 1) { m_centerOfMass = m_lastPosition; } else { m_centerOfMass *= (m_selectionFaces.Count - 1) / (float)m_selectionFaces.Count; m_centerOfMass += m_lastPosition / m_selectionFaces.Count; } }
public IEnumerable <int> GetIndices(PBMesh mesh) { return(SelectedIndices[mesh.gameObject]); }
public void Remove(ProBuilderMesh mesh, IEnumerable <int> indices = null) { HashSet <int> indicesHs; if (m_meshToIndices.TryGetValue(mesh, out indicesHs)) { MeshFilter vertices = m_meshToSelection[mesh]; List <int> indicesList = m_meshToIndicesList[mesh]; if (indices != null) { int[] selectedIndices = indices.Where(i => indicesHs.Contains(i)).ToArray(); SetVerticesColor(mesh, vertices, m_color, selectedIndices); for (int i = 0; i < selectedIndices.Length; ++i) { indicesHs.Remove(selectedIndices[i]); indicesList.Remove(selectedIndices[i]); } Vertex[] selectedVertices = mesh.GetVertices(selectedIndices); UpdateCenterOfMassOnRemove(mesh.transform, selectedVertices); } else { Vertex[] selectedVertices = mesh.GetVertices(indicesList); UpdateCenterOfMassOnRemove(mesh.transform, selectedVertices); indicesHs.Clear(); indicesList.Clear(); } if (indicesHs.Count == 0) { m_meshToIndices.Remove(mesh); m_meshToIndicesList.Remove(mesh); m_meshToSelection.Remove(mesh); Destroy(vertices.gameObject); int index = m_meshes.IndexOf(mesh); if (index != -1) { m_meshes.RemoveAt(index); PBMesh pbMesh = m_pbMeshes[index]; if (pbMesh != null) { pbMesh.RaiseUnselected(); } m_pbMeshes.RemoveAt(index); } } if (indicesList.Count > 0) { Vertex[] lastVertex = mesh.GetVertices(new[] { indicesList.Last() }); m_lastPosition = lastVertex[0].position; m_lastNormal = lastVertex[0].normal; } else { if (LastMesh != null) { Vertex[] lastVertex = LastMesh.GetVertices(new[] { m_meshToIndicesList[LastMesh].Last() }); m_lastPosition = lastVertex[0].position; m_lastNormal = lastVertex[0].normal; } else { m_lastPosition = Vector3.zero; m_lastPosition = Vector3.forward; } } } }