using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; namespace HighlightPlus { public partial class HighlightEffect : MonoBehaviour { static readonly List occluders = new List(); static readonly Dictionary occludersFrameCount = new Dictionary(); static CommandBuffer cbOccluder; static Material fxMatSeeThroughOccluder, fxMatDepthWrite; static RaycastHit[] hits; /// /// True if the see-through is cancelled by an occluder using raycast method /// public bool IsSeeThroughOccluded(Camera cam) { // Compute bounds Bounds bounds = new Bounds(); for (int r = 0; r < rms.Length; r++) { if (rms[r].renderer != null) { if (bounds.size.x == 0) { bounds = rms[r].renderer.bounds; } else { bounds.Encapsulate(rms[r].renderer.bounds); } } } Vector3 pos = bounds.center; Vector3 camPos = cam.transform.position; Vector3 offset = pos - camPos; float maxDistance = Vector3.Distance(pos, camPos); if (hits == null || hits.Length == 0) { hits = new RaycastHit[64]; } int occludersCount = occluders.Count; int hitCount = Physics.BoxCastNonAlloc(pos - offset, bounds.extents * 0.9f, offset.normalized, hits, Quaternion.identity, maxDistance); for (int k = 0; k < hitCount; k++) { for (int j = 0; j < occludersCount; j++) { if (hits[k].collider.transform == occluders[j].transform) { return true; } } } return false; } public static void RegisterOccluder(HighlightSeeThroughOccluder occluder) { if (!occluders.Contains(occluder)) { occluders.Add(occluder); } } public static void UnregisterOccluder(HighlightSeeThroughOccluder occluder) { if (occluders.Contains(occluder)) { occluders.Remove(occluder); } } /// /// Test see-through occluders. /// /// The camera to be tested /// Returns true if there's no raycast-based occluder cancelling the see-through effect public bool RenderSeeThroughOccluders(Camera cam) { int occludersCount = occluders.Count; if (occludersCount == 0 || rmsCount == 0) return true; bool useRayCastCheck = false; // Check if raycast method is needed for (int k = 0; k < occludersCount; k++) { HighlightSeeThroughOccluder occluder = occluders[k]; if (occluder == null || !occluder.isActiveAndEnabled) continue; if (occluder.detectionMethod == DetectionMethod.RayCast) { useRayCastCheck = true; break; } } if (useRayCastCheck) { if (IsSeeThroughOccluded(cam)) return false; } // do not render see-through occluders more than once this frame per camera (there can be many highlight effect scripts in the scene, we only need writing to stencil once) int lastFrameCount; occludersFrameCount.TryGetValue(cam, out lastFrameCount); int currentFrameCount = Time.frameCount; if (currentFrameCount == lastFrameCount) return true; occludersFrameCount[cam] = currentFrameCount; if (cbOccluder == null) { cbOccluder = new CommandBuffer(); cbOccluder.name = "Occluder"; } if (fxMatSeeThroughOccluder == null) { InitMaterial(ref fxMatSeeThroughOccluder, "HighlightPlus/Geometry/SeeThroughOccluder"); if (fxMatSeeThroughOccluder == null) return true; } if (fxMatDepthWrite == null) { InitMaterial(ref fxMatDepthWrite, "HighlightPlus/Geometry/JustDepth"); if (fxMatDepthWrite == null) return true; } cbOccluder.Clear(); for (int k = 0; k < occludersCount; k++) { HighlightSeeThroughOccluder occluder = occluders[k]; if (occluder == null || !occluder.isActiveAndEnabled) continue; if (occluder.detectionMethod == DetectionMethod.Stencil) { if (occluder.meshData == null) continue; int meshDataLength = occluder.meshData.Length; // Per renderer for (int m = 0; m < meshDataLength; m++) { // Per submesh Renderer renderer = occluder.meshData[m].renderer; if (renderer.isVisible) { for (int s = 0; s < occluder.meshData[m].subMeshCount; s++) { cbOccluder.DrawRenderer(renderer, occluder.mode == OccluderMode.BlocksSeeThrough ? fxMatSeeThroughOccluder : fxMatDepthWrite, s); } } } } } Graphics.ExecuteCommandBuffer(cbOccluder); return true; } bool CheckOcclusion(Camera cam) { if (!perCameraOcclusionData.TryGetValue(cam, out PerCameraOcclusionData occlusionData)) { occlusionData = new PerCameraOcclusionData(); perCameraOcclusionData[cam] = occlusionData; } float now = Time.time; int frameCount = Time.frameCount; // ensure all cameras are checked this frame if (now - occlusionData.checkLastTime < seeThroughOccluderCheckInterval && Application.isPlaying && occlusionData.occlusionRenderFrame != frameCount) return occlusionData.lastOcclusionTestResult; occlusionData.checkLastTime = now; occlusionData.occlusionRenderFrame = frameCount; if (rms == null || rms.Length == 0 || rms[0].renderer == null) return false; Vector3 camPos = cam.transform.position; if (seeThroughOccluderCheckIndividualObjects) { for (int r = 0; r < rms.Length; r++) { if (rms[r].renderer != null) { Bounds bounds = rms[r].renderer.bounds; Vector3 pos = bounds.center; float maxDistance = Vector3.Distance(pos, camPos); if (Physics.BoxCast(pos, bounds.extents * seeThroughOccluderThreshold, (camPos - pos).normalized, Quaternion.identity, maxDistance, seeThroughOccluderMask)) { occlusionData.lastOcclusionTestResult = true; return true; } } } occlusionData.lastOcclusionTestResult = false; return false; } else { // Compute combined bounds Bounds bounds = rms[0].renderer.bounds; for (int r = 1; r < rms.Length; r++) { if (rms[r].renderer != null) { bounds.Encapsulate(rms[r].renderer.bounds); } } Vector3 pos = bounds.center; float maxDistance = Vector3.Distance(pos, camPos); occlusionData.lastOcclusionTestResult = Physics.BoxCast(pos, bounds.extents * seeThroughOccluderThreshold, (camPos - pos).normalized, Quaternion.identity, maxDistance, seeThroughOccluderMask); return occlusionData.lastOcclusionTestResult; } } const int MAX_OCCLUDER_HITS = 50; static RaycastHit[] occluderHits; void AddWithoutRepetition(List target, List source) { int sourceCount = source.Count; for (int k = 0; k < sourceCount; k++) { T entry = source[k]; if (entry != null && !target.Contains(entry)) { target.Add(entry); } } } void CheckOcclusionAccurate(Camera cam) { if (!perCameraOcclusionData.TryGetValue(cam, out PerCameraOcclusionData occlusionData)) { occlusionData = new PerCameraOcclusionData(); perCameraOcclusionData[cam] = occlusionData; } float now = Time.time; int frameCount = Time.frameCount; // ensure all cameras are checked this frame bool reuse = now - occlusionData.checkLastTime < seeThroughOccluderCheckInterval && Application.isPlaying && occlusionData.occlusionRenderFrame != frameCount; if (!reuse) { if (rms == null || rms.Length == 0 || rms[0].renderer == null) return; occlusionData.checkLastTime = now; occlusionData.occlusionRenderFrame = frameCount; Quaternion quaternionIdentity = Quaternion.identity; Vector3 camPos = cam.transform.position; occlusionData.cachedOccluders.Clear(); if (occluderHits == null || occluderHits.Length < MAX_OCCLUDER_HITS) { occluderHits = new RaycastHit[MAX_OCCLUDER_HITS]; } if (seeThroughOccluderCheckIndividualObjects) { for (int r = 0; r < rms.Length; r++) { if (rms[r].renderer != null) { Bounds bounds = rms[r].renderer.bounds; Vector3 pos = bounds.center; float maxDistance = Vector3.Distance(pos, camPos); int numOccluderHits = Physics.BoxCastNonAlloc(pos, bounds.extents * seeThroughOccluderThreshold, (camPos - pos).normalized, occluderHits, quaternionIdentity, maxDistance, seeThroughOccluderMask); for (int k = 0; k < numOccluderHits; k++) { occluderHits[k].collider.transform.root.GetComponentsInChildren(tempRR); AddWithoutRepetition(occlusionData.cachedOccluders, tempRR); } } } } else { // Compute combined bounds Bounds bounds = rms[0].renderer.bounds; for (int r = 1; r < rms.Length; r++) { if (rms[r].renderer != null) { bounds.Encapsulate(rms[r].renderer.bounds); } } Vector3 pos = bounds.center; float maxDistance = Vector3.Distance(pos, camPos); int numOccluderHits = Physics.BoxCastNonAlloc(pos, bounds.extents * seeThroughOccluderThreshold, (camPos - pos).normalized, occluderHits, quaternionIdentity, maxDistance, seeThroughOccluderMask); for (int k = 0; k < numOccluderHits; k++) { occluderHits[k].collider.transform.root.GetComponentsInChildren(tempRR); AddWithoutRepetition(occlusionData.cachedOccluders, tempRR); } } } // render occluders int occluderRenderersCount = occlusionData.cachedOccluders.Count; if (occluderRenderersCount > 0) { cbSeeThrough.Clear(); for (int k = 0; k < occluderRenderersCount; k++) { Renderer r = occlusionData.cachedOccluders[k]; if (r != null) { cbSeeThrough.DrawRenderer(r, fxMatSeeThroughMask); } } Graphics.ExecuteCommandBuffer(cbSeeThrough); } } public List GetOccluders(Camera camera) { if (perCameraOcclusionData.TryGetValue(camera, out PerCameraOcclusionData occlusionData)) { return occlusionData.cachedOccluders; } return null; } } }