Created
April 16, 2015 18:40
-
-
Save Longor1996/64ca31a25286470e3730 to your computer and use it in GitHub Desktop.
Voronoise Noise Function
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
/** | |
* Voronoise Function! | |
* Taken from: https://www.shadertoy.com/view/Xd23Dh | |
* Created by inigo quilez - iq/2014 | |
* Ported by Lars Longor1996 K - 16.04.2015 | |
* Version: 3 | |
* | |
* @param xX sample position, x | |
* @param xY sample position, y | |
* @param u squareness | |
* @param v smoothness | |
**/ | |
float noise(float xX, float xY, float u, float v) { | |
// vec2 p = floor(x) | |
float pX = floor(xX); | |
float pY = floor(xY); | |
// vec2 f = fract(x); | |
float fX = fract(xX); | |
float fY = fract(xY); | |
// -1:1- | |
float k = 1f + 63f * pow(1f-v, 4f); | |
float va = 0f; | |
float wt = 0f; | |
// ALL THE VARIABLES | |
float gX = 0f; | |
float gY = 0f; | |
float pgX = 0f; | |
float pgX = 0f; | |
float _qX = 0f; | |
float _qY = 0f; | |
float _qZ = 0f; | |
float _rX = 0f; | |
float _rY = 0f; | |
float _rZ = 0f; | |
float nX = 0f; | |
float nY = 0f; | |
float nZ = 0f; | |
float oX = 0f; | |
float oY = 0f; | |
float oZ = 0f; | |
float rX = 0f; | |
float rY = 0f; | |
float d = 0f; | |
float w = 0f; | |
// -1:1- | |
for(int j = -2; j <= 2; j++) | |
for(int i = -2; i <= 2; i++) | |
{ | |
// vec2 g = vec2(float(i), float(j)); | |
gX = (float) i; | |
gY = (float) j; | |
// p + g | |
pgX = pX + gX; | |
pgY = pY + gY; | |
// NOISE FUNCTION CALL f(p); | |
_qX = dot(pX, pY, 127.1f, 311.7f); | |
_qY = dot(pX, pY, 269.5f, 183.3f); | |
_qZ = dot(pX, pY, 419.2f, 371.9f); | |
_rX = fract(sin(_qX)*43758.5453); | |
_rY = fract(sin(_qY)*43758.5453); | |
_rZ = fract(sin(_qZ)*43758.5453); | |
// vec2 n = noise(p + g); | |
nX = _rX; | |
nY = _rY; | |
nZ = _rZ; | |
// vec3 o = n * vec3(u,u,1); | |
oX = nX * u; | |
oY = nY * u; | |
oZ = nZ; | |
// vec2 r = g - f + o.xy; | |
rX = gX - fX + oX; | |
rY = gY - fY + oY; | |
// float d = dot(r,r); | |
d = dotproduct(rX,rY,rX,rY); | |
// -1:1- | |
w = pow(1f - smoothstep(0.0f,1.414f, sqrt(d)), k); | |
// -1:1- | |
va += w*oZ; | |
wt += w; | |
} | |
// -1:1- | |
return va / wt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment