12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Events;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using AOT;
- using SC.XR.Unity;
- public class GSXRManager : MonoBehaviour
- {
- public static GSXRManager Instance;
- //{
- // get
- // {
- // if (instance == null) instance = FindObjectOfType<GSXRManager>();
- // if (instance == null) Debug.LogError("GSXRManager object component not found");
- // return instance;
- // }
- //}
- //private static GSXRManager instance;
- static public int EyeLayerMax = 8; // svrApi SVR_MAX_EYE_LAYERS
- static public int OverlayLayerMax = 8; // svrApi SVR_MAX_OVERLAY_LAYERS
- static public int RenderLayersMax = 16; // svrApi SVR_MAX_RENDER_LAYERS
- public Action SvrInitializedCallBack;
- public enum SpecificationType {
- SVR,
- GSXR,
- }
- [SerializeField]
- private SpecificationType m_SpecificationType = SpecificationType.SVR;
- public SpecificationType specificationType {
- get { return m_SpecificationType; }
- private set { m_SpecificationType = value; }
- }
- public enum svrEventType
- {
- kEventNone = 0,
- kEventSdkServiceStarting = 1,
- kEventSdkServiceStarted = 2,
- kEventSdkServiceStopped = 3,
- kEventControllerConnecting = 4,
- kEventControllerConnected = 5,
- kEventControllerDisconnected = 6,
- kEventThermal = 7,
- kEventVrModeStarted = 8,
- kEventVrModeStopping = 9,
- kEventVrModeStopped = 10,
- kEventSensorError = 11,
- kEventMagnometerUncalibrated = 12,
- kEventBoundarySystemCollision = 13,
- kEvent6dofRelocation = 14,
- kEvent6dofWarningFeatureCount = 15,
- kEvent6dofWarningLowLight = 16,
- kEvent6dofWarningBrightLight = 17,
- kEvent6dofWarningCameraCalibration = 18
- };
- [Serializable]
- public class SvrSettings
- {
- public enum eAntiAliasing
- {
- k1 = 1,
- k2 = 2,
- k4 = 4,
- };
-
- public enum eDepth
- {
- k16 = 16,
- k24 = 24
- };
-
- public enum eChromaticAberrationCorrection
- {
- kDisable = 0,
- kEnable = 1
- };
-
- public enum eVSyncCount
- {
- k0 = 0,
- k1 = 1,
- k2 = 2,
- };
-
- public enum eMasterTextureLimit
- {
- k0 = 0, // full size
- k1 = 1, // half size
- k2 = 2, // quarter size
- k3 = 3, // ...
- k4 = 4 // ...
- };
-
- public enum ePerfLevel
- {
- Minimum = 1,
- Medium = 2,
- Maximum = 3
- };
- public enum eFrustumType
- {
- Camera = 0,
- Device = 1,
- }
- public enum eEyeBufferType
- {
- //Mono = 0,
- StereoSeperate = 1,
- //StereoSingle = 2,
- //Array = 3,
- }
- [Tooltip("If head tracking lost, fade the display")]
- public bool poseStatusFade = true;
- [Tooltip("Use eye tracking (if available)")]
- public bool trackEyes = true;
- [Tooltip("Use position tracking (if available)")]
- public bool trackPosition = true;
- [Tooltip("Track position conversion from meters")]
- public float trackPositionScale = 1;
- [Tooltip("Height of the eyes from base of head")]
- public float headHeight = 0.0750f;
- [Tooltip("Depth of the eyes from center of head")]
- public float headDepth = 0.0805f;
- [Tooltip("Distance between the eyes")]
- public float interPupilDistance = 0.064f;
- //[Tooltip("Distance of line-of-sight convergence (0 disabled)")]
- //public float stereoConvergence = 0;
- //[Tooltip("Pitch angle to the horizon in degrees")]
- //public float horizonElevation = 0;
- [Tooltip("Eye field of view render target reprojection margin (% of fov) [0..]")]
- public float eyeFovMargin = 0.0f;
- [Tooltip("Eye render target scale factor")]
- public float eyeResolutionScaleFactor = 1.0f;
- [Tooltip("Eye render target depth buffer")]
- public eDepth eyeDepth = eDepth.k24;
- [Tooltip("Eye render target MSAA value")]
- public eAntiAliasing eyeAntiAliasing = eAntiAliasing.k2;
- [Tooltip("Overlay render target scale factor")]
- public float overlayResolutionScaleFactor = 1.0f;
- [Tooltip("Overlay render target depth buffer")]
- public eDepth overlayDepth = eDepth.k16;
- [Tooltip("Overlay render target MSAA value")]
- public eAntiAliasing overlayAntiAliasing = eAntiAliasing.k1;
- [Tooltip("Limit refresh rate")]
- public eVSyncCount vSyncCount = eVSyncCount.k1;
- [Tooltip("Chromatic Aberration Correction")]
- public eChromaticAberrationCorrection chromaticAberationCorrection = eChromaticAberrationCorrection.kEnable;
- [Tooltip("QualitySettings TextureQuality FullRes, HalfRes, etc.")]
- public eMasterTextureLimit masterTextureLimit = eMasterTextureLimit.k0;
- [Tooltip("CPU performance level")]
- public ePerfLevel cpuPerfLevel = ePerfLevel.Medium;
- [Tooltip("GPU performance level")]
- public ePerfLevel gpuPerfLevel = ePerfLevel.Medium;
- [Tooltip("Foveated render gain [1..], 0/feature disabled")]
- public Vector2 foveationGain = new Vector2(0.0f, 0.0f);
- [Tooltip("Foveated render hires area [0..]")]
- public float foveationArea = 0.0f;
- [Tooltip("Foveated render min pixel density [1/16..1]")]
- public float foveationMinimum = 0.25f;
- [Tooltip("Use perspective of unity camera or device frustum data")]
- public eFrustumType frustumType = eFrustumType.Camera;
- [Tooltip("Display buffer type (default stereo seperate)")]
- public eEyeBufferType displayType = eEyeBufferType.StereoSeperate;
- }
- [SerializeField]
- public SvrSettings settings;
- [Serializable]
- public class SvrStatus
- {
- [Tooltip("SnapdragonVR SDK Initialized")]
- public bool initialized = false;
- [Tooltip("SnapdragonVR SDK Running")]
- public bool running = false;
- [Tooltip("SnapdragonVR SDK Pose Status: 0/None, 1/Rotation, 2/Position, 3/RotationAndPosition")]
- public int pose = 0;
- }
- [SerializeField]
- public SvrStatus status;
- public enum eFadeState { FadeIn, FadeOut }
- [NonSerialized]
- public eFadeState fadeState = eFadeState.FadeIn;
- [NonSerialized]
- public float fadeDuration = 0.5f;
- [Header("Camera Rig")]
- public Transform head;
- public Transform gaze;
- public Camera monoCamera;
- public Camera leftCamera;
- public Camera rightCamera;
- public Camera leftOverlay;
- public Camera rightOverlay;
- public Camera monoOverlay;
- public GSXROverlay fadeOverlay;
- public GSXROverlay reticleOverlay;
- private bool IsUseOpticsCalibration = true;
- public Vector2 FocalPoint { get; set; } // Foveated Rendering Focal Point
- public int FrameCount { get { return frameCount; } }
-
- private int frameCount = 0;
- private static WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
- public GSXRPlugin plugin = null;
- private float sensorWarmupDuration = 0.25f;
- private List<GSXREye> eyes = new List<GSXREye>(EyeLayerMax);
- private List<GSXROverlay> overlays = new List<GSXROverlay>(OverlayLayerMax);
- private bool disableInput = false;
- private Coroutine onResume = null;
- private Coroutine submitFrame = null;
- private Matrix4x4 headDeltaInitLocalToWorld = Matrix4x4.identity;
- /// <summary>
- /// Svr event listener.
- /// </summary>
- public interface SvrEventListener {
- /// <summary>
- /// Raises the svr event event.
- /// </summary>
- /// <param name="ev">Ev.</param>
- void OnSvrEvent (SvrEvent ev);
- };
- public enum svrThermalLevel
- {
- kSafe,
- kLevel1,
- kLevel2,
- kLevel3,
- kCritical,
- kNumThermalLevels
- };
- public enum svrThermalZone
- {
- kCpu,
- kGpu,
- kSkin,
- kNumThermalZones
- };
- public struct svrEventData_Thermal
- {
- public svrThermalZone zone; //!< Thermal zone
- public svrThermalLevel level; //!< Indication of the current zone thermal level
- };
- [StructLayout(LayoutKind.Explicit)]
- public struct svrEventData
- {
- [FieldOffset(0)]
- public svrEventData_Thermal thermal;
- //[FieldOffset(0)]
- //public svrEventData_New newData;
- }
- public struct SvrEvent
- {
- public svrEventType eventType; //!< Type of event
- public uint deviceId; //!< An identifier for the device that generated the event (0 == HMD)
- public float eventTimeStamp; //!< Time stamp for the event in seconds since the last svrBeginVr call
- public svrEventData eventData; //!< Event specific data payload
- };
- // david start
- public struct SCSingleLayerData
- {
- public UInt32 layerId;
- public UInt32 parentLayerId;
- public UInt32 layerTextureId;
- [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)]
- public float[] modelMatrixData;
- public UInt32 editFlag;
- public int z;
- [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 12)]
- public float[] vertexPosition;
- [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 8)]
- public float[] vertexUV;
- public float alpha;
- public byte bOpaque;
- public UInt32 taskId;
- };
- public struct SCAllLayers
- {
- public UInt32 layerNum;
- [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)]
- public float[] viewMatrixData;
- public IntPtr layerData;
- };
- // david end
- private List<SvrEventListener> eventListeners = new List<SvrEventListener>();
- public bool Initialized
- {
- get { return status.initialized; }
- }
- public bool IsRunning
- {
- get { return status.running; }
- }
- public bool DisableInput
- {
- get { return disableInput; }
- set { disableInput = value; }
- }
- void Awake() {
- if (Instance) {
- DestroyImmediate(gameObject);
- return;
- }
- Instance = this;
- Debug.Log("SpecificationType:" + specificationType);
- if (!ValidateReferencedComponents ())
- {
- enabled = false;
- return;
- }
- RegisterListeners();
- Input.backButtonLeavesApp = true;
- Screen.sleepTimeout = SleepTimeout.NeverSleep;
- Application.targetFrameRate = -1;
- if(API_Module_SDKConfiguration.HasKey("Module_Slam", "IsUseOpticsCalibration")){
- IsUseOpticsCalibration = API_Module_SDKConfiguration.GetBool("Module_Slam", "IsUseOpticsCalibration", 1);
- DebugMy.Log("IsUseOpticsCalibration:" + IsUseOpticsCalibration, this,true);
- }
- if (API_Module_SDKConfiguration.HasKey("Module_Slam", "trackPositionScale")) {
- settings.trackPositionScale = API_Module_SDKConfiguration.GetFloat("Module_Slam", "trackPositionScale", 1);
- DebugMy.Log("settings.trackPositionScale:" + settings.trackPositionScale, this, true);
- }
- }
- void OnGUI() {
-
- }
- bool ValidateReferencedComponents()
- {
- plugin = GSXRPlugin.Instance;
- if(plugin == null)
- {
- Debug.LogError("Svr Plugin failed to load. Disabling...");
- return false;
- }
- if(head == null)
- {
- Debug.LogError("Required head gameobject not found! Disabling...");
- return false;
- }
- if(monoCamera == null && (leftCamera == null || rightCamera == null))
- {
- Debug.LogError("Required eye components are missing! Disabling...");
- return false;
- }
- return true;
- }
- #if UNITY_2018
- void OnEnable()
- {
- if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
- {
- UnityEngine.Experimental.Rendering.RenderPipeline.beginCameraRendering += OnPreRenderEvent;
- }
- }
- void OnDisable()
- {
- if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
- {
- UnityEngine.Experimental.Rendering.RenderPipeline.beginCameraRendering -= OnPreRenderEvent;
- }
- }
- private void OnPreRenderEvent(Camera camera)
- {
- camera.SendMessage("OnPreRenderEvent", SendMessageOptions.DontRequireReceiver);
- }
- #endif
- IEnumerator Start ()
- {
- yield return StartCoroutine(Initialize());
- status.initialized = plugin.IsInitialized();
- SetOverlayFade(eFadeState.FadeIn);
- yield return StartCoroutine(plugin.BeginVr((int)settings.cpuPerfLevel, (int)settings.gpuPerfLevel));
- if (!plugin.IsRunning())
- {
- Debug.LogError("Svr failed!");
- Application.Quit();
- yield return null; // Wait one frame
- }
- float recenterTimeout = 1f;
- while (!plugin.RecenterTracking() && recenterTimeout > 0f)
- {
- yield return null; // Wait one frame
- recenterTimeout -= Time.deltaTime;
- }
- yield return new WaitForSecondsRealtime(sensorWarmupDuration);
- submitFrame = StartCoroutine(SubmitFrame());
- status.running = true;
- SvrInitializedCallBack?.Invoke();
-
- plugin.SetGlassDisconnectedCallBack(GlassDisconnetedCallBack);
- Debug.Log("Svr initialized!");
-
- Vector3 localLeftShoulder=Vector3.zero;
- if (API_Module_SDKConfiguration.HasKey("Module_Slam", "LeftShoulderX") && API_Module_SDKConfiguration.HasKey("Module_Slam", "LeftShoulderY") && API_Module_SDKConfiguration.HasKey("Module_Slam", "LeftShoulderZ")) {
- localLeftShoulder = new Vector3(API_Module_SDKConfiguration.GetFloat("Module_Slam", "LeftShoulderX", 0.0f), API_Module_SDKConfiguration.GetFloat("Module_Slam", "LeftShoulderY", 0.0f), API_Module_SDKConfiguration.GetFloat("Module_Slam", "LeftShoulderZ", 0.0f));
- }
- shoulder = new Shoulder(head, localLeftShoulder == Vector3.zero ? new Vector3(-0.15f,-0.08f,-0.1f): localLeftShoulder);
- }
- [MonoPInvokeCallback(typeof(Action))]
- private static void GlassDisconnetedCallBack() {
- Debug.Log("GlassDisconnetedCallBack");
- Application.Quit();
- }
- IEnumerator Initialize()
- {
- // Plugin must be initialized OnStart in order to properly
- // get a valid surface
- // GameObject mainCameraGo = GameObject.FindWithTag("MainCamera");
- // if (mainCameraGo)
- // {
- // mainCameraGo.SetActive(false);
- //
- // Debug.Log("Camera with MainCamera tag found.");
- // if (!disableInput)
- // {
- // Debug.Log("Will use translation and orientation from the MainCamera.");
- // transform.position = mainCameraGo.transform.position;
- // transform.rotation = mainCameraGo.transform.rotation;
- // }
- //
- // Debug.Log("Disabling Camera with MainCamera tag");
- // }
- GL.Clear(false, true, Color.black);
- yield return StartCoroutine(plugin.Initialize());
- InitializeCameras();
- InitializeEyes();
- InitializeOverlays();
- int trackingMode = (int)GSXRPlugin.TrackingMode.kTrackingOrientation;
- if (API_Module_SDKConfiguration.HasKey("Module_Slam", "IsSlamUse3Dof")) {
- if (API_Module_SDKConfiguration.GetBool("Module_Slam", "IsSlamUse3Dof", 0)) {
- DebugMy.Log("Slam Use3Dof", this, true);
- settings.trackPosition = false;
- }
- }
- if (settings.trackPosition)
- trackingMode |= (int)GSXRPlugin.TrackingMode.kTrackingPosition;
- if (settings.trackEyes)
- trackingMode |= (int)GSXRPlugin.TrackingMode.kTrackingEye;
- plugin.SetTrackingMode(trackingMode);
- plugin.SetVSyncCount((int)settings.vSyncCount);
- QualitySettings.vSyncCount = (int)settings.vSyncCount;
- }
- Matrix4x4 invertZ = new Matrix4x4(
- new Vector4(1, 0, 0, 0),
- new Vector4(0, 1, 0, 0),
- new Vector4(0, 0, -1, 0),
- new Vector4(0, 0, 0, 1));
- private void InitializeCameras()
- {
- float stereoConvergence = plugin.deviceInfo.targetFrustumConvergence; //settings.stereoConvergence
- float horizonElevation = plugin.deviceInfo.targetFrustumPitch; //settings.horizonElevation
- float convergenceAngle = 0f;
- if (stereoConvergence > Mathf.Epsilon) convergenceAngle = Mathf.Rad2Deg * Mathf.Atan2(0.5f * settings.interPupilDistance, stereoConvergence);
- else if (stereoConvergence < -Mathf.Epsilon) convergenceAngle = -Mathf.Rad2Deg * Mathf.Atan2(0.5f * settings.interPupilDistance, -stereoConvergence);
- // left
- Vector3 eyePos;
- eyePos.x = -0.5f * settings.interPupilDistance;
- eyePos.y = (!settings.trackPosition ? settings.headHeight : 0);
- eyePos.z = (!settings.trackPosition ? settings.headDepth : 0);
- eyePos += head.transform.localPosition;
- Quaternion eyeRot;
- eyeRot = Quaternion.Euler(horizonElevation, convergenceAngle, 0);
- if (leftCamera != null)
- {
- leftCamera.transform.localPosition = eyePos;
- leftCamera.transform.localRotation = eyeRot;
- }
- if (leftOverlay != null)
- {
- leftOverlay.transform.localPosition = eyePos;
- leftOverlay.transform.localRotation = eyeRot;
- }
- // right
- eyePos.x *= -1;
- eyeRot = Quaternion.Euler(horizonElevation, -convergenceAngle, 0);
- if (rightCamera != null)
- {
- rightCamera.transform.localPosition = eyePos;
- rightCamera.transform.localRotation = eyeRot;
- }
- if (rightOverlay != null)
- {
- rightOverlay.transform.localPosition = eyePos;
- rightOverlay.transform.localRotation = eyeRot;
- }
- // mono
- eyePos.x = 0.0f;
- eyeRot = Quaternion.Euler(horizonElevation, 0, 0);
- if (monoCamera != null)
- {
- monoCamera.transform.localPosition = eyePos;
- monoCamera.transform.localRotation = eyeRot;
- }
- if (monoOverlay != null)
- {
- monoOverlay.transform.localPosition = eyePos;
- monoOverlay.transform.localRotation = eyeRot;
- }
- }
- private void AddEyes(Camera cam, GSXREye.eSide side)
- {
- bool enableCamera = false;
- var eyesFound = cam.gameObject.GetComponents<GSXREye>();
- for (int i = 0; i < eyesFound.Length; i++)
- {
- eyesFound[i].Side = side;
- if (eyesFound[i].imageType == GSXREye.eType.RenderTexture) enableCamera = true;
- }
- eyes.AddRange(eyesFound);
- if (eyesFound.Length == 0)
- {
- var eye = cam.gameObject.AddComponent<GSXREye>();
- eye.Side = side;
- eyes.Add(eye);
- enableCamera = true;
- }
- #if UNITY_5_4 || UNITY_5_5
- cam.hdr = false;
- #else // UNITY_5_6 plus
- cam.allowHDR = false;
- cam.allowMSAA = false;
- #endif
- //cam.enabled = enableCamera;
- }
- private void InitializeEyes()
- {
- eyes.Clear();
- if (monoCamera != null && monoCamera.gameObject.activeSelf)
- {
- AddEyes(monoCamera, GSXREye.eSide.Both);
- }
- if (leftCamera != null && leftCamera.gameObject.activeSelf)
- {
- AddEyes(leftCamera, GSXREye.eSide.Left);
- }
- if (rightCamera != null && rightCamera.gameObject.activeSelf)
- {
- AddEyes(rightCamera, GSXREye.eSide.Right);
- }
- for (int i = 0; i < GSXREye.Instances.Count; i++)
- {
- var eye = GSXREye.Instances[i];
- if (!eyes.Contains(eye))
- {
- eyes.Add(eye); // Add eyes found outside of svr camera hierarchy
- }
- }
- GSXRPlugin.DeviceInfo info = plugin.deviceInfo;
- foreach(GSXREye eye in eyes)
- {
- if (eye == null) continue;
- eye.FovMargin = settings.eyeFovMargin;
- eye.Format = RenderTextureFormat.Default;
- eye.Resolution = new Vector2(info.targetEyeWidthPixels, info.targetEyeHeightPixels);
- eye.ResolutionScaleFactor = settings.eyeResolutionScaleFactor;
- eye.Depth = (int)settings.eyeDepth;
- eye.AntiAliasing = (int)settings.eyeAntiAliasing; // hdr not supported with antialiasing
- eye.FrustumType = (int)settings.frustumType;
- eye.OnPreRenderListener = OnPreRenderListener;
- eye.OnPostRenderListener = OnPostRenderListener;
- eye.Initialize();
- }
- }
- private void AddOverlays(Camera cam, GSXROverlay.eSide side)
- {
- bool enableCamera = false;
- var overlaysFound = cam.gameObject.GetComponents<GSXROverlay>();
- for (int i = 0; i < overlaysFound.Length; i++)
- {
- overlaysFound[i].Side = side;
- if (overlaysFound[i].imageType == GSXROverlay.eType.RenderTexture) enableCamera = true;
- }
- overlays.AddRange(overlaysFound);
- if (overlaysFound.Length == 0)
- {
- var overlay = cam.gameObject.AddComponent<GSXROverlay>();
- overlay.Side = side;
- overlays.Add(overlay);
- enableCamera = true;
- }
- #if UNITY_5_4 || UNITY_5_5
- cam.hdr = false;
- #else // UNITY_5_6 plus
- cam.allowHDR = false;
- cam.allowMSAA = false;
- #endif
- cam.enabled = enableCamera;
- }
- private void InitializeOverlays()
- {
- overlays.Clear();
- if (leftOverlay != null && leftOverlay.gameObject.activeSelf)
- {
- AddOverlays(leftOverlay, GSXROverlay.eSide.Left);
- }
- if (rightOverlay != null && rightOverlay.gameObject.activeSelf)
- {
- AddOverlays(rightOverlay, GSXROverlay.eSide.Right);
- }
- if (monoOverlay != null && monoOverlay.gameObject.activeSelf)
- {
- AddOverlays(monoOverlay, GSXROverlay.eSide.Both);
- }
- for (int i = 0; i < GSXROverlay.Instances.Count; i++)
- {
- var overlay = GSXROverlay.Instances[i];
- if (!overlays.Contains(overlay))
- {
- overlays.Add(overlay); // Add overlays found outside of svr camera hierarchy
- }
- }
- GSXRPlugin.DeviceInfo info = plugin.deviceInfo;
- foreach (GSXROverlay overlay in overlays)
- {
- if (overlay == null) continue;
- overlay.Format = RenderTextureFormat.Default;
- overlay.Resolution = new Vector2(info.targetEyeWidthPixels, info.targetEyeHeightPixels);
- overlay.ResolutionScaleFactor = settings.overlayResolutionScaleFactor;
- overlay.Depth = (int)settings.overlayDepth;
- overlay.AntiAliasing = (int)settings.overlayAntiAliasing; // hdr not supported with antialiasing
- overlay.FrustumType = (int)settings.frustumType;
- overlay.OnPreRenderListener = OnPreRenderListener;
- overlay.OnPostRenderListener = OnPostRenderListener;
- overlay.Initialize();
- }
- }
- public void SetOverlayFade(eFadeState fadeValue)
- {
- fadeState = fadeValue;
- var startAlpha = fadeState == eFadeState.FadeIn ? 1f : 0f;
- UpdateOverlayFade(startAlpha);
- }
- public bool IsOverlayFading()
- {
- return !Mathf.Approximately((float)fadeState, fadeAlpha);
- }
- private float fadeAlpha = 0f;
- private void UpdateOverlayFade(float targetAlpha, float rate = 0)
- {
- if (fadeOverlay == null) return;
- fadeAlpha = rate > 0 ? Mathf.MoveTowards(fadeAlpha, targetAlpha, rate) : targetAlpha;
- var fadeTexture = fadeOverlay.imageTexture as Texture2D;
- if (fadeTexture != null)
- {
- var fadeColors = fadeTexture.GetPixels();
- for (int i = 0; i < fadeColors.Length; ++i)
- {
- fadeColors[i].a = fadeAlpha;
- }
- fadeTexture.SetPixels(fadeColors);
- fadeTexture.Apply(false);
- }
- var isActive = fadeAlpha > 0.0f;
- if (fadeOverlay.enabled != isActive)
- {
- fadeOverlay.enabled = isActive;
- }
- }
- IEnumerator SubmitFrame ()
- {
- Vector3 frustumSize = Vector3.zero;
- frustumSize.x = 0.5f * (plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left);
- frustumSize.y = 0.5f * (plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom);
- frustumSize.z = plugin.deviceInfo.targetFrustumLeft.near;
- //Debug.LogFormat("SubmitFrame: Frustum Size ({0:F2}, {1:F2}, {2:F2})", frustumSize.x, frustumSize.y, frustumSize.z);
- while (true)
- {
- yield return waitForEndOfFrame;
- if ((plugin.GetTrackingMode() & (int)GSXRPlugin.TrackingMode.kTrackingEye) != 0) // Request eye pose
- {
- status.pose |= plugin.GetEyePose(ref eyePose);
- }
- var eyePoint = Vector3.zero;
- if ((status.pose & (int)GSXRPlugin.TrackingMode.kTrackingEye) != 0) // Valid eye pose
- {
- //Debug.LogFormat("Left Eye Position: {0}, Direction: {1}", eyePose.leftPosition.ToString(), eyePose.leftDirection.ToString());
- //Debug.LogFormat("Right Eye Position: {0}, Direction: {1}", eyePose.rightPosition.ToString(), eyePose.rightDirection.ToString());
- //Debug.LogFormat("Combined Eye Position: {0}, Direction: {1}", eyePose.combinedPosition.ToString(), eyePose.combinedDirection.ToString());
- var combinedDirection = Vector3.zero;
- if ((eyePose.leftStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.leftDirection;
- if ((eyePose.rightStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.rightDirection;
- //if ((eyePose.combinedStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.combinedDirection;
- if (combinedDirection.sqrMagnitude > 0f)
- {
- combinedDirection.Normalize();
- //Debug.LogFormat("Eye Direction: ({0:F2}, {1:F2}, {2:F2})", combinedDirection.x, combinedDirection.y, combinedDirection.z);
- float denominator = Vector3.Dot(combinedDirection, Vector3.forward);
- if (denominator > float.Epsilon)
- {
- // eye direction intersection with frustum near plane (left)
- eyePoint = combinedDirection * frustumSize.z / denominator;
- eyePoint.x /= frustumSize.x; // [-1..1]
- eyePoint.y /= frustumSize.y; // [-1..1]
- //Debug.LogFormat("Eye Point: ({0:F2}, {1:F2})", eyePoint.x, eyePoint.y);
- }
- }
- }
- var currentGain = GSXROverrideSettings.FoveateGain == Vector2.zero ?
- settings.foveationGain : GSXROverrideSettings.FoveateGain;
- var currentArea = GSXROverrideSettings.FoveateArea == 0f ?
- settings.foveationArea : GSXROverrideSettings.FoveateArea;
- var currentMinimum = GSXROverrideSettings.FoveateMinimum == 0f ?
- settings.foveationMinimum : GSXROverrideSettings.FoveateMinimum;
- for (int i = 0; i < eyes.Count; i++)
- {
- var eye = eyes[i];
- if (eye.TextureId > 0 && eye.ImageType == GSXREye.eType.RenderTexture)
- {
- plugin.SetFoveationParameters(eye.TextureId, eye.PreviousId, eyePoint.x, eyePoint.y, currentGain.x, currentGain.y, currentArea, currentMinimum);
- plugin.ApplyFoveation();
- }
- }
- var horizontalFieldOfView = 0f;
- if (settings.eyeFovMargin > 0f)
- {
- horizontalFieldOfView = (monoCamera.enabled ? monoCamera.fieldOfView / monoCamera.aspect : leftCamera.fieldOfView / leftCamera.aspect) * Mathf.Deg2Rad;
- }
- plugin.SubmitFrame(frameCount, horizontalFieldOfView, (int)settings.displayType);
-
- frameCount++;
- }
- }
- public bool RecenterTracking()
- {
- if (!Initialized)
- return false;
- return plugin.RecenterTracking();
- }
- void OnPreRenderListener (int sideMask, int textureId, int previousId)
- {
- if (!IsRunning)
- return;
- var currentGain = GSXROverrideSettings.FoveateGain == Vector2.zero ?
- settings.foveationGain : GSXROverrideSettings.FoveateGain;
- var currentArea = GSXROverrideSettings.FoveateArea == 0f ?
- settings.foveationArea : GSXROverrideSettings.FoveateArea;
- var currentMinimum = GSXROverrideSettings.FoveateMinimum == 0f ?
- settings.foveationMinimum : GSXROverrideSettings.FoveateMinimum;
- plugin.SetFoveationParameters(textureId, previousId, FocalPoint.x, FocalPoint.y, currentGain.x, currentGain.y, currentArea, currentMinimum);
- plugin.BeginEye(sideMask, frameDelta);
- }
- void OnPostRenderListener (int sideMask, int layerMask)
- {
- if (!IsRunning)
- return;
- plugin.EndEye (sideMask, layerMask);
- }
- public void SetPause(bool pause)
- {
- if (!Initialized)
- return;
- if (pause)
- {
- OnPause();
- }
- else
- {
- onResume = StartCoroutine(OnResume());
- }
- }
- void OnPause()
- {
- //Debug.Log("GSXRManager.OnPause()");
- status.running = false;
- if (onResume != null) {
- StopCoroutine(onResume);
- onResume = null;
- }
- if (submitFrame != null) { StopCoroutine(submitFrame); submitFrame = null; }
- if (plugin.IsRunning()) plugin.EndVr();
- }
- IEnumerator OnResume()
- {
- //Debug.Log("GSXRManager.OnResume()");
- SetOverlayFade(eFadeState.FadeIn);
- yield return StartCoroutine(plugin.BeginVr((int)settings.cpuPerfLevel, (int)settings.gpuPerfLevel));
- float recenterTimeout = 1f;
- while (!plugin.RecenterTracking() && recenterTimeout > 0f)
- {
- yield return null; // Wait one frame
- recenterTimeout -= Time.deltaTime;
- }
- yield return new WaitForSecondsRealtime(sensorWarmupDuration);
- submitFrame = StartCoroutine (SubmitFrame ());
- status.running = plugin.IsRunning();
- onResume = null;
- }
- [NonSerialized]
- public GSXRPlugin.HeadPose headPose;
- [NonSerialized]
- public GSXRPlugin.EyePose eyePose;
- [NonSerialized]
- public Vector3 eyeDirection = Vector3.forward;
- [NonSerialized]
- public Vector2 eyeFocus = Vector2.zero;
- [NonSerialized]
- public float eyeSmoother = 0.2f;
- [NonSerialized]
- public float[] frameDelta = new float[9];
-
- public Vector3 modifyPosition = Vector3.zero;
- public Quaternion modifyOrientation = Quaternion.identity;
- bool outBload = false;
- float[] outTransformArray = new float[32];
- Matrix4x4 leftcalib = Matrix4x4.identity;
- Matrix4x4 rightCalib = Matrix4x4.identity;
- void LateUpdate()
- {
- if (!IsRunning)
- {
- return;
- }
- int trackingMode = plugin.GetTrackingMode();
- var prevOrientation = headPose.orientation;
- status.pose = plugin.GetHeadPose(ref headPose, frameCount);
- if ((trackingMode & (int)GSXRPlugin.TrackingMode.kTrackingEye) != 0)
- {
- status.pose |= plugin.GetEyePose(ref eyePose, frameCount);
- }
- if (!disableInput)
- {
- if ((status.pose & (int)GSXRPlugin.TrackingMode.kTrackingOrientation) != 0)
- {
- head.transform.localRotation = headPose.orientation;
- // delta orientation screen space x, y offset for foveated rendering
- var deltaOrientation = Quaternion.Inverse(prevOrientation) * headPose.orientation;
- var lookDirection = deltaOrientation * Vector3.forward;
- //Debug.LogFormat("Look Direction: {0}", lookDirection.ToString());
- lookDirection *= plugin.deviceInfo.targetFrustumLeft.near / lookDirection.z;
- float xTotal = 0.5f * (plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left);
- float xAdjust = (xTotal != 0.0f) ? lookDirection.x / xTotal : 0.0f;
- float yTotal = 0.5f * (plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom);
- float yAdjust = (yTotal != 0.0f) ? lookDirection.y / yTotal : 0.0f;
- //xAdjust *= 0.5f * plugin.deviceInfo.targetEyeWidthPixels;
- //yAdjust *= 0.5f * plugin.deviceInfo.targetEyeHeightPixels;
- // rotation around z [cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]
- Vector3 deltaEulers = deltaOrientation.eulerAngles;
- float cosZ = Mathf.Cos(deltaEulers.z * Mathf.Deg2Rad);
- float sinZ = Mathf.Sin(deltaEulers.z * Mathf.Deg2Rad);
- // Output rotation and translation
- frameDelta[0] = cosZ;
- frameDelta[1] = sinZ;
- frameDelta[2] = 0.0f;
- frameDelta[3] = -sinZ;
- frameDelta[4] = cosZ;
- frameDelta[5] = 0.0f;
- frameDelta[6] = xAdjust;
- frameDelta[7] = yAdjust;
- frameDelta[8] = 1.0f;
- }
- if (settings.trackPosition && (status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0)
- {
- head.transform.localPosition = headPose.position * settings.trackPositionScale;
- }
- if ((status.pose & (int)GSXRPlugin.TrackingMode.kTrackingEye) != 0)
- {
- //Debug.LogFormat("Left Eye Position: {0}, Direction: {1}", eyePose.leftPosition.ToString(), eyePose.leftDirection.ToString());
- //Debug.LogFormat("Right Eye Position: {0}, Direction: {1}", eyePose.rightPosition.ToString(), eyePose.rightDirection.ToString());
- //Debug.LogFormat("Combined Eye Position: {0}, Direction: {1}", eyePose.combinedPosition.ToString(), eyePose.combinedDirection.ToString());
- var combinedDirection = Vector3.zero;
- if ((eyePose.leftStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.leftDirection;
- if ((eyePose.rightStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.rightDirection;
- //if ((eyePose.combinedStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.combinedDirection;
- if (combinedDirection.sqrMagnitude > 0f)
- {
- combinedDirection.Normalize();
- //Debug.LogFormat("Eye Direction: ({0:F2}, {1:F2}, {2:F2})", combinedDirection.x, combinedDirection.y, combinedDirection.z);
- eyeDirection = eyeSmoother > 0.001f ? Vector3.Lerp(eyeDirection, combinedDirection, eyeSmoother) : combinedDirection;
- //var combinedPosition = Vector3.zero;
- //if ((eyePose.leftStatus & (int)GSXRPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.leftPosition;
- //if ((eyePose.rightStatus & (int)GSXRPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.rightPosition;
- ////if ((eyePose.combinedStatus & (int)GSXRPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.combinedPosition;
- gaze.localPosition = monoCamera.transform.localPosition;
- gaze.localRotation = Quaternion.LookRotation(eyeDirection, Vector3.up);
- float denominator = Vector3.Dot(eyeDirection, Vector3.forward);
- if (denominator > float.Epsilon)
- {
- // eye direction intersection with frustum near plane (left)
- var eyePoint = eyeDirection * plugin.deviceInfo.targetFrustumLeft.near / denominator;
- // size of the frustum near plane (left)
- var nearSize = new Vector2(0.5f*(plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left),
- 0.5f*(plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom));
- eyeFocus.Set(eyePoint.x / nearSize.x, eyePoint.y / nearSize.y); // Normalized [-1,1]
- //Debug.LogFormat("Eye Focus: {0}", eyeFocus.ToString());
- FocalPoint = eyeFocus; // Cache for foveated rendering
- }
- }
- }
- }
- //var isValid = true;
- //if (settings.poseStatusFade)
- //{
- // isValid = !settings.trackPosition
- // || ((trackingMode & (int)GSXRPlugin.TrackingMode.kTrackingPosition) == 0)
- // || (status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0;
- //}
- //var targetAlpha = fadeState == eFadeState.FadeOut || !isValid ? 1f : 0f;
- var targetAlpha = fadeState == eFadeState.FadeOut ? 1f : 0f;
- UpdateOverlayFade(targetAlpha, Time.deltaTime / fadeDuration);
- if (plugin.GSXR_Is_SupportOpticsCalibration()) {
- if (leftcalib == Matrix4x4.identity || rightCalib == Matrix4x4.identity) {
- plugin.GSXR_Get_TransformMatrix(ref outBload, outTransformArray);
- if (outBload) {
- leftcalib.SetColumn(0, new Vector4(outTransformArray[0], outTransformArray[1], outTransformArray[2], outTransformArray[3]));
- leftcalib.SetColumn(1, new Vector4(outTransformArray[4], outTransformArray[5], outTransformArray[6], outTransformArray[7]));
- leftcalib.SetColumn(2, new Vector4(outTransformArray[8], outTransformArray[9], outTransformArray[10], outTransformArray[11]));
- leftcalib.SetColumn(3, new Vector4(outTransformArray[12], outTransformArray[13], outTransformArray[14], outTransformArray[15]));
- rightCalib.SetColumn(0, new Vector4(outTransformArray[16], outTransformArray[17], outTransformArray[18], outTransformArray[19]));
- rightCalib.SetColumn(1, new Vector4(outTransformArray[20], outTransformArray[21], outTransformArray[22], outTransformArray[23]));
- rightCalib.SetColumn(2, new Vector4(outTransformArray[24], outTransformArray[25], outTransformArray[26], outTransformArray[27]));
- rightCalib.SetColumn(3, new Vector4(outTransformArray[28], outTransformArray[29], outTransformArray[30], outTransformArray[31]));
- Debug.Log($"leftcalib [{leftcalib.m00}, {leftcalib.m01}, {leftcalib.m02}, {leftcalib.m03};" +
- $"{leftcalib.m10}, {leftcalib.m11}, {leftcalib.m12}, {leftcalib.m13};" +
- $"{leftcalib.m20}, {leftcalib.m21}, {leftcalib.m22}, {leftcalib.m23};" +
- $"{leftcalib.m30}, {leftcalib.m31}, {leftcalib.m32}, {leftcalib.m33}]");
- Debug.Log($"rightCalib [{rightCalib.m00}, {rightCalib.m01}, {rightCalib.m02}, {rightCalib.m03};" +
- $"{rightCalib.m10}, {rightCalib.m11}, {rightCalib.m12}, {rightCalib.m13};" +
- $"{rightCalib.m20}, {rightCalib.m21}, {rightCalib.m22}, {rightCalib.m23};" +
- $"{rightCalib.m30}, {rightCalib.m31}, {rightCalib.m32}, {rightCalib.m33}]");
- }
- }
- //if (outBload) {
- // leftCamera.worldToCameraMatrix = leftcalib * invertZ * Matrix4x4.Inverse(Matrix4x4.TRS(leftCamera.transform.position, leftCamera.transform.rotation, leftCamera.transform.lossyScale));
- // rightCamera.worldToCameraMatrix = rightCalib * invertZ * Matrix4x4.Inverse(Matrix4x4.TRS(rightCamera.transform.position, rightCamera.transform.rotation, rightCamera.transform.lossyScale));
- //}
- if (Application.platform == RuntimePlatform.Android) {
- if (head.position != head.localPosition || head.rotation != head.localRotation) {
- ///for If Head parent not at origin,must mul the delta Matirx for Camera ViewMatrix
- //MD*Ml*P = Mw*P ==> MD == Mw * (M1 -1 ni)
- headDeltaInitLocalToWorld = Matrix4x4.TRS(head.position, head.rotation, Vector3.one) * Matrix4x4.Inverse(Matrix4x4.TRS(head.localPosition, head.localRotation, Vector3.one));
- }
- leftCamera.worldToCameraMatrix = plugin.leftViewMatrix * invertZ;
- leftCamera.worldToCameraMatrix = Matrix4x4.Inverse(headDeltaInitLocalToWorld * leftCamera.cameraToWorldMatrix/* not worldToCameraMatrix*/);
- rightCamera.worldToCameraMatrix = plugin.rightViewMatrix * invertZ;
- rightCamera.worldToCameraMatrix = Matrix4x4.Inverse(headDeltaInitLocalToWorld * rightCamera.cameraToWorldMatrix/* not worldToCameraMatrix*/);
- }
- }
- }
- public void Shutdown()
- {
- Debug.Log("GSXRManager.Shutdown()");
- status.running = false;
- if (submitFrame != null) { StopCoroutine(submitFrame); submitFrame = null; }
- if (plugin.IsRunning()) plugin.EndVr();
- if (plugin.IsInitialized()) plugin.Shutdown();
- status.initialized = false;
- }
- void OnDestroy() {
- if (Instance != this)
- return;
- UnregisterListeners();
- Shutdown();
- }
- public delegate void OnApplicationPauseDele(bool pause);
- public static event OnApplicationPauseDele onApplicationPauseDele;
- void OnApplicationPause(bool pause)
- {
- Debug.LogFormat("GSXRManager.OnApplicationPause({0})",pause);
- onApplicationPauseDele?.Invoke(pause);
- SetPause(pause);
- }
- void OnApplicationQuit()
- {
- //Debug.Log("GSXRManager.OnApplicationQuit()");
- //Shutdown();
- }
- static public Matrix4x4 Perspective(float left, float right, float bottom, float top, float near, float far)
- {
- float x = 2.0F * near / (right - left);
- float y = 2.0F * near / (top - bottom);
- float a = (right + left) / (right - left);
- float b = (top + bottom) / (top - bottom);
- float c = -(far + near) / (far - near);
- float d = -(2.0F * far * near) / (far - near);
- float e = -1.0F;
- Matrix4x4 m = new Matrix4x4();
- m[0, 0] = x;
- m[0, 1] = 0;
- m[0, 2] = a;
- m[0, 3] = 0;
- m[1, 0] = 0;
- m[1, 1] = y;
- m[1, 2] = b;
- m[1, 3] = 0;
- m[2, 0] = 0;
- m[2, 1] = 0;
- m[2, 2] = c;
- m[2, 3] = d;
- m[3, 0] = 0;
- m[3, 1] = 0;
- m[3, 2] = e;
- m[3, 3] = 0;
- return m;
- }
- void RegisterListeners()
- {
- GSXROverrideSettings.OnEyeAntiAliasingChangedEvent += OnEyeAntiAliasingChanged;
- GSXROverrideSettings.OnEyeDepthChangedEvent += OnEyeDepthChanged;
- GSXROverrideSettings.OnEyeResolutionScaleFactorChangedEvent += OnEyeResolutionScaleFactorChanged;
- GSXROverrideSettings.OnOverlayAntiAliasingChangedEvent += OnOverlayAntiAliasingChanged;
- GSXROverrideSettings.OnOverlayDepthChangedEvent += OnOverlayDepthChanged;
- GSXROverrideSettings.OnOverlayResolutionScaleFactorChangedEvent += OnOverlayResolutionScaleFactorChanged;
- GSXROverrideSettings.OnChromaticAberrationCorrectionChangedEvent += OnChromaticAberrationCorrectionChanged;
- GSXROverrideSettings.OnVSyncCountChangedEvent += OnVSyncCountChanged;
- GSXROverrideSettings.OnMasterTextureLimitChangedEvent += OnMasterTextureLimitChanged;
- GSXROverrideSettings.OnPerfLevelChangedEvent += OnPerfLevelChanged;
- GSXROverrideSettings.OnFoveateChangedEvent += OnFoveateChanged;
- }
-
- void UnregisterListeners()
- {
- GSXROverrideSettings.OnEyeAntiAliasingChangedEvent -= OnEyeAntiAliasingChanged;
- GSXROverrideSettings.OnEyeDepthChangedEvent -= OnEyeDepthChanged;
- GSXROverrideSettings.OnEyeResolutionScaleFactorChangedEvent -= OnEyeResolutionScaleFactorChanged;
- GSXROverrideSettings.OnOverlayAntiAliasingChangedEvent -= OnOverlayAntiAliasingChanged;
- GSXROverrideSettings.OnOverlayDepthChangedEvent -= OnOverlayDepthChanged;
- GSXROverrideSettings.OnOverlayResolutionScaleFactorChangedEvent -= OnOverlayResolutionScaleFactorChanged;
- GSXROverrideSettings.OnChromaticAberrationCorrectionChangedEvent -= OnChromaticAberrationCorrectionChanged;
- GSXROverrideSettings.OnVSyncCountChangedEvent -= OnVSyncCountChanged;
- GSXROverrideSettings.OnMasterTextureLimitChangedEvent -= OnMasterTextureLimitChanged;
- GSXROverrideSettings.OnPerfLevelChangedEvent -= OnPerfLevelChanged;
- GSXROverrideSettings.OnFoveateChangedEvent -= OnFoveateChanged;
- }
- void OnEyeAntiAliasingChanged(GSXROverrideSettings.eAntiAliasing antiAliasing)
- {
- foreach (GSXREye eye in eyes)
- {
- eye.AntiAliasing = antiAliasing == GSXROverrideSettings.eAntiAliasing.NoOverride ?
- (int)settings.eyeAntiAliasing : (int)antiAliasing;
- }
- }
-
- void OnEyeDepthChanged(GSXROverrideSettings.eDepth depth)
- {
- foreach (GSXREye eye in eyes)
- {
- eye.Depth = depth == GSXROverrideSettings.eDepth.NoOverride ?
- (int)settings.eyeDepth : (int)depth;
- }
- }
-
- void OnEyeResolutionScaleFactorChanged(float scaleFactor)
- {
- foreach (GSXREye eye in eyes)
- {
- eye.ResolutionScaleFactor = scaleFactor <= 0 ? settings.eyeResolutionScaleFactor : scaleFactor;
- }
- }
-
- void OnOverlayAntiAliasingChanged(GSXROverrideSettings.eAntiAliasing antiAliasing)
- {
- foreach (GSXROverlay overlay in overlays)
- {
- overlay.AntiAliasing = antiAliasing == GSXROverrideSettings.eAntiAliasing.NoOverride ?
- (int)settings.overlayAntiAliasing : (int)antiAliasing;
- }
- }
-
- void OnOverlayDepthChanged(GSXROverrideSettings.eDepth depth)
- {
- foreach (GSXROverlay overlay in overlays)
- {
- overlay.Depth = depth == GSXROverrideSettings.eDepth.NoOverride ?
- (int)settings.overlayDepth : (int)depth;
- }
- }
-
- void OnOverlayResolutionScaleFactorChanged(float scaleFactor)
- {
- foreach (GSXROverlay overlay in overlays)
- {
- overlay.ResolutionScaleFactor = scaleFactor <= 0 ? settings.overlayResolutionScaleFactor : scaleFactor;
- }
- }
-
- void OnChromaticAberrationCorrectionChanged(GSXROverrideSettings.eChromaticAberrationCorrection aberrationCorrection)
- {
- if(aberrationCorrection == GSXROverrideSettings.eChromaticAberrationCorrection.kDisable)
- {
- plugin.SetFrameOption(GSXRPlugin.FrameOption.kDisableChromaticCorrection);
- }
- else
- {
- plugin.UnsetFrameOption(GSXRPlugin.FrameOption.kDisableChromaticCorrection);
- }
- }
-
- void OnVSyncCountChanged(GSXROverrideSettings.eVSyncCount vSyncCount)
- {
- if (vSyncCount == GSXROverrideSettings.eVSyncCount.NoOverride)
- {
- plugin.SetVSyncCount((int)settings.vSyncCount);
- QualitySettings.vSyncCount = (int)settings.vSyncCount;
- }
- else
- {
- plugin.SetVSyncCount((int)vSyncCount);
- QualitySettings.vSyncCount = (int)settings.vSyncCount;
- }
- }
-
- void OnMasterTextureLimitChanged(GSXROverrideSettings.eMasterTextureLimit masterTextureLimit)
- {
- QualitySettings.masterTextureLimit = masterTextureLimit == GSXROverrideSettings.eMasterTextureLimit.NoOverride ?
- (int)settings.masterTextureLimit : (int)masterTextureLimit;
- }
-
- void OnPerfLevelChanged(GSXROverrideSettings.ePerfLevel cpuPerfLevel, GSXROverrideSettings.ePerfLevel gpuPerfLevel)
- {
- int currentCpuPerfLevel = cpuPerfLevel == GSXROverrideSettings.ePerfLevel.NoOverride ?
- (int)settings.cpuPerfLevel : (int)GSXROverrideSettings.CpuPerfLevel;
- int currentGpuPerfLevel = gpuPerfLevel == GSXROverrideSettings.ePerfLevel.NoOverride ?
- (int)settings.gpuPerfLevel : (int)GSXROverrideSettings.GpuPerfLevel;
- plugin.SetPerformanceLevels(currentCpuPerfLevel, currentGpuPerfLevel);
- }
- void OnFoveateChanged(Vector2 gain, float area, float minPixelDensity)
- {
- var point = Vector2.zero;
- var currentGain = gain == Vector2.zero ?
- settings.foveationGain : GSXROverrideSettings.FoveateGain;
- var currentArea = area == 0f ?
- settings.foveationArea : GSXROverrideSettings.FoveateArea;
- var currentMinimum = minPixelDensity == 0f ?
- settings.foveationMinimum : GSXROverrideSettings.FoveateMinimum;
- plugin.SetFoveationParameters(0, 0, point.x, point.y, currentGain.x, currentGain.y, currentArea, currentMinimum);
- }
- /// <summary>
- /// Update this instance.
- /// </summary>
- //---------------------------------------------------------------------------------------------
- void Update()
- {
- SvrEvent frameEvent = new SvrEvent ();
- while (plugin.PollEvent(ref frameEvent))
- {
- //Debug.LogFormat("SvrEvent: {0}", frameEvent.eventType.ToString());
- for (int i = 0; i < eventListeners.Count; i++) {
- eventListeners [i].OnSvrEvent (frameEvent);
- }
- }
- }
- /// <summary>
- /// Adds the event listener.
- /// </summary>
- /// <param name="listener">Listener.</param>
- //---------------------------------------------------------------------------------------------
- public void AddEventListener(SvrEventListener listener)
- {
- if (listener != null) {
- eventListeners.Insert (0, listener);
- }
- }
-
- /// <summary>
- /// Start Tracking
- /// </summary>
- /// <returns>Handle to the controller</returns>
- /// <param name="desc">Desc.</param>
- //---------------------------------------------------------------------------------------------
- public int ControllerStartTracking(string desc)
- {
- return plugin.ControllerStartTracking(desc);
- }
-
- /// <summary>
- /// Stop Tracking
- /// </summary>
- /// <param name="handle">Handle.</param>
- //---------------------------------------------------------------------------------------------
- public void ControllerStopTracking(int handle)
- {
- plugin.ControllerStopTracking(handle);
- }
-
- /// <summary>
- /// Get current state
- /// </summary>
- /// <returns>Controller State.</returns>
- /// <param name="handle">Handle.</param>
- //---------------------------------------------------------------------------------------------
- public GSXRControllerState ControllerGetState(int handle, int space = 0)
- {
- return plugin.ControllerGetState(handle, space);
- }
- /// <summary>
- /// Send an event to the controller
- /// </summary>
- /// <param name="handle">Handle.</param>
- /// <param name="what">What.</param>
- /// <param name="arg1">Arg1.</param>
- /// <param name="arg2">Arg2.</param>
- //---------------------------------------------------------------------------------------------
- public void ControllerSendMessage(int handle, GSXRController.svrControllerMessageType what, int arg1, int arg2)
- {
- plugin.ControllerSendMessage (handle, what, arg1, arg2);
- }
- /// <summary>
- /// Controllers the query.
- /// </summary>
- /// <returns>The query.</returns>
- /// <param name="handle">Handle.</param>
- /// <param name="what">What.</param>
- /// <param name="mem">Mem.</param>
- /// <param name="size">Size.</param>
- //---------------------------------------------------------------------------------------------
- public object ControllerQuery(int handle, GSXRController.svrControllerQueryType what)
- {
- return plugin.ControllerQuery (handle, what);
- }
- Coroutine startSlameCor;
- public void StartSlam() {
- if (startSlameCor == null) {
- Debug.Log("StartSlam Coroutine Start");
- startSlameCor = StartCoroutine(startSlam());
- } else {
- Debug.Log("startSlame Coroutine Not Null");
- }
- }
- IEnumerator startSlam() {
- yield return new WaitUntil(() => GSXRManager.Instance != null);
- yield return new WaitUntil(() => !GSXRManager.Instance.IsRunning);
- GSXRManager.Instance.SetPause(false);
- yield return new WaitUntil(() => IsTrackingValid);
- startSlameCor = null;
- }
- Coroutine stopSlameCor;
- //when stopSlam you must be sure no StartCorutine run before!
- public void StopSlam() {
- if (stopSlameCor == null && startSlameCor == null) {
- Debug.Log("stopSlame Coroutine Start");
- stopSlameCor = StartCoroutine(stopSlam());
- } else if (stopSlameCor != null) {
- Debug.Log("stopSlame Coroutine Not Null");
- } else if (startSlameCor != null) {
- Debug.Log("stopSlame: startSlame Coroutine Not Null");
- }
- }
- IEnumerator stopSlam() {
- yield return new WaitUntil(() => GSXRManager.Instance != null);
- yield return new WaitUntil(() => GSXRManager.Instance.IsRunning);
- GSXRManager.Instance.SetPause(true);
- yield return new WaitUntil(() => !GSXRManager.Instance.IsRunning);
- stopSlameCor = null;
- }
- Coroutine resetSlameCor;
- public void ResetSlam() {
- if (resetSlameCor == null) {
- Debug.Log("resetSlameCor Coroutine Start");
- resetSlameCor = StartCoroutine(resetSlam());
- } else {
- Debug.Log("resetSlameCor Coroutine Not Null");
- }
- }
- IEnumerator resetSlam() {
- Debug.Log("ResetSlam Coroutine Step 0: CheckFlag...");
- yield return new WaitUntil(() => GSXRManager.Instance != null);
- yield return new WaitUntil(() => GSXRManager.Instance.IsRunning);
- Debug.Log("ResetSlam Coroutine Step 1: StopSlam");
- GSXRManager.Instance.SetPause(true);
- yield return new WaitUntil(() => !GSXRManager.Instance.IsRunning);
- Debug.Log("ResetSlam Coroutine Step 2: StartSlam");
- GSXRManager.Instance.SetPause(false);
- yield return new WaitUntil(() => IsTrackingValid);
- Debug.Log("ResetSlam Coroutine Finish");
- resetSlameCor = null;
- }
- public bool IsTrackingValid {
- get {
- if (GSXRManager.Instance && GSXRManager.Instance.IsRunning) {
- return !GSXRManager.Instance.settings.trackPosition
- || ((GSXRManager.Instance.plugin.GetTrackingMode() & (int)GSXRPlugin.TrackingMode.kTrackingPosition) == 0)
- || (GSXRManager.Instance.status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0;
- }
- return false;
- }
- }
- public Shoulder shoulder;
- public class Shoulder {
- public Transform Left { get; private set; }
- public Transform Right { get; private set; }
- private Transform head;
- private Vector3 leftOffset;
- public Shoulder(Transform head,Vector3 leftOffset) {
- this.head = head;
- this.leftOffset = leftOffset;
- if (head) {
- Left = new GameObject("LeftShoulder").transform;
- Left.SetParent(head,false);
- Left.localPosition = leftOffset;
- Right = new GameObject("RightShoulder").transform;
- Right.SetParent(head, false);
- Right.localPosition = new Vector3(-leftOffset.x, leftOffset.y, leftOffset.z) ;
- }
- }
- }
-
- }
|