예제 #1
0
        /// <summary>
        /// Recalculates the grid debugging, both the connections and the nodes.
        /// </summary>
        private void RecalculateDebug()
        {
            DeallocateNativeDatastructures();

            int numNodes    = gm.GridDepth * gm.GridWidth;
            int arrayLength = numNodes * GridMaster.NodeNumNeighbors * 2;

            connectionsMeshVertices = new NativeArray <float3>(arrayLength, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            connectionsMeshIndices  = new NativeArray <int>(arrayLength, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            JobifiedQuadRenderer.QuadData quadNodesData = new JobifiedQuadRenderer.QuadData(numNodes);

            JobHandle nodeQuadsDeps = new CalculateNodeQuadsDataJob
            {
                quadPositions    = quadNodesData.quadPositions,
                quadNormals      = quadNodesData.quadNormals,
                quadDimensions   = quadNodesData.quadDimensions,
                quadRotations    = quadNodesData.quadRotations,
                quadColors       = quadNodesData.quadColors,
                nodeTransforms   = gm.NodesTransforms,
                nodesTypes       = gm.NodesTypes,
                invalidColor     = invalidNodeColor,
                walkableColor    = walkableNodeColor,
                nonWalkableColor = nonWalkableNodeColor,
                dimension        = new float2(NodeVisualDebugSize, NodeVisualDebugSize),
            }
            .ScheduleParallel(numNodes, 64, default(JobHandle));

            nodeQuadsDeps = nodesRenderer.ScheduleBuildMesh(quadNodesData, 64, nodeQuadsDeps);
            JobHandle disposeNodeDeps = quadNodesData.Dispose(nodeQuadsDeps);

            CalculateConnectionMeshJob calcConnectionsMeshJob = new CalculateConnectionMeshJob
            {
                vertices        = connectionsMeshVertices,
                indices         = connectionsMeshIndices,
                nodesTransforms = gm.NodesTransforms,
                nodesNeighbors  = gm.NodesNeighbors,
                numNeighbors    = GridMaster.NodeNumNeighbors,
            };

            JobHandle calcConnectionMeshHandle = calcConnectionsMeshJob.ScheduleParallel(numNodes, 64, default(JobHandle));

            JobHandle.CompleteAll(ref calcConnectionMeshHandle, ref nodeQuadsDeps);

            connectionsMesh.SetVertices(calcConnectionsMeshJob.vertices);
            connectionsMesh.SetIndices(calcConnectionsMeshJob.indices, MeshTopology.Lines, 0);
            nodesRenderer.UpdateMesh();

            UpdateShowDebug();

            disposeNodeDeps.Complete();
        }
예제 #2
0
        /// <summary>
        /// Adds the nodes debugging.
        /// </summary>
        public void RecalculateDebug()
        {
            DisposeDebugNativeDatastructures();
            int numNodes = gridDepth * gridWidth;

            // prepare the job that calculates the vertices for the neighbor connection lines
            int arrayLength = numNodes * NodeNeighbors * 2;

            connectionsMeshVertices = new NativeArray <Vector3>(arrayLength, Allocator.Persistent);
            connectionsMeshIndices  = new NativeArray <int>(arrayLength, Allocator.Persistent);

            CalculateConnectionMeshJob calcConnectionsMeshJob = new CalculateConnectionMeshJob(NodeNeighbors, nodesTransforms, nodesNeighbors, connectionsMeshVertices, connectionsMeshIndices);
            JobHandle calcConnectionMeshHandle = calcConnectionsMeshJob.Schedule(numNodes, 8);

            // do other required stuff before calling complete so we have actual parallelism
            MeshRenderer mr = Utils.GetOrAddComponent <MeshRenderer>(transform, out bool createdRenderer);

            mr.shadowCastingMode    = ShadowCastingMode.Off;
            mr.sharedMaterial       = nodeConnectionsMaterial;
            mr.lightProbeUsage      = LightProbeUsage.Off;
            mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
            mr.enabled = showNodesConnections;

            MeshFilter filter = Utils.GetOrAddComponent <MeshFilter>(transform, out bool createdFilter);

            filter.sharedMesh = connectionsMesh;

            // the nodes themselves
            nodeBatcher.Clear();

            if (showNodes)
            {
                for (int i = 0; i < numNodes; i++)
                {
                    NodeTransform nt       = nodesTransforms[i];
                    NodeType      nodeType = nodesTypes[i];

                    Color32 c;

                    if (nodeType == NodeType.Invalid)
                    {
                        c = invalidNodeColor;
                    }
                    else if (nodeType == NodeType.OccupiedByObstacle)
                    {
                        c = nonWalkableNodeColor;
                    }
                    else
                    {
                        c = walkableNodeColor;
                    }

                    Vector3   pos = nt.Pos + (nt.Up * NodeVisualNormalOffset);
                    Matrix4x4 trs = Matrix4x4.TRS(pos, nt.GetRotation(), Vector3.one);

                    // batch each node quad debug
                    nodeBatcher.AddItem(c, trs);
                }
            }

            calcConnectionMeshHandle.Complete();

            // set the mesh using the results of the job
            connectionsMesh.SetVertices(calcConnectionsMeshJob.vertices);
            connectionsMesh.SetIndices(calcConnectionsMeshJob.indices, MeshTopology.Lines, 0);
        }