需求是从hololens 截屏出来的图像中画线 然后投射到实际空间位置。遇到的问题是划不准。
整体图像偏右,以为是以右眼为准,将画线的camera绑定到右眼位置和朝向仍然不准。
所以猜测截屏的camera跟hololens camera左右眼camera都不一样。
通过photo capture 截一帧的图像,在回调中会有cameraToWorld的matrix,利用这个matrix计算出
世界坐标和朝向,仍然发现不准。后来注意到这个matrix是会随着头动而改变的,截图的时候的matrix返回
回调的时候跟当前的位置可能有延迟误差,将hololens放到桌子上,这个matrix就不变了,然后用这个matrix
计算出截图camera相对于hololens camera的位置和朝向,用这个值作为画线camera的位置和朝向并将画线camera
作为hololens camera的child gameobject,之后测试,仍然不准!!!
最后采用实时从配置文件中加载偏移值的方法,来校准位置和朝向,运行时候向很远的地方画线,根据偏差不断调整配置文件中角度值rx,ry,rz,然后在hololens控制台中上传更新这个文件,角度调差不多的时候,向很近的地方画线,这个时候偏差主要是位置的偏差造成的,调整xyz的值,并更新配置文件,如此反复调整角度和位置,应该能得到一个不错的结果。
void Update()
{
if (Time.realtimeSinceStartup < (lastCheckTime + 5f)) return;
lastCheckTime = Time.realtimeSinceStartup;
Debug.Log("check debug file:"+ debugFilePath);
string line = "";
if (File.Exists(debugFilePath))
{
Debug.Log("file exist");
StreamReader fs;
fs = File.OpenText(debugFilePath);
Debug.Log("file open");
line = fs.ReadLine();
float rx = 0, ry = 0, rz = 0, x = 0, y = 0, z = 0;
while (!String.IsNullOrEmpty(line))
{
Debug.Log("read line:"+line);
if (line.StartsWith("rx")) rx = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("ry")) ry = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("rz")) rz = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("x")) x = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("y")) y = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("z")) z = float.Parse(line.Split(':')[1]);
line = fs.ReadLine();
{
if (Time.realtimeSinceStartup < (lastCheckTime + 5f)) return;
lastCheckTime = Time.realtimeSinceStartup;
Debug.Log("check debug file:"+ debugFilePath);
string line = "";
if (File.Exists(debugFilePath))
{
Debug.Log("file exist");
StreamReader fs;
fs = File.OpenText(debugFilePath);
Debug.Log("file open");
line = fs.ReadLine();
float rx = 0, ry = 0, rz = 0, x = 0, y = 0, z = 0;
while (!String.IsNullOrEmpty(line))
{
Debug.Log("read line:"+line);
if (line.StartsWith("rx")) rx = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("ry")) ry = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("rz")) rz = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("x")) x = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("y")) y = float.Parse(line.Split(':')[1]);
else if (line.StartsWith("z")) z = float.Parse(line.Split(':')[1]);
line = fs.ReadLine();
}
fs.Dispose();
Debug.Log("file close");
File.Delete(configFilePath);
Debug.Log("delete "+ configFilePath);
File.Copy(debugFilePath, configFilePath);
Debug.Log("Copy " + debugFilePath+" ->"+ configFilePath);
File.Delete(debugFilePath);
Debug.Log("delete " + debugFilePath);
lastDrawcamOffset = new Vector3(x, y, z);
lastDrawcamEulerAngles = new Vector3(rx, ry, rz);
drawCam.transform.localPosition = lastDrawcamOffset;
drawCam.transform.localEulerAngles = lastDrawcamEulerAngles;
Debug.Log("read drawcamera offset from strRemoteconfigDebug :" + x + " " + y + " " + " " + z + " " + rx + " " + ry + " " + rz);
fs.Dispose();
Debug.Log("file close");
File.Delete(configFilePath);
Debug.Log("delete "+ configFilePath);
File.Copy(debugFilePath, configFilePath);
Debug.Log("Copy " + debugFilePath+" ->"+ configFilePath);
File.Delete(debugFilePath);
Debug.Log("delete " + debugFilePath);
lastDrawcamOffset = new Vector3(x, y, z);
lastDrawcamEulerAngles = new Vector3(rx, ry, rz);
drawCam.transform.localPosition = lastDrawcamOffset;
drawCam.transform.localEulerAngles = lastDrawcamEulerAngles;
Debug.Log("read drawcamera offset from strRemoteconfigDebug :" + x + " " + y + " " + " " + z + " " + rx + " " + ry + " " + rz);
}