I’m using RaycastCommands to gain a bit of performance when casting millions of rays in Unity 2018.3.1, but I’m getting a strange message if (and only if) I use large buffers.
Here’s the code (you can just attach it to some object and press play):
Script
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
public class RaycastCommandExample : MonoBehaviour{
public int raycastCount = 2048*2048;
[ReadOnly] private NativeArray<RaycastHit> results;
[ReadOnly] private NativeArray<RaycastCommand> commands;
void Start() {
results = new NativeArray<RaycastHit>(raycastCount, Allocator.TempJob);
commands = new NativeArray<RaycastCommand>(raycastCount, Allocator.TempJob);
for (int i = 0; i < raycastCount; i++) {
commands[i] = new RaycastCommand(Vector3.zero, Vector3.forward, 1);
}
JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 32, default(JobHandle));
handle.Complete();
//...do something with results array entries
commands.Dispose();
results.Dispose();
}
}
When raycastCount is lower than a certain value (that in my case happens to be 1568529), I get no messages.
But when I use a higher value I get this:
I have exactly the same issue.
Anyone know if this is a new problem?
I’m new to the job system so don’t know if this has always been the case. I may try a slightly earlier Unity version to test.
No not sure either.
As far as I can tell my code still works as intended with all batched raycasts completed, so I think it may just be an erroneous assert.
Does your code still work as intended, or you can see other issues in addition to the error message?
A wild guess but I think they probably limit the amount of ray according to what the max size a batch buffer can handle.
I ran into this issue before, I decided to divide by groups the rays then use a job to initialized the commands, then batch the raycast, something like this.
protected override JobHandle OnUpdate(JobHandle handle)
{
for (int i = 0; i < groups.Length; ++i)
{
int Length = groups[i].NumberOfRaysOnGroup;
NativeArray<RaycastCommand> raycastCommands = new NativeArray<RaycastCommand>(
Length, Allocator.TempJob, NativeArrayOptions.ClearMemory
);
NativeArray<RaycastHit> raycastHits = new NativeArray<RaycastHit>(
Length, Allocator.TempJob, NativeArrayOptions.ClearMemory
);
InitializeRaycastCommandJob setupRaycastsJob = new InitializeRaycastCommandJob()
{
Raycasts = raycastCommands,
Tracer = Rays_LUT[i]
};
//Schedule the commands
JobHandle setupDependency = setupRaycastsJob.Schedule(Length,
Length / Job_Settings.InitializeRayCastCommandJobDivider, handle);
//Schedule the rays on batch
JobHandle raycastDependency = RaycastCommand.ScheduleBatch(raycastCommands,
raycastHits, Length / Job_Settings.MinimumRaysPerJobDivider, setupDependency);
}
}
Good workaround! Thanks for sharing!!
Do you think the max batch size is something hard coded? 'Cause to me only lengths higher than 1568529 produce this error. This is not a common number…