图片选择:1.灰度 2.特征点
图片选择:1.灰度 2.特征点
AR camera有两个作用,一个调用相机,了另一个使用vuforia behaviour脚本
world center mode
vuforia configuration -camera direction
前置摄像头/后置摄像头
图片需要先上传到vuforia数据库才能被识别
世界中心模式
选中specific时,以我么指定的一个作为世界中心的坐标
选中first target时,以摄像机照射到的第一个需要识别的目标为世界中心的坐标
选中camera时,以摄像机为世界中心坐标
选中device时,以设备正在追踪的物体为世界中心的坐标
如果A>b A=10,那么X=N+1
public GameObject Appearance SnowMiku-2010Prefab;
//找到了
GameObject Appearance SnowMiku-2010 = GameObject.INstantiate(Appearance SnowMiku-2010Prefab);
Appearance SnowMiku-2010.transform.position = this.transform.position;
Appearance SnowMiku-2010.transform.parent = this.transfoem;
//丢失了
Destroy(GameObject.Find(Appearance SnowMiku-2010);
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);
}
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);
}
}
}
1.用上一集的方法是不成功的。
因为ARcamera 渲染所有的物体,ui也不例外。所以之前的操作都是无效的。
解决方法是新建一个摄像机,只渲染UI 。
让ARcamera渲染其它的物体。
将uicamera 的projection设置为Orthographic
将clear flags 改成depth only
将depth改成2
2.将canvas里面的render mode改成screen space-camera,调用的是UIcamera。
3.代码修改
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;
动态生成时,imagetarget的大小会对生成的物体产生
1.新建一个rotate脚本。
2.public class PlayerRotate : MonoBehaviour {
float xSpeed = 150.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0)) {
if(Input.touchCount == 1){
if (Input.GetTouch(0).phase == TouchPhase.Moved) {
transform.Rotate(Vector3.up *Input .GetAxis ("Mouse X")*-xSpeed*Time .deltaTime ,Space .World);
}
}
}
}
}
1.要判断长按,需要定义一个变量来记录按的时间
private float touchTime;
2.定义一个变量,判断是不是新按上去的
private bool newTouch=false;
3.在update里面编写功能。
void Update () {
if (Input.GetMouseButtonDown (0)) {
//发射一条射线,位置是点击到的位置
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//用来接收返回回来的信息
RaycastHit hitInfo;
//如果接收到了返回来的信息
if (Physics.Raycast (ray, out hitInfo)) {
//touchCount是指有几个手指碰到屏幕,后面的是指第一次触摸
if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Began) {
if (Input.GetTouch (0).tapCount == 2)
Destroy (hitInfo.collider.gameObject);
}
}
}
}
}
1.先给imagetarget添加一个audiosourse组件,将BGM放到这个组件之中。
2.在脚本中先定义两个音频private AudioSource audio;
private Audio welcomeCilp;
3.在start里面获得audiosourse组件,然后在动画播放六秒后播放欢迎语句。
audio = this.GetComponent <AudioSource > ();
Invoke ("PlaywelcomeCilp", 6.0f);
4.在found中添加播放BGM的代码。
if (!audio.isPlaying) {
audio.Play ();
}
5.添加一个播放欢迎语句的方法PlaywelcomeClip()然后在start里面调用这个方法。
private void PlaywelcomeClip(){
AudioSource.PlayClipAtPoint (welcomeCilp, transform.position);
}
阴影制作
1.将摄像头放置于图片上面,能捕捉到图片。
2.建立texture文件夹,在文件夹里面新建Render texture。并将此添加到主相机的target Texture。(这时的render texture的纹路即为主摄像机所见内容)。将main camera 里面的Clear Flags设置为solid color。
3.新建plane,将render texture作为贴图拖放置于上。
选中plane-ARshadow-legacy shadow-transparent-diffuse。(只留下了想要的纹理)
4.将aixi放到摄像机下面,做艾希的阴影。
先设置aixi 的渲染优先级。(第一步在aixi的layer组件中设置8-aixi、第二部在main camera里面的culling mask里面先选nothing,再选aixi)
然后调整
Create \ RenderTexture
放入MainCamera的TargetTexture
将MainCamera对着物体;
新建Plane,将RenderTexture赋给Plane;
Camera的ClearFlags改为:SolidColor
Plane的Shadow改为LegacyShaders\Transparent\Diffuse
给要显示的物体指定层级,修改Camera渲染的层级:CullingMask:改为指定层级
改变摄像机的位置,获得不同位置照射的物体,作为阴影;
将Plane和Camera 都放在物体下,物体放在ImageTarget下;
transform.Translate (new Vector3 (0f, 0.7f, 0f) * Time.deltaTime);
//用来进行缓慢移动的操作
//生成初始位置时往y下移1.6
GameObject Aixi = GameObject.Instantiate (AixiPrefab,transform .position -new Vector3 (0f,1.6f,0f), transform.rotation );
// Aixi.transform.position = this.transform.position;
Aixi.transform.parent = this.transform;
//判断艾希是否已经到达纸面上
if (transform.localPosition.y > 0) {
return;
}
将特效加入的方法和生成寒冰特效一摸一样。
生成代码可以参考如下:
//生成血池
GameObject e1 = GameObject.Instantiate (bloodprefab ,transform .position ,Quaternion .identity );
// bloodprefab .transform.position = this.transform.position;
e1 .transform.parent = this.transform;
定时销毁代码:Destroy(e1,10.0f);
放于生成
ARCamera:负责真实世界的显示 = 调用设备的摄像机
GameObject Aixi = GameObject.Instantiate (AixiPrefab );
Aixi.transform.position = this.transform.position;
Aixi.transform.parent = this.transform;
在图片表面生成物体
Destroy (GameObject.Find ("Aixi(Clone)"));
销毁生成的