# 两种屏幕-世界坐标转换的性能比较

Raycasting思路大致是根据屏幕坐标反投影出两个空间坐标，一般可以取及最远点，这样就组成穿过viewport的一根射线，射线和viewport相交的点就是需要的空间坐标点。

    class Program
{
static Matrix Get2DTranslationMatrix(ViewportF viewPort, Vector2 pos, Matrix world, Matrix view, Matrix projection)
{
var screenZ = viewPort.Project(Vector3.Zero, projection, view, world);
var diff = new Vector3(pos, 0) - screenZ;

var projM = world * view * projection;
var transV = Vector3.Transform(Vector3.Zero, projM);
if (MathUtil.IsZero(transV.W))
{
transV.W = 1.0f;
}

var w = viewPort.Width;
var h = viewPort.Height;
var C1 = viewPort.X;
var C2 = viewPort.Y;
var C3 = viewPort.MaxDepth;
var C4 = viewPort.MinDepth;

var L1 = projM.M11 - projM.M14 * (transV.X / transV.W + diff.X * 2 / w);
var L2 = projM.M21 - projM.M24 * (transV.X / transV.W + diff.X * 2 / w);
var L3 = projM.M31 - projM.M34 * (transV.X / transV.W + diff.X * 2 / w);

var M1 = projM.M12 + projM.M14 * (-transV.Y / transV.W + diff.Y * 2 / h);
var M2 = projM.M22 + projM.M24 * (-transV.Y / transV.W + diff.Y * 2 / h);
var M3 = projM.M32 + projM.M34 * (-transV.Y / transV.W + diff.Y * 2 / h);

var N1 = projM.M13 - projM.M14 * (transV.Z / transV.W * (C3 - C4) + C4);
var N2 = projM.M23 - projM.M24 * (transV.Z / transV.W * (C3 - C4) + C4);
var N3 = projM.M33 - projM.M34 * (transV.Z / transV.W * (C3 - C4) + C4);

var A1 = transV.W * (transV.X / transV.W + diff.X * 2 / w) - transV.X;
var A2 = -transV.W * (-transV.Y / transV.W + diff.Y * 2 / h) - transV.Y;
var A3 = transV.W * (transV.Z / transV.W * (C3 - C4) + C4) - transV.Z;

var z = ((A3 * L1 - A1 * N1) * (M2 * L1 - L2 * M1) - (A2 * L1 - A1 * M1) * (N2 * L1 - L2 * N1)) / ((N3 * L1 - L3 * N1) * (M2 * L1 - L2 * M1) - (M3 * L1 - L3 * M1) * (N2 * L1 - L2 * N1));
var y = ((A2 * L1 - A1 * M1) - (M3 * L1 - L3 * M1) * z) / (M2 * L1 - L2 * M1);
var x = A1 / L1 - z * L3 / L1 - y * L2 / L1;

return Matrix.Translation(x, y, z);
}

static Matrix RayTest(ViewportF viewPort, Vector2 pos, Matrix world, Matrix view, Matrix projection)
{
var ray = Ray.GetPickRay((int)pos.X, (int)pos.Y, viewPort, world * view * projection);
var distance = Math.Abs(ray.Position.Z / ray.Direction.Z);
var point = ray.Position + ray.Direction * distance;

return Matrix.Translation(point);
}

static void Main(string[] args)
{
Matrix view = new Matrix(
-1f, 0f, -6.278329E-07f, 0f,
0f, 1f, 0f, 0f,
6.278329E-07f, 0f, -1f, 0f,
-0.03199686f, 0f, -5f, 1f);
Matrix projection = new Matrix(
0.9297889f, 0f, 0f, 0f,
0f, 0.7522834f, 0f, 0f,
0.01567176f, 0f, -1.000001f, -1f,
0f, 0f, -0.001000001f, 0f);
ViewportF viewPort = new ViewportF(0f, 0f, 1182f, 1461f, 0f, 1f);

Matrix world = Matrix.Scaling(0.003f);

var v = new Vector2(320, 200);

Stopwatch sw = new Stopwatch();

sw.Reset();
sw.Start();
for (int i = 0; i < 10000000; ++i)
Get2DTranslationMatrix(viewPort, v, world, view, projection);
sw.Stop();
var c1 = sw.ElapsedMilliseconds;
Console.WriteLine(sw.ElapsedMilliseconds);

sw.Reset();
sw.Start();
for (int i = 0; i < 10000000; ++i)
RayTest(viewPort, v, world, view, projection);
sw.Stop();
var c2 = sw.ElapsedMilliseconds;
Console.WriteLine(sw.ElapsedMilliseconds);

Console.WriteLine("{0}", c2 - c1);
Console.WriteLine("{0}", 1 - (double)c1 / c2);

}
}
• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
03-26 4752
06-22 476
06-14
11-14

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

guoyichao

¥2 ¥4 ¥6 ¥10 ¥20

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