CameraOffsetHelper

18 篇文章 0 订阅
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

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值