Exemplo n.º 1
0
        /// <summary>
        /// Creates the grid of nodes.
        /// </summary>
        public void CreateGrid()
        {
            DestroyGrid();

            // Note: perhaps we might want to snap the extents value when editing the bounding box
            // in the editor?
            Bounds           scanBounds   = scanCollider.bounds;
            ScanAreaSettings scanSettings = new ScanAreaSettings(scanBounds.center, scanBounds.extents, walkableMask);
            int gridDimension             = scanSettings.dimension;

            nodesTransforms = new NativeArray <NodeTransform>(gridDimension, Allocator.Persistent);
            nodesTypes      = new NativeArray <NodeType>(gridDimension, Allocator.Persistent);
            nodesNeighbors  = new NativeArray <NodeNeighbor>(gridDimension * NodeNeighbors, Allocator.Persistent);

            // calculate the raycast commands
            CalculateRaycastCommandsJob calculateCommandsJob = new CalculateRaycastCommandsJob(scanSettings);
            JobHandle calculateCommandsHandle = calculateCommandsJob.Schedule(gridDimension, 32);

            // schedule the raycast commands to retrieve the hits
            NativeArray <RaycastHit> nodeHits = new NativeArray <RaycastHit>(gridDimension, Allocator.TempJob);
            JobHandle raycastCommandHandle    = RaycastCommand.ScheduleBatch(calculateCommandsJob.commands, nodeHits, 1, calculateCommandsHandle);

            // build the nodes using the received hits
            CreateNodesJob createNodesJob    = new CreateNodesJob(nodesTransforms, nodesTypes, nodeHits, calculateCommandsJob.commands);
            JobHandle      createNodesHandle = createNodesJob.Schedule(gridDimension, 32, raycastCommandHandle);

            // calculate the boxcast commands to bake obstacles
            BakeObstaclesSettings       bakeObstaclesSettings       = new BakeObstaclesSettings(maxCharacterHeight, boxToNodeObstaclePercentage, obstacleMask);
            CalculateBoxcastCommandsJob calculateBoxcastCommandsJob = new CalculateBoxcastCommandsJob(bakeObstaclesSettings, nodesTransforms);
            JobHandle calculateBoxcastHandle = calculateBoxcastCommandsJob.Schedule(gridDimension, 32, createNodesHandle);

            // schedule the boxcast commands to retrieve the hits
            NativeArray <RaycastHit> obstacleHits = new NativeArray <RaycastHit>(gridDimension, Allocator.TempJob);
            JobHandle boxcastCommandHandle        = BoxcastCommand.ScheduleBatch(calculateBoxcastCommandsJob.commands, obstacleHits, 1, calculateBoxcastHandle);

            // prepare the bake obstacles job
            BakeObstaclesJob bakeObstaclesJob    = new BakeObstaclesJob(nodesTypes, obstacleHits);
            JobHandle        bakeObstaclesHandle = bakeObstaclesJob.Schedule(gridDimension, 32, boxcastCommandHandle);

            // now calculate the neighbors
            CalculateNeighborsJob calculateNeighborsJob    = new CalculateNeighborsJob(nodesNeighbors, nodesTransforms, scanSettings, maxWalkableStep);
            JobHandle             calculateNeighborsHandle = calculateNeighborsJob.Schedule(gridDimension, 32, bakeObstaclesHandle);

            // schedule disposing the stuff
            JobHandle disposeHandle = calculateCommandsJob.commands.Dispose(createNodesHandle);

            disposeHandle = JobHandle.CombineDependencies(disposeHandle, nodeHits.Dispose(createNodesHandle));
            disposeHandle = JobHandle.CombineDependencies(disposeHandle, calculateBoxcastCommandsJob.commands.Dispose(boxcastCommandHandle));
            disposeHandle = JobHandle.CombineDependencies(disposeHandle, obstacleHits.Dispose(bakeObstaclesHandle));

            // wait to complete all the scheduled stuff
            calculateNeighborsHandle.Complete();

            gridWidth     = scanSettings.gridWidth;
            gridDepth     = scanSettings.gridDepth;
            isGridCreated = true;

#if DEBUG_RENDER
            RecalculateDebug();
#endif
        }
Exemplo n.º 2
0
 public CalculateBoxcastCommandsJob(BakeObstaclesSettings settings, NativeArray <NodeTransform> nodesTransforms)
 {
     this.commands        = new NativeArray <BoxcastCommand>(nodesTransforms.Length, Allocator.TempJob);
     this.settings        = settings;
     this.nodesTransforms = nodesTransforms;
 }