本文整理汇总了C#中Plane.Raycast方法的典型用法代码示例。如果您正苦于以下问题:C# Plane.Raycast方法的具体用法?C# Plane.Raycast怎么用?C# Plane.Raycast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Plane
的用法示例。
在下文中一共展示了Plane.Raycast方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Raycast
public int Raycast(Ray ray, uint[] triangles, Vector3f[] vertices, List<int> triangleMap)
{
double closestDist = double.MaxValue;
int closestHit = -1;
for (int i = 0; i < triangles.Length; i += 3)
{
Vector3 p0 = (Vector3)vertices[triangles[i+0]];
Vector3 p1 = (Vector3)vertices[triangles[i+1]];
Vector3 p2 = (Vector3)vertices[triangles[i+2]];
Plane plane = new Plane(p0, p1, p2);
//Console.WriteLine(i+" "+plane);
double dist;
if (plane.Raycast(ray, out dist))
{
Vector3 hitPoint = ray.origin+ray.direction*dist;
Vector3 bary = MathUtil.Barycentric(p0, p1, p2, hitPoint);
if (MathUtil.BarycentricIsInside(bary))
{
if (dist < closestDist)
{
closestDist = dist;
closestHit = triangleMap[i/3];
}
}
}
}
return closestHit;
}
示例2: ScreenToCanvas
private Vector3 ScreenToCanvas(Vector3 screenPosition)
{
Vector3 localPosition;
Vector2 min;
Vector2 max;
var canvasSize = m_canvasRectTransform.sizeDelta;
if (m_canvas.renderMode == RenderMode.ScreenSpaceOverlay || (m_canvas.renderMode == RenderMode.ScreenSpaceCamera && m_canvas.worldCamera == null))
{
localPosition = screenPosition;
min = Vector2.zero;
max = canvasSize;
}
else
{
var ray = m_canvas.worldCamera.ScreenPointToRay(screenPosition);
var plane = new Plane(m_canvasRectTransform.forward, m_canvasRectTransform.position);
float distance;
if (plane.Raycast(ray, out distance) == false)
{
throw new Exception("Is it practically possible?");
};
var worldPosition = ray.origin + ray.direction * distance;
localPosition = m_canvasRectTransform.InverseTransformPoint(worldPosition);
min = -Vector2.Scale(canvasSize, m_canvasRectTransform.pivot);
max = Vector2.Scale(canvasSize, Vector2.one - m_canvasRectTransform.pivot);
}
// keep window inside canvas
localPosition.x = Mathf.Clamp(localPosition.x, min.x + KeepWindowInCanvas, max.x - KeepWindowInCanvas);
localPosition.y = Mathf.Clamp(localPosition.y, min.y + KeepWindowInCanvas, max.y - KeepWindowInCanvas);
return localPosition;
}
示例3: PlaneTest
public static bool PlaneTest(this Ray ray, Vector3 planeCenter, Quaternion planeRot, Vector2 planeSize, out Vector3 hitPosition, float gridSize = 0.0f, bool edge = true)
{
Plane plane = new Plane(planeRot * Vector3.up, planeCenter);
hitPosition = Vector3.zero;
float hitDistance = 0.0f;
if (!plane.Raycast(ray, out hitDistance))
{
return false;
}
hitPosition = ray.origin + ray.direction * hitDistance;
Vector3 hitOffset = hitPosition - planeCenter;
float distanceLf = Vector3.Dot(hitOffset, planeRot * Vector3.left);
float distanceUp = Vector3.Dot(hitOffset, planeRot * Vector3.forward);
if (gridSize > 0.0f)
{
if (edge)
{
distanceLf = distanceLf.SnapToEdge(gridSize);
distanceUp = distanceUp.SnapToEdge(gridSize);
}
else
{
distanceLf = distanceLf.SnapToCenter(gridSize);
distanceUp = distanceUp.SnapToCenter(gridSize);
}
}
hitPosition = planeCenter;
hitPosition += (planeRot * Vector3.left) * distanceLf;
hitPosition += (planeRot * Vector3.forward) * distanceUp;
return true;
}