AR 从入门到精通开发系列教程 (4)--- 脱卡功能总结

1,242 阅读3分钟

                  使用EasyAR SDK 来开发好玩的 AR 应用

脱卡是目前AR 应用开发中最普遍的功能,因此我今天给大家借助EasyAR  SDK介绍两种最普遍的脱卡功能。

第一部分:前言

关注AR 酱(发现闪闪发光的AR从业者|增强现实|混合现实|虚拟现实):



EasyAR 开发者:605785368


第二部分:实现脱卡第一种方案

思路:我们知道的,当所识别的卡片离开摄像头时,EasyImageTargetBehaviour  这段脚本中的Image的SetActive为false,因此其子物也就不显示了,因此解决的办法就是在Target (false)时间将模型放到一个合适的位置,这样就能实现脱卡,当Target (true)时,再回到原来位置。

1.首先在EasyAR下面的Augmenter下建设一个空物体,用来存放脱卡后的模型命名cube,这个位置的模型不会移动,永远都在屏幕固定位置。然后在cube下面建一个空物体用来保存模型的最佳位置和角度。(具体位置与角度大家根据自己的要求自行调节)



2.然后在ImageTarget的下面建一个空物体用来保存target(名字随便起)出现时间的位置和角度。



3.建一个空物体并挂上这段脚本(记住:这个空物体千万不要建到ImageTarget 下,否则脱卡功能会失效)


using UnityEngine;

using System.Collections;

public class ZSetactive : MonoBehaviour

{

public Transform GreatTransfrom;                  //脱卡后最佳位置

public GameObject cubePos;                              //模型脱卡时存放位置

public Transform[] TargetTransfrom;              //模型在卡片上的最佳位置

public GameObject[] Target;                          //卡片

public GameObject[] cube;                            //模型

void Start ()

{

for (int i = 0; i < cube.Length; i++) {          //所有模型初始化全部不显示

cube [i].SetActive (false);

}

}

public void tiaozheng ()                              //模型倾斜时调整最佳位置

{

GreatTransfrom.localPosition = new Vector3 (0f, 0f, 0f);

GreatTransfrom.localRotation = Quaternion.identity;

for (int i = 0; i < cube.Length; i++) {

cube [i].transform.localPosition = GreatTransfrom.localPosition;

cube [i].transform.localRotation = GreatTransfrom.localRotation;

}

}

void Update ()

{

WhoShouldShow ();                            //哪个模型应该显示

TargetT ();                                          //有卡片时

TargetF ();                                        //无卡片时

}

int index = -1;

void WhoShouldShow ()                      //哪个模型应该显示

{

for (int i = 0; i < Target.Length; i++) {

if (Target [i].activeSelf == true) {

cube [i].SetActive (true);

index = i;

}

if (i != index) {

cube [i].SetActive (false);

}

}

}

void TargetT ()                                    //不脱卡

{

for (int i = 0; i < Target.Length; i++) {

if (Target [i].activeSelf == true) {

cube [i].transform.parent = Target [i].transform;

cube [i].transform.position = TargetTransfrom [i].position;

}

}

}

void TargetF ()                                    //脱卡

{

for (int i = 0; i < Target.Length; i++) {

if (Target [i].activeSelf == false) {

cube [i].transform.parent = zhenf.transform;

cube [i].transform.localPosition = GreatTransfrom.localPosition;

}

}

}

}



第三部分:实现脱卡第二种方案

思路:大致思路就是,托卡后将模型的父类设为ARCamera下的一个游戏物体,并调整好位置。

1.EasyAR下的Augmenter下也添加一个模型(与ImageTarget 模型一样),并设置这个模型在你相机下的位置大小各种参数。



using UnityEngine;

using System.Collections;

public class Tuoka : MonoBehaviour

{

public GameObject Target;//卡片

public GameObject zhen;//识别图上的模型

public GameObject zhen2;//相机下的模型

bool firstFound = false;//是否是第一次识别

void Start()

{

zhen.SetActive(false);

zhen2.SetActive(false);

}

void Update()

{

if (Target.activeSelf == true)

{

zhen.SetActive(true);

zhen2.SetActive(false);//不显示脱卡状态的模型

zhen2.transform.GetComponent().Stop("Take 001");//停止脱卡状态下模型的动画

zhen.GetComponent().Stop("Take 001");//识别图上的模型不播放动画

firstFound = true;

}

if (Target.activeSelf == false && firstFound == true)

{

zhen.SetActive(false);

zhen2.SetActive(true);//显示模型

zhen2.transform.GetComponent().Play("Take 001");

}

}

}