public void AddChunkComponentWithGroup() { Measure.Method(() => { m_Manager.AddChunkComponentData(group, new EcsTestData4(7)); }) .SetUp(CreateEntities) .CleanUp(DestroyEntities) .Run(); }
protected override void OnUpdate() { var ecb = _endSimulationEcbSystem.CreateCommandBuffer(); var ecb_pw = ecb.AsParallelWriter(); EntityManager.AddChunkComponentData <ChunkWorldRenderBounds>( _query_noCWRB, default(ChunkWorldRenderBounds) ); if (LineRendererWorld.IsCreated && this.World == LineRendererWorld.GetWorld()) { var boundsJob = new BoundsJob { rendererBounds = GetComponentTypeHandle <RenderBounds>(true), localToWorld = GetComponentTypeHandle <LocalToWorld>(true), worldRenderBounds = GetComponentTypeHandle <WorldRenderBounds>(), chunkWorldRenderBounds = GetComponentTypeHandle <ChunkWorldRenderBounds>(), }; Dependency = boundsJob.ScheduleParallel(_query, Dependency); } Entities .WithName($"add_missing_{nameof(WorldRenderBounds)}_components_job") .WithAll <Segment>() .WithNone <WorldRenderBounds>() .ForEach((in int entityInQueryIndex, in Entity entity) => { ecb_pw.AddComponent <WorldRenderBounds>(entityInQueryIndex, entity); })
internal static void CreateEntities() { // we need entity manager in order to create entities EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager; // create an entity archetype (a blueprint), that will later be used in instantiation and querying EntityArchetype entityArchetype = entityManager.CreateArchetype( typeof(BlockTypesComponent), typeof(CoordinatesComponent)); for (int x = 0; x < GlobalVariables.Settings.WorldSizeX * Constants.CHUNK_SIZE; x++) { for (int y = 0; y < Constants.WORLD_SIZE_Y * Constants.CHUNK_SIZE; y++) { for (int z = 0; z < GlobalVariables.Settings.WorldSizeZ * Constants.CHUNK_SIZE; z++) { Entity entity = entityManager.CreateEntity(entityArchetype); entityManager.SetComponentData(entity, new CoordinatesComponent(new int3(x, y, z))); } } } // add chunk component EntityQuery query = entityManager.CreateEntityQuery(typeof(BlockTypesComponent), typeof(CoordinatesComponent)); entityManager.AddChunkComponentData(query, new SeedComponent(Seed)); }
protected override void OnUpdate() { // This is a structural change and a sync point EntityManager.AddChunkComponentData <ChunkAABB>( queryWithoutChunkComponent, new ChunkAABB() ); }
protected override void OnUpdate() { if (matrixChangedChunkQuery == default(EntityQuery)) { matrixChangedChunkQuery = GetEntityQuery(ComponentType.ChunkComponent <SpriteMatrixChangeTag>()); } EntityManager.RemoveChunkComponentData <SpriteMatrixChangeTag>(matrixChangedChunkQuery); for (int sheet = 0; sheet < spriteRenderingSystem.Baked; sheet++) { var filter = spriteRenderingSystem.filters[sheet]; query.SetSharedComponentFilter(filter); if (query.CalculateEntityCount() == 0) { continue; } spriteRenderingSystem.BufferStructureChanged[sheet] = true; var toBeBuffered = query.ToEntityArray(Allocator.TempJob); if (Logging) { UnityEngine.Debug.Log(string.Format("ToBeBuffered: {0}.", toBeBuffered.Length)); } BakedSpriteSheet bss = spriteRenderingSystem.bakedSpriteSheets[sheet]; int pooledIndexs = spriteRenderingSystem.freeIndexs[sheet].Length; uint currentCap = bss.Capacity; uint usedIndexRight = bss.Users; if (toBeBuffered.Length <= pooledIndexs) { for (int i = 0; i < toBeBuffered.Length; i++) { SpriteRenderSubject srs = EntityManager.GetComponentData <SpriteRenderSubject>(toBeBuffered[i]); srs.bufferIndex = spriteRenderingSystem.freeIndexs[sheet][pooledIndexs - 1]; if (Logging) { UnityEngine.Debug.Log(string.Format("Entitiy {1} is taking pooled index {0}. (pool enough)", srs.bufferIndex, toBeBuffered[i].Index)); } spriteRenderingSystem.freeIndexs[sheet].Resize(spriteRenderingSystem.freeIndexs[sheet].Length - 1, NativeArrayOptions.ClearMemory); EntityManager.SetComponentData(toBeBuffered[i], srs); EntityManager.AddComponentData(toBeBuffered[i], new BufferedRenderSubjectTag()); pooledIndexs--; } if (Logging) { UnityEngine.Debug.Log(string.Format("Reused {0} indexs.", toBeBuffered.Length)); } } else { int needIndexs = toBeBuffered.Length - pooledIndexs; if (needIndexs > currentCap - usedIndexRight) // Need to grow { uint properCap = currentCap; while (properCap <= currentCap + needIndexs) { properCap *= 2; } spriteRenderingSystem.GrowBuffersForNewUser(sheet, (int)properCap); if (Logging) { UnityEngine.Debug.Log(string.Format("Extended buffers' length of baked sprite sheet#{0} to {1}.", sheet, properCap)); } } uint prevIndexRight = usedIndexRight; if (Logging) { UnityEngine.Debug.Log(string.Format("Reusing {0}/{1}.", pooledIndexs, toBeBuffered.Length)); } for (int i = 0; i < toBeBuffered.Length; i++) { SpriteRenderSubject srs = EntityManager.GetComponentData <SpriteRenderSubject>(toBeBuffered[i]); if (pooledIndexs > 0) { srs.bufferIndex = spriteRenderingSystem.freeIndexs[sheet][pooledIndexs - 1]; spriteRenderingSystem.freeIndexs[sheet].Resize(spriteRenderingSystem.freeIndexs[sheet].Length - 1, NativeArrayOptions.ClearMemory); if (Logging) { UnityEngine.Debug.Log(string.Format("Entitiy {1} is taking pooled index {0}. (pool inenough)", srs.bufferIndex, toBeBuffered[i].Index)); } pooledIndexs--; } else { srs.bufferIndex = (int)usedIndexRight; if (Logging) { UnityEngine.Debug.Log(string.Format("Entitiy {1} is taking new index {0}.", srs.bufferIndex, toBeBuffered[i].Index)); } usedIndexRight++; } EntityManager.SetComponentData(toBeBuffered[i], srs); EntityManager.AddComponentData(toBeBuffered[i], new BufferedRenderSubjectTag()); EntityManager.AddChunkComponentData <SpriteMatrixChangeTag>(toBeBuffered[i]); } if (Logging) { UnityEngine.Debug.Log(string.Format("UsedIndex: {0} -> {1}.", prevIndexRight, usedIndexRight)); } bss.Users = usedIndexRight; } if (Logging) { UnityEngine.Debug.Log(string.Format("Added {0} users to the buffer of sprite sheet#{1}.", toBeBuffered.Length, sheet)); } toBeBuffered.Dispose(); if (Logging) { UnityEngine.Debug.Log(string.Format("MatrixChangedChunks: " + matrixChangedChunkQuery.CalculateChunkCount())); } } }
private void SetupPass() { EntityManager.AddComponent <SystemEntityVersion>(_setupEntityVersionQuery); EntityManager.AddChunkComponentData <SystemVersion>(_setupVersionQuery, default); }
private void snippets() { #region component-list-chunk-component ComponentType[] compTypes = { ComponentType.ChunkComponent <ChunkComponentA>(), ComponentType.ReadOnly <GeneralPurposeComponentA>() }; Entity entity = EntityManager.CreateEntity(compTypes); #endregion #region em-snippet EntityManager.AddChunkComponentData <ChunkComponentA>(entity); #endregion #region desc-chunk-component EntityQueryDesc ChunksWithoutComponentADesc = new EntityQueryDesc() { None = new ComponentType[] { ComponentType.ChunkComponent <ChunkComponentA>() } }; EntityQuery ChunksWithoutChunkComponentA = GetEntityQuery(ChunksWithoutComponentADesc); EntityManager.AddChunkComponentData <ChunkComponentA>(ChunksWithoutChunkComponentA, new ChunkComponentA() { Value = 4 }); #endregion #region use-chunk-component EntityQueryDesc ChunksWithChunkComponentADesc = new EntityQueryDesc() { All = new ComponentType[] { ComponentType.ChunkComponent <ChunkComponentA>() } }; #endregion #region archetype-chunk-component EntityArchetype ArchetypeWithChunkComponent = EntityManager.CreateArchetype( ComponentType.ChunkComponent(typeof(ChunkComponentA)), ComponentType.ReadWrite <GeneralPurposeComponentA>()); Entity newEntity = EntityManager.CreateEntity(ArchetypeWithChunkComponent); #endregion { EntityQuery ChunksWithChunkComponentA = null; #region read-chunk-component NativeArray <ArchetypeChunk> chunks = ChunksWithChunkComponentA.CreateArchetypeChunkArray(Allocator.TempJob); foreach (var chunk in chunks) { var compValue = EntityManager.GetChunkComponentData <ChunkComponentA>(chunk); //.. } chunks.Dispose(); #endregion } #region read-entity-chunk-component if (EntityManager.HasChunkComponent <ChunkComponentA>(entity)) { ChunkComponentA chunkComponentValue = EntityManager.GetChunkComponentData <ChunkComponentA>(entity); } #endregion { ArchetypeChunk chunk; #region set-chunk-component EntityManager.SetChunkComponentData <ChunkComponentA>(chunk, new ChunkComponentA() { Value = 7 }); #endregion } #region set-entity-chunk-component var entityChunk = EntityManager.GetChunk(entity); EntityManager.SetChunkComponentData <ChunkComponentA>(entityChunk, new ChunkComponentA() { Value = 8 }); #endregion }
protected override void OnUpdate() { srs.MatrixUpdateThisFrame = false; if (fastQuery_RotationChange == default(EntityQuery)) { EntityQueryDesc desc = new EntityQueryDesc { All = new ComponentType[] { ComponentType.ReadOnly <Rotation2D>(), ComponentType.ReadOnly <SpriteMatrix>(), ComponentType.ReadOnly <BufferedRenderSubjectTag>(), }, None = new ComponentType[] { ComponentType.ChunkComponent <SpriteMatrixChangeTag>() } }; fastQuery_RotationChange = GetEntityQuery(desc); fastQuery_RotationChange.AddChangedVersionFilter(typeof(Rotation2D)); } if (fastQuery_TranslationChange == default(EntityQuery)) { if (UseLocalToWorldInsteadOfTranslation) { EntityQueryDesc desc = new EntityQueryDesc { All = new ComponentType[] { ComponentType.ReadOnly <LocalToWorld>(), ComponentType.ReadOnly <SpriteMatrix>(), ComponentType.ReadOnly <BufferedRenderSubjectTag>(), }, None = new ComponentType[] { ComponentType.ChunkComponent <SpriteMatrixChangeTag>() }, }; fastQuery_TranslationChange = GetEntityQuery(desc); fastQuery_TranslationChange.AddChangedVersionFilter(typeof(LocalToWorld)); } else { EntityQueryDesc desc = new EntityQueryDesc { All = new ComponentType[] { ComponentType.ReadOnly <Translation>(), ComponentType.ReadOnly <SpriteMatrix>(), ComponentType.ReadOnly <BufferedRenderSubjectTag>(), }, None = new ComponentType[] { ComponentType.ChunkComponent <SpriteMatrixChangeTag>() }, }; fastQuery_TranslationChange = GetEntityQuery(desc); fastQuery_TranslationChange.AddChangedVersionFilter(typeof(Translation)); } } if (fastQuery_ScaleChange == default(EntityQuery)) { EntityQueryDesc desc = new EntityQueryDesc { All = new ComponentType[] { ComponentType.ReadOnly <Scale>(), ComponentType.ReadOnly <SpriteMatrix>(), ComponentType.ReadOnly <BufferedRenderSubjectTag>(), }, None = new ComponentType[] { ComponentType.ChunkComponent <SpriteMatrixChangeTag>() } }; fastQuery_ScaleChange = GetEntityQuery(desc); fastQuery_ScaleChange.AddChangedVersionFilter(typeof(Scale)); } if (fastQuery_RotationChange.CalculateChunkCount() > 0) { EntityManager.AddChunkComponentData(fastQuery_RotationChange, new SpriteMatrixChangeTag()); } if (fastQuery_TranslationChange.CalculateChunkCount() > 0) { EntityManager.AddChunkComponentData(fastQuery_TranslationChange, new SpriteMatrixChangeTag()); } if (fastQuery_ScaleChange.CalculateChunkCount() > 0) { EntityManager.AddChunkComponentData(fastQuery_ScaleChange, new SpriteMatrixChangeTag()); } ; UpdateMatrixJob udpateJob = new UpdateMatrixJob { TranslationComponent_Type = GetArchetypeChunkComponentType <Translation>(true), Rotation_Type = GetArchetypeChunkComponentType <Rotation2D>(true), Scale_Type = GetArchetypeChunkComponentType <Scale>(true), Matrix_Type = GetArchetypeChunkComponentType <SpriteMatrix>(), LastSystemVersion = this.LastSystemVersion, }; srs.MatrixUpdateHandle = udpateJob.ScheduleParallel(query); srs.MatrixUpdateThisFrame = true; }
private void UpdateMeshes() { EntityManager.AddChunkComponentData(_spriteMissingMesh, new ChunkSpriteMeshComponent { ChunkMeshId = -1 }); using (var chunks = _spriteEntities.CreateArchetypeChunkArray(Allocator.TempJob)) { var materialType = GetArchetypeChunkSharedComponentType <SharedSpriteMaterialComponent>(); var chunkSpriteMeshType = GetArchetypeChunkComponentType <ChunkSpriteMeshComponent>(); var spriteComponentType = GetArchetypeChunkComponentType <SpriteComponent>(true); var localToWorldType = GetArchetypeChunkComponentType <LocalToWorld>(true); for (int chunkIdx = 0; chunkIdx < chunks.Length; chunkIdx++) { var chunk = chunks[chunkIdx]; var spriteMaterial = chunk.GetSharedComponentData(materialType, EntityManager); // find mesh ChunkMeshData meshData; { var chunkSpriteMesh = chunk.GetChunkComponentData(chunkSpriteMeshType); if (chunkSpriteMesh.ChunkMeshId <= 0) { chunkSpriteMesh.ChunkMeshId = _nextMeshId++; chunk.SetChunkComponentData(chunkSpriteMeshType, chunkSpriteMesh); meshData = new ChunkMeshData { Material = chunk.GetSharedComponentData(materialType, EntityManager), Mesh = new Mesh(), MaterialVersion = LastSystemVersion, }; } else { meshData = _meshById[chunkSpriteMesh.ChunkMeshId]; bool hasMaterialChanged = chunk.DidChange(materialType, meshData.MaterialVersion) && !spriteMaterial.Equals(meshData.Material); if (!chunk.DidChange(localToWorldType, meshData.MeshVersion) && !chunk.DidChange(spriteComponentType, meshData.MeshVersion) && !hasMaterialChanged) { continue; } if (hasMaterialChanged) { meshData.Material = spriteMaterial; meshData.MaterialVersion = LastSystemVersion; } } meshData.MeshVersion = LastSystemVersion; _meshById[chunkSpriteMesh.ChunkMeshId] = meshData; } // generate mesh int numVertices = 4 * chunk.Count; var vertices = new NativeArray <float3>(numVertices, Allocator.Temp); var triangles = new NativeArray <int>(2 * 3 * chunk.Count, Allocator.Temp); var uvs = new NativeArray <float2>(numVertices, Allocator.Temp); var colors = new NativeArray <float4>(numVertices, Allocator.Temp); var localToWorld = chunk.GetNativeArray(localToWorldType); var sprites = chunk.GetNativeArray(spriteComponentType); for (int i = 0; i < chunk.Count; i++) { float4 size = new float4(-sprites[i].HalfSize, sprites[i].HalfSize); vertices[4 * i + 0] = math.mul(localToWorld[i].Value, new float4(size.xy, 0, 1)).xyz; vertices[4 * i + 1] = math.mul(localToWorld[i].Value, new float4(size.zy, 0, 1)).xyz; vertices[4 * i + 2] = math.mul(localToWorld[i].Value, new float4(size.zw, 0, 1)).xyz; vertices[4 * i + 3] = math.mul(localToWorld[i].Value, new float4(size.xw, 0, 1)).xyz; } for (int i = 0; i < chunk.Count; i++) { triangles[6 * i + 0] = 4 * i + 0; triangles[6 * i + 1] = 4 * i + 2; triangles[6 * i + 2] = 4 * i + 1; triangles[6 * i + 3] = 4 * i + 0; triangles[6 * i + 4] = 4 * i + 3; triangles[6 * i + 5] = 4 * i + 2; } for (int i = 0; i < chunk.Count; i++) { uvs[4 * i + 0] = sprites[i].MinUV; uvs[4 * i + 1] = new float2(sprites[i].MaxUV.x, sprites[i].MinUV.y); uvs[4 * i + 2] = sprites[i].MaxUV; uvs[4 * i + 3] = new float2(sprites[i].MinUV.x, sprites[i].MaxUV.y); colors[4 * i + 0] = sprites[i].Color; colors[4 * i + 1] = sprites[i].Color; colors[4 * i + 2] = sprites[i].Color; colors[4 * i + 3] = sprites[i].Color; } Mesh mesh = meshData.Mesh; mesh.Clear(); mesh.SetVertices(vertices); mesh.SetIndices(triangles, MeshTopology.Triangles, 0); mesh.SetUVs(0, uvs); mesh.SetColors(colors); vertices.Dispose(); triangles.Dispose(); uvs.Dispose(); colors.Dispose(); } } }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { dstManager.AddChunkComponentData <ChunkComponent>(entity); }
bool ISimWorldWriteAccessor.AddChunkComponentData <T>(Entity entity) => EntityManager.AddChunkComponentData <T>(entity);
void ISimWorldWriteAccessor.AddChunkComponentData <T>(EntityQuery entityQuery, T componentData) => EntityManager.AddChunkComponentData <T>(entityQuery, componentData);