private static void QuickSort(TexturePixelArray pixels, int lowIdx, int highIdx, IComparer <Color> pixelComparer) { int i = lowIdx, j = highIdx; Color pivot = pixels[(lowIdx + highIdx) / 2]; while (i <= j) { while (pixelComparer.Compare(pixels[i], pivot) < 0) { i++; } while (pixelComparer.Compare(pixels[j], pivot) > 0) { j--; } if (i <= j) { pixels.Swap(i, j); i++; j--; } } if (lowIdx < j) { QuickSort(pixels, lowIdx, j, pixelComparer); } if (i < highIdx) { QuickSort(pixels, i, highIdx, pixelComparer); } }
private static Vector3 FindAxisOfGreatestDeviation(TexturePixelArray pixels) { Vector3 centroid = AverageXYZ(pixels); Vector3 farthestPoint = FindFarthestPoint(pixels, centroid); return(farthestPoint.normalized); }
private static Vector3 AverageXYZ(TexturePixelArray pixels) { Vector3 sum = Vector3.zero; for (int k = 0; k < pixels.Count; k++) { sum += Util.RGBtoXYZ(pixels[k]); } return(sum / (float)pixels.Count); }
private static bool VerifySort(TexturePixelArray pixels, IComparer <Color> pixelComparer) { for (int i = 0; i < pixels.Count - 1; i++) { if (pixelComparer.Compare(pixels[i], pixels[i + 1]) > 0) { return(false); } } return(true); }
public static void SortTexture(Texture2D tex) { Color[] pixelsArr = tex.GetPixels(); TexturePixelArray pixels = new TexturePixelArray(pixelsArr, tex.width); SortTexturePixelArray(pixels, Vector3.right); tex.SetPixels(pixelsArr); tex.Apply(false, false); }
private static Vector3 FindFarthestPoint(TexturePixelArray pixels, Vector3 fromPoint) { Vector3 farthestPoint = Util.RGBtoXYZ(pixels[0]); float farthestSqDist = Util.SquareDistance(farthestPoint, fromPoint); for (int k = 1; k < pixels.Count; k++) { Vector3 testPoint = Util.RGBtoXYZ(pixels[k]); float testSqDist = Util.SquareDistance(farthestPoint, testPoint); if (testSqDist > farthestSqDist) { farthestSqDist = testSqDist; farthestPoint = testPoint; } } return(farthestPoint); }
/// <summary> Splits the TexturePixelArray into two halves, horizontally /// if the indexingOrder is RowByRow, vertically otherwise. The two returned /// TexturePixelArray objects reference the same underlying pixel array.</summary> public void Bisect(out TexturePixelArray half0, out TexturePixelArray half1) { IntRect half0Rect, half1Rect; if (indexingOrder == IndexingOrder.RowByRow) // two long rectangles { half0Rect = new IntRect(X, Y, Width, Height / 2); half1Rect = new IntRect(X, Height / 2 + Y, Width, Height / 2); } else // ColumnByColumn, two thin rectangles { half0Rect = new IntRect(X, Y, Width / 2, Height); half1Rect = new IntRect(Width / 2 + X, Y, Width / 2, Height); } half0 = new TexturePixelArray(_pixels, _texWidth, half0Rect, indexingOrder: _indexingOrder); half1 = new TexturePixelArray(_pixels, _texWidth, half1Rect, indexingOrder: _indexingOrder); }
private static void SortTexturePixelArray(TexturePixelArray pixels, Vector3 sortingAxis) { QuickSort(pixels, new AxisComparer(sortingAxis)); // Bisection & recursion TexturePixelArray half0, half1; pixels.Bisect(out half0, out half1); if (half0.Count >= 2) { half0.FlipIndexingOrder(); SortTexturePixelArray(half0, NextSortingAxis(sortingAxis)); } if (half1.Count >= 2) { half1.FlipIndexingOrder(); SortTexturePixelArray(half1, NextSortingAxis(sortingAxis)); } }
//float angleVel = 1F; //void Update() { // this.transform.Rotate(Vector3.up * angleVel); //} private Texture2D GenerateParticles(int sqRootWidth) { TexturePixelArray particles = new TexturePixelArray(new Color[sqRootWidth * sqRootWidth], sqRootWidth); for (int i = 0; i < particles.Width; i++) { for (int j = 0; j < particles.Height; j++) { Vector3 rV = Random.insideUnitSphere; particles[i, j] = new Sphere(Vector3.one * 0.5F + (rV * 0.8F), 0.005F); } } Texture2D tex = new Texture2D(sqRootWidth, sqRootWidth, TextureFormat.RGBAFloat, true); tex.filterMode = FilterMode.Point; tex.SetPixels(particles.Pixels); tex.Apply(false, false); return(tex); }
private static void QuickSort(TexturePixelArray pixels, IComparer <Color> pixelComparer) { QuickSort(pixels, 0, pixels.Count - 1, pixelComparer); }
/// <summary> Recursively sorts the underlying TexturePixelArray array /// into an optimized arrangement for quadtrees by sorting and recursively /// re-sorting half partitions horizontally and vertically by that partition's /// axis of greatest difference from the partition centroid. </summary> private static void SortTexturePixelArray(TexturePixelArray pixels) { Vector3 sortingAxis = FindAxisOfGreatestDeviation(pixels); SortTexturePixelArray(pixels, sortingAxis); }