Esempio n. 1
0
        public static void ReimportModel(LXFMLDoc lxfml, Model model, DictionaryIntToModelGroupImportSettings importSettings)
        {
            var brickBuilding = SceneBrickBuilder.GetToggleBrickBuildingStatus();

            if (brickBuilding)
            {
                SceneBrickBuilder.ToggleBrickBuilding();
            }

            // FIXME Next version could include option to match groups up manually.

            var groups = model.GetComponentsInChildren <ModelGroup>();

            for (var i = groups.Length - 1; i >= 0; i--)
            {
                var group = groups[i];
                if (group.autoGenerated)
                {
                    Undo.DestroyObjectImmediate(group.gameObject);
                }
                else if (group.number >= lxfml.groups.Length)
                {
                    Debug.LogWarning("Group " + group.number + " " + group.groupName + " does not match up with files. Wiping.");
                    Undo.DestroyObjectImmediate(group.gameObject);
                }
            }

            groups = model.GetComponentsInChildren <ModelGroup>();

            var removedGroups = new List <LXFMLDoc.BrickGroup>();

            for (var i = 0; i < lxfml.groups.Length; i++)
            {
                LXFMLDoc.BrickGroup group = lxfml.groups[i];
                bool exists = false;
                for (var j = 0; j < groups.Length; j++)
                {
                    if (groups[j].number == group.number)
                    {
                        exists = true;
                        break;
                    }
                }

                if (!exists)
                {
                    removedGroups.Add(group);
                }
            }

            // Assign the new model import settings to the model.
            model.importSettings = new DictionaryIntToModelGroupImportSettings(importSettings);
            var bricksWithConnectivity = new HashSet <Brick>();

            if (removedGroups.Count > 0)
            {
                var resultBricks = new Dictionary <int, Brick>(lxfml.bricks.Count);
                foreach (var group in removedGroups)
                {
                    var number = group.number;
                    InstantiateModelBricks(lxfml, model.importSettings, ref resultBricks, number);
                    var groupGO = InstantiateModelGroup(group, number, model.gameObject, model.absoluteFilePath, model.relativeFilePath, ref resultBricks, false, importSettings[number]);
                    groupGO.transform.position = model.transform.position;
                    Undo.RegisterCreatedObjectUndo(groupGO, "Re-creating model group");
                    if (importSettings[number].connectivity)
                    {
                        bricksWithConnectivity.UnionWith(groupGO.GetComponentsInChildren <Brick>());
                    }
                }
            }

            foreach (var group in groups)
            {
                group.absoluteFilePath = model.absoluteFilePath;
                group.relativeFilePath = model.relativeFilePath;
                ReimportModelGroup(lxfml, group, importSettings[group.number]);
                if (group.importSettings.connectivity)
                {
                    bricksWithConnectivity.UnionWith(group.GetComponentsInChildren <Brick>());
                }
            }

            if (bricksWithConnectivity.Count > 0)
            {
                var sceneBricks = new HashSet <Brick>(StageUtility.GetCurrentStageHandle().FindComponentsOfType <Brick>());
                DetectConnectivity(bricksWithConnectivity, sceneBricks);
            }

            if (brickBuilding)
            {
                SceneBrickBuilder.ToggleBrickBuilding();
            }

            if (SceneBrickBuilder.GetAutoUpdateHierarchy())
            {
                groups = model.GetComponentsInChildren <ModelGroup>();
                var bricks = new HashSet <Brick>();
                foreach (var group in groups)
                {
                    if (group.importSettings.connectivity)
                    {
                        var groupBricks = group.GetComponentsInChildren <Brick>();
                        foreach (var brick in groupBricks)
                        {
                            bricks.Add(brick);
                        }
                    }
                }

                // On reimport, the model will be positioned weirdly compared to the rest of the scene, so we just ignore all other bricks
                ModelGroupUtility.RecomputeModelGroups(bricks);
            }

            EditorUtility.ClearProgressBar();
        }
Esempio n. 2
0
        public static void ReimportModel(LXFMLDoc lxfml, Model model, Model.Pivot previousPivot, DictionaryIntToModelGroupImportSettings importSettings)
        {
            var brickBuilding = SceneBrickBuilder.GetToggleBrickBuildingStatus();

            if (brickBuilding)
            {
                SceneBrickBuilder.ToggleBrickBuilding();
            }

            // FIXME Next version could include option to match groups up manually.

            var oldPosition = model.transform.position;
            var oldRotation = model.transform.rotation;

            model.transform.position = Vector3.zero;
            model.transform.rotation = Quaternion.identity;

            var groups = model.GetComponentsInChildren <ModelGroup>();

            for (var i = groups.Length - 1; i >= 0; i--)
            {
                var group = groups[i];
                if (group.autoGenerated)
                {
                    Undo.DestroyObjectImmediate(group.gameObject);
                }
                else if (group.number >= lxfml.groups.Length)
                {
                    Debug.LogWarning("Group " + group.number + " " + group.groupName + " does not match up with files. Wiping.");
                    Undo.DestroyObjectImmediate(group.gameObject);
                }
            }

            groups = model.GetComponentsInChildren <ModelGroup>();

            var removedGroups = new List <LXFMLDoc.BrickGroup>();

            for (var i = 0; i < lxfml.groups.Length; i++)
            {
                LXFMLDoc.BrickGroup group = lxfml.groups[i];
                bool exists = false;
                for (var j = 0; j < groups.Length; j++)
                {
                    if (groups[j].number == group.number)
                    {
                        exists = true;
                        break;
                    }
                }

                if (!exists)
                {
                    removedGroups.Add(group);
                }
            }

            // Assign the new model import settings to the model.
            model.importSettings = new DictionaryIntToModelGroupImportSettings(importSettings);
            var bricksWithConnectivity = new HashSet <Brick>();

            if (removedGroups.Count > 0)
            {
                var resultBricks = new Dictionary <int, Brick>(lxfml.bricks.Count);
                foreach (var group in removedGroups)
                {
                    var number = group.number;
                    InstantiateModelBricks(lxfml, model.importSettings, ref resultBricks, number);
                    var groupGO = InstantiateModelGroup(group, number, model.gameObject, model.absoluteFilePath, model.relativeFilePath, ref resultBricks, false, importSettings[number]);
                    groupGO.transform.position = model.transform.position;
                    Undo.RegisterCreatedObjectUndo(groupGO, "Re-creating model group");
                    if (importSettings[number].connectivity)
                    {
                        bricksWithConnectivity.UnionWith(groupGO.GetComponentsInChildren <Brick>());
                    }
                }
            }

            foreach (var group in groups)
            {
                group.absoluteFilePath = model.absoluteFilePath;
                group.relativeFilePath = model.relativeFilePath;
                ReimportModelGroup(lxfml, group, importSettings[group.number]);
                if (group.importSettings.connectivity)
                {
                    bricksWithConnectivity.UnionWith(group.GetComponentsInChildren <Brick>());
                }
            }

            if (bricksWithConnectivity.Count > 0)
            {
                var sceneBricks = new HashSet <Brick>(StageUtility.GetCurrentStageHandle().FindComponentsOfType <Brick>());
                DetectConnectivity(bricksWithConnectivity, sceneBricks);
            }

            if (SceneBrickBuilder.GetAutoUpdateHierarchy())
            {
                groups = model.GetComponentsInChildren <ModelGroup>();
                var bricks = new HashSet <Brick>();
                foreach (var group in groups)
                {
                    if (group.importSettings.connectivity)
                    {
                        var groupBricks = group.GetComponentsInChildren <Brick>();
                        foreach (var brick in groupBricks)
                        {
                            bricks.Add(brick);
                        }
                    }
                }
                ModelGroupUtility.RecomputeHierarchy(bricks, false, ModelGroupUtility.UndoBehavior.withUndo);

                var oldToNew = model.transform.position;
                model.transform.rotation = oldRotation;
                model.transform.position = oldPosition;
                oldToNew = model.transform.TransformVector(oldToNew);

                if ((previousPivot == Model.Pivot.Center || previousPivot == Model.Pivot.BottomCenter) && model.pivot == Model.Pivot.Original)
                {
                    ModelGroupUtility.RecomputePivot(model.transform, previousPivot, true, ModelGroupUtility.UndoBehavior.withoutUndo);
                    var oldPivotPos = model.transform.position;
                    model.transform.position = oldPosition;
                    var diff = model.transform.position - oldPivotPos;
                    model.transform.position += diff;
                    foreach (var brick in model.GetComponentsInChildren <Brick>())
                    {
                        brick.transform.position -= diff;
                    }
                    model.transform.rotation = oldRotation;
                }

                if (model.pivot != Model.Pivot.Original && previousPivot == Model.Pivot.Original)
                {
                    model.transform.position += oldToNew;
                    model.transform.rotation  = oldRotation;
                }

                if (model.pivot != Model.Pivot.Original && previousPivot != Model.Pivot.Original)
                {
                    if (model.pivot != previousPivot)
                    {
                        ModelGroupUtility.RecomputePivot(model.transform, previousPivot, true, ModelGroupUtility.UndoBehavior.withoutUndo);
                        model.transform.position = oldPosition;
                        ModelGroupUtility.RecomputePivot(model, false, ModelGroupUtility.UndoBehavior.withoutUndo);
                        model.transform.rotation = oldRotation;
                    }
                }
            }

            if (brickBuilding)
            {
                SceneBrickBuilder.ToggleBrickBuilding();
            }

            EditorUtility.ClearProgressBar();
        }