--- a/Assets/Drawing/DrawingData.cs +++ b/Assets/Drawing/DrawingData.cs @@ -176,8 +176,10 @@ } } - public bool IsValidForCamera (Camera camera, bool allowGizmos, bool allowCameraDefault) { + public bool IsValidForCamera (Camera camera, bool allowGizmos, bool allowCameraDefault, HashSet hiddenScopes) { if (!allowGizmos && meta.isGizmos) return false; + // Hidden scopes keep their data alive but are never rendered. See RedrawScope.SetVisible. + if (hiddenScopes.Count > 0 && meta.redrawScope2.isValid && hiddenScopes.Contains(meta.redrawScope2.id)) return false; if (meta.cameraTargets != null) { return meta.cameraTargets.Contains(camera); @@ -766,10 +768,11 @@ public void SubmitMeshes (DrawingData gizmos, Camera camera, int versionThreshold, bool allowGizmos, bool allowCameraDefault) { if (data == null) return; MarkerSchedule.Begin(); + var hiddenScopes = gizmos.hiddenRedrawScopes; var cameraInfo = new GeometryBuilder.CameraInfo(camera); int c = 0; for (int i = 0; i < data.Length; i++) { - if (data[i].isValid && data[i].meta.version >= versionThreshold && data[i].IsValidForCamera(camera, allowGizmos, allowCameraDefault)) { + if (data[i].isValid && data[i].meta.version >= versionThreshold && data[i].IsValidForCamera(camera, allowGizmos, allowCameraDefault, hiddenScopes)) { c++; data[i].Schedule(gizmos, ref cameraInfo); } @@ -782,7 +785,7 @@ MarkerBuild.Begin(); for (int i = 0; i < data.Length; i++) { - if (data[i].isValid && data[i].meta.version >= versionThreshold && data[i].IsValidForCamera(camera, allowGizmos, allowCameraDefault)) { + if (data[i].isValid && data[i].meta.version >= versionThreshold && data[i].IsValidForCamera(camera, allowGizmos, allowCameraDefault, hiddenScopes)) { data[i].BuildMeshes(gizmos); } } @@ -804,10 +807,10 @@ MarkerPool.End(); } - public void CollectMeshes (int versionThreshold, List meshes, Camera camera, bool allowGizmos, bool allowCameraDefault) { + public void CollectMeshes (int versionThreshold, List meshes, Camera camera, bool allowGizmos, bool allowCameraDefault, HashSet hiddenScopes) { if (data == null) return; for (int i = 0; i < data.Length; i++) { - if (data[i].isValid && data[i].meta.version >= versionThreshold && data[i].IsValidForCamera(camera, allowGizmos, allowCameraDefault)) { + if (data[i].isValid && data[i].meta.version >= versionThreshold && data[i].IsValidForCamera(camera, allowGizmos, allowCameraDefault, hiddenScopes)) { data[i].CollectMeshes(meshes); } } @@ -1092,6 +1095,8 @@ #if ALINE_TRACK_REDRAW_SCOPE_LEAKS Dictionary persistentRedrawScopeInfos = new Dictionary(); #endif + /// IDs of redraw scopes whose items are kept alive but not rendered. See . + HashSet hiddenRedrawScopes = new HashSet(); internal System.Runtime.InteropServices.GCHandle gizmosHandle; public RedrawScope frameRedrawScope; @@ -1160,6 +1165,7 @@ #endif foreach (var v in shouldBeDestroyed) { persistentRedrawScopes.Remove(v); + hiddenRedrawScopes.Remove(v); #if ALINE_TRACK_REDRAW_SCOPE_LEAKS persistentRedrawScopeInfos.Remove(v); #endif @@ -1215,12 +1221,23 @@ if (scope.isValid) { processedData.SetVersion(scope, -1); persistentRedrawScopes.Remove(scope.id); + hiddenRedrawScopes.Remove(scope.id); #if ALINE_TRACK_REDRAW_SCOPE_LEAKS && UNITY_EDITOR persistentRedrawScopeInfos.Remove(scope.id); #endif } } + internal void SetRedrawScopeVisible (RedrawScope scope, bool visible) { + if (!scope.isValid) return; + if (visible) hiddenRedrawScopes.Remove(scope.id); + else hiddenRedrawScopes.Add(scope.id); + } + + internal bool IsRedrawScopeVisible (RedrawScope scope) { + return !hiddenRedrawScopes.Contains(scope.id); + } + void RefreshRedrawScopes () { #if UNITY_EDITOR && UNITY_2020_1_OR_NEWER var currentStage = UnityEditor.SceneManagement.StageUtility.GetCurrentStage(); @@ -1460,7 +1477,7 @@ MarkerBuildMeshes.End(); MarkerCollectMeshes.Begin(); meshes.Clear(); - processedData.CollectMeshes(cameraRenderingRange.start, meshes, cam, allowGizmos, allowCameraDefault); + processedData.CollectMeshes(cameraRenderingRange.start, meshes, cam, allowGizmos, allowCameraDefault, hiddenRedrawScopes); processedData.PoolDynamicMeshes(this); MarkerCollectMeshes.End(); @@ -1591,6 +1608,7 @@ // the session whenever the "a drawing instance is still active" error is logged. meshes.Clear(); persistentRedrawScopes.Clear(); + hiddenRedrawScopes.Clear(); #if ALINE_TRACK_REDRAW_SCOPE_LEAKS persistentRedrawScopeInfos.Clear(); #endif --- a/Assets/Drawing/DrawingDataTypes.cs +++ b/Assets/Drawing/DrawingDataTypes.cs @@ -82,6 +82,33 @@ #endif } + /// + /// True unless hidden with . + /// Always true when ALINE is excluded from builds. + /// + public bool isVisible { + get { +#if !ALINE_EXCLUDED_IN_BUILD || UNITY_EDITOR + if (gizmos.IsAllocated && gizmos.Target is DrawingData gizmosTarget) return gizmosTarget.IsRedrawScopeVisible(this); +#endif + return true; + } + } + + /// + /// Shows or hides everything drawn with this scope, without discarding it. + /// + /// Hidden items are kept alive (for scopes from ) but are not built or rendered for any camera. + /// Showing the scope again renders the retained items without having to redraw them. + /// Hidden items still use memory until the scope is disposed. Items drawn with a duration keep counting down while hidden. + /// Visibility is shared by all copies of the scope, and is reset when the scope is disposed. + /// + public void SetVisible (bool visible) { +#if !ALINE_EXCLUDED_IN_BUILD || UNITY_EDITOR + if (gizmos.IsAllocated && gizmos.Target is DrawingData gizmosTarget) gizmosTarget.SetRedrawScopeVisible(this, visible); +#endif + } + internal void DrawUntilDispose (GameObject associatedGameObject) { #if !ALINE_EXCLUDED_IN_BUILD || UNITY_EDITOR if (gizmos.Target is DrawingData gizmosTarget) gizmosTarget.DrawUntilDisposed(this, associatedGameObject); --- /dev/null +++ b/Assets/Drawing/CommandBuilderExtensions.Game.cs @@ -0,0 +1,170 @@ +using Unity.Burst; +using UnityEngine; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using Unity.Collections; +using Unity.Mathematics; +using Unity.Collections.LowLevel.Unsafe; +using Unity.Jobs; + +namespace Pathfinding.Drawing +{ + public partial struct CommandBuilder + { + public void AddSphereSweep(Ray ray, float radius, float maxDistance, in RaycastHit hit, string optionalLabel = null, float labelSize = 14.0f) + { +#if !ALINE_EXCLUDED_IN_BUILD || UNITY_EDITOR + bool hitSomething = hit.collider != null; + + Vector3 src = ray.origin; + Vector3 end = ray.GetPoint(hitSomething ? hit.distance : maxDistance); + + Color endColor = hitSomething ? Color.red : Color.green; + WireSphere(src, radius, Color.green); + WireSphere(end, radius, endColor); + + // draw sweep path subtle + endColor.a = 0.1f; + Line(src, end, endColor); + + if (!string.IsNullOrEmpty(optionalLabel)) + { + Vector3 avg = (src + end) / 2.0f; + Label2D(avg, optionalLabel, labelSize, LabelAlignment.Center); + } +#endif + } + public void AddSphereSweep(Ray ray, float radius, float maxDistance, RaycastHit[] hits, int hitCount, string optionalLabel = null, float labelSize = 14.0f) + { +#if !ALINE_EXCLUDED_IN_BUILD || UNITY_EDITOR + float? nearestHitDistance = null; + for (int i = 0; i < hitCount; i++) + { + ref RaycastHit hit = ref hits[i]; + if (hit.distance == 0.0f) + { + using (WithColor(Color.red)) + { + SphereOutline(ray.origin, radius - 0.1f); + } + continue; + } + + if (!nearestHitDistance.HasValue) + { + nearestHitDistance = hit.distance; + } + + SolidPlane(ray.GetPoint(hit.distance), -ray.direction, new(0.25f)); + } + + Vector3 src = ray.origin; + Vector3 end = ray.GetPoint(nearestHitDistance ?? maxDistance); + + Color endColor = nearestHitDistance.HasValue ? Color.red : Color.green; + WireSphere(src, radius, Color.green); + WireSphere(end, radius, endColor); + + // draw sweep path subtle + endColor.a = 0.1f; + Line(src, end, endColor); + + if (!string.IsNullOrEmpty(optionalLabel)) + { + Vector3 avg = (src + end) / 2.0f; + Label2D(avg, optionalLabel, labelSize, LabelAlignment.Center); + } +#endif + } + + public void AddCapsuleSweep(float3 p0, float3 p1, float3 sweepDirection, float radius, float maxDistance, in RaycastHit hit, string optionalLabel = null, float labelSize = 14.0f) + { +#if !ALINE_EXCLUDED_IN_BUILD || UNITY_EDITOR + bool hitSomething = hit.collider != null; + float3 delta = sweepDirection * (hitSomething ? hit.distance : maxDistance); + + float3 p0a = p0; + float3 p0b = p1; + float3 p1a = p0 + delta; + float3 p1b = p1 + delta; + + WireCapsule(p0a, p0b, radius, Color.green); + + // draw sweep path subtle + Color endColor = hitSomething ? Color.red : Color.green; + + endColor.a = 0.1f; + using (WithColor(endColor)) + { + WireCapsule(p1a, p1b, radius); + + Line(p0a, p1a); + Line(p0b, p1b); + + Vector3 mid = p0a + p0b + p1a + p1b; + Vector3 normal = Vector3.Cross(p1a - p0, sweepDirection); + SolidTriangle(p0a, p0b, Vector3.Lerp(p1a,p1b, 0.5f)); + } + + if (!string.IsNullOrEmpty(optionalLabel)) + { + Vector3 avg = (p0a + p0b + p1a + p1b) / 4.0f; + Label2D(avg, optionalLabel, labelSize, LabelAlignment.Center); + } +#endif + } + + public void AddCapsuleSweep(float3 p0, float3 p1, float3 sweepDirection, float radius, float maxDistance, RaycastHit[] hits, int hitCount, string optionalLabel = null, float labelSize = 14.0f) + { +#if !ALINE_EXCLUDED_IN_BUILD || UNITY_EDITOR + float? nearestHitDistance = null; + for (int i = 0; i < hitCount; i++) + { + ref RaycastHit hit = ref hits[i]; + if (hit.distance == 0.0f) + { + using (WithLineWidth(3.0f)) + { + WireCapsule(p0, p1, radius - 0.05f, Color.red); + } + continue; + } + + nearestHitDistance ??= hit.distance; + nearestHitDistance = Mathf.Min(nearestHitDistance.Value, hit.distance); + + SolidPlane(hit.point, -sweepDirection, new(0.25f)); + } + float3 delta = sweepDirection * (nearestHitDistance ?? maxDistance); + + float3 p0a = p0; + float3 p0b = p1; + float3 p1a = p0 + delta; + float3 p1b = p1 + delta; + + WireCapsule(p0a, p0b, radius, Color.green); + + // draw sweep path subtle + Color endColor = nearestHitDistance.HasValue ? Color.red : Color.green; + endColor.a = 0.1f; + using (WithColor(endColor)) + { + WireCapsule(p1a, p1b, radius, endColor); + + Line(p0a, p1a); + Line(p0b, p1b); + + Vector3 mid = p0a + p0b + p1a + p1b; + Vector3 normal = Vector3.Cross(p1a - p0, sweepDirection); + SolidTriangle(p0a, p0b, Vector3.Lerp(p1a,p1b, 0.5f)); + } + + if (!string.IsNullOrEmpty(optionalLabel)) + { + Vector3 avg = (p0a + p0b + p1a + p1b) / 4.0f; + Label2D(avg, optionalLabel, labelSize, LabelAlignment.Center); + } +#endif + } + } +} \ No newline at end of file --- /dev/null +++ b/Assets/Drawing/CommandBuilderExtensions.Game.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d90ebb5c737f37a4db3ac25bb69ca63e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: