Exemple #1
0
        /// <summary>
        /// Ensures that the active scene has a MARS Session
        /// </summary>
        /// <returns>Thew newly created MARSSession, or null if a session previously exists</returns>
        public static MARSSession EnsureSessionInActiveScene()
        {
            var session = GameObjectUtils.GetComponentInActiveScene <MARSSession>();

            if (SessionConfigured(session))
            {
                return(null);
            }

            using (var undoBlock = new UndoBlock("Ensure Session in Active Scene", TestMode))
            {
                if (!session)
                {
                    var marsBehaviorsInScene = MarsRuntimeUtils.HasMarsBehaviors(SceneManager.GetActiveScene());
                    session = CreateSession(undoBlock, marsBehaviorsInScene);

                    var userRef = GameObjectUtils.Instantiate(MARSRuntimePrefabs.instance.UserPrefab).transform;
                    userRef.name          = k_UserName;
                    userRef.parent        = session.m_CameraReference.transform;
                    userRef.localPosition = Vector3.zero;
                    userRef.localRotation = Quaternion.identity;
                    userRef.localScale    = Vector3.one;
                    undoBlock.RegisterCreatedObject(userRef.gameObject);
                    session.m_UserReference = userRef;

                    DirtySimulatableSceneIfNeeded();
                    return(session);
                }

                EnsureSessionConfigured(session, undoBlock);
            }

            return(null);
        }
        internal static Transform CreateGeneratedPlanesRoot(Transform parent, UndoBlock undoBlock)
        {
            var planesRoot = new GameObject(GeneratedPlanesRoot.PlanesRootName, typeof(GeneratedPlanesRoot)).transform;

            planesRoot.SetParent(parent);
            undoBlock.RegisterCreatedObject(planesRoot.gameObject);
            return(planesRoot);
        }
#pragma warning restore 649

        public override bool CreateGameObject(out GameObject createdObj, Transform parentTransform = null)
        {
            MARSSession.EnsureRuntimeState();

            createdObj = null;

            SimulationView activeView = SimulationView.ActiveSimulationView as SimulationView;

            if (activeView != null)
            {
                activeView.EnvironmentSceneActive = true;
            }

            Tools.current = Tool.Move;

            GameObject markerGO;

            using (var undoBlock = new UndoBlock("Synthetic Marker Creation"))
            {
                var imageMarkerParent = GetOrGenerateUniqueParent(k_SimulatedMarkersParentName);
                if (imageMarkerParent == null)
                {
                    Debug.LogWarning("Unable to create synthetic marker.");
                    return(false);
                }

                var syntheticMarker = CreateSyntheticMarker();
                var markerTransform = syntheticMarker.transform;
                markerTransform.parent = imageMarkerParent;

                // Zero out position and rotation, leave scale in case the user is working with other scale.
                markerTransform.localPosition = Vector3.zero;
                markerTransform.localRotation = Quaternion.identity;

                markerTransform.name = GameObjectUtility.GetUniqueNameForSibling(imageMarkerParent, m_ObjectName);

                markerGO = markerTransform.gameObject;
                undoBlock.RegisterCreatedObject(markerGO);

                Selection.activeTransform = markerTransform;

                var initialScale = Vector2.one * MarsWorldScaleModule.GetWorldScale();
                syntheticMarker.UpdateMarkerSize(initialScale);

                markerTransform.localScale = new Vector3(1, 0.01f, 1);
                createdObj = markerTransform.gameObject;
            }

            return(true);
        }
Exemple #4
0
        static Camera CreateCamera(Transform sessionTransform, UndoBlock undoBlock)
        {
            Debug.LogWarning("Scene does not have a main camera.  Adding one to the scene.");
            var newCamera = new GameObject(k_CameraName, k_CameraComponents)
            {
                tag = k_CameraTag
            };
            var cameraRef = newCamera.GetComponent <Camera>();

            cameraRef.nearClipPlane    = k_DefaultNearPlane * sessionTransform.localScale.x;
            newCamera.transform.parent = sessionTransform;
            undoBlock.RegisterCreatedObject(newCamera);
            return(cameraRef);
        }
        static GameObject CreateHorizontalSurface(UndoBlock undoBlock)
        {
            var gameObject = new GameObject("Horizontal Surface");

            undoBlock.RegisterCreatedObject(gameObject);
            gameObject.transform.parent        = Selection.activeTransform;
            gameObject.transform.localPosition = Vector3.zero;
            gameObject.AddComponent <Proxy>();
            gameObject.AddComponent <IsPlaneCondition>();
            gameObject.AddComponent <AlignmentCondition>();
            gameObject.AddComponent <SetPoseAction>();
            gameObject.AddComponent <ShowChildrenOnTrackingAction>();

            return(gameObject);
        }
        static GameObject CreateReplicator(UndoBlock undoBlock)
        {
            var count = string.Empty;

            var gameObject = new GameObject($"On Every{count}");

            undoBlock.RegisterCreatedObject(gameObject);
            gameObject.transform.position = s_LastProxyPosition;
            s_LastProxyPosition          += Vector3.right * k_ReplicatorSceneOffset;
            gameObject.AddComponent <Replicator>();

            if (!s_LastSetChildrenVisible)
            {
                gameObject.hideFlags = HideFlags.HideInHierarchy;
            }

            return(gameObject);
        }
Exemple #7
0
        static void EnsureSessionConfigured(MARSSession session, UndoBlock undoBlock, Transform overrideUserRef = null)
        {
            var sessionObject    = session.gameObject;
            var sessionTransform = session.transform;
            var changed          = false;

            // Make sure we have a properly configured MARS Camera
            if (!TestMode && session.m_CameraReference == null)
            {
                var marsCameraRef = GameObjectUtils.ExhaustiveComponentSearch <MARSCamera>(sessionObject);

                // If we can't find a MARS camera, get the main camera and create one on that
                if (marsCameraRef == null)
                {
                    LogRuntimeIssue("MARSCamera not present in the scene.  Adding one to the main camera.");

                    var createdNewCamera = false;
                    var cameraRef        = GameObjectUtils.ExhaustiveTaggedComponentSearch <Camera>(sessionObject, k_CameraTag);
                    if (cameraRef == null)
                    {
                        cameraRef        = CreateCamera(sessionTransform, undoBlock);
                        createdNewCamera = true;
                    }

                    marsCameraRef = undoBlock.AddComponent <MARSCamera>(cameraRef.gameObject);

                    if (!createdNewCamera)
                    {
                        var nearPlane    = cameraRef.nearClipPlane;
                        var cameraParent = cameraRef.transform.parent;
                        if (cameraParent == null)
                        {
                            cameraParent = sessionTransform;
                        }

                        var worldScale         = cameraParent.localScale.x;
                        var scaledMaxNearPlane = k_MaxNearPlane * worldScale;
                        if (nearPlane > scaledMaxNearPlane)
                        {
                            LogRuntimeIssue("Camera near clip plane is greater than the recommended distance. " +
                                            $"Setting near plane from {nearPlane} to {scaledMaxNearPlane}.");

                            undoBlock.RecordObject(cameraRef);
                            cameraRef.nearClipPlane = scaledMaxNearPlane;
                        }
                    }
                }

                session.m_CameraReference = marsCameraRef;
                changed = true;
            }

            const string correctBrokenSessionMessage = "If you have not customized the MARSSession game object, just delete " +
                                                       "the object and go to Create > MARS > Session.";

            var cameraTrans = session.m_CameraReference.transform;

            if (!TestMode && cameraTrans.GetComponentInParent <MARSSession>() == null)
            {
                const string cameraParentRequirementMessage = "MARSCamera must have a MARSSession object in its list of parents.";
                var          canReparent = true;
#if INCLUDE_AR_FOUNDATION
                if (cameraTrans.GetComponentInParent <ARSessionOrigin>() != null)
                {
                    canReparent = false;
                    Debug.LogError($"{cameraParentRequirementMessage} Please make sure the MARSSession component " +
                                   $"is on the same game object as the ARSessionOrigin. {correctBrokenSessionMessage}");
                }
#endif
                if (canReparent)
                {
                    LogRuntimeIssue($"{cameraParentRequirementMessage} Re-parenting.");
                    var cameraParent = cameraTrans.parent;
                    if (cameraParent)
                    {
                        sessionTransform.position   = cameraParent.position;
                        sessionTransform.rotation   = cameraParent.rotation;
                        sessionTransform.localScale = cameraParent.localScale;
                    }

                    undoBlock.SetTransformParent(cameraTrans, sessionTransform);
                    changed = true;
                }
            }

            // Look for the user object, if it's not in the scene, make a new one
            var userRef = session.m_UserReference;
            if (!TestMode && userRef == null)
            {
                userRef = session.m_CameraReference.transform.Find(k_UserName);
                if (userRef == null)
                {
                    if (overrideUserRef == null)
                    {
                        userRef = GameObjectUtils.Instantiate(MARSRuntimePrefabs.instance.UserPrefab).transform;
                    }
                    else
                    {
                        userRef = overrideUserRef;
                    }

                    userRef.name          = k_UserName;
                    userRef.parent        = session.m_CameraReference.transform;
                    userRef.localPosition = Vector3.zero;
                    userRef.localRotation = Quaternion.identity;
                    userRef.localScale    = Vector3.one;
                    undoBlock.RegisterCreatedObject(userRef.gameObject);
                }

                session.m_UserReference = userRef;
                changed = true;
            }

            // One more bizarre scenario to catch - the MARS Session and Camera being *one* object.
            // We can't just delete the MARS Session since the user might have other scripts there, so we just throw up a warning telling them to fix things manually
            if (!TestMode && session.m_CameraReference.transform == sessionTransform)
            {
                Debug.LogError("The MARS Session should be a parent of the MARSCamera, *NOT* the same object!  " +
                               $"Please correct this in your scene! {correctBrokenSessionMessage}");
            }

            if (sessionObject.activeInHierarchy == false)
            {
                LogRuntimeIssue("There is a MARS Session object in your scene that is *not* active.  Running your scene with an inactive MARS Session will cause problems.");
                sessionObject.SetActive(true);
                changed = true;
            }

            var cameraObject = session.cameraReference.gameObject;
            if (cameraObject.activeInHierarchy == false)
            {
                LogRuntimeIssue("There is a MARS Camera GameObject in your scene that is *not* active.  " +
                                "Re-enabling here, as running your scene with an inactive MARS Camera would cause problems.");

                cameraObject.SetActive(true);
                changed = true;
            }

            if (changed)
            {
                DirtySimulatableSceneIfNeeded();
            }
        }
Exemple #8
0
        static MARSSession CreateSession(UndoBlock undoBlock, bool marsBehaviorsInScene = true)
        {
            const string sessionNotPresentMessage = "MARS Session not present in the scene - creating one now.";

            if (marsBehaviorsInScene)
            {
                LogRuntimeIssue(sessionNotPresentMessage);
            }
            else if (!TestMode)
            {
                Debug.Log(sessionNotPresentMessage);
            }

            GameObject sessionObject = null;
            Camera     cameraRef     = null;

            k_Cameras.Clear();
            GameObjectUtils.GetComponentsInActiveScene(k_Cameras);
            if (k_Cameras.Count > 0)
            {
                cameraRef = k_Cameras[0];
                foreach (var camera in k_Cameras)
                {
                    if (camera.CompareTag(k_CameraTag))
                    {
                        cameraRef = camera;
                        break;
                    }
                }
            }

            if (cameraRef != null && cameraRef.transform.parent != null)
            {
                sessionObject = cameraRef.transform.parent.gameObject;
                var addToParentMessage = "Adding MARS Session component to immediate parent of main camera.";
#if INCLUDE_AR_FOUNDATION
                var arSessionOrigin = cameraRef.GetComponentInParent <ARSessionOrigin>();
                if (arSessionOrigin != null)
                {
                    sessionObject      = arSessionOrigin.gameObject;
                    addToParentMessage = "Adding MARS Session component to AR Session Origin game object.";
                }
#endif

                Debug.Log(addToParentMessage);
                undoBlock.RecordObject(sessionObject);
#if UNITY_EDITOR
                EditorGUIUtility.PingObject(sessionObject);
#endif
            }

            if (sessionObject == null)
            {
                sessionObject = new GameObject(k_ObjectName);
                sessionObject.transform.SetAsFirstSibling();
                undoBlock.RegisterCreatedObject(sessionObject);
            }

            var sessionTrans = sessionObject.transform;
            if (cameraRef == null)
            {
                cameraRef = CreateCamera(sessionTrans, undoBlock);
            }

            var cameraObj     = cameraRef.gameObject;
            var marsCameraRef = cameraObj.GetComponent <MARSCamera>();
            if (!marsCameraRef)
            {
                Debug.Log("Adding MARS Camera component to main camera.");

                // TODO: configure near plane
                marsCameraRef = undoBlock.AddComponent <MARSCamera>(cameraObj);
            }

            var session = undoBlock.AddComponent <MARSSession>(sessionObject);
            Instance = session;
            session.m_CameraReference = marsCameraRef;

            return(session);
        }