public void DuplicateFaces_ToSubmesh()
        {
            //Make first faces selected
            ProBuilderMesh mesh = selectables[0];

            Assume.That(mesh.faces, Is.Not.Null);
            Face        face          = selectables[0].faces[0];
            List <Face> selectedFaces = new List <Face>();

            selectedFaces.Add(face);
            mesh.SetSelectedFaces(selectedFaces);
            Assert.AreEqual(mesh.selectedFaceCount, 1);
            MeshSelection.SetSelection(mesh.gameObject);
            MeshSelection.OnObjectSelectionChanged();

            UnityEditor.ProBuilder.Actions.DuplicateFaces duplicateFaces = new UnityEditor.ProBuilder.Actions.DuplicateFaces();
            ProBuilderSettings.Set <UnityEditor.ProBuilder.Actions.DuplicateFaces.DuplicateFaceSetting>("DuplicateFaces.target", UnityEditor.ProBuilder.Actions.DuplicateFaces.DuplicateFaceSetting.Submesh);
            duplicateFaces.DoAction();

            //All selectable object should have all faces selected
            Assert.AreEqual(selectables[0].faces.Count, 7);

            Assert.AreEqual(MeshSelection.selectedObjectCount, 1);
            Assert.AreEqual(UnityEditor.Selection.objects[0], mesh.gameObject);
        }
Example #2
0
    public void CollapseVertices_SelectedSharedVertices_ActionEnabled()
    {
        // check that selecting two shared vertices will enable collapse vertices
        Assert.That(m_PBMesh, Is.Not.Null);

        var sharedVertices = m_PBMesh.sharedVerticesInternal;

        Assert.That(sharedVertices, Is.Not.Null);
        Assert.That(sharedVertices.Length, Is.GreaterThanOrEqualTo(2));

        var selectedVertices = sharedVertices[0].Union(sharedVertices[1]);

        Assert.That(selectedVertices.Count(), Is.GreaterThan(1));

        // Set the selected vertices to two different shared vertices (collapsable)
        m_PBMesh.SetSelectedVertices(selectedVertices);
        Assert.That(m_PBMesh.selectedIndexesInternal.Length, Is.EqualTo(selectedVertices.Count()));

        MeshSelection.SetSelection(m_PBMesh.gameObject);
        MeshSelection.OnObjectSelectionChanged();

        UnityEditor.ProBuilder.Actions.CollapseVertices collapseVertices = new UnityEditor.ProBuilder.Actions.CollapseVertices();

        Assert.That(collapseVertices.enabled, Is.True);
    }
    public void SelectMaterial_WithNullMaterial()
    {
        //Make first faces selected
        ProBuilderMesh mesh = selectables[0];

        MeshSelection.SetSelection(mesh.gameObject);
        mesh.AddToFaceSelection(3);
        MeshSelection.OnObjectSelectionChanged();

        UnityEditor.ProBuilder.Actions.SelectMaterial selectMaterial = new UnityEditor.ProBuilder.Actions.SelectMaterial();
        var oldValue = selectMaterial.m_RestrictToSelectedObjects.value;

        selectMaterial.m_RestrictToSelectedObjects.value = false;
        selectMaterial.PerformAction();
        selectMaterial.m_RestrictToSelectedObjects.value = oldValue;

        //We need to force the object selection change here to ensure that MeshSelection reflect the result
        //of the action, which typically would notify the MeshSelection asynchronously.
        MeshSelection.OnObjectSelectionChanged();

        Assert.That(MeshSelection.selectedObjectCount, Is.EqualTo(1));
        Assert.That(mesh.selectedFaceCount, Is.EqualTo(3));
        Assert.That(mesh.selectedFaceIndexes.IndexOf(3), !Is.EqualTo(-1));
        Assert.That(mesh.selectedFaceIndexes.IndexOf(4), !Is.EqualTo(-1));
        Assert.That(mesh.selectedFaceIndexes.IndexOf(5), !Is.EqualTo(-1));
        Assert.That(selectables[1].selectedFaceCount, Is.EqualTo(0));
    }
    public void Manual_PlanarProjection()
    {
        //Select faces

        List <Face> selectedFaces = new List <Face>();

        selectedFaces.Add(m_cube.faces[2]);
        selectedFaces.Add(m_cube.faces[4]);
        selectedFaces.Add(m_cube.faces[5]);
        MeshSelection.SetSelection(m_cube.gameObject);
        m_cube.SetSelectedFaces(selectedFaces);
        MeshSelection.OnObjectSelectionChanged();

        foreach (Face f in selectedFaces)
        {
            Assert.That(f.manualUV, Is.EqualTo(false));
        }

        //Select faces
        UVEditor.instance.Menu_SetManualUV();

        foreach (Face f in selectedFaces)
        {
            Assert.That(f.manualUV, Is.EqualTo(true));
        }

        //Modify those faces
        Vector2 minimalUV = UVEditor.instance.UVSelectionMinimalUV();

        Assert.That(minimalUV, !Is.EqualTo(UVEditor.LowerLeft));

        UVEditor.instance.Menu_PlanarProject();
        minimalUV = UVEditor.instance.UVSelectionMinimalUV();
        Assert.That(minimalUV, Is.EqualTo(UVEditor.LowerLeft));
    }
        public void DuplicateFaces_ToObject()
        {
            //Make first faces selected
            ProBuilderMesh mesh = selectables[0];

            Assume.That(mesh.faces, Is.Not.Null);
            Face        face          = selectables[0].faces[0];
            List <Face> selectedFaces = new List <Face>();

            selectedFaces.Add(face);
            mesh.SetSelectedFaces(selectedFaces);
            Assert.AreEqual(mesh.selectedFaceCount, 1);
            MeshSelection.SetSelection(mesh.gameObject);
            MeshSelection.OnObjectSelectionChanged();


            UnityEditor.ProBuilder.Actions.DuplicateFaces duplicateFaces = new UnityEditor.ProBuilder.Actions.DuplicateFaces();
            ProBuilderSettings.Set <UnityEditor.ProBuilder.Actions.DuplicateFaces.DuplicateFaceSetting>("DuplicateFaces.target", UnityEditor.ProBuilder.Actions.DuplicateFaces.DuplicateFaceSetting.GameObject);
            duplicateFaces.DoAction();

            //selectable object should keep all faces selected
            Assert.AreEqual(selectables[0].faces.Count, 6);

            Assert.AreEqual(MeshSelection.selectedObjectCount, 1);
            Assert.AreNotEqual(UnityEditor.Selection.objects[0], mesh.gameObject);

            //This needs to be called explicitly in the case of unit test so that the internal representation of ProBuilder MeshSelection
            //gets updated prior to accessing it
            MeshSelection.OnObjectSelectionChanged();
            ProBuilderMesh newMesh = MeshSelection.activeMesh;

            Assert.AreEqual(newMesh.faces.Count, 1);
        }
