From 54156504ea1c5fdb4f4fee2f79645b6c222b60d0 Mon Sep 17 00:00:00 2001 From: ook3d <47336113+ook3D@users.noreply.github.com> Date: Tue, 24 Feb 2026 22:06:11 -0500 Subject: [PATCH] properly toggle drawable collision mesh layers --- CodeWalker.Core/World/Space.cs | 15 ++++++++++-- CodeWalker/WorldForm.cs | 45 ++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/CodeWalker.Core/World/Space.cs b/CodeWalker.Core/World/Space.cs index 2c788837d..99c24a127 100644 --- a/CodeWalker.Core/World/Space.cs +++ b/CodeWalker.Core/World/Space.cs @@ -1238,7 +1238,7 @@ public void GetVisibleYnvs(Camera cam, int gridrange, List ynvs) } - public SpaceRayIntersectResult RayIntersect(Ray ray, float maxdist = float.MaxValue, bool[] layers = null) + public SpaceRayIntersectResult RayIntersect(Ray ray, float maxdist = float.MaxValue, bool[] layers = null, bool testDrawableCollisions = true) { var res = new SpaceRayIntersectResult(); if (GameFileCache == null) return res; @@ -1250,7 +1250,6 @@ public SpaceRayIntersectResult RayIntersect(Ray ray, float maxdist = float.MaxVa if ((BoundsStore == null) || (MapDataStore == null)) return res; var boundslist = BoundsStore.GetItems(ref ray, layers); - var mapdatalist = MapDataStore.GetItems(ref ray); for (int i = 0; i < boundslist.Count; i++) { @@ -1281,6 +1280,18 @@ public SpaceRayIntersectResult RayIntersect(Ray ray, float maxdist = float.MaxVa } } + if (!testDrawableCollisions) + { + if (res.Hit) + { + res.Position = ray.Position + ray.Direction * res.HitDist; + } + res.TestComplete = testcomplete; + return res; + } + + var mapdatalist = MapDataStore.GetItems(ref ray); + for (int i = 0; i < mapdatalist.Count; i++) { var mapdata = mapdatalist[i]; diff --git a/CodeWalker/WorldForm.cs b/CodeWalker/WorldForm.cs index 8f641ab5b..d84b3eec6 100644 --- a/CodeWalker/WorldForm.cs +++ b/CodeWalker/WorldForm.cs @@ -2369,22 +2369,51 @@ private void BeginMouseHitTest() } + private SpaceRayIntersectResult _cachedMouseRay; + private Vector3 _lastMouseRayPosition; + private Vector3 _lastMouseRayDirection; + private Vector3 _lastCameraPosition; + private bool _lastDrawableCollisionEnabled; + public SpaceRayIntersectResult GetSpaceMouseRay() { - SpaceRayIntersectResult ret = new SpaceRayIntersectResult(); - if (space.Inited && space.BoundsStore != null) + if (!space.Inited || space.BoundsStore == null) + { + return new SpaceRayIntersectResult(); + } + + // check if we can use cached result + var currentMouseRayPos = camera.MouseRay.Position; + var currentMouseRayDir = camera.MouseRay.Direction; + var currentCameraPos = camera.Position; + var drawableCollisionEnabled = Renderer.rendercollisionmeshlayerdrawable; + + if (_cachedMouseRay.Hit && + _lastMouseRayPosition == currentMouseRayPos && + _lastMouseRayDirection == currentMouseRayDir && + _lastCameraPosition == currentCameraPos && + _lastDrawableCollisionEnabled == drawableCollisionEnabled) { - Ray mray = new Ray(); - mray.Position = camera.MouseRay.Position + camera.Position; - mray.Direction = camera.MouseRay.Direction; - return space.RayIntersect(mray, float.MaxValue, collisionmeshlayers); + return _cachedMouseRay; } - return ret; + + // calculate new ray intersection + Ray mray = new Ray(); + mray.Position = currentMouseRayPos + currentCameraPos; + mray.Direction = currentMouseRayDir; + + _cachedMouseRay = space.RayIntersect(mray, float.MaxValue, collisionmeshlayers); + _lastMouseRayPosition = currentMouseRayPos; + _lastMouseRayDirection = currentMouseRayDir; + _lastCameraPosition = currentCameraPos; + _lastDrawableCollisionEnabled = drawableCollisionEnabled; + + return _cachedMouseRay; } public SpaceRayIntersectResult Raycast(Ray ray) { - return space.RayIntersect(ray, float.MaxValue, collisionmeshlayers); + return space.RayIntersect(ray, float.MaxValue, collisionmeshlayers, Renderer.rendercollisionmeshlayerdrawable); } private void UpdateMouseHits()