Example #1
0
        public Plane GetBoxFacePlane(BoxFace boxFace)
        {
            Plane   modelSpacePlane        = _modelSpaceBox.GetBoxFacePlane(boxFace);
            Vector3 modelSpacePointOnPlane = _modelSpaceBox.GetBoxFaceCenter(boxFace);

            return(modelSpacePlane.Transform(TransformMatrix.ToMatrix4x4x, modelSpacePointOnPlane));
        }
        public static Vector3 MeshPivotToWorldPoint(GameObject meshObject, MeshPivot meshPivot)
        {
            Box meshWorldOOBB = meshObject.GetMeshWorldBox();

            if (meshPivot == MeshPivot.Center)
            {
                return(meshWorldOOBB.Center);
            }
            else if (meshPivot == MeshPivot.BackCenter)
            {
                return(meshWorldOOBB.GetBoxFaceCenter(BoxFace.Back));
            }
            else if (meshPivot == MeshPivot.FrontCenter)
            {
                return(meshWorldOOBB.GetBoxFaceCenter(BoxFace.Front));
            }
            else if (meshPivot == MeshPivot.BottomCenter)
            {
                return(meshWorldOOBB.GetBoxFaceCenter(BoxFace.Bottom));
            }
            else if (meshPivot == MeshPivot.TopCenter)
            {
                return(meshWorldOOBB.GetBoxFaceCenter(BoxFace.Top));
            }
            else if (meshPivot == MeshPivot.LeftCenter)
            {
                return(meshWorldOOBB.GetBoxFaceCenter(BoxFace.Left));
            }
            else
            {
                return(meshWorldOOBB.GetBoxFaceCenter(BoxFace.Right));
            }
        }
        private static List <Box> BuildModelVertOverlapBoxes(Octave3DMesh octaveMesh)
        {
            const float overlapAmount     = 0.2f;
            float       halfOverlapAmount = overlapAmount * 0.5f;
            Box         modelMeshBox      = octaveMesh.ModelAABB;
            Vector3     meshBoxSize       = modelMeshBox.Size;

            BoxFace[] meshBoxFaces = Object2ObjectBoxSnapData.GetBoxFaceToSnapBoxIDMap();

            // Must have 1 to 1 mapping with Object2ObjectBoxSnapData.SnapBox enum
            const float sizeEps = 0.001f;

            Vector3[] overlapBoxSizes = new Vector3[]
            {
                // Left and right
                new Vector3(overlapAmount, meshBoxSize.y + sizeEps, meshBoxSize.z + sizeEps),
                new Vector3(overlapAmount, meshBoxSize.y + sizeEps, meshBoxSize.z + sizeEps),

                // Bottom and top
                new Vector3(meshBoxSize.x + sizeEps, overlapAmount, meshBoxSize.z + sizeEps),
                new Vector3(meshBoxSize.x + sizeEps, overlapAmount, meshBoxSize.z + sizeEps),

                // Back and front
                new Vector3(meshBoxSize.x + sizeEps, meshBoxSize.y + sizeEps, overlapAmount),
                new Vector3(meshBoxSize.x + sizeEps, meshBoxSize.y + sizeEps, overlapAmount),
            };

            var overlapBoxes = new List <Box>();

            for (int boxFaceIndex = 0; boxFaceIndex < meshBoxFaces.Length; ++boxFaceIndex)
            {
                BoxFace meshBoxFace   = meshBoxFaces[boxFaceIndex];
                Vector3 faceCenter    = modelMeshBox.GetBoxFaceCenter(meshBoxFace);
                Vector3 faceNormal    = modelMeshBox.GetBoxFacePlane(meshBoxFace).normal;
                Vector3 overlapCenter = faceCenter - faceNormal * halfOverlapAmount;
                overlapBoxes.Add(new Box(overlapCenter, overlapBoxSizes[boxFaceIndex]));
            }

            return(overlapBoxes);
        }
