Noise Modeler Library  0.1
Library for procedurally generating noise-based terrains and other content
 All Classes Files Functions Variables Enumerations Enumerator Groups Pages
glslsourcenoise2d.hpp
1 /*
2 Copyright (C) 2011 by Ashima Arts (Simplex noise)
3 Copyright (C) 2011 by Stefan Gustavson (Classic noise)
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */
23 
24 #ifndef GLSLSOURCENOISE2D_HPP
25 #define GLSLSOURCENOISE2D_HPP
26 
27 constexpr const char * source_noise2d = R"lobotomized(
28 
29 //modified 2014 to add support for seeds
30 
31 //
32 // Description : Array and textureless GLSL 2D simplex noise function.
33 // Author : Ian McEwan, Ashima Arts.
34 // Maintainer : ijm
35 // Lastmod : 20110822 (ijm)
36 // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
37 // Distributed under the MIT License. See LICENSE file.
38 // https://github.com/ashima/webgl-noise
39 //
40 
41 vec3 mod289(vec3 x) {
42  return x - floor(x * (1.0 / 289.0)) * 289.0;
43 }
44 
45 vec2 mod289(vec2 x) {
46  return x - floor(x * (1.0 / 289.0)) * 289.0;
47 }
48 
49 vec3 permute(vec3 x) {
50  return mod289(((x*34.0)+1.0)*x);
51 }
52 
53 float snoise(vec2 v, float seed)
54  {
55  const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
56  0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
57  -0.577350269189626, // -1.0 + 2.0 * C.x
58  0.024390243902439); // 1.0 / 41.0
59 // First corner
60  vec2 i = floor(v + dot(v, C.yy) );
61  vec2 x0 = v - i + dot(i, C.xx);
62 
63 // Other corners
64  vec2 i1;
65  //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
66  //i1.y = 1.0 - i1.x;
67  i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
68  // x0 = x0 - 0.0 + 0.0 * C.xx ;
69  // x1 = x0 - i1 + 1.0 * C.xx ;
70  // x2 = x0 - 1.0 + 2.0 * C.xx ;
71  vec4 x12 = x0.xyxy + C.xxzz;
72  x12.xy -= i1;
73 
74 // Permutations
75  i = mod289(i); // Avoid truncation effects in permutation
76  vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
77  + i.x + vec3(0.0, i1.x, 1.0 ));
78  p = permute(p+vec3(seed));
79 
80  vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
81  m = m*m ;
82  m = m*m ;
83 
84 // Gradients: 41 points uniformly over a line, mapped onto a diamond.
85 // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
86 
87  vec3 x = 2.0 * fract(p * C.www) - 1.0;
88  vec3 h = abs(x) - 0.5;
89  vec3 ox = floor(x + 0.5);
90  vec3 a0 = x - ox;
91 
92 // Normalise gradients implicitly by scaling m
93 // Approximation of: m *= inversesqrt( a0*a0 + h*h );
94  m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
95 
96 // Compute final noise value at P
97  vec3 g;
98  g.x = a0.x * x0.x + h.x * x0.y;
99  g.yz = a0.yz * x12.xz + h.yz * x12.yw;
100  return 130.0 * dot(m, g);
101 }
102 
103 )lobotomized";
104 
105 #endif // GLSLSOURCENOISE2D_HPP