Perlin noise: Difference between revisions
Content added Content deleted
m (moved ocaml to its alphabetical place in the page) |
(→{{header|Go}}: added GLSL) |
||
Line 357: | Line 357: | ||
</pre> |
</pre> |
||
=={{header|GLSL}}== |
|||
From https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 : |
|||
<lang glsl> |
|||
float rand(vec2 c){ |
|||
return fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453); |
|||
} |
|||
float noise(vec2 p, float freq ){ |
|||
float unit = screenWidth/freq; |
|||
vec2 ij = floor(p/unit); |
|||
vec2 xy = mod(p,unit)/unit; |
|||
//xy = 3.*xy*xy-2.*xy*xy*xy; |
|||
xy = .5*(1.-cos(PI*xy)); |
|||
float a = rand((ij+vec2(0.,0.))); |
|||
float b = rand((ij+vec2(1.,0.))); |
|||
float c = rand((ij+vec2(0.,1.))); |
|||
float d = rand((ij+vec2(1.,1.))); |
|||
float x1 = mix(a, b, xy.x); |
|||
float x2 = mix(c, d, xy.x); |
|||
return mix(x1, x2, xy.y); |
|||
} |
|||
float pNoise(vec2 p, int res){ |
|||
float persistance = .5; |
|||
float n = 0.; |
|||
float normK = 0.; |
|||
float f = 4.; |
|||
float amp = 1.; |
|||
int iCount = 0; |
|||
for (int i = 0; i<50; i++){ |
|||
n+=amp*noise(p, f); |
|||
f*=2.; |
|||
normK+=amp; |
|||
amp*=persistance; |
|||
if (iCount == res) break; |
|||
iCount++; |
|||
} |
|||
float nf = n/normK; |
|||
return nf*nf*nf*nf; |
|||
} |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
<lang go>package main |
<lang go>package main |