| 1 |
// -*-C++-*- |
| 2 |
|
| 3 |
// This is a library of noise functions, taking a coordinate vector and a wavelength |
| 4 |
// as input and returning a number [0:1] as output. |
| 5 |
|
| 6 |
// * Noise2D(in vec2 coord, in float wavelength) is 2d Perlin noise |
| 7 |
// * Noise3D(in vec3 coord, in float wavelength) is 3d Perlin noise |
| 8 |
// * DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dDensity) |
| 9 |
// is sparse dot noise and takes a dot density parameter |
| 10 |
// * DropletNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dDensity) |
| 11 |
// is sparse dot noise modified to look like liquid and takes a dot density parameter |
| 12 |
// * VoronoiNoise2D(in vec2 coord, in float wavelength, in float xrand, in float yrand) |
| 13 |
// is a function mapping the terrain into random domains, based on Voronoi tiling of a regular grid |
| 14 |
// distorted with xrand and yrand |
| 15 |
// * SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness) |
| 16 |
// computes a semi-random set of lines along the direction of steepest descent, allowing to |
| 17 |
// simulate e.g. water erosion patterns |
| 18 |
// * Strata3D(in vec3 coord, in float wavelength, in float variation) |
| 19 |
// computers a vertically stratified random pattern, appropriate e.g. for rock textures |
| 20 |
|
| 21 |
// Thorsten Renk 2014 |
| 22 |
|
| 23 |
#version 120 |
| 24 |
|
| 25 |
|
| 26 |
float rand2D(in vec2 co){ |
| 27 |
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); |
| 28 |
} |
| 29 |
|
| 30 |
float rand3D(in vec3 co){ |
| 31 |
return fract(sin(dot(co.xyz ,vec3(12.9898,78.233,144.7272))) * 43758.5453); |
| 32 |
} |
| 33 |
|
| 34 |
float cosine_interpolate(in float a, in float b, in float x) |
| 35 |
{ |
| 36 |
float ft = x * 3.1415927; |
| 37 |
float f = (1.0 - cos(ft)) * .5; |
| 38 |
|
| 39 |
return a*(1.0-f) + b*f; |
| 40 |
} |
| 41 |
|
| 42 |
float simple_interpolate(in float a, in float b, in float x) |
| 43 |
{ |
| 44 |
return a + smoothstep(0.0,1.0,x) * (b-a); |
| 45 |
} |
| 46 |
|
| 47 |
float interpolatedNoise2D(in float x, in float y) |
| 48 |
{ |
| 49 |
float integer_x = x - fract(x); |
| 50 |
float fractional_x = x - integer_x; |
| 51 |
|
| 52 |
float integer_y = y - fract(y); |
| 53 |
float fractional_y = y - integer_y; |
| 54 |
|
| 55 |
float v1 = rand2D(vec2(integer_x, integer_y)); |
| 56 |
float v2 = rand2D(vec2(integer_x+1.0, integer_y)); |
| 57 |
float v3 = rand2D(vec2(integer_x, integer_y+1.0)); |
| 58 |
float v4 = rand2D(vec2(integer_x+1.0, integer_y +1.0)); |
| 59 |
|
| 60 |
float i1 = simple_interpolate(v1 , v2 , fractional_x); |
| 61 |
float i2 = simple_interpolate(v3 , v4 , fractional_x); |
| 62 |
|
| 63 |
return simple_interpolate(i1 , i2 , fractional_y); |
| 64 |
} |
| 65 |
|
| 66 |
float interpolatedNoise3D(in float x, in float y, in float z) |
| 67 |
{ |
| 68 |
float integer_x = x - fract(x); |
| 69 |
float fractional_x = x - integer_x; |
| 70 |
|
| 71 |
float integer_y = y - fract(y); |
| 72 |
float fractional_y = y - integer_y; |
| 73 |
|
| 74 |
float integer_z = z - fract(z); |
| 75 |
float fractional_z = z - integer_z; |
| 76 |
|
| 77 |
float v1 = rand3D(vec3(integer_x, integer_y, integer_z)); |
| 78 |
float v2 = rand3D(vec3(integer_x+1.0, integer_y, integer_z)); |
| 79 |
float v3 = rand3D(vec3(integer_x, integer_y+1.0, integer_z)); |
| 80 |
float v4 = rand3D(vec3(integer_x+1.0, integer_y +1.0, integer_z)); |
| 81 |
|
| 82 |
float v5 = rand3D(vec3(integer_x, integer_y, integer_z+1.0)); |
| 83 |
float v6 = rand3D(vec3(integer_x+1.0, integer_y, integer_z+1.0)); |
| 84 |
float v7 = rand3D(vec3(integer_x, integer_y+1.0, integer_z+1.0)); |
| 85 |
float v8 = rand3D(vec3(integer_x+1.0, integer_y +1.0, integer_z+1.0)); |
| 86 |
|
| 87 |
|
| 88 |
float i1 = simple_interpolate(v1,v5, fractional_z); |
| 89 |
float i2 = simple_interpolate(v2,v6, fractional_z); |
| 90 |
float i3 = simple_interpolate(v3,v7, fractional_z); |
| 91 |
float i4 = simple_interpolate(v4,v8, fractional_z); |
| 92 |
|
| 93 |
float ii1 = simple_interpolate(i1,i2,fractional_x); |
| 94 |
float ii2 = simple_interpolate(i3,i4,fractional_x); |
| 95 |
|
| 96 |
|
| 97 |
return simple_interpolate(ii1 , ii2 , fractional_y); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
float Noise2D(in vec2 coord, in float wavelength) |
| 102 |
{ |
| 103 |
return interpolatedNoise2D(coord.x/wavelength, coord.y/wavelength); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
float Noise3D(in vec3 coord, in float wavelength) |
| 108 |
{ |
| 109 |
return interpolatedNoise3D(coord.x/wavelength, coord.y/wavelength, coord.z/wavelength); |
| 110 |
} |
| 111 |
|
| 112 |
float dotNoise2D(in float x, in float y, in float fractionalMaxDotSize, in float dDensity) |
| 113 |
{ |
| 114 |
float integer_x = x - fract(x); |
| 115 |
float fractional_x = x - integer_x; |
| 116 |
|
| 117 |
float integer_y = y - fract(y); |
| 118 |
float fractional_y = y - integer_y; |
| 119 |
|
| 120 |
if (rand2D(vec2(integer_x+1.0, integer_y +1.0)) > dDensity) |
| 121 |
{return 0.0;} |
| 122 |
|
| 123 |
float xoffset = (rand2D(vec2(integer_x, integer_y)) -0.5); |
| 124 |
float yoffset = (rand2D(vec2(integer_x+1.0, integer_y)) - 0.5); |
| 125 |
float dotSize = 0.5 * fractionalMaxDotSize * max(0.25,rand2D(vec2(integer_x, integer_y+1.0))); |
| 126 |
|
| 127 |
|
| 128 |
vec2 truePos = vec2 (0.5 + xoffset * (1.0 - 2.0 * dotSize) , 0.5 + yoffset * (1.0 -2.0 * dotSize)); |
| 129 |
|
| 130 |
float distance = length(truePos - vec2(fractional_x, fractional_y)); |
| 131 |
|
| 132 |
return 1.0 - smoothstep (0.3 * dotSize, 1.0* dotSize, distance); |
| 133 |
} |
| 134 |
|
| 135 |
float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dDensity) |
| 136 |
{ |
| 137 |
return dotNoise2D(coord.x/wavelength, coord.y/wavelength, fractionalMaxDotSize, dDensity); |
| 138 |
} |
| 139 |
|
| 140 |
float dropletNoise2D(in float x, in float y, in float fractionalMaxDotSize, in float dDensity) |
| 141 |
{ |
| 142 |
float integer_x = x - fract(x); |
| 143 |
float fractional_x = x - integer_x; |
| 144 |
|
| 145 |
float integer_y = y - fract(y); |
| 146 |
float fractional_y = y - integer_y; |
| 147 |
|
| 148 |
if (rand2D(vec2(integer_x+1.0, integer_y +1.0)) > dDensity) |
| 149 |
{return 0.0;} |
| 150 |
|
| 151 |
float xoffset = (rand2D(vec2(integer_x, integer_y)) -0.5); |
| 152 |
float yoffset = (rand2D(vec2(integer_x+1.0, integer_y)) - 0.5); |
| 153 |
float dotSize = 0.5 * fractionalMaxDotSize * max(0.25,rand2D(vec2(integer_x, integer_y+1.0))); |
| 154 |
|
| 155 |
float x1offset = 2.0 * (rand2D(vec2(integer_x+5.0, integer_y)) -0.5); |
| 156 |
float y1offset = 2.0 * (rand2D(vec2(integer_x, integer_y + 5.0)) - 0.5); |
| 157 |
float x2offset = 2.0 * (rand2D(vec2(integer_x-5.0, integer_y)) -0.5); |
| 158 |
float y2offset = 2.0 * (rand2D(vec2(integer_x-5.0, integer_y -5.0)) - 0.5); |
| 159 |
float smear = (rand2D(vec2(integer_x + 3.0, integer_y)) -0.5); |
| 160 |
|
| 161 |
|
| 162 |
vec2 truePos = vec2 (0.5 + xoffset * (1.0 - 4.0 * dotSize) , 0.5 + yoffset * (1.0 -4.0 * dotSize)); |
| 163 |
vec2 secondPos = truePos + vec2 (dotSize * x1offset, dotSize * y1offset); |
| 164 |
vec2 thirdPos = truePos + vec2 (dotSize * x2offset, dotSize * y2offset); |
| 165 |
|
| 166 |
float distance = length(truePos - vec2(fractional_x, fractional_y)); |
| 167 |
float dist1 = length(secondPos - vec2(fractional_x, fractional_y)); |
| 168 |
float dist2 = length(thirdPos - vec2(fractional_x, fractional_y)); |
| 169 |
|
| 170 |
return clamp(3.0 - smoothstep (0.3 * dotSize, 1.0* dotSize, distance) - smoothstep (0.3 * dotSize, 1.0* dotSize, dist1) - smoothstep ((0.1 + 0.5 * smear) * dotSize, 1.0* dotSize, dist2), 0.0,1.0); |
| 171 |
} |
| 172 |
|
| 173 |
float DropletNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dDensity) |
| 174 |
{ |
| 175 |
return dropletNoise2D(coord.x/wavelength, coord.y/wavelength, fractionalMaxDotSize, dDensity); |
| 176 |
} |
| 177 |
|
| 178 |
float voronoiNoise2D(in float x, in float y, in float xrand, in float yrand) |
| 179 |
{ |
| 180 |
float integer_x = x - fract(x); |
| 181 |
float fractional_x = x - integer_x; |
| 182 |
|
| 183 |
float integer_y = y - fract(y); |
| 184 |
float fractional_y = y - integer_y; |
| 185 |
|
| 186 |
float val[4]; |
| 187 |
|
| 188 |
val[0] = rand2D(vec2(integer_x, integer_y)); |
| 189 |
val[1] = rand2D(vec2(integer_x+1.0, integer_y)); |
| 190 |
val[2] = rand2D(vec2(integer_x, integer_y+1.0)); |
| 191 |
val[3] = rand2D(vec2(integer_x+1.0, integer_y+1.0)); |
| 192 |
|
| 193 |
float xshift[4]; |
| 194 |
|
| 195 |
xshift[0] = xrand * (rand2D(vec2(integer_x+0.5, integer_y)) - 0.5); |
| 196 |
xshift[1] = xrand * (rand2D(vec2(integer_x+1.5, integer_y)) -0.5); |
| 197 |
xshift[2] = xrand * (rand2D(vec2(integer_x+0.5, integer_y+1.0))-0.5); |
| 198 |
xshift[3] = xrand * (rand2D(vec2(integer_x+1.5, integer_y+1.0))-0.5); |
| 199 |
|
| 200 |
float yshift[4]; |
| 201 |
|
| 202 |
yshift[0] = yrand * (rand2D(vec2(integer_x, integer_y +0.5)) - 0.5); |
| 203 |
yshift[1] = yrand * (rand2D(vec2(integer_x+1.0, integer_y+0.5)) -0.5); |
| 204 |
yshift[2] = yrand * (rand2D(vec2(integer_x, integer_y+1.5))-0.5); |
| 205 |
yshift[3] = yrand * (rand2D(vec2(integer_x+1.5, integer_y+1.5))-0.5); |
| 206 |
|
| 207 |
|
| 208 |
float dist[4]; |
| 209 |
|
| 210 |
dist[0] = sqrt((fractional_x + xshift[0]) * (fractional_x + xshift[0]) + (fractional_y + yshift[0]) * (fractional_y + yshift[0])); |
| 211 |
dist[1] = sqrt((1.0 -fractional_x + xshift[1]) * (1.0-fractional_x+xshift[1]) + (fractional_y +yshift[1]) * (fractional_y+yshift[1])); |
| 212 |
dist[2] = sqrt((fractional_x + xshift[2]) * (fractional_x + xshift[2]) + (1.0-fractional_y +yshift[2]) * (1.0-fractional_y + yshift[2])); |
| 213 |
dist[3] = sqrt((1.0-fractional_x + xshift[3]) * (1.0-fractional_x + xshift[3]) + (1.0-fractional_y +yshift[3]) * (1.0-fractional_y + yshift[3])); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
int i, i_min; |
| 218 |
float dist_min = 100.0; |
| 219 |
for (i=0; i<4;i++) |
| 220 |
{ |
| 221 |
if (dist[i] < dist_min) |
| 222 |
{ |
| 223 |
dist_min = dist[i]; |
| 224 |
i_min = i; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return val[i_min]; |
| 229 |
//return val[0]; |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
float VoronoiNoise2D(in vec2 coord, in float wavelength, in float xrand, in float yrand) |
| 234 |
{ |
| 235 |
return voronoiNoise2D(coord.x/wavelength, coord.y/wavelength, xrand, yrand); |
| 236 |
} |
| 237 |
|
| 238 |
float slopeLines2D(in float x, in float y, in float sx, in float sy, in float steepness) |
| 239 |
{ |
| 240 |
float integer_x = x - fract(x); |
| 241 |
float fractional_x = x - integer_x; |
| 242 |
|
| 243 |
float integer_y = y - fract(y); |
| 244 |
float fractional_y = y - integer_y; |
| 245 |
|
| 246 |
vec2 O = vec2 (0.2 + 0.6* rand2D(vec2 (integer_x, integer_y+1)), 0.3 + 0.4* rand2D(vec2 (integer_x+1, integer_y))); |
| 247 |
vec2 S = vec2 (sx, sy); |
| 248 |
vec2 P = vec2 (-sy, sx); |
| 249 |
vec2 X = vec2 (fractional_x, fractional_y); |
| 250 |
|
| 251 |
float radius = 0.0 + 0.3 * rand2D(vec2 (integer_x, integer_y)); |
| 252 |
|
| 253 |
float b = (X.y - O.y + O.x * S.y/S.x - X.x * S.y/S.x) / (P.y - P.x * S.y/S.x); |
| 254 |
float a = (X.x - O.x - b*P.x)/S.x; |
| 255 |
|
| 256 |
return (1.0 - smoothstep(0.7 * (1.0-steepness), 1.2* (1.0 - steepness), 0.6* abs(a))) * (1.0 - smoothstep(0.0, 1.0 * radius,abs(b))); |
| 257 |
|
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
float SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness) |
| 263 |
{ |
| 264 |
return slopeLines2D(coord.x/wavelength, coord.y/wavelength, gradDir.x, gradDir.y, steepness); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
float strata3D(in float x, in float y, in float z, in float variation) |
| 269 |
{ |
| 270 |
float integer_x = x - fract(x); |
| 271 |
float fractional_x = x - integer_x; |
| 272 |
|
| 273 |
float integer_y = y - fract(y); |
| 274 |
float fractional_y = y - integer_y; |
| 275 |
|
| 276 |
float integer_z = z - fract(z); |
| 277 |
float fractional_z = z - integer_z; |
| 278 |
|
| 279 |
float rand_value_low = rand3D(vec3(0.0, 0.0, integer_z)); |
| 280 |
float rand_value_high = rand3D(vec3(0.0, 0.0, integer_z+1)); |
| 281 |
|
| 282 |
float rand_var = 0.5 - variation + 2.0 * variation * rand3D(vec3(integer_x, integer_y, integer_z)); |
| 283 |
|
| 284 |
|
| 285 |
return (1.0 - smoothstep(rand_var -0.15, rand_var + 0.15, fract(z))) * rand_value_low + smoothstep(rand_var-0.15, rand_var + 0.15, fract(z)) * rand_value_high; |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
float Strata3D(in vec3 coord, in float wavelength, in float variation) |
| 291 |
{ |
| 292 |
return strata3D(coord.x/wavelength, coord.y/wavelength, coord.z/wavelength, variation); |
| 293 |
} |