Created
November 22, 2022 18:02
-
-
Save jostly/4aabc384aa810cf7c3945984b6d2e269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
namespace Sorter | |
{ | |
[RequireComponent(typeof(MeshRenderer))] | |
public class RandomTexture : MonoBehaviour | |
{ | |
[SerializeField] private Vector2Int textureDimension = new Vector2Int(128, 128); | |
private Color32[] colors; | |
private float[] hues; | |
private float[] values; | |
private Material material; | |
private Texture2D texture; | |
private void Start() | |
{ | |
Random.InitState(123123132); | |
RandomizeTexture(); | |
} | |
private void RandomizeTexture() | |
{ | |
material = GetComponent<MeshRenderer>().material; | |
texture = new Texture2D(textureDimension.x, textureDimension.y) | |
{ | |
filterMode = FilterMode.Point, | |
wrapMode = TextureWrapMode.Clamp | |
}; | |
var pixels = textureDimension.x * textureDimension.y; | |
colors = new Color32[pixels]; | |
hues = new float[pixels]; | |
values = new float[pixels]; | |
for (var i = colors.Length - 1; i >= 0; i--) | |
{ | |
hues[i] = Random.value; | |
values[i] = Random.value; | |
colors[i] = Color.HSVToRGB(hues[i], 1, values[i]); | |
} | |
UpdateTexture(); | |
StartCoroutine(SortTexture()); | |
} | |
private IEnumerator SortTexture() | |
{ | |
var width = texture.width; | |
var height = texture.height; | |
yield return null; | |
var timeStart = Time.time; | |
for (var b = 0; b < width - 1; b++) | |
{ | |
var changed = false; | |
for (var y = 0; y < height; y++) | |
{ | |
var ibase = y * width; | |
for (var x0 = 0; x0 < width - 1; x0++) | |
{ | |
var i0 = x0 + ibase; | |
for (var x1 = x0 + 1; x1 < width; x1++) | |
{ | |
var i1 = x1 + ibase; | |
if (hues[i1] >= hues[i0]) continue; | |
(hues[i0], hues[i1]) = (hues[i1], hues[i0]); | |
(values[i0], values[i1]) = (values[i1], values[i0]); | |
(colors[i0], colors[i1]) = (colors[i1], colors[i0]); | |
changed = true; | |
} | |
} | |
} | |
UpdateTexture(); | |
yield return null; | |
for (var x = 0; x < width; x++) | |
{ | |
for (var y0 = 0; y0 < height - 1; y0++) | |
{ | |
var i0 = y0 * width + x; | |
for (var y1 = y0 + 1; y1 < height; y1++) | |
{ | |
var i1 = y1 * width + x; | |
if (values[i1] >= values[i0]) continue; | |
(hues[i0], hues[i1]) = (hues[i1], hues[i0]); | |
(values[i0], values[i1]) = (values[i1], values[i0]); | |
(colors[i0], colors[i1]) = (colors[i1], colors[i0]); | |
changed = true; | |
} | |
} | |
} | |
UpdateTexture(); | |
yield return null; | |
if (!changed) | |
{ | |
Debug.Log($"Breaking at {b} / {width - 1} because of stable solution"); | |
break; | |
} | |
} | |
var timeEnd = Time.time; | |
Debug.Log($"Sorting complete in {timeEnd - timeStart} seconds"); | |
} | |
private void UpdateTexture() | |
{ | |
texture.SetPixels32(colors); | |
texture.Apply(); | |
material.mainTexture = texture; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment