天天看點

Hololens中的虛拟物體通過Vuforia的碼實作虛實融合打開hololens像機

功能描述,通過碼将虛拟物體放置在合适的位置,然後Cursor和要放置的虛拟物體碰撞時,通過确定的手勢将虛拟的位置固定。

1,建一個表示Gaze的物體,碰撞檢測需要MeshCollider并挂腳本

Hololens中的虛拟物體通過Vuforia的碼實作虛實融合打開hololens像機

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<MeshRenderer>();
    }

    // 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的碼實作虛實融合打開hololens像機
Hololens中的虛拟物體通過Vuforia的碼實作虛實融合打開hololens像機

3, ARcamera的相關設定

Hololens中的虛拟物體通過Vuforia的碼實作虛實融合打開hololens像機

勾選自己的unitypackage,這是給hololens去釋出

如果隻是在電腦上看則

Hololens中的虛拟物體通過Vuforia的碼實作虛實融合打開hololens像機

3 放置的虛拟物體設定

Hololens中的虛拟物體通過Vuforia的碼實作虛實融合打開hololens像機

去除物體的父子關系

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<WorldAnchor>();

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

    }




}



           

4,功能有待完善,如果第一次放的位置不對,但已經通過手勢将物體和碼的位置關系脫離後就隻能重新啟動再标定。可以通過手勢互動讓物體和碼恢複父子關系,然後重新調整。第一次解除,第二次恢複。

5, 這樣操作以後,Hololens的像機就會被vuforia占用,如果像在網頁上打開可以hololens使用者看到的就不行。這時可以通過定位後将vuforia與關閉實作

對上面的代碼稍作修改

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

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<WorldAnchor>();

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

        GameObject.Find("ARCamera").GetComponent<VuforiaBehaviour>().enabled = false;


    }




}