从本文开始将介绍Qcom SDK中SvrPluginWin的实现。与SvrPluginAndroid脚本的作用类似,SvrPluginWin的代码是负责Win平台中VR效果的实现
具体的代码逻辑如下:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
class SvrPluginWin : SvrPlugin
{
public static SvrPluginWin Create()
{
return new SvrPluginWin ();
}
private SvrPluginWin() { }
public override bool IsInitialized() { return svrCamera != null; }
public override bool IsRunning() { return eyes != null; }
public override IEnumerator Initialize()
{
yield return base.Initialize();
deviceInfo = GetDeviceInfo();
yield break;
}
public override IEnumerator BeginVr(int cpuPerfLevel, int gpuPerfLevel)
{
yield return base.BeginVr(cpuPerfLevel, gpuPerfLevel);
yield break;
}
public override void SetVSyncCount(int vSyncCount)
{
QualitySettings.vSyncCount = vSyncCount;
}
public override int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex)
{
int poseStatus = 0;
orientation = Quaternion.identity;
position = Vector3.zero;
。。。
if (eyes != null) for (int i = 0; i < eyes.Length; i++)
{
if (eyes[i].isActiveAndEnabled == false) continue;
if (eyes[i].TexturePtr == null) continue;
if (eyes[i].imageTransform != null && eyes[i].imageTransform.gameObject.activeSelf == false) continue;
if (eyes[i].imageTransform != null && !eyes[i].imageTransform.IsChildOf(svrCamera.transform)) continue; // svr only
var eyeRectMin = eyes[i].clipLowerLeft; eyeRectMin /= eyeRectMin.w;
var eyeRectMax = eyes[i].clipUpperRight; eyeRectMax /= eyeRectMax.w;
if (eyes[i].Side == SvrEye.eSide.Left || eyes[i].Side == SvrEye.eSide.Both)
{
leftScreen = Rect.MinMaxRect(
leftCenter.x + eyeExtent.x * eyeRectMin.x,
leftCenter.y + eyeExtent.y * eyeRectMin.y,
leftCenter.x + eyeExtent.x * eyeRectMax.x,
leftCenter.y + eyeExtent.y * eyeRectMax.y);
Graphics.DrawTexture(leftScreen, eyes[i].TexturePtr, textureRect, 0, 0, 0, 0);
}
if (eyes[i].Side == SvrEye.eSide.Right || eyes[i].Side == SvrEye.eSide.Both)
{
rightScreen = Rect.MinMaxRect(
rightCenter.x + eyeExtent.x * eyeRectMin.x,
rightCenter.y + eyeExtent.y * eyeRectMin.y,
rightCenter.x + eyeExtent.x * eyeRectMax.x,
rightCenter.y + eyeExtent.y * eyeRectMax.y);
Graphics.DrawTexture(rightScreen, eyes[i].TexturePtr, textureRect, 0, 0, 0, 0);
}
}
if (overlays != null) for (int i = 0; i < overlays.Length; i++)
{
if (overlays[i].isActiveAndEnabled == false) continue;
if (overlays[i].TexturePtr == null) continue;
if (overlays[i].imageTransform != null && overlays[i].imageTransform.gameObject.activeSelf == false) continue;
if (overlays[i].imageTransform != null && !overlays[i].imageTransform.IsChildOf(svrCamera.transform)) continue; // svr only
var eyeRectMin = overlays[i].clipLowerLeft; eyeRectMin /= eyeRectMin.w;
var eyeRectMax = overlays[i].clipUpperRight; eyeRectMax /= eyeRectMax.w;
textureRect.Set(overlays[i].uvLowerLeft.x, overlays[i].uvLowerLeft.y,
overlays[i].uvUpperRight.x - overlays[i].uvLowerLeft.x,
overlays[i].uvUpperRight.y - overlays[i].uvLowerLeft.y);
if (overlays[i].Side == SvrOverlay.eSide.Left || overlays[i].Side == SvrOverlay.eSide.Both)
{
leftScreen = Rect.MinMaxRect(
leftCenter.x + eyeExtent.x * eyeRectMin.x,
leftCenter.y + eyeExtent.y * eyeRectMin.y,
leftCenter.x + eyeExtent.x * eyeRectMax.x,
leftCenter.y + eyeExtent.y * eyeRectMax.y);
Graphics.DrawTexture(leftScreen, overlays[i].TexturePtr, textureRect, 0, 0, 0, 0);
}
if (overlays[i].Side == SvrOverlay.eSide.Right || overlays[i].Side == SvrOverlay.eSide.Both)
{
rightScreen = Rect.MinMaxRect(
rightCenter.x + eyeExtent.x * eyeRectMin.x,
rightCenter.y + eyeExtent.y * eyeRectMin.y,
rightCenter.x + eyeExtent.x * eyeRectMax.x,
rightCenter.y + eyeExtent.y * eyeRectMax.y);
Graphics.DrawTexture(rightScreen, overlays[i].TexturePtr, textureRect, 0, 0, 0, 0);
}
}
}
public override void Shutdown()
{
base.Shutdown();
}
}