Esempio n. 1
0
        void Start()
        {
            // Import from a GameObject. In this case we're loading and assigning to the same GameObject, but you may
            // load and apply to different Objects as well.

            // Create a new MeshImporter
            var importer = new MeshImporter(gameObject);

            importer.Import();

            // Since we're loading and setting from the same object, it is necessary to create a new mesh to avoid
            // overwriting the mesh that is being read from.
            var filter = GetComponent <MeshFilter>();

            filter.sharedMesh = new Mesh();

            //Retrieve the create PB Mesh
            var mesh = gameObject.GetComponent <ProBuilderMesh>();

            // Do something with the pb_Object. Here we're extruding every face on the object by .25.
            mesh.Extrude(mesh.faces, ExtrudeMethod.IndividualFaces, .25f);

            // Apply the imported geometry to the pb_Object
            mesh.ToMesh();

            // Rebuild UVs, Collisions, Tangents, etc.
            mesh.Refresh();
        }
Esempio n. 2
0
        private static void ImportMesh(MeshFilter filter, ProBuilderMesh mesh, Vector2 uvScale)
        {
            MeshImporter importer = new MeshImporter(mesh);
            Renderer     renderer = mesh.GetComponent <Renderer>();

            importer.Import(filter.sharedMesh, renderer.sharedMaterials, m_defaultImportSettings);

            Dictionary <int, List <Face> > submeshIndexToFace = new Dictionary <int, List <Face> >();
            int submeshCount = filter.sharedMesh.subMeshCount;

            for (int i = 0; i < submeshCount; ++i)
            {
                submeshIndexToFace.Add(i, new List <Face>());
            }

            IList <Face> faces = mesh.faces;

            if (uvScale != Vector2.one)
            {
                AutoUnwrapSettings uv = AutoUnwrapSettings.defaultAutoUnwrapSettings;
                uv.scale = uvScale;
                for (int i = 0; i < mesh.faceCount; ++i)
                {
                    Face face = faces[i];
                    face.uv = uv;
                    submeshIndexToFace[face.submeshIndex].Add(face);
                }
            }
            else
            {
                for (int i = 0; i < mesh.faceCount; ++i)
                {
                    Face face = faces[i];
                    submeshIndexToFace[face.submeshIndex].Add(face);
                }
            }

            filter.sharedMesh = new Mesh();
            mesh.ToMesh();
            mesh.Refresh();

            Material[] materials = renderer.sharedMaterials;
            for (int i = 0; i < submeshCount && i < materials.Length; ++i)
            {
                List <Face> submeshFaces = submeshIndexToFace[i];
                Material    material     = materials[i];

                if (material != null)
                {
                    mesh.SetMaterial(submeshFaces, material);
                }
            }

            mesh.ToMesh();
            mesh.Refresh();
        }
        private static void ImportMesh(MeshFilter filter, ProBuilderMesh mesh)
        {
            MeshImporter importer = new MeshImporter(mesh);
            Renderer     renderer = mesh.GetComponent <Renderer>();

            importer.Import(filter.sharedMesh, renderer.sharedMaterials);

            filter.sharedMesh = new Mesh();

            mesh.ToMesh();
            mesh.Refresh();
        }
Esempio n. 4
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()));
        }
