本文整理汇总了C#中UnityEngine.Camera.WorldToViewportPoint方法的典型用法代码示例。如果您正苦于以下问题:C# Camera.WorldToViewportPoint方法的具体用法?C# Camera.WorldToViewportPoint怎么用?C# Camera.WorldToViewportPoint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Camera
的用法示例。
在下文中一共展示了Camera.WorldToViewportPoint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: avoidsEmptyCenters
/*
* Considering the following 3 x 3 rule of thirds grid:
* 0 | 1 | 2
* -----------
* 3 | 4 | 5
* -----------
* 6 | 7 | 8
*
* Higher scoring images have average or better densities of objects in sector 4.
*/
public static float avoidsEmptyCenters(GameObject subject, List<GameObject> visibleObjects, Camera cam) {
// First, get average object density for each sector
List<float> sectorDensity = new List<float>(new float[9]);
// The value of sectorBoundaries is a quartet of floats such that:
// value[0] and value[1] are the min and max x values
// value[2] and value[3] are the min and max y values
Dictionary<int, List<float>> sectorBoundaries = new Dictionary<int, List<float>> ();
sectorBoundaries.Add (0, new List<float> (){0.000f, 0.333f, 0.666f, 1.000f});
sectorBoundaries.Add (1, new List<float> (){0.333f, 0.666f, 0.666f, 1.000f});
sectorBoundaries.Add (2, new List<float> (){0.666f, 1.000f, 0.666f, 1.000f});
sectorBoundaries.Add (3, new List<float> (){0.000f, 0.333f, 0.333f, 0.666f});
sectorBoundaries.Add (4, new List<float> (){0.333f, 0.666f, 0.333f, 0.666f});
sectorBoundaries.Add (5, new List<float> (){0.666f, 1.000f, 0.333f, 0.666f});
sectorBoundaries.Add (6, new List<float> (){0.000f, 0.333f, 0.000f, 0.333f});
sectorBoundaries.Add (7, new List<float> (){0.333f, 0.666f, 0.000f, 0.333f});
sectorBoundaries.Add (8, new List<float> (){0.666f, 1.000f, 0.000f, 0.333f});
// Compute object density for each sector (just the sum of every object in that sector)
foreach (GameObject obj in visibleObjects) {
foreach (var sectorBoundary in sectorBoundaries) {
// WorldToViewportPoint gives x and y values between 0 and 1.000
Vector3 viewportPoint = cam.WorldToViewportPoint (obj.transform.position);
if (viewportPoint.x >= sectorBoundary.Value[0] && viewportPoint.x < sectorBoundary.Value[1]
&& viewportPoint.y >= sectorBoundary.Value[2] && viewportPoint.y < sectorBoundary.Value[3]) {
sectorDensity [sectorBoundary.Key] += 1;
break;
}
}
}
// Compute the average sector density for each sector BUT sector 4
float averageSectorDensity = (sectorDensity [0]
+ sectorDensity [1]
+ sectorDensity [2]
+ sectorDensity [3]
+ sectorDensity [5]
+ sectorDensity [6]
+ sectorDensity [7]
+ sectorDensity [8]) / 8f;
if (averageSectorDensity < 0.125f) {
averageSectorDensity = 0.06f;
}
float score = sectorDensity [4] / averageSectorDensity;
// Guard against potential negative values
if (score < 0f) {
return 0f;
}
return score;
}
示例2: nearby
bool nearby(Camera podCam)
{
//doing this since we don't have to be extremely precise with the max distance where the label is visible.
Vector3 viewport = podCam.WorldToViewportPoint(pod.transform.position + 3*Vector3.up);
Vector3 difference = podCam.transform.position - pod.transform.position;
if (Mathf.Abs (difference.x) <maxDist && Mathf.Abs (difference.z) <maxDist && Mathf.Abs (difference.x) >minDist && Mathf.Abs (difference.z) >minDist && viewport.z>viewportZThreshold)
return true;
else return false;
}
示例3: WorldToCanvasPosition
private static Vector2 WorldToCanvasPosition (RectTransform canvas, Camera camera, Vector3 position)
{
Vector2 retVAl = camera.WorldToViewportPoint(position);
retVAl.x *= canvas.sizeDelta.x;
retVAl.y *= canvas.sizeDelta.y;
retVAl.x -= canvas.sizeDelta.x * canvas.pivot.x;
retVAl.y -= canvas.sizeDelta.y * canvas.pivot.y;
return retVAl;
}
示例4: ConnectUV
public void ConnectUV(PortalBase pair, Camera cam)
{
var pair2world = pair.transform.localToWorldMatrix;
var vertices = pair._vertices;
var vertexCount = vertices.Length;
var uvs = pair._uvs;
for (var i = 0; i < vertexCount; i++)
uvs[i] = (Vector2)cam.WorldToViewportPoint(pair2world.MultiplyPoint3x4(vertices[i]));
_mesh.uv = uvs;
pair._mesh.uv = uvs;
}
示例5: UpdateWithData
public void UpdateWithData(RectTransform parentRect = null, Camera minimapCamera = null, Vector3 offset = default(Vector3), Transform unitTransform = null)
{
// Transform this position into minimap-space
var viewportPoint = minimapCamera.WorldToViewportPoint (unitTransform.position + offset);
// Now transform this viewport point to parent rect space
var width = parentRect.rect.width;
var height = parentRect.rect.height;
var newPosition = new Vector2 (-width / 2 + width * viewportPoint.x, -height / 2 + height * viewportPoint.y);
// Set this position;
rectTransform.anchoredPosition = newPosition;
}
示例6: DrawPlane
/**
* Returns the distance this grid is drawing
*/
public static float DrawPlane(Camera cam, Vector3 pivot, Vector3 tangent, Vector3 bitangent, float snapValue, Color color, float alphaBump)
{
if(!gridMesh || !gridMaterial || !gridObject)
Init();
gridMaterial.SetFloat("_AlphaCutoff", .1f);
gridMaterial.SetFloat("_AlphaFade", .6f);
pivot = pg_Util.SnapValue(pivot, snapValue);
Vector3 p = cam.WorldToViewportPoint(pivot);
bool inFrustum = (p.x >= 0f && p.x <= 1f) &&
(p.y >= 0f && p.y <= 1f) &&
p.z >= 0f;
float[] distances = GetDistanceToFrustumPlanes(cam, pivot, tangent, bitangent, 24f);
if(inFrustum)
{
tan_iter = (int)(Mathf.Ceil( (Mathf.Abs(distances[0]) + Mathf.Abs(distances[2]))/snapValue ));
bit_iter = (int)(Mathf.Ceil( (Mathf.Abs(distances[1]) + Mathf.Abs(distances[3]))/snapValue ));
max = Mathf.Max( tan_iter, bit_iter );
// if the max is around 3x greater than min, we're probably skewing the camera at near-plane
// angle, so use the min instead.
if(max > Mathf.Min(tan_iter, bit_iter) * 2)
max = (int) Mathf.Min(tan_iter, bit_iter) * 2;
div = 1;
float dot = Vector3.Dot( cam.transform.position-pivot, Vector3.Cross(tangent, bitangent) );
if(max > MAX_LINES)
{
if(Vector3.Distance(cam.transform.position, pivot) > 50f * snapValue && Mathf.Abs(dot) > .8f)
{
while(max/div > MAX_LINES)
div += div;
}
else
{
max = MAX_LINES;
}
}
}
// origin, tan, bitan, increment, iterations, divOffset, color, primary alpha bump
DrawFullGrid(cam, pivot, tangent, bitangent, snapValue*div, max/div, div, color, alphaBump);
return ((snapValue*div)*(max/div));
}
示例7: LateUpdate
public void LateUpdate() {
if(!enabled || m_Target == null){
return;
}
m_WorldCamera = NGUITools.FindCameraForLayer(m_Target.layer);
m_Position = m_WorldCamera.WorldToViewportPoint(m_Target.transform.position);
m_Position = m_GUICamera.ViewportToWorldPoint(m_Position);
m_Position.z = 0.0f;
r_Transform.position = m_Position + m_Offset;
}
示例8: WorldToCanvas
public static Vector2 WorldToCanvas(this Canvas canvas, Vector3 world_position, Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
// Debug.Log("Convertendo posicao global para posicao no canvas");
var viewport_position = camera.WorldToViewportPoint(world_position);
var canvas_rect = canvas.GetComponent<RectTransform>();
return new Vector2((viewport_position.x * canvas_rect.sizeDelta.x) - (canvas_rect.sizeDelta.x * 0.5f),
(viewport_position.y * canvas_rect.sizeDelta.y) - (canvas_rect.sizeDelta.y * 0.5f));
}
示例9: GetViewportToWorldPoint
/// <summary>
/// Gets the 3D world position.
/// </summary>
/// <returns>The 3D world position.</returns>
/// <param name="ui">NGUI.</param>
/// <param name="cam">Cam.</param>
public static Vector3 GetViewportToWorldPoint(GameObject ui, Camera cam, Camera guiCam = null)
{
if (guiCam == null) {
guiCam = SceneManager.Instance.NGUICamera;
if (guiCam == null) {
return Vector3.zero;
}
}
Vector3 position = cam.ViewportToWorldPoint (guiCam.WorldToViewportPoint (ui.transform.position));
position.z = 0;
return position;
}
示例10: LateUpdate
public void LateUpdate() {
worldCamera = NGUITools.FindCameraForLayer(this.gameObject.layer);//頭頂物件
guiCamera = NGUITools.FindCameraForLayer(InsVitalBar.gameObject.layer);//2D物件
Vector3 pos = worldCamera.WorldToViewportPoint(this.transform.position); //先知道"target3D物件"的螢幕位置(空物件)
if(pos.z < 0.0f) {
pos = new Vector3(-10000.0f, -10000.0f, -10000.0f); //給予一個很大的值讓他不要在畫面上,也可以做成不顯示
} else {
pos = guiCamera.ViewportToWorldPoint(pos); //再把"target3D物件的螢幕位置"投射到3D位置
pos.z = 0.0f;
}
InsVitalBar.transform.position = pos; //最後將該位置指給要擺放的2D物件
}
示例11: WorldToNormalizedViewportPoint
public static Vector3 WorldToNormalizedViewportPoint(Camera camera, Vector3 point)
{
point = camera.WorldToViewportPoint(point);
if (camera.isOrthoGraphic)
{
point.z = (2 * (point.z - camera.nearClipPlane) / (camera.farClipPlane - camera.nearClipPlane)) - 1f;
}
else
{
point.z = ((camera.farClipPlane + camera.nearClipPlane) / (camera.farClipPlane - camera.nearClipPlane))
+ (1 / point.z) * (-2 * camera.farClipPlane * camera.nearClipPlane / (camera.farClipPlane - camera.nearClipPlane));
}
return point;
}
示例12: CalculateBoxFromCntr
/// <summary>
/// Calculates the box surrounding the of the unit.
/// </summary>
/// <returns>The box</returns>
/// <param name="cntr">The center of the unit</param>
public static Rect CalculateBoxFromCntr(Vector3 cntr, Camera cam, int pix)
{
Rect box = new Rect();
Vector3 actor_cntr = cam.WorldToViewportPoint(cntr);
actor_cntr = cam.ViewportToScreenPoint(actor_cntr);
Debug.Log(actor_cntr.x + " : " + actor_cntr.y + " : " + actor_cntr.z);
box.xMin = actor_cntr.x - pix;
box.xMax = actor_cntr.x + pix;
box.yMin = Screen.height - actor_cntr.y - pix;
box.yMax = Screen.height - actor_cntr.y + pix;
return box;
}
示例13: SetPosition
public void SetPosition( GameObject target)
{
Debug.Log(target);
this.target = target;
worldCam = NGUITools.FindCameraForLayer( target.layer );
guiCam = NGUITools.FindCameraForLayer( gameObject.layer );
pos = worldCam.WorldToViewportPoint( target.transform.position + new Vector3(0, 1, 0) );
pos = guiCam.ViewportToWorldPoint( pos );
pos.z = 0;
trans.position = pos;
transform.localScale = new Vector3(24,24,1);
Debug.Log(trans.localPosition);
TweenPosition tp = gameObject.GetComponent<TweenPosition>();
Debug.Log(pos);
tp.from = trans.localPosition;
tp.to = trans.localPosition + new Vector3(0, 12, 0);
}
示例14: WorldToCanvas
public static Vector2 WorldToCanvas(this Canvas canvas,
Vector3 worldPosition,
Camera camera = null)
{
if (camera == null)
{
camera = Camera.main;
}
//pos between 0-1
var viewport_position = camera.WorldToViewportPoint(worldPosition);
var canvas_rect = canvas.pixelRect;
//TODO: Use GUIUtility instead
//Need to adjust due to canvas rect center is origin (0,0) whereas viewport bottom left
return new Vector2((viewport_position.x * canvas_rect.width) - (canvas_rect.width * 0.5f),
(viewport_position.y * canvas_rect.height) - (canvas_rect.height * 0.5f));
}
示例15: BoundsToViewportRect
public static Rect BoundsToViewportRect(Bounds bounds, Camera camera = null)
{
if (camera == null)
camera = Camera.main;
Vector3 bmin = bounds.min;
Vector3 bmax = bounds.max;
Vector2 min = new Vector2(float.MaxValue, float.MaxValue);
Vector2 max = new Vector2(float.MinValue, float.MinValue);
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmax.x, bmax.y, bmax.z)));
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmin.x, bmax.y, bmax.z)));
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmax.x, bmin.y, bmax.z)));
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmin.x, bmin.y, bmax.z)));
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmax.x, bmax.y, bmin.z)));
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmin.x, bmax.y, bmin.z)));
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmax.x, bmin.y, bmin.z)));
MinMax(ref min, ref max, camera.WorldToViewportPoint(new Vector3(bmin.x, bmin.y, bmin.z)));
return Rect.MinMaxRect(min.x, min.y, max.x, max.y);
}