Example #6
0
    public void SelectFaces_WithoutColor()
    {
        Setup();

        //Make first faces selected
        ProBuilderMesh mesh = selectables[0];

        Assert.IsNotNull(mesh.faces);
        Face        face          = selectables[0].faces[0];
        List <Face> selectedFaces = new List <Face>();

        selectedFaces.Add(face);
        mesh.SetSelectedFaces(selectedFaces);
        Assert.AreEqual(mesh.selectedFaceCount, 1);
        MeshSelection.SetSelection(mesh.gameObject);
        MeshSelection.OnObjectSelectionChanged();

        foreach (var currObject in selectables)
        {
            //Validate that prior not all faces are selected
            Assert.AreNotEqual(currObject.selectedFacesInternal.Length, 6);
        }

        UnityEditor.ProBuilder.Actions.SelectVertexColor selectColorAction = new UnityEditor.ProBuilder.Actions.SelectVertexColor();
        selectColorAction.DoAction();

        foreach (var currObject in selectables)
        {
            //All selectable object should have all faces selected
            Assert.AreEqual(currObject.selectedFacesInternal.Length, 6);
        }
    }
Example #7
0
        /// <summary>
        /// Adds pb_Object component without duplicating the objcet. Is undo-able.
        /// </summary>
        /// <param name="selected"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static ActionResult DoProBuilderize(
            IEnumerable <MeshFilter> selected,
            MeshImportSettings settings)
        {
            int   i     = 0;
            float count = selected.Count();

            // Return immediately from the action so that the GUI can resolve. Displaying a progress bar interrupts the
            // event loop causing a layoutting error.
            EditorApplication.delayCall += () =>
            {
                foreach (var mf in selected)
                {
                    if (mf.sharedMesh == null)
                    {
                        continue;
                    }

                    GameObject go              = mf.gameObject;
                    Mesh       sourceMesh      = mf.sharedMesh;
                    Material[] sourceMaterials = go.GetComponent <MeshRenderer>()?.sharedMaterials;

                    try
                    {
                        var destination  = Undo.AddComponent <ProBuilderMesh>(go);
                        var meshImporter = new MeshImporter(sourceMesh, sourceMaterials, destination);
                        meshImporter.Import(settings);

                        destination.Rebuild();
                        destination.Optimize();

                        i++;
                    }
                    catch (System.Exception e)
                    {
                        Debug.LogWarning("Failed ProBuilderizing: " + go.name + "\n" + e.ToString());
                    }

                    UnityEditor.EditorUtility.DisplayProgressBar("ProBuilderizing", mf.gameObject.name, i / count);
                }

                UnityEditor.EditorUtility.ClearProgressBar();
                MeshSelection.OnObjectSelectionChanged();
                ProBuilderEditor.Refresh();
            };

            if (i < 1)
            {
                return(new ActionResult(ActionResult.Status.Canceled, "Nothing Selected"));
            }
            return(new ActionResult(ActionResult.Status.Success, "ProBuilderize " + i + (i > 1 ? " Objects" : " Object").ToString()));
        }
        public static void StripAllSelected()
        {
            if (!UnityEditor.EditorUtility.DisplayDialog("Strip ProBuilder Scripts", "This will remove all ProBuilder scripts on the selected objects.  You will no longer be able to edit these objects.  There is no undo, please exercise caution!\n\nAre you sure you want to do this?", "Okay", "Cancel"))
            {
                return;
            }

            foreach (Transform t in Selection.transforms)
            {
                foreach (ProBuilderMesh pb in t.GetComponentsInChildren <ProBuilderMesh>(true))
                {
                    DoStrip(pb);
                }
            }
            MeshSelection.OnObjectSelectionChanged();
        }
