zspace初級教程


  1. 先檢查電腦配置(一下是最低要求)

https://support.zspace.com/hc/en-us/articles/204780665-zSpace-System-Requirements

處理器:Intel i3-4370

內存:8GB RAM

顯卡:AMD FirePro W5170M GPU

硬盤:128GB以上

或者你還可以下載一個zspace系統配置檢查,就是它啦

https://zspace.com/downloads

 

 

2.滿足要求了,打開顯卡設置,NVIDIA控制面板—》3D設置—》管理3D設置—》立體-啟用—》開

 

NOW 現在你就可以去官方下載一個demo試試效果了,地址

https://zspace.com/downloads

3.看完官方的demo之后,有木有心動,是不是也想自己動手做一個,好的,本菜鳥看完之后就想自己做一個,繼續往下走

4.安裝完unity3d后,我安裝的版本是unity3d2017.1.0p5,其余的版本類似,進入zspace官方,下載兩個開發包

http://developer.zspace.com/downloads

 

   

第一個直接安裝,第二個導入unity3d

 

5.導入之后,在資源里邊找到zCore,將zCore拖入Hierarchy窗口

6.找到Inspector屬性面板-》shereo Carmera-》current Camera將Main Camera為其賦值

 

7.接下來,很重要一步,有了它,立體效果才會出現Edit->Project Settings->Player-Settings for PC->Other Settings->Rendering->Virtual Reality SDKs添加 Stereo Display

 

8.接下來進入開發階段,向場景中拖入一個cube,在創建一個空物體,將RayGrapGameObject拖到空物體下,RayGrapGameObject代碼如下

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using zSpace.Core;

public class RayGrapGameObject : MonoBehaviour {
    private GameObject _stylusBeamObject=null;
    private LineRenderer _stylusBeamRenderer=null;
    //private StylusState RayGrapGameObject.Ra_stylusState = StylusState.Idle;
    private StylusState _stylusState = StylusState.Idle;

    // Use this for initialization(初始化)
    private ZCore _zCore = null;
    private bool _wasButtonPressed = false;

    private static readonly float DEFAULT_STYLUS_BEAM_WIDTH = 0.0002f;
    private static readonly float DEFAULT_STYLUS_BEAM_LENGTH = 0.3f;

    private float _stylusBeamLength = DEFAULT_STYLUS_BEAM_LENGTH;
    private GameObject _grabObject=null;
    private Vector3 _initialGrabOffset = Vector3.zero;
    private Quaternion _initialGrabRotation=Quaternion.identity;
    private float _initialGrabDistance = 0.0f;

    public GameObject Cube;

    void Start()
    {
        _zCore = GameObject.FindObjectOfType<ZCore>();
        if (_zCore == null)
        {
            Debug.LogError("無法找到參考");
            this.enabled = false;
            return;
        }

        //創建觸控筆對象
        _stylusBeamObject = new GameObject("StylusBeam");
        _stylusBeamRenderer = _stylusBeamObject.AddComponent<LineRenderer>();
        _stylusBeamRenderer.material = new Material(Shader.Find("Transparent/Diffuse"));

        _stylusBeamRenderer.startColor = Color.green;
        _stylusBeamRenderer.endColor = Color.black;
    }

    void Update()
    {
        //獲取觸控筆按鈕狀態信息
        ZCore.Pose pose = _zCore.GetTargetPose(ZCore.TargetType.Primary,ZCore.CoordinateSpace.World);
        bool isButtonPressed = _zCore.IsTargetButtonPressed(ZCore.TargetType.Primary,0);

        switch (_stylusState)
        {
            case StylusState.Idle:
                {
                    _stylusBeamLength = DEFAULT_STYLUS_BEAM_WIDTH;
                    RaycastHit hit;
                    if (Physics.Raycast(pose.Position, pose.Direction, out hit))
                    {
                        _stylusBeamLength = hit.distance / _zCore.ViewerScale;     
                        if (isButtonPressed && !_wasButtonPressed)
                        {
                            Cube.GetComponent<Renderer>().material.color = Color.red;
                            //開始抓取
                            this.BeginGrab(hit.collider.gameObject, hit.distance, pose.Position, pose.Rotation);
                            _stylusState = StylusState.Grab;
                        }
                    }
                }
                break;

            case StylusState.Grab:
                {
                    //抓取
                    this.UpdateGrab(pose.Position, pose.Rotation);
                    //如果前面的觸控筆按鈕被釋放,就結束抓取
                    if (!isButtonPressed && _wasButtonPressed)
                    {
                        _stylusState = StylusState.Idle;
                        Cube.GetComponent<Renderer>().material.color = Color.white;
                    }
                }
                break;

            default:
                break;
        }
        //更新光線
        this.UpdateStylusBeam(pose.Position, pose.Direction);
        _wasButtonPressed = isButtonPressed;
    }

    private void UpdateStylusBeam(Vector3 stylusPosition, Vector3 stylusDirection)
    {
        if (_stylusBeamRenderer != null)
        {
            float stylusBeamWidth = DEFAULT_STYLUS_BEAM_WIDTH * _zCore.ViewerScale;
            float stylusBeamLength = _stylusBeamLength * _zCore.ViewerScale;

            #if UNITY_5_4
                            _stylusBeamRenderer.SetWidth(stylusBeamWidth, stylusBeamWidth);
            #else
                        _stylusBeamRenderer.startWidth = stylusBeamWidth;
                        _stylusBeamRenderer.endWidth = stylusBeamWidth;
            #endif
                        _stylusBeamRenderer.SetPosition(0, stylusPosition);
                        _stylusBeamRenderer.SetPosition(1, stylusPosition + (stylusDirection * stylusBeamLength));
        }
    }
    private void UpdateGrab(Vector3 inputPosition, Quaternion inputRotation)
    {
        Vector3 inputEndPosition = inputPosition + (inputRotation * (Vector3.forward * _initialGrabDistance));
        //更新物體的角度
        Quaternion objectRotation = inputRotation * _initialGrabRotation;
        _grabObject.transform.rotation = objectRotation;

        //更新抓取位置
        Vector3 objectPosition = inputEndPosition + (objectRotation * _initialGrabOffset);
        _grabObject.transform.position = objectPosition;
    }

    private void BeginGrab(GameObject hitObject, float hitDistance, Vector3 inputPosition, Quaternion inputRotation)
    {
        Vector3 inputEndPosition = inputPosition + (inputRotation * (Vector3.forward * hitDistance));
        //儲存初始抓取狀態
        _grabObject = hitObject;
        _initialGrabOffset = Quaternion.Inverse(hitObject.transform.rotation)*(hitObject.transform.position-inputEndPosition);
        _initialGrabRotation = Quaternion.Inverse(inputRotation) * hitObject.transform.rotation;
        _initialGrabDistance = hitDistance;
    }
    private enum StylusState
    {
        Idle=0,
        Grab=1,
    }

 

將cube賦值,打開運行窗口,打開zspace->Open Priview Windows帶起眼鏡,拿起你手中的筆就可以操作啦,啦啦啦,開心吧!!!!

 

第一次書寫博客,如有不當,請各位批評指正

(注:相關技術參考來自官方的文檔)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM