Last active
March 7, 2024 15:10
-
-
Save t-mat/c4cba4c6c3a506a2b6f11d24f0a80bac to your computer and use it in GitHub Desktop.
Martin Roberts' R2 sequence for dithering in HLSL
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
// Martin Roberts' R2 sequence in HLSL | |
// http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ | |
namespace MRR2 { | |
float triangularWave(float x) { | |
return abs(0.5f - x) * -2.0f + 1.0f; | |
} | |
static const float a1 = (0.6180339887498948); | |
static const float2 a2 = float2(0.7548776662466927, 0.5698402909980532); | |
static const float3 a3 = float3(0.8191725133961644, 0.671043606703789, 0.5497004779019701); | |
static const float4 a4 = float4(0.8566748838545029, 0.733891856627126, 0.6287067210378086, 0.53859725722361); | |
float computeRawFloatFrom(float i) { return frac(dot(i, a1)); } | |
float computeRawFloatFrom(float2 i) { return frac(dot(i, a2)); } | |
float computeRawFloatFrom(float3 i) { return frac(dot(i, a3)); } | |
float computeRawFloatFrom(float4 i) { return frac(dot(i, a4)); } | |
float computeFloatFrom(float i) { return triangularWave(computeRawFloatFrom(i)); } | |
float computeFloatFrom(float2 i) { return triangularWave(computeRawFloatFrom(i)); } | |
float computeFloatFrom(float3 i) { return triangularWave(computeRawFloatFrom(i)); } | |
float computeFloatFrom(float4 i) { return triangularWave(computeRawFloatFrom(i)); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment