/// <summary> /// set the buffer elements of the given aBuffer /// </summary> /// <param name="entityManager"></param> /// <param name="entity"></param> /// <param name="bBuffer"></param> public static void SetBuffer(EntityManager entityManager, Entity entity, DynamicBuffer <EntityPrefabBufferElement> bBuffer) { DynamicBuffer <EntityPrefabBufferElement> aBuffer = GetBuffer(entityManager, entity); aBuffer.Clear(); aBuffer.CopyFrom(bBuffer); }
/// <summary> /// set the buffer elements of the given aBuffer /// </summary> /// <param name="entityManager"></param> /// <param name="entity"></param> /// <param name="bBuffer"></param> public static void SetBuffer(EntityManager entityManager, Entity entity, DynamicBuffer <ProceduralGenerationSpawnDataBufferElement> bBuffer) { DynamicBuffer <ProceduralGenerationSpawnDataBufferElement> aBuffer = GetBuffer(entityManager, entity); aBuffer.Clear(); aBuffer.CopyFrom(bBuffer); }
protected override JobHandle OnUpdate(JobHandle dependency) { var unitPositions = m_UnitQuery.ToComponentDataArray <Translation>(Allocator.TempJob); var workerPositions = m_WorkerQuery.ToComponentDataArray <Translation>(Allocator.TempJob); var selectedPositions = m_SelectedQuery.ToComponentDataArray <Translation>(Allocator.TempJob); Entity entity = m_MinimapQuery.GetSingletonEntity(); DynamicBuffer <RenderTexture> buffer = EntityManager.GetBuffer <RenderTexture>(entity); NativeArray <float4> colorArray = new NativeArray <float4>(width * height, Allocator.TempJob); dependency = Job.WithCode(() => { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { colorArray[x + (y * width)] = new float4(143.0f / 255.0f, 113.0f / 255.0f, 92.0f / 255.0f, 1.0f); } } }).Schedule(dependency); dependency = Schedule(new float4(0, 0, 1, 1), unitPositions, colorArray, dependency); dependency = Schedule(new float4(1, 1, 0, 1), workerPositions, colorArray, dependency); dependency = Schedule(new float4(0, 1, 0, 1), selectedPositions, colorArray, dependency); NativeArray <int2> outVect = new NativeArray <int2>(4, Allocator.TempJob); outVect[0] = new int2(0, 0); outVect[1] = new int2(0, height - 1); outVect[2] = new int2(width - 1, height - 1); outVect[3] = new int2(width - 1, 0); MiniMapHelpers.ConstructCameraCoordonates(outVect, width, height); dependency = Job.WithCode(() => { for (int i = 0; i < 4; i++) { int idx = (i + 1) % 4; MiniMapHelpers.DrawLine(colorArray, width, height, outVect[i][0], outVect[i][1], outVect[idx][0], outVect[idx][1], new float4(1, 1, 1, 1)); } }).Schedule(dependency); dependency.Complete(); dependency = outVect.Dispose(dependency); buffer.CopyFrom(colorArray.Reinterpret <RenderTexture>()); dependency = colorArray.Dispose(dependency); dependency = unitPositions.Dispose(dependency); dependency = workerPositions.Dispose(dependency); dependency = selectedPositions.Dispose(dependency); return(dependency); }
public void AddBufferOverflow() { var cmds = new EntityCommandBuffer(Allocator.TempJob); var e = cmds.CreateEntity(); DynamicBuffer <EcsIntElement> buffer = cmds.AddBuffer <EcsIntElement>(e); buffer.CopyFrom(new EcsIntElement[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); cmds.Playback(m_Manager); VerifySingleBuffer(10); cmds.Dispose(); }
public void SetBufferExplicit() { var e = m_Manager.CreateEntity(typeof(EcsIntElement)); var cmds = new EntityCommandBuffer(Allocator.TempJob); DynamicBuffer <EcsIntElement> buffer = cmds.SetBuffer <EcsIntElement>(e); buffer.CopyFrom(new EcsIntElement[] { 1, 2, 3 }); cmds.Playback(m_Manager); VerifySingleBuffer(3); cmds.Dispose(); }
public void AddBufferImplicitNoOverflow() { var cmds = new EntityCommandBuffer(Allocator.TempJob); cmds.CreateEntity(); DynamicBuffer <EcsIntElement> buffer = cmds.AddBuffer <EcsIntElement>(); buffer.CopyFrom(new EcsIntElement[] { 1, 2, 3 }); cmds.Playback(m_Manager); VerifySingleBuffer(3); cmds.Dispose(); }
public void Execute() { // add up all the blockFaces counts, for a total sector faces count SectorVisFacesCount sectorFacesCount; NativeArray <BlockFaces> facesCount = CheckBlockFaces(entity, out sectorFacesCount); ECBuffer.AddComponent(entity, sectorFacesCount); DynamicBuffer <BlockFaces> facesBuffer = ECBuffer.AddBuffer <BlockFaces>(entity); facesBuffer.CopyFrom(facesCount); ECBuffer.RemoveComponent(entity, typeof(DrawMeshTag)); facesCount.Dispose(); }
public void PlaybackInvalidatesBuffers() { var cmds = new EntityCommandBuffer(Allocator.TempJob); var e = cmds.CreateEntity(); DynamicBuffer <EcsIntElement> buffer = cmds.AddBuffer <EcsIntElement>(e); buffer.CopyFrom(new EcsIntElement[] { 1, 2, 3 }); cmds.Playback(m_Manager); // Should not be possible to access the temporary buffer after playback. Assert.Throws <InvalidOperationException>(() => { buffer.Add(1); }); cmds.Dispose(); }
public static unsafe void FromString(this DynamicBuffer <char> buffer, string name) { if (string.IsNullOrEmpty(name)) { buffer.Clear(); return; } fixed(char *ptr = name) { var array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <char>(ptr, name.Length, Allocator.None); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref array, AtomicSafetyHandle.Create()); #endif buffer.CopyFrom(array); } }
public void Execute() { for (int e = 0; e < Entities.Length; e++) { Entity sectorEntity = Entities[e]; DynamicBuffer <SurfaceCell> surfaceCellBuffer = SurfaceCellBufferFrom[sectorEntity]; for (int i = 0; i < surfaceCellBuffer.Length; i++) { SurfaceCell currentCell = surfaceCellBuffer[i]; if (!UniqueSurfaceCellsBufferContainsCurrentCell(UniqueCellList, currentCell)) { UniqueCellList.Add(currentCell); } } surfaceCellBuffer.CopyFrom(UniqueCellList); UniqueCellList.Clear(); EntitiesForTagRemoval.Enqueue(sectorEntity); } }
protected override void OnUpdate() { NativeArray <Entity> chunkManager = chunkManagerQuery.ToEntityArray(Allocator.TempJob); NativeArray <Entity> chunks = chunksQuery.ToEntityArray(Allocator.TempJob); Entity chunkManagerEntity = chunkManager[0]; chunkManager.Dispose(); if (World.Active.EntityManager.GetComponentData <ChunkManagerInfo>(chunkManagerEntity).status == 0) { if (hasDispatched == false) { for (int i = 0; i < chunks.Length; i++) { if (World.Active.EntityManager.GetComponentData <ChunkInfo>(chunks[i]).chunkProgress == 0) { chunkID = i; break; } else { chunkID = -1; } } if (chunkID == -1) { chunks.Dispose(); return; } Entity chunk = chunks[chunkID]; chunks.Dispose(); ChunkInfo chunkInfo = World.Active.EntityManager.GetComponentData <ChunkInfo>(chunk); voxelDataGenerator.SetInt("SizeX", chunkInfo.size.x + 1); voxelDataGenerator.SetInt("SizeY", chunkInfo.size.y + 1); voxelDataGenerator.SetInt("SizeZ", chunkInfo.size.z + 1); voxelDataGenerator.SetInt("x", chunkInfo.pos.x); voxelDataGenerator.SetInt("y", chunkInfo.pos.y); voxelDataGenerator.SetInt("z", chunkInfo.pos.z); voxelDataGenerator.Dispatch(GenerateVoxelDataKernel, (int)math.ceil((chunkInfo.size.x + 1) / 1), (int)math.ceil((chunkInfo.size.y + 1) / 1), (int)math.ceil((chunkInfo.size.z + 1) / 1)); hasDispatched = true; request = AsyncGPUReadback.Request(voxelBuffer); } if (request.done) { if (request.hasError == true) { throw new System.NotImplementedException(); } hasDispatched = false; voxelData = request.GetData <float>(); DynamicBuffer <float> voxelDynamicBuffer = World.Active.EntityManager.GetBuffer <VoxelData>(chunkManagerEntity).Reinterpret <float>(); voxelDynamicBuffer.CopyFrom(voxelData); for (int i = 0; i < 17 * 17 * 17; i++) { Debug.Log(voxelData[i]); } //World.Active.EntityManager.SetComponentData<VoxelData>(chunkManagerEntity, voxelDynamicBuffer.Reinterpret<VoxelData>()); ChunkInfo chunkInfo = World.Active.EntityManager.GetComponentData <ChunkInfo>(chunks[chunkID]); chunkInfo.chunkProgress = 1; World.Active.EntityManager.SetComponentData(chunks[chunkID], chunkInfo); ChunkManagerInfo managerInfo = World.Active.EntityManager.GetComponentData <ChunkManagerInfo>(chunkManagerEntity); managerInfo.status = 1; World.Active.EntityManager.SetComponentData(chunkManagerEntity, managerInfo); } } //throw new System.NotImplementedException(); }
private void ShowAdd() { DynamicBuffer <int> buffer = new DynamicBuffer <int>(); DynamicBuffer <int> secondBuffer = new DynamicBuffer <int>(); #region dynamicbuffer.add buffer.Add(5); #endregion #region dynamicbuffer.addrange int[] source = { 1, 2, 3, 4, 5 }; NativeArray <int> newElements = new NativeArray <int>(source, Allocator.Persistent); buffer.AddRange(newElements); #endregion #region dynamicbuffer.asnativearray int[] intArray = { 1, 2, 3, 4, 5 }; NativeArray <int> .Copy(intArray, buffer.AsNativeArray()); #endregion #region dynamicbuffer.capacity #endregion #region dynamicbuffer.clear buffer.Clear(); #endregion #region dynamicbuffer.copyfrom.dynamicbuffer buffer.CopyFrom(secondBuffer); #endregion #region dynamicbuffer.copyfrom.nativearray int[] sourceArray = { 1, 2, 3, 4, 5 }; NativeArray <int> nativeArray = new NativeArray <int>(source, Allocator.Persistent); buffer.CopyFrom(nativeArray); #endregion #region dynamicbuffer.copyfrom.nativeslice NativeSlice <int> nativeSlice = new NativeSlice <int>(nativeArray, 1, 3); buffer.CopyFrom(nativeSlice); #endregion #region dynamicbuffer.copyfrom.array int[] integerArray = { 1, 2, 3, 4, 5 }; buffer.CopyFrom(integerArray); #endregion #region dynamicbuffer.getenumerator foreach (var element in buffer) { //Use element... } #endregion #region dynamicbuffer.getunsafeptr #endregion int insertionIndex = 2; #region dynamicbuffer.insert if (insertionIndex < buffer.Length) { buffer.Insert(insertionIndex, 6); } #endregion #region dynamicbuffer.iscreated #endregion #region dynamicbuffer.length for (int i = 0; i < buffer.Length; i++) { buffer[i] = i * i; } #endregion #region dynamicbuffer.removeat if (insertionIndex < buffer.Length) { buffer.RemoveAt(insertionIndex); } #endregion int start = 1; #region dynamicbuffer.removerange buffer.RemoveRange(start, 5); #endregion #region dynamicbuffer.reserve buffer.EnsureCapacity(buffer.Capacity + 10); #endregion #region dynamicbuffer.resizeuninitialized buffer.ResizeUninitialized(buffer.Length + 10); #endregion #region dynamicbuffer.indexoperator for (int i = 0; i < buffer.Length; i++) { buffer[i] = i * i; } #endregion #region dynamicbuffer.tonativearray NativeArray <int> copy = buffer.ToNativeArray(Allocator.Persistent); #endregion #region dynamicbuffer.trimexcess if (buffer.Capacity > buffer.Length) { buffer.TrimExcess(); } #endregion }
/// <summary> /// set the buffer elements of the given aBuffer /// </summary> /// <param name="ecb"></param> /// <param name="aBuffer"></param> /// <param name="bBuffer"></param> public static void SetBuffer(EntityCommandBuffer.ParallelWriter ecb, DynamicBuffer <ProceduralGenerationSpawnDataBufferElement> aBuffer, DynamicBuffer <ProceduralGenerationSpawnDataBufferElement> bBuffer) { aBuffer.Clear(); aBuffer.CopyFrom(bBuffer); }
public void GenerateLifeSeed() { var lifeStart = GeneratePatternStamps(NumberOfStartingSeeds, GridSize, new int2[][] { gliderTable, lightweightspaceShip, pentomino, acorn }); // Generate the entities in one batch int entityCount = GridSize.x * GridSize.y; using (var cells = new NativeArray <Entity>(entityCount, Allocator.Persistent)) { var worldUpdateDetails = new WorldUpdateDetails { WorldUpdateRate = this.WorldUpdateRate, ShouldLimitUpdates = this.ShouldLimitUpdates, lastUpdateTime = this.WorldUpdateRate }; // Kick off the loading/setup for the particle system for this world/grid var particleDetails = SetupParticleSystem(); var(shouldDieFunction, shouldComeToLifeFunction) = GameRules.GetRuleFunctions(RuleSet); var worldDetails = new WorldDetails() { DeadRenderer = DeadCellPrefab, AliveRenderer = AliveCellPrefab, updateDetails = worldUpdateDetails, particleDetails = particleDetails, shouldDie = shouldDieFunction, shouldComeToLifeDie = shouldComeToLifeFunction }; entityManager.CreateEntity(cellArcheType, cells); // Offset from any given entity to the surrounding enities int2[] offsetTable = { new int2(-1, -1), new int2(-0, -1), new int2(1, -1), new int2(-1, 0), /*new int2( -0, 0 ),*/ new int2(1,0), new int2(-1, 1), new int2(0, 1), new int2(1, 1), }; var renderableEntitys = new NativeArray <Entity>(entityCount, Allocator.Persistent); // Generate adjency information for each cell for (int x = 0; x < GridSize.x; ++x) { for (int y = 0; y < GridSize.y; ++y) { int2 location = new int2(x, y); EntityElement[] adjacency = new EntityElement[offsetTable.Length]; for (int i = 0; i < offsetTable.Length; ++i) { int2 entityLocation = location + offsetTable[i]; entityLocation = WrapLocation(entityLocation, GridSize); int idx = ConvertToEntityIndex(entityLocation, GridSize); adjacency[i] = cells[idx]; } int entityIdx = ConvertToEntityIndex(location, GridSize); // Populate the entity information - all cells start off 'dead' entityManager.SetComponentData(cells[entityIdx], new LifeCell { gridPosition = location }); entityManager.SetComponentData(cells[entityIdx], new Translation { Value = GetLocationAroundCentre(new float3(x, 0, y)) }); // Setup which system will perform the update if (SystemToUse == UpdateSystem.SingleThreaded) { entityManager.AddComponentData(cells[entityIdx], new SingleThreadUpdateTag()); } else { entityManager.AddComponentData(cells[entityIdx], new MultiThreadUpdateTag()); } // This instantiates a copy of the dead cell prefab and parents it to the cell we are processing var cellMesh = entityManager.Instantiate(DeadCellPrefab); entityManager.AddComponentData(cellMesh, new Parent { Value = cells[entityIdx] }); entityManager.AddComponentData(cellMesh, new LocalToParent()); // This lets us track which entity is our current renderable so we can swap it out later when we need // to update our state of being entityManager.SetComponentData(cells[entityIdx], new Renderable { value = cellMesh }); renderableEntitys[entityIdx] = cellMesh; // As we can't hold an array of Entity references in a component the adjancy information is stored in a // buffer attached to the entity and populated by copying from the details we generated DynamicBuffer <EntityElement> entityBuffer = entityManager.GetBuffer <EntityElement>(cells[entityIdx]); entityBuffer.CopyFrom(adjacency); // And finally associate some shared data so that we can swap the prefabs around later // and track the world update state entityManager.AddSharedComponentData(cells[entityIdx], worldDetails); } } SetupBoardCondition(cells, GridSize, lifeStart, renderableEntitys); renderableEntitys.Dispose(); // Finally we setup an entity which tracks if this instance of the world needs to be updated or not // It has tags for the threading of the update system so we can correctly dispatch later var worldUpdateTracker = entityManager.CreateEntity(); entityManager.AddComponentData(worldUpdateTracker, new WorldUpdateTracker()); if (SystemToUse == UpdateSystem.SingleThreaded) { entityManager.AddComponentData(worldUpdateTracker, new SingleThreadUpdateTag()); } else { entityManager.AddComponentData(worldUpdateTracker, new MultiThreadUpdateTag()); } entityManager.AddSharedComponentData(worldUpdateTracker, worldDetails); } }
public void GenerateLifeSeed(int2 gridSize) { var lifeStart = GeneratePatternStamps(NumberOfStartingSeeds, gridSize, new int2[][] { gliderTable, lightweightspaceShip, pentomino, acorn }); // Generate the entities in one batch int entityCount = gridSize.x * gridSize.y; var cells = new NativeArray <Entity>(entityCount, Allocator.Persistent); EntityManager.CreateEntity(defaultArcheType, cells); // Offset from any given entity to the surrounding enities int2[] offsetTable = new int2[] { new int2(-1, -1), new int2(-0, -1), new int2(1, -1), new int2(-1, 0), /*new int2( -0, 0 ),*/ new int2(1, 0), new int2(-1, 1), new int2(0, 1), new int2(1, 1), }; // Generate adjency information for each cell for (int x = 0; x < gridSize.x; ++x) { for (int y = 0; y < gridSize.y; ++y) { int2 location = new int2(x, y); EntityElement[] adjacency = new EntityElement[offsetTable.Length]; for (int i = 0; i < offsetTable.Length; ++i) { int2 entityLocation = location + offsetTable[i]; entityLocation = WrapLocation(entityLocation, gridSize); int idx = ConvertToEntityIndex(entityLocation, gridSize); adjacency[i] = cells[idx]; } int entityIdx = ConvertToEntityIndex(location, gridSize); // Populate the entity information - all cells start off 'dead' EntityManager.SetComponentData(cells[entityIdx], new LifeCell { gridPosition = location }); EntityManager.SetComponentData(cells[entityIdx], new Translation { Value = new float3(x, 0, y) }); EntityManager.SetSharedComponentData(cells[entityIdx], deadRenderMesh); // As we can't hold an array of Entity references in a component the adjancy information is stored in a // buffer attached to the entity and populated by copying from the details we generated DynamicBuffer <EntityElement> entityBuffer = EntityManager.GetBuffer <EntityElement>(cells[entityIdx]); entityBuffer.CopyFrom(adjacency); } } SetupBoardCondition(cells, gridSize, lifeStart); cells.Dispose(); }
/// <summary> /// set the buffer elements of the given aBuffer /// </summary> /// <param name="ecb"></param> /// <param name="aBuffer"></param> /// <param name="bBuffer"></param> public static void SetBuffer(EntityCommandBuffer.ParallelWriter ecb, DynamicBuffer <EntityPrefabBufferElement> aBuffer, DynamicBuffer <EntityPrefabBufferElement> bBuffer) { aBuffer.Clear(); aBuffer.CopyFrom(bBuffer); }
void AddPointArrayFromMatrix() { DynamicBuffer <WorleyNoise.PointData> worleyBuffer = commandBuffer.AddBuffer <WorleyNoise.PointData>(sectorEntity); worleyBuffer.CopyFrom(pointMatrix.matrix); }
/// <summary> /// set the buffer elements of the given aBuffer /// </summary> /// <param name="ecb"></param> /// <param name="aBuffer"></param> /// <param name="bBuffer"></param> public static void SetBuffer(EntityCommandBuffer ecb, DynamicBuffer <IntBufferElement> aBuffer, DynamicBuffer <IntBufferElement> bBuffer) { aBuffer.Clear(); aBuffer.CopyFrom(bBuffer); }