Last active
September 14, 2018 07:39
-
-
Save vl4dimir/7721af04fc558d8379cfeeff897e3419 to your computer and use it in GitHub Desktop.
A computationally cheap version of smooth 3D noise, 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
float random(float3 seed) { | |
float result = frac(sin(dot(seed, float3(12.9898, 78.233, 49.0154))) * 43758.5453123); | |
return result; | |
} | |
// Returns a smoothly varying value in the [0, 1] range for the given 3D position. | |
float simpleNoise(float3 position) { | |
float3 integers = floor(position); | |
float random000 = random(integers); | |
float random001 = random(integers + float3(0.0, 0.0, 1.0)); | |
float random010 = random(integers + float3(0.0, 1.0, 0.0)); | |
float random011 = random(integers + float3(0.0, 1.0, 1.0)); | |
float random100 = random(integers + float3(1.0, 0.0, 0.0)); | |
float random101 = random(integers + float3(1.0, 0.0, 1.0)); | |
float random110 = random(integers + float3(1.0, 1.0, 0.0)); | |
float random111 = random(integers + float3(1.0, 1.0, 1.0)); | |
// Use cosine interpolation, it's cheapest of the smooth interpolation variants: | |
// http://paulbourke.net/miscellaneous/interpolation/ | |
float3 fracs = frac(position); | |
float3 ts1 = (1.0 - cos(fracs * 3.14159265)) * 0.5; | |
float3 ts0 = 1.0 - ts1; | |
// TODO: Optimize the number of multiplications, maybe? Not sure if worthwhile. | |
return random000 * ts0.x * ts0.y * ts0.z + | |
random001 * ts0.x * ts0.y * ts1.z + | |
random010 * ts0.x * ts1.y * ts0.z + | |
random011 * ts0.x * ts1.y * ts1.z + | |
random100 * ts1.x * ts0.y * ts0.z + | |
random101 * ts1.x * ts0.y * ts1.z + | |
random110 * ts1.x * ts1.y * ts0.z + | |
random111 * ts1.x * ts1.y * ts1.z; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace
float
andfloat3
withhalf
andhalf3
if you're running on a mobile device and need even more performance.