本文整理汇总了C#中UnityEngine.Camera.WorldToScreenPoint方法的典型用法代码示例。如果您正苦于以下问题:C# Camera.WorldToScreenPoint方法的具体用法?C# Camera.WorldToScreenPoint怎么用?C# Camera.WorldToScreenPoint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Camera
的用法示例。
在下文中一共展示了Camera.WorldToScreenPoint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScreenPoint
public static Vector2 ScreenPoint(Camera mainCamera, Vector3 worldPosition, RectTransform parent, Canvas canvas)
{
var screenPosition = mainCamera.WorldToScreenPoint(worldPosition);
var localPosition = Vector2.zero;
RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, screenPosition, canvas.worldCamera, out localPosition);
return localPosition;
}
示例2: Update
public void Update(IList<Transform> points, Camera cam)
{
_count = Mathf.Min(_capacity, points.Count);
for (var i = 0; i < _count; i++)
_pointData[i] = (Vector2)cam.WorldToScreenPoint(points[i].position);
Upload();
}
示例3: WorldToScreenPoint
public static Vector2 WorldToScreenPoint(Camera cam, Vector3 worldPoint)
{
if (cam == null)
{
return new Vector2(worldPoint.x, worldPoint.y);
}
return cam.WorldToScreenPoint(worldPoint);
}
示例4: checkVisible
public static bool checkVisible(Collider collider, Camera mainCam)
{
Vector3 screenPos = mainCam.WorldToScreenPoint(collider.bounds.center);
Ray ray = mainCam.ScreenPointToRay(screenPos);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
return collider == hit.collider;
else
return false;
}
示例5: ReTranslateFromCamera
public static Vector3 ReTranslateFromCamera(Vector3 inPos, Camera sourceCamera, Camera targetCamera, float depth)
{
Vector3 sourceScreen = sourceCamera.WorldToScreenPoint(inPos);
Ray sourceRay = targetCamera.ScreenPointToRay(sourceScreen);
Vector3 position = (sourceRay.origin + sourceRay.direction * depth);
return position;
}
示例6: ScreenBounds
/// <summary>
/// Returns the screenspace bounds for the given bounding box.
/// </summary>
/// <param name="camera">Camera.</param>
/// <param name="bounds">Bounds.</param>
public static Bounds ScreenBounds(Camera camera, Bounds bounds)
{
BoundsUtils.GetCorners(bounds, ref s_BoundsCorners);
Array.Resize(ref s_ScreenCorners, s_BoundsCorners.Length);
for (int index = 0; index < s_BoundsCorners.Length; index++)
s_ScreenCorners[index] = camera.WorldToScreenPoint(s_BoundsCorners[index]);
return BoundsUtils.GetBounds(s_ScreenCorners);
}
示例7: LoadContextInfo
public bool LoadContextInfo( SceneView sceneView ) {
R2DD_State state = R2DD_State.Instance;
this.sceneView = sceneView;
cam = sceneView.camera;
editorCameraSize = cam.orthographicSize;
gridPixelSquareSize = cam.WorldToScreenPoint( new Vector3(1, 1) ) - cam.WorldToScreenPoint( new Vector3(0, 0) );
scale = Vector3.one;
origin = Vector3.zero;
Context context = state.context;
if( context.type == ContextType.Canvas ) {
if( context.gameObject == null ) {
return false;
}
scale = context.gameObject.transform.lossyScale;
origin = context.gameObject.GetComponent<RectTransform>().position;
if( scale.x <= 0.001f || scale.y <= 0.001f ) {
return false;
}
}
else if( context.type == ContextType.NGUI ) {
if( context.gameObject == null ) {
return false;
}
scale = context.gameObject.transform.lossyScale;
origin = context.gameObject.GetComponent<Transform>().position;
}
LoadPixelFrame();
LoadWorldUnits();
return true;
}
示例8: HandleCameraMouseDrag
private void HandleCameraMouseDrag(CameraState cameraState, Camera cam)
{
Event current = Event.current;
switch (this.m_CurrentViewTool)
{
case ViewTool.Orbit:
this.OrbitCameraBehavior(cameraState, cam);
break;
case ViewTool.Pan:
{
cameraState.FixNegativeSize();
Vector3 position = cam.WorldToScreenPoint(cameraState.pivot.value) + new Vector3(-Event.current.delta.x, Event.current.delta.y, 0f);
Vector3 vector7 = cam.ScreenToWorldPoint(position) - cameraState.pivot.value;
if (current.shift)
{
vector7 = (Vector3) (vector7 * 4f);
}
AnimVector3 pivot = cameraState.pivot;
pivot.value += vector7;
break;
}
case ViewTool.Zoom:
{
float num = HandleUtility.niceMouseDeltaZoom * (!current.shift ? ((float) 3) : ((float) 9));
this.m_TotalMotion += num;
if (this.m_TotalMotion >= 0f)
{
cameraState.viewSize.value += (num * this.m_ZoomSpeed) * 0.003f;
break;
}
cameraState.viewSize.value = this.m_StartZoom * (1f + (this.m_TotalMotion * 0.001f));
break;
}
case ViewTool.FPS:
{
Vector3 vector = cameraState.pivot.value - ((Vector3) ((cameraState.rotation.value * Vector3.forward) * cameraState.GetCameraDistance()));
Quaternion quaternion = cameraState.rotation.value;
quaternion = Quaternion.AngleAxis((current.delta.y * 0.003f) * 57.29578f, (Vector3) (quaternion * Vector3.right)) * quaternion;
quaternion = Quaternion.AngleAxis((current.delta.x * 0.003f) * 57.29578f, Vector3.up) * quaternion;
cameraState.rotation.value = quaternion;
cameraState.pivot.value = vector + ((Vector3) ((quaternion * Vector3.forward) * cameraState.GetCameraDistance()));
break;
}
}
current.Use();
}
示例9: DistancePoint2DToLine
public static float DistancePoint2DToLine(Camera cam, Vector2 mousePosition, Vector3 worldPosition1, Vector3 worldPosition2)
{
Vector2 v0 = cam.WorldToScreenPoint(worldPosition1);
Vector2 v1 = cam.WorldToScreenPoint(worldPosition2);
return pb_Math.DistancePointLineSegment(mousePosition, v0, v1);
}
示例10: EdgeRaycast
/**
* Checks if mouse is over an edge, and if so, returns true setting @edge.
*/
public static bool EdgeRaycast(Camera cam, Vector2 mousePosition, pb_Object mesh, pb_Edge[] edges, Vector3[] verticesInWorldSpace, out pb_Edge edge)
{
Vector3 v0, v1;
float bestDistance = Mathf.Infinity;
float distance = 0f;
edge = null;
GameObject go = ObjectRaycast(cam, mousePosition, (GameObject[]) Resources.FindObjectsOfTypeAll(typeof(GameObject)));
if( go == null || go != mesh.gameObject)
{
int width = Screen.width;
int height = Screen.height;
for(int i = 0; i < edges.Length; i++)
{
v0 = verticesInWorldSpace[edges[i].x];
v1 = verticesInWorldSpace[edges[i].y];
distance = pb_HandleUtility.DistancePoint2DToLine(cam, mousePosition, v0, v1);
if ( distance < bestDistance && distance < MAX_EDGE_SELECT_DISTANCE )// && !PointIsOccluded(mesh, (v0+v1)*.5f) )
{
Vector3 vs0 = cam.WorldToScreenPoint(v0);
// really simple frustum check (will fail on edges that have vertices outside the frustum but is visible)
if( vs0.z <= 0 || vs0.x < 0 || vs0.y < 0 || vs0.x > width || vs0.y > height )
continue;
Vector3 vs1 = cam.WorldToScreenPoint(v1);
if( vs1.z <= 0 || vs1.x < 0 || vs1.y < 0 || vs1.x > width || vs1.y > height )
continue;
bestDistance = distance;
edge = edges[i];
}
}
}
else
{
// Test culling
List<pb_RaycastHit> hits;
Ray ray = cam.ScreenPointToRay(mousePosition);// HandleUtility.GUIPointToWorldRay(mousePosition);
if( FaceRaycast(ray, mesh, out hits, Mathf.Infinity, Culling.FrontBack) )
{
// Sort from nearest hit to farthest
hits.Sort( (x, y) => x.Distance.CompareTo(y.Distance) );
// Find the nearest edge in the hit faces
Vector3[] v = mesh.vertices;
for(int i = 0; i < hits.Count; i++)
{
if( pb_HandleUtility.PointIsOccluded(cam, mesh, mesh.transform.TransformPoint(hits[i].Point)) )
continue;
foreach(pb_Edge e in mesh.faces[hits[i].FaceIndex].GetEdges())
{
float d = pb_Math.DistancePointLineSegment(hits[i].Point, v[e.x], v[e.y]);
if(d < bestDistance)
{
bestDistance = d;
edge = e;
}
}
if( Vector3.Dot(ray.direction, mesh.transform.TransformDirection(hits[i].Normal)) < 0f )
break;
}
if(edge != null && pb_HandleUtility.DistancePoint2DToLine(cam, mousePosition, mesh.transform.TransformPoint(v[edge.x]), mesh.transform.TransformPoint(v[edge.y])) > MAX_EDGE_SELECT_DISTANCE)
{
edge = null;
}
// else
// {
// edge.x = mesh.ToUserIndex(edge.x);
// edge.y = mesh.ToUserIndex(edge.y);
// }
}
}
return edge != null;
}
示例11: GetOverlayPosition
/// <summary>
/// Get position of worldpoint at gui relative to widget container.
/// </summary>
/// <param name="worldCamera">World camera.</param>
/// <param name="worldPoint">World point.</param>
/// <param name="widgetContainer">Widget container, can be null.</param>
public Vector3 GetOverlayPosition(Camera worldCamera, Vector3 worldPoint, Transform widgetContainer)
{
if ((object) worldCamera == null) {
return worldPoint;
}
var pos = worldCamera.WorldToScreenPoint (worldPoint);
pos = Camera.ScreenToWorldPoint (pos);
if ((object) widgetContainer != null) {
pos = widgetContainer.InverseTransformPoint (pos);
}
pos.z = 0f;
return pos;
}
示例12: ScaleObjectRelative
// This allows you to scale an object by a change in pinch scale, relative to a point on the screen
public static void ScaleObjectRelative(Transform transform, float scale, Vector2 referencePoint, Camera camera = null)
{
if (transform != null && scale != 1.0f)
{
if (camera == null) camera = Camera.main;
if (camera != null)
{
// Find screen position of transform
var localPosition = camera.WorldToScreenPoint(transform.position);
// Scale screen position away from referencePoint
localPosition.x = referencePoint.x + (localPosition.x - referencePoint.x) * scale;
localPosition.y = referencePoint.y + (localPosition.y - referencePoint.y) * scale;
// Update position
transform.position = camera.ScreenToWorldPoint(localPosition);
// Scale up
transform.localScale *= scale;
}
}
}
示例13: GetPixelWidth
private float GetPixelWidth(Transform cachedTransform, Camera cachedCamera)
{
var position = cachedTransform.position;
var screenPos = cachedCamera.WorldToScreenPoint(position - cachedTransform.forward * .01f);
var offset = Vector3.zero;
if (screenPos.x > 0)
offset = screenPos - Vector3.right;
else
offset = screenPos + Vector3.right;
if (screenPos.y > 0)
offset = screenPos - Vector3.up;
else
offset = screenPos + Vector3.up;
offset = cachedCamera.ScreenToWorldPoint(offset);
return 1f / (cachedTransform.InverseTransformPoint(position) - cachedTransform.InverseTransformPoint(offset)).magnitude;
}
示例14: convertScreenPoint
/// <summary>
/// Converts the screen point.
/// </summary>
/// <returns>The screen point.</returns>
/// <param name="screenPoint">Screen point.</param>
/// <param name="quad">Quad.</param>
/// <param name="cam">Cam.</param>
static Point convertScreenPoint(Point screenPoint, GameObject quad, Camera cam)
{
Vector2 tl;
Vector2 tr;
Vector2 br;
Vector2 bl;
tl = cam.WorldToScreenPoint (new Vector3 (quad.transform.localPosition.x - quad.transform.localScale.x / 2, quad.transform.localPosition.y + quad.transform.localScale.y / 2, quad.transform.localPosition.z));
tr = cam.WorldToScreenPoint (new Vector3 (quad.transform.localPosition.x + quad.transform.localScale.x / 2, quad.transform.localPosition.y + quad.transform.localScale.y / 2, quad.transform.localPosition.z));
br = cam.WorldToScreenPoint (new Vector3 (quad.transform.localPosition.x + quad.transform.localScale.x / 2, quad.transform.localPosition.y - quad.transform.localScale.y / 2, quad.transform.localPosition.z));
bl = cam.WorldToScreenPoint (new Vector3 (quad.transform.localPosition.x - quad.transform.localScale.x / 2, quad.transform.localPosition.y - quad.transform.localScale.y / 2, quad.transform.localPosition.z));
Mat srcRectMat = new Mat (4, 1, CvType.CV_32FC2);
Mat dstRectMat = new Mat (4, 1, CvType.CV_32FC2);
srcRectMat.put (0, 0, tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
dstRectMat.put (0, 0, 0.0, 0.0, quad.transform.localScale.x, 0.0, quad.transform.localScale.x, quad.transform.localScale.y, 0.0, quad.transform.localScale.y);
Mat perspectiveTransform = Imgproc.getPerspectiveTransform (srcRectMat, dstRectMat);
// Debug.Log ("srcRectMat " + srcRectMat.dump ());
// Debug.Log ("dstRectMat " + dstRectMat.dump ());
// Debug.Log ("perspectiveTransform " + perspectiveTransform.dump ());
MatOfPoint2f srcPointMat = new MatOfPoint2f (screenPoint);
MatOfPoint2f dstPointMat = new MatOfPoint2f ();
Core.perspectiveTransform (srcPointMat, dstPointMat, perspectiveTransform);
// Debug.Log ("srcPointMat " + srcPointMat.dump ());
// Debug.Log ("dstPointMat " + dstPointMat.dump ());
return dstPointMat.toArray () [0];
}
示例15: ShowAnswer
public void ShowAnswer( EAnswer i_answer, Vector3 i_position, Camera i_camera = null )
{
var ui = FindUnunseAnswerUI();
var rect = ui.GetComponent< RectTransform >();
if( i_camera == null )
{
i_camera = Camera.main;
}
var screenPos = i_camera.WorldToScreenPoint( i_position );
var localPos = Vector2.zero;
RectTransformUtility.ScreenPointToLocalPointInRectangle( m_answerParent, screenPos, null, out localPos );
rect.localPosition = localPos;
ui.sprite = GetAnswerUISprite( i_answer );
ui.gameObject.SetActive( true );
}