Beispiel #1
0
    // checks for image anchor, to create or destroy the coffee cups
    private void CheckForImageAnchor()
    {
        MultiARManager marManager = MultiARManager.Instance;

        // Check that motion tracking is tracking.
        StringBuilder sbMessage = new StringBuilder();

        if (!marManager || marManager.GetCameraTrackingState() != MultiARInterop.CameraTrackingState.NormalTracking)
        {
            sbMessage.AppendLine("Camera - Not tracking.");
            sbMessage.AppendLine("detectedImage: " + (imageAnchorName != string.Empty ? imageAnchorName : "-"));

            ShowDebugText(sbMessage.ToString());
            return;
        }

        // Get updated augmented images for this frame.
        List <string> alImageAnchors = marManager.GetTrackedImageAnchorNames();

        sbMessage.AppendLine(alImageAnchors.Count.ToString() + " anchor images found: ");

//		for (int i = 0; i < alImageAnchors.Count; i++)
//		{
//			sbMessage.Append(i).Append (" - ").Append (alImageAnchors[i]).AppendLine();
//		}

        sbMessage.AppendLine("detectedImage: " + (imageAnchorName != string.Empty ? imageAnchorName : "-"));
        sbMessage.AppendLine("imageAnchor: " + (imageAnchor != null ? imageAnchor.name : "none"));

        ShowDebugText(sbMessage.ToString());

        if (infoText)
        {
            infoText.text = "Found image anchor: " + (imageAnchorName != string.Empty ? imageAnchorName : "-");

            if (cupIndexSelected >= 0)
            {
                infoText.text = string.Format("Selected: {0}", coffeeCupTitles[cupIndexSelected]);
            }
        }

        // check for found image anchor
        string foundImageAnchor = marManager.GetFirstTrackedImageAnchorName();

        if (imageAnchorName == string.Empty)
        {
            imageAnchorName = foundImageAnchor;

            if (imageAnchorName != string.Empty)
            {
                imageAnchor = marManager.GetTrackedImageAnchorByName(imageAnchorName);
                Camera mainCamera = marManager.GetMainCamera();

                if (imageAnchor != null && mainCamera != null)
                {
                    // create coffee cups-parent (used to smooth anchor jittering)
                    cupAnchor = new GameObject("CupAnchor");                     // GameObject.CreatePrimitive(PrimitiveType.Cube);

                    cupAnchor.transform.position = imageAnchor.transform.position;
                    Vector3 vAnchorDir = imageAnchor.transform.position - mainCamera.transform.position;
                    cupAnchor.transform.rotation = Quaternion.LookRotation(vAnchorDir, Vector3.up);                     // imageAnchor.transform.rotation;

//					// create anchor-pointer object (cube)
//					GameObject anchorPoiterObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
//					anchorPoiterObj.transform.parent = imageAnchor.transform;
//
//					anchorPoiterObj.transform.localPosition = Vector3.zero;
//					anchorPoiterObj.transform.localRotation = Quaternion.identity;
//					anchorPoiterObj.transform.localScale = new Vector3(0.05f, 0.1f, 0.15f);

                    // create cup models
                    StartCoroutine(CreateAndPlaceCups());
                }
            }
        }
        else if (imageAnchorName != foundImageAnchor)
        {
            // destroy the coffee cups
            DestroyCups();
        }
    }