Example #9
0
    public void Setup()
    {
        // make sure the ProBuilder window is open
        if (ProBuilderEditor.instance == null)
        {
            ProBuilderEditor.MenuOpenWindow();
        }

        Assume.That(ProBuilderEditor.instance, Is.Not.Null);

        m_PBMesh = ShapeFactory.Instantiate(typeof(UnityEngine.ProBuilder.Shapes.Plane));
        MeshSelection.SetSelection(m_PBMesh.gameObject);
        MeshSelection.OnObjectSelectionChanged();

        m_PreviousSelectMode        = ProBuilderEditor.selectMode;
        ProBuilderEditor.selectMode = SelectMode.Object;
    }
    public void Setup()
    {
        // make sure the ProBuilder window is open
        if (ProBuilderEditor.instance == null)
        {
            ProBuilderEditor.MenuOpenWindow();
        }

        Assume.That(ProBuilderEditor.instance, Is.Not.Null);

        m_PBMesh = ShapeGenerator.CreateShape(ShapeType.Plane);
        MeshSelection.SetSelection(m_PBMesh.gameObject);
        MeshSelection.OnObjectSelectionChanged();

        m_PreviousSelectMode        = ProBuilderEditor.selectMode;
        ProBuilderEditor.selectMode = SelectMode.Object;
    }
Example #11
0
    public void SelectFaces_WithColor()
    {
        Setup(true /*with color*/);

        //Make first faces selected
        ProBuilderMesh mesh = selectables[0];

        Assert.IsNotNull(mesh.faces);
        Face        face          = selectables[0].faces[0];
        List <Face> selectedFaces = new List <Face>();

        selectedFaces.Add(face);
        mesh.SetSelectedFaces(selectedFaces);
        Assert.AreEqual(mesh.selectedFaceCount, 1);
        MeshSelection.SetSelection(mesh.gameObject);
        MeshSelection.OnObjectSelectionChanged();

        //Validate that prior only a face on first cube is selected
        Assert.AreEqual(selectables[0].selectedFacesInternal.Length, 1);
        Assert.AreEqual(selectables[1].selectedFacesInternal.Length, 0);

        UnityEditor.ProBuilder.Actions.SelectVertexColor selectColorAction = new UnityEditor.ProBuilder.Actions.SelectVertexColor();
        selectColorAction.DoAction();

        //Validate that after a face is selected on both cube
        Color[] colors0 = selectables[0].colorsInternal;
        Color[] colors1 = selectables[1].colorsInternal;

        Assert.AreEqual(selectables[0].selectedFacesInternal.Length, 1);
        Assert.AreEqual(selectables[1].selectedFacesInternal.Length, 1);

        int[] tris0 = selectables[0].selectedFacesInternal[0].distinctIndexesInternal;
        int[] tris1 = selectables[1].selectedFacesInternal[0].distinctIndexesInternal;
        Assert.AreEqual(tris0.Length, tris1.Length);

        //Validate that the face match
        for (int n = 0; n < tris0.Length; n++)
        {
            Assert.AreEqual(colors0[tris0[n]], colors1[tris1[n]]);
            Assert.AreEqual(colors0[tris0[n]], faceColors[0]);
        }
    }
        public static void Strip(ProBuilderMesh[] all)
        {
            for (int i = 0; i < all.Length; i++)
            {
                if (UnityEditor.EditorUtility.DisplayCancelableProgressBar(
                        "Stripping ProBuilder Scripts",
                        "Working over " + all[i].id + ".",
                        ((float)i / all.Length)))
                {
                    break;
                }

                DoStrip(all[i]);
            }

            UnityEditor.EditorUtility.ClearProgressBar();
            UnityEditor.EditorUtility.DisplayDialog("Strip ProBuilder Scripts", "Successfully stripped out all ProBuilder components.", "Okay");

            ProBuilderEditor.Refresh();
            MeshSelection.OnObjectSelectionChanged();
        }
