コード例 #1
0
ファイル: BooleanEditor.cs プロジェクト: rpg-unamed-01/world
        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));
        }
コード例 #2
0
        void DoBooleanOperation(BoolOp operation)
        {
            CSG_Model result;

            /**
             * All boolean operations accept two gameobjects and return a new mesh.
             * Order matters - left, right vs. right, left will yield different
             * results in some cases.
             */
            switch (operation)
            {
            case BoolOp.Union:
                result = Boolean.Union(left, right);
                break;

            case BoolOp.SubtractLR:
                result = Boolean.Subtract(left, right);
                break;

            case BoolOp.SubtractRL:
                result = Boolean.Subtract(right, left);
                break;

            default:
                result = Boolean.Intersect(right, left);
                break;
            }

            composite = new GameObject();
            composite.AddComponent <MeshFilter>().sharedMesh        = result.mesh;
            composite.AddComponent <MeshRenderer>().sharedMaterials = result.materials.ToArray();

            GenerateBarycentric(composite);

            Destroy(left);
            Destroy(right);
        }