本文整理汇总了C#中Camera.ViewportToWorldPoint方法的典型用法代码示例。如果您正苦于以下问题:C# Camera.ViewportToWorldPoint方法的具体用法?C# Camera.ViewportToWorldPoint怎么用?C# Camera.ViewportToWorldPoint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Camera
的用法示例。
在下文中一共展示了Camera.ViewportToWorldPoint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculateViewFrustum
public static Vector3[] CalculateViewFrustum(Camera cam, ref Vector3 dimensions)
{
Vector3[] frustum = new Vector3[FRUSTUM_SIZE];
// Near clipping plane bounds
frustum[BOTTOM_LEFT_POINT] = cam.ViewportToWorldPoint(new Vector3(0f, 0f, cam.nearClipPlane));
frustum[TOP_LEFT_POINT] = cam.ViewportToWorldPoint(new Vector3(0f, 1f, cam.nearClipPlane));
frustum[BOTTOM_RIGHT_POINT] = cam.ViewportToWorldPoint(new Vector3(1f, 0f, cam.nearClipPlane));
frustum[TOP_RIGHT_POINT] = cam.ViewportToWorldPoint(new Vector3(1f, 1f, cam.nearClipPlane));
// Clipping planes: 0/left, 1/right, 2/bottom, 3/top, 4/near, 5/far
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
frustum[BOTTOM_LEFT_VEC] = Vector3.Cross(planes[0].normal, planes[2].normal);
frustum[TOP_LEFT_VEC] = Vector3.Cross(planes[3].normal, planes[0].normal);
frustum[TOP_RIGHT_VEC] = Vector3.Cross(planes[1].normal, planes[3].normal);
frustum[BOTTOM_RIGHT_VEC] = Vector3.Cross(planes[2].normal, planes[1].normal);
dimensions.x = (frustum[BOTTOM_LEFT_POINT] - frustum[BOTTOM_RIGHT_POINT]).magnitude;
dimensions.y = (frustum[TOP_LEFT_POINT] - frustum[BOTTOM_LEFT_POINT]).magnitude;
// Radius needed for sphere cast - distance from corner to center of viewport
dimensions.z = (frustum[BOTTOM_LEFT_POINT] - cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, cam.nearClipPlane))).magnitude;
return frustum;
}
示例2: PositionUpdate
void PositionUpdate(Camera targetCamera, float z)
{
leftTop = targetCamera.ViewportToWorldPoint(new Vector3(1, 0, z));
rightDown = targetCamera.ViewportToWorldPoint(new Vector3(0, 1, z));
rightTop = targetCamera.ViewportToWorldPoint(new Vector3(0, 0, z));
leftDown = targetCamera.ViewportToWorldPoint(new Vector3(1, 1, z));
}
示例3: Start
void Start ()
{
cam = GetComponent<Camera>();
float camHeight = cam.ViewportToWorldPoint(new Vector2(1, 1)).y - cam.ViewportToWorldPoint(new Vector2(0, 0)).y;
baseHeight = transform.position.y;
initialTarget = transform.position.y - camHeight / 2 + camHeight * followThreshold;
}
示例4: CreateBottomPlane
public static Plane CreateBottomPlane(Camera camera)
{
Vector3 inPoint = camera.ViewportToWorldPoint(new Vector3(0.0f, 0.0f, camera.nearClipPlane));
Vector3 vector3 = camera.ViewportToWorldPoint(new Vector3(1f, 0.0f, camera.nearClipPlane));
Vector3 inNormal = Vector3.Cross(camera.ViewportToWorldPoint(new Vector3(0.0f, 0.0f, camera.farClipPlane)) - inPoint, vector3 - inPoint);
inNormal.Normalize();
return new Plane(inNormal, inPoint);
}
示例5: Start
// Use this for initialization
void Start()
{
cam = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera>();
leftBound = new Vector3 (cam.ViewportToWorldPoint (new Vector3 (0, 0, 0)).x / 3,
transform.position.y, transform.position.z);
rightBound = new Vector3 (cam.ViewportToWorldPoint (new Vector3 (1, 0, 0)).x / 3,
transform.position.y, transform.position.z);
}
示例6: Start
void Start()
{
thisRigidbody = GetComponent<Rigidbody2D>();
if (thisRigidbody != null) {
foregroundCamera = GameObject.Find ("Foreground Camera").GetComponent<Camera>();
boundaryLeftEdge = foregroundCamera.ViewportToWorldPoint (new Vector3(0, 0, 0)).x + padding;
boundaryRightEdge = foregroundCamera.ViewportToWorldPoint (new Vector3(1, 1, 0)).x - padding;
}
}
示例7: createMesh
protected override Mesh createMesh(Camera camera)
{
Mesh mesh = new Mesh();
Vector3 tr = camera.ViewportToWorldPoint(new Vector3(1, 1, DEPTH));
Vector3 tl = camera.ViewportToWorldPoint(new Vector3(0, 1, DEPTH));
Vector3 br = camera.ViewportToWorldPoint(new Vector3(1, 0, DEPTH));
Vector3 bl = camera.ViewportToWorldPoint(new Vector3(0, 0, DEPTH));
mesh.vertices = new Vector3[] {tr, tl, br, bl};
mesh.triangles = new int[] {0, 2, 1, 1, 2, 3};
return mesh;
}
示例8: Start
void Start () {
// GET OBJECTS
GameObject cameraObj = GameObject.Find ("Camera");
cam = cameraObj.GetComponent<Camera> ();
p1healthbar = GameObject.Find ("Player1HB");
p2healthbar = GameObject.Find ("Player2HB");
base1 = GameObject.Find ("Base1");
base2 = GameObject.Find ("Base2");
bar1 = GameObject.Find ("Bar1");
bar2 = GameObject.Find ("Bar2");
top1 = GameObject.Find ("Top1");
top2 = GameObject.Find ("Top2");
bottom1 = GameObject.Find ("Bottom1");
bottom2 = GameObject.Find ("Bottom2");
// SET TRANSFORM ATTRIBUTES
baseXScale = 0.05f;
edgeYZScale = 0.05f;
distance = 0.5f;
xOffset = 0.05f;
yOffset = 0.05f;
barHeight = 0.10f;
barWidth = 0.4f;
// SET INITIAL TRANSFORM
base1.transform.localScale = new Vector3 (baseXScale, 1.0f, 1.0f);
bar1.transform.localScale = new Vector3 (1.0f - baseXScale, 1.0f - (2.0f * edgeYZScale), 1.0f - (2.0f * edgeYZScale));
top1.transform.localScale = new Vector3 (1.0f - baseXScale, edgeYZScale, 1.0f);
bottom1.transform.localScale = new Vector3 (1.0f - baseXScale, 1.0f, edgeYZScale);
base2.transform.localScale = new Vector3 (baseXScale, 1.0f, 1.0f);
bar2.transform.localScale = new Vector3 (1.0f - baseXScale, 1.0f - (2.0f * edgeYZScale), 1.0f - (2.0f * edgeYZScale));
top2.transform.localScale = new Vector3 (1.0f - baseXScale, edgeYZScale, 1.0f);
bottom2.transform.localScale = new Vector3 (1.0f - baseXScale, 1.0f, edgeYZScale);
base1.transform.localPosition = new Vector3 (baseXScale * 0.5f, 0.0f, 0.0f);
bar1.transform.localPosition = new Vector3 (((1.0f - baseXScale) * 0.5f) + baseXScale, 0.0f, 0.0f);
top1.transform.localPosition = new Vector3 (((1.0f - baseXScale) * 0.5f) + baseXScale, ((1.0f - edgeYZScale) * 0.5f), 0.0f);
bottom1.transform.localPosition = new Vector3 (((1.0f - baseXScale) * 0.5f) + baseXScale, 0.0f, ((1.0f - edgeYZScale) * 0.5f));
base2.transform.localPosition = new Vector3 (baseXScale * -0.5f, 0.0f, 0.0f);
bar2.transform.localPosition = new Vector3 (((1.0f - baseXScale) * -0.5f) - baseXScale, 0.0f, 0.0f);
top2.transform.localPosition = new Vector3 (((1.0f - baseXScale) * -0.5f) - baseXScale, ((1.0f - edgeYZScale) * 0.5f), 0.0f);
bottom2.transform.localPosition = new Vector3 (((1.0f - baseXScale) * -0.5f) - baseXScale, 0.0f, ((1.0f - edgeYZScale) * 0.5f));
Vector3 wpLeft = cam.ViewportToWorldPoint(new Vector3(xOffset, 1.0f - yOffset - (barHeight * 0.5f), distance));
Vector3 wpRight = cam.ViewportToWorldPoint(new Vector3(xOffset + barWidth, 1.0f - yOffset - (barHeight * 0.5f), distance));
float wpBarWidth = Vector3.Distance (wpLeft, wpRight);
Vector3 wpTop = cam.ViewportToWorldPoint(new Vector3(xOffset, 1.0f - yOffset, distance));
Vector3 wpBottom = cam.ViewportToWorldPoint(new Vector3(xOffset, 1.0f - yOffset - barHeight, distance));
float wpBarHeight = Vector3.Distance (wpTop, wpBottom);
p1healthbar.transform.localScale = new Vector3 (wpBarWidth, wpBarHeight, wpBarHeight);
p2healthbar.transform.localScale = new Vector3 (wpBarWidth, wpBarHeight, wpBarHeight);
}
示例9: GetRandomVectorOutsideCamera
// Could probably be better.
// Alternatively could have used a particle system but I think that would have overcomplicated things not being able to use GameObjects as particles.
// In the editor it gets the vector points of the editor camera rather than the game camera for some seriously strange reason I cannot fathom.
// Works absolutely as expected when built.
public static Vector2 GetRandomVectorOutsideCamera(AXIS_BIAS bias, Camera camera)
{
float chanceX = Random.Range(0, 100);
float chanceY = Random.Range(0, 100);
// We only randomise the vector between 25 and 75 to ensure they result in off-screen coords
// i.e.25 +- 1 = offscreen. 0 +- 1 = potentially on-screen.
Vector2 coords = new Vector2(Random.Range(25, 75f) / 100f, Random.Range(25, 75f) / 100f);
//Debug.Log("Old Coords: " + coords);
// A different way would be to detect which edge the random coord is closest to and move the difference.
// This'll do for for now.
if (chanceX < 50 && bias == AXIS_BIAS.HORIZONTAL)
coords.x -= 1;
else if (chanceY < 50 && bias == AXIS_BIAS.VERTICAL)
coords.y -= 1;
else if (chanceX < 100 && bias == AXIS_BIAS.HORIZONTAL)
coords.x += 1;
else if (chanceY < 100 && bias == AXIS_BIAS.VERTICAL)
coords.y += 1;
// Clamp the coordinates
coords.x = Mathf.Clamp(coords.x, -0.1f, 1.1f);
coords.y = Mathf.Clamp(coords.y, -0.1f, 1.1f);
//Debug.Log("New Coords: " + coords);
return camera.ViewportToWorldPoint(coords);
}
示例10: GetCameraViewportWorldPoint
/// <summary>
/// 获取摄像机 视口每个点的坐标
/// </summary>
/// <param name="camera"></param>
/// <param name="distance"></param>摄像机到视口的距离
/// <returns></returns>
public static Vector3[] GetCameraViewportWorldPoint(Camera camera, float distance)
{
Vector3[] corners = new Vector3[4];
// 左上
corners[0] = camera.ViewportToWorldPoint(new Vector3(0, 1, distance));
// 右上
corners[1] = camera.ViewportToWorldPoint(new Vector3(1, 1, distance));
// 左下
corners[2] = camera.ViewportToWorldPoint(new Vector3(0, 0, distance));
// 右下
corners[3] = camera.ViewportToWorldPoint(new Vector3(1, 0, distance));
return corners;
}
示例11: Start
void Start()
{
cam = GameObject.FindGameObjectWithTag ("Static camera").GetComponent<Camera>();
Camera.main.GetComponent<CameraFollower> ().target = transform;
GameObject g = Instantiate (charFace) as GameObject;
g.transform.parent = cam.transform;
g.transform.position = cam.ViewportToWorldPoint (new Vector3(0f,1,5));
GameObject.Find ("Barra de Vida").GetComponent<LifeBarPositioner> ().Initialize ();
}
示例12: Awake
void Awake()
{
cam = GetComponent<Camera>();
player = target.GetComponent<PlayerController>();
//set initial position
float origZ = cam.transform.position.z;
Vector3 initPos = cam.transform.position + target.position - cam.ViewportToWorldPoint(anchor);
initPos.z = origZ;
cam.transform.position = initPos;
}
示例13: FrustumBoundingSphereBinarySearch
public static SunshineMath.BoundingSphere FrustumBoundingSphereBinarySearch(Camera camera, float nearClip, float farClip, bool radial, float radialPadding, float maxError = 0.01f, int maxSteps = 100)
{
float num = SunshineMath.RadialClipCornerRatio(camera);
float z = (!radial) ? nearClip : (nearClip * num);
float z2 = (!radial) ? farClip : (farClip * num);
Vector3 from = camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, nearClip));
Vector3 to = camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, z2));
Vector3 vector = camera.ViewportToWorldPoint(new Vector3(0f, 0f, z));
Vector3 vector2 = camera.ViewportToWorldPoint(new Vector3(1f, 1f, farClip));
Vector3 vector3 = (!radial) ? vector2 : camera.ViewportToWorldPoint(new Vector3(1f, 1f, z2));
SunshineMath._frustumTestPoints[0] = vector;
SunshineMath._frustumTestPoints[1] = vector3;
float num2 = 3.40282347E+38f;
Vector3 origin = Vector3.zero;
float num3 = 0f;
float num4 = 0.2f;
for (int i = 0; i < maxSteps; i++)
{
Vector3 vector4 = Vector3.Lerp(from, to, num3);
float num5 = SunshineMath.MinRadiusSq(vector4, SunshineMath._frustumTestPoints);
if (num5 < num2)
{
num2 = num5;
origin = vector4;
}
else
{
num4 *= -0.5f;
if (Mathf.Abs(num4) < maxError)
{
break;
}
}
num3 += num4;
}
return new SunshineMath.BoundingSphere
{
origin = origin,
radius = Mathf.Sqrt(num2) + radialPadding
};
}
示例14: Start
// Use this for initialization
private void Start()
{
myCam = GameObject.Find("Main Camera").camera;
screen = myCam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
zeroPosWorldPoint = myCam.ViewportToWorldPoint(new Vector3(0, 0, 0));
rightSidePosWorldPoint = myCam.ViewportToWorldPoint(new Vector3(1, 0, 0));
leftSidePosWorldPoint = myCam.ViewportToWorldPoint(new Vector3(0, 1, 0));
if (myCam == null)
Debug.Log("Error. Needs to assigne main camera for screen wrapping!");
if (OnlyScreenWrapNoClone)
return;
if (Clone == null)
Debug.Log("Error. Needs to assigne a clone for screen wrapping!");
if (Use2DCollider)
cloneBoxCollider2D = Clone.GetComponent<BoxCollider2D>();
else
cloneBoxCollider = Clone.GetComponent<BoxCollider>();
}
示例15: Start
// Use this for initialization
void Start () {
sizeDown = false;
cam = Camera.main;
Pos = this.gameObject.transform.position;
Pos.y += 4f;
this.gameObject.transform.position = Pos;
Pos = cam.transform.position;
Pos = cam.ViewportToWorldPoint (new Vector3 (0, 1, 0));
Pos.x += 20;
Pos.y += 40;
//Pos.z += 0;
//Debug.Log (Pos);
}