Skip to content

Instantly share code, notes, and snippets.

@Defunctionalize
Last active July 14, 2020 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Defunctionalize/75156fad916c8ae9ab334fcb7de364f1 to your computer and use it in GitHub Desktop.
Save Defunctionalize/75156fad916c8ae9ab334fcb7de364f1 to your computer and use it in GitHub Desktop.
Working version but slightly less parallel
using System.Linq;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
[GenerateAuthoringComponent]
public struct MyThreshold : IComponentData
{
public float threshold;
}
[GenerateAuthoringComponent]
public struct MyComponent : IComponentData
{
public bool closeEnough;
}
public class CheckAllPairsSystem : SystemBase
{
[BurstCompile]
struct CheckAllPairsJob : IJobParallelFor
{
[DeallocateOnJobCompletion] public NativeArray<ArchetypeChunk> Chunks;
[ReadOnly] public ArchetypeChunkComponentType<Translation> TranslationType;
[ReadOnly] public ArchetypeChunkComponentType<MyThreshold> MyThresholdType;
public ArchetypeChunkComponentType<MyComponent> MyComponentType;
public void Execute(int jobIndex)
{
var chunk = Chunks[jobIndex];
var translations = chunk.GetNativeArray(TranslationType);
var mycomponents = chunk.GetNativeArray(MyComponentType);
var mythresholds = chunk.GetNativeArray(MyThresholdType);
var instanceCount = chunk.Count;
ArchetypeChunk chunk2;
NativeArray<Translation> translations2;
int instanceCount2;
for (int i = 0; i < Chunks.Length; i++)
{
chunk2 = Chunks[i];
translations2 = chunk2.GetNativeArray(TranslationType);
instanceCount2 = chunk2.Count;
for (int j = 0; j < instanceCount; j++)
{
for (int k = 0; k < instanceCount2; k++)
{
// if checking against the same chunk, dont check against the same index,
// because that would be the same entity
if (i == jobIndex && j == k) { continue; }
var a = mycomponents[j];
var aTheshold = mythresholds[j];
var aTrans = translations[j];
var bTrans = translations2[k];
var distance = math.distance(aTrans.Value, bTrans.Value);
a.closeEnough = a.closeEnough || distance < aTheshold.threshold;
mycomponents[j] = a;
}
}
}
}
}
EntityQuery query;
protected override void OnCreate()
{
query = GetEntityQuery(typeof(MyComponent),
ComponentType.ReadOnly<MyThreshold>(),
ComponentType.ReadOnly<Translation>());
}
protected override void OnUpdate()
{
var translationType = GetArchetypeChunkComponentType<Translation>(true);
var myComponentType = GetArchetypeChunkComponentType<MyComponent>();
var myThesholdType = GetArchetypeChunkComponentType<MyThreshold>(true);
var chunks = query.CreateArchetypeChunkArray(Allocator.TempJob);
var checkAlignedJob = new CheckAllPairsJob()
{
Chunks = chunks,
MyComponentType = myComponentType,
MyThresholdType = myThesholdType,
TranslationType = translationType
};
this.Dependency = checkAlignedJob.Schedule(chunks.Length,32, this.Dependency);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment