Hololens中的虚拟物体通过Vuforia的码实现虚实融合

功能描述,通过码将虚拟物体放置在合适的位置,然后Cursor和要放置的虚拟物体碰撞时,通过确定的手势将虚拟的位置固定。

1,建一个表示Gaze的物体,碰撞检测需要MeshCollider并挂脚本

Hololens中的虚拟物体通过Vuforia的码实现虚实融合_第1张图片

world cursor.cs

using UnityEngine;

public class WorldCursor : MonoBehaviour
{
    private MeshRenderer meshRenderer;

    // Use this for initialization
    void Start()
    {
        // Grab the mesh renderer that's on the same object as this script.
        meshRenderer = this.gameObject.GetComponentInChildren();
    }

    // Update is called once per frame
    void Update()
    {
        // Do a raycast into the world based on the user's
        // head position and orientation.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            // If the raycast hit a hologram...

            // Display the cursor mesh.
            meshRenderer.enabled = true;
            // Move the cursor to the point where the raycast hit.
            this.transform.position = hitInfo.point;
            // Rotate the cursor to hug the surface of the hologram.
            this.transform.rotation =
                Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
        }
        else
        {
            // If the raycast did not hit a hologram, hide the cursor mesh.
            meshRenderer.enabled = false;
        }
    }
}

2, 生成并导入Vuforia包,做相关设置

Hololens中的虚拟物体通过Vuforia的码实现虚实融合_第2张图片

Hololens中的虚拟物体通过Vuforia的码实现虚实融合_第3张图片

3, ARcamera的相关设置

Hololens中的虚拟物体通过Vuforia的码实现虚实融合_第4张图片

勾选自己的unitypackage,这是给hololens去发布

如果只是在电脑上看则

Hololens中的虚拟物体通过Vuforia的码实现虚实融合_第5张图片

3 放置的虚拟物体设置

Hololens中的虚拟物体通过Vuforia的码实现虚实融合_第6张图片

去除物体的父子关系

FixedPos.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.WSA;

public class fixedPos : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}


    public void OnSelect()
    {
        //添加空间锚点
        WorldAnchor anchor = this.gameObject.AddComponent();

        //移除父物体
        this.gameObject.transform.parent = null;

    }




}



4,功能有待完善,如果第一次放的位置不对,但已经通过手势将物体和码的位置关系脱离后就只能重新启动再标定。可以通过手势交互让物体和码恢复父子关系,然后重新调整。第一次解除,第二次恢复。

 

 

你可能感兴趣的:(VR/AR/MR/XR)