bool BuildHoneycomb() { Vector3[] vertex = null; int[] f = null; NavMesh.Triangulate(out vertex, out f); if (f == null) { return(false); } CalcBound(vertex); LayerHeight = (bound_max.y - bound_min.y) / iLayer; iLine = (int)((bound_max.x - bound_min.x) / fScale) + 1; iRow = (int)((bound_max.z - bound_min.z) / honeycombLine) + 1; Count = iLine * iRow; BuildHoneycombMesh(); navData = new byte[Count]; int iFaceCount = f.Length / 3; //遍历每个三角形 逐个注入到双层的2维纹理中.. for (int i = 0; i < iFaceCount; i++) { Vector3[] v = new Vector3[3]; v[0] = vertex[f[i * 3]]; //计算每个三角形的 包围盒 只计算XZ.. Vector2 vmin = new Vector2(v[0].x, v[0].z); Vector2 vmax = vmin; for (int j = 1; j < 3; j++) { v[j] = vertex[f[i * 3 + j]]; Vector2 vTemp = new Vector2(v[j].x, v[j].z); if (vmin.x > vTemp.x) { vmin.x = vTemp.x; } if (vmin.y > vTemp.y) { vmin.y = vTemp.y; } if (vmax.x < vTemp.x) { vmax.x = vTemp.x; } if (vmax.y < vTemp.y) { vmax.y = vTemp.y; } } //将包围盒转化为 索引值.. int minx = (int)((vmin.x - bound_min.x) / fScale); int miny = (int)((vmin.y - bound_min.z) / honeycombLine); int maxx = (int)((vmax.x - bound_min.x) / fScale); int maxy = (int)((vmax.y - bound_min.z) / honeycombLine); //最最低索引开始 发射2维射线与三角形求交 并得到 相交部分的高度值.. for (int j = minx; j <= maxx + 1; j++) { if (j >= iLine) { continue; } for (int k = miny; k <= maxy + 1; k++) { if (k >= iRow) { continue; } float xoffset = 0.0f; if ((k & 1) == 1) { xoffset = fScale * 0.5f; } float x = j * fScale + bound_min.x + xoffset; float z = k * honeycombLine + bound_min.z; Ray r = new Ray(new Vector3(x, bound_max.y + 1.0f, z), new Vector3(0, -1, 0)); float dis = 100000.0f; if (FingerGesturesInitializer.Ray_Triangle(r, v[0], v[1], v[2], ref dis)) { float height = bound_max.y + 1.0f - dis; //uint h = (uint)((height - bound_min.y) / LayerHeight); //SaveVoxel(heightArray, j, k, h); //Debug.Log(j + " " + k); //if (h >= 63) //{ // h = 63; //} //navData[k * iLine + j] = (byte)( 0x80+h); Vector3 ver = RenderVertex[k * iLine + j]; ver.y = height - bound_min.y; RenderVertex[k * iLine + j] = ver; } } } } return(true); }
// 从已有的行走面片构建Hexagon数据aa void BuildHexagonData(Vector3[] vertex, int[] face) { int iFaceCount = face.Length / 3; //遍历每个三角形 逐个注入到双层的2维纹理中.. for (int i = 0; i < iFaceCount; i++) { Vector3[] v = new Vector3[3]; v[0] = vertex[face[i * 3]]; //计算每个三角形的 包围盒 只计算XZ.. Vector2 vmin = new Vector2(v[0].x, v[0].z); Vector2 vmax = vmin; for (int j = 1; j < 3; j++) { v[j] = vertex[face[i * 3 + j]]; Vector2 vTemp = new Vector2(v[j].x, v[j].z); if (vmin.x > vTemp.x) { vmin.x = vTemp.x; } if (vmin.y > vTemp.y) { vmin.y = vTemp.y; } if (vmax.x < vTemp.x) { vmax.x = vTemp.x; } if (vmax.y < vTemp.y) { vmax.y = vTemp.y; } } //将包围盒转化为 索引值.. int minx = (int)((vmin.x - bound_min.x) / fXStep); int miny = (int)((vmin.y - bound_min.z) / fZStep); int maxx = (int)((vmax.x - bound_min.x) / fXStep); int maxy = (int)((vmax.y - bound_min.z) / fZStep); //最最低索引开始 发射2维射线与三角形求交 并得到 相交部分的高度值.. for (int x = minx; x <= maxx + 1; x++) { if (x >= xCount) { continue; } for (int z = miny; z <= maxy + 1; z++) { if (z >= zCount) { continue; } float xoffset = 0.0f; if ((z & 1) == 1) { xoffset = fXStep * 0.5f; } float fx = x * fXStep + bound_min.x + xoffset; float fz = z * fZStep + bound_min.z; Ray r = new Ray(new Vector3(fx, bound_max.y + 1.0f, fz), new Vector3(0, -1, 0)); float dis = 100000.0f; if (FingerGesturesInitializer.Ray_Triangle(r, v[0], v[1], v[2], ref dis)) { float height = bound_max.y + 1.0f - dis; uint h = (uint)((height - bound_min.y) / fYLayerHeight); if (h >= Tile.HeightMask) { h = Tile.HeightMask; } SavePoint(x, z, h); } } } } }