-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise3.vert
78 lines (63 loc) · 1.56 KB
/
noise3.vert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#version 330 core
#define E 2.71828182845904523536;
layout (std140) uniform camera
{
mat4 projection;
mat4 view;
mat4 pvm;
mat4 ortho;
vec4 position;
};
vec2 hash2(vec2 v)
{
vec2 rand = vec2(0,0);
rand = 20. * 2.05 * fract(v * 2.3183099 + vec2(0.71, 0.113));
rand = -2.0 + 2 * 3. * fract(rand.x * rand.y * (rand.x + rand.y) * rand);
return rand;
}
float perlin_noise(vec2 v)
{
float noise = 0;
vec2 i = floor(v);
vec2 f = fract(v);
vec2 u = f * f * (3.0 - 2.0 * f);
// Four corners in 2D of a tile
vec2 a = i;
vec2 b = i + vec2(1.0, 0.0);
vec2 c = i + vec2(0.0, 1.0);
vec2 d = i + vec2(1.0, 1.0);
noise = mix(mix(dot(hash2(a), f),
dot(hash2(b), f - vec2(1.0, 0.0)), u.x),
mix(dot(hash2(c), f - vec2(0.0, 1.0)),
dot(hash2(d), f - vec2(1.0, 1.0)), u.x), u.y);
return noise;
}
float noiseOctave(vec2 v, int num)
{
float sum = 0;
for (int i = 0; i < num; i++) {
sum += pow(2.0, -i) * perlin_noise(pow(2.0, i) * v);
}
return sum;
}
float height(vec2 v){
float h = 0;
float h1 = 0.75 * noiseOctave(v, 10);
float h2 = 0.5 * pow(2.71828182845904523536, noiseOctave(v, 10));
h = h1 + h2;
return h;
}
/*uniform variables*/
uniform float iTime; // time
/*input variables*/
layout (location=0) in vec4 pos;
layout (location=2) in vec4 normal;
layout (location=3) in vec4 uv;
layout (location=4) in vec4 tangent;
/*output variables*/
out vec3 vtx_pos; // vertex position in the world space
void main()
{
vtx_pos = vec3(pos.x, height(pos.xz), pos.z);
gl_Position = pvm * vec4(vtx_pos.xyz, 1.);
}