Esempio n. 5
0
        static ActionResult MenuBooleanOperation(BooleanOperation operation, ProBuilderMesh lhs, ProBuilderMesh rhs)
        {
            if (lhs == null || rhs == null)
            {
                return(new ActionResult(ActionResult.Status.Failure, "Must Select 2 Objects"));
            }

            string op_string = operation == BooleanOperation.Union ? "Union" : (operation == BooleanOperation.Subtract ? "Subtract" : "Intersect");

            ProBuilderMesh[] sel = new ProBuilderMesh[] { lhs, rhs };

            UndoUtility.RecordSelection(sel, op_string);

            UnityEngine.ProBuilder.Csg.Model result;

            switch (operation)
            {
            case BooleanOperation.Union:
                result = Boolean.Union(lhs.gameObject, rhs.gameObject);
                break;

            case BooleanOperation.Subtract:
                result = Boolean.Subtract(lhs.gameObject, rhs.gameObject);
                break;

            default:
                result = Boolean.Intersect(lhs.gameObject, rhs.gameObject);
                break;
            }

            var            materials = result.materials.ToArray();
            ProBuilderMesh pb        = ProBuilderMesh.Create();

            pb.GetComponent <MeshFilter>().sharedMesh        = (Mesh)result;
            pb.GetComponent <MeshRenderer>().sharedMaterials = materials;
            MeshImporter importer = new MeshImporter(pb.gameObject);

            importer.Import(new MeshImportSettings()
            {
                quads = true, smoothing = true, smoothingAngle = 1f
            });
            pb.Rebuild();
            pb.CenterPivot(null);
            Selection.objects = new Object[] { pb.gameObject };

            return(new ActionResult(ActionResult.Status.Success, op_string));
        }
        private static void ImportMesh(MeshFilter filter, ProBuilderMesh mesh)
        {
            MeshImporter importer = new MeshImporter(mesh);
            Renderer     renderer = mesh.GetComponent <Renderer>();

            importer.Import(filter.sharedMesh, renderer.sharedMaterials);

            filter.sharedMesh = new Mesh();

            foreach (Face face in mesh.faces)
            {
                face.manualUV = false;
            }

            mesh.ToMesh();
            mesh.Refresh();
        }
        public static void CubeSurvivesRoundTrip()
        {
            var pb = ShapeGenerator.CreateShape(ShapeType.Cube);

            try
            {
                var dup      = new GameObject().AddComponent <ProBuilderMesh>();
                var importer = new MeshImporter(dup);
                importer.Import(pb.gameObject);
                dup.ToMesh();
                dup.Refresh();
                TestUtility.AssertAreEqual(pb.mesh, dup.mesh, message: pb.name);
            }
            catch
            {
                UnityEngine.Object.DestroyImmediate(pb.gameObject);
            }
        }
    public static void ImportCube_MatchesDefaultCube()
    {
        var pb = ShapeGenerator.CreateShape(ShapeType.Cube);

        try
        {
#pragma warning disable 612
            var dup      = new GameObject().AddComponent <ProBuilderMesh>();
            var importer = new MeshImporter(dup);
            importer.Import(pb.gameObject);
#pragma warning restore 612
            dup.ToMesh();
            dup.Refresh();
            TestUtility.AssertAreEqual(pb.mesh, dup.mesh, message: pb.name);
        }
        catch
        {
            UnityEngine.Object.DestroyImmediate(pb.gameObject);
        }
    }
        public static void QuadsImportWithCorrectWinding()
        {
            var srcPath = TestUtility.TemporarySavedAssetsDirectory + "maya-cube-quads.fbx";

            // do this song and dance because AssetDatabase.LoadAssetAtPath doesn't seem to work with models in the
            // Package directories
            File.Copy(TestUtility.TemplatesDirectory + "MeshImporter/maya-cube-quads.fbx", srcPath);
            AssetDatabase.Refresh();
            var source       = AssetDatabase.LoadMainAssetAtPath(srcPath);
            var meshImporter = (ModelImporter)AssetImporter.GetAtPath(srcPath);

            meshImporter.globalScale = 100f;
            meshImporter.isReadable  = true;
            meshImporter.SaveAndReimport();

            Assert.IsNotNull(source);

            var instance = (GameObject)UObject.Instantiate(source);
            var result   = new GameObject().AddComponent <ProBuilderMesh>();
            var importer = new MeshImporter(result);

            Assert.IsTrue(importer.Import(instance, new MeshImportSettings()
            {
                quads          = true,
                smoothing      = false,
                smoothingAngle = 1f
            }), "Failed importing mesh");

            result.Rebuild();

            // Assert.IsNotNull doesn't work with  UnityObject magic
            Assert.IsFalse(result.mesh == null);

#if PB_CREATE_TEST_MESH_TEMPLATES
            TestUtility.SaveAssetTemplate(result.mesh, "imported-cube-triangles");
#endif

            TestUtility.AssertMeshesAreEqual(TestUtility.GetAssetTemplate <Mesh>("imported-cube-triangles"), result.mesh);

            UObject.DestroyImmediate(result);

            UObject.DestroyImmediate(instance);
            meshImporter.keepQuads = true;
            meshImporter.SaveAndReimport();
            instance = (GameObject)UObject.Instantiate(source);

            var quadMesh = instance.GetComponent <MeshFilter>().sharedMesh;
            Assert.AreEqual(MeshTopology.Quads, quadMesh.GetTopology(0));

            result   = new GameObject().AddComponent <ProBuilderMesh>();
            importer = new MeshImporter(result);

            Assert.IsTrue(importer.Import(instance, new MeshImportSettings()
            {
                quads          = true,
                smoothing      = false,
                smoothingAngle = 1f
            }), "Failed importing mesh");

            result.Rebuild();

#if PB_CREATE_TEST_MESH_TEMPLATES
            TestUtility.SaveAssetTemplate(result.mesh, "imported-cube-quads");
#endif

            TestUtility.AssertMeshesAreEqual(TestUtility.GetAssetTemplate <Mesh>("imported-cube-quads"), result.mesh);
            UObject.DestroyImmediate(result);
            AssetDatabase.DeleteAsset(TestUtility.TemporarySavedAssetsDirectory + "maya-cube-quads.fbx");
        }
Esempio n. 10
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();

            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()));
            }
        }