美文网首页
AR开发实战Vuforia项目之房地产(AR与VR混合开发)

AR开发实战Vuforia项目之房地产(AR与VR混合开发)

作者: TonyWan_AR | 来源:发表于2018-02-11 10:44 被阅读1357次

一、框架视图

二、关键代码

CameraMode

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;


/// <summary>
/// 自动对焦功能
/// </summary>
public class CameraMode : MonoBehaviour
{


    void Start()
    {
        //一开始自动对焦
        //Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
        VuforiaARController.Instance.RegisterOnPauseCallback(OnPaused);
    }

    void Update()
    {
        //触碰的时候对焦
        //if (Input.GetMouseButtonUp(0))
        //{
        //    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        //    {
        //        Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        //    }
        //}

    }
    private void OnVuforiaStarted()
    {
        CameraDevice.Instance.SetFocusMode(
        CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }

    private void OnPaused(bool paused)
    {
        if (!paused)
        { // resumed
            // Set again autofocus mode when app is resumed
            CameraDevice.Instance.SetFocusMode(
            CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        }
    }

}

IntoOrOutside_Button

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
//引用场景管理器命名空间

public class IntoOrOutside_Button : MonoBehaviour {

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

    //进入房间
    public void In_Button(){
        SceneManager.LoadScene ("IntoHouse");
    }


    //退出房间
    public void Out_Button()
    {
        SceneManager.LoadScene("Main");
    }


    public void Vr_Button() {
        SceneManager.LoadScene("vr1");
    }

}

Load_Mark

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Load_Mark : MonoBehaviour {

    public Texture[] Load_Textures;
    //申请贴图类型的数组储存每一张进度条图片

    public float Load_Speed = 0.2f;
    //圆形进度条每张图片切换的时间 进度条的读取速度

    public float Load_Time;
    //记录进度条所使用的时间

    public int Load_Count;
    //记录当前进度条该播放哪张图片

    public Texture Alpha;

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

        if(Load_Count>16){
        //当图片超过16张时 
            SceneManager.LoadScene ("vr2");
            //跳转到场景二
        }

        Load_Time += Time.deltaTime;
        //每一帧增加 帧所使用的时间  即进度条经过的时间

        Load_Count = (int)Mathf.Floor (Load_Time / Load_Speed);
        //计算出当前应该播放哪张图片 用所经过的时间除以每张图片切换的速度
        //Mathf.Floor() 向下取正
        if (Load_Count<16)
        {
            gameObject.GetComponent<Renderer>().material.mainTexture = Load_Textures[Load_Count];
            //把当前所对应的进度条图片显示出来
        }




    }

    // OnDisable函数 在该脚本被取消的时候会执行一次
    void OnDisable(){
        Load_Count = 0;
        Load_Time = 0;
        gameObject.GetComponent<Renderer> ().material.mainTexture = Alpha;  
    }


}

MobileGyro

using UnityEngine;
using System.Collections;
//摄像机  陀螺仪转动
public class MobileGyro : MonoBehaviour
{
    Gyroscope gyro;
    Quaternion quatMult;
    Quaternion quatMap;
    GameObject player;
    GameObject camParent;
    void Awake()
    {
        player = GameObject.Find("Player");
        // find the current parent of the camera's transform
        Transform currentParent = transform.parent;
        // instantiate a new transform
        camParent = new GameObject("camParent");
        // match the transform to the camera position
        camParent.transform.position = transform.position;
        // make the new transform the parent of the camera transform
        transform.parent = camParent.transform;
        // make the original parent the grandparent of the camera transform
        //camParent.transform.parent = currentParent;
        // instantiate a new transform
        GameObject camGrandparent = new GameObject("camGrandParent");
        // match the transform to the camera position
        camGrandparent.transform.position = transform.position;
        // make the new transform the parent of the camera transform
        camParent.transform.parent = camGrandparent.transform;
        // make the original parent the grandparent of the camera transform
        camGrandparent.transform.parent = currentParent;

        Input.gyro.enabled=true;
        gyro = Input.gyro;

        gyro.enabled = true;
        //camParent.transform.eulerAngles = new Vector3(90, 0, 0);
        camParent.transform.eulerAngles = new Vector3(90,-135, 0);
       //camParent.transform.eulerAngles = new Vector3(90, 0, 180);
        //quatMult = new Quaternion(0, 0, 1, 0);
        quatMult = new Quaternion(0, 0, 1, 0);
    }

