Hey all,
I’ve got this hierarchy system where entities reference a parent entity that has a Position component, and then set their own Position component based on that. But I’m having trouble turning the job into a parallel one.
using System.Collections;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
public class HierarchyPositionSystem : JobComponentSystem
{
struct HierarchyPositionGroup
{
[ReadOnly] public ComponentDataArray<TransformParent> parents;
[ReadOnly] public ComponentDataArray<LocalRotation> localRotations;
[ReadOnly] public ComponentDataArray<RotationSpring> springs;
[ReadOnly] public EntityArray entities;
public ComponentDataArray<Position> positions;
public ComponentDataArray<Rotation> rotations;
public ComponentDataArray<PreviousPosition> prevPositions;
public int Length;
}
[Inject] private HierarchyPositionGroup hierarchyDataGroup;
[Inject] ComponentDataFromEntity<Position> positionGroup;
[Inject] ComponentDataFromEntity<Rotation> rotationGroup;
[BurstCompile]
struct HierarchyPositionJob : IJobParallelFor
{
[ReadOnly] public ComponentDataFromEntity<Position> allPositions;
[ReadOnly] public ComponentDataFromEntity<Rotation> allRotations;
[ReadOnly] public ComponentDataArray<TransformParent> parents;
[ReadOnly] public ComponentDataArray<LocalRotation> localRotations;
[ReadOnly] public ComponentDataArray<RotationSpring> springs;
[ReadOnly] public EntityArray entities;
public ComponentDataArray<Position> positions;
public ComponentDataArray<Rotation> rotations;
public ComponentDataArray<PreviousPosition> prevPositions;
public void Execute(int i)
{
var childEntity = entities[i];
var parentEntity = parents[i].Value;
var parentPosition = allPositions[parentEntity].Value;
Quaternion parentRotation = allRotations[parentEntity].Value;
Quaternion childRotation = localRotations[i].Value;
Vector3 anchor = Vector3.up;
Vector3 offset = springs[i].position;
Quaternion offsetQ = Quaternion.FromToRotation(anchor, offset);
childRotation *= offsetQ;
childRotation *= parentRotation;
rotations[i] = new Rotation
{
Value = childRotation
};
float3 up = childRotation * Vector3.up;
prevPositions[i] = new PreviousPosition() { Value = positions[i].Value };
positions[i] = new Position(parentPosition + up);
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var job = new HierarchyPositionJob()
{
positions = hierarchyDataGroup.positions,
prevPositions = hierarchyDataGroup.prevPositions,
rotations = hierarchyDataGroup.rotations,
parents = hierarchyDataGroup.parents,
localRotations = hierarchyDataGroup.localRotations,
springs = hierarchyDataGroup.springs,
entities = hierarchyDataGroup.entities,
allPositions = positionGroup,
allRotations = rotationGroup
};
return job.Schedule(hierarchyDataGroup.Length,32, inputDeps);
}
}
I got the problem of not being able to write to ComponentDataArray, so I made those read only and figured I could have a separate array of positions related only to the entities in the job. But Unity complains that these two arrays are the same. As far as I know they should be different, right? The ComponentDataArray relates to the entities in this job, and the ComponentDataArrayFromEntity has all the entities in it…?