• U3D 高通AR


     

    1,高通AR流程

    HiAR_各种功能案例:

     HiAR SDK:Unity Package包形式:http://pan.baidu.com/s/1jItIyjW

    AR  脱卡(小明离开背景图片时也会显示出来)

    脚本挂ImageTarget上
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class OutOfCardScript : ImageTargetBehaviour
    {
    
        // 脱卡的物体
        public GameObject Target;
        //记录脱卡时距离摄像机的远近
        Vector3 camPos = new Vector3 (0, 0, 15);
        //记录开始的旋转角度
        Quaternion rot;
    
        void Start ()
        {
            base.Start ();
            //获取到脱卡的物体Target
            Target = transform.Find ("xiaoliang_model").gameObject;
            //记录开始的旋转
            rot = Target.transform.localRotation;
            //设置模型隐藏
            Target.SetActive (false);
        }
    
        //重写父类的方法
        public override void OnTargetFound (RecoResult recoResult)
        {
            base.OnTargetFound (recoResult);
            //Debug.Log ("找到目标对象1111");
        }
        //追踪目标对象
        public override void OnTargetTracked (RecoResult recoResult, Matrix4x4 pose)
        {
            base.OnTargetTracked (recoResult, pose);
            //如果已经识别,就设置当前模型目标为ImageTarget的子物体
            //并且设置相关坐标
            if (Target.activeSelf) {
                //设置父物体
                Target.transform.parent = transform;
                //设置位置
                Target.transform.localPosition = Vector3.zero;
                //设置旋转
                Target.transform.localRotation = rot;
            }
        }
        //目标对象丢失
        public override  void OnTargetLost (RecoResult recoResult)
        {
            //Debug.Log ("目标对象丢失");
            base.OnTargetLost (recoResult);
            //设置相关坐标
            Target.SetActive (true);
            Target.transform.parent = Camera.main.transform;
            Target.transform.localPosition = camPos;
            Target.transform.localRotation = rot;
        }
    }

    AR 动态加载(点击按钮加载不同的物体)

    _______________
    这个脚本不用挂
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System .IO;
    
    public class AndroidPathScript : MonoBehaviour
    {
    
        public     IEnumerator SetAndroidPath (string keyId, string fullPath)
        {
            //预编译
            #if UNITY_ANDROID 
    
            WWW www = new WWW (Application.streamingAssetsPath + keyId);
            yield return www;
            //判断安卓运行环境下是否存在该文件
            if (!Directory.Exists (Application.persistentDataPath + "/Data")) {
                //如果不存在,创建这样一个路径
                Directory.CreateDirectory (Application.persistentDataPath + "/Data");
            }
            //将当前这个文件写入这个路径下
            File.WriteAllBytes (Application.persistentDataPath + "/Data/" + keyId, www.bytes);
            #endif 
        }
    
        void Start ()
        {
            StartCoroutine (SetAndroidPath ("1d547122f7788bce3766d34065e7f4b9.db", "/HiAR/picture_01/1d547122f7788bce3766d34065e7f4b9.db"));
            StartCoroutine (SetAndroidPath ("2799d80c498feb62a8876bffb011b034.db", "/HiAR/sample/2799d80c498feb62a8876bffb011b034.db"));
        }
    }

    AR 点击按钮动态加载视频

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System .IO;
    using hiscene;
    using UnityEngine.UI;
    using UnityEngine.Networking.Types;
    using UnityEngine.Video;
    
    
    public class DynamicLoadVideoScript : TartgetDynamicBehaviour
    {
        //播放视频的材质
        public Material videoMate;
    
        public override void OnDynamicReco (RecoResult recoResult)
        {
            //创建一个空物体
            GameObject gameObject = null;
            gameObject = new GameObject ();
            //当前动态识别的是图片的时候
            if (recoResult.keyType == KeyType.IMAGE) {
                //给空物体添加扫描图片的所需要的组件
                gameObject.AddComponent <ImageTargetBehaviour> ();
            }
            //获取识别目标
            Target target = gameObject.GetComponent <Target > ();
            target.PixelWidth = recoResult.Width * 0.01f;
            target.PixelHeight = recoResult.Height * 0.01f;
            gameObject.transform.parent = transform.parent;
            gameObject.SetActive (true);
    
    
            //开始加载视频
            StartCoroutine (Loading ());
            //判断当前识别的目标卡片是否是播放视频的卡片
            if (recoResult.KeyId.Equals ("1d547122f7788bce3766d34065e7f4b9")) {
                //创建视频
                string path = null;
                #if UNITY_EDITOR
                path = Application.streamingAssetsPath + "/video.mp4";
                #elif  UNITY_ANDROID 
                path =Application.persistentDataPath + "/Data/video.mp4";
                #endif
                GameObject video = CreateVideoPlayerAction (path, recoResult.Width, recoResult.Height);
                //将video设置为目标识别物体(ImageTarget)的子物体
                video.transform.parent = gameObject.transform;
            }
            //绑定ImageTarget和识别目标的KeyID;
            bindingGameObject (gameObject, recoResult.KeyId);
        }
    
        //加载视频的协程方法
        IEnumerator Loading ()
        {
            #if UNITY_ANDROID 
            //通过www找到视频资源
            WWW www = new WWW (Application.streamingAssetsPath + "/video.mp4");
            yield return www;
    
            //判断安卓运行环境下是否存在Data的路径
            if (!Directory.Exists (Application.persistentDataPath + "/Data")) {
                //创建这个路径
                Directory.CreateDirectory (Application.persistentDataPath + "/Data");
            }
            //将视频文件写入安卓的可读取路径
            File.WriteAllBytes (Application.persistentDataPath + "/Data/video.mp4", www.bytes);
            #endif
    
    
        }
    
    
        //创建视频播放的方法
        GameObject CreateVideoPlayerAction (string videoPath, int imgWidth, int imgHeight)
        {
            //创建一个Plane
            GameObject videObject = GameObject.CreatePrimitive (PrimitiveType.Plane);
            //设置名字
            videObject.name = "VideoPlayer";
            //设置缩放
            videObject.transform.localScale = new Vector3 (imgWidth * 0.001f, 0, imgHeight * 0.001f);
            //设置为隐藏状态
            videObject.SetActive (false);
            //添加视频播放的脚本组件
            videObject.AddComponent <VideoPlayerBehaviour> ();
    
            //设置视频播放的材质
            videObject.GetComponent <MeshRenderer > ().material = videoMate;
    
            //获取脚本组件,添加播放资源
            VideoPlayerBehaviour videoPlayer =    videObject.GetComponent <VideoPlayerBehaviour> ();
            //播放路径设置
            videoPlayer.AbsolutePath = videoPath;
            //是否是本地视频资源
            videoPlayer.IsLocal = true;
            //是否透明
            videoPlayer.IsTransparent = true;
            //设置可见
            videObject.SetActive (true);
            //返回视频播放对象
            return videObject;
        }
    
        void OnGUI ()
        {
            if (GUI.Button (new Rect (30, 30, 220, 100), "播放视频")) {
                #if UNITY_EDITOR
                AddImageTarget (Application.streamingAssetsPath + "/HiAR/picture_01", "1d547122f7788bce3766d34065e7f4b9", 854, 480);
                #elif UNITY_ANDROID 
                AddImageTarget (Application .persistentDataPath  +"/Data","1d547122f7788bce3766d34065e7f4b9",854,480);
                #endif
            }
        }
    }

    AR 获取系统摄像机

    效果:虚体物体和现实相融合

    修改Image的透明度

    Canvas身上挂脚本,另一个设置如下

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine .UI;
    using UnityEngine.Networking.Types;
    
    public class SystemCameraScript : MonoBehaviour
    {
        //展示摄像头内容的图片
        public Image image;
    
        void Start ()
        {
            //获取摄像头
            StartCoroutine (RequestCamera ());
        }
    
        IEnumerator RequestCamera ()
        {
            //协程返回用户权限
        
            yield return Application.RequestUserAuthorization (UserAuthorization.WebCam);
            //如果用户有权限
            if (Application.HasUserAuthorization (UserAuthorization.WebCam)) {
                //获取所有的摄像头设备
                WebCamDevice[] deveices = WebCamTexture.devices;
                //定义一个记录摄像头的编号
                int index = -1;
                for (int i = 0; i < deveices.Length; i++) {
                    if (deveices [i].isFrontFacing) {
                        index = i;
                        break;
                    }
                }
                //根据摄像头名字,创建一个摄像机图像,展示到UI界面上
                WebCamTexture texture = new WebCamTexture (deveices [index].name);
                image.canvasRenderer.SetTexture (texture);
                //启动摄像头
                texture.Play ();
            }
        }
    }

    AR 获得系统的麦克风(录音)

    创建一个空物体起名Microphone,身上挂一下一个脚本和一个Audio Source

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GetSystemMicrophoneScript : MonoBehaviour
    {
    
        // 存放声音的声音片段
        AudioClip clip;
        AudioSource audioSource;
    
        void Start ()
        {
            audioSource = GetComponent <AudioSource > ();
        }
        
        // 开始录音
        void StartRecord ()
        {
            //null:表示系统默认的麦克风,
            //false:是否循环录制 ,
            //30:录制的时间长短,
            //8000:声音的频率
            clip = Microphone.Start (null, false, 30, 8000);
        }
        //停止录音
        void StopRecord ()
        {
            Microphone.End (null);
            //播放声音
            audioSource.PlayOneShot (clip);
        }
    
        void Update ()
        {
            if (Input.GetKeyDown (KeyCode.Space)) {
                StartRecord ();
            }
            if (Input.GetKeyUp (KeyCode.Space)) {
                StopRecord ();
            }
        }
    }

    AR   小球在图片上跳动

    创建一个物理材质,设置如下,挂在Image Target上和小球上

     

     在小球上添加Rigidbody,和物理材质

     在Image Target上添加Mesh Collider

  • 相关阅读:
    【Gamma】 Phylab 展示博客
    【技术博客】Postman接口测试教程
    【技术博客】利用Python将markdown文档转为html文档
    【技术博客】使用PhpStorm和Xdebug实现Laravel工程的远程开发及调试
    【技术博客】Laravel5.1文件上传单元测试
    【技术博客】移动端的点击事件与Sticky Hover问题
    【技术博客】 Laravel 5.1单元测试(PHPUnit)入门
    Scrum Meeting博客目录
    团队事后分析
    Gamma阶段测试报告
  • 原文地址:https://www.cnblogs.com/zpy1993-09/p/13193909.html
Copyright © 2020-2023  润新知