using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; public class FakeCamFrustum : MonoBehaviour { public float farDistance; public float fov; public float viewRatio; public Material frustumMat; float farHeight,farWidth; Vector3 farCenter, farTopLeft, farTopRight, farBotLeft, farBotRight; GameObject[] Triangles; void Start () { farHeight = 2.0f * farDistance * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad); farWidth = farHeight * viewRatio; farCenter = transform.position - transform.forward * farDistance; farTopLeft = farCenter + transform.up * (farHeight * 0.5f) - transform.right * (farWidth * 0.5f); farTopRight = farCenter + transform.up * (farHeight * 0.5f) + transform.right * (farWidth * 0.5f); farBotLeft = farCenter - transform.up * (farHeight * 0.5f) - transform.right * (farWidth * 0.5f); farBotRight = farCenter - transform.up * (farHeight * 0.5f) + transform.right * (farWidth * 0.5f); Triangles = new GameObject[4]; for (int i = 0; i < 4; i++) { GameObject child = new GameObject(); MeshFilter mf = child.AddComponent(); child.transform.parent = transform; Triangles[i] = child; Vector3[] vertices = new Vector3[] { Vector3.zero, Vector3.zero, Vector3.zero }; Vector3[] normals = new Vector3[] { Vector3.zero, Vector3.zero, Vector3.zero }; int[] triangles = new int[] { 2,1,0 }; mf.mesh = new Mesh(); mf.mesh.vertices = vertices; mf.mesh.triangles = triangles; MeshRenderer mr = child.AddComponent(); mr.material = frustumMat; } Triangles[0].GetComponent().mesh.vertices = new[] { transform.position, farTopLeft, farTopRight }; Triangles[0].GetComponent().mesh.normals = new[] { new Plane(transform.position, farTopLeft, farTopRight).normal, new Plane(transform.position, farTopLeft, farTopRight).normal, new Plane(transform.position, farTopLeft, farTopRight).normal }; Triangles[1].GetComponent().mesh.vertices = new[] { transform.position, farTopRight, farBotRight }; Triangles[1].GetComponent().mesh.normals = new[] { new Plane(transform.position, farTopRight, farBotRight).normal, new Plane(transform.position, farTopRight, farBotRight).normal, new Plane(transform.position, farTopRight, farBotRight).normal }; Triangles[2].GetComponent().mesh.vertices = new[] { transform.position, farBotRight, farBotLeft }; Triangles[2].GetComponent().mesh.normals = new[] { new Plane(transform.position, farBotRight, farBotLeft).normal, new Plane(transform.position, farBotRight, farBotLeft).normal, new Plane(transform.position, farBotRight, farBotLeft).normal }; Triangles[3].GetComponent().mesh.vertices = new[] { transform.position, farBotLeft, farTopLeft }; Triangles[3].GetComponent().mesh.normals = new[] { new Plane(transform.position, farBotLeft, farTopLeft).normal, new Plane(transform.position, farBotLeft, farTopLeft).normal, new Plane(transform.position, farBotLeft, farTopLeft).normal }; } }