using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MoCap.CameraCapture
{
[RequireComponent(typeof(Camera))]
public class CameraOffsetHelper : MonoBehaviour {
[Tooltip("位置偏移")]
public Vector3 posOffset = new Vector3(0, 0, 0);
[Tooltip("旋转偏移")]
public Vector3 rotOffset = Vector3.zero;
[Header("输入位置和旋转(世界坐标)")]
public Vector3 inputPos;
public Quaternion inputRot;
private Camera _Camera;
void Awake()
{
_Camera = GetComponent<Camera> ();
_Camera.stereoTargetEye = StereoTargetEyeMask.None;
}
void LateUpdate()
{
//用户直接选择相机tracker
//Collect delta rotation and displacement between Tracker and Accessory
Vector3 delta_displacement = posOffset;
Quaternion delta_rotation = Quaternion.Euler(rotOffset);
//Get current Tracker pose
Vector3 tracker_position = inputPos;
Quaternion tracker_rotation = inputRot;
//Transform current Tracker pose to Accessory pose
transform.rotation = tracker_rotation * delta_rotation;
transform.position = tracker_position + (tracker_rotation * delta_rotation) * delta_displacement;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MoCap.BodyCapture;
using DM_SteamVR;
namespace MoCap.CameraCapture
{
[RequireComponent(typeof(Camera))]
public class SteamCameraHepler : MonoBehaviour {
public SteamVR_Manager steamVR;
[Tooltip("位置偏移")]
public Vector3 posOffset = new Vector3(0, 0, 0);
[Tooltip("旋转偏移")]
public Vector3 rotOffset = Vector3.zero;
private Camera _Camera;
void Awake()
{
_Camera = GetComponent<Camera>();
_Camera.stereoTargetEye = StereoTargetEyeMask.None;
_Camera.depth = 100;
_Camera.fieldOfView = 50;
delayTime = DM_HumanAvatar.DelayTime;
}
//延迟3秒设置index,因为经测试启动后的前2帧hmd的位置为Zero
private float delayTime;
public int trackerIDIndex = -1;
public string[] trackerStrs = {};
private Quaternion lastVritualRotation = Quaternion.identity;
void LateUpdate()
{
if (delayTime > 0)
{
delayTime -= Time.deltaTime;
return;
}
if (trackerIDIndex == -1)
{
refreshValidTrackerIndexs();
trackerIDIndex = 0;
}
//用户直接选择相机tracker
if (trackerStrs != null && trackerIDIndex >= 0 && trackerIDIndex < trackerStrs.Length)
{
//Get current Tracker pose
Vector3 tracker_position;
Quaternion tracker_rotation;
steamVR.checkTrackerTransform(trackerStrs[trackerIDIndex], out tracker_position, out tracker_rotation);
//offsetPos = physicalCamera.position - virtualCamera.position;
//Quaternion offsetRot = physicalCamera.rotation * Quaternion.Inverse(virtualCamera.rotation);
Quaternion offsetRot_Frame = tracker_rotation * Quaternion.Inverse(lastVritualRotation);
calibrate(tracker_position, tracker_rotation, transform, posOffset, Quaternion.Euler(rotOffset), offsetRot_Frame);
lastVritualRotation = tracker_rotation;
}
}
/// <summary>
///
/// </summary>
/// <param name="tracker_position"></param>
/// <param name="tracker_rotation"></param>
/// <param name="calibrateVirtualCamera"></param>
/// <param name="offsetPos">physicalCamera.position - virtualCamera.position</param>
/// <param name="offsetRot">physicalCamera.rotation * Quaternion.Inverse(virtualCamera.rotation)</param>
/// <param name="offsetRot_Frame">虚拟相机本帧与上一帧的旋转差</param>
private static void calibrate(Vector3 tracker_position, Quaternion tracker_rotation, Transform calibrateVirtualCamera, Vector3 offsetPos, Quaternion offsetRot, Quaternion offsetRot_Frame)
{
calibrateVirtualCamera.rotation = offsetRot * tracker_rotation;
calibrateVirtualCamera.position = tracker_position + (offsetRot_Frame) * offsetPos;
}
public void refreshValidTrackerIndexs()
{
trackerStrs = steamVR.getNodeIds().ToArray();
if (trackerIDIndex < 0 || trackerIDIndex >= trackerStrs.Length)
{
trackerIDIndex = 0;
}
}
#region 自然入口
public float curTime;
public RecordCameraAnimation recordAnim;
public RecordPPSAnimation recordPPS;
void Update()
{
curTime += Time.deltaTime;
if (recordAnim != null) {
recordAnim.recordFrame (curTime);
}
if (recordPPS != null)
{
recordPPS.recordFrame(curTime);
}
}
#endregion
}
}