-
-
Save disouzam/2f2e4a7470907ade69bbd27bf5ddba4b to your computer and use it in GitHub Desktop.
A demonstration of Halton Sequencing Quasirandom Noise
This file contains hidden or 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 System.Collections.Generic; | |
| using UnityEngine; | |
| // Note that the Halton bases must consist of coprime integers. | |
| // Learn more about the Halton sequence: https://en.wikipedia.org/wiki/Halton_sequence | |
| // Learn more about low discrepancy sequences: https://en.wikipedia.org/wiki/Low-discrepancy_sequence | |
| // Learn more about coprime numbers: https://en.wikipedia.org/wiki/Coprime_integers | |
| public class HaltonSequencer : MonoBehaviour | |
| { | |
| [Header("Texture Generator")] | |
| public bool generateHaltonTexture; | |
| [Header("Demo")] | |
| public bool generateHaltonSamples; | |
| public float rate = 0.2f; | |
| public GameObject haltonSample; | |
| private float t = 0; | |
| private int index = 1; | |
| public float boxDimension = 5f; | |
| private void OnValidate() | |
| { | |
| if (generateHaltonTexture) | |
| { | |
| GenerateHaltonTexture(); | |
| generateHaltonTexture = false; | |
| } | |
| if (generateHaltonSamples) | |
| { | |
| t = Time.time; | |
| } | |
| } | |
| private void Update() | |
| { | |
| if (generateHaltonSamples) | |
| { | |
| if(Time.time - t > rate) | |
| { | |
| float x = (Halton(2, index) - 0.5f) * boxDimension; | |
| float y = (Halton(3, index) - 0.5f) * boxDimension; | |
| float z = (Halton(5, index) - 0.5f) * boxDimension; | |
| Instantiate(haltonSample, new Vector3(x, z, y), Quaternion.identity); | |
| index++; | |
| t = Time.time; | |
| } | |
| } | |
| } | |
| private float Halton(int b, int index) | |
| { | |
| float result = 0.0f; | |
| float f = 1.0f; | |
| while (index > 0) | |
| { | |
| f = f / (float)b; | |
| result += f * (float)(index % b); | |
| index = index / b; | |
| } | |
| return result; | |
| } | |
| private void GenerateHaltonTexture(int baseR = 2, int baseG = 3, int baseB = 5, int baseA = 7, int maxIndex = 256) | |
| { | |
| Texture2D t = new Texture2D(maxIndex, 1, TextureFormat.RGBA32, false); | |
| Color[] c = new Color[maxIndex]; | |
| for (int x = 0; x < maxIndex; x++) | |
| { | |
| float r = Halton(baseR, x + 30); | |
| float g = Halton(baseG, x + 30); | |
| float b = Halton(baseB, x + 30); | |
| float a = Halton(baseA, x + 30); | |
| c[x] = new Color(r, g, b, a); | |
| } | |
| t.SetPixels(c); | |
| t.Apply(); | |
| byte[] encodedTexture = t.EncodeToPNG(); | |
| System.IO.File.WriteAllBytes(Application.dataPath + "/halton.png", encodedTexture); | |
| } | |
| private void OnGUI() | |
| { | |
| GUI.Label(new Rect(0, 0, 300, 100), "Index: " + index); | |
| } | |
| private void OnDrawGizmos() | |
| { | |
| Gizmos.DrawWireCube(Vector3.zero, Vector3.one * 5); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment