komodor
November 26, 2020, 1:53pm
1
I am looking for something like this
Entities.ForEach((Component1 c1, Translation t1) =>
{
Entities.ForEach((Component2 c2, Translation t2) =>
{
// create job to calculate c1/c2 stuff and apply result to translations
}).Schedule();
}).Schedule();
of course, i want it with burst but right now i am interested just in having this working with jobs to start somewhere
if anybody can help with some example or tutorial I’d be glad
Make sure it won’t approach O(n²) or will scale badly² otherwise.
public struct Component1 : IComponentData{ public int Value; }
public struct Component2 : IComponentData{ public int Value; }
public class MySystem : SystemBase
{
EndSimulationEntityCommandBufferSystem _endSimulationEcbSystem;
EntityQuery _query2;
protected override void OnCreate ()
{
_endSimulationEcbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
_query2 = EntityManager.CreateEntityQuery(
ComponentType.ReadWrite<Translation>()
, ComponentType.ReadOnly<Component2>()
);
}
protected override void OnUpdate ()
{
var command = _endSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();
var translationData = GetComponentDataFromEntity<Translation>( isReadOnly:true );
var component2Data = GetComponentDataFromEntity<Component2>( isReadOnly:true );
NativeArray<Entity> entities2 = _query2.ToEntityArray( Allocator.TempJob );
Entities
.WithName("my_job")
.WithReadOnly(translationData)
.WithReadOnly(component2Data)
.WithReadOnly(entities2).WithDisposeOnCompletion(entities2)
.ForEach( ( in int entityInQueryIndex , in Translation translation , in Component1 component1 , in Entity entity )=>
{
float3 newPos = translation.Value;
for( int i=0 ; i<entities2.Length ; i++ )
{
Entity otherEntity = entities2*;*
float3 otherPos = translationData[otherEntity].Value;*
Component2 component2 = component2Data[otherEntity];*
/* calculations */
}
command.SetComponent<Translation>( entityInQueryIndex , entity , new Translation{
Value = newPos
} );
} )
.WithBurst().ScheduleParallel();
_endSimulationEcbSystem.AddJobHandleForProducer( Dependency );
}
}