10491人加入学习
(62人评价)
AR系列教程 - Vuforia入门 ( Unity 2017.3 )

制作完成于2018-01-16

价格 免费

TurnOffBehaviour.cs
获得摄像机后,销毁Mesh Renderer组件、Mesh Filter组件,使ImageTarget不显现。

 

Default Trackable Event Handle.cs

OnTrackingFound()

//找到了需要识别的物体,调用此方法

{//获得Renderer渲染组件、Collider组件、Canvas画布组件,都设成可用

}

OnTtrackingLost()

//丢失了需要识别的物体,调用此方法

{//获得上述组件设成不可用

}

OnTrackaleStateChanged()

//检测到追踪的状态改变,调用此方法

{//当前状态是检测到新东西、追踪、拓展追踪。即识别到图片,调用OnTrackingFound()

//当前状态是丢失。调用OnTtrackingLost()
}

Start()

 

[展开全文]

更改摄像机的角度,Align with view 与视图对齐

不想调用摄像头Build Settings中的Inspector面板取消勾选Vuforia Augmented Realit,弹出框直接关闭。

[展开全文]

复制一份Defaulte.cs文件,修改名称,打开文件,命名空间的名字要同文件名一致

如果运行后一直出现空物体,就在ARCamera的面板下打开Open Vuforia configuration,关闭其他的数据库。

[展开全文]

在ImageTarget上添加Audio Source组件,将要播放的音乐拖入AudioClip
脚本中添加

private AudioSource audio;

public AudioClip welcomeClip;

Start(){audio=this.GetComponent<AudioSource>();
Invoke("PlayClipAtPoint",6.0f);}

OnTrackingFound()

{if(!audio.isPlaying){audio.Play();}}

private void PlayWelcomeClip(){
AudioSource.PlayClipAtPoint(welcomeClip,transform.position);}

[展开全文]

