本文整理汇总了C#中Camera.ScreenToWorldPoint方法的典型用法代码示例。如果您正苦于以下问题:C# Camera.ScreenToWorldPoint方法的具体用法?C# Camera.ScreenToWorldPoint怎么用?C# Camera.ScreenToWorldPoint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Camera
的用法示例。
在下文中一共展示了Camera.ScreenToWorldPoint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Awake
float rightBoundary; //right boundary of screen
#endregion Fields
#region Methods
void Awake()
{
playerRef = GameObject.FindGameObjectWithTag ("Player").transform;
mainCam = Camera.main;
leftBoundary = mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - offset;
rightBoundary = mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + offset;
}
示例2: CreateFullScreenQuadMesh
public static Mesh CreateFullScreenQuadMesh(Camera camera)
{
float z = 10;
// 0 - 1
// 2 - 3
Vector3[] verts = new Vector3[4];
verts [0] = camera.ScreenToWorldPoint (new Vector3 (0, 0, z));
verts [1] = camera.ScreenToWorldPoint (new Vector3 (camera.pixelWidth, 0, z));
verts [2] = camera.ScreenToWorldPoint (new Vector3 (0, camera.pixelHeight, z));
verts [3] = camera.ScreenToWorldPoint (new Vector3 (camera.pixelWidth, camera.pixelHeight, z));
Vector2[] uvs = new Vector2[4];
uvs [0] = new Vector2 (0f, 0f);
uvs [1] = new Vector2 (1f, 0f);
uvs [2] = new Vector2 (0f, 1f);
uvs [3] = new Vector2 (1f, 1f);
int[] tris = new int[6] { 2, 1, 0, 3, 1, 2 };
Mesh mesh = new Mesh ();
mesh.vertices = verts;
mesh.uv = uvs;
mesh.triangles = tris;
mesh.RecalculateNormals ();
return mesh;
}
示例3: SetCameraBounds
// This function is used by camBounds to set _camBounds and can also be
// called directly
public static void SetCameraBounds(Camera cam = null) {
// If no Camera, use the main camera
if (cam == null) cam = Camera.main;
// This makes a couple of important assumptions about the camera
// 1. Camera is orthographic
// 2. Camera is at a rotation of R:[0,0,0]
// Make Vector3s at the topLeft and bottomRight of the screen coords
Vector3 topLeft = new Vector3(0,0,0);
Vector3 bottomRight = new Vector3 (Screen.width, Screen.height, 0);
// Convert these to world coordinates
Vector3 boundTLN = cam.ScreenToWorldPoint(topLeft);
Vector3 boundBRF = cam.ScreenToWorldPoint (bottomRight);
// Adjust their zs to be at the near and far camera clipping planes
boundTLN.z += cam.nearClipPlane;
boundBRF.z += cam.farClipPlane;
// Find the center of the Bounds
Vector3 center = (boundTLN + boundBRF)/2f;
_camBounds = new Bounds (center, Vector3.zero);
// Expand _camBounds to encapsulate the extents.
_camBounds.Encapsulate(boundTLN);
_camBounds.Encapsulate (boundBRF);
}
示例4: Start
void Start()
{
mainCamera = GetComponent<Camera> ();
//Create a Dictionary to contain all our Objects/Transforms
System.Collections.Generic.Dictionary<string,Transform> colliders = new System.Collections.Generic.Dictionary<string,Transform>();
//Create our GameObjects and add their Transform components to the Dictionary we created above
colliders.Add("Top",new GameObject().transform);
colliders.Add("Bottom",new GameObject().transform);
colliders.Add("Right",new GameObject().transform);
colliders.Add("Left",new GameObject().transform);
//Generate world space point information for position and scale calculations
Vector3 cameraPos = mainCamera.transform.position;
screenSize.x = Vector3.Distance (mainCamera.ScreenToWorldPoint(new Vector3(0,0,-cameraPos.z)),mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0,-cameraPos.z))) * 0.5F; //Grab the world-space position values of the start and end positions of the screen, then calculate the distance between them and store it as half, since we only need half that value for distance away from the camera to the edge
screenSize.y = Vector3.Distance (mainCamera.ScreenToWorldPoint(new Vector3(0,0,-cameraPos.z)),mainCamera.ScreenToWorldPoint(new Vector3(0, Screen.height,-cameraPos.z))) * 0.5F;
//For each Transform/Object in our Dictionary
foreach(KeyValuePair<string,Transform> valPair in colliders){
valPair.Value.gameObject.AddComponent<BoxCollider>().material = physicsMaterial; //Add our colliders. Remove the "2D", if you would like 3D colliders.
valPair.Value.name = valPair.Key + "Collider"; //Set the object's name to it's "Key" name, and take on "Collider". i.e: TopCollider
valPair.Value.parent = transform; //Make the object a child of whatever object this script is on (preferably the camera)
if(valPair.Key == "Left" || valPair.Key == "Right") //Scale the object to the width and height of the screen, using the world-space values calculated earlier
valPair.Value.localScale = new Vector3(colThickness, screenSize.y * 2, 10*colThickness);
else
valPair.Value.localScale = new Vector3(screenSize.x * 2, 10*colThickness, colThickness);
}
//Change positions to align perfectly with outter-edge of screen, adding the world-space values of the screen we generated earlier, and adding/subtracting them with the current camera position, as well as add/subtracting half out objects size so it's not just half way off-screen
colliders["Right"].position = new Vector3(cameraPos.x + screenSize.x + (colliders["Right"].localScale.x * 0.5f), cameraPos.y, zPosition);
colliders["Left"].position = new Vector3(cameraPos.x - screenSize.x - (colliders["Left"].localScale.x * 0.5f), cameraPos.y, zPosition);
colliders["Top"].position = new Vector3(cameraPos.x, cameraPos.y + screenSize.y + (colliders["Top"].localScale.y * 0.5f), zPosition);
colliders["Bottom"].position = new Vector3(cameraPos.x, cameraPos.y - screenSize.y - (colliders["Bottom"].localScale.y * 0.5f), zPosition);
}
示例5: Start
//Runs as soon as the program starts
void Start()
{
mainCamera = FindObjectOfType<Camera> ();
gameManager = FindObjectOfType<GameManager> ();
StartingLocation ();
topWall = (new Vector3 (0f,mainCamera.ScreenToWorldPoint(new Vector3 (0f, Screen.height,0f)).y,0).y) - (transform.localScale.y);
bottomWall = (new Vector3 (0f,mainCamera.ScreenToWorldPoint(new Vector3 (0f, 0f ,0f)).y).y) + (transform.localScale.y);
}
示例6: Start
// Use this for initialization
void Start()
{
target = GameMain.Instance.playerObject;
transform.position = target.transform.position;
camera = GetComponent<Camera>();
cameraSize = new Vector3 (0,0,0);
cameraSize.x = transform.position.x - camera.ScreenToWorldPoint(new Vector3(0,0,0)).x;
cameraSize.y = transform.position.y - camera.ScreenToWorldPoint(new Vector3(0,0,0)).y;
}
示例7: OnDrawGizmos
void OnDrawGizmos()
{
focusedCamera = camera;
topRight = focusedCamera.ScreenToWorldPoint(new Vector3(focusedCamera.pixelWidth, focusedCamera.pixelHeight, focusedCamera.nearClipPlane));
bottomRight = focusedCamera.ScreenToWorldPoint(new Vector3(focusedCamera.pixelWidth, 0, focusedCamera.nearClipPlane));
topLeft = focusedCamera.ScreenToWorldPoint(new Vector3(0, focusedCamera.pixelHeight, focusedCamera.nearClipPlane));
bottomLeft = focusedCamera.ScreenToWorldPoint(new Vector3(0, 0, focusedCamera.nearClipPlane)) ;
Vector3 offset = (focusedCamera.transform.forward * screenSurfaceDepth);
if(displayScreenInformation){
Gizmos.color = Color.blue;
if(displayTextInformation)
Gizmos.DrawIcon((topLeft + bottomRight) * 0.5F, "CloseClippingPlane.png");
Gizmos.DrawLine(topLeft, topRight);
Gizmos.DrawLine(topRight, bottomRight);
Gizmos.DrawLine(bottomRight, bottomLeft);
Gizmos.DrawLine(bottomLeft, topLeft);
Gizmos.color = Color.cyan;
if(displayTextInformation)
Gizmos.DrawIcon(((topLeft + offset) + (bottomRight + offset)) * 0.5F, "ScreenSpace.png");
Gizmos.DrawLine(topLeft + offset, topRight + offset);
Gizmos.DrawLine(topRight + offset, bottomRight + offset);
Gizmos.DrawLine(bottomRight + offset, bottomLeft + offset);
Gizmos.DrawLine(bottomLeft + offset, topLeft + offset);
}
if(displayGuideInformation){
Gizmos.color = Color.yellow;
drawFlatForwardLine(bottomLeft + offset, 1);
drawFlatForwardLine(bottomRight + offset, 1);
drawLineFromCameraPastPoint(topLeft, 1);
drawLineFromCameraPastPoint(topRight, 1);
drawLineFromCameraPastPoint(bottomLeft, 1);
drawLineFromCameraPastPoint(bottomRight, 1);
drawFlatForwardLine(slidePointDownCameraEdge(bottomLeft, floorDepth), 1);
drawFlatForwardLine(slidePointDownCameraEdge(bottomRight, floorDepth), 1);
}
if(displayFloorInformation){
Gizmos.color = FloorColor;
drawFloorExample(slidePointDownCameraEdge(bottomRight, floorDepth), slidePointDownCameraEdge(bottomLeft, floorDepth), 1);
}
}
示例8: Start
// Use this for initialization
void Start () {
Player = GameObject.FindWithTag("Player");
rb = GetComponent<Rigidbody>();
camera = Camera.main;
Debug.Log("Yo Camera Bound");
var bottomLeft = camera.ScreenToWorldPoint(Vector3.zero);
var topRight = camera.ScreenToWorldPoint(new Vector3(
camera.pixelWidth, camera.pixelHeight));
cameraRect = new Rect(
bottomLeft.x,
bottomLeft.y,
topRight.x - bottomLeft.x,
topRight.y - bottomLeft.y);
}
示例9: Update
void Update()
{
cam = Camera.main;
mousePosition = Input.mousePosition;
mousePosition = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z - cam.transform.position.z));
transform.eulerAngles = new Vector3(0,0,Mathf.Atan2((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x))*Mathf.Rad2Deg);
}
示例10: Start
void Start ()
{
mainCamera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
upperCorner = mainCamera.ScreenToWorldPoint(upperCorner);
//Debug.Log ("upper corner: " + upperCorner);
}
示例11: splitByLine
public GameObject[] splitByLine(GameObject target, Camera camera, Vector3 _start, Vector3 _end, bool destroyOriginal)
{
Vector3 targetPositionRelativeToCamera = camera.transform.worldToLocalMatrix.MultiplyPoint3x4( target.transform.position );
_start.z = targetPositionRelativeToCamera.z;
_end.z = targetPositionRelativeToCamera.z;
Vector3 _middle = (_start + _end) / 2f;
_middle.z *= 2f;
Vector3 start = camera.ScreenToWorldPoint(_start);
Vector3 middle = camera.ScreenToWorldPoint(_middle);
Vector3 end = camera.ScreenToWorldPoint(_end);
return splitByTriangle(target, new[] { start, middle, end }, destroyOriginal );
}
示例12: GeneratePlanes
private void GeneratePlanes()
{
cameraComponent = sceneCamera.GetComponent<Camera>();
screenHeightInWorldUnits = cameraComponent.orthographicSize * 2;
screenWidthInWorldUnits = screenHeightInWorldUnits * cameraComponent.aspect;
for (int i = 0; i < numberOfHorizontalRectangles; i++)
for (int j = 0; j < numberOfVerticalRectangles; j++)
{
Transform plane = GameObject.Instantiate(planePrefab);
plane.GetComponent<Renderer>().material.color = new Color(0, 0, 0);
//Set rotation
plane.rotation = Quaternion.Euler(-90, 0, 0);
//Set position
float horizontalSpacing = cameraComponent.pixelWidth / numberOfHorizontalRectangles;
float verticalSpacing = cameraComponent.pixelHeight / numberOfVerticalRectangles;
plane.position = cameraComponent.ScreenToWorldPoint(new Vector3(horizontalSpacing * 0.5f + horizontalSpacing * i,
verticalSpacing * 0.5f + verticalSpacing * j,
1));
//Set scale
plane.localScale = new Vector3(1 * cameraComponent.aspect / numberOfHorizontalRectangles - spacingBetweenRectangles / 2,
1,
1f / numberOfVerticalRectangles - spacingBetweenRectangles / 2);
planes.Add(plane);
}
}
示例13: Draw
public override int Draw(Microsoft.Xna.Framework.Graphics.GraphicsDevice device, Camera cam)
{
if (ApplicationSettings.ShowDebugLines)
{
Debug.DrawLines(device, cam);
}
if (ApplicationSettings.ShowDebugPhysics)
{
Matrix proj = cam.projectionMatrix;
Matrix view = cam.view;
if (rayCastPlane.HasValue)
{
if (rayCastPlane.Value.normal == Vector3.up)
{
view = Matrix.CreateRotationX(MathHelper.ToRadians(90)) * view;
}
}
physicsDebugView.RenderDebugData(ref proj, ref view, b => dontDrawColliders.Contains(b.UserData));
// NOTE: This only works for XY plane stuff
PressPlay.FFWD.Vector3 inPos = Input.mousePosition;
inPos.z = cam.nearClipPlane;
PressPlay.FFWD.Vector2 castPos = cam.ScreenToWorldPoint(inPos);
if (rayCastPlane.HasValue)
{
float dist;
Ray ray = Camera.main.ScreenPointToRay(inPos);
if (rayCastPlane.Value.Raycast(ray, out dist))
{
Vector3 pt = ray.GetPoint(dist);
castPos = new Vector2(pt.x, pt.z);
}
else
{
castPos = Vector2.zero;
}
}
Debug.Display("Mouse / Physics", inPos + " / " + castPos);
RaycastHit[] hits = Physics.PointcastAll(castPos, cam.cullingMask);
if (hits.Length > 0)
{
Debug.Display("Over", String.Join("\n", hits.Select(h => h.collider.ToString()).OrderBy(s => s).ToArray()));
}
else
{
Debug.Display("Over", "");
}
}
if(ApplicationSettings.ShowDebugPhysicsCustom)
{
Physics.DrawDebug();
}
return 0;
}
示例14: SetCameraBounds
public static void SetCameraBounds(Camera cam = null)
{
if (cam == null) cam = Camera.main;
Vector3 topLeft = new Vector3(0, 0, 0);
Vector3 bottomRight = new Vector3(Screen.width, Screen.height, 0);
Vector3 boundTLN = cam.ScreenToWorldPoint(topLeft);
Vector3 boundBRF = cam.ScreenToWorldPoint(bottomRight);
boundTLN.z += cam.nearClipPlane;
boundBRF.z += cam.farClipPlane;
Vector3 center = (boundTLN + boundBRF) / 2f;
_camBounds = new Bounds(center, Vector3.zero);
_camBounds.Encapsulate(boundBRF);
_camBounds.Encapsulate(boundTLN);
}
示例15: GetRelativeMousePosition
/// <summary>
/// get relative mouse position under parent node
/// the camera must be orthometric
/// </summary>
static public Vector3 GetRelativeMousePosition(Camera camera, Transform parent)
{
Vector3 v = camera.ScreenToWorldPoint(Input.mousePosition);//screen position to world position
if (parent!=null)
v = parent.InverseTransformPoint(v);//world position to local position
v.z = 0;//flat z
return v;
}