本文整理汇总了C#中UnityEngine.Camera.ViewportToWorldPoint方法的典型用法代码示例。如果您正苦于以下问题:C# Camera.ViewportToWorldPoint方法的具体用法?C# Camera.ViewportToWorldPoint怎么用?C# Camera.ViewportToWorldPoint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Camera
的用法示例。
在下文中一共展示了Camera.ViewportToWorldPoint方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetScreenSizeInWorldCoords
public static Vector2 GetScreenSizeInWorldCoords(Camera gameCamera, float distance = 10f)
{
float width = 0f;
float height = 0f;
if (gameCamera.orthographic)
{
if (gameCamera.orthographicSize <= .001f)
return Vector2.zero;
var p1 = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, gameCamera.nearClipPlane));
var p2 = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, gameCamera.nearClipPlane));
var p3 = gameCamera.ViewportToWorldPoint(new Vector3(1, 1, gameCamera.nearClipPlane));
width = (p2 - p1).magnitude;
height = (p3 - p2).magnitude;
}
else
{
height = 2.0f * distance * Mathf.Tan(gameCamera.fieldOfView * 0.5f * Mathf.Deg2Rad);
width = height * gameCamera.aspect;
}
return new Vector2(width, height);
}
示例2: DrawGrid
private void DrawGrid(Camera camera, Vector3 incrementSize, Color colour, bool isMajorGrid = false)
{
if (incrementSize.x <= 0 || incrementSize.y <= 0 || incrementSize.z <= 0)
return;
incrementSize *= settings.SelectedUnit.UnityUnits;
Vector3 down = camera.cameraToWorldMatrix.MultiplyVector(new Vector3(0, -1));
Vector3 right = camera.cameraToWorldMatrix.MultiplyVector(new Vector3(1, 0));
Vector3 topLeft = camera.ViewportToWorldPoint(new Vector3(0, 1, 1));
Vector3 bottomLeft = camera.ViewportToWorldPoint(new Vector3(0, 0, 1));
Vector3 topRight = camera.ViewportToWorldPoint(new Vector3(1, 1, 1));
Vector3 bottomRight = camera.ViewportToWorldPoint(new Vector3(1, 0, 1));
topLeft = Snap(topLeft, incrementSize) + Mul(-down + -right, incrementSize);
bottomLeft = Snap(bottomLeft, incrementSize) + Mul(down + -right, incrementSize);
topRight = Snap(topRight, incrementSize) + Mul(-down + right, incrementSize);
bottomRight = Snap(bottomRight, incrementSize) + Mul(down + right, incrementSize);
float x = camera.orthographicSize * 2 * Screen.width / Screen.height * 1.15f;
float y = camera.orthographicSize * 2;
float xIncrement = LargestComponent(Mask(incrementSize, right));
float yIncrement = LargestComponent(Mask(incrementSize, down));
int xCount = (int)(Mathf.Ceil(x / xIncrement));
int yCount = (int)(Mathf.Ceil(y / yIncrement));
Handles.color = colour;
for (int i = 0; i <= xCount; i++)
{
Vector3 a = topLeft + i * Mul(incrementSize, right);
Vector3 b = bottomLeft + i * Mul(incrementSize, right);
Handles.DrawLine(a, b);
}
for (int j = 0; j <= yCount; j++)
{
Vector3 a = topLeft + j * Mul(down, incrementSize);
Vector3 b = topRight + j * Mul(down, incrementSize);
Handles.DrawLine(a, b);
}
if (settings.DrawAxes)
{
Handles.color = Color.red;
Handles.DrawLine(new Vector3(topLeft.x, 0, 0), new Vector3(topRight.x, 0, 0));
Handles.color = Color.green;
Handles.DrawLine(new Vector3(0, topLeft.y, 0), new Vector3(0, bottomLeft.y, 0));
Handles.color = Color.blue;
Handles.DrawLine(new Vector3(0, 0, topLeft.z), new Vector3(0, 0, bottomRight.z));
}
}
示例3: GetCameraBounds
public static Rect GetCameraBounds(Camera _camera, float _tolerance = 5)
{
Rect _rct = new Rect(0, 0, 0, 0);
Vector3 upperLeft = _camera.ViewportToWorldPoint(new Vector3(0, 0, _camera.farClipPlane));
Vector3 lowerRight = _camera.ViewportToWorldPoint(new Vector3(1, 1, _camera.farClipPlane));
_rct.Set(upperLeft.x - _tolerance, upperLeft.y - _tolerance, lowerRight.x - upperLeft.x + _tolerance * 2, lowerRight.y - upperLeft.y + _tolerance * 2);
return _rct;
}
示例4: DrawCameraFrustrum
static void DrawCameraFrustrum( Camera camera , bool isSelected)
{
Handles.color = (isSelected == true) ? ( Color.grey ) : ( Color.white );
float z = camera.nearClipPlane;
Vector3 v1a = camera.ViewportToWorldPoint(new Vector3(0f, 0f, z));
Vector3 v2a = camera.ViewportToWorldPoint(new Vector3(0f, 1f, z));
Vector3 v3a = camera.ViewportToWorldPoint(new Vector3(1f, 1f, z));
Vector3 v4a = camera.ViewportToWorldPoint(new Vector3(1f, 0f, z));
z = camera.farClipPlane;
Vector3 v1b = camera.ViewportToWorldPoint(new Vector3(0f, 0f, z));
Vector3 v2b = camera.ViewportToWorldPoint(new Vector3(0f, 1f, z));
Vector3 v3b = camera.ViewportToWorldPoint(new Vector3(1f, 1f, z));
Vector3 v4b = camera.ViewportToWorldPoint(new Vector3(1f, 0f, z));
Handles.DrawLine(v1a, v1b);
Handles.DrawLine(v2a, v2b);
Handles.DrawLine(v3a, v3b);
Handles.DrawLine(v4a, v4b);
Handles.DrawLine(v1a, v2a);
Handles.DrawLine(v1a, v4a);
Handles.DrawLine(v3a, v2a);
Handles.DrawLine(v3a, v4a);
Handles.DrawLine(v1b, v2b);
Handles.DrawLine(v1b, v4b);
Handles.DrawLine(v3b, v2b);
Handles.DrawLine(v3b, v4b);
}
示例5: GetCorners
/// <summary>
/// Gets the corners.
/// </summary>
/// <param name="camera">Camera.</param>
/// <param name="distance">Distance.</param>
/// <param name="corners">Corners.</param>
public static void GetCorners(Camera camera, float distance, ref Vector3[] corners)
{
Array.Resize(ref corners, 4);
// Top left
corners[0] = camera.ViewportToWorldPoint(new Vector3(0, 1, distance));
// Top right
corners[1] = camera.ViewportToWorldPoint(new Vector3(1, 1, distance));
// Bottom left
corners[2] = camera.ViewportToWorldPoint(new Vector3(0, 0, distance));
// Bottom right
corners[3] = camera.ViewportToWorldPoint(new Vector3(1, 0, distance));
}
示例6: Start
void Start ()
{
camera = GetComponent<Camera> ();
vertExtent = camera.orthographicSize;
horzExtent = vertExtent * Screen.width / Screen.height;
deltaCenterVec = camera.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 0))
- camera.ViewportToWorldPoint (new Vector3 (cameraCenterX, cameraCenterY, 0));
isFollowHorizontal = (followType & Direction.Horizontal) == Direction.Horizontal;
isFollowVertical = (followType & Direction.Vertical) == Direction.Vertical;
isBoundHorizontal = (boundType & Direction.Horizontal) == Direction.Horizontal;
isBoundVertical = (boundType & Direction.Vertical) == Direction.Vertical;
isDeadZoneHorizontal = ((deadZoneType & Direction.Horizontal) == Direction.Horizontal) && isFollowHorizontal;
isDeadZoneVertical = ((deadZoneType & Direction.Vertical) == Direction.Vertical) && isFollowVertical;
tempVec = Vector3.one;
}
示例7: 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;
}
示例8: GetFrustum
private static bool GetFrustum(Camera camera, Vector3[] near, Vector3[] far, out float frustumAspect)
{
frustumAspect = GetFrustumAspectRatio(camera);
if (frustumAspect < 0f)
{
return false;
}
if (far != null)
{
far[0] = new Vector3(0f, 0f, camera.farClipPlane);
far[1] = new Vector3(0f, 1f, camera.farClipPlane);
far[2] = new Vector3(1f, 1f, camera.farClipPlane);
far[3] = new Vector3(1f, 0f, camera.farClipPlane);
for (int i = 0; i < 4; i++)
{
far[i] = camera.ViewportToWorldPoint(far[i]);
}
}
if (near != null)
{
near[0] = new Vector3(0f, 0f, camera.nearClipPlane);
near[1] = new Vector3(0f, 1f, camera.nearClipPlane);
near[2] = new Vector3(1f, 1f, camera.nearClipPlane);
near[3] = new Vector3(1f, 0f, camera.nearClipPlane);
for (int j = 0; j < 4; j++)
{
near[j] = camera.ViewportToWorldPoint(near[j]);
}
}
return true;
}
示例9: Start
// Use this for initialization
void Start()
{
//initialize the player character
Vector3 charPos = new Vector3(25.5f, -1.8f, 2.0f);
characters.Add((GameObject)Instantiate(Character, charPos, Quaternion.identity));
//set camera to follow the Camera Target
Vector3 camPos = new Vector3(25.5f, -1.8f, 0.0f);
cameraTargets.Add((GameObject)Instantiate(CamTarget, camPos, Quaternion.identity));
Transform targetTransform = cameraTargets[0].transform;
mainCam = Camera.allCameras[0];
mainCam.GetComponent<SmoothFollow>().target = targetTransform;
//Find the light
light = GameObject.Find("Directional Light");
//initialize the front wall
Vector3 wallPos = mainCam.ViewportToWorldPoint(new Vector3(1.0f, 0.0f, 0.0f));
wallPos.z = 2.0f; // resets to platform Z
wallPos.y = -3.0f; // brings wall to ground
wallPos.x *= 1.6f; // moves wall half of screen width away from camera
frontWalls.Add((GameObject)Instantiate(FrontWall, wallPos, Quaternion.identity));
frontWalls[0].transform.Rotate(0.0f, 0.0f, 90.0f);
//initialize the kill zone
Vector3 killZonePos = mainCam.ScreenToViewportPoint(new Vector3(0.0f, 0.0f, 2.0f));
killZonePos.x += 7.0f; // move slightly right from current lock location
KillZones.Add((GameObject)Instantiate(KillZone, killZonePos, Quaternion.identity));
killZones[0].transform.Rotate(0.0f, 0.0f, 90.0f);
// create bottom killzone
Vector3 underworld = new Vector3(0.0f, -11.5f, 2.0f);
killZones.Add((GameObject)Instantiate(KillZone, underworld, Quaternion.identity));
// create knife
knifeSpawn = new Vector3(killZonePos.x, killZonePos.y, killZonePos.z);
knifeSpawn.x += 2.0f; // move slightly right from current lock location
knife = (GameObject)Instantiate(Knife, knifeSpawn, Quaternion.identity);
knife.transform.localScale = new Vector3(0.6f, 0.6f, 0.6f);
thrown = false;
// create chef
chefSpawn = new Vector3(killZonePos.x, killZonePos.y, killZonePos.z);
chefSpawn.x += 2.0f; // move slightly right from current lock location
Chef = (GameObject)Instantiate(Chef, chefSpawn, Quaternion.identity);
chefOnScreen = false;
chefMovingOut = false;
mainCam.aspect = (1920f / 910f);
//initialize the game fragments
for (int i = 0; i < numFrags; i++)
{
Vector3 fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
//make the first two fragments flat
if (i == 0 || i == 1) fragments.Add((GameObject)Instantiate(GameFragments[0], fragPos, Quaternion.identity));
else
{
float fragType = Random.Range(0.0f, (float)GameFragments.Count);
if (fragType >= 0 && fragType < 1)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[0], fragPos, Quaternion.identity));
}
else if (fragType >= 1 && fragType < 2)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[1], fragPos, Quaternion.identity));
}
else if (fragType >= 2 && fragType < 3)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[2], fragPos, Quaternion.identity));
}
else if (fragType >= 3 && fragType < 4)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[3], fragPos, Quaternion.identity));
}
else if (fragType >= 4 && fragType < 5)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[4], fragPos, Quaternion.identity));
}
else if (fragType >= 5 && fragType < 6)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[5], fragPos, Quaternion.identity));
}
else if (fragType >= 6 && fragType < 7)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[6], fragPos, Quaternion.identity));
}
else if (fragType >= 7 && fragType < 8)
{
fragPos = new Vector3(i * 25.0f, -3.5f, 2.0f);
fragments.Add((GameObject)Instantiate(GameFragments[7], fragPos, Quaternion.identity));
//.........这里部分代码省略.........
示例10: GetFrustum
private static bool GetFrustum(Camera camera, Vector3[] near, Vector3[] far, out float frustumAspect)
{
frustumAspect = CameraEditor.GetFrustumAspectRatio(camera);
if ((double) frustumAspect < 0.0)
return false;
if (far != null)
{
far[0] = new Vector3(0.0f, 0.0f, camera.farClipPlane);
far[1] = new Vector3(0.0f, 1f, camera.farClipPlane);
far[2] = new Vector3(1f, 1f, camera.farClipPlane);
far[3] = new Vector3(1f, 0.0f, camera.farClipPlane);
for (int index = 0; index < 4; ++index)
far[index] = camera.ViewportToWorldPoint(far[index]);
}
if (near != null)
{
near[0] = new Vector3(0.0f, 0.0f, camera.nearClipPlane);
near[1] = new Vector3(0.0f, 1f, camera.nearClipPlane);
near[2] = new Vector3(1f, 1f, camera.nearClipPlane);
near[3] = new Vector3(1f, 0.0f, camera.nearClipPlane);
for (int index = 0; index < 4; ++index)
near[index] = camera.ViewportToWorldPoint(near[index]);
}
return true;
}