Update(){
if(Input.GetMouseButtonDown(0)){//如果触摸屏幕
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机到屏幕发送射线到点击的位置
RaycastHit hitInfo;//接收发送的信息
if(Physics.Raycast(ray,out hitInfo)){//如果射到东西,有Capsule Collider组件的东西才能被检测到
if(Input.touchCount==1&&Input.GetTouch(0).phase==TouchPhase.Began)//几根手指碰到屏幕,并且是刚开始触摸到屏幕
{//if(Input..GetTouch(0).taopCount==2)//双击
Destory(hitInfo.collider.gameObject);}
}此脚本挂到ARCamera上

发布到手机
Player Setting-Package Name改个名字,
Android TV Compatibility取消勾选。Build
 

[展开全文]

private float touchTime;//记录长按的时间

private boll newTouch;

if(Input.GetMouseButton(0))//如果长按{
  Ray ray=Camera.main.ScreenPointToRay(InputmousePosition};
RaycastHit hitInfo;
if(PhysicsRaycasr(ray,out hitInfo)){
   if(Input.touchCount==1){
Touch touch=Input.GetTouch(0);
if(touch.phase==Began){newTouch=true;touchTime=Time.time;}
else if(touch.phase==TouchPhase.Stationary)//按住没有动
if(newTouch=true&&Time.time-touchTime>1f)//认为是长按
newTouch=false;
Destory(hitInfo.collider.gameObject);}
else{neTouch=false;}}

[展开全文]

手机上的模型大小比在电脑上观察上的小。

动态生成的模型大小是ImageTarget大小*模型大小

float xSpeed=150.0f//横屏滑动

Update(){
if(Input.GetMouseButton(0)){
if(Input.touchCount==1){
if(Input.GetTouch(0).phase==TouchPhase.Move)//滑动{
transform.Rotate(Vector3.up*Input.GetAxis("Mouse X")*-xSpeed*Time.deltaTime,Space.World}}}
}
 

[展开全文]
public class Enlarge : MonoBehaviour {
    //左手手指
    Vector2 oldPos1;
    //右手手指
    Vector2 oldPos2;
    // Use this for initialization
    void Start() {
    } 
    // Update is called once per frame
    void Update() {
        if(Input.touchCount == 2){
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
//现在手指所在坐标
                Vector2 temPos1 = Input.GetTouch(0).position;
                Vector2 temPos2 = Input.GetTouch(1).position;

 

                if (isEnlarge(oldPos1, oldPos2, temPos1, temPos2))
                {
//放大
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale * 1.025f;
                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                else
                {
//缩小
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale / 1.025f;
                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                oldPos1 = temPos1;
                oldPos2 = temPos2;
            }
        }
    }

 

 
 

 

    //判断手势

 

    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
    float length1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float length2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (length1 < length2) {
            return true;
        }
        else { return false; }
    }
}

 

 
 
[展开全文]

1.新建screenshots 的脚本文件,挂到Canvas上面。

//下述为整个截屏代码

 

using System.Collections;

using System.Collections.Generic;
using UnityEngine;
using System .IO ;
public class ScreenShot : MonoBehaviour {

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

     public void OnScreenShotClick()
    {
        //获取当前系统的时间
        System .DateTime now = System.DateTime.Now;
        //将时间转换为字符串
        string  times=now .ToString ();
        times = times.Trim ();
        //定义格式
        times = times.Replace ("/", "/");
        string  fileName="ARScreenShot"+times +".png";

        //截屏功能
        if (Application.platform == RuntimePlatform.Android) {
            //把屏幕截图到一张贴图上
            Texture2D texture = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);
            //读取贴图
            texture .ReadPixels (new Rect (0,0,Screen .width ,Screen .height ),0,0);
            //应用
            texture .Apply ();


            //自定义存储
            byte[] bytes=texture .EncodeToPNG ();

            string destination="/sdcard/DCIM/screenshots";
            //判断目录是否存在
            if (!Directory .Exists (destination ))
            {
                //创造目录
                Directory.CreateDirectory (destination);
        }
            //保存路径
            string pathSave = destination + "/" + fileName;

            //转换好的数组放到这个路径下面
            File.WriteAllBytes (pathSave,bytes);
    }
}
}

2.在button下面的ON click()里面选中canvas组件,并选中此组件中的onscreenshot()方法。

3.下述不带Ui的方法是

先调用unity的UI界面

using UnityEngine.UI ; 

 

//隐藏ui
            this.GetComponentInChildren <Image >().enabled =false ;
            this.GetComponentInChildren <Text >().enabled =false ;

    //显示ui
            this.GetComponentInChildren <Image >().enabled =true  ;
            this.GetComponentInChildren <Text >().enabled =true;

[展开全文]

1.用上一集的方法是不成功的。

   因为ARcamera 渲染所有的物体,ui也不例外。所以之前的操作都是无效的。

解决方法是新建一个摄像机,只渲染UI 。

让ARcamera渲染其它的物体。

将uicamera 的projection设置为Orthographic

将clear flags 改成depth only

将depth改成2

2.将canvas里面的render mode改成screen space-camera,调用的是UIcamera。

3.代码修改

 

 

[展开全文]

1.新建一个camera setting的脚本文件。

2.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//用到了Vuforia的命名空间
namespace  Vuforia{
public class CameraSetting : MonoBehaviour {
        //编写回调函数
    // Use this for initialization
    void Start () {
        //先找到AR controller
            var vuforia=VuforiaARController .Instance ;

            vuforia.RegisterVuforiaStartedCallback (OnVuforiaStarted);
            vuforia.RegisterOnPauseCallback (Onpaused);
    }
    
    // Update is called once per frame
    void Update () {
        
    }
        //程序启动时
        private  void OnVuforiaStarted(){
        //程序一启动就自动对焦,调用vuforia的api组件。
            CameraDevice .Instance.SetFocusMode  (CameraDevice.FocusMode.FOCUS_MODE_MACRO );

        }

        //程序暂停时
            private void Onpaused(bool isPaused{
        
        }

        public void  OnFocusModeClick(){
            //手动对焦,点一下对焦一下。
            CameraDevice.Instance.SetFocusMode (CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);
        }
}
}

[展开全文]

renderTexture纹理贴图 

[展开全文]

1.在camerasetting里面添加摄像机前后切换及闪光灯的开启。


        //前后摄像头切换
        public  void  SwitchCameraDirection(){
            CameraDevice.Instance.Stop ();
            CameraDevice.Instance.Deinit ();
            //转换为前置摄像头
            CameraDevice.Instance.Init (CameraDevice.CameraDirection.CAMERA_FRONT);
            CameraDevice.Instance.Start ();
        }

        //打开闪光灯
        public void FlashTourch(bool ON){
            CameraDevice.Instance.SetFlashTorchMode (ON);
        }

 

[展开全文]

Vuforia.TurnOffBehaviour函数

VuforiaRuntimeUtilities.IsVuforiaEnabled()是否获取了设备上摄像头的连接

Default Trackable Event Hander(Script)函数

OnTrackingFound找到了需要识别的物体

OnTrackingLost丢失了需要识别的物体

OnTrableStateChanged当状态改变

 

[展开全文]

ARcamera

World Center Mode:世界中心模式

FirstTarget:第一个照射到的目标为世界中心的坐标;

DEVICE TRACKING:以设备正在追踪的物体为世界中心的坐标

[展开全文]
  1. 选择评分四星级以上的图片上传服务器和数据库
  2. 添加需要识别的目标
  3. single image 图片,记得设置大小
  4. 完成图片识别的条件: 
  • 调用AR camera
  • 创建vuforia image

 

 

 

 

[展开全文]

ARCamera

1. 调用设备摄像机

2. behavior接口

 

[展开全文]

image target下三个脚本:

1、image target behavior:对数据库中的图片进行设置

2、turn off behavior:

命名空间为Vuforia,想要调用其API时,需要引入此命名空间。

其中只有一个Awake函数:

if判断是否获取了设备上的摄像头

meshrenderer、meshfilter组件作用:用来显示,销毁后不显示。当点击运行程序时,meshrenderer、meshfilter消失,所以不会在scene场景中显示targrt,但是物体还存在,只是不再显示。

3、defalut脚本下的两个函数:

状态改变函数:

  • OntrackingFound意思:当找到了需要识别的物体,调用此方法
  • OntrackingLost意思:丢失了需要识别的物体,调用此方法

 

 

[展开全文]

ImageTarget:识别图片

官网创建数据库、导入图片,然后下载

ImageTarget的Database勾选需要的数据库和图片

ARCamera的open vuforia configura下勾选前述database并设置active

[展开全文]

授课教师

SIKI学院老师

课程特色

图文(1)
下载资料(1)
视频(24)

学员动态