private void PaintDistances(TriGrid triGrid, Color[] vertexDistanceColors) { float halfSideSize = sideSize / 2; int halfSideSizeInt = (int)halfSideSize; float height = TriangleHeight; float halfHeight = height / 2.0f; Vector2 texPoint = new Vector2(); for (int y = 0; y != tex.height; ++y) { int row = (int)(((float)(tex.height - y)) / height); // y texture to grid row texPoint.y = y + 0.5f; int centerY = tex.height - ((int)(halfHeight + row * height)); float north = centerY + halfHeight + 0.5f; float south = north - height - 0.5f; for (int x = 0; x != tex.width; ++x) { Color color = backgroundColor; texPoint.x = x + 0.5f; int halfSizeCount = (int)(x / halfSideSize); int fromCol = Mathf.Max(0, halfSizeCount - 1); int maxCol = Mathf.Min(halfSizeCount + 2, triGrid.width); for (int col = fromCol; col != maxCol; ++col) { int vertex = triGrid.PositionToVertex(row, col); int centerX = halfSideSizeInt + col * halfSideSizeInt; int west = Mathf.Max(0, centerX - halfSideSizeInt); int east = centerX + halfSideSizeInt; Vector2 a = new Vector2(); Vector2 b = new Vector2(); Vector2 c = new Vector2(); if (triGrid.IsUpright(vertex)) { a.Set(centerX + 0.5f, north); b.Set(east + 0.5f, south); c.Set(west + 0.5f, south); } else { a.Set(centerX + 0.5f, south); b.Set(west + 0.5f, north); c.Set(east + 0.5f, north); } if (PointTest2D.IsInsideTri(texPoint, a, b, c)) { color = vertexDistanceColors[vertex]; break; } } tex.SetPixel(x, y, color); } } }