本文整理汇总了C#中UnityEngine.Camera.ScreenPointToRay方法的典型用法代码示例。如果您正苦于以下问题:C# Camera.ScreenPointToRay方法的具体用法?C# Camera.ScreenPointToRay怎么用?C# Camera.ScreenPointToRay使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Camera
的用法示例。
在下文中一共展示了Camera.ScreenPointToRay方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertScreenToWorld
// ConvertScreenToWorld //
// Converts Vector3's from screen to world space - by raycasting from the passed camera //
// to a plane parallel to the camera, outputs an array of 2 Vector3's //
public static Vector3[] ConvertScreenToWorld( Vector3 point_a, Vector3 point_b, Vector3 position, Camera camera )
{
// Convert screen touches into rays from main camera
Ray ray_a = camera.ScreenPointToRay( point_a );
Ray ray_b = camera.ScreenPointToRay( point_b );
// Create a plane parallel to camera, facing the screen - then rotated by the cameras y rotation
Vector3 parallel_to_camera = RotateY( new Vector3( 0, 0, -1 ), camera.transform.rotation.y );
Plane plane = new Plane( parallel_to_camera, position );
// Hit distances
float hit1;
float hit2;
// Hit point Vector3's
Vector3 hit_point_a = Vector3.zero;
Vector3 hit_point_b = Vector3.zero;
// Shoot ray_a at plane
if( plane.Raycast( ray_a, out hit1 ) )
{
// Get where ray_a collides with the plane
hit_point_a = ray_a.GetPoint( hit1 );
}
// Shoot ray_b at plane
if( plane.Raycast( ray_b, out hit2 ) )
{
// Get where ray_b collides with the plane
hit_point_b = ray_b.GetPoint( hit2 );
}
// Return two vectors as array
return new Vector3[ 2 ] { hit_point_a, hit_point_b };
}
示例2: PointOfConvergence
public static Vector3 PointOfConvergence (Camera camera) {
Ray cornerRay = camera.ScreenPointToRay(new Vector3(0, 0, 0));
Ray centerRay = camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
// Calculate angles of the corner and center ray for the trig that is about to happen
float xzCornerAngle = Vector2.Angle(new Vector2(centerRay.direction.x, centerRay.direction.z), new Vector2(cornerRay.direction.x, cornerRay.direction.z));
float zyCornerAngle = Vector2.Angle(new Vector2(centerRay.direction.z, centerRay.direction.y), new Vector2(cornerRay.direction.z, cornerRay.direction.y));
// Angle "i"
float xzCenterAngle = Vector2.Angle(new Vector2(camera.transform.right.x, camera.transform.right.z), new Vector2(centerRay.direction.x, centerRay.direction.z));
float zyCenterAngle = Vector2.Angle(new Vector2(camera.transform.right.z, camera.transform.right.y), new Vector2(centerRay.direction.z, centerRay.direction.y));
// Angle "o"
float xzOuterAngle = 180.0f - (xzCornerAngle + 90.0f);
float zyOuterAngle = 180.0f - (zyCornerAngle + 90.0f);
// Angle "v"
float xzPointAngle = 180.0f - (xzOuterAngle + xzCenterAngle);
float zyPointAngle = 180.0f - (zyOuterAngle + zyCenterAngle);
// Get the distance from center point to corner point on both planes
Vector2 halfScreen = new Vector2(Vector3.Distance(centerRay.origin, new Vector3(cornerRay.origin.x, centerRay.origin.y, cornerRay.origin.z)), Vector3.Distance(cornerRay.origin, new Vector3(cornerRay.origin.x, centerRay.origin.y, cornerRay.origin.z)));
// Calculate the hypotenuse of the PoC calculation triangle
float xzHypotenuse = (Mathf.Sin(xzCenterAngle * Mathf.Deg2Rad) * halfScreen.x) / Mathf.Sin(xzPointAngle * Mathf.Deg2Rad);
float zyHypotenuse = (Mathf.Sin(zyCenterAngle * Mathf.Deg2Rad) * halfScreen.y) / Mathf.Sin(zyPointAngle * Mathf.Deg2Rad);
// Calculate the final PoC by calculating the PoC on both planes
Vector3 inverseCorner = -cornerRay.direction;
Vector2 xzPoC = new Vector2(inverseCorner.x, inverseCorner.z) * xzHypotenuse;
Vector2 zyPoC = new Vector2(inverseCorner.z, inverseCorner.y) * zyHypotenuse; // zyPoc.x = xzPoc.y and therefore isnt used
Vector3 pointOfConvergence = cornerRay.origin + new Vector3(xzPoC.x, zyPoC.y, xzPoC.y);
// DEBUG
//Debug.DrawRay(centerRay.origin, centerRay.direction, Color.yellow);
Debug.Log ("cA " + xzCornerAngle + " o: " + xzOuterAngle + " i: " + xzCenterAngle + " v: " + xzPointAngle + " halfScreen: " + halfScreen.x + " hyp: " + xzHypotenuse);
Debug.DrawRay(cornerRay.origin, inverseCorner * 2.0f, Color.green);
Debug.DrawLine(cornerRay.origin, pointOfConvergence, Color.yellow);
return pointOfConvergence;
}
示例3: GetMouseWaterPosition
/// <summary>
/// Gets the position of the mouse on the water plane.
/// </summary>
static public Vector3 GetMouseWaterPosition (Camera cam)
{
// Since the water plane is always at (0, 0, 0) and points straight up, distance
// to plane calculation has been greatly simplified.
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
return ray.origin - ray.direction * (ray.origin.y / ray.direction.y);
}
示例4: getTargetObjects
public GameObject getTargetObjects(Vector3 startpoint, Camera cam)
{
//GameObject[] colliderobjects = new GameObject[10];
//RaycastHit[] hits = Physics.raycastAll(startpoint, direction, distance);
//return colliderobjects;
GameObject firstCollidedObject = null;
RaycastHit hit;
Ray fireray;
//Startpoint is in het geval van guitexture de transform.position
//bool success = Physics.Raycast(startpoint, direction, out hit, distance); //Werkt niet echt goed
fireray = cam.ScreenPointToRay(startpoint);
bool success = Physics.Raycast(fireray, out hit, distance);
if (success){
firstCollidedObject = hit.collider.gameObject;
if(firstCollidedObject != null)
;//print("There was a hit on " + firstCollidedObject.name);
else
;//print("Debug further");
}
if (debug)
{
drawRay5Sec(startpoint, hit.point);
}
return firstCollidedObject;
}
示例5: InCamera
public static bool InCamera(Camera LookCamera, GameObject go, float VisionDistance)
{
#region 檢查是否在指定攝影範圍內
if (LookCamera == null)
return false;
Collider collider = go.GetComponent<Collider>();
if (collider == null)
return false;
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(LookCamera);
bool isOnCamera = GeometryUtility.TestPlanesAABB(planes, collider.bounds);
if (!isOnCamera)
return false;
#endregion 檢查是否在指定攝影範圍內
#region 檢查是否有遮蔽物
//這是簡陋板,只能檢查無體中新點是否有被遮擋無法判定是否有任何地方有顯示
#region 取得看到的物體
if (LookCamera == null)
return false;
Vector3 screenPoint = LookCamera.WorldToScreenPoint(go.transform.position);
Ray ray = LookCamera.ScreenPointToRay(screenPoint);
RaycastHit hit;
if (!Physics.Raycast(ray, out hit, VisionDistance))
return false;
#endregion 取得看到的物體
//檢查看到的物件是否是自己
bool result = hit.transform == go.transform;
#endregion 檢查是否有遮蔽物
return result;
}
示例6: CameraToPlaneProjection
/// <summary>
/// Projects a screen point to a plane.
/// </summary>
/// <param name="position">Screen point.</param>
/// <param name="camera">The camera.</param>
/// <param name="projectionPlane">The projection plane.</param>
/// <returns></returns>
public static Vector3 CameraToPlaneProjection(Vector2 position, Camera camera, Plane projectionPlane)
{
var ray = camera.ScreenPointToRay(position);
var relativeIntersection = 0f;
projectionPlane.Raycast(ray, out relativeIntersection);
return ray.origin + ray.direction*relativeIntersection;
}
示例7: GetRaycastHitFromCache
/// <summary>
///
/// </summary>
/// <param name="camera"></param>
/// <param name="hit"></param>
/// <returns></returns>
public static bool GetRaycastHitFromCache(Camera camera, out RaycastHit hit)
{
RaycastHit? hitRef;
if (!HitTestContext.raycastHits.TryGetValue(camera, out hitRef))
{
Ray ray = camera.ScreenPointToRay(HitTestContext.screenPoint);
if (Physics.Raycast(ray, out hit))
{
HitTestContext.raycastHits[camera] = hit;
return true;
}
else
{
HitTestContext.raycastHits[camera] = null;
return false;
}
}
else if (hitRef == null)
{
hit = new RaycastHit();
return false;
}
else
{
hit = (RaycastHit)hitRef;
return true;
}
}
示例8: getGameObjectCollided
private GameObject getGameObjectCollided(Vector3 position, Camera camera)
{
Ray ray = camera.ScreenPointToRay(position);
RaycastHit hit;
Physics.Raycast(ray, out hit);
return hit.collider ? hit.collider.gameObject : null;
}
示例9: GetMousePosition
//
// GetMousePosition
//
public static Vector3 GetMousePosition( Camera camera )
{
RaycastHit hit;
Ray cast = camera.ScreenPointToRay( Input.mousePosition );
Physics.Raycast( cast, out hit );
return hit.point;
}
示例10: GetCurrentDistance
/// <summary>
/// Using the given camera the two touches are projectes onto the given plane and the distance is calculated
/// </summary>
/// <returns>
/// The current distance.
/// </returns>
public float GetCurrentDistance(Camera camera, Plane plane, out Vector3 center)
{
Ray r1 = camera.ScreenPointToRay(firstTouch.endPosition);
Ray r2 = camera.ScreenPointToRay(secondTouch.endPosition);
float dist1 = 0;
float dist2 = 0;
plane.Raycast(r1, out dist1);
plane.Raycast(r2, out dist2);
Vector3 p1 = r1.GetPoint(dist1);
Vector3 p2 = r2.GetPoint(dist2);
center = (p2 - p1) * 0.5f + p2;
return Vector3.Distance(p1,p2);
}
示例11: GetHovered2DObject
public static GameObject GetHovered2DObject(Vector2 cursorPos, Camera camera=null)
{
if(camera==null) camera=Camera.main;
if(camera==null) return null;
Ray ray = camera.ScreenPointToRay(cursorPos);
RaycastHit2D hit2D=Physics2D.GetRayIntersection(ray);
return hit2D.collider!=null ? hit2D.collider.gameObject : null;
}
示例12: ScreenPointToWorldPointOnPlane
public static Vector3 ScreenPointToWorldPointOnPlane( Vector3 screenPoint , Plane plane , Camera camera )
{
// Set up a ray corresponding to the screen position
Ray ray = camera.ScreenPointToRay (screenPoint);
// Find out where the ray intersects with the plane
return PlaneRayIntersection (plane, ray);
}
示例13: ScreenPointToWorldPoint
public Vector3 ScreenPointToWorldPoint(Vector3 screenPoint, Plane plane,Camera camera)
{
RaycastHit hit;
if (Physics.Raycast(camera.ScreenPointToRay(screenPoint), out hit)){
return hit.point;
}
return ScreenPointToWorldPointOnPlane(screenPoint, plane, camera);
}
示例14: GetHovered3DObject
public static GameObject GetHovered3DObject(Vector2 cursorPos, Camera camera=null)
{
if(camera==null) camera=Camera.main;
if(camera==null) return null;
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(cursorPos);
if(Physics.Raycast(ray, out hit)) return hit.collider.gameObject;
return null;
}
示例15: Pick
public static RaycastHit Pick( Camera camera, Vector3 point )
{
Ray ray = camera.ScreenPointToRay( point );
RaycastHit hit;
if( Physics.Raycast( ray, out hit ) ) {
return hit;
}
return DefaultRaycastHit;
}