public static Color NearestNeighbour(Flat2DArray <Color> colors, Vector2 uv) { int x = (int)uv.x; int y = (int)uv.y; while (x < 0) { x += colors.width; } while (x >= colors.width) { x -= colors.width; } while (y < 0) { y += colors.height; } while (y >= colors.height) { y -= colors.height; } return(colors[x, y]); }
// todo: UV parameter should be renamed to 'coordinate' since it's not in normalized space public static Color Bicubic(Flat2DArray <Color> colors, Vector2 uv, bool snapToNearest) { while (uv.x < 0.0f) { uv.x += colors.width; } while (uv.x >= colors.width) { uv.x -= colors.width; } while (uv.y < 0.0f) { uv.y += colors.height; } while (uv.y >= colors.height) { uv.y -= colors.height; } int x1 = (int)uv.x, x0 = x1.Ring(colors.width, -1), x2 = x1.Ring(colors.width, 1), x3 = x1.Ring(colors.width, 2); int y1 = (int)uv.y, y0 = y1.Ring(colors.height, -1), y2 = y1.Ring(colors.height, 1), y3 = y1.Ring(colors.height, 2); Vector2 t = new Vector2(uv.x - x1, uv.y - y1); Color h0 = CatmullRom(colors[x0, y0], colors[x0, y1], colors[x0, y2], colors[x0, y3], t.y, false); Color h1 = CatmullRom(colors[x1, y0], colors[x1, y1], colors[x1, y2], colors[x1, y3], t.y, false); Color h2 = CatmullRom(colors[x2, y0], colors[x2, y1], colors[x2, y2], colors[x2, y3], t.y, false); Color h3 = CatmullRom(colors[x3, y0], colors[x3, y1], colors[x3, y2], colors[x3, y3], t.y, false); Color result = CatmullRom(h0, h1, h2, h3, t.x, false); if (!snapToNearest) { return(result); } return(new[] { colors[x1, y1], colors[x2, y1], colors[x1, y2], colors[x2, y2] } .MinBy(c => Vector4.Distance(result, c))); }