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[JUtilities.To1DIndex(xFloor, yFloor, width)]; Color f01 = textureData[JUtilities.To1DIndex(xFloor, yCeil, width)]; Color f10 = textureData[JUtilities.To1DIndex(xCeil, yFloor, width)]; Color f11 = textureData[JUtilities.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[JUtilities.To1DIndex(xFloor, yFloor, width)]; float f01 = data[JUtilities.To1DIndex(xFloor, yCeil, width)]; float f10 = data[JUtilities.To1DIndex(xCeil, yFloor, width)]; float f11 = data[JUtilities.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[JUtilities.To1DIndex(x, y, width)] = c; } } t.filterMode = FilterMode.Bilinear; t.SetPixels(colors); t.Apply(); return(t); }
private void ExportFace(Cubemap cube, CubemapFace face) { Texture2D tex = new Texture2D(cube.width, cube.height); Color[] data = cube.GetPixels(face); Color[] flipData = new Color[data.Length]; for (int y = 0; y < Resolution; ++y) { for (int x = 0; x < Resolution; ++x) { flipData[JUtilities.To1DIndex(x, y, Resolution)] = data[JUtilities.To1DIndex(Resolution - 1 - x, Resolution - 1 - y, Resolution)]; } } tex.SetPixels(flipData); tex.Apply(); byte[] bytes = tex.EncodeToPNG(); string fileName = Path.Combine(Directory, cube.name + "-" + face.ToString() + ".png"); File.WriteAllBytes(fileName, bytes); JUtilities.DestroyObject(tex); }
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[JUtilities.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 = JUtilities.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); }