    void Update()
    {

        quatMap = new Quaternion(gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
        Quaternion qt=quatMap * quatMult;

        transform.localRotation =qt;

    }

}

Rotate

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour {

    //记录上一帧手指的位置;
    private Vector3 PreviousPosition;

    //记录手指的偏移值;
    private Vector3 Offset;

    void Start () {
        
    }
    

    void Update () {
        //判断是否触屏;
        if (Input.GetMouseButtonDown(0))
        {
            //记录手指的偏移值
            Offset = Input.mousePosition - PreviousPosition;
            //记录上一帧的手指的位置;
            PreviousPosition = Input.mousePosition;
            //通过手指的偏移值来控制物体的旋转;
            transform.Rotate(Vector3.Cross(Offset*0.01f,Vector3.up).normalized,Offset.magnitude,Space.Self);
        }
        
    }
}

SRay

using UnityEngine;
using System.Collections;

public class SRay : MonoBehaviour {

    public RaycastHit HitInfo;
    public Transform HitPoint;
    public GameObject LoadPlane;

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

        Debug.Log (HitInfo.collider.gameObject.name);
    }


    void SendRay() {
        
        if (Physics.Raycast (HitPoint.position, HitPoint.forward, out HitInfo)) {
            

            if (HitInfo.collider.gameObject.tag == "A") {
                Debug.Log("111");
                LoadPlane.GetComponent<Load_Mark> ().enabled = true;
                //如果射线撞击的位置标签是否为“face” 则

            }else{   
                Debug.Log("222");
                LoadPlane.GetComponent<Load_Mark> ().enabled = false;

            }



        }
    }
}

SX

using UnityEngine;
using System.Collections;

public class SX : MonoBehaviour {

    public GameObject Load_Obj;
    //储存附有进度条脚本的物体
    private RaycastHit HitIfo;
    //申请变量记录碰撞的信息


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

    //发射射线的函数
    public void SendRay(){
        if (Physics.Raycast (transform.position, transform.forward, out HitIfo)) {
            //发射射线固定格式Physics.Raycast (发射射线的位置, 发射射线的方向, 射线的返回信息)

            if(HitIfo.collider.tag=="Door"){
            //如果碰到的碰撞器的标签是Door
                Load_Obj.GetComponent<Load_Mark>().enabled=true;
                //启用进度条脚本
            }else{
                Load_Obj.GetComponent<Load_Mark>().enabled=false;
                //禁用进度条脚本
            }
        }
    }
}

TransformChange

using UnityEngine;
using System.Collections;


public class TransformChange : MonoBehaviour {

    public Transform EndPosition;
    //储存第一人称控制器摄像机的位置

    public float CostTime=0.2f;
    //摄像机移动所经过的时间

    public GameObject FPSCtrl;
    //储存第一人称控制器

    public GameObject UICtrl;
    //储存虚拟摇杆


    //退出房间
    public GameObject Close_Btn;

    //切换VR场景
    public GameObject Vr_Btn;

    // Use this for initialization
    void Start () {
        StartCoroutine(CameraMove ());
        //摄像机移动
        StartCoroutine (Show());
        //延迟执行激活 第一人称控制器与虚拟摇杆的函数

      // Vector3  qua = EndPosition.gameObject.transform.eulerAngles; 

    }
    
    // Update is called once per frame
    void Update () {
    
    }

    IEnumerator CameraMove(){
        yield return new WaitForSeconds (0.5f);
        iTween.MoveTo (gameObject,EndPosition.position,CostTime); //iTween 调不出来的话吧编辑器关闭再打开
        //从摄像机起始位置 经过CostTime这段时间  移动到EndPosition.position的位置
        iTween.RotateTo(gameObject,EndPosition.rotation.eulerAngles,CostTime);
        //从摄像机起始位置 经过CostTime这段时间  移动到EndPosition.position的角度
       
        
    }

