static List <GameObject> __rootGameObjects = new List <GameObject>(); // static to avoid allocations
        internal static ChiselModel CreateDefaultModel(CSGSceneHierarchy sceneHierarchy)
        {
            var currentScene = sceneHierarchy.Scene;

            currentScene.GetRootGameObjects(__rootGameObjects);
            for (int i = 0; i < __rootGameObjects.Count; i++)
            {
                if (!IsDefaultModel(__rootGameObjects[i]))
                {
                    continue;
                }

                var gameObject = __rootGameObjects[i];
                var model      = gameObject.GetComponent <ChiselModel>();
                if (model)
                {
                    return(model);
                }

                var transform = gameObject.GetComponent <Transform>();
                transform.localPosition = Vector3.zero;
                transform.localRotation = Quaternion.identity;
                transform.localScale    = Vector3.one;

                model = gameObject.AddComponent <ChiselModel>();
                UpdateModelFlags(model);
                return(model);
            }


            var oldActiveScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();

            if (currentScene != oldActiveScene)
            {
                UnityEngine.SceneManagement.SceneManager.SetActiveScene(currentScene);
            }

            try
            {
                var model = ChiselComponentFactory.Create <ChiselModel>(GeneratedDefaultModelName);
                UpdateModelFlags(model);
                return(model);
            }
            finally
            {
                if (currentScene != oldActiveScene)
                {
                    UnityEngine.SceneManagement.SceneManager.SetActiveScene(oldActiveScene);
                }
            }
        }
        static List <GameObject> __rootGameObjects = new List <GameObject>(); // static to avoid allocations
        internal static ChiselModel CreateDefaultModel(ChiselSceneHierarchy sceneHierarchy)
        {
            var currentScene = sceneHierarchy.Scene;

            currentScene.GetRootGameObjects(__rootGameObjects);
            for (int i = 0; i < __rootGameObjects.Count; i++)
            {
                if (!IsDefaultModel(__rootGameObjects[i]))
                {
                    continue;
                }

                var gameObject = __rootGameObjects[i];
                var model      = gameObject.GetComponent <ChiselModel>();
                if (model)
                {
                    return(model);
                }

                var transform = gameObject.GetComponent <Transform>();
                ChiselObjectUtility.ResetTransform(transform);

                model = gameObject.AddComponent <ChiselModel>();
                UpdateModelFlags(model);
                return(model);
            }


            var oldActiveScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();

            if (currentScene != oldActiveScene)
            {
                UnityEngine.SceneManagement.SceneManager.SetActiveScene(currentScene);
            }

            try
            {
                var model = ChiselComponentFactory.Create <ChiselModel>(kGeneratedDefaultModelName);
                model.IsDefaultModel = true;
                UpdateModelFlags(model);
                return(model);
            }
            finally
            {
                if (currentScene != oldActiveScene)
                {
                    UnityEngine.SceneManagement.SceneManager.SetActiveScene(oldActiveScene);
                }
            }
        }
        public override bool ConvertToBrushes()
        {
            var topGameObject = this.gameObject;

            UnityEditor.Undo.DestroyObjectImmediate(this);
            topGameObject.SetActive(false);
            bool success = false;

            try
            {
                if (brushContainerAsset.SubMeshCount == 1)
                {
                    var brush = UnityEditor.Undo.AddComponent <ChiselBrush>(topGameObject);
                    brush.Operation           = this.operation;
                    brush.BrushContainerAsset = brushContainerAsset;
                    brush.LocalTransformation = localTransformation;
                    brush.PivotOffset         = pivotOffset;
                    UnityEditor.Undo.SetCurrentGroupName("Converted Shape to Brush");
                }
                else
                {
                    var operationComponent = UnityEditor.Undo.AddComponent <ChiselOperation>(topGameObject);
                    operationComponent.Operation = this.operation;
                    var parentTransform = topGameObject.transform;
                    for (int i = 0; i < brushContainerAsset.SubMeshCount; i++)
                    {
                        var newBrushContainerAsset = UnityEngine.ScriptableObject.CreateInstance <ChiselBrushContainerAsset>();
                        newBrushContainerAsset.SetSubMeshes(new[] { new BrushMesh(brushContainerAsset.BrushMeshes[i]) });
                        var brush = ChiselComponentFactory.Create <ChiselBrush>("Brush (" + (i + 1) + ")", parentTransform, Vector3.zero, Quaternion.identity, Vector3.one);
                        brush.BrushContainerAsset = newBrushContainerAsset;
                        brush.LocalTransformation = localTransformation;
                        brush.PivotOffset         = pivotOffset;
                        brush.Operation           = brushContainerAsset.Operations[i];
                        UnityEditor.Undo.RecordObject(brush, "Modified brush");
                    }
                    UnityEditor.Undo.SetCurrentGroupName("Converted " + NodeTypeName + " to Multiple Brushes");
                }
                success = true;
            }
            finally
            {
                topGameObject.SetActive(true);
            }
            return(success);
        }
        // TODO: fix this so we keep the active scene in mind, as in, when the active scene changes,
        // we should be using a model in the active scene, not in another scene.
        public static ChiselModel GetActiveModelOrCreate(ChiselModel overrideModel = null)
        {
            if (overrideModel)
            {
                ChiselModelManager.ActiveModel = overrideModel;
                return(overrideModel);
            }

            var activeModel = ChiselModelManager.ActiveModel;

            if (!activeModel)
            {
                // TODO: ensure we create this in the active scene
                // TODO: handle scene being locked by version control
                activeModel = ChiselComponentFactory.Create <ChiselModel>("Model");
                ChiselModelManager.ActiveModel = activeModel;
            }
            return(activeModel);
        }
        public override bool ConvertToBrushes()
        {
            if (brushContainerAsset == null ||
                brushContainerAsset.BrushMeshes == null ||
                brushContainerAsset.BrushMeshes.Length == 0)
            {
                return(false);
            }

            string groupName;
            var    topGameObject       = this.gameObject;
            var    sourceBrushMeshes   = this.brushContainerAsset.BrushMeshes;
            var    sourceOperations    = this.brushContainerAsset.Operations;
            var    topOperation        = this.operation;
            var    pivotOffset         = this.pivotOffset;
            var    localTransformation = this.localTransformation;
            var    nodeTypeName        = this.NodeTypeName;

            UnityEditor.Undo.DestroyObjectImmediate(this);
            topGameObject.SetActive(false);
            try
            {
                if (sourceBrushMeshes.Length == 1)
                {
                    groupName = "Converted Shape to Brush";
                    var brush = ChiselComponentFactory.AddComponent <ChiselBrush>(topGameObject);
                    brush.LocalTransformation = localTransformation;
                    brush.PivotOffset         = pivotOffset;
                    brush.Operation           = topOperation;
                    brush.definition          = new BrushDefinition
                    {
                        brushOutline = new BrushMesh(sourceBrushMeshes[0])
                    };
                    // TODO: create surfacedefinition
                }
                else
                {
                    groupName = "Converted " + NodeTypeName + " to Multiple Brushes";
                    var operationComponent = ChiselComponentFactory.AddComponent <ChiselOperation>(topGameObject);
                    operationComponent.Operation = topOperation;
                    var parentTransform = topGameObject.transform;
                    for (int i = 0; i < sourceBrushMeshes.Length; i++)
                    {
                        var brush = ChiselComponentFactory.Create <ChiselBrush>("Brush (" + (i + 1) + ")", parentTransform);
                        brush.LocalTransformation = localTransformation;
                        brush.PivotOffset         = pivotOffset;
                        brush.Operation           = sourceOperations[i];
                        brush.definition          = new BrushDefinition
                        {
                            brushOutline = new BrushMesh(sourceBrushMeshes[i])
                        };
                        // TODO: create surfacedefinition
                    }
                }
            }
            finally
            {
                topGameObject.SetActive(true);
            }
            UnityEditor.Undo.SetCurrentGroupName(groupName);
            return(true);
        }
Пример #6
0
 public static ChiselModel CreateNewModel(Transform parent = null)
 {
     return(ChiselComponentFactory.Create <ChiselModel>(kDefaultModelName, parent));
 }