#define LightCone_OnDrawGizmos_Collide using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent( typeof(Light) )] public class LightCone : MonoBehaviour { public int wedges = 36; public float dist = 20; public float width = 2; public float height = 2; [SerializeField] private Color _color = new Color(1, 1, 1, 0.25f); public LayerMask collidesWith; public int gizmoWedges = 4; [Header("Spotlight Settings")] public bool spotlightEnabled = true; public float spotAnglePercent = 100; public float rangePercent = 100; MeshFilter mF; Mesh m; Vector3[] verts; int[] tris; Material mat; RaycastHit[] hits; private Light lt; private float dist0, width0, sAP0, rP0; public Color color { get { return _color; } set { _color = value; UpdateLight(); } } // Use this for initialization void Start () { mF = GetComponent(); m = mF.mesh; m.Clear(); MakeVerts(); tris = new int[wedges*3]; int triN=0; int vertN=1; for (int i=0; i().material; UpdateLight(); } void MakeVerts() { verts = new Vector3[wedges+1]; verts[0] = Vector3.zero; Vector3 v, r; hits = new RaycastHit[wedges]; for (int i=0; i0) { Gizmos.DrawLine(pts[i-1], pts[i]); if (i == gizmoWedges-1) { Gizmos.DrawLine(pts[i], pts[0]); } } #endif } #if LightCone_OnDrawGizmos_Collide Vector3[] ptsColl = new Vector3[gizmoWedges]; RaycastHit hitInfo; for (int i = 0; i < pts.Length; i++) { ptsColl[i] = pts[i]; vDir = ptsColl[i] - transform.position; if (Physics.Raycast(transform.position, vDir, out hitInfo, vDir.magnitude, collidesWith)) { // The line collided with something, so make it shorter ptsColl[i] = hitInfo.point; } // Draw the area past the collision Gizmos.color = grayColor; Gizmos.DrawLine(ptsColl[i], pts[i]); if (i > 0) { Gizmos.DrawLine(pts[i - 1], pts[i]); if (i == gizmoWedges - 1) { Gizmos.DrawLine(pts[i], pts[0]); } } // Draw the collision Gizmos.color = litColor; Gizmos.DrawLine(transform.position, ptsColl[i]); if (i > 0) { Gizmos.DrawLine(ptsColl[i - 1], ptsColl[i]); if (i == gizmoWedges - 1) { Gizmos.DrawLine(ptsColl[i], ptsColl[0]); } } } #endif } private void OnDrawGizmos() { if (Application.isPlaying) return; UpdateLight(); } void UpdateLight() { if (lt == null) { lt = GetComponent(); lt.type = LightType.Spot; } if (dist0 != dist || width0 != width || sAP0 != spotAnglePercent || rangePercent != rP0) { lt.spotAngle = Mathf.Atan2(width, dist)*360/Mathf.PI * (spotAnglePercent / 100f); lt.range = dist * rangePercent / 100f; dist0 = dist; width0 = width; sAP0 = spotAnglePercent; rP0 = rangePercent; } lt.color = color; lt.enabled = spotlightEnabled; } public RaycastHit[] GetRaycastHits() { return hits; } }