计算网格边之间的距离
Calculate distance between Mesh Edges
我的鼠标在屏幕上的位置很容易找到,
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
现在想象一个立方体。在这个立方体上的任何地方单击,都会从单击的边缘绘制一条线,穿过对象,并在另一端停止。方向,垂直或水平,由点击哪一侧决定,4 侧之一,或顶部或底部。
如何确定距离(从网格的一个边缘到另一边缘)和方向(垂直或水平)?
想法?
到目前为止,我唯一的想法是使用碰撞检测并使用 CollisionEnter 作为起点,并以某种方式绘制一条到达网格另一端的线,并使用 CollisionExit 确定目的地(或出口)点。然后做一些计算以确定 Enter 和 Exit 方法之间的距离。
我能想到的解决这个问题的唯一方法是向另一个方向投射光线 back....
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//offset the ray, keeping it along the XZ plane of the hit
Vector3 offsetDirection = -hit.normal;
offsetDirection.y = 0;
//offset a long way, minimum thickness of the object
ray.origin = hit.point + offsetDirection * 100;
//point the ray back at the first hit point
ray.direction = (hit.point - ray.origin).normalized;
//raycast all, because there might be other objects in the way
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach (RaycastHit h in hits)
{
if (h.collider == hit.collider)
{
h.point; //this is the point you're interested in
}
}
}
这会将光线偏移到新位置,使其保留与原始命中相同的 XZ 坐标,因此生成的端点形成一条与世界/场景 Y 轴垂直的线。为此,我们使用相机的 Forward
方向(因为我们想要获得离视点更远的点)。如果我们想获得垂直于命中表面(平行于表面法线)的直线的点,我们可以使用 hit.normal
创建一个偏移量。
您可能希望将 layermask 或 maxdist 参数放入两个光线投射方法中(因此它检查的东西更少并且速度更快),但这取决于您。
原代码: 找到穿过物体的 "single" 射线的两个端点。
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//offset the ray along its own direction by A LOT
//at a minimum, this would be the maximum thickness of any object we care about,
//PLUS the distance away from the camera that it is
ray.origin += ray.direction * 100;
//reverse the direction of the ray so it points towards the camera
ray.direction *= -1;
//raycast all, because there might be other objects in the way
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach(RaycastHit h in hits)
{
if(h.collider == hit.collider)
{
h.point; //this is the point you're interested in
}
}
}
我的鼠标在屏幕上的位置很容易找到,
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
现在想象一个立方体。在这个立方体上的任何地方单击,都会从单击的边缘绘制一条线,穿过对象,并在另一端停止。方向,垂直或水平,由点击哪一侧决定,4 侧之一,或顶部或底部。
如何确定距离(从网格的一个边缘到另一边缘)和方向(垂直或水平)?
想法?
到目前为止,我唯一的想法是使用碰撞检测并使用 CollisionEnter 作为起点,并以某种方式绘制一条到达网格另一端的线,并使用 CollisionExit 确定目的地(或出口)点。然后做一些计算以确定 Enter 和 Exit 方法之间的距离。
我能想到的解决这个问题的唯一方法是向另一个方向投射光线 back....
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//offset the ray, keeping it along the XZ plane of the hit
Vector3 offsetDirection = -hit.normal;
offsetDirection.y = 0;
//offset a long way, minimum thickness of the object
ray.origin = hit.point + offsetDirection * 100;
//point the ray back at the first hit point
ray.direction = (hit.point - ray.origin).normalized;
//raycast all, because there might be other objects in the way
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach (RaycastHit h in hits)
{
if (h.collider == hit.collider)
{
h.point; //this is the point you're interested in
}
}
}
这会将光线偏移到新位置,使其保留与原始命中相同的 XZ 坐标,因此生成的端点形成一条与世界/场景 Y 轴垂直的线。为此,我们使用相机的 Forward
方向(因为我们想要获得离视点更远的点)。如果我们想获得垂直于命中表面(平行于表面法线)的直线的点,我们可以使用 hit.normal
创建一个偏移量。
您可能希望将 layermask 或 maxdist 参数放入两个光线投射方法中(因此它检查的东西更少并且速度更快),但这取决于您。
原代码: 找到穿过物体的 "single" 射线的两个端点。
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//offset the ray along its own direction by A LOT
//at a minimum, this would be the maximum thickness of any object we care about,
//PLUS the distance away from the camera that it is
ray.origin += ray.direction * 100;
//reverse the direction of the ray so it points towards the camera
ray.direction *= -1;
//raycast all, because there might be other objects in the way
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach(RaycastHit h in hits)
{
if(h.collider == hit.collider)
{
h.point; //this is the point you're interested in
}
}
}