Exemple #1
0
        // Update is called once per frame
        void Update()
        {
            Session.GetTrackables <AugmentedImage>(
                m_TempAugmentedImages, TrackableQueryFilter.Updated);
            foreach (var image in m_TempAugmentedImages)
            {
                if (image.DatabaseIndex != TargetImageIndex)
                {
                    continue;
                }

                if (image.TrackingState == TrackingState.Tracking && MarkerObj.activeSelf == false)
                {
                    // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                    Anchor anchor = image.CreateAnchor(image.CenterPose);
                    MarkerObj.transform.parent        = anchor.transform;
                    MarkerObj.transform.localPosition = Vector3.zero;
                    MarkerObj.transform.localRotation = Quaternion.identity;
                    MarkerObj.SetActive(true);
                    ResetFrame.ResetFrameBoth(MarkerObj.transform);
                    ToastUtil.Toast(this, "Set Robot pose by the marker.");
                }
                else if (image.TrackingState == TrackingState.Stopped && MarkerObj.activeSelf == true)
                {
                    if (MarkerObj.transform.parent != null)
                    {
                        Destroy(MarkerObj.transform.parent.gameObject);
                    }
                    MarkerObj.transform.parent = null;
                }
            }
        }
        private void OnClickCallBack()
        {
            var text = "BtnOnClickTest OnClick AddImage";

            Debug.Log(text);
            ToastUtil.showToast(text);
            MsgTextView.text = text;

            //设置中间 ImageView 的图像为 ContentImageSprite
            ContentImageView.overrideSprite = ContentImageSprite;

            //通知 ARCoreImageController
            //GameObject.Find("需要传递到游戏对象的Name").SendMessage(
            //      "脚本中的方法名称",
            //      Object 传递的参数类型,
            //      SendMessageOptions.DontRequireReceiver//这句代码表示不需要请求返回参数
            //);
            GameObject.Find("ExampleController").SendMessage("AddImageBtnClick", 0, SendMessageOptions.DontRequireReceiver);
        }
        private void addSimpleImage()
        {
            //每次只取第 0 个图

            //这个不好使
            //tempAugmentedImageVisualizer = (ARcoreImagePrefabVisualizerOverride)Instantiate(ImageViewContentVisualizerPrefab[0], anchor.transform);
            //var gameObject = Instantiate(ImageViewContentVisualizerPrefab[0], hit.Pose.position, hit.Pose.rotation);

            var targetAnchor = getAnchor();

            if (targetAnchor != null)
            {
                try
                {
                    ToastUtil.showToast("addSimpleImage targetAnchor is not null , ImageViewContentVisualizerPrefab.Count= " + ImageViewContentVisualizerPrefab.Count);
                    var position = new Vector3(0.0f, 0f, 0.0f);
                    var rotation = new Quaternion(1f, 0f, 0f, -90f);

                    //这种方式只对 AugmentedImageVisualizerPrefab 有效
                    //var gameObject = Instantiate(ImageViewContentVisualizerPrefab[0], position, rotation); //ImageViewContentVisualizerPrefab  AugmentedImageVisualizerPrefab

                    //方式二: 使用这种方式试试 ImageViewContentVisualizerPrefab, 然而并不好使
                    var        gameObject = (GameObject)Instantiate(Resources.Load("Prefab/ArCoreImagePrefab"));
                    GameObject uiCanvas   = GameObject.Find("Canvas");
                    gameObject.transform.parent = uiCanvas.transform;
                    ToastUtil.showToast("添加 ArCoreImagePrefab 完毕");

                    //gameObject.transform.Rotate(0, _prefabRotation, 0, Space.Self);
                    ////var anchor = hit.Trackable.CreateAnchor(hit.Pose);
                    //gameObject.transform.parent = targetAnchor.transform;
                }catch (Exception e)
                {
                    ToastUtil.showToast("exception =" + e.Message);
                }
            }
            else
            {
                Debug.Log("addSimpleImage targetAnchor is null");
                ToastUtil.showToast("addSimpleImage targetAnchor is null");
            }
        }
        public void Update()
        {
            if (Input.GetKey(KeyCode.Escape))
            {
                Application.Quit();
            }

            if (Session.Status != SessionStatus.Tracking)
            {
                return;
            }

            Session.GetTrackables(m_TempAugmentedImages, TrackableQueryFilter.Updated);

            foreach (var image in m_TempAugmentedImages)
            {
                Debug.Log("name======" + image.Name);
                Debug.Log("index======" + image.DatabaseIndex);
                AugmentedImageVisualizerOverride tempAugmentedImageVisualizer = null;
                m_Visualizers.TryGetValue(image.DatabaseIndex, out tempAugmentedImageVisualizer);

                if (image.TrackingState == TrackingState.Tracking && tempAugmentedImageVisualizer == null)
                {
                    Debug.Log("TrackName" + image.Name);
                    Debug.Log("TrackIndex" + image.DatabaseIndex);
                    if (!m_isDetectedImgShowToast)
                    {
                        ToastUtil.showToast("ARCoreImageController Update() 检测到图像成功 !");
                        m_isDetectedImgShowToast = true;
                    }

                    Anchor anchor = image.CreateAnchor(image.CenterPose);

                    //将锚点放到集合里
                    if (!m_anchorDict.ContainsKey(image.DatabaseIndex))
                    {
                        m_anchorDict.Add(image.DatabaseIndex, anchor);
                    }

                    //visualizer = AugmentedImageVisualizerPrefab[image.DatabaseIndex];
                    //visualizer.gameObject.SetActive(true);
                    //visualizer.transform.parent = anchor.transform;
                    //visualizer.transform.localPosition = Vector3.zero;
                    //visualizer.transform.localRotation = new Quaternion(0, 0, 0, 0);
                    //visualizer.transform.parent = null;

                    //识别出来就添加了一个图片,这里先注释了改为手动添加
                    //使用 AugmentedImageVisualizerPrefab 通过 image.DatabaseIndex 初始化一个 AugmentedImageVisualizerOverride 对象,并设置锚点的 transform
                    //tempAugmentedImageVisualizer = (AugmentedImageVisualizerOverride)Instantiate(AugmentedImageVisualizerPrefab[image.DatabaseIndex], anchor.transform);

                    ////为该 AugmentedImageVisualizerOverride 对象设置识别到的 AugmentedImage (增强图像) 对象
                    //tempAugmentedImageVisualizer.Image = image;

                    //m_Visualizers.Add(image.DatabaseIndex, tempAugmentedImageVisualizer);
                }
            }

            foreach (var visualizer in m_Visualizers.Values)
            {
                if (visualizer.Image.TrackingState == TrackingState.Tracking)
                {
                    FitToScanOverlay.SetActive(false);
                    return;
                }
            }
        }
 public void AddImageBtnClick()
 {
     ToastUtil.showToast("ARCoreImageController AddImageBtnClick() invoked");
     addSimpleImage();
 }