Example #13
0
    public static void DetachFaceUndoTest()
    {
        var cube      = ShapeFactory.Instantiate <Cube>();
        var duplicate = UnityEngine.Object.Instantiate(cube.gameObject).GetComponent <ProBuilderMesh>();

        duplicate.MakeUnique();

        // Select the mesh
        MeshSelection.SetSelection(cube.gameObject);
        MeshSelection.OnObjectSelectionChanged();
        Assume.That(MeshSelection.selectedObjectCount, Is.EqualTo(1));

        // Select a face
        cube.SetSelectedFaces(new Face[] { cube.facesInternal[0] });
        Assume.That(cube.selectedFacesInternal.Length, Is.EqualTo(1));

        // Perform `Detach Faces` action
        var detachAction = new DetachFaces();
        var result       = detachAction.PerformAction();

        Assume.That(result.status, Is.EqualTo(ActionResult.Status.Success));

        UnityEditor.Undo.PerformUndo();

        // this is usually caught by UndoUtility
        cube.InvalidateCaches();

        cube.ToMesh();
        cube.Refresh();

        // After undo, previously edited mesh should match the duplicate
        TestUtility.AssertAreEqual(duplicate.mesh, cube.mesh);

        UnityEngine.Object.DestroyImmediate(cube.gameObject);
        UnityEngine.Object.DestroyImmediate(duplicate.gameObject);
    }
Example #14
0
    public void CollapseVertices_SelectSharedVertex_ActionDisabled()
    {
        Assert.That(m_PBMesh, Is.Not.Null);

        var sharedVertices = m_PBMesh.sharedVerticesInternal;

        Assert.That(sharedVertices, Is.Not.Null);
        Assert.That(sharedVertices.Length, Is.GreaterThanOrEqualTo(1));

        var sharedVertex = sharedVertices[0];

        Assert.That(sharedVertex.Count, Is.GreaterThan(1));

        // Set the selected vertices to all vertices belonging to a single shared vertex
        m_PBMesh.SetSelectedVertices(sharedVertex);
        Assert.That(m_PBMesh.selectedIndexesInternal.Length, Is.EqualTo(sharedVertex.Count));

        MeshSelection.SetSelection(m_PBMesh.gameObject);
        MeshSelection.OnObjectSelectionChanged();

        UnityEditor.ProBuilder.Actions.CollapseVertices collapseVertices = new UnityEditor.ProBuilder.Actions.CollapseVertices();

        Assert.That(collapseVertices.enabled, Is.False);
    }
        /// <summary>
        /// Adds pb_Object component without duplicating the objcet. Is undo-able.
        /// </summary>
        /// <param name="selected"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static ActionResult DoProBuilderize(
            IEnumerable <MeshFilter> selected,
            MeshImportSettings settings)
        {
            int   i     = 0;
            float count = selected.Count();

            foreach (var mf in selected)
            {
                if (mf.sharedMesh == null)
                {
                    continue;
                }

                GameObject go           = mf.gameObject;
                Mesh       originalMesh = mf.sharedMesh;

                try
                {
                    ProBuilderMesh pb = Undo.AddComponent <ProBuilderMesh>(go);

                    MeshImporter meshImporter = new MeshImporter(pb);
                    meshImporter.Import(go, settings);

                    // if this was previously a pb_Object, or similarly any other instance asset, destroy it.
                    // if it is backed by saved asset, leave the mesh asset alone but assign a new mesh to the
                    // renderer so that we don't modify the asset.
                    if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(originalMesh)))
                    {
                        Undo.DestroyObjectImmediate(originalMesh);
                    }
                    else
                    {
                        go.GetComponent <MeshFilter>().sharedMesh = new Mesh();
                    }

                    pb.ToMesh();
                    pb.Refresh();
                    pb.Optimize();

                    i++;
                }
                catch (System.Exception e)
                {
                    Debug.LogWarning("Failed ProBuilderizing: " + go.name + "\n" + e.ToString());
                }

                UnityEditor.EditorUtility.DisplayProgressBar("ProBuilderizing", mf.gameObject.name, i / count);
            }

            UnityEditor.EditorUtility.ClearProgressBar();
            MeshSelection.OnObjectSelectionChanged();
            ProBuilderEditor.Refresh();

            if (i < 1)
            {
                return(new ActionResult(ActionResult.Status.Canceled, "Nothing Selected"));
            }
            else
            {
                return(new ActionResult(ActionResult.Status.Success, "ProBuilderize " + i + (i > 1 ? " Objects" : " Object").ToString()));
            }
        }