烘培程式碼在 rcBuildHeightfieldLayers
本質上是為每個tile生成高度上的不同layer
演算法的關鍵是三層迴圈:
for z 軸迴圈
for x 軸迴圈
for 高度span 迴圈
判斷span和相鄰span的連通性(x/z平面相鄰cell)
如果聯通, 則標註為同一個layer, 也就是在x/z平面上標註layer, 形成像是互不相交的麵包片疊放的樣子, 也有有坡度的layer
然後做了一些layer合併處理, 相鄰的layer且在x/z平面不重疊且合併後高度差較小的, 可以合併為一個layer
同時layer記錄了當前layer的上下高度範圍, 邊界(座標系), 邊界(體素),
heights記錄了layer內每個span相對於layer的體素下邊界的高度差(體素單位)
areas記錄了layer內每個span的areas
cons記錄了layer和span的相鄰關係
(注意程式碼裡改了一些變數的命名, 過於簡化的變數名不利於新手看懂程式碼)
(另外, 程式碼裡把y改成了z, recast本身程式碼裡體素遍歷都是 x/y平面, 按Unity習慣, 改成了 x/z 平面遍歷, y代表高度)
/// See the #rcConfig documentation for more information on the configuration parameters. /// /// @see rcAllocHeightfieldLayerSet, rcCompactHeightfield, rcHeightfieldLayerSet, rcConfig bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf, const int borderSize, const int walkableHeight, rcHeightfieldLayerSet& lset) { rcAssert(ctx); rcScopedTimer timer(ctx, RC_TIMER_BUILD_LAYERS); const int w = chf.width; const int h = chf.height; rcScopedDelete<unsigned char> srcReg((unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP)); if (!srcReg) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'srcReg' (%d).", chf.spanCount); return false; } memset(srcReg,0xff,sizeof(unsigned char)*chf.spanCount); const int nsweeps = chf.width; rcScopedDelete<rcLayerSweepSpan> sweeps((rcLayerSweepSpan*)rcAlloc(sizeof(rcLayerSweepSpan)*nsweeps, RC_ALLOC_TEMP)); if (!sweeps) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'sweeps' (%d).", nsweeps); return false; } // Partition walkable area into monotone regions. int prevCount[256]; unsigned char regId = 0; //注意這裡是三層迴圈: // for z 平面 // for x 平面 // for y 平面 (高度) // 最內層是對每個y平面的處理, 在每個y層面上根據span在x/z的連線性做region分配和合並, 也就是layer的意義: 按高度分層. 像是切片面包. // 從3d視角看是, 遍歷x/z平面的每個cell, 依次檢查當前cell與相鄰cell在高度上的切片span是否有聯通的, 如果有聯通就把x/z平面相鄰的cell上region賦值為相同id. 讓x/z平面形成region.高度上 for (int z = borderSize; z < h-borderSize; ++z) { // prevCount 記錄的是當前x軸上的sweep和上一輪x迴圈(-z方向)的region相連的span數量. memset(prevCount,0,sizeof(int)*regId); //(按行掃描編號), 這個編號在y的迴圈體內, 也就是每掃描一行x則重置, 掃描完一行後後面會把sweepId變成regionId, 所以重置沒問題. unsigned char nowSweepId = 0; for (int x = borderSize; x < w-borderSize; ++x) { const rcCompactCell& c = chf.cells[x+z*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { const rcCompactSpan& s = chf.spans[i]; if (chf.areas[i] == RC_NULL_AREA) continue; unsigned char sweepId = 0xff; //(-1, 0)方向如果有連線 // -x if (rcGetCon(s, 0) != RC_NOT_CONNECTED) { const int ax = x + rcGetDirOffsetX(0); const int ay = z + rcGetDirOffsetY(0); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0); //如果連線的不是NULL_AREA且它的sweepId並不是未初始化狀態(未設定, 預設值0xff) (sweepId儲存在srcReg裡) //那麼把自己的sweepId也設定為相鄰這個span的sweepId,因為是從左到右遍歷, 所以-x是剛剛遍歷過的,如果連線(x軸相鄰的span)且有srcReg, 則設定為相同srcReg if (chf.areas[ai] != RC_NULL_AREA && srcReg[ai] != 0xff) sweepId = srcReg[ai]; } // 如果和左側相鄰span(-1, 0)沒有連線, 或者連線的area是NULL, 或者sweepId無效, 則把自己的sweepId設定為新的id. (新分配一個掃描編號) if (sweepId == 0xff) { sweepId = nowSweepId++; sweeps[sweepId].nei = 0xff; sweeps[sweepId].ns = 0; } // 檢查完-x方向. 再檢查之前掃描過的z方向的鄰居 (上一輪掃描過的) // 如果相連且sweepId不是0xff, 則判斷是不是剛剛x方向新加的sweepId(還沒鄰居), 如果是則把z方向的這個鄰居設定成自己的鄰居 // 如果當前鄰居是z方向的這個span, 則把ns++, 把鄰居sweepId記錄的數量也加1(prevCount[nrSweepId]++) // 如果當前鄰居不是z方向這個span, 說明和-z這一行有兩個鄰居, 則把鄰居置為無效值 // (0, -1) x/z平面的下面 -> -z, 注意原始碼是 x/y 平面, 這裡原本註釋寫的 -y if (rcGetCon(s,3) != RC_NOT_CONNECTED) { const int ax = x + rcGetDirOffsetX(3); const int ay = z + rcGetDirOffsetY(3); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3); const unsigned char nrRegId = srcReg[ai]; if (nrRegId != 0xff) { // Set neighbour when first valid neighbour is encoutered. if (sweeps[sweepId].ns == 0) sweeps[sweepId].nei = nrRegId; if (sweeps[sweepId].nei == nrRegId) { // Update existing neighbour sweeps[sweepId].ns++; prevCount[nrRegId]++; } else { // This is hit if there is nore than one neighbour. // Invalidate the neighbour. sweeps[sweepId].nei = 0xff; } } } srcReg[i] = sweepId; } } // Create unique ID. for (int i = 0; i < nowSweepId; ++i) { /// 如果鄰居設定了, 而且鄰居連線我的數量和我數量相同則說明我們是完全相臨的, 可以合併, 否則意味著我的鄰居可能還有其他sweepId和他相連. /// 類似下面, A先掃描完, 形成了一個完整連續的region=1, 再遍歷B時, prevCount[1] = 4, (A行3個1和1個2), 但是sweeps[1] = 3, (B行3個1) /// 所以此時B行裡的1和A行裡的1不能合併了. 要給B行的1分配新的regionId /// /// <--- -x方向(左) /// | /// B: [1] [1] [1] [2] | -> 此處的1, 2都還是sweepId, 代表從左到右的掃描分割序號. /// A: [1] [1] [1] [1] [1] | -> 此時的1已經是regionId了. /// -z方向(下) /// /// /// B: [1] [1] | /// A: [1] [1] [1] | -> 這種情況可以合併, prevCount[A1].nei = 2, sweeps[B1].ns = 2 /// /// B: [1] [1] [1] [1] | /// A: [1] [1] [1] | -> 這種情況也可以合併, prevCount[A1].nei = 3, sweeps[B1].ns = 3, (B第四個[1]因為和下面無連線, 所以兩邊都不計數) /// /// If the neighbour is set and there is only one continuous connection to it, /// the sweep will be merged with the previous one, else new region is created. if (sweeps[i].nei != 0xff && prevCount[sweeps[i].nei] == (int)sweeps[i].ns) { sweeps[i].id = sweeps[i].nei; } else { if (regId == 255) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Region ID overflow."); return false; } sweeps[i].id = regId++; } } // 之前srcReg裡記錄的是sweepId, 現在改回regionId // Remap local sweep ids to region ids. for (int x = borderSize; x < w-borderSize; ++x) { const rcCompactCell& c = chf.cells[x+z*w]; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) { if (srcReg[i] != 0xff) srcReg[i] = sweeps[srcReg[i]].id; } } } // Allocate and init layer regions. const int nregs = (int)regId; rcScopedDelete<rcLayerRegion> regs((rcLayerRegion*)rcAlloc(sizeof(rcLayerRegion)*nregs, RC_ALLOC_TEMP)); if (!regs) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'regs' (%d).", nregs); return false; } memset(regs, 0, sizeof(rcLayerRegion)*nregs); for (int i = 0; i < nregs; ++i) { regs[i].layerId = 0xff; regs[i].ymin = 0xffff; regs[i].ymax = 0; } // Find region neighbours and overlapping regions. for (int z = 0; z < h; ++z) //遍歷 z { for (int x = 0; x < w; ++x) //遍歷 x/z 平面 { const rcCompactCell& c = chf.cells[x+z*w]; //記錄y方向的區域id和數量 unsigned char lregs[RC_MAX_LAYERS]; int nlregs = 0; for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) //遍歷 y 方向 span { const rcCompactSpan& s = chf.spans[i]; const unsigned char regionId = srcReg[i]; if (regionId == 0xff) continue; //跳過沒有區域的span regs[regionId].ymin = rcMin(regs[regionId].ymin, s.y); regs[regionId].ymax = rcMax(regs[regionId].ymax, s.y); // Collect all region layers. if (nlregs < RC_MAX_LAYERS) lregs[nlregs++] = regionId; // Update neighbours // 遍歷4個方向, 記錄鄰居區域資訊 (和自己不同區域) for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(s, dir) != RC_NOT_CONNECTED) { const int ax = x + rcGetDirOffsetX(dir); const int ay = z + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir); const unsigned char nrReg = srcReg[ai]; if (nrReg != 0xff && nrReg != regionId) //鄰居的region 和 自己不一樣 { // Don't check return value -- if we cannot add the neighbor // it will just cause a few more regions to be created, which // is fine. addUnique(regs[regionId].neis, regs[regionId].nneis, RC_MAX_NEIS, nrReg); } } } } // 兩層遍歷高度(y)方向的區域 (兩兩檢查), // Update overlapping regions. for (int i = 0; i < nlregs-1; ++i) { for (int j = i+1; j < nlregs; ++j) { if (lregs[i] != lregs[j]) { rcLayerRegion& ri = regs[lregs[i]]; rcLayerRegion& rj = regs[lregs[j]]; //在兩個region的layers裡記錄該region在x/z平面上重疊的其他高度的regionId. 用於索引高度上的不同層. if (!addUnique(ri.layers, ri.nlayers, RC_MAX_LAYERS, lregs[j]) || !addUnique(rj.layers, rj.nlayers, RC_MAX_LAYERS, lregs[i])) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS."); return false; } } } } } } // Create 2D layers from regions. unsigned char layerId = 0; static const int MAX_STACK = 64; unsigned char stack[MAX_STACK]; int nstack = 0; for (int i = 0; i < nregs; ++i) { rcLayerRegion& root = regs[i]; // Skip already visited. if (root.layerId != 0xff) continue; // Start search. // 分配 layerId root.layerId = layerId; root.base = 1; nstack = 0; stack[nstack++] = (unsigned char)i; //region序號入棧 while (nstack) { // Pop front rcLayerRegion& reg = regs[stack[0]]; nstack--; for (int j = 0; j < nstack; ++j) //移除stack第一個元素. stack[j] = stack[j+1]; const int nneis = (int)reg.nneis; for (int j = 0; j < nneis; ++j) { const unsigned char nei = reg.neis[j]; rcLayerRegion& nrReg = regs[nei]; // Skip already visited. if (nrReg.layerId != 0xff) continue; // Skip if the neighbour is overlapping root region. // 跳過 鄰居是x/z重疊的不同高度的region if (contains(root.layers, root.nlayers, nei)) continue; // Skip if the height range would become too large. // 如果兩個區域加起來的高度落差太大 跳過 (因為高度差不大的情況下會合並layer, 但是合併太多後會導致layer上下表面的高差越來越大, 這時候就要打斷合併了) const int ymin = rcMin(root.ymin, nrReg.ymin); const int ymax = rcMax(root.ymax, nrReg.ymax); if ((ymax - ymin) >= 255) continue; if (nstack < MAX_STACK) { // Deepen 鄰居入棧 stack[nstack++] = (unsigned char)nei; // Mark layer id // 將鄰居的layerId設定為自己的layerId. 合併成一個layer nrReg.layerId = layerId; // Merge current layers to root. // 將鄰居的高度layers也合併到自己的layers, (合併成一個layer了, 高度重疊區域資訊也要合併). for (int k = 0; k < nrReg.nlayers; ++k) { if (!addUnique(root.layers, root.nlayers, RC_MAX_LAYERS, nrReg.layers[k])) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS."); return false; } } root.ymin = rcMin(root.ymin, nrReg.ymin); // 更新合併後的layer上下表面. root.ymax = rcMax(root.ymax, nrReg.ymax); } } } layerId++; } // Merge non-overlapping regions that are close in height. // 合併高度上差異不大, 而且沒有重疊的區域, 樓梯, 坡等 const unsigned short mergeHeight = (unsigned short)walkableHeight * 4; for (int i = 0; i < nregs; ++i) { rcLayerRegion& ri = regs[i]; if (!ri.base) continue; //只需要查詢layer的 base region unsigned char newId = ri.layerId; for (;;) { unsigned char oldId = 0xff; for (int j = 0; j < nregs; ++j) //雙層遍歷 region 兩兩計算 { if (i == j) continue; rcLayerRegion& rj = regs[j]; if (!rj.base) continue; // Skip if the regions are not close to each other. // 兩個區域的上下表面+合併高差 不重疊, 則無法合併 if (!overlapRange(ri.ymin,ri.ymax+mergeHeight, rj.ymin,rj.ymax+mergeHeight)) continue; // Skip if the height range would become too large. const int ymin = rcMin(ri.ymin, rj.ymin); const int ymax = rcMax(ri.ymax, rj.ymax); if ((ymax - ymin) >= 255) //合併後高差太大 跳過 continue; // Make sure that there is no overlap when merging 'ri' and 'rj'. bool overlap = false; // Iterate over all regions which have the same layerId as 'rj' for (int k = 0; k < nregs; ++k) { if (regs[k].layerId != rj.layerId) continue; // Check if region 'k' is overlapping region 'ri' // Index to 'regs' is the same as region id. // 和j相同layerId的區域, 判斷是否和ri有重疊, 如果有重疊說明合併regionI 和 regionJ 後會導致用一個region在x/z平面出現重疊. 所以此時要break. 不能合併 if (contains(ri.layers,ri.nlayers, (unsigned char)k)) { overlap = true; break; } } // Cannot merge of regions overlap. if (overlap) continue; // Can merge i and j. oldId = rj.layerId; break; } // Could not find anything to merge with, stop. if (oldId == 0xff) break; // Merge for (int j = 0; j < nregs; ++j) { rcLayerRegion& rj = regs[j]; if (rj.layerId == oldId) { rj.base = 0; // Remap layerIds. rj.layerId = newId; // Add overlaid layers from 'rj' to 'ri'. // 合併之後, 同樣也需要 將鄰居的高度layers也合併到自己的layers, (合併成一個layer了, 高度重疊區域資訊也要合併). for (int k = 0; k < rj.nlayers; ++k) { if (!addUnique(ri.layers, ri.nlayers, RC_MAX_LAYERS, rj.layers[k])) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS."); return false; } } // Update height bounds. ri.ymin = rcMin(ri.ymin, rj.ymin); // 更新合併後的layer上下表面. ri.ymax = rcMax(ri.ymax, rj.ymax); } } } } // 合併後layerId不連續了, 所以這裡要重新remap下, 保持layerId連續 // Compact layerIds unsigned char remap[256]; memset(remap, 0, 256); // Find number of unique layers. layerId = 0; for (int i = 0; i < nregs; ++i) remap[regs[i].layerId] = 1; for (int oldLayerId = 0; oldLayerId < 256; ++oldLayerId) { if (remap[oldLayerId]) remap[oldLayerId] = layerId++; else remap[oldLayerId] = 0xff; } // Remap ids. for (int i = 0; i < nregs; ++i) regs[i].layerId = remap[regs[i].layerId]; //從remap裡查詢oldLayerId對應的新layerId, 並賦值 // No layers, return empty. if (layerId == 0) return true; // Create layers. rcAssert(lset.layers == 0); const int lw = w - borderSize*2; const int lh = h - borderSize*2; // Build contracted bbox for layers. float bmin[3], bmax[3]; rcVcopy(bmin, chf.bmin); rcVcopy(bmax, chf.bmax); bmin[0] += borderSize*chf.cs; bmin[2] += borderSize*chf.cs; bmax[0] -= borderSize*chf.cs; bmax[2] -= borderSize*chf.cs; lset.nlayers = (int)layerId; lset.layers = (rcHeightfieldLayer*)rcAlloc(sizeof(rcHeightfieldLayer)*lset.nlayers, RC_ALLOC_PERM); if (!lset.layers) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'layers' (%d).", lset.nlayers); return false; } memset(lset.layers, 0, sizeof(rcHeightfieldLayer)*lset.nlayers); // Store layers. for (int i = 0; i < lset.nlayers; ++i) { unsigned char curId = (unsigned char)i; rcHeightfieldLayer* layer = &lset.layers[curId]; const int gridSize = sizeof(unsigned char)*lw*lh; //體素x/z空間size, 二維陣列長度 layer->heights = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM); if (!layer->heights) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'heights' (%d).", gridSize); return false; } memset(layer->heights, 0xff, gridSize); layer->areas = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM); if (!layer->areas) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'areas' (%d).", gridSize); return false; } memset(layer->areas, 0, gridSize); layer->cons = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM); if (!layer->cons) { ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'cons' (%d).", gridSize); return false; } memset(layer->cons, 0, gridSize); // Find layer height bounds. int hmin = 0, hmax = 0; //上下表面高度 (體素單位) for (int j = 0; j < nregs; ++j) { if (regs[j].base && regs[j].layerId == curId) { hmin = (int)regs[j].ymin; hmax = (int)regs[j].ymax; //此處應該可以break ? } } layer->width = lw; layer->height = lh; layer->cs = chf.cs; layer->ch = chf.ch; // Adjust the bbox to fit the heightfield. rcVcopy(layer->bmin, bmin); rcVcopy(layer->bmax, bmax); layer->bmin[1] = bmin[1] + hmin*chf.ch; //體素高度轉座標高度 layer->bmax[1] = bmin[1] + hmax*chf.ch; layer->hmin = hmin; layer->hmax = hmax; // Update usable data region. layer->minx = layer->width; layer->maxx = 0; layer->miny = layer->height; layer->maxy = 0; // Copy height and area from compact heightfield. for (int z = 0; z < lh; ++z) { for (int x = 0; x < lw; ++x) { const int cx = borderSize+x; const int cz = borderSize+z; const rcCompactCell& c = chf.cells[cx+cz*w]; for (int j = (int)c.index, nj = (int)(c.index+c.count); j < nj; ++j) { const rcCompactSpan& span = chf.spans[j]; // Skip unassigned regions. if (srcReg[j] == 0xff) continue; // Skip of does nto belong to current layer. unsigned char lid = regs[srcReg[j]].layerId; if (lid != curId) continue; // Update data bounds. layer->minx = rcMin(layer->minx, x); layer->maxx = rcMax(layer->maxx, x); layer->miny = rcMin(layer->miny, z); layer->maxy = rcMax(layer->maxy, z); // Store height and area type. const int idx = x+z*lw; layer->heights[idx] = (unsigned char)(span.y - hmin); layer->areas[idx] = chf.areas[j]; // Check connection. unsigned char portal = 0; unsigned char con = 0; for (int dir = 0; dir < 4; ++dir) { if (rcGetCon(span, dir) != RC_NOT_CONNECTED) { const int ax = cx + rcGetDirOffsetX(dir); const int ay = cz + rcGetDirOffsetY(dir); const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(span, dir); unsigned char alid = srcReg[ai] != 0xff ? regs[srcReg[ai]].layerId : 0xff; // Portal mask if (chf.areas[ai] != RC_NULL_AREA && lid != alid) { portal |= (unsigned char)(1<<dir); // Update height so that it matches on both sides of the portal. const rcCompactSpan& as = chf.spans[ai]; if (as.y > hmin) layer->heights[idx] = rcMax(layer->heights[idx], (unsigned char)(as.y - hmin)); } // Valid connection mask // 相鄰的同layer的span連線資訊記錄在 cons的低4位. (上下左右) if (chf.areas[ai] != RC_NULL_AREA && lid == alid) { const int nx = ax - borderSize; const int ny = ay - borderSize; if (nx >= 0 && ny >= 0 && nx < lw && ny < lh) con |= (unsigned char)(1<<dir); } } } layer->cons[idx] = (portal << 4) | con; //相鄰的不同layer的資訊記錄在cons的高4位. } } } if (layer->minx > layer->maxx) layer->minx = layer->maxx = 0; if (layer->miny > layer->maxy) layer->miny = layer->maxy = 0; } return true; }
有兩個文件也可以看一下, 看懂了上面的程式碼再去看文章就清楚多了. 如果不好理解程式碼. 可以結合文章圖例一起看. 程式碼註釋已經非常詳細了, 只是沒有圖例
https://blog.csdn.net/zstu_zy/article/details/97247013
https ://www.jianshu.com/p/f6cd9b7696f6