- 1 using System.Collections;
- 2 using System.Collections.Generic;
- 3 using UnityEngine;
- 4
- 5 public static class TestExpand
- 6 {
- 7 /// <summary>
- 8 /// 检查一个世界坐标系下的点是否在摄像机可见范围内
- 9 /// </summary>
- 10 /// <param name="camera"></param>
- 11 /// <param name="worldPos"></param>
- 12 /// <returns></returns>
- 13 public static bool CheckPointIsInCamera(this Camera camera, Vector3 worldPos)
- 14 {
- 15 Vector3 viewPos = camera.WorldToViewportPoint(worldPos);
- 16 //Debug.Log(worldPos+" "+viewPos);
- 17 if(viewPos.x<0 || viewPos.x>1 || viewPos.y<0 || viewPos.y > 1 || viewPos.z<0 || viewPos.z>camera.farClipPlane)
- 18 {
- 19 return false;
- 20 }
- 21 return true;
- 22 }
- 23
- 24 /// <summary>
- 25 /// 检查bound某个点是否在摄像机可见范围内
- 26 /// </summary>
- 27 /// <param name="bound"></param>
- 28 /// <param name="camera"></param>
- 29 /// <returns></returns>
- 30 public static bool CheckBoundIsInCamera(this Bounds bound, Camera camera)
- 31 {
- 32 Vector4 worldPos = Vector4.one;
- 33 for (int i = -1; i <= 1; i += 2)
- 34 {
- 35 for (int j = -1; j <= 1; j += 2)
- 36 {
- 37 for (int k = -1; k <= 1; k += 2)
- 38 {
- 39 worldPos.x = bound.center.x + i * bound.extents.x;
- 40 worldPos.y = bound.center.y + j * bound.extents.y;
- 41 worldPos.z = bound.center.z + k * bound.extents.z;
- 42 if (camera.CheckPointIsInCamera(worldPos))
- 43 {
- 44 return true;
- 45 }
- 46
- 47 }
- 48 }
- 49 }
- 50 return false;
- 51 }
- 52 }