IEnumerator SpawnFlyingCubes(FlyingCube prefab, Vector3 cubePos) { ClipPlanePoints clipPoints = ClipPlanePoints.Instance; Camera camera = clipPoints.Camera; if (!clipPoints.CollidesTop(cubePos) && !clipPoints.CollidesBottom(cubePos)) { float cubeSpeed = Random.Range( m_minCubeSpeed, m_maxCubeSpeed); float firstSpawnTime = 0f; //Allows closer cubes to spawn more. If the cube is closer, maxRandMulti will be less. float spawnMulti = 0.05f * Mathf.Max(Vector3.Distance(clipPoints.NearUpperLeft, cubePos) / Vector3.Distance(clipPoints.NearUpperLeft, clipPoints.FarUpperLeft), m_minSpawnTime); float maxSpawnTime = Mathf.Max(spawnMulti * m_maxSpawnTime, m_minSpawnTime); do { float spawnTime = Random.Range(firstSpawnTime, maxSpawnTime); yield return(new WaitForSeconds(spawnTime)); firstSpawnTime = m_minSpawnTime; prefab.Clone(transform, cubePos, cubeSpeed * camera.transform.right); } while (true); } }
IEnumerator SpawnStreamingCube() { ClipPlanePoints clipPoints = ClipPlanePoints.Instance; Camera camera = clipPoints.Camera; Vector3 cubeVel = camera.transform.up * m_streamCubeSpeed; do { for (int i = 0, count = m_spawnIndices.Length; i < count; ++i) { if (m_spawnIndices[i]) { SpawnStreamingCube(i, cubeVel); } else if (m_lastSpawnIndices[i]) { StreamingCube.CleanupAll(i); } m_lastSpawnIndices[i] = m_spawnIndices[i]; } yield return(new WaitForSeconds(m_streamCubeSpawnTime)); } while (AppManager.IsInAmbientMode); CLeanupAllStreamingCubes(); ResetAllIndices(); m_streamCubes = null; }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 Pos) { ClipPlanePoints CPP = new ClipPlanePoints(); if (Camera.main == null) { return(CPP); } Transform T = Camera.main.transform; float HalfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; float aspect = Camera.main.aspect; float distance = Camera.main.nearClipPlane; float height = distance * Mathf.Tan(HalfFOV); float width = height * aspect; CPP.BottomRight = Pos + T.right * width; CPP.BottomRight -= T.up * height; CPP.BottomRight += T.forward * distance; CPP.BottomLeft = Pos - T.right * width; CPP.BottomLeft -= T.up * height; CPP.BottomLeft += T.forward * distance; CPP.UpperRight = Pos + T.right * width; CPP.UpperRight += T.up * height; CPP.UpperRight += T.forward * distance; CPP.UpperLeft = Pos - T.right * width; CPP.UpperLeft += T.up * height; CPP.UpperLeft += T.forward * distance; return(CPP); }
void MakeSpawnPoints() { ClipPlanePoints points = ClipPlanePoints.Instance; points.UpdateCameraClipPlanePoints(); Camera camera = points.Camera; Vector3 top = points.FarUpperLeft; Vector3 bottom = points.FarLowerLeft; Vector3 virtDir = -camera.transform.up; Vector3 horizDir = -camera.transform.forward; float horizDist = camera.farClipPlane; float vertDist = Vector3.Distance(top, bottom); //+1, don't want point at near plane... float horizOffset = horizDist / (m_numCubesFacingCamera + 1); float vertOffset = vertDist / (m_numCubesVertically - 1); //spawn points... int lastFacingIndex = m_numCubesFacingCamera - m_numRemovedFromFacing; for (int i = 0; i < m_numCubesVertically; ++i) { Vector3 spawnPoint = top + (virtDir * vertOffset * i) + (horizDir * horizOffset * lastFacingIndex); m_attractSpawnPoints.Add(spawnPoint); for (int j = 1, count = m_numCubesFacingCamera - m_numRemovedFromFacing; j < count; ++j) { spawnPoint = top + (virtDir * vertOffset * i) + (horizDir * horizOffset * j); m_flyingSpawnPoints.Add(spawnPoint); } } }
private ClipPlanePoints ClipPlaneAtNear(Vector3 position) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); Transform cameraTransform = myTransform; float distance = Camera.mainCamera.nearClipPlane; Bounds bounds = originalTarget.parent.GetComponent <Collider>().bounds; float height = bounds.size.x; float width = bounds.size.x / 2; clipPlanePoints.LowerRight = position + cameraTransform.right * width; clipPlanePoints.LowerRight += cameraTransform.forward * distance; clipPlanePoints.LowerLeft = position - cameraTransform.right * width; clipPlanePoints.LowerLeft += cameraTransform.forward * distance; clipPlanePoints.UpperRight = position + cameraTransform.right * width; clipPlanePoints.UpperRight += cameraTransform.up * height; clipPlanePoints.UpperRight += cameraTransform.forward * distance; clipPlanePoints.UpperLeft = position - cameraTransform.right * width; clipPlanePoints.UpperLeft += cameraTransform.up * height; clipPlanePoints.UpperLeft += cameraTransform.forward * distance; return(clipPlanePoints); }
HashSet <Renderer> checkCameraPoints() { HashSet <Renderer> returnedHits = new HashSet <Renderer>(); Vector3 targetPos = Target.position; ClipPlanePoints clipPlanePoints = ClipPlaneAtNear(targetPos); if (DEBUG) { Debug.DrawLine(myTransform.position, Target.position, Color.red); Debug.DrawLine(myTransform.position, clipPlanePoints.LowerLeft); Debug.DrawLine(myTransform.position, clipPlanePoints.LowerRight); Debug.DrawLine(myTransform.position, clipPlanePoints.UpperLeft); Debug.DrawLine(myTransform.position, clipPlanePoints.UpperRight); Debug.DrawLine(clipPlanePoints.UpperLeft, clipPlanePoints.UpperRight); Debug.DrawLine(clipPlanePoints.LowerLeft, clipPlanePoints.LowerRight); Debug.DrawLine(clipPlanePoints.UpperLeft, clipPlanePoints.LowerLeft); Debug.DrawLine(clipPlanePoints.UpperRight, clipPlanePoints.LowerRight); } LayerMask mask = 1 << LayerMask.NameToLayer("NPC"); mask |= 1 << LayerMask.NameToLayer("Ignore Raycast"); mask = ~mask; getHits(Target.position, mask, ref returnedHits); getHits(clipPlanePoints.UpperLeft, mask, ref returnedHits); getHits(clipPlanePoints.UpperRight, mask, ref returnedHits); getHits(clipPlanePoints.LowerLeft, mask, ref returnedHits); getHits(clipPlanePoints.LowerRight, mask, ref returnedHits); return(returnedHits); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { ClipPlanePoints cpp = new ClipPlanePoints(); if (Camera.main == null) { return(cpp); } Transform cameraTransform = Camera.main.transform; float halfFOV = (Camera.main.fieldOfView / 2f) * Mathf.Deg2Rad; float aspect = Camera.main.aspect; float clipPlaneDist = Camera.main.nearClipPlane; float height = clipPlaneDist * Mathf.Tan(halfFOV); float width = height * aspect; cpp.lowerRight = pos + cameraTransform.right * width; cpp.lowerRight -= cameraTransform.up * height; cpp.lowerRight += cameraTransform.forward * clipPlaneDist; cpp.lowerLeft = pos - cameraTransform.right * width; cpp.lowerLeft -= cameraTransform.up * height; cpp.lowerLeft += cameraTransform.forward * clipPlaneDist; cpp.upperRight = pos + cameraTransform.right * width; cpp.upperRight += cameraTransform.up * height; cpp.upperRight += cameraTransform.forward * clipPlaneDist; cpp.upperLeft = pos - cameraTransform.right * width; cpp.upperLeft += cameraTransform.up * height; cpp.upperLeft += cameraTransform.forward * clipPlaneDist; return(cpp); }
void DrawSpawnPointGrid() { for (int i = 1, count = m_flyingSpawnPoints.Count; i < count; ++i) { Vector3 startPoint = m_flyingSpawnPoints[i - 1]; Vector3 endPoint = m_flyingSpawnPoints[i]; Debug.DrawLine(startPoint, endPoint, Color.green); } ClipPlanePoints clipPoints = ClipPlanePoints.Instance; Camera camera = clipPoints.Camera; if (clipPoints != null) { Vector3 leftDir = (clipPoints.NearLowerLeft - clipPoints.FarLowerLeft).normalized; Vector3 rightDir = (clipPoints.NearLowerRight - clipPoints.FarLowerRight).normalized; Vector3 startPoint = clipPoints.NearLowerLeft - (leftDir * m_streamCubeDist); Vector3 endPoint = clipPoints.NearLowerRight - (rightDir * m_streamCubeDist); Debug.DrawLine(startPoint, endPoint, Color.blue); } }
//method to find the 4 points of the nearClipPlane and returns them public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); if (Camera.main == null) { return(clipPlanePoints); } var transform = Camera.main.transform; var halfFoV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = Camera.main.aspect; var distance = Camera.main.nearClipPlane; //dist from cam to NCP var height = distance * Mathf.Tan(halfFoV); var width = height * aspect; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; //moving down by height, - to invert the up clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; //moving down by height, - to invert the up clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; //moving down by height, - to invert the up clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; //moving down by height, - to invert the up clipPlanePoints.UpperLeft += transform.forward * distance; return(clipPlanePoints); }
public static ClipPlanePoints NearClipPlanePoints(this Camera camera, Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); var transform = camera.transform; var halfFOV = (camera.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = camera.aspect; var distance = camera.nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; height *= 2.5f; width *= 2.5f; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; return clipPlanePoints; }
public ClipPlanePoints cameraClipPlanePoints(float distance) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); Transform transform = Camera.main.transform; Vector3 pos = transform.position; float halfFOV = (Camera.main.fieldOfView * 0.5f) * Mathf.Deg2Rad; float aspect = Camera.main.aspect; float height = Mathf.Tan(halfFOV) * distance; float width = height * aspect; //Lower Right clipPlanePoints.LowerRight = pos + transform.forward * distance; clipPlanePoints.LowerRight += transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; //Lower Left clipPlanePoints.LowerLeft = pos + transform.forward * distance; clipPlanePoints.LowerLeft -= transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; //Upper Right clipPlanePoints.UpperRight = pos + transform.forward * distance; clipPlanePoints.UpperRight += transform.right * width; clipPlanePoints.UpperRight += transform.up * height; //Upper Left clipPlanePoints.UpperLeft = pos + transform.forward * distance; clipPlanePoints.UpperLeft -= transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; return(clipPlanePoints); }
public ClipPlanePoints ClipPlaneAtNear(Vector3 pos) // pos - position of our camera/target { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); float halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; float aspect = Camera.main.aspect; float distance = Camera.main.nearClipPlane; float height = distance * Mathf.Tan(halfFOV); float width = height * aspect; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; return(clipPlanePoints); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); //Holds the calculated clipplane point //Make sure there's a camera if(Camera.main == null) return clipPlanePoints; var transform = Camera.main.transform; //Holds the camera transform var halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; //Get the field of view as radians var aspect = Camera.main.aspect; //Camera aspect ratio var distance = Camera.main.nearClipPlane; //Distance from camera to near clip plane var height = distance * Mathf.Tan (halfFOV); //Height of clip plane var width = height * aspect; //Width of clip plane //Set the clip plane point positions clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; //Move the clip plane forward to distance clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; //Move the clip plane forward to distance clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; //Move the clip plane forward to distance clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; //Move the clip plane forward to distance return clipPlanePoints; }
public static ClipPlanePoints getClipPlanePoints(Camera mainCamera, float distance) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); Transform cameraTransform = mainCamera.transform; Vector3 pos = cameraTransform.position; float halfFOV = (mainCamera.fieldOfView * 0.5f) * Mathf.Deg2Rad; float height = Mathf.Tan(halfFOV) * distance; float width = height * mainCamera.aspect; Vector3 pcfd = pos + cameraTransform.forward * distance; Vector3 crw = cameraTransform.right * width; Vector3 cth = cameraTransform.up * height; // lower right clipPlanePoints.LowerRight = pcfd; clipPlanePoints.LowerRight += crw; clipPlanePoints.LowerRight -= cth; // lower left clipPlanePoints.LowerLeft = pcfd; clipPlanePoints.LowerLeft -= crw; clipPlanePoints.LowerLeft -= cth; // upper right clipPlanePoints.UpperRight = pcfd; clipPlanePoints.UpperRight += crw; clipPlanePoints.UpperRight += cth; // upper left clipPlanePoints.UpperLeft = pcfd; clipPlanePoints.UpperLeft -= crw; clipPlanePoints.UpperLeft += cth; return(clipPlanePoints); }
protected override void Update() { if (m_attractToCamera != null) { if (AppManager.State != AppManager.AppState.ATTRACT_CUBES) { ClipPlanePoints clipPoints = ClipPlanePoints.Instance; if (clipPoints.IsOutsideBounds(transform.position)) { Recycle(); } else { StopCoroutine(m_attractToCamera); m_attractToCamera = null; m_animator.SetTrigger("cleanup"); m_animator.SetBool("isEnabled", false); } } } base.Update(); }
IEnumerator AttractToCamera() { if (m_animator == null) { m_animator = GetComponentInChildren <Animator>(); } ClipPlanePoints clipPoints = ClipPlanePoints.Instance; Camera camera = clipPoints.Camera; while (clipPoints.CollidesLeft(transform.position, 3.7f)) { yield return(new WaitForSeconds(.1f)); } m_velMultiplier = 1f; //TODO: Don't hard-code this... while (clipPoints.CollidesLeft(transform.position, -.8f)) { yield return(new WaitForSeconds(.1f)); } m_animator.SetFloat("speed", Velocity.magnitude); m_animator.SetBool("isEnabled", true); //TODO: Don't hard-code this... while (!clipPoints.CollidesRight(transform.position, -4.4f)) { yield return(new WaitForSeconds(.1f)); } m_animator.SetBool("isEnabled", false); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var _clipPlanePoint = new ClipPlanePoints(); if (Camera.main == null) { return(_clipPlanePoint); } var transform = Camera.main.transform; var halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = Camera.main.aspect; var distance = Camera.main.nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; // Move our point from pos to the right by the width _clipPlanePoint.LowerRight = pos + transform.right * width; _clipPlanePoint.LowerRight -= transform.up * height; _clipPlanePoint.LowerRight += transform.forward; _clipPlanePoint.LowerLeft = pos - transform.right * width; _clipPlanePoint.LowerLeft -= transform.up * height; _clipPlanePoint.LowerLeft += transform.forward; _clipPlanePoint.UpperRight = pos + transform.right * width; _clipPlanePoint.UpperRight += transform.up * height; _clipPlanePoint.UpperRight += transform.forward; _clipPlanePoint.UpperLeft = pos - transform.right * width; _clipPlanePoint.UpperLeft += transform.up * height; _clipPlanePoint.UpperLeft += transform.forward; return(_clipPlanePoint); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 Pos) { ClipPlanePoints CPP = new ClipPlanePoints (); if (Camera.main == null) { return CPP; } Transform T = Camera.main.transform; float HalfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; float aspect = Camera.main.aspect; float distance = Camera.main.nearClipPlane; float height = distance * Mathf.Tan (HalfFOV); float width = height * aspect; CPP.BottomRight = Pos + T.right * width; CPP.BottomRight -= T.up * height; CPP.BottomRight += T.forward * distance; CPP.BottomLeft = Pos - T.right * width; CPP.BottomLeft -= T.up * height; CPP.BottomLeft += T.forward * distance; CPP.UpperRight = Pos + T.right * width; CPP.UpperRight += T.up * height; CPP.UpperRight += T.forward * distance; CPP.UpperLeft = Pos - T.right * width; CPP.UpperLeft += T.up * height; CPP.UpperLeft += T.forward * distance; return CPP; }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); if (Camera.main == null) { return(clipPlanePoints); } var transform = Camera.main.transform; var halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = Camera.main.aspect; var distance = Camera.main.nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var witdh = height * aspect; clipPlanePoints.LowerRight = pos + transform.right * witdh; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * witdh; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * witdh; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * witdh; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; return(clipPlanePoints); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); var transform = TP_Camera.Instance.GetComponentInChildren <Camera>().transform; var halfFOV = (TP_Camera.Instance.GetComponentInChildren <Camera>().fieldOfView / 2) * Mathf.Deg2Rad; var aspect = TP_Camera.Instance.GetComponentInChildren <Camera>().aspect; var distance = TP_Camera.Instance.GetComponentInChildren <Camera>().nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; return(clipPlanePoints); }
public static ClipPlanePoints CLIPPLANEPOINTS(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); var transform = Camera.mainCamera.transform; var halfFOV = (Camera.mainCamera.fieldOfView / 2) * Mathf.Rad2Deg; var aspect = Camera.mainCamera.aspect; var distance = Mathf.Abs(Camera.mainCamera.transform.position.z) + 22.5f *(1 - M_MainCamera.INSTANCE.ZoomRate); var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; return clipPlanePoints; }
private void CollisionDetection() { RaycastHit result; Vector3 pos = (anchorRot * Vector3.forward) * followDistance; ClipPlanePoints plane = GetComponent <Camera>().NearClipPlanePoints(trueCameraPos + pos, margin); if (Physics.Raycast(trueCameraPos, plane.LowerLeft - trueCameraPos, out result, followDistance + 0.2f, layer)) { followDistance = result.distance; } if (Physics.Raycast(trueCameraPos, plane.LowerRight - trueCameraPos, out result, followDistance + 0.2f, layer)) { if (followDistance > result.distance) { followDistance = result.distance; } } if (Physics.Raycast(trueCameraPos, plane.UpperLeft - trueCameraPos, out result, followDistance + 0.2f, layer)) { if (followDistance > result.distance) { followDistance = result.distance; } } if (Physics.Raycast(trueCameraPos, plane.UpperRight - trueCameraPos, out result, followDistance + 0.2f, layer)) { if (followDistance > result.distance) { followDistance = result.distance; } } }
//Brings cube back to default animation state and //removes cube if outside camera bounds... void OnCLeanup(int sectionIndex) { if (!gameObject.activeSelf || m_sectionIndex != sectionIndex) { return; } m_sectionIndex = -1; StopCoroutine(StreamCubes()); if (m_animator == null) { m_animator = GetComponentInChildren <Animator>(); } m_animator.SetBool("isEnabled", false); m_animator.SetTrigger("cleanup"); ClipPlanePoints clipPoints = ClipPlanePoints.Instance; if (clipPoints.IsOutsideBounds(transform.position)) { Recycle(); } }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); if (Camera.main == null) { return(clipPlanePoints); } Transform transform = Camera.main.transform; float halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad + 10; float aspect = Camera.main.aspect; float distance = Camera.main.nearClipPlane; float height = distance * Mathf.Tan(halfFOV); float width = height * aspect; clipPlanePoints.bottomRight = pos + transform.right * width; clipPlanePoints.bottomRight -= transform.up * height; clipPlanePoints.bottomRight += transform.forward * distance; clipPlanePoints.bottomLeft = pos - transform.right * width; clipPlanePoints.bottomLeft -= transform.up * height; clipPlanePoints.bottomLeft += transform.forward * distance; clipPlanePoints.topRight = pos + transform.right * width; clipPlanePoints.topRight += transform.up * height; clipPlanePoints.topRight += transform.forward * distance; clipPlanePoints.topLeft = pos - transform.right * width; clipPlanePoints.topLeft += transform.up * height; clipPlanePoints.topLeft += transform.forward * distance; return(clipPlanePoints); }
IEnumerator StreamCubes() { if (m_animator == null) { m_animator = GetComponentInChildren <Animator>(); } ClipPlanePoints clipPoints = ClipPlanePoints.Instance; Camera camera = clipPoints.Camera; //TODO: Don't hard-code this... while (clipPoints.CollidesBottom(transform.position, -.1f)) { yield return(new WaitForSeconds(.1f)); } m_animator.SetBool("isEnabled", true); //TODO: Don't hard-code this... while (!clipPoints.CollidesTop(transform.position, -3.3f)) { yield return(new WaitForSeconds(.1f)); } m_animator.SetBool("isEnabled", false); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); float halfFOV = (mainCam.fieldOfView * 0.5f) * Mathf.Deg2Rad; float aspect = mainCam.aspect; float nearClipPlaneDist = mainCam.nearClipPlane; float height = nearClipPlaneDist * Mathf.Tan(halfFOV); float width = height * aspect; //Bottom Right clipPlanePoints.bottomRight = pos + camTransform.right * width; clipPlanePoints.bottomRight -= camTransform.up * height; clipPlanePoints.bottomRight += camTransform.forward * nearClipPlaneDist; //Bottom Left clipPlanePoints.bottomLeft = pos - camTransform.right * width; clipPlanePoints.bottomLeft -= camTransform.up * height; clipPlanePoints.bottomLeft += camTransform.forward * nearClipPlaneDist; //Top Right clipPlanePoints.topRight = pos + camTransform.right * width; clipPlanePoints.topRight += camTransform.up * height; clipPlanePoints.topRight += camTransform.forward * nearClipPlaneDist; //Top Left clipPlanePoints.topLeft = pos - camTransform.right * width; clipPlanePoints.topLeft += camTransform.up * height; clipPlanePoints.topLeft += camTransform.forward * nearClipPlaneDist; return(clipPlanePoints); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos)//pos is the proposed location of camera { var clipPlanePoints = new ClipPlanePoints(); if (Camera.main == null) return clipPlanePoints; var transform = Camera.main.transform; var halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad;//convert to radians, b/c tangent needs rads var aspect = Camera.main.aspect; var distance = Camera.main.nearClipPlane; var height = distance * Mathf.Tan(halfFOV);//all the work for this height var var width = height * aspect; //^all the info we need for the 4 points clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; // moving down by height clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; // moving down by height clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; // moving down by height clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; // moving down by height clipPlanePoints.UpperLeft += transform.forward * distance; return clipPlanePoints;//gonna have to return this, 2nd thing i typed }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); if (Camera.mainCamera == null) { return(clipPlanePoints); } Transform transform = Camera.mainCamera.transform; float halfFOV = (Camera.mainCamera.fieldOfView / 2) * Mathf.Deg2Rad; float aspect = Camera.mainCamera.aspect; float distance = Camera.mainCamera.nearClipPlane; float height = distance * Mathf.Tan(halfFOV); float width = height * aspect; clipPlanePoints.LowerRight = pos + (transform.right * width); clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - (transform.right * width); clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + (transform.right * width); clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - (transform.right * width); clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; return(clipPlanePoints); }
public static ClipPlanePoints CLIPPLANEPOINTS(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); var transform = Camera.mainCamera.transform; var halfFOV = (Camera.mainCamera.fieldOfView / 2) * Mathf.Rad2Deg; var aspect = Camera.mainCamera.aspect; var distance = Mathf.Abs(Camera.mainCamera.transform.position.z) + 22.5f * (1 - M_MainCamera.INSTANCE.ZoomRate); var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; return(clipPlanePoints); }
//Calcula el rectangulo de vision basado en la posicion de la camara y del vector que le pasamos public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); //Si la camera no existe, no creamos el cuadrado if (Camera.main == null) return clipPlanePoints; var transform = Camera.main.transform; //Necesitamos la mitad del fov en radianes var halfFOV = (Camera.main.fieldOfView/2) * Mathf.Deg2Rad; var aspect = Camera.main.aspect; var distance = Camera.main.nearClipPlane; //Como se trata de un triangulo rectangulo, con la tangente de la mitad del FOV, hallamos la altura var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; clipPlanePoints.lowerRight = pos + transform.right * width; //lo movemos a la der clipPlanePoints.lowerRight -= transform.up * height; //lo movemos hacia abajo clipPlanePoints.lowerRight += transform.forward * distance; //Lo movemos hacia delante la distancia + 1 unidad, para que este un poco por delante de la camera clipPlanePoints.lowerLeft = pos - transform.right * width; //lo movemos a la izq clipPlanePoints.lowerLeft -= transform.up * height; //lo movemos hacia abajo clipPlanePoints.lowerLeft += transform.forward * distance; //Lo movemos hacia delante la distancia + 1 unidad, para que este un poco por delante de la camera clipPlanePoints.upperRight = pos + transform.right * width; //lo movemos a la der clipPlanePoints.upperRight += transform.up * height; //lo movemos hacia arriba clipPlanePoints.upperRight += transform.forward * distance; //Lo movemos hacia delante la distancia + 1 unidad, para que este un poco por delante de la camera clipPlanePoints.upperLeft = pos - transform.right * width; //lo movemos a la izq clipPlanePoints.upperLeft += transform.up * height; //lo movemos hacia arriba clipPlanePoints.upperLeft += transform.forward * distance; //Lo movemos hacia delante la distancia + 1 unidad, para que este un poco por delante de la camera return clipPlanePoints; }
public static ClipPlanePoints NearClipPlanePoints(this Camera camera, Vector3 pos, float clipPlaneMargin) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); Transform transform = camera.transform; float halfFOV = (camera.fieldOfView / 2) * Mathf.Deg2Rad; float aspect = camera.aspect; float distance = camera.nearClipPlane; float height = distance * Mathf.Tan(halfFOV); float width = height * aspect; height *= 1 + clipPlaneMargin; width *= 1 + clipPlaneMargin; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; return(clipPlanePoints); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); if (Camera.mainCamera == null) return clipPlanePoints; var transform = Camera.mainCamera.transform; var halfFOV = (Camera.mainCamera.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = Camera.mainCamera.aspect; var distance = Camera.mainCamera.nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; return clipPlanePoints; }
private ClipPlanePoints NearClipPlanePoints(Vector3 pos, float clipPlaneMargin) { var clipPlanePoints = new ClipPlanePoints(); var transform = camera.transform; var halfFOV = (camera.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = camera.aspect; var distance = camera.nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; height *= 1 + clipPlaneMargin; width *= 1 + clipPlaneMargin; clipPlanePoints.LowerRight = pos + transform.right * width; clipPlanePoints.LowerRight -= transform.up * height; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = pos - transform.right * width; clipPlanePoints.LowerLeft -= transform.up * height; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = pos + transform.right * width; clipPlanePoints.UpperRight += transform.up * height; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = pos - transform.right * width; clipPlanePoints.UpperLeft += transform.up * height; clipPlanePoints.UpperLeft += transform.forward * distance; return(clipPlanePoints); }
private void UpdateMovement() { if (!lastWasBlocked) { distance = Mathf.Lerp(distance, defaultDistance, smoothFollow * Time.deltaTime); } currentZoom = defaultDistance; cullingDistance = Mathf.Lerp(cullingDistance, currentZoom, stateSmoothing * Time.deltaTime); var camDir = (-1 * lookAt.forward); camDir = camDir.normalized; var targetPos = new Vector3(target.position.x, target.position.y, target.position.z) + target.transform.up; desired_cPos = targetPos + target.transform.up * height; current_cPos = targetPos + target.transform.up * currentHeight; ClipPlanePoints planePoints = NearClipPlanePoints(current_cPos + (camDir * (distance)), clipPlaneMargin); ClipPlanePoints oldPoints = NearClipPlanePoints(desired_cPos + (camDir * currentZoom), clipPlaneMargin); //Check if Height is not blocked if (Physics.SphereCast(targetPos, checkHeightRadius, target.transform.up, out RaycastHit hitInfo, cullingHeight + 0.2f, cullingLayer)) { var t = hitInfo.distance - 0.2f; t -= height; t /= (cullingHeight - height); cullingHeight = Mathf.Lerp(height, cullingHeight, Mathf.Clamp(t, 0.0f, 1.0f)); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints (); // Ensure there is a main camera for us to check. if (!Camera.main) return clipPlanePoints; // Properties to determine the main camera's near clip plane. Transform mainCameraTransform = Camera.main.transform; float halfFieldOfView = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; float aspect = Camera.main.aspect; float distance = Camera.main.nearClipPlane; float height = distance * Mathf.Tan (halfFieldOfView); float width = height * aspect; clipPlanePoints.lowerRight = pos + mainCameraTransform.right * width; clipPlanePoints.lowerRight -= mainCameraTransform.up * height; clipPlanePoints.lowerRight += mainCameraTransform.forward * distance; clipPlanePoints.lowerLeft = pos - mainCameraTransform.right * width; clipPlanePoints.lowerLeft -= mainCameraTransform.up * height; clipPlanePoints.lowerLeft += mainCameraTransform.forward * distance; clipPlanePoints.upperRight = pos + mainCameraTransform.right * width; clipPlanePoints.upperRight += mainCameraTransform.up * height; clipPlanePoints.upperRight += mainCameraTransform.forward * distance; clipPlanePoints.upperLeft = pos - mainCameraTransform.right * width; clipPlanePoints.upperLeft += mainCameraTransform.up * height; clipPlanePoints.upperLeft += mainCameraTransform.forward * distance; return clipPlanePoints; }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var clipPlanePoints = new ClipPlanePoints(); if (Camera.main == null) { return(clipPlanePoints); } var transform = Camera.main.transform; var halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = Camera.main.aspect; var distance = Camera.main.nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; //LOWER RIGHT //Move our point to right from position by width clipPlanePoints.LowerRight = pos + transform.right * width; //Move our point down i.e. down by height clipPlanePoints.LowerRight -= transform.up * height; //Ensures that we are moving forward relative to camera clipPlanePoints.LowerRight += transform.forward * distance; //LOWER LEFT //Move our point to left from position by width clipPlanePoints.LowerLeft = pos - transform.right * width; //Move our point down i.e. down by height clipPlanePoints.LowerLeft -= transform.up * height; //Ensures that we are moving forward relative to camera clipPlanePoints.LowerLeft += transform.forward * distance; //UPPER RIGHT //Move our point to right from position by width clipPlanePoints.UpperRight = pos + transform.right * width; //Move our point up i.e. up by height clipPlanePoints.UpperRight += transform.up * height; //Ensures that we are moving forward relative to camera clipPlanePoints.UpperRight += transform.forward * distance; //UPPER LEFT //Move our point to left from position by width clipPlanePoints.UpperLeft = pos - transform.right * width; //Move our point up i.e. up by height clipPlanePoints.UpperLeft += transform.up * height; //Ensures that we are moving forward relative to camera clipPlanePoints.UpperLeft += transform.forward * distance; return(clipPlanePoints); }
//Determining the 4 points of Near clipping plane here. public static ClipPlanePoints ClipPlaneNear(Vector3 position) { //ClipPlanePoints calculated here. var clipPlanePoints = new ClipPlanePoints(); //local var to store clipPoints if (Camera.main == null) //idiotcheck. { return(clipPlanePoints); //This is zero everything. Cause there is no cam. } //MATHTIME: var tranform = Camera.main.transform; //Taking the FOV and dividing it to 2 and converting degrees to radians. var halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = Camera.main.aspect; //kuvasuhde //distance between the nearClipPlane and camera var distance = Camera.main.nearClipPlane; //To get height: distance is multiplied by tanget of half of FOV //that was calculated few lines above. var height = distance * Mathf.Tan(halfFOV); //getting widht. Also increases width relative to aspect ratio. var width = height * aspect; //Now to get the corners of Near Clipping plane! //CALCULATING CORNERS //Calculating Lower Right Corner clipPlanePoints.LowRight = position + tranform.right * width; clipPlanePoints.LowRight -= tranform.up * height; //what a weird way to say: "down" //Moving this point away from camera, so offset: clipPlanePoints.LowRight += tranform.forward * distance; //Calculating Lower Left Corner clipPlanePoints.LowLeft = position - tranform.right * width; //left = -right clipPlanePoints.LowLeft -= tranform.up * height; //Moving this point away from camera, so offset: clipPlanePoints.LowLeft += tranform.forward * distance; //Calculating Upper Right Corner clipPlanePoints.UpRight = position + tranform.right * width; clipPlanePoints.UpRight += tranform.up * height; //Moving this point away from camera, so offset: clipPlanePoints.UpRight += tranform.forward * distance; //Calculating Upper Left Corner clipPlanePoints.UpLeft = position - tranform.right * width; clipPlanePoints.UpLeft += tranform.up * height; //Moving this point away from camera, so offset: clipPlanePoints.UpLeft += tranform.forward * distance; return(clipPlanePoints); //here the whole package is returned to camCtrl. }
protected override void OnUpdate() { int i = 0; Entities.WithAll <Transform, CameraTarget>().ForEach((Entity targetEntity, Transform targetTransform) => { //Only want to do first entity with CaemraTarget if (i > 0) { return; } i++; //Get Target Entity from priority var dt = Time.deltaTime; var cameraTransform = Camera.main.transform; var cameraTargetData = EntityManager.GetSharedComponentData <CameraTarget>(targetEntity).data; //Get Camera Socekt targetTransform = targetTransform.Find("CameraSocket"); //Rotate { inputX += GInput.GetAxisRaw(GAxis.RIGHTHORIZONTAL) * dt * cameraTargetData.rotationSpeed; inputY += GInput.GetAxisRaw(GAxis.RIGHTVERTICAL) * dt * cameraTargetData.rotationSpeed; inputY = Mathf.Clamp(inputY, cameraTargetData.minRotatonY, cameraTargetData.maxRotationY); cameraTransform.eulerAngles = new Vector3(inputY, inputX); } //Zoom { if (!currentDistances.ContainsKey(targetEntity.Index)) { currentDistances.Add(targetEntity.Index, cameraTargetData.defaultDistance); } // currentDistances[targetEntity.Index] += Input.GetAxis("Mouse ScrollWheel") * cameraTargetData.zoomSpeed * dt; currentDistances[targetEntity.Index] = Mathf.Clamp(currentDistances[targetEntity.Index], cameraTargetData.minDistance, cameraTargetData.maxDistance); } //Move To Default Position cameraTransform.position = (targetTransform.position) - cameraTransform.forward * currentDistances[targetEntity.Index]; //Collision { if (!cameraTargetData.doCollision) { return; } ClipPlanePoints nearClipPlanePoints = GetCameraClipPlanePoints(); DetectCollision(ref nearClipPlanePoints, targetTransform); //Move To Position based on collision cameraTransform.position = (targetTransform.position) - cameraTransform.forward * ((nearClipPlanePoints.didCollide) ? nearClipPlanePoints.hitDistance : currentDistances[targetEntity.Index]); } }); }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 pos) { var myClipPlanePoints = new ClipPlanePoints(); // Do we even have a main camera to do calcuations? if (Camera.main == null) { return myClipPlanePoints; } // See 3D Buzz video "Enhanced Character Sytstem" pt 12 (NearClipPlane theory) for explainations var cameraTransform = Camera.main.transform; float halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; // FOV divided by 2 to make a right triangle. Tan() gives you radians. We want degrees, so convert. var aspect = Camera.main.aspect; // aspect ratio float distance = Camera.main.nearClipPlane; float height = distance * Mathf.Tan (halfFOV); // Geometry 101!!! (formula of right angles to find height) float width = height * aspect; // gets width in respect to aspect ratio // Moves LowerRight point from position to the right by the width myClipPlanePoints.LowerRight = pos + cameraTransform.right * width; // Move LowerRight point to bottom myClipPlanePoints.LowerRight -= cameraTransform.up * height; // Move LowerRight point away from the camera myClipPlanePoints.LowerRight += cameraTransform.forward * distance; // Moves LowerLeft point from position to the left by the width myClipPlanePoints.LowerLeft = pos - cameraTransform.right * width; // Move LowerLeft point to bottom myClipPlanePoints.LowerLeft -= cameraTransform.up * height; // Move LowerLeft point away from the camera myClipPlanePoints.LowerLeft += cameraTransform.forward * distance; // Moves UpperRight point from position to the right by the width myClipPlanePoints.UpperRight = pos + cameraTransform.right * width; // Move UpperRight point to top myClipPlanePoints.UpperRight += cameraTransform.up * height; // Move UpperRight point away from the camera myClipPlanePoints.UpperRight += cameraTransform.forward * distance; // Moves UpperLeft point from position to the left by the width myClipPlanePoints.UpperLeft = pos - cameraTransform.right * width; // Move UpperLeft point to top myClipPlanePoints.UpperLeft += cameraTransform.up * height; // Move UpperLeft point away from the camera myClipPlanePoints.UpperLeft += cameraTransform.forward * distance; return myClipPlanePoints; }
public static ClipPlanePoints ClipPlaneAtNear(Vector3 position) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); if (Camera.mainCamera != null) { // Get values from camera Transform transform = Camera.mainCamera.transform; float halfFov = Camera.mainCamera.fov * Mathf.Deg2Rad / 2.0f; float aspect = Camera.mainCamera.aspect; float distance = Camera.mainCamera.nearClipPlane; // Calculate width and height of clip plane float halfHeight = distance * Mathf.Tan(halfFov); float halfWidth = halfHeight * aspect; // Calculate corners of clip plane clipPlanePoints.LowerRight = position + transform.right * halfWidth; clipPlanePoints.LowerRight -= transform.up * halfHeight; clipPlanePoints.LowerRight += transform.forward * distance; clipPlanePoints.LowerLeft = position - transform.right * halfWidth; clipPlanePoints.LowerLeft -= transform.up * halfHeight; clipPlanePoints.LowerLeft += transform.forward * distance; clipPlanePoints.UpperRight = position + transform.right * halfWidth; clipPlanePoints.UpperRight += transform.up * halfHeight; clipPlanePoints.UpperRight += transform.forward * distance; clipPlanePoints.UpperLeft = position - transform.right * halfWidth; clipPlanePoints.UpperLeft += transform.up * halfHeight; clipPlanePoints.UpperLeft += transform.forward * distance; } return clipPlanePoints; }
private ClipPlanePoints ClipPlaneAtNear(Vector3 position) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints(); Transform cameraTransform = myTransform; float distance = Camera.mainCamera.nearClipPlane; Bounds bounds = originalTarget.parent.GetComponent<Collider>().bounds; float height = bounds.size.x; float width = bounds.size.x / 2; clipPlanePoints.LowerRight = position + cameraTransform.right * width; clipPlanePoints.LowerRight += cameraTransform.forward * distance; clipPlanePoints.LowerLeft = position - cameraTransform.right * width; clipPlanePoints.LowerLeft += cameraTransform.forward * distance; clipPlanePoints.UpperRight = position + cameraTransform.right * width; clipPlanePoints.UpperRight += cameraTransform.up * height; clipPlanePoints.UpperRight += cameraTransform.forward * distance; clipPlanePoints.UpperLeft = position - cameraTransform.right * width; clipPlanePoints.UpperLeft += cameraTransform.up * height; clipPlanePoints.UpperLeft += cameraTransform.forward * distance; return clipPlanePoints; }
// works out plane points at any distance inside a camera public ClipPlanePoints CameraClipPlanePoints(float distance) { ClipPlanePoints clipPlanePoints = new ClipPlanePoints (); Transform transform = Camera.main.transform; Vector3 position = transform.position; float halfFieldOfView = (Camera.main.fieldOfView * 0.5f) * Mathf.Deg2Rad; float aspect = Camera.main.aspect; float height = Mathf.Tan (halfFieldOfView) * distance; float width = height * aspect; // Lower Right clipPlanePoints.lowerRight = position + transform.forward * distance; clipPlanePoints.lowerRight += transform.right * width; clipPlanePoints.lowerRight -= transform.up * height; // Lower Left clipPlanePoints.lowerLeft = position + transform.forward * distance; clipPlanePoints.lowerLeft -= transform.right * width; clipPlanePoints.lowerLeft -= transform.up * height; // Upper Right clipPlanePoints.upperRight = position + transform.forward * distance; clipPlanePoints.upperRight += transform.right * width; clipPlanePoints.upperRight += transform.up * height; // Upper Left clipPlanePoints.upperLeft = position + transform.forward * distance; clipPlanePoints.upperLeft -= transform.right * width; clipPlanePoints.upperLeft += transform.up * height; return clipPlanePoints; }
bool CheckCullingRayCast( Vector3 from, ClipPlanePoints _to, float distance, LayerMask cullingLayer) { bool value = false; if (Physics.Raycast(from, _to.LowerLeft - from, distance, cullingLayer)) { value = true; } if (Physics.Raycast(from, _to.LowerRight - from, distance, cullingLayer)) { value = true; } if (Physics.Raycast(from, _to.UpperLeft - from, distance, cullingLayer)) { value = true; } if (Physics.Raycast(from, _to.UpperRight - from, distance, cullingLayer)) { value = true; } return value; }
/// <summary> /// Custom Raycast using NearClipPlanesPoints /// </summary> /// <param name="_to"></param> /// <param name="from"></param> /// <param name="hitInfo"></param> /// <param name="distance"></param> /// <param name="cullingLayer"></param> /// <returns></returns> bool CullingRayCast( Vector3 from, ClipPlanePoints _to, out RaycastHit hitInfo, float distance, LayerMask cullingLayer) { bool value = false; if (Physics.Raycast(from, _to.LowerLeft - from, out hitInfo, distance, cullingLayer)) { value = true; if (desiredDistance > hitInfo.distance) desiredDistance = hitInfo.distance; } if (Physics.Raycast(from, _to.LowerRight - from, out hitInfo, distance, cullingLayer)) { value = true; if (desiredDistance > hitInfo.distance) desiredDistance = hitInfo.distance ; } if (Physics.Raycast(from, _to.UpperLeft - from, out hitInfo, distance, cullingLayer)) { value = true; if (desiredDistance > hitInfo.distance) desiredDistance = hitInfo.distance ; } if (Physics.Raycast(from, _to.UpperRight - from, out hitInfo, distance, cullingLayer)) { value = true; if (desiredDistance > hitInfo.distance ) desiredDistance = hitInfo.distance; } return value; }
/// Creates 4 Vector3 points to represent cameras near clipping plane /// Used to determine player occlusion public static ClipPlanePoints NearClipPlane(Vector3 position) { var PlanePoints = new ClipPlanePoints(); if(Camera.main == null) return PlanePoints; var transform = Camera.main.transform; var halfFOV = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad; var aspect = Camera.main.aspect; var distance = Camera.main.nearClipPlane; var height = distance * Mathf.Tan(halfFOV); var width = height * aspect; PlanePoints.LowerRight = position + transform.right * width; PlanePoints.LowerRight -= transform.up * height; PlanePoints.LowerRight += transform.forward * distance; PlanePoints.LowerLeft = position - transform.right * width; PlanePoints.LowerLeft -= transform.up * height; PlanePoints.LowerLeft += transform.forward * distance; PlanePoints.UpperRight = position + transform.right * width; PlanePoints.UpperRight += transform.up * height; PlanePoints.UpperRight += transform.forward * distance; PlanePoints.UpperLeft = position - transform.right * width; PlanePoints.UpperLeft += transform.up * height; PlanePoints.UpperLeft += transform.forward * distance; return PlanePoints; }