Skip to content

Commit

Permalink
【ECSLearn】 Boids 项目研究
Browse files Browse the repository at this point in the history
  • Loading branch information
hexinping committed Aug 17, 2021
1 parent 6703ffc commit b5476d9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Samples.Boids
{
[Serializable]
[WriteGroup(typeof(LocalToWorld))]
[WriteGroup(typeof(LocalToWorld))] //如果有个system关心了Boid组件,并且标记了writeGroupFilterOption,就会剔除LocalToWorld的Entity
public struct Boid : ISharedComponentData
{
public float CellRadius;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct BoidSchool : IComponentData
public partial class BoidSchoolSpawnSystem : SystemBase
{
[BurstCompile]
struct SetBoidLocalToWorld : IJobParallelFor
struct SetBoidLocalToWorld : IJobParallelFor //并行的job
{
[NativeDisableContainerSafetyRestriction]
[NativeDisableParallelForRestriction]
Expand All @@ -45,24 +45,35 @@ public void Execute(int i)

protected override void OnUpdate()
{

/*
*
* 当您调用Entities.ForEach.Run()时,作业调度程序会在开始ForEach迭代之前完成system所依赖的所有调度job。==> 先完成依赖项
* 如果您还使用WithStructuralChanges()作为构造的一部分,则job调度程序将完成所有正在运行和待调度的jobs。结构更改还会使对component数据的任何直接引用无效
*/
Entities.WithStructuralChanges().ForEach((Entity entity, int entityInQueryIndex, in BoidSchool boidSchool, in LocalToWorld boidSchoolLocalToWorld) =>
{
//分配一个NavtiveContainer,如果NavtiveContainer直接用来写的话,可以用NativeArrayOptions.UninitializedMemory,会提升效率
var boidEntities = new NativeArray<Entity>(boidSchool.Count, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

Profiler.BeginSample("Instantiate");
EntityManager.Instantiate(boidSchool.Prefab, boidEntities);
Profiler.EndSample();



//All component data of type T.==> 返回的是所有Entity的LocalToWorld组件,取到对应Entity上Component需要 localToWorldFromEntity[Entity]
var localToWorldFromEntity = GetComponentDataFromEntity<LocalToWorld>();
//开始创建Job,然后调度
var setBoidLocalToWorldJob = new SetBoidLocalToWorld
{
LocalToWorldFromEntity = localToWorldFromEntity,
Entities = boidEntities,
Center = boidSchoolLocalToWorld.Position,
Radius = boidSchool.InitialRadius
Entities = boidEntities, //NativeContainer
Center = boidSchoolLocalToWorld.Position, //中心位置
Radius = boidSchool.InitialRadius
};

//这个参数不是很明白
//这个参数不是很明白 64 是使用64个核
Dependency = setBoidLocalToWorldJob.Schedule(boidSchool.Count, 64, Dependency);
Dependency = boidEntities.Dispose(Dependency);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ public void ExecuteNext(int cellIndex, int index)

protected override void OnUpdate()
{
//得到Entity的count
var obstacleCount = m_ObstacleQuery.CalculateEntityCount();
var targetCount = m_TargetQuery.CalculateEntityCount();


//获取所有shareComponentdata的数据 这里是Boid组件
EntityManager.GetAllUniqueSharedComponentData(m_UniqueTypes);

// Each variant of the Boid represents a different value of the SharedComponentData and is self-contained,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public partial class SampledAnimationClipPlaybackSystem : SystemBase
protected override void OnUpdate()
{
var deltaTime = math.min(0.05f, Time.DeltaTime);


//位置和旋转
Entities.ForEach((ref Translation translation, ref Rotation rotation, in SampledAnimationClip sampledAnimationClip) =>
{
var frameIndex = sampledAnimationClip.FrameIndex;
Expand All @@ -26,7 +27,8 @@ protected override void OnUpdate()
translation.Value = math.lerp(prevTranslation, nextTranslation, timeOffset);
rotation.Value = math.slerp(prevRotation, nextRotation, timeOffset);
}).ScheduleParallel();


//动画切换
Entities.ForEach((ref SampledAnimationClip sampledAnimationClip) =>
{
var currentTime = sampledAnimationClip.CurrentTime + deltaTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void Convert(Entity entity, EntityManager dstManager, GameObjectConversio
var lengthSeconds = animationClip.length;
var sampleRate = 1.0f / SamplesPerSecond;
var frameCount = (int)(lengthSeconds / sampleRate);
// Debug.Log($"TransformRecorderAuthoring {lengthSeconds} {frameCount}");
if (frameCount < 2) // Minimum two frames of animation to capture.
{
return;
Expand All @@ -38,14 +39,16 @@ public void Convert(Entity entity, EntityManager dstManager, GameObjectConversio
ref var transformSamplesBlob = ref blobBuilder.ConstructRoot<TransformSamples>();
var translationSamples = blobBuilder.Allocate(ref transformSamplesBlob.TranslationSamples, frameCount);
var rotationSamples = blobBuilder.Allocate(ref transformSamplesBlob.RotationSamples, frameCount);


//把动作每一帧的信息保存起来,位置和旋转
for (int i = 0; i < frameCount; i++)
{
animationClip.SampleAnimation(gameObject, s);

translationSamples[i] = gameObject.transform.position;
rotationSamples[i] = gameObject.transform.rotation;


//Debug.Log($"TransformRecorderAuthoring {i} {entity.Index}=={gameObject.transform.position} {gameObject.transform.rotation}");
s += sampleRate;
}

Expand Down

0 comments on commit b5476d9

Please sign in to comment.