Example #4
0
        public static Prefab CreateFromSelectedObjects(Pivot prefabPivot)
        {
            // Ensure that all necessary data is in place
            ObjectSelectionPrefabCreationSettings prefabCreationSettings = ObjectSelectionPrefabCreationSettings.Get();

            if (string.IsNullOrEmpty(prefabCreationSettings.PrefabName) || string.IsNullOrEmpty(prefabCreationSettings.DestinationFolder))
            {
                return(null);
            }
            if (ObjectSelection.Get().NumberOfSelectedObjects == 0)
            {
                return(null);
            }

            List <GameObject> allSelectedObjects = ObjectSelection.Get().GetAllSelectedGameObjects();

            if (allSelectedObjects.Count == 0)
            {
                return(null);
            }

            // Check if a prefab with the same name already exists
            bool       shouldPrefabBeCreated = true;
            GameObject prefabWithSameName    = ProjectAssetDatabase.LoadPrefabWithNameInFolder(prefabCreationSettings.PrefabName, prefabCreationSettings.DestinationFolder, false);

            if (prefabWithSameName != null)
            {
                if (EditorUtility.DisplayDialog("Are you sure?", "A prefab with the specified name already exists in the specified folder. Would you like to overwrite it?", "Yes", "No"))
                {
                    // If the user chose 'Yes', we have to remove the existing prefab from its category
                    PrefabCategory categoryWhichContainsSamePrefab = PrefabCategoryDatabase.Get().GetPrefabCategoryWhichContainsPrefab(prefabWithSameName);
                    if (categoryWhichContainsSamePrefab != null)
                    {
                        categoryWhichContainsSamePrefab.RemoveAndDestroyPrefab(prefabWithSameName);
                    }
                }
                else
                {
                    shouldPrefabBeCreated = false;
                }
            }
            if (!shouldPrefabBeCreated)
            {
                return(null);
            }

            // Create all the objects which will reside in the prefab hierarchy
            GameObject prefabRoot = new GameObject(prefabCreationSettings.PrefabName);
            Box        objectCollectionWorldBox    = Box.GetInvalid();
            var        allObjectsInPrefabHierarchy = new List <GameObject>();

            foreach (GameObject gameObject in allSelectedObjects)
            {
                Transform  gameObjectTransform = gameObject.transform;
                GameObject gameObjectClone     = Octave3DWorldBuilder.Instantiate(gameObject, gameObjectTransform.position, gameObjectTransform.rotation) as GameObject;
                allObjectsInPrefabHierarchy.Add(gameObjectClone);

                Transform cloneTransform = gameObjectClone.transform;
                gameObjectClone.name      = gameObject.name;
                cloneTransform.localScale = gameObjectTransform.lossyScale;

                if (objectCollectionWorldBox.IsValid())
                {
                    objectCollectionWorldBox.Encapsulate(gameObjectClone.GetWorldBox());
                }
                else
                {
                    objectCollectionWorldBox = gameObjectClone.GetWorldBox();
                }
            }

            // Now calculate the root object's position based on the specified pivot point
            Transform prefabRootTransform = prefabRoot.transform;

            if (prefabPivot == Pivot.Center)
            {
                prefabRootTransform.position = objectCollectionWorldBox.Center;
            }
            else if (prefabPivot == Pivot.BottomCenter)
            {
                prefabRootTransform.position = objectCollectionWorldBox.GetBoxFaceCenter(BoxFace.Bottom);
            }

            // Now that the root object's position is in place, attach all objects as children of the root
            foreach (GameObject gameObject in allObjectsInPrefabHierarchy)
            {
                gameObject.transform.parent = prefabRootTransform;
            }

            // Create the prefab and assign it to the chosen category
            GameObject createdUnityPrefab = ProjectAssetDatabase.CreatePrefab(prefabRoot, prefabCreationSettings.PrefabName, prefabCreationSettings.DestinationFolder);

            if (createdUnityPrefab == null)
            {
                return(null);
            }

            UndoEx.RecordForToolAction(prefabCreationSettings.DestinationCategory);
            Prefab createdPrefab = PrefabFactory.Create(createdUnityPrefab);

            prefabCreationSettings.DestinationCategory.AddPrefab(createdPrefab);

            Octave3DWorldBuilder.DestroyImmediate(prefabRoot);
            PrefabManagementWindow.Get().RepaintOctave3DWindow();

            return(createdPrefab);
        }