protected override JobHandle OnUpdate(JobHandle inputDeps) { //Migrate material on LineStyle to RenderMesh by chunks using (var aca = newRegisterQuery.CreateArchetypeChunkArray(Allocator.TempJob)) { if (aca.Length > 0) { EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp); var lineStyleType = GetArchetypeChunkSharedComponentType <LineStyle>(); //TODO : This shouldn't be needed, but somehow the mesh became `null` in editor world?? CreateMeshIfNotYet(); for (int i = 0; i < aca.Length; i++) { ArchetypeChunk ac = aca[i]; var lineStyle = ac.GetSharedComponentData <LineStyle>(lineStyleType, EntityManager); //Filter to narrow down chunk operation. newRegisterQuery.SetSharedComponentFilter(lineStyle); ecb.AddSharedComponent(newRegisterQuery, new RenderMesh { mesh = lineMesh, material = lineStyle.material }); } ecb.Playback(EntityManager); newRegisterQuery.ResetFilter(); } } //Use EQ operation to prepare other components where they don't need initialization value. EntityManager.AddComponent(newRegisterQuery, ComponentType.ReadOnly <Translation>()); EntityManager.AddComponent(newRegisterQuery, ComponentType.ReadOnly <Rotation>()); EntityManager.AddComponent(newRegisterQuery, ComponentType.ReadOnly <NonUniformScale>()); //Unity stopped adding LTW for us without GO conversion. EntityManager.AddComponent(newRegisterQuery, ComponentType.ReadOnly <LocalToWorld>()); //This make them not registered again. EntityManager.AddComponent(newRegisterQuery, ComponentType.ReadOnly <RegisteredState>()); //This is for clean up of system state component in the case the entity was destroyed. EntityManager.RemoveComponent(cleanUpQuery, ComponentType.ReadOnly <RegisteredState>()); return(default);
public Runtime PrepareToExecuteOnEntitiesIn(ref ArchetypeChunk chunk) => new Runtime() { _data = chunk.GetSharedComponentData(_type, _entityManager) };
protected override void OnUpdate() { //While not attaching registered state yet, add render mesh to all new members. // using (var aca = toRegisterLinesQuery.CreateArchetypeChunkArray(Allocator.TempJob)) // { // if (aca.Length > 0) // { // NativeList<int> uniqueLineStyleIndexes = new NativeList<int>(Allocator.Temp); // var lineStyleType = GetArchetypeChunkSharedComponentType<LineStyle>(); // //TODO : This shouldn't be needed, but somehow the mesh became `null` in editor world?? // CreateMeshIfNotYet(); // for (int i = 0; i < aca.Length; i++) // { // ArchetypeChunk ac = aca[i]; // var index = ac.GetSharedComponentIndex(lineStyleType); // Debug.Log($"Shared index to filter : {index}"); // uniqueLineStyleIndexes.Add(index); // } // //Use filter to batch migrate the line style to render mesh. // for (int i = 0; i < uniqueLineStyleIndexes.Length; i++) // { // var ls = EntityManager.GetSharedComponentData<LineStyle>(uniqueLineStyleIndexes[i]); // toRegisterLinesQuery.SetFilter(ls); // EntityManager.AddSharedComponentData(toRegisterLinesQuery, new RenderMesh { mesh = lineMesh, material = ls.lineMaterial }); // } // toRegisterLinesQuery.ResetFilter(); // } // } using (var aca = toRegisterLinesQuery.CreateArchetypeChunkArray(Allocator.TempJob)) { if (aca.Length > 0) { EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp); var lineStyleType = GetArchetypeChunkSharedComponentType <LineStyle>(); var entityType = GetArchetypeChunkEntityType(); //TODO : This shouldn't be needed, but somehow the mesh became `null` in editor world?? CreateMeshIfNotYet(); for (int i = 0; i < aca.Length; i++) { ArchetypeChunk ac = aca[i]; var ea = ac.GetNativeArray(entityType); var lineStyle = ac.GetSharedComponentData <LineStyle>(lineStyleType, EntityManager); for (int j = 0; j < ea.Length; j++) { //Must use ECB or else it would invalidate the interating chunks, etc. ecb.AddSharedComponent(ea[j], new RenderMesh { mesh = lineMesh, material = lineStyle.material }); } } ecb.Playback(EntityManager); } } //Use EQ operation to prepare other components where they don't need initialization value. EntityManager.AddComponent(toRegisterLinesQuery, ComponentType.ReadOnly <Translation>()); EntityManager.AddComponent(toRegisterLinesQuery, ComponentType.ReadOnly <Rotation>()); EntityManager.AddComponent(toRegisterLinesQuery, ComponentType.ReadOnly <NonUniformScale>()); //Unity stopped adding LTW for us without GO conversion. EntityManager.AddComponent(toRegisterLinesQuery, ComponentType.ReadOnly <LocalToWorld>()); //This make them not registered again. EntityManager.AddComponent(toRegisterLinesQuery, ComponentType.ReadOnly <RegisteredState>()); //This is for clean up of system state component. EntityManager.RemoveComponent(toUnregisterLinesQuery, ComponentType.ReadOnly <RegisteredState>()); }