Here’s the pattern in use now:
The raycasting code:
bool NoRaycasts = false;
protected override void OnUpdate(float timeStep)
{
if(!NoRaycasts) Raycast();
}
bool IsRaycasting = true;
DateTime lastRaycast = DateTime.Now;
void Raycast()
{
if (IsRaycasting)
{
var result = Scene.GetComponent<Octree>().RaycastSingle(RightCamera.GetScreenRay(0.5f, 0.5f), RayQueryLevel.Triangle, 100, DrawableFlags.Geometry, 0x70000000);
if (result.HasValue)
{
IsRaycasting = false;
lastRaycast = DateTime.Now;
nodeQ.Add(result.Value);
}
}
else if (DateTime.Now - lastRaycast > TimeSpan.FromSeconds(.1)) { IsRaycasting = true; }
}
The raycast processing code …
Task.Run(() => {
while (!nodeQ.IsCompleted)
{
var result = nodeQ.Take();
var OriginalMask = result.Drawable.ViewMask;
result.Drawable.ViewMask = 0x80000000;
var currentScale = result.Node.Scale.X;
InvokeOnMainAsync(() => result.Node.RunActions(new ScaleTo(0.1f, currentScale * 2 / 3f), new Blink(1f, 6), new ScaleTo(0.1f, currentScale), new CallFunc(()=> result.Drawable.ViewMask = OriginalMask)));
}
});
It’s still tightly bound to the OS, as using DateTime, BlockingCollection, Task classes.
Allows multiple items to be in raycasted state at once, only blocks raycasts for .1 seconds at a time, and leaves the object to reset in the raycast processing code. It’s not quite done, as every now and then an object disappears, and not sure why yet. Might be something else unrelated to this.
The disappearing object is because the Task to process the nodeQ delay starts and the queue would get multiple occurrences of the same object. So simply started raycasting only after the task started, and everything works perfectly now.