public static Color GetColorBilinear(Color[] textureData, int width, int height, Vector2 uv) { Color color = Color.clear; Vector2 pixelCoord = new Vector2( Mathf.Lerp(0, width - 1, uv.x), Mathf.Lerp(0, height - 1, uv.y)); //apply a bilinear filter int xFloor = Mathf.FloorToInt(pixelCoord.x); int xCeil = Mathf.CeilToInt(pixelCoord.x); int yFloor = Mathf.FloorToInt(pixelCoord.y); int yCeil = Mathf.CeilToInt(pixelCoord.y); Color f00 = textureData[PUtilities.To1DIndex(xFloor, yFloor, width)]; Color f01 = textureData[PUtilities.To1DIndex(xFloor, yCeil, width)]; Color f10 = textureData[PUtilities.To1DIndex(xCeil, yFloor, width)]; Color f11 = textureData[PUtilities.To1DIndex(xCeil, yCeil, width)]; Vector2 unitCoord = new Vector2( pixelCoord.x - xFloor, pixelCoord.y - yFloor); color = f00 * (1 - unitCoord.x) * (1 - unitCoord.y) + f01 * (1 - unitCoord.x) * unitCoord.y + f10 * unitCoord.x * (1 - unitCoord.y) + f11 * unitCoord.x * unitCoord.y; return(color); }
public static float GetValueBilinear(float[] data, int width, int height, Vector2 uv) { float value = 0; Vector2 pixelCoord = new Vector2( Mathf.Lerp(0, width - 1, uv.x), Mathf.Lerp(0, height - 1, uv.y)); //apply a bilinear filter int xFloor = Mathf.FloorToInt(pixelCoord.x); int xCeil = Mathf.CeilToInt(pixelCoord.x); int yFloor = Mathf.FloorToInt(pixelCoord.y); int yCeil = Mathf.CeilToInt(pixelCoord.y); float f00 = data[PUtilities.To1DIndex(xFloor, yFloor, width)]; float f01 = data[PUtilities.To1DIndex(xFloor, yCeil, width)]; float f10 = data[PUtilities.To1DIndex(xCeil, yFloor, width)]; float f11 = data[PUtilities.To1DIndex(xCeil, yCeil, width)]; Vector2 unitCoord = new Vector2( pixelCoord.x - xFloor, pixelCoord.y - yFloor); value = f00 * (1 - unitCoord.x) * (1 - unitCoord.y) + f01 * (1 - unitCoord.x) * unitCoord.y + f10 * unitCoord.x * (1 - unitCoord.y) + f11 * unitCoord.x * unitCoord.y; return(value); }
public static Texture2D CreateTextureFromCurve(AnimationCurve curve, int width, int height) { Texture2D t = new Texture2D(width, height, TextureFormat.ARGB32, false); t.wrapMode = TextureWrapMode.Clamp; Color[] colors = new Color[width * height]; for (int x = 0; x < width; ++x) { float f = Mathf.InverseLerp(0, width - 1, x); float value = curve.Evaluate(f); Color c = new Color(value, value, value, value); for (int y = 0; y < height; ++y) { colors[PUtilities.To1DIndex(x, y, width)] = c; } } t.filterMode = FilterMode.Bilinear; t.SetPixels(colors); t.Apply(); return(t); }
private void FillLine(int lineIndex, List <float> intersectX) { intersectX.Sort(); List <int> columnIndices = new List <int>(); for (int i = 0; i < intersectX.Count; ++i) { columnIndices.Add((int)(intersectX[i] * MASK_RESOLUTION)); } int pairCount = columnIndices.Count / 2; for (int p = 0; p < pairCount; ++p) { int c0 = columnIndices[p * 2 + 0]; int c1 = columnIndices[p * 2 + 1]; for (int c = c0; c <= c1; ++c) { mask[PUtilities.To1DIndex(c, lineIndex, MASK_RESOLUTION)] = 1.0f; } } }
public static IEnumerable <Rect> CompareHeightMap(int gridSize, Color[] oldValues, Color[] newValues) { if (oldValues.LongLength != newValues.LongLength) { return(new Rect[1] { new Rect(0, 0, 1, 1) }); } Rect[] rects = new Rect[gridSize * gridSize]; for (int x = 0; x < gridSize; ++x) { for (int z = 0; z < gridSize; ++z) { rects[PUtilities.To1DIndex(x, z, gridSize)] = GetUvRange(gridSize, x, z); } } HashSet <Rect> dirtyRects = new HashSet <Rect>(); int index = 0; int resolution = Mathf.RoundToInt(Mathf.Sqrt(newValues.LongLength)); for (int rectIndex = 0; rectIndex < rects.Length; ++rectIndex) { Rect r = rects[rectIndex]; int startX = (int)Mathf.Lerp(0, resolution - 1, r.min.x); int startY = (int)Mathf.Lerp(0, resolution - 1, r.min.y); int endX = (int)Mathf.Lerp(0, resolution - 1, r.max.x); int endY = (int)Mathf.Lerp(0, resolution - 1, r.max.y); for (int x = startX; x <= endX; ++x) { for (int y = startY; y <= endY; ++y) { index = PUtilities.To1DIndex(x, y, resolution); if (oldValues[index].r == newValues[index].r && oldValues[index].g == newValues[index].g && oldValues[index].b == newValues[index].b && oldValues[index].a == newValues[index].a) { continue; } dirtyRects.Add(r); Rect hRect = new Rect(); hRect.size = new Vector2(r.width * 1.2f, r.height); hRect.center = r.center; dirtyRects.Add(hRect); Rect vRect = new Rect(); vRect.size = new Vector2(r.width, r.height * 1.2f); vRect.center = r.center; dirtyRects.Add(vRect); break; } if (dirtyRects.Contains(r)) { break; } } } return(dirtyRects); }