Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions CodeWalker.Core/World/Space.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ public void GetVisibleYnvs(Camera cam, int gridrange, List<YnvFile> 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;
Expand All @@ -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++)
{
Expand Down Expand Up @@ -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];
Expand Down
45 changes: 37 additions & 8 deletions CodeWalker/WorldForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down