Advertisement
Guest User

PathFinding.compute

a guest
Mar 10th, 2017
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.14 KB | None | 0 0
  1. // Each #kernel tells which function to compile; you can have many kernels
  2. #pragma kernel CSMain
  3.  
  4. // Create a RenderTexture with enableRandomWrite flag and set it
  5. // with cs.SetTexture
  6. RWTexture2D<float4> Result;
  7. #define _TOTAL_NODE_NUM 100
  8. #define _SEEKER_NUM 10
  9. #define _NODENUM_ _TOTAL_NODE_NUM * _SEEKER_NUM
  10.  
  11. uint GLB_gridSizeX, GLB_gridSizeY;
  12. uint GLB_gridWorldSizeX, GLB_gridWorldSizeY;
  13.  
  14. struct NODE_NUM_TARGET
  15. {
  16.     int currentNodeNum;
  17.     int targetNodeNum;
  18.     uint __padding1;
  19.     uint __padding2;
  20. };
  21.  
  22. struct NODE
  23. {
  24.     int nodeNum;
  25.     int parentNodeNum;
  26.     uint walkable;
  27.     uint gCost;
  28.     uint hCost;
  29.     uint seekerNum;
  30.     uint __padding1;
  31.     uint __padding2;
  32. };
  33.  
  34. struct VEC2_INT
  35. {
  36.     int x;
  37.     int y;
  38. };
  39.  
  40. struct PATH
  41. {
  42.     int seekerNo;
  43.     int nodeNum;
  44. };
  45.  
  46. RWStructuredBuffer<NODE_NUM_TARGET> node_curDest_Buffer;
  47. RWStructuredBuffer<NODE> agentsInfo_Buffer;
  48. RWStructuredBuffer<PATH> pathData;
  49.  
  50. int InitNodeFull(inout NODE node)
  51. {
  52.     node.nodeNum = -99;
  53.     node.parentNodeNum = -99;
  54.     node.walkable = -99;
  55.     node.gCost = 0;
  56.     node.hCost = 0;
  57.     node.seekerNum = -99;
  58.  
  59.     return 0;
  60. }
  61.  
  62. int InitPath(int seekerNo)
  63. {
  64.     [allow_uav_condition]
  65.     for (int i = 0; i < _NODENUM_; i++)//??
  66.     {
  67.         if (pathData[i].seekerNo == seekerNo)
  68.         {
  69.             pathData[i].nodeNum = -99;
  70.         }
  71.     }
  72.  
  73.     return 0;
  74. }
  75.  
  76. int InitParent(inout NODE node)
  77. {
  78.     node.parentNodeNum = -99;
  79.     return 0;
  80. }
  81.  
  82. int ResetParentInfoForThisSeeker(uint seekerNum)
  83. {
  84.     [allow_uav_condition]
  85.     for (uint i = 0; i < _NODENUM_; i++)
  86.     {
  87.         if (agentsInfo_Buffer[i].seekerNum == seekerNum)
  88.         {
  89.             InitParent(agentsInfo_Buffer[i]);
  90.         }
  91.     }
  92.     return 0;
  93. }
  94.  
  95. int GetNodeFromID(int nodeNum, uint seekerNo, inout NODE node)
  96. {
  97.     [allow_uav_condition]
  98.     for (uint i = 0; i < _NODENUM_; i++)
  99.     {
  100.         if (agentsInfo_Buffer[i].seekerNum == seekerNo && agentsInfo_Buffer[i].nodeNum == nodeNum)
  101.         {
  102.             node = agentsInfo_Buffer[i];
  103.         }
  104.     }
  105.     return 0;
  106. }
  107.  
  108. int CountOnSet(NODE set[_TOTAL_NODE_NUM])
  109. {
  110.     int countNum = 0;
  111.  
  112.     [allow_uav_condition]
  113.     for (int i = 0; i < _TOTAL_NODE_NUM; i++)
  114.     {
  115.         if (set[i].nodeNum < 0 || set[i].seekerNum < 0)
  116.         {
  117.             //continue;
  118.         }
  119.         else
  120.         {
  121.             countNum++;
  122.         }
  123.     }
  124.     return countNum;
  125. }
  126.  
  127. int GetFirstElement(NODE set[_TOTAL_NODE_NUM], inout NODE node)
  128. {
  129.     [allow_uav_condition]
  130.     for (int i = 0; i < _TOTAL_NODE_NUM; i++)
  131.     {
  132.         if (set[i].nodeNum >= 0 && set[i].seekerNum >= 0)
  133.         {
  134.             node = set[i];
  135.             break;
  136.         }
  137.     }
  138.  
  139.     return 0;
  140. }
  141.  
  142. bool ContainsOnSet(NODE set[_TOTAL_NODE_NUM], NODE node)
  143. {
  144.     bool isContains = false;
  145.     [allow_uav_condition]
  146.     for (int i = 0; i < _TOTAL_NODE_NUM; i++)
  147.     {
  148.         if (set[i].nodeNum == node.nodeNum)
  149.         {
  150.             isContains = true;
  151.         }
  152.     }
  153.  
  154.     return isContains;
  155. }
  156.  
  157. int PopulateSetSeekerNum(inout NODE set[_TOTAL_NODE_NUM], uint seekerNum)
  158. {
  159.     [allow_uav_condition]
  160.     for (int i = 0; i < _TOTAL_NODE_NUM; i++)
  161.     {
  162.         set[i].seekerNum = seekerNum;
  163.     }
  164.  
  165.     return 0;
  166. }
  167.  
  168. int GetLowestCostNode(NODE openSet[_TOTAL_NODE_NUM], NODE closedSet[_TOTAL_NODE_NUM], inout NODE node)
  169. {
  170.     [allow_uav_condition]
  171.     for (int i = 0; i < _TOTAL_NODE_NUM; i++)
  172.     {
  173.         if (openSet[i].nodeNum >= 0 && openSet[i].seekerNum >= 0)
  174.         {
  175.             if (ContainsOnSet(closedSet, openSet[i]) == true)
  176.             {
  177.                 continue;
  178.             }
  179.             else
  180.             {
  181.                 if ((openSet[i].gCost + openSet[i].hCost) <= (node.gCost + node.hCost) && openSet[i].hCost < node.hCost)
  182.                 {
  183.                     node = openSet[i];
  184.                 }
  185.             }
  186.         }
  187.     }
  188.  
  189.     return 0;
  190. }
  191.  
  192. int RemoveFromSet(inout NODE set[_TOTAL_NODE_NUM], NODE data)
  193. {
  194.     [allow_uav_condition]
  195.     for (uint i = 0; i < _TOTAL_NODE_NUM; i++)
  196.     {
  197.         if (set[i].nodeNum == data.nodeNum)
  198.         {
  199.             InitNodeFull(set[i]);
  200.         }
  201.     }
  202.  
  203.     return 0;
  204. }
  205.  
  206. int AddToSet(inout NODE set[_TOTAL_NODE_NUM], NODE data)
  207. {
  208.     bool added = false;
  209.     [allow_uav_condition]
  210.     for (uint i = 0; i < _TOTAL_NODE_NUM; i++)
  211.     {
  212.         if (added == false)
  213.         {
  214.             if (set[i].nodeNum < 0 || set[i].seekerNum < 0)
  215.             {
  216.                 set[i] = data;
  217.                 added = true;
  218.             }
  219.         }
  220.     }
  221.  
  222.     return 0;
  223. }
  224.  
  225. VEC2_INT ToVEC2_INT(int nodeNumFlat1D)
  226. {
  227.     int x = 0;
  228.     int y = 0;
  229.     uint nIJ = (uint)nodeNumFlat1D;
  230.  
  231.     uint cNum = nIJ + 1;
  232.     uint cSizex = GLB_gridSizeX + 1;
  233.     if (cNum % cSizex == 0)
  234.     {
  235.         y = cNum / cSizex;
  236.     }
  237.     else
  238.     {
  239.         y = (cNum / cSizex) + 1;
  240.     }
  241.     y = y - 1;
  242.  
  243.     uint rCount = cSizex * y;
  244.     x = (int)(nIJ - rCount);
  245.  
  246.     VEC2_INT id;
  247.     id.x = x;
  248.     id.y = y;
  249.  
  250.     return id;
  251. }
  252.  
  253. uint To1D_NODENUM(VEC2_INT id)
  254. {
  255.     return id.x + (id.y * (GLB_gridSizeX + 1));
  256. }
  257.  
  258. int GetDistance(NODE nodeA, NODE nodeB)
  259. {
  260.     VEC2_INT nodeA_2D = ToVEC2_INT(nodeA.nodeNum);
  261.     VEC2_INT nodeB_2D = ToVEC2_INT(nodeB.nodeNum);
  262.  
  263.     int dstX = abs(nodeA_2D.x - nodeB_2D.x);
  264.     int dstY = abs(nodeA_2D.y - nodeB_2D.y);
  265.  
  266.     if (dstX > dstY)
  267.     {
  268.         return 14 * dstY + 10 * (dstX - dstY);
  269.     }
  270.     else
  271.     {
  272.         return 14 * dstX + 10 * (dstY - dstX);
  273.     }
  274. }
  275.  
  276. bool IsEqual(NODE a, NODE b)
  277. {
  278.     if (a.nodeNum == b.nodeNum && a.seekerNum == b.seekerNum)
  279.     {
  280.         return true;
  281.     }
  282.     else
  283.     {
  284.         return false;
  285.     }
  286. }
  287.  
  288.  
  289.  
  290. int UpdateCostData(NODE currentNode, NODE targetNode, NODE neighborNode, uint seekerNo, uint new_g_Cost)
  291. {
  292.     [allow_uav_condition]
  293.     for (int i = 0; i < _NODENUM_; i++)
  294.     {
  295.         if (agentsInfo_Buffer[i].seekerNum == seekerNo)
  296.         {
  297.             if (neighborNode.nodeNum == agentsInfo_Buffer[i].nodeNum)
  298.             {
  299.                 agentsInfo_Buffer[i].gCost = new_g_Cost;
  300.                 agentsInfo_Buffer[i].hCost = GetDistance(neighborNode, targetNode);
  301.                 agentsInfo_Buffer[i].parentNodeNum = currentNode.nodeNum;
  302.                 break;
  303.             }
  304.         }
  305.  
  306.     }
  307.  
  308.     return 0;
  309. }
  310.  
  311. int UpdateNeighbours(inout NODE neighbors[8], NODE curNode, uint seekerNo)
  312. {
  313.     [allow_uav_condition]
  314.     for (uint i = 0; i < 8; i++)
  315.     {
  316.         InitNodeFull(neighbors[i]);
  317.     }
  318.  
  319.     [allow_uav_condition]
  320.     for (int x = -1; x <= 1; x++)
  321.     {
  322.         [allow_uav_condition]
  323.         for (int y = -1; y <= 1; y++)
  324.         {
  325.             if (x == 0 && y == 0)
  326.                 continue;
  327.  
  328.             VEC2_INT node2D = ToVEC2_INT(curNode.nodeNum);
  329.  
  330.             int checkX = node2D.x + x;
  331.             int checkY = node2D.y + y;
  332.  
  333.             int gridSizeX_CNV = (int)GLB_gridSizeX;
  334.             int gridSizeY_CNV = (int)GLB_gridSizeY;
  335.  
  336.             if (checkX >= 0 && checkX < gridSizeX_CNV && checkY >= 0 && checkY < gridSizeY_CNV)
  337.             {
  338.                 VEC2_INT v;
  339.                 v.x = checkX;
  340.                 v.y = checkY;
  341.                 int newNodeNum = To1D_NODENUM(v);
  342.                 NODE thisNeighbor;
  343.                 GetNodeFromID(newNodeNum, seekerNo, thisNeighbor);
  344.                 //AddToNeighbor(thisNeighbor, seekerNo);
  345.  
  346.                 bool added = false;
  347.                 [allow_uav_condition]
  348.                 for (uint i = 0; i < 8; i++)
  349.                 {
  350.                     if (added == false)
  351.                     {
  352.                         if (neighbors[i].nodeNum < 0)
  353.                         {
  354.                             neighbors[i] = thisNeighbor;
  355.                             //break;
  356.                             added = true;
  357.                         }
  358.                     }
  359.                 }
  360.             }
  361.         }
  362.     }
  363.  
  364.     return 0;
  365. }
  366.  
  367. int UpdatePathData(int nodeNumList[_TOTAL_NODE_NUM], int seekerNo)
  368. {
  369.     [allow_uav_condition]
  370.     for (int i = 0; i < _TOTAL_NODE_NUM; i++)
  371.     {
  372.         bool added = false;
  373.         [allow_uav_condition]
  374.         for (int j = 0; j < _NODENUM_; j++)
  375.         {
  376.             if (pathData[j].seekerNo == seekerNo)
  377.             {
  378.                 if (added == false)
  379.                 {
  380.                     if (pathData[j].nodeNum < 0)
  381.                     {
  382.                         pathData[j].nodeNum = nodeNumList[i];
  383.                         added = true;
  384.                     }
  385.                 }
  386.             }
  387.         }
  388.     }
  389.  
  390.     return 0;
  391. }
  392.  
  393. int RetracePath(NODE startNode, NODE endNode, int seekerNo)
  394. {
  395.     NODE currentNode = endNode;
  396.  
  397.     int nodeNumList[_TOTAL_NODE_NUM];
  398.     [allow_uav_condition]
  399.     for (int k = 0; k < _TOTAL_NODE_NUM; k++)
  400.     {
  401.         nodeNumList[k] = -99;
  402.     }
  403.  
  404.     InitPath(seekerNo);
  405.  
  406.     [allow_uav_condition]
  407.     while (IsEqual(currentNode, startNode) != true)
  408.     {
  409.         //AddToPath(currentNode.nodeNum, seekerNo);
  410.         [allow_uav_condition]
  411.         for (int l = 0; l < _TOTAL_NODE_NUM; l++)
  412.         {
  413.             if (nodeNumList[l] < 0)
  414.             {
  415.                 nodeNumList[l] = currentNode.nodeNum;
  416.                 break;
  417.             }
  418.  
  419.         }
  420.         GetNodeFromID(currentNode.parentNodeNum, seekerNo, currentNode);
  421.     }
  422.  
  423.     //ReversePath(seekerNo);
  424.  
  425.     int tmpList[_TOTAL_NODE_NUM];
  426.  
  427.     [allow_uav_condition]
  428.     for (int m = 0; m < _TOTAL_NODE_NUM; m++)
  429.     {
  430.         tmpList[m] = nodeNumList[m];
  431.     }
  432.  
  433.     [allow_uav_condition]
  434.     for (int n = 0; n < _TOTAL_NODE_NUM; n++)
  435.     {
  436.         nodeNumList[n] = -99;
  437.     }
  438.  
  439.     int r = 0;
  440.  
  441.     [allow_uav_condition]
  442.     for (int s = _TOTAL_NODE_NUM - 1; s >= 0; s--)
  443.     {
  444.         if (tmpList[s] < 0)
  445.         {
  446.             //continue;
  447.         }
  448.         else
  449.         {
  450.             nodeNumList[r] = tmpList[s];
  451.             r++;
  452.         }
  453.     }
  454.     UpdatePathData(nodeNumList, seekerNo);
  455.  
  456.     return 0;
  457. }
  458. //
  459.  
  460.  
  461. int FindPath(int startPosID, int targetPosID, uint seekerNo)
  462. {
  463.     ResetParentInfoForThisSeeker(seekerNo);
  464.     NODE startNode;
  465.     NODE targetNode;
  466.     GetNodeFromID(startPosID, seekerNo, startNode);
  467.     GetNodeFromID(targetPosID, seekerNo, targetNode);
  468.  
  469.     NODE openSet[_TOTAL_NODE_NUM];
  470.     NODE closedSet[_TOTAL_NODE_NUM];
  471.  
  472.     [allow_uav_condition]
  473.     for (int i = 0; i < _TOTAL_NODE_NUM; i++)
  474.     {
  475.         InitNodeFull(openSet[i]);
  476.         InitNodeFull(closedSet[i]);
  477.     }
  478.     openSet[0] = startNode;
  479.  
  480.     uint counter = 0;
  481.     [allow_uav_condition]
  482.     while (CountOnSet(openSet) > 0)
  483.     {
  484.         NODE currentNode;
  485.         GetFirstElement(openSet, currentNode);
  486.  
  487.         if (counter > 0)
  488.         {
  489.             GetLowestCostNode(openSet, closedSet, currentNode);
  490.         }
  491.  
  492.         RemoveFromSet(openSet, currentNode);
  493.         AddToSet(closedSet, currentNode);
  494.  
  495.         if (IsEqual(currentNode, targetNode) == true)
  496.         {
  497.             RetracePath(startNode, targetNode, seekerNo);
  498.             return 0;
  499.         }
  500.  
  501.  
  502.         NODE neighbors[8];
  503.  
  504.         UpdateNeighbours(neighbors, currentNode, seekerNo);
  505.  
  506.         [allow_uav_condition]
  507.         for (int n = 0; n < 8; n++)
  508.         {
  509.             if (neighbors[n].nodeNum < 0)
  510.             {
  511.                 continue;
  512.             }
  513.             if (neighbors[n].walkable == 0 || ContainsOnSet(closedSet, neighbors[n]) == true)
  514.             {
  515.                 continue;
  516.             }
  517.  
  518.             uint newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbors[n]);
  519.             if (newMovementCostToNeighbour < neighbors[n].gCost || ContainsOnSet(openSet, neighbors[n]) == false)
  520.             {
  521.                 UpdateCostData(currentNode, targetNode, neighbors[n], seekerNo, newMovementCostToNeighbour);
  522.  
  523.                 if (ContainsOnSet(openSet, neighbors[n]) == false)
  524.                 {
  525.                     AddToSet(openSet, neighbors[n]);
  526.                 }
  527.             }
  528.         }
  529.  
  530.         counter++;
  531.     }
  532.  
  533.     return 0;
  534. }
  535.  
  536.  
  537. [numthreads(8,8,1)]
  538. void CSMain (uint3 id : SV_DispatchThreadID)
  539. {
  540.     // TODO: insert actual code here!
  541.  
  542.     //Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);
  543.  
  544.     int sd = FindPath(node_curDest_Buffer[id.x].currentNodeNum, node_curDest_Buffer[id.x].targetNodeNum, id.x);
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement