从零开始开发一款AR应用

Vuforia

什么是vuforia

  • 我没有自己看过它的概念,但是在使用的过程中它的作用实现图片、模型的识别,识别后就可以显示我们预设好的内容了。

怎么使用

  • 进入Vuforia,右上角注册。
  • 点击Develop进入下图

image

  • License Manager:管理你的授权,其实和其它一些SDK一样就是管理一下APP key(就是一串字符串)。在使用vuforia的SDK的时候需要用到。

  • Target Manager:target就是你扫描的图片或者模型或者其它什么的东西,当你扫描完成后如果匹配上了,就会在手机上显示相关的内容。

生成License

  • 点击add按钮→选择类型(我选的是Development,因为只是用来学习)→输入APP name→点击Confirm

  • 完成上述内容后你会得到下图的内容
    image

  • 点击ddd这个license,会进入下图。这个license key在程序开发中需要用到。
    image

生成Target

  • 选择Target Manager tab

  • 点击add database。其实这里用database是很准确的,因为生成的target就像一个数据库,而我们摄像头扫描到的内容就是数据,通过查询如果在DB中找到了相同的数据就认为是匹配到了。

  • 选择类型(我用的是Device),输入名字,点击Create。结果如下:
    image

  • 点击target(ddd)进入
    image

  • 点击add target。这个里面有多种target类型。我们选择最简单的single image。这个也是市面上很多AR应用使用的,也就是手机扫描一个图片,然后显示一个对应的模型。在这个页面中,file是选择你要上传的图片。width设置和场景中虚拟内容大小一致即可。

image

  • 输入名字然后点击add,下图是我之前设置好的一些target。
    image

-这里如果点击一个target,会进入下图。注意两个红色框,点击下面那个【Show Features】按钮后图片上会出现黄色区域,这个有点像做了个卷积识别色块的边缘。黄色部分越多说明在摄像头识别时越容易。而Augmentable指数对于了这个数值,也是越高越好。

image

  • add target页面有个download database按钮,可以选择要下载什么数据。注意对于target的理解完全可以用数据库的概念套入。你下载了什么数据,在应用中才能对下载数据进行使用和比对。点击download后出现
    image

  • 选unity editor,然后点击download按钮。会得到一个unitypackage文件,然后导入到工程中。

Unity部分的开发

导入vuforia sdk

  • 可以在vuforia官网下载unity对应的sdk,也可以通过asset store下载Vuforia AR Starter Kit来直接使用,我两个都试过在设置上是一样的。后者有很多自带的demo很不错。
  • 导入刚才获得的target package。
  • 从asset store上随便下载个免费的模型。

场景设置

  • 创建新场景
  • 找到ARCamera和ImageTarget的prefab拖入场景。

ARCamera设置

image

  • 点击ARCamera,再点击图中按钮进行设置。

image

  • 上图中第一个框就是之前申请的APP key
  • 第二个框是它们导入的target,我在这个demo中导入的target的名字就是【target_images】,注意这两个复选框一定要都选。

ImageTarget 设置

image

  • 红框部分Type需要选择图中的
  • db选择我们导入的target_images。如果是导入了Vuforia AR Starter Kit会有几个自带的target。
  • image target选择你要用的图片。在一个场景中可以有多个ImageTarget对象,不同的对象选择不同的图片即可。

image

  • 上图中的马就是要显示的模型,注意要作为ImageTarget的子对象。马下面的那个白色就是ImageTarget。
  • 对于要显示模型的位置和角度,只要记住手机摄像头面向的方向是白色方块的方向,只要要怎么显示模型就有了参考。我图中的设置是因为我要用摄像头扫描显示器上的图片,所以马是这样放置的。

测试

  • 如果你用的笔记本电脑带摄像头的,可以直接运行测试了。没有的,找个webcam就行了。

有用的事件代码

  • DefaultTrackableEventHandler类的OnTrackingFound函数是match到了图片后调用的函数,在这个里面可以做一些逻辑功能。

打包apk

生成keystore

  • build里面选择Android
  • 在player setting里面的publishing setting中可以生成keystore,方法看最后的附录。
  • 至于jdk和SDK都可以在网上下载到。NDK比较尴尬,unity在Android上的IL2CPP需要r10e,这个版本已经有点难下到了。我最后没用这个。

结束语

  • 到此为止一个最简单的AR应用就做完了,整个过程浪费时间在注册和下载Android SDK。

附录

keystore生成

The keystore is a signature file that is used to sign your apk. When you create development builds Unity will not sign the apk.

You only need one Keystore file for your projects. The file is your signature. To create one do this:

  • In the publishing settings check the “Create New Keystore” box.

  • Click browse and choose a location where you want to store the file. It should be somewhere central since it’s not really related to your project.

  • Set a protection password for the file and confirm it.

  • Select “Create a new key” from the “Key Alias” dropdown.

-PS:此处注意,选择了create a new key后最上面会自动勾选use existing keystore,无视即可。在弹出的窗口里面输入相关内容。

  • A window should open which you have to fill with your personal data about your organisation / person. The alias at the top is just a name of the keystore. If you have multiple identities make sure you choose a meaningful name. You need to set a keystore password. Note this is not the same as the protection password above. As far as i know they can be the same, but for security you should use two different. If you haven’t write the passwords onto a sheet of paper ;)

  • Click “Create Key” to generate your keystore file.

  • To use a keystore for signing you have to:

  • Check the “Use Existing Keystore” box.

  • Click browse and select your keystore file.

  • enter your protection key.

  • select your desired identity from the alias dropdown.

  • enter the alias password in the field below

  • Make sure you have set your Android Market Licensing RSA Public Key at the bottom of the publishing settings. The public key has to be acquired from your Google market account.

  • Now create your APK file and it will be signed with your identity.

Note: You can create multiple Keys / Identities in one Keystore file. The first protection password will protect the file itself. When you select an identity from the file in the alias dropdown you have to type in the password for the identity.

# VR/AR
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×