Exemplo n.º 1
0
 public CalculateNeighborsJob(NativeArray <NodeNeighbor> neighbors, NativeArray <NodeTransform> nodesTransforms, ScanAreaSettings scanSettings, CalculateNeighborSettings neighborSettings)
 {
     this.neighbors        = neighbors;
     this.nodesTransforms  = nodesTransforms;
     this.scanSettings     = scanSettings;
     this.neighborSettings = neighborSettings;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the grid of nodes.
        /// </summary>
        public void CreateGrid()
        {
            Bounds scanBounds = scanCollider.bounds;

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

            gridWidth = scanSettings.gridWidth;
            gridDepth = scanSettings.gridDepth;

            int gridDimension = gridWidth * gridDepth;

            if (isGridCreated)
            {
                nodesTransforms.Dispose();
                nodesTypes.Dispose();
                nodesNeighbors.Dispose();
            }

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

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

            // schedule the 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 to bake obstacles
            BakeObstaclesSettings       bakeObstaclesSettings       = new BakeObstaclesSettings(maxCharacterHeight, boxToNodeObstaclePercentage, obstacleMask);
            CalculateBoxcastCommandsJob calculateBoxcastCommandsJob = new CalculateBoxcastCommandsJob(bakeObstaclesSettings, nodesTransforms);
            JobHandle calculateBoxcastHandle = calculateBoxcastCommandsJob.Schedule(gridDimension, 32, createNodesHandle);

            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
            CalculateNeighborSettings neighborSettings      = new CalculateNeighborSettings(maxWalkableHeightWithSlope, maxWalkableHeightWithStep);
            CalculateNeighborsJob     calculateNeighborsJob = new CalculateNeighborsJob(nodesNeighbors, nodesTransforms, scanSettings, neighborSettings);
            JobHandle calculateNeighborsHandle = calculateNeighborsJob.Schedule(gridDimension, 32, bakeObstaclesHandle);

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

            calculateCommandsJob.Dispose();
            calculateBoxcastCommandsJob.Dispose();
            nodeHits.Dispose();
            obstacleHits.Dispose();

#if DEBUG_RENDER
            RecalculateDebug();
#endif
            isGridCreated = true;
        }