    IEnumerator Show(){
        yield return new WaitForSeconds (4.7f);
        //延迟函数
        FPSCtrl.SetActive(true);
        //激活第一人称控制器
        UICtrl.SetActive(true);
        //激活虚拟摇杆

        //激活关闭按钮
        Close_Btn.SetActive(true);

        //激活Vr场景按钮
        Vr_Btn.SetActive(true);
    }

    

}

UI_Manager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UI_Manager : MonoBehaviour {

    //储存信息面板
    public GameObject Info_Panel;
    //二级面板引用;
    public GameObject Inside_Panel;


    //贮存外部模型;
    public GameObject Building;

    //储存户型1;
    public GameObject Plan1;

    //储存户型2;
    public GameObject Plan2;

    //储存户型3;
    public GameObject Plan3;


    //记录二级菜单状态;
    // public int Inside_Ctrl = 0;
    private bool isShow = true;



    //记录面板的信息
    private bool Info_P_State = true;


    //贮存案例的面板
    public GameObject Example_Panel;

    //储存案例名称的Text组件
    public Text Example_Content_Text;

    //储存显示案例图片的Image组件
    public Image Example_Content_Image;

    //申请整型int变量来作为案例显示的顺序
    public int Example_Count = 0;

    //储存案例图片的数组;
    public Sprite[] SpriteChange;

    //申请字符串来储存案例的名称
    public string[] NameChange;

    //记录面板的状态
    public bool Example_P_State = false;


    void Start() {

    }


    void Update() {

    }

    /// <summary>
    /// 显示二级面板
    /// </summary>
    public void Inside_Button() {
        Example_Panel.SetActive(false);
        Example_P_State = false;

        if (isShow)
        {
            Inside_Panel.SetActive(true);

        }
        else
        {
            Inside_Panel.SetActive(false);

        }

        isShow = !isShow;

    }

    //方案1按钮
    public void Plan1_Button() {
        //隐藏二级面板
        Inside_Panel.SetActive(false);
        Plan1.SetActive(true);
        Plan2.SetActive(false);
        Plan3.SetActive(false);
        Building.SetActive(false);
        isShow = !isShow;
    }

    //方案2按钮
    public void Plan2_Button()
    {
        //隐藏二级面板
        Inside_Panel.SetActive(false);
        Plan1.SetActive(false);
        Plan2.SetActive(true);
        Plan3.SetActive(false);
        Building.SetActive(false);
        isShow = !isShow;
    }

    //方案3按钮
    public void Plan3_Button()
    {
        //隐藏二级面板
        Inside_Panel.SetActive(false);
        Plan1.SetActive(false);
        Plan2.SetActive(false);
        Plan3.SetActive(true);
        Building.SetActive(false);
        isShow = !isShow;
    }


    //外部效果按钮
    public void Outside_Button() {
        Plan1.SetActive(false);
        Plan2.SetActive(false);
        Plan3.SetActive(false);
        Building.SetActive(true);
    }


    //关于我们的按钮
    public void Info_Button() {

        //让成功案例面板处于隐藏
        Example_Panel.SetActive(false);
        //成功案例面板状态设置为false
        Example_P_State = false;

        if (Info_P_State)
        {
            Info_Panel.SetActive(true);

        }
        else
        {
            Info_Panel.SetActive(false);
        }
        //没点击一次改变一次值;
        Info_P_State = !Info_P_State;
    }

    //信息面板中的关闭按钮;
    public void Info_Close_Button() {
        Info_Panel.SetActive(false);
        Info_P_State = true;
    }

    //成功案例的按钮
    public void Example_Button() {
        //关闭信息面板
        Info_Close_Button();
        if (Example_P_State==false)
        {
            Example_Panel.SetActive(true);
            Example_P_State = true;
        }
        else
        {
            Example_Panel.SetActive(false);
            Example_P_State = false;
        }

    }



    //成功案例面板中的关闭按钮;
    public void Example_Close_Button() {
        Example_Panel.SetActive(false);
    }

    /*

    //成功案例面板中的向右按钮;
    public void Right_Button() {
        //第一种判断方法;
        if (Example_Count == 0)
        {
            Example_Content_Image.sprite = SpriteChange[1];
            Example_Content_Text.text = "金字塔";
            Example_Count = 1;
        }
        else if (Example_Count == 1)
        {
            Example_Content_Image.sprite = SpriteChange[2];
            Example_Content_Text.text = "吴哥窟";
            Example_Count = 2;
        }
        else if (Example_Count == 2)
        {
            Example_Content_Image.sprite = SpriteChange[0];
            Example_Content_Text.text = "长城";
            Example_Count = 0;
        }

        //第二种判断方法;
        //switch (Example_Count)
        //{
        //    case 0:
        //        Example_Content_Image.sprite = SpriteChange[++Example_Count];
        //        Example_Content_Text.text = "金字塔";
        //        break;
        //    case 1:
        //        Example_Content_Image.sprite = SpriteChange[++Example_Count];
        //        Example_Content_Text.text = "吴哥窟";
        //        break;
        //    case 2:
        //        Example_Content_Image.sprite = SpriteChange[Example_Count=0];
        //        Example_Content_Text.text = "长城";
        //        break;
        //    default:
        //        break;
        //}

    }

    //成功案例面板中的向左按钮;
    public void Left_Button()
    {
        //switch (Example_Count)
        //{
        //    case 0:
        //        Example_Content_Image.sprite = SpriteChange[SpriteChange.Length-(++Example_Count)];
        //        Example_Content_Text.text = "吴哥窟";
        //        break;
        //    case 1:
        //        Example_Content_Image.sprite = SpriteChange[SpriteChange.Length-(++Example_Count)];
        //        Example_Content_Text.text = "金字塔";
        //        break;
        //    case 2:
        //        Example_Content_Image.sprite = SpriteChange[Example_Count = 0];
        //        Example_Content_Text.text = "长城";
        //        break;
        //    default:
        //        break;
        //}


        if (Example_Count == 0)
        {
            Example_Content_Image.sprite = SpriteChange[2];
            Example_Content_Text.text = "吴哥窟";
            Example_Count = 2;
        }
        else if (Example_Count == 2)
        {
            Example_Content_Image.sprite = SpriteChange[1];
            Example_Content_Text.text = "金字塔";
            Example_Count = 1;
        }
        else if (Example_Count == 1)
        {
            Example_Content_Image.sprite = SpriteChange[0];
            Example_Content_Text.text = "长城";
            Example_Count = 0;
        }
    }
    */


    //通过序号加减来切换案例图片和名称
    //右键
    public void Right_Button() {
        //向右最后一张图片的时候跳回第一张
        if (Example_Count== SpriteChange.Length - 1)
        {
            Example_Count =0;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }
        else
        {
            Example_Count++;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }
    }

    //左键
    public void Left_Button() {
        if (Example_Count == 0)
        {
            Example_Count = SpriteChange.Length - 1;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }
        else
        {
            Example_Count--;
            Example_Content_Image.sprite = SpriteChange[Example_Count];
            Example_Content_Text.text = NameChange[Example_Count];
        }

    }






    //AR看房
    public void ARScene_Button()
    {
        SceneManager.LoadScene("AScene");
    }

    //AR视频
    public void ARVideo_Button()
    {

        SceneManager.LoadScene("Movie");
    }
}

VedioCtrl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class VedioCtrl : MonoBehaviour {

    public MediaPlayerCtrl scrMedia;

    public bool m_bFinish = false;
    // Use this for initialization
    void Start()
    {
        scrMedia.OnEnd += OnEnd;

    }


    // Update is called once per frame
    void Update()
    {



    }

    //播放视频1
    public void Vedio_01() {
        scrMedia.Load("k3.mp4");
        m_bFinish = false;
        //开启协程;
        //StartCoroutine("PlayVedio");
        StartCoroutine(PlayVedio());
    }


    public void Vedio_02()
    {
        scrMedia.Load("Demo.mp4");
        m_bFinish = false;
        //开启协程;
        //StartCoroutine("PlayVedio");
        StartCoroutine(PlayVedio());
    }

    //停止加载
    public void UnLoad(){
        scrMedia.UnLoad();

    }

    //协程函数
    IEnumerator PlayVedio() {
        
        yield return new WaitForSeconds(0.3f);
        scrMedia.Play();
        m_bFinish = false;
    }


   /* void OnGUI()
    {


        if (GUI.Button(new Rect(50, 50, 100, 100), "Load"))
        {
            scrMedia.Load("EasyMovieTexture.mp4");
            m_bFinish = false;
        }

        if (GUI.Button(new Rect(50, 200, 100, 100), "Play"))
        {
            scrMedia.Play();
            m_bFinish = false;
        }

        if (GUI.Button(new Rect(50, 350, 100, 100), "stop"))
        {
            scrMedia.Stop();
        }

        if (GUI.Button(new Rect(50, 500, 100, 100), "pause"))
        {
            scrMedia.Pause();
        }

        if (GUI.Button(new Rect(50, 650, 100, 100), "Unload"))
        {
            scrMedia.UnLoad();
        }

        if (GUI.Button(new Rect(50, 800, 100, 100), " " + m_bFinish))
        {

        }

        if (GUI.Button(new Rect(200, 50, 100, 100), "SeekTo"))
        {
            scrMedia.SeekTo(10000);
        }


        if (scrMedia.GetCurrentState() == MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING)
        {
            if (GUI.Button(new Rect(200, 200, 100, 100), scrMedia.GetSeekPosition().ToString()))
            {

            }

            if (GUI.Button(new Rect(200, 350, 100, 100), scrMedia.GetDuration().ToString()))
            {

            }

            if (GUI.Button(new Rect(200, 450, 100, 100), scrMedia.GetVideoWidth().ToString()))
            {

            }

            if (GUI.Button(new Rect(200, 550, 100, 100), scrMedia.GetVideoHeight().ToString()))
            {

            }
        }

        if (GUI.Button(new Rect(200, 650, 100, 100), scrMedia.GetCurrentSeekPercent().ToString()))
        {

        }


    }

    */

    void OnEnd()
    {
        m_bFinish = true;
    }



    //退出房间
    public void Out_Button()
    {
        SceneManager.LoadScene("Main");
    }

}

三、效果展示

相关文章

网友评论

      本文标题:AR开发实战Vuforia项目之房地产(AR与VR混合开发)

      本文链接:https://www.haomeiwen.com/subject/xbkdextx.html