protected JobHandle Setup(JobHandle inputDeps) { CleanUp(); _sizeCopy = new NativeArray <Size>(_mSpatialData.Length, Allocator.TempJob); _positionsCopy = new NativeArray <Position>(_mSpatialData.Length, Allocator.TempJob); _entitiesCopy = new NativeArray <Entity>(_mSpatialData.Length, Allocator.TempJob); var copySizesJob = new CopyComponentData <Size> { Source = _mSpatialData.Size, Results = _sizeCopy }; var copyPositionsJob = new CopyComponentData <Position> { Source = _mSpatialData.Position, Results = _positionsCopy }; var copyEntitiesJob = new CopyEntities { Source = _mSpatialData.Entities, Results = _entitiesCopy }; var copySizesJobHandle = copySizesJob.Schedule(_mSpatialData.Length, 64, inputDeps); var copyPositionsJobHandle = copyPositionsJob.Schedule(_mSpatialData.Length, 64, inputDeps); var copyEntitiesJobHandle = copyEntitiesJob.Schedule(_mSpatialData.Length, 64, inputDeps); return(JobHandle.CombineDependencies(copyPositionsJobHandle, copySizesJobHandle, copyEntitiesJobHandle)); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { this.DisposeBuffers(); var handle = inputDeps; var playerGroupLength = this._playerGroup.CalculateLength(); // Allocate Memory this._playerColliders = new NativeArray <SphereCollider>( playerGroupLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); // ComponentDataArray handle = new CopyComponentData <SphereCollider> { Source = this._playerGroup.GetComponentDataArray <SphereCollider>(), Results = this._playerColliders, }.Schedule(playerGroupLength, 32, handle);; // Check Hit handle = new CheckHitJob { PlayerColliders = this._playerColliders }.Schedule(this, handle); return(handle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { this.DisposeBuffers(); var handle = inputDeps; var playerGroupLength = this._playerGroup.CalculateLength(); // --------------------- // Allocate Memory this._playerColliders = new NativeArray <SphereCollider>( playerGroupLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); this._playerHashmap = new NativeMultiHashMap <int, int>( playerGroupLength * MaxGridNum, Allocator.TempJob); // --------------------- // ComponentDataArray var copyPlayerColliderJobHandle = new CopyComponentData <SphereCollider> { Source = this._playerGroup.GetComponentDataArray <SphereCollider>(), Results = this._playerColliders, }.Schedule(playerGroupLength, 32, handle); // Hashmap Settings var playerHashmapJobHandle = new HashPositions { CellRadius = CellRadius, Offsets = this._offsets, SphereColliders = this._playerGroup.GetComponentDataArray <SphereCollider>(), Hashmap = this._playerHashmap.ToConcurrent(), }.Schedule(playerGroupLength, 32, handle); // ※Jobの依存関係の結合 var handles = new NativeArray <JobHandle>(2, Allocator.Temp); handles[0] = copyPlayerColliderJobHandle; handles[1] = playerHashmapJobHandle; handle = JobHandle.CombineDependencies(handles); handles.Dispose(); // --------------------- // Check Hit handle = new CheckHitJob { CellRadius = CellRadius, PlayerColliders = this._playerColliders, PlayerHashmap = this._playerHashmap, }.Schedule(this, handle); return(handle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { dispose_resources(); handle_ = inputDeps; player_colliders_ = new NativeArray <SphereCollider>(player_group_.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); enemy_colliders_ = new NativeArray <SphereCollider>(enemy_group_.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); player_bullet_colliders_ = new NativeArray <SphereCollider>(player_bullet_group_.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); enemy_bullet_colliders_ = new NativeArray <SphereCollider>(enemy_bullet_group_.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); player_hashmap_ = new NativeMultiHashMap <int, int>(player_group_.Length * 27 /* expanded */, Allocator.TempJob); enemy_hashmap_ = new NativeMultiHashMap <int, int>(enemy_group_.Length * 27 /* expanded */, Allocator.TempJob); player_bullet_hashmap_ = new NativeMultiHashMap <int, int>(player_bullet_group_.Length * 27 /* expand */, Allocator.TempJob); enemy_bullet_hashmap_ = new NativeMultiHashMap <int, int>(enemy_bullet_group_.Length * 27 /* expand */, Allocator.TempJob); var initial_player_collider_job = new CopyComponentData <SphereCollider> { Source = player_group_.sphere_collider_list_, Results = player_colliders_, }; var initial_player_collider_handle_ = initial_player_collider_job.Schedule(player_group_.Length, 32, handle_); var initial_enemy_collider_job = new CopyComponentData <SphereCollider> { Source = enemy_group_.sphere_collider_list_, Results = enemy_colliders_, }; var initial_enemy_collider_handle_ = initial_enemy_collider_job.Schedule(enemy_group_.Length, 32, handle_); var initial_player_bullet_collider_job = new CopyComponentData <SphereCollider> { Source = player_bullet_group_.sphere_collider_list_, Results = player_bullet_colliders_, }; var initial_player_bullet_collider_handle_ = initial_player_bullet_collider_job.Schedule(player_bullet_group_.Length, 32, handle_); var initial_enemy_bullet_collider_job = new CopyComponentData <SphereCollider> { Source = enemy_bullet_group_.sphere_collider_list_, Results = enemy_bullet_colliders_, }; var initial_enemy_bullet_collider_handle_ = initial_enemy_bullet_collider_job.Schedule(enemy_bullet_group_.Length, 32, handle_); const float CELL_RADIUS = 8f; var initial_player_hashmap_job = new HashPositions { cell_radius_ = CELL_RADIUS, offsets_ = offsets_, sphere_collider_list_ = player_group_.sphere_collider_list_, hashmap_ = player_hashmap_, }; var initial_player_hashmap_handle_ = initial_player_hashmap_job.Schedule(player_group_.Length, 32, handle_); var initial_enemy_hashmap_job = new HashPositions { cell_radius_ = CELL_RADIUS, offsets_ = offsets_, sphere_collider_list_ = enemy_group_.sphere_collider_list_, hashmap_ = enemy_hashmap_, }; var initial_enemy_hashmap_handle_ = initial_enemy_hashmap_job.Schedule(enemy_group_.Length, 32, handle_); var initial_player_bullet_hashmap_job = new HashPositions { cell_radius_ = CELL_RADIUS, offsets_ = offsets_, sphere_collider_list_ = player_bullet_group_.sphere_collider_list_, hashmap_ = player_bullet_hashmap_, }; var initial_player_bullet_hashmap_handle_ = initial_player_bullet_hashmap_job.Schedule(player_bullet_group_.Length, 32, handle_); var initial_enemy_bullet_hashmap_job = new HashPositions { cell_radius_ = CELL_RADIUS, offsets_ = offsets_, sphere_collider_list_ = enemy_bullet_group_.sphere_collider_list_, hashmap_ = enemy_bullet_hashmap_, }; var initial_enemy_bullet_hashmap_handle_ = initial_enemy_bullet_hashmap_job.Schedule(enemy_bullet_group_.Length, 32, handle_); { var handle_s = new NativeArray <JobHandle>(8, Allocator.Temp); var idx = 0; handle_s[idx] = initial_player_collider_handle_; ++idx; handle_s[idx] = initial_enemy_collider_handle_; ++idx; handle_s[idx] = initial_player_bullet_collider_handle_; ++idx; handle_s[idx] = initial_enemy_bullet_collider_handle_; ++idx; handle_s[idx] = initial_player_hashmap_handle_; ++idx; handle_s[idx] = initial_enemy_hashmap_handle_; ++idx; handle_s[idx] = initial_player_bullet_hashmap_handle_; ++idx; handle_s[idx] = initial_enemy_bullet_hashmap_handle_; ++idx; handle_ = JobHandle.CombineDependencies(handle_s); handle_s.Dispose(); } { var job = new CheckEnemyToPlayer { cell_radius_ = CELL_RADIUS, enemy_colliders_ = enemy_colliders_, player_colliders_ = player_colliders_, player_hashmap_ = player_hashmap_, enemy_list_ = enemy_group_.enemy_list_, player_list_ = player_group_.player_list_, }; handle_ = job.Schedule(enemy_group_.Length, 32, handle_); } { var job = new CheckEnemyBulletToPlayer { time_ = Time.GetCurrent(), cell_radius_ = CELL_RADIUS, enemy_bullet_colliders_ = enemy_bullet_colliders_, player_colliders_ = player_colliders_, player_hashmap_ = player_hashmap_, player_list_ = player_group_.player_list_, destroyable_list_ = enemy_bullet_group_.destroyable_list_, enemy_bullet_collision_info_list_ = enemy_bullet_group_.collision_info_list_, }; handle_ = job.Schedule(enemy_bullet_group_.Length, 32, handle_); } { var job = new CheckPlayerBulletToEnemy { time_ = Time.GetCurrent(), cell_radius_ = CELL_RADIUS, player_bullet_colliders_ = player_bullet_colliders_, enemy_colliders_ = enemy_colliders_, enemy_hashmap_ = enemy_hashmap_, destroyable_list_ = player_bullet_group_.destroyable_list_, enemy_list_ = enemy_group_.enemy_list_, player_bullet_collision_info_list_ = player_bullet_group_.collision_info_list_, }; handle_ = job.Schedule(player_bullet_group_.Length, 32, handle_); } return(handle_); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { EntityManager.GetAllUniqueSharedComponentDatas(m_UniqueTypes); var obstaclePositions = m_ObstacleGroup.GetComponentDataArray <Position>(); var targetPositions = m_TargetGroup.GetComponentDataArray <Position>(); // Ingore typeIndex 0, can't use the default for anything meaningful. for (int typeIndex = 1; typeIndex < m_UniqueTypes.Count; typeIndex++) { var settings = m_UniqueTypes[typeIndex]; m_BoidGroup.SetFilter(settings); var positions = m_BoidGroup.GetComponentDataArray <Position>(); var headings = m_BoidGroup.GetComponentDataArray <Heading>(); var cacheIndex = typeIndex - 1; var boidCount = positions.Length; var cellIndices = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var hashMap = new NativeMultiHashMap <int, int>(boidCount, Allocator.TempJob); var copyTargetPositions = new NativeArray <Position>(targetPositions.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var copyObstaclePositions = new NativeArray <Position>(obstaclePositions.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellAlignment = new NativeArray <Heading>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellSeparation = new NativeArray <Position>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellObstacleDistance = new NativeArray <float>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellObstaclePositionIndex = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellTargetPositionIndex = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellCount = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var nextCells = new PrevCells { cellIndices = cellIndices, hashMap = hashMap, copyObstaclePositions = copyObstaclePositions, copyTargetPositions = copyTargetPositions, cellAlignment = cellAlignment, cellSeparation = cellSeparation, cellObstacleDistance = cellObstacleDistance, cellObstaclePositionIndex = cellObstaclePositionIndex, cellTargetPistionIndex = cellTargetPositionIndex, cellCount = cellCount }; if (cacheIndex > (m_PrevCells.Count - 1)) { m_PrevCells.Add(nextCells); } else { m_PrevCells[cacheIndex].hashMap.Dispose(); m_PrevCells[cacheIndex].cellIndices.Dispose(); m_PrevCells[cacheIndex].cellObstaclePositionIndex.Dispose(); m_PrevCells[cacheIndex].cellTargetPistionIndex.Dispose(); m_PrevCells[cacheIndex].copyTargetPositions.Dispose(); m_PrevCells[cacheIndex].copyObstaclePositions.Dispose(); m_PrevCells[cacheIndex].cellAlignment.Dispose(); m_PrevCells[cacheIndex].cellSeparation.Dispose(); m_PrevCells[cacheIndex].cellObstacleDistance.Dispose(); m_PrevCells[cacheIndex].cellCount.Dispose(); } m_PrevCells[cacheIndex] = nextCells; var hashPositionsJob = new HashPositions { positions = positions, hashMap = hashMap, cellRadius = settings.cellRadius }; var hashPositionsJobHandle = hashPositionsJob.Schedule(boidCount, 64, inputDeps); var initialCellAlignmentJob = new CopyComponentData <Heading> { Source = headings, Results = cellAlignment }; var initialCellAlignmentJobHandle = initialCellAlignmentJob.Schedule(boidCount, 64, inputDeps); var initialCellSeparationJob = new CopyComponentData <Position> { Source = positions, Results = cellSeparation }; var initialCellSeparationJobHandle = initialCellSeparationJob.Schedule(boidCount, 64, inputDeps); var initialCellCountJob = new MemsetNativeArray <int> { Source = cellCount, Value = 1 }; var initialCellCountJobHandle = initialCellCountJob.Schedule(boidCount, 64, inputDeps); var initialCellBarrierJobHandle = JobHandle.CombineDependencies(initialCellAlignmentJobHandle, initialCellSeparationJobHandle, initialCellCountJobHandle); var copyTargetPositionsJob = new CopyComponentData <Position> { Source = targetPositions, Results = copyTargetPositions }; var copyTargetPositionsJobHandle = copyTargetPositionsJob.Schedule(targetPositions.Length, 2, inputDeps); var copyObstaclePositionsJob = new CopyComponentData <Position> { Source = obstaclePositions, Results = copyObstaclePositions }; var copyObstaclePositionsJobHandle = copyObstaclePositionsJob.Schedule(obstaclePositions.Length, 2, inputDeps); var copyTargetObstacleBarrierJobHandle = JobHandle.CombineDependencies(copyTargetPositionsJobHandle, copyObstaclePositionsJobHandle); var mergeCellsBarrierJobHandle = JobHandle.CombineDependencies(hashPositionsJobHandle, initialCellBarrierJobHandle, copyTargetObstacleBarrierJobHandle); var mergeCellsJob = new MergeCells { cellIndices = cellIndices, cellAlignment = cellAlignment, cellSeparation = cellSeparation, cellObstacleDistance = cellObstacleDistance, cellObstaclePositionIndex = cellObstaclePositionIndex, cellTargetPistionIndex = cellTargetPositionIndex, cellCount = cellCount, targetPositions = copyTargetPositions, obstaclePositions = copyObstaclePositions }; var mergeCellsJobHandle = mergeCellsJob.Schedule(hashMap, 64, mergeCellsBarrierJobHandle); var steerJob = new Steer { cellIndices = nextCells.cellIndices, settings = settings, cellAlignment = cellAlignment, cellSeparation = cellSeparation, cellObstacleDistance = cellObstacleDistance, cellObstaclePositionIndex = cellObstaclePositionIndex, cellTargetPistionIndex = cellTargetPositionIndex, cellCount = cellCount, targetPositions = copyTargetPositions, obstaclePositions = copyObstaclePositions, dt = Time.deltaTime, positions = positions, headings = headings, }; var steerJobHandle = steerJob.Schedule(boidCount, 64, mergeCellsJobHandle); inputDeps = steerJobHandle; } m_UniqueTypes.Clear(); return(inputDeps); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { if (cameraTransform == null) { cameraTransform = GameObject.Find("Main Camera").transform; } EntityManager.GetAllUniqueSharedComponentData(uniqueTypes); ComponentDataArray <SPHCollider> colliders = SPHColliderGroup.GetComponentDataArray <SPHCollider>(); int colliderCount = colliders.Length; for (int typeIndex = 1; typeIndex < uniqueTypes.Count; typeIndex++) { // Get the current chunk setting SPHParticle settings = uniqueTypes[typeIndex]; SPHCharacterGroup.SetFilter(settings); // Cache the data ComponentDataArray <Position> positions = SPHCharacterGroup.GetComponentDataArray <Position>(); ComponentDataArray <SPHVelocity> velocities = SPHCharacterGroup.GetComponentDataArray <SPHVelocity>(); int cacheIndex = typeIndex - 1; int particleCount = positions.Length; NativeMultiHashMap <int, int> hashMap = new NativeMultiHashMap <int, int>(particleCount, Allocator.TempJob); NativeArray <Position> particlesPosition = new NativeArray <Position>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <SPHVelocity> particlesVelocity = new NativeArray <SPHVelocity>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <float3> particlesForces = new NativeArray <float3>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <float> particlesPressure = new NativeArray <float>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <float> particlesDensity = new NativeArray <float>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <int> particleIndices = new NativeArray <int>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <int> cellOffsetTableNative = new NativeArray <int>(cellOffsetTable, Allocator.TempJob); NativeArray <SPHCollider> copyColliders = new NativeArray <SPHCollider>(colliderCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); // Add new or dispose previous particle chunks PreviousParticle nextParticles = new PreviousParticle { hashMap = hashMap, particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, particlesForces = particlesForces, particlesPressure = particlesPressure, particlesDensity = particlesDensity, particleIndices = particleIndices, cellOffsetTable = cellOffsetTableNative, copyColliders = copyColliders }; if (cacheIndex > previousParticles.Count - 1) { previousParticles.Add(nextParticles); } else { previousParticles[cacheIndex].hashMap.Dispose(); previousParticles[cacheIndex].particlesPosition.Dispose(); previousParticles[cacheIndex].particlesVelocity.Dispose(); previousParticles[cacheIndex].particlesForces.Dispose(); previousParticles[cacheIndex].particlesPressure.Dispose(); previousParticles[cacheIndex].particlesDensity.Dispose(); previousParticles[cacheIndex].particleIndices.Dispose(); previousParticles[cacheIndex].cellOffsetTable.Dispose(); previousParticles[cacheIndex].copyColliders.Dispose(); } previousParticles[cacheIndex] = nextParticles; // Copy the component data to native arrays CopyComponentData <Position> particlesPositionJob = new CopyComponentData <Position> { Source = positions, Results = particlesPosition }; JobHandle particlesPositionJobHandle = particlesPositionJob.Schedule(particleCount, 64, inputDeps); CopyComponentData <SPHVelocity> particlesVelocityJob = new CopyComponentData <SPHVelocity> { Source = velocities, Results = particlesVelocity }; JobHandle particlesVelocityJobHandle = particlesVelocityJob.Schedule(particleCount, 64, inputDeps); CopyComponentData <SPHCollider> copyCollidersJob = new CopyComponentData <SPHCollider> { Source = colliders, Results = copyColliders }; JobHandle copyCollidersJobHandle = copyCollidersJob.Schedule(colliderCount, 64, inputDeps); MemsetNativeArray <float> particlesPressureJob = new MemsetNativeArray <float> { Source = particlesPressure, Value = 0.0f }; JobHandle particlesPressureJobHandle = particlesPressureJob.Schedule(particleCount, 64, inputDeps); MemsetNativeArray <float> particlesDensityJob = new MemsetNativeArray <float> { Source = particlesDensity, Value = 0.0f }; JobHandle particlesDensityJobHandle = particlesDensityJob.Schedule(particleCount, 64, inputDeps); MemsetNativeArray <int> particleIndicesJob = new MemsetNativeArray <int> { Source = particleIndices, Value = 0 }; JobHandle particleIndicesJobHandle = particleIndicesJob.Schedule(particleCount, 64, inputDeps); MemsetNativeArray <float3> particlesForcesJob = new MemsetNativeArray <float3> { Source = particlesForces, Value = new float3(0, 0, 0) }; JobHandle particlesForcesJobHandle = particlesForcesJob.Schedule(particleCount, 64, inputDeps); // Put positions into a hashMap HashPositions hashPositionsJob = new HashPositions { positions = particlesPosition, hashMap = hashMap.ToConcurrent(), cellRadius = settings.radius }; JobHandle hashPositionsJobHandle = hashPositionsJob.Schedule(particleCount, 64, particlesPositionJobHandle); JobHandle mergedPositionIndicesJobHandle = JobHandle.CombineDependencies(hashPositionsJobHandle, particleIndicesJobHandle); MergeParticles mergeParticlesJob = new MergeParticles { particleIndices = particleIndices }; JobHandle mergeParticlesJobHandle = mergeParticlesJob.Schedule(hashMap, 64, mergedPositionIndicesJobHandle); JobHandle mergedMergedParticlesDensityPressure = JobHandle.CombineDependencies(mergeParticlesJobHandle, particlesPressureJobHandle, particlesDensityJobHandle); // Compute density pressure ComputeDensityPressure computeDensityPressureJob = new ComputeDensityPressure { particlesPosition = particlesPosition, densities = particlesDensity, pressures = particlesPressure, hashMap = hashMap, cellOffsetTable = cellOffsetTableNative, settings = settings }; JobHandle computeDensityPressureJobHandle = computeDensityPressureJob.Schedule(particleCount, 64, mergedMergedParticlesDensityPressure); JobHandle mergeComputeDensityPressureVelocityForces = JobHandle.CombineDependencies(computeDensityPressureJobHandle, particlesForcesJobHandle, particlesVelocityJobHandle); // Compute forces ComputeForces computeForcesJob = new ComputeForces { particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, particlesForces = particlesForces, particlesPressure = particlesPressure, particlesDensity = particlesDensity, cellOffsetTable = cellOffsetTableNative, hashMap = hashMap, settings = settings }; JobHandle computeForcesJobHandle = computeForcesJob.Schedule(particleCount, 64, mergeComputeDensityPressureVelocityForces); // Integrate Integrate integrateJob = new Integrate { particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, particlesDensity = particlesDensity, particlesForces = particlesForces }; JobHandle integrateJobHandle = integrateJob.Schedule(particleCount, 64, computeForcesJobHandle); JobHandle mergedIntegrateCollider = JobHandle.CombineDependencies(integrateJobHandle, copyCollidersJobHandle); // Compute Colliders ComputeColliders computeCollidersJob = new ComputeColliders { particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, copyColliders = copyColliders, settings = settings }; JobHandle computeCollidersJobHandle = computeCollidersJob.Schedule(particleCount, 64, mergedIntegrateCollider); // Apply positions ApplyPositions applyPositionsJob = new ApplyPositions { particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, positions = positions, velocities = velocities }; JobHandle applyPositionsJobHandle = applyPositionsJob.Schedule(particleCount, 64, computeCollidersJobHandle); inputDeps = applyPositionsJobHandle; } // Done uniqueTypes.Clear(); return(inputDeps); }
//La data del sphereParticle es compartiiiida protected override JobHandle OnUpdate(JobHandle inputDeps) { if (cameraTransform == null) { cameraTransform = GameObject.Find("Main Camera").transform; } EntityManager.GetAllUniqueSharedComponentData(uniqueTypes); ComponentDataArray <SMBCollider> colliders = SMBColliderGroup.GetComponentDataArray <SMBCollider>(); int colliderCount = colliders.Length; for (int typeIndex = 1; typeIndex < uniqueTypes.Count; typeIndex++) { // Get the current chunk setting SMBProperties settings = uniqueTypes[typeIndex]; //SMBDestination smbdestination = _destination[typeIndex]; SMBCharacterGroup.SetFilter(settings); // Cache the data ComponentDataArray <Position> positions = SMBCharacterGroup.GetComponentDataArray <Position>(); ComponentDataArray <SMBVelocity> velocities = SMBCharacterGroup.GetComponentDataArray <SMBVelocity>(); ComponentDataArray <SMBDestination> SMBdestinations = SMBCharacterGroup.GetComponentDataArray <SMBDestination>(); ComponentDataArray <SMBSspeed> SMBSspeeds = SMBCharacterGroup.GetComponentDataArray <SMBSspeed>(); ComponentDataArray <SMBPath> indexPaths = SMBCharacterGroup.GetComponentDataArray <SMBPath>(); int cacheIndex = typeIndex - 1; int particleCount = positions.Length; NativeMultiHashMap <int, int> hashMap = new NativeMultiHashMap <int, int>(particleCount, Allocator.TempJob); NativeArray <Position> particlesPosition = new NativeArray <Position>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <Position> finalposition = new NativeArray <Position>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <SMBVelocity> particlesVelocity = new NativeArray <SMBVelocity>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <SMBDestination> particlesDestination = new NativeArray <SMBDestination>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <SMBSspeed> particlesSspeed = new NativeArray <SMBSspeed>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <SMBPath> particlesindexPaths = new NativeArray <SMBPath>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <float3> particlesForces = new NativeArray <float3>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <float> particlesPressure = new NativeArray <float>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <float> particlesDensity = new NativeArray <float>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <int> particleIndices = new NativeArray <int>(particleCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); NativeArray <int> cellOffsetTableNative = new NativeArray <int>(cellOffsetTable, Allocator.TempJob); NativeArray <SMBCollider> copyColliders = new NativeArray <SMBCollider>(colliderCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); // Add new or dispose previous particle chunks PreviousParticle nextParticles = new PreviousParticle { hashMap = hashMap, particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, particlesDestination = particlesDestination, particlesSspeed = particlesSspeed, particlesindexPaths = particlesindexPaths, particlesForces = particlesForces, particlesPressure = particlesPressure, particlesDensity = particlesDensity, particleIndices = particleIndices, cellOffsetTable = cellOffsetTableNative, copyColliders = copyColliders }; if (cacheIndex > previousParticles.Count - 1) { previousParticles.Add(nextParticles); } else { previousParticles[cacheIndex].hashMap.Dispose(); previousParticles[cacheIndex].particlesPosition.Dispose(); previousParticles[cacheIndex].particlesVelocity.Dispose(); previousParticles[cacheIndex].particlesDestination.Dispose(); previousParticles[cacheIndex].particlesSspeed.Dispose(); previousParticles[cacheIndex].particlesindexPaths.Dispose(); previousParticles[cacheIndex].particlesForces.Dispose(); previousParticles[cacheIndex].particlesPressure.Dispose(); previousParticles[cacheIndex].particlesDensity.Dispose(); previousParticles[cacheIndex].particleIndices.Dispose(); previousParticles[cacheIndex].cellOffsetTable.Dispose(); previousParticles[cacheIndex].copyColliders.Dispose(); } previousParticles[cacheIndex] = nextParticles; // Copy the component data to native arrays CopyComponentData <Position> particlesPositionJob = new CopyComponentData <Position> { Source = positions, Results = particlesPosition }; JobHandle particlesPositionJobHandle = particlesPositionJob.Schedule(particleCount, 64, inputDeps); CopyComponentData <SMBVelocity> particlesVelocityJob = new CopyComponentData <SMBVelocity> { Source = velocities, Results = particlesVelocity }; JobHandle particlesVelocityJobHandle = particlesVelocityJob.Schedule(particleCount, 64, inputDeps); CopyComponentData <SMBDestination> particlesDestinationJob = new CopyComponentData <SMBDestination> { Source = SMBdestinations, Results = particlesDestination }; JobHandle particlesDestinationJobHandle = particlesDestinationJob.Schedule(particleCount, 64, inputDeps); CopyComponentData <SMBSspeed> particlesSspeedJob = new CopyComponentData <SMBSspeed> { Source = SMBSspeeds, Results = particlesSspeed }; JobHandle particlesSspeedJobHandle = particlesSspeedJob.Schedule(particleCount, 64, inputDeps); CopyComponentData <SMBCollider> copyCollidersJob = new CopyComponentData <SMBCollider> { Source = colliders, Results = copyColliders }; JobHandle copyCollidersJobHandle = copyCollidersJob.Schedule(colliderCount, 64, inputDeps); MemsetNativeArray <float> particlesPressureJob = new MemsetNativeArray <float> { Source = particlesPressure, Value = 0.0f }; JobHandle particlesPressureJobHandle = particlesPressureJob.Schedule(particleCount, 64, inputDeps); MemsetNativeArray <float> particlesDensityJob = new MemsetNativeArray <float> { Source = particlesDensity, Value = 0.0f }; JobHandle particlesDensityJobHandle = particlesDensityJob.Schedule(particleCount, 64, inputDeps); MemsetNativeArray <int> particleIndicesJob = new MemsetNativeArray <int> { Source = particleIndices, Value = 0 }; JobHandle particleIndicesJobHandle = particleIndicesJob.Schedule(particleCount, 64, inputDeps); MemsetNativeArray <float3> particlesForcesJob = new MemsetNativeArray <float3> { Source = particlesForces, Value = new float3(0, 0, 0) }; JobHandle particlesForcesJobHandle = particlesForcesJob.Schedule(particleCount, 64, inputDeps); MemsetNativeArray <Position> finalpositionJob = new MemsetNativeArray <Position> { Source = finalposition, Value = new Position { Value = new float3() } }; JobHandle finalpositionJobHandle = finalpositionJob.Schedule(particleCount, 64, inputDeps); //JobHandle computepathsJobHandle = particlesPositionJobHandle; if (first) { int index = 0, firsTriangle = 1; for (int i = 0; i < particleCount; ++i) { astar.cleanStructures(); astar.setOrigin(positions[i].Value); astar.setDestination(SMBdestinations[i].destination); astar.trianglePath2(); //Allpaths.AddRange(astar.trianglePath2()); wayPointsPath.AddRange(astar.getWayPoints()); int aux = wayPointsPath.Count; if (aux - index == 0) { firsTriangle = -1; } else { firsTriangle = 1; } Unity.Mathematics.Random ola = new Unity.Mathematics.Random(1); indexPaths[i] = new SMBPath { indexIni = index, indexFin = aux, NextPoint = new float3(), Firsttriangle = firsTriangle, recalculate = ola.NextInt(15), fordwarDir = new float3() }; index = aux; } first = false; } //Una vez llegado al destino ir a otro, funciona pero va muuuuy lento /*else * { * int index = 0, firsTriangle = 1, aux = 0; * int diff = 0; * for (int i = 0; i < particleCount; ++i) * { * index = indexPaths[i].indexIni + diff; * aux = indexPaths[i].indexFin + diff; * float3 NextPoint = indexPaths[i].NextPoint, fordwarDir = indexPaths[i].fordwarDir; * int recalculate = indexPaths[i].recalculate; * firsTriangle = indexPaths[i].Firsttriangle; * if (SMBdestinations[i].finished == 1) * { * firsTriangle = 1; * astar.cleanStructures(); * astar.setOrigin(positions[i].Value); * astar.setDestination(SMBdestinations[i].destinations2); * astar.trianglePath2(); * int count = 0; * if (i == 0) * { * wayPointsPath.RemoveRange(0, indexPaths[i].indexFin); * index = 0; * count = indexPaths[i].indexFin; * } * else * { * index = indexPaths[i - 1].indexFin; * count = indexPaths[i].indexFin + diff - indexPaths[i - 1].indexFin; * wayPointsPath.RemoveRange(indexPaths[i - 1].indexFin, count); * } * List<SMBWaypoint> wayaux = astar.getWayPoints(); * wayPointsPath.InsertRange(index, wayaux); * * aux = wayaux.Count; * * indexPaths[i] = new SMBPath { indexIni = index, indexFin = aux + index, NextPoint = new float3(), Firsttriangle = firsTriangle, recalculate = recalculate, fordwarDir = new float3() }; * SMBdestinations[i] = new SMBDestination {finished = 2, destinations2 = SMBdestinations[i].destinations2, destination = SMBdestinations[i].destination }; * diff += aux - count; * } * else indexPaths[i] = new SMBPath { indexIni = index, indexFin = aux, NextPoint = NextPoint, Firsttriangle = firsTriangle, recalculate = recalculate, fordwarDir = fordwarDir }; * } * }*/ NativeArray <SMBWaypoint> NwayPointspaths = new NativeArray <SMBWaypoint>(wayPointsPath.Count, Allocator.TempJob); //MemsetNativeArray<SMBWaypoint> waypointsJob = new MemsetNativeArray<SMBWaypoint> { Source = NwayPointspaths, Value = new SMBWaypoint { } }; //NativeArray<int>.Copy(Allpaths.ToArray(), paths); NativeArray <SMBWaypoint> .Copy(wayPointsPath.ToArray(), NwayPointspaths, wayPointsPath.Count); //CopyComponentData<SMBDestination> particlesDestinationJob = new CopyComponentData<SMBDestination> { Source = SMBdestinations, Results = particlesDestination }; //JobHandle particlesDestinationJobHandle = particlesDestinationJob.Schedule(particleCount, 64, inputDeps); CopyComponentData <SMBPath> particlesIndexPathJob = new CopyComponentData <SMBPath> { Source = indexPaths, Results = particlesindexPaths }; JobHandle particlesIndexPathJobHandle = particlesIndexPathJob.Schedule(particleCount, 64, inputDeps); // Put positions into a hashMap HashPositions hashPositionsJob = new HashPositions { positions = particlesPosition, hashMap = hashMap.ToConcurrent(), cellRadius = settings.radius }; JobHandle hashPositionsJobHandle = hashPositionsJob.Schedule(particleCount, 64, particlesPositionJobHandle); JobHandle mergedPositionIndicesJobHandle = JobHandle.CombineDependencies(hashPositionsJobHandle, particleIndicesJobHandle); MergeParticles mergeParticlesJob = new MergeParticles { particleIndices = particleIndices }; JobHandle mergeParticlesJobHandle = mergeParticlesJob.Schedule(hashMap, 64, mergedPositionIndicesJobHandle); JobHandle mergedMergedParticlesDensityPressure = JobHandle.CombineDependencies(mergeParticlesJobHandle, particlesPressureJobHandle, particlesDensityJobHandle); // Compute density pressure ComputeDensityPressure computeDensityPressureJob = new ComputeDensityPressure { particlesPosition = particlesPosition, densities = particlesDensity, pressures = particlesPressure, hashMap = hashMap, cellOffsetTable = cellOffsetTableNative, settings = settings }; JobHandle computeDensityPressureJobHandle = computeDensityPressureJob.Schedule(particleCount, 64, mergedMergedParticlesDensityPressure); JobHandle mergeComputeDensityPressureVelocityForces = JobHandle.CombineDependencies(computeDensityPressureJobHandle, particlesForcesJobHandle, particlesVelocityJobHandle); // Compute forces ComputeForces computeForcesJob = new ComputeForces { particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, particlesForces = particlesForces, particlesPressure = particlesPressure, particlesDensity = particlesDensity, cellOffsetTable = cellOffsetTableNative, hashMap = hashMap, settings = settings }; JobHandle computeForcesJobHandle = computeForcesJob.Schedule(particleCount, 64, mergeComputeDensityPressureVelocityForces); // Integrate Integrate integrateJob = new Integrate { particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, particlesDensity = particlesDensity, particlesForces = particlesForces }; JobHandle integrateJobHandle = integrateJob.Schedule(particleCount, 64, computeForcesJobHandle); JobHandle mergedIntegrateCollider = JobHandle.CombineDependencies(integrateJobHandle, copyCollidersJobHandle); //JobHandle mergedIntegrateCollider = JobHandle.CombineDependencies(particlesPositionJobHandle, particlesVelocityJobHandle, copyCollidersJobHandle); // Compute Colliders ComputeColliders computeCollidersJob = new ComputeColliders { particlesPosition = particlesPosition, particlesVelocity = particlesVelocity, copyColliders = copyColliders, settings = settings }; JobHandle computeCollidersJobHandle = computeCollidersJob.Schedule(particleCount, 64, mergedIntegrateCollider); JobHandle allReady = JobHandle.CombineDependencies(computeCollidersJobHandle, particlesIndexPathJobHandle, particlesDestinationJobHandle); ComputeNewPoint computeNewPointJob = new ComputeNewPoint { particlesPosition = particlesPosition, waypoints = NwayPointspaths, indexPaths = particlesindexPaths, particlesDestination = particlesDestination }; JobHandle computeNewPointJobHandle = computeNewPointJob.Schedule(particleCount, 64, allReady); computeNewPointJobHandle = JobHandle.CombineDependencies(computeNewPointJobHandle, finalpositionJobHandle); RecomputeNewPoint RecomputeNewPointJob = new RecomputeNewPoint { particlesPosition = particlesPosition, waypoints = NwayPointspaths, indexPaths = particlesindexPaths }; JobHandle RecomputeNewPointJobHandle = RecomputeNewPointJob.Schedule(particleCount, 64, computeNewPointJobHandle); JobHandle preparedToComputePositions = JobHandle.CombineDependencies(RecomputeNewPointJobHandle, particlesSspeedJobHandle); ComputePosition computePositionJob = new ComputePosition { particlesPosition = particlesPosition, particlesDestination = particlesDestination, particlesSspeed = particlesSspeed, particlesVelocity = particlesVelocity, indexPaths = particlesindexPaths, hashMap = hashMap, settings = settings, cellOffsetTable = cellOffsetTableNative, finalPosition = finalposition }; JobHandle comptePositionJobHandle = computePositionJob.Schedule(particleCount, 64, preparedToComputePositions); // Apply positions ApplyPositions applyPositionsJob = new ApplyPositions { particlesPosition = finalposition, particlesVelocity = particlesVelocity, particlesindexPaths = particlesindexPaths, //particlesDestination = particlesDestination, particlesSspeed = particlesSspeed, positions = positions, velocities = velocities, indexPaths = indexPaths, SMBSspeeds = SMBSspeeds, //SMBdestinations = SMBdestinations, }; JobHandle applyPositionsJobHandle = applyPositionsJob.Schedule(particleCount, 64, comptePositionJobHandle); inputDeps = applyPositionsJobHandle; inputDeps.Complete(); NwayPointspaths.Dispose(); finalposition.Dispose(); } // Done uniqueTypes.Clear(); return(inputDeps); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { // Realloc(); if (formations.Length == 0) { return(inputDeps); } //NativeArrayExtensions.ResizeNativeArray(ref raycastHits, math.max(raycastHits.Length,minions.Length)); //NativeArrayExtensions.ResizeNativeArray(ref raycastCommands, math.max(raycastCommands.Length, minions.Length)); var copyNavigationJob = new CopyNavigationPositionToFormation { formations = formations.data, agents = formations.agents, navigators = formations.navigators, navigationData = formations.navigationData, dt = Time.deltaTime }; var copyNavigationJobHandle = copyNavigationJob.Schedule(formations.Length, SimulationState.SmallBatchSize, inputDeps); var copyFormations = new NativeArray <FormationData>(formations.data.Length, Allocator.TempJob); var copyFormationsJob = new CopyComponentData <FormationData> { Source = formations.data, Results = copyFormations }; var copyFormationJobHandle = copyFormationsJob.Schedule(formations.data.Length, SimulationState.HugeBatchSize, copyNavigationJobHandle); var copyFormationEntities = new NativeArray <Entity>(formations.entities.Length, Allocator.TempJob); var copyFormationEntitiesJob = new CopyEntities { Source = formations.entities, Results = copyFormationEntities }; var copyFormationEntitiesJobHandle = copyFormationEntitiesJob.Schedule(formations.entities.Length, SimulationState.HugeBatchSize, copyNavigationJobHandle); var copyBarrier = JobHandle.CombineDependencies(copyFormationJobHandle, copyFormationEntitiesJobHandle); var closestSearchJob = new SearchClosestFormations { formations = copyFormations, closestFormations = formations.closestFormations, formationEntities = copyFormationEntities }; var closestSearchJobHandle = closestSearchJob.Schedule(formations.Length, SimulationState.HugeBatchSize, copyBarrier); var updateFormationsJob = new UpdateFormations { closestFormations = formations.closestFormations, formationNavigators = formations.navigators, formationHighLevelPath = formations.highLevelPaths, formations = formations.data, }; var updateFormationsJobHandle = updateFormationsJob.Schedule(formations.Length, SimulationState.SmallBatchSize, closestSearchJobHandle); // Pass two, rearrangeing the minion indices // TODO Split this system into systems that make sense // calculate formation movement // advance formations // calculate minion position and populate the array return(updateFormationsJobHandle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { var boidCount = m_Boids.boidTag.Length; var cellIndices = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var hashMap = new NativeMultiHashMap <int, int>(boidCount, Allocator.TempJob); var copyTargetPositions = new NativeArray <Position>(m_Boids.boidTarget.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var copyObstaclePositions = new NativeArray <Position>(m_Obstacles.obstaclePositions.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellSeparation = new NativeArray <Position>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellObstacleDistance = new NativeArray <float>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellObstaclePositionIndex = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellCount = new NativeArray <int>(boidCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var hashPositionsJob = new HashPositions { positions = m_Boids.boidPositions, hashMap = hashMap, cellRadius = ECSBootstrapper.boidSettings.cellRadius }; var hashPositionsJobHandle = hashPositionsJob.Schedule(boidCount, 64, inputDeps); var initialCellSeparationJob = new CopyComponentData <Position> { Source = m_Boids.boidPositions, Results = cellSeparation }; var initialCellSeparationJobHandle = initialCellSeparationJob.Schedule(boidCount, 64, inputDeps); var initialCellCountJob = new MemsetNativeArray <int> { Source = cellCount, Value = 1 }; var initialCellCountJobHandle = initialCellCountJob.Schedule(boidCount, 64, inputDeps); var initialCellBarrierJobHandle = JobHandle.CombineDependencies(initialCellSeparationJobHandle, initialCellCountJobHandle); var copyTargetPositionJob = new CopyTargetPositions { Source = m_Boids.boidTarget, Results = copyTargetPositions }; var copyTargetPositionsJobHandle = copyTargetPositionJob.Schedule(m_Boids.boidTarget.Length, 2, inputDeps); var copyObstaclePositionsJob = new CopyComponentData <Position> { Source = m_Obstacles.obstaclePositions, Results = copyObstaclePositions }; var copyObstaclePositionsJobHandle = copyObstaclePositionsJob.Schedule(m_Obstacles.obstaclePositions.Length, 2, inputDeps); var copyTargetObstacleBarrierJobHandle = JobHandle.CombineDependencies(copyTargetPositionsJobHandle, copyObstaclePositionsJobHandle); var mergeCellsBarrierJobHandle = JobHandle.CombineDependencies(hashPositionsJobHandle, initialCellBarrierJobHandle, copyTargetObstacleBarrierJobHandle); var mergeCellsJob = new MergeCells { cellIndices = cellIndices, cellSeparation = cellSeparation, cellObstacleDistance = cellObstacleDistance, cellObstaclePositionIndex = cellObstaclePositionIndex, cellCount = cellCount, obstaclePositions = copyObstaclePositions }; var mergeCellsJobHandle = mergeCellsJob.Schedule(hashMap, 64, mergeCellsBarrierJobHandle); var steerJob = new Steer { cellIndices = cellIndices, //D settings = ECSBootstrapper.boidSettings, cellSeparation = cellSeparation, cellObstacleDistance = cellObstacleDistance, cellObstaclePositionIndex = cellObstaclePositionIndex, cellCount = cellCount, targetPositions = copyTargetPositions, obstaclePositions = copyObstaclePositions, dt = Time.deltaTime, positions = m_Boids.boidPositions, headings = m_Boids.boidHeadings, }; var steerJobHandle = steerJob.Schedule(boidCount, 64, mergeCellsJobHandle); steerJobHandle.Complete(); cellIndices.Dispose(); hashMap.Dispose(); copyTargetPositions.Dispose(); copyObstaclePositions.Dispose(); cellSeparation.Dispose(); cellObstacleDistance.Dispose(); cellObstaclePositionIndex.Dispose(); cellCount.Dispose(); return(inputDeps); }
//----------------------------------------------------------------------------- protected override JobHandle OnUpdate(JobHandle inputDeps) { var settings = m_Agents.GridSettings[0]; var agentCount = m_Agents.Positions.Length; // process the previous frame if (m_PreviousJobData.IsValid && m_PreviousJobData.JobHandle.IsCompleted) { m_PreviousJobData.JobHandle.Complete(); // copy data back to the components var copyPrevJobHandle = new CopyPreviousResultsToAgentsJob { Positions = m_PreviousJobData.Positions, Rotations = m_PreviousJobData.Rotations, Velocities = m_PreviousJobData.Velocities, TargetReached = m_PreviousJobData.TargetReached, OutputPositions = m_Agents.Positions, OutputRotations = m_Agents.Rotations, OutputVelocities = m_Agents.Velocities, OutputTargetReached = m_Agents.TargetReached, }.Schedule(m_PreviousJobData.Length, 64, inputDeps); copyPrevJobHandle.Complete(); m_PreviousJobData.Goals.Dispose(); m_PreviousJobData.NeighborHashMap.Dispose(); } else if (m_PreviousJobData.IsValid && !m_PreviousJobData.JobHandle.IsCompleted) { return(inputDeps); } var positions = new NativeArray <Position>(agentCount, Allocator.TempJob); var copyPositions = new CopyComponentData <Position> { Source = m_Agents.Positions, Results = positions }.Schedule(agentCount, 64, inputDeps); var rotations = new NativeArray <Rotation>(agentCount, Allocator.TempJob); var copyRotation = new CopyComponentData <Rotation> { Source = m_Agents.Rotations, Results = rotations }.Schedule(agentCount, 64, inputDeps); var velocities = new NativeArray <Velocity>(agentCount, Allocator.TempJob); var copyVelocities = new CopyComponentData <Velocity> { Source = m_Agents.Velocities, Results = velocities }.Schedule(agentCount, 64, inputDeps); var goals = new NativeArray <Goal>(agentCount, Allocator.TempJob); var copyGoals = new CopyComponentData <Goal> { Source = m_Agents.Goals, Results = goals }.Schedule(agentCount, 64, inputDeps); var targetReached = new NativeArray <TargetReached>(agentCount, Allocator.TempJob); var copyTargetReached = new CopyComponentData <TargetReached> { Source = m_Agents.TargetReached, Results = targetReached }.Schedule(agentCount, 64, inputDeps); var copyJobs = JobHandle.CombineDependencies(JobHandle.CombineDependencies(copyPositions, copyRotation, copyVelocities), copyGoals, copyTargetReached); CopyFlowField(); var neighborHashMap = new NativeMultiHashMap <int, int>(agentCount, Allocator.TempJob); var vecFromNearestNeighbor = new NativeArray <float3>(agentCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var cellNeighborIndices = new NativeArray <int>(agentCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var neighborHashes = new NativeArray <int>(agentCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var avgNeighborPositions = new NativeArray <float3>(agentCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var avgNeighborVelocities = new NativeArray <float3>(agentCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var steerParams = Main.ActiveSteeringParams; var neighborCellSize = steerParams.NeighbourHashCellSize; var hashNeighborPositionsJob = new HashPositionsWidthSavedHash { Positions = positions, HashMap = neighborHashMap, CellRadius = neighborCellSize, HashedPositions = neighborHashes }; var hashNeighborPositionsJobHandle = hashNeighborPositionsJob.Schedule(agentCount, 64, copyJobs); var mergeNeighborCellsJob = new MergeNeighborCells { cellIndices = cellNeighborIndices, }; var mergeNeighborCellsJobHandle = mergeNeighborCellsJob.Schedule(neighborHashMap, 64, hashNeighborPositionsJobHandle); var closestNeighborJob = new FindClosestNeighbor { CellHash = neighborHashMap, CellHashes = cellNeighborIndices, Positions = positions, ClosestNeighbor = vecFromNearestNeighbor, CellRadius = neighborCellSize, Hashes = neighborHashes, SteerParams = steerParams, AvgNeighborPositions = avgNeighborPositions, AvgNeighborVelocities = avgNeighborVelocities, Velocities = velocities }; var closestNeighborJobHandle = closestNeighborJob.Schedule(agentCount, 64, mergeNeighborCellsJobHandle); var steerJob = new Steer { Settings = settings, AvgVelocities = avgNeighborVelocities, AvgPositions = avgNeighborPositions, DeltaTime = Time.deltaTime, VecFromNearestNeighbor = vecFromNearestNeighbor, Positions = positions, Velocities = velocities, TerrainFlowfield = Main.TerrainFlow, Goals = goals, TargetReached = targetReached, FlowFields = m_AllFlowFields, FlowFieldLength = m_AllFlowFields.Length / TileSystem.k_MaxNumFlowFields, SteerParams = steerParams }; var speedJob = new PositionRotationJob { Velocity = velocities, Positions = positions, Rotations = rotations, TimeDelta = Time.deltaTime, SteerParams = steerParams, GridSettings = settings, Heights = Main.TerrainHeight, Normals = Main.TerrainNormals }; var steerJobHandle = steerJob.Schedule(agentCount, 64, closestNeighborJobHandle); var speedJobHandel = speedJob.Schedule(agentCount, 64, steerJobHandle); m_PreviousJobData = new PreviousJobData { JobHandle = speedJobHandel, IsValid = true, Positions = positions, Rotations = rotations, Velocities = velocities, Goals = goals, TargetReached = targetReached, NeighborHashMap = neighborHashMap, Length = agentCount, }; return(copyJobs); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { this.DisposeBuffers(); var handle = inputDeps; // ---------------------------------------------- // Allocate Memory var groupLength = this._rendererGroup.CalculateLength(); this._localToWorlds = new NativeArray <LocalToWorld>(groupLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); this._animationPlayData = new NativeArray <AnimationPlayData>(groupLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); // ---------------------------------------------- // CopyComponentData handle = new CopyComponentData <LocalToWorld> { Source = this._rendererGroup.GetComponentDataArray <LocalToWorld>(), Results = this._localToWorlds, }.Schedule(groupLength, 32, handle); handle = new CopyComponentData <AnimationPlayData> { Source = this._rendererGroup.GetComponentDataArray <AnimationPlayData>(), Results = this._animationPlayData, }.Schedule(groupLength, 32, handle); // ---------------------------------------------- // アニメーションタイプ毎に再生情報を振り分けていく // FIXME: 今回の実装でNativeMultiHashMapで確保しているメモリはサンプルのために適当。 // → ここらの仕様は最大描画数などを考慮した上で、どれくらい必要なのかすり合わせた方が良いかと思われる。 var playDataMap = new NativeMultiHashMap <int, SendPlayData>(1000000, Allocator.TempJob); handle = new MapAnimationPlayDataJob { LocalToWorlds = this._localToWorlds, AnimationPlayData = this._animationPlayData, SendPlayDataMap = playDataMap.ToConcurrent(), }.Schedule(groupLength, 32, handle); // ---------------------------------------------- // 再生情報の更新 handle = new PlayAnimationJob { DeltaTime = Time.deltaTime, AnimationLengthList = this._animationLengthList, MaxAnimationType = this._maxAnimationType, }.Schedule(this, handle); handle.Complete(); // ---------------------------------------------- // GPU Instancing for (int i = 0; i < this._maxAnimationType; ++i) { // アニメーションタイプに応じた再生情報の取得 var buffer = new NativeArray <SendPlayData>(groupLength, Allocator.Temp); SendPlayData sendPlayData; NativeMultiHashMapIterator <int> it; int instanceCount = 0; // ※ iの値はAnimationTypeに該当 if (!playDataMap.TryGetFirstValue(i, out sendPlayData, out it)) { continue; } do { // 同一のアニメーションが再生されているインスタンスの再生情報をbufferに確保していく。 buffer[instanceCount] = sendPlayData; ++instanceCount; } while (playDataMap.TryGetNextValue(out sendPlayData, ref it)); // Materialに対し再生するアニメーションデータなど(ComputeBuffer)を設定していく。 var renderer = this._animationMeshes[i]; var computeBuffers = this._sendBuffers[i]; // 初回 or 同一のアニメーションが再生されているインスタンス数に変更があったらバッファを初期化 if (computeBuffers.CurrentInstance <= 0 || computeBuffers.CurrentInstance != instanceCount) { if (computeBuffers.SendPlayBuffer != null) { computeBuffers.SendPlayBuffer.Release(); } computeBuffers.SendPlayBuffer = new ComputeBuffer(instanceCount, Marshal.SizeOf(typeof(SendPlayData))); if (computeBuffers.GPUInstancingArgsBuffer != null) { computeBuffers.GPUInstancingArgsBuffer.Release(); } computeBuffers.GPUInstancingArgsBuffer = new ComputeBuffer(1, this._GPUInstancingArgs.Length * sizeof(uint), ComputeBufferType.IndirectArguments); computeBuffers.CurrentInstance = instanceCount; } // 再生情報の設定 var bufferSlice = buffer.Slice(0, instanceCount); var copyArray = new NativeArray <SendPlayData>(bufferSlice.Length, Allocator.Temp); bufferSlice.CopyTo(copyArray); // ※ComputeBufferにはNativeArrayを渡すことが可能。(内部的にもポインタ渡ししていた覚え) computeBuffers.SendPlayBuffer.SetData(copyArray); renderer.AnimationMaterial.SetBuffer(this._playDataBufferID, computeBuffers.SendPlayBuffer); // 「Graphics.DrawMeshInstancedIndirect -> bufferWithArgs」の設定 this._GPUInstancingArgs[0] = (uint)renderer.Mesh.GetIndexCount(0); this._GPUInstancingArgs[1] = (uint)instanceCount; this._GPUInstancingArgs[2] = (uint)renderer.Mesh.GetIndexStart(0); this._GPUInstancingArgs[3] = (uint)renderer.Mesh.GetBaseVertex(0); computeBuffers.GPUInstancingArgsBuffer.SetData(this._GPUInstancingArgs); // 描画 Graphics.DrawMeshInstancedIndirect( renderer.Mesh, 0, renderer.AnimationMaterial, new Bounds(Vector3.zero, 1000000 * Vector3.one), computeBuffers.GPUInstancingArgsBuffer); buffer.Dispose(); copyArray.Dispose(); } playDataMap.Dispose(); return(handle); }