void Update()
    {
//		// don't consider taps over the UI
//		if(UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
//			return;

        // check for click
        if (objectPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true))
        {
            MultiARInterop.InputAction action = arManager.GetInputAction();

            if (action == MultiARInterop.InputAction.Click)
            {
                // raycast world
                //Vector2 screenPos = Input.GetTouch(0).position;
                MultiARInterop.TrackableHit hit;

                if (arManager.RaycastToWorld(true, out hit))
                {
                    // instantiate the object and anchor it to the world position
                    GameObject spawnObj = Instantiate(objectPrefab, hit.point, !verticalModel ? hit.rotation : Quaternion.identity);
                    arManager.AnchorGameObjectToWorld(spawnObj, hit);

                    // look at the camera
                    if (modelLookingAtCamera)
                    {
                        Camera arCamera = arManager.GetMainCamera();
                        MultiARInterop.TurnObjectToCamera(spawnObj, arCamera, hit.point, hit.normal);
                    }
                }
            }
        }
    }
    void Update()
    {
//		// don't consider taps over the UI
//		if(UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
//			return;

        // check for tap
        if (arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true))
        {
            MultiARInterop.InputAction action = arManager.GetInputAction();

            if (action == MultiARInterop.InputAction.Click || action == MultiARInterop.InputAction.Grip)
            {
                // check if there is a model selected
                if (currentModel == null)
                {
                    Debug.LogError("No model selected!");
                    return;
                }

                // update the currently selected model
                currentModel = GetModelHit();

                // raycast world
                MultiARInterop.TrackableHit hit;
                if (currentModel && arManager.RaycastToWorld(true, out hit))
                {
                    // anchor the model if needed
                    if (currentModel.parent == null)
                    {
                        arManager.AnchorGameObjectToWorld(currentModel.gameObject, hit);
                    }

                    // set the new position of the model
                    SetModelWorldPos(hit.point, !verticalModel ? hit.rotation : Quaternion.identity);
                }
            }
        }

        // update the info
        if (infoText)
        {
            infoText.text = currentModel ? "Selected: " + currentModel.gameObject.name : "No model selected";
        }

        // turn off the toggles, if the respective models are not active
        UpdateToggleStatus(toggle1, model1);
        UpdateToggleStatus(toggle2, model2);
        UpdateToggleStatus(toggle3, model3);
    }
Exemple #3
0
    // invoked by the anchor-toggle
    public void AnchorToggleSelected(bool bOn)
    {
        if (bIntAction || !anchorTransform || !arManager)
        {
            return;
        }

        if (bOn)
        {
            // activate the anchor transform, if needed
            if (!anchorTransform.gameObject.activeSelf)
            {
                anchorTransform.gameObject.SetActive(true);
            }

            // create the anchor if needed
            if (!arManager.IsValidAnchorId(anchorId))
            {
                if (modelTransform && modelTransform.gameObject.activeSelf)
                {
                    // create the world anchor at model's position
                    anchorId = arManager.AnchorGameObjectToWorld(anchorTransform.gameObject, modelTransform.position, Quaternion.identity);
                }
                else
                {
                    // raycast center of the screen
                    //Vector2 screenPos = new Vector2(Screen.width / 2f, Screen.height / 2f);
                    MultiARInterop.TrackableHit hit;
                    if (arManager.RaycastToWorld(false, out hit))
                    {
                        // create the world anchor at hit's position
                        anchorId = arManager.AnchorGameObjectToWorld(anchorTransform.gameObject, hit);
                    }
                }

                // attach the model to the same anchor
                if (arManager.IsValidAnchorId(anchorId) && modelTransform && modelTransform.gameObject.activeSelf)
                {
                    arManager.AttachObjectToAnchor(modelTransform.gameObject, anchorId, false, false);
                }
            }
        }
        else
        {
            // remove the anchor as needed
            if (arManager.IsValidAnchorId(anchorId))
            {
                // create the world anchor
                if (arManager.RemoveGameObjectAnchor(anchorId, true))
                {
                    anchorId = string.Empty;
                }
            }

            // deactivate the anchor transform, if needed
            if (anchorTransform.gameObject.activeSelf)
            {
                anchorTransform.gameObject.SetActive(false);
            }
        }
    }
    void Update()
    {
        // check for click
        if (objectPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true))
        {
            MultiARInterop.InputAction action = arManager.GetInputAction();

            if (action == MultiARInterop.InputAction.Click && (Time.time - lastInstanceTime) >= 1.5f)
            {
                // dont allow too often instance creation
                lastInstanceTime = Time.time;

                // remove current object, if any
                if (objectInstance)
                {
                    DestroyObjectInstance();
                }

                // raycast world
                MultiARInterop.TrackableHit hit;
                if (arManager.RaycastToWorld(true, out hit))
                {
                    // instantiate the object and anchor it to the world position
                    objectInstance = Instantiate(objectPrefab, hit.point, !verticalModel ? hit.rotation : Quaternion.identity);
                    arManager.AnchorGameObjectToWorld(objectInstance, hit);

                    // get object renderer & initial scale
                    objectRenderer = GetObjectRenderer(objectInstance);
                    objectScale    = objectInstance.transform.localScale;

                    // look at the camera
                    if (modelLookingAtCamera)
                    {
                        Camera arCamera = arManager.GetMainCamera();
                        MultiARInterop.TurnObjectToCamera(objectInstance, arCamera, hit.point, hit.normal);
                    }

                    if (infoText)
                    {
                        infoText.text = string.Format("{0} placed at {1}", objectPrefab.name, hit.point);
                    }
                }
            }
            else if (objectInstance && action == MultiARInterop.InputAction.Grip)
            {
                // get nav coordinates
                Vector3 navCoords = arManager.GetInputNavCoordinates();
                lastInstanceTime = Time.time;

                // estimate the scale change and target scale
                Vector3 scaleChange = navCoords.x >= 0 ? (objectScale * navCoords.x) : ((objectScale / 2f) * navCoords.x);
                targetScale = objectScale + scaleChange;

                objectInstance.transform.localScale = Vector3.Lerp(objectInstance.transform.localScale, targetScale, smoothFactor * Time.deltaTime);

                if (infoText)
                {
                    float fScaleChange = 1f + (navCoords.x >= 0 ? navCoords.x : (0.5f * navCoords.x));
                    infoText.text = string.Format("Scale change: {0:F2}", fScaleChange);
                }

                // outline object bounds
                if (objectRenderer && boundsPrefab)
                {
                    Bounds objectBounds = objectRenderer.bounds;

                    // instantiate bounds-cube, if needed
                    if (boundsInstance == null)
                    {
                        boundsInstance = GameObject.Instantiate(boundsPrefab);
                        boundsInstance.transform.SetParent(objectInstance.transform);
                    }

                    // set the bounds-cube tras=nsform
                    boundsInstance.transform.position = objectBounds.center;
                    boundsInstance.transform.rotation = objectInstance.transform.rotation;

                    Vector3 objScale    = objectInstance.transform.localScale;
                    Vector3 boundsScale = new Vector3(objectBounds.size.x / objScale.x, objectBounds.size.y / objScale.y, objectBounds.size.z / objScale.z);
                    boundsInstance.transform.localScale = boundsScale;
                }
            }
            else if (action == MultiARInterop.InputAction.Release)
            {
                // instantiate bounds-cube, if needed
                if (boundsInstance != null)
                {
                    Destroy(boundsInstance);
                    boundsInstance = null;
                }

                if (infoText)
                {
                    infoText.text = "Tap to place the object, then drag right or left, to scale it up or down.";
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        // check for tap
        if (portalPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true))
        {
            MultiARInterop.InputAction action = arManager.GetInputAction();

            if (action == MultiARInterop.InputAction.Click)
            {
                // raycast to world
                MultiARInterop.TrackableHit hit;
                if (arManager.RaycastToWorld(true, out hit))
                {
                    // create the portal object, if needed
                    if (!portalObj)
                    {
                        portalObj = Instantiate(portalPrefab);
                    }

                    // set its position and rotation
                    portalObj.transform.position = hit.point;
                    portalObj.transform.rotation = !verticalPortal ? hit.rotation : Quaternion.identity;

                    // look at the camera
                    if (portalLookingAtCamera)
                    {
                        Camera arCamera = arManager.GetMainCamera();
                        MultiARInterop.TurnObjectToCamera(portalObj, arCamera, hit.point, hit.normal);
                    }

                    // remove object anchor, if it was anchored before
                    string anchorId = arManager.GetObjectAnchorId(portalObj);
                    if (anchorId != string.Empty)
                    {
                        arManager.RemoveGameObjectAnchor(anchorId, true);
                    }

                    // anchor it to the new world position
                    arManager.AnchorGameObjectToWorld(portalObj, hit);

                    // apply the vertical offset
                    if (verticalOffset != 0f)
                    {
                        Vector3 objPos = portalObj.transform.position;
                        //objPos.y += verticalOffset;
                        objPos += portalObj.transform.up * verticalOffset;
                        portalObj.transform.position = objPos;
                    }

                    // play portal-open animation
                    if (playAnimation != string.Empty)
                    {
                        // get reference to the portal animator
                        if (!animator)
                        {
                            animator = portalObj.GetComponent <Animator>();
                        }

                        if (animator)
                        {
                            animator.Play(playAnimation, 0, 0f);
                        }
                    }

                    // create camera rigidbody (no gravity) & box-collider, if needed
                    if (cameraBoxCollider != Vector3.zero)
                    {
                        Camera arCamera = arManager.GetMainCamera();

                        Rigidbody camRigidbody = arCamera.gameObject.GetComponent <Rigidbody>();
                        if (camRigidbody == null)
                        {
                            camRigidbody            = arCamera.gameObject.AddComponent <Rigidbody>();
                            camRigidbody.useGravity = false;
                        }

                        BoxCollider camBoxCollider = arCamera.gameObject.GetComponent <BoxCollider>();
                        if (camBoxCollider == null)
                        {
                            camBoxCollider           = arCamera.gameObject.AddComponent <BoxCollider>();
                            camBoxCollider.size      = cameraBoxCollider;
                            camBoxCollider.isTrigger = true;
                        }
                    }
                }
            }
        }
    }