void Start() { targetHeight = mImgTarget.GetSize()[1]; targetWidth = mImgTarget.GetSize()[0]; ptTopRight = new Vector3(0.5f * (targetWidth / targetHeight), 0, 0.5f); ptTopLeft = new Vector3(-0.5f * (targetWidth / targetHeight), 0, 0.5f); ptBottomRight = new Vector3(0.5f * (targetWidth / targetHeight), 0, -0.5f); ptBottomLeft = new Vector3(-0.5f * (targetWidth / targetHeight), 0, -0.5f); mTrackableBehaviour = GetComponent <TrackableBehaviour>(); if (mTrackableBehaviour) { mTrackableBehaviour.RegisterTrackableEventHandler(this); } }
// Update is called once per frame void Update() { if (mImageTargetBehaviour == null) { Debug.Log("ImageTargetBehaviour not found"); return; } Vector2 targetSize = mImageTargetBehaviour.GetSize(); float targetAspect = targetSize.x / targetSize.y; // We define a point in the target local reference // we take the bottom-left corner of the target, // just as an example // Note: the target reference plane in Unity is X-Z, // while Y is the normal direction to the target plane Vector3 pointOnTarget = new Vector3(-0.5f, 0, -0.5f / targetAspect); // We convert the local point to world coordinates Vector3 targetPointInWorldRef = transform.TransformPoint(pointOnTarget); // We project the world coordinates to screen coords (pixels) Vector3 screenPoint = Camera.main.WorldToScreenPoint(targetPointInWorldRef); world_x = targetPointInWorldRef.x; world_y = targetPointInWorldRef.y; world_z = targetPointInWorldRef.z; screen_x = screenPoint.x; screen_y = screenPoint.y; }
private void fitToTarget(Transform fittee, Vuforia.ImageTargetBehaviour imgTarget, float scale) { // Get image targe size Vector2 imgTargetSize = imgTarget.GetSize(); float imgTargetWidth = imgTargetSize.x; float imgTargetHeight = imgTargetSize.y; // Scale plane to image target size fittee.transform.localScale = new Vector3( imgTargetWidth * scale, 1f * scale, imgTargetHeight * scale); }
/// <summary> /// Instantiates a new user-defined target and is also responsible for dispatching callback to /// IUserDefinedTargetEventHandler::OnNewTrackableSource /// </summary> public void BuildNewTarget() { if (mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_MEDIUM || mFrameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_HIGH) { // create the name of the next target. // the TrackableName of the original, linked ImageTargetBehaviour is extended with a continuous number to ensure unique names string targetName = string.Format("{0}-{1}", ImageTargetTemplate.TrackableName, mTargetCounter); // generate a new target: mTargetBuildingBehaviour.BuildNewTarget(targetName, ImageTargetTemplate.GetSize().x); UIManager.ShowMessage("专家来了!"); } else { Debug.Log("Cannot build new target, due to poor camera image quality"); UIManager.ShowErrorMessage("当前画面信息量太低!"); } }