Unity zSpace 开发

文章目录

  • 1.下载 zSpace 开发环境
    • 1.1 zCore Unity Package
    • 1.2 zView Unity Package
  • 2. 导入工程
  • 3. 发布设置
  • 4.功能实现
    • 4.1 用触控笔来实现对模型的拖拽:
  • 5. 后续更新

1.下载 zSpace 开发环境

官网地址
Unity zSpace 开发_第1张图片

1.1 zCore Unity Package

  • zSpace 开发核心
  • 必须

1.2 zView Unity Package

  • 主要用途:在zSpace设备外接显示屏或投影时,将zSpace设备的3D画面转为2D画面进行展示(zSpace显示3D,外接设备显示2D)
  • 按需下载Unity zSpace 开发_第2张图片

2. 导入工程

将下载好的 zCore Unity Package 导入到 Unity
Unity zSpace 开发_第3张图片

3. 发布设置

  1. Edit—ProjectSettings—Player—OtherSetting—Rendering—ColorSpace 改为 Gamma
  2. Edit—ProjectSettings—Player—OtherSetting—AutoGraphicsAPIforWindows 取消勾选
  3. Edit—ProjectSettings—Player—OtherSetting—GraphicsAPIsforWindows-添加OpenGLCore,其他全部删除
    Unity zSpace 开发_第4张图片
  4. Edit—ProjectSettings—Player—XRSetting 中勾选 Virtual Reality Supported
  5. Edit—ProjectSettings—Player—XRSetting—Virtual Reality SDKs 删除其他项添加 Stereo Display (non head-mounted)
    Unity zSpace 开发_第5张图片

4.功能实现

4.1 用触控笔来实现对模型的拖拽:


//
//  Copyright (C) 2007-2020 zSpace, Inc.  All Rights Reserved.
//


using UnityEngine;
using UnityEngine.EventSystems;

using zSpace.Core.EventSystems;
using zSpace.Core.Input;

namespace zSpace.Core.Samples
{
    public class Draggable :
        ZPointerInteractable, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        
        // Public Methods
        

        public override ZPointer.DragPolicy GetDragPolicy(ZPointer pointer)
        {
            if (pointer is ZMouse)
            {
                return ZPointer.DragPolicy.LockToScreenAlignedPlane;
            }

            if (pointer is ZStylus)
            {
                return ZPointer.DragPolicy.LockHitPosition;
            }

            return base.GetDragPolicy(pointer);
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            ZPointerEventData pointerEventData = eventData as ZPointerEventData;
            if (pointerEventData == null ||
                pointerEventData.button != PointerEventData.InputButton.Left)
            {
                return;
            }

            Pose pose = pointerEventData.Pointer.EndPointWorldPose;

            // Cache the initial grab state.
            this._initialGrabOffset =
                Quaternion.Inverse(this.transform.rotation) *
                (this.transform.position - pose.position);

            this._initialGrabRotation =
                Quaternion.Inverse(pose.rotation) *
                this.transform.rotation;

            // If the grabbable object has a rigidbody component,
            // mark it as kinematic during the grab.
            var rigidbody = this.GetComponent<Rigidbody>();
            if (rigidbody != null)
            {
                this._isKinematic = rigidbody.isKinematic;
                rigidbody.isKinematic = true;
            }

            // Capture pointer events.
            pointerEventData.Pointer.CapturePointer(this.gameObject);
        }

        public void OnDrag(PointerEventData eventData)
        {
            ZPointerEventData pointerEventData = eventData as ZPointerEventData;
            if (pointerEventData == null ||
                pointerEventData.button != PointerEventData.InputButton.Left)
            {
                return;
            }

            Pose pose = pointerEventData.Pointer.EndPointWorldPose;

            // Update the grab object's rotation.
            this.transform.rotation =
                pose.rotation * this._initialGrabRotation;

            // Update the grab object's position.
            this.transform.position =
                pose.position + 
                (this.transform.rotation * this._initialGrabOffset);
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            ZPointerEventData pointerEventData = eventData as ZPointerEventData;
            if (pointerEventData == null ||
                pointerEventData.button != PointerEventData.InputButton.Left)
            {
                return;
            }

            // Release the pointer.
            pointerEventData.Pointer.CapturePointer(null);

            // If the grabbable object has a rigidbody component,
            // restore its original isKinematic state.
            var rigidbody = this.GetComponent<Rigidbody>();
            if (rigidbody != null)
            {
                rigidbody.isKinematic = this._isKinematic;
            }
        }

        
        // Private Members
        

        private Vector3 _initialGrabOffset = Vector3.zero;
        private Quaternion _initialGrabRotation = Quaternion.identity;
        private bool _isKinematic = false;
    }
}

5. 后续更新

你可能感兴趣的:(Unity,学习笔记,unity,游戏引擎,zSpace)