Beispiel #1
0
    private GameObject CreateBoundingBox(string modelTargetName, OrientedBoundingBox3D bbox)
    {
        var bboxGameObj = new GameObject(modelTargetName + "_BoundingBox");

        bboxGameObj.transform.localPosition = bbox.Center;
        bboxGameObj.transform.localRotation = Quaternion.identity;
        bboxGameObj.transform.localScale    = 2 * bbox.HalfExtents;
        bboxGameObj.AddComponent <BoundingBoxRenderer>();
        return(bboxGameObj);
    }
    /// <summary>
    /// Handles new search results.
    /// </summary>
    /// <param name="searchResult"></param>
    public virtual void OnNewSearchResult(TargetFinder.TargetSearchResult searchResult)
    {
        Debug.Log("<color=cyan>OnNewSearchResult() called: </color>" + searchResult.TargetName);

        // Find or create the referenced model target
        GameObject modelTargetGameObj = null;

        bool builtFromTemplate = false;

        var existingModelTarget = FindExistingModelTarget(searchResult);

        if (existingModelTarget)
        {
            modelTargetGameObj = existingModelTarget.gameObject;
            builtFromTemplate  = false;
        }
        else if (ModelTargetTemplate)
        {
            modelTargetGameObj = Instantiate(ModelTargetTemplate.gameObject);
            builtFromTemplate  = true;
        }

        if (!modelTargetGameObj)
        {
            Debug.LogError("Could not create a Model Target.");
            return;
        }

        // Enable the new search result as a Model Target
        ModelTargetBehaviour mtb = targetFinder.EnableTracking(searchResult, modelTargetGameObj)
                                   as ModelTargetBehaviour;

        if (mtb)
        {
            this.lastRecoModelTarget = mtb;

            // If the model target was created from a template,
            // we augment it with a bounding box game object
            if (builtFromTemplate && ShowBoundingBox)
            {
                OrientedBoundingBox3D modelBoundingBox = mtb.ModelTarget.GetBoundingBox();
                var boundingBoxGameObj = CreateBoundingBox(mtb.ModelTarget.Name, modelBoundingBox);

                // Parent the bounding box under the model target.
                boundingBoxGameObj.transform.SetParent(modelTargetGameObj.transform, false);
            }

            if (this.StopSearchWhenModelFound)
            {
                // StopService the target finder
                this.modelRecoBehaviour.ModelRecoEnabled = false;
            }
        }
    }
    private void DrawOrientedBoundingBox3D(OrientedBoundingBox3D bbox3d)
    {
        if (this.boundingBox)
        {
            // Calculate local position and scale from Model Target bounding box.
            var bboxLocalPosition = new Vector3(bbox3d.Center.x, bbox3d.Center.y, bbox3d.Center.z);
            var bboxLocalScale    = new Vector3(bbox3d.HalfExtents.x * 2, bbox3d.HalfExtents.y * 2, bbox3d.HalfExtents.z * 2);

            // Assign values to augmentation bounding box.
            this.boundingBox.transform.SetParent(mTrackableBehaviour.transform);
            this.boundingBox.transform.localEulerAngles = Vector3.zero;
            this.boundingBox.transform.position         = Vector3.zero;
            this.boundingBox.transform.localPosition    = bboxLocalPosition;
            this.boundingBox.transform.localScale       = bboxLocalScale;
        }
    }