summaryrefslogtreecommitdiff
path: root/themes/src/shaders/water_vert.cpp
blob: 44dabd88041132006bd7fa81adc9a1074f70dd1c (plain)
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
#include "water_vert.h"

const char* shader_water_vert = "precision highp float; \n"
" \n"
"attribute vec2 position;      // local (x, z) on the plane \n"
" \n"
"uniform mat4 projection; \n"
"uniform mat4 view; \n"
"uniform mat4 model; \n"
"uniform float time; \n"
"uniform float amplitude; \n"
" \n"
"varying vec3 vWorldPos; \n"
"varying vec3 vNormal; \n"
" \n"
"float hash(vec2 p) { \n"
"    p = fract(p * vec2(123.34, 456.21)); \n"
"    p += dot(p, p + 45.32); \n"
"    return fract(p.x * p.y); \n"
"} \n"
" \n"
"float valueNoise(vec2 p) { \n"
"    vec2 i = floor(p); \n"
"    vec2 f = fract(p); \n"
"    float a = hash(i); \n"
"    float b = hash(i + vec2(1.0, 0.0)); \n"
"    float c = hash(i + vec2(0.0, 1.0)); \n"
"    float d = hash(i + vec2(1.0, 1.0)); \n"
"    vec2 u = f * f * (3.0 - 2.0 * f); \n"
"    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y); \n"
"} \n"
" \n"
"// Irregular height: non-harmonic directional sines plus two octaves of \n"
"// scrolling value noise so it never reads as a single clean wave. \n"
"float waveHeight(vec2 p, float t) { \n"
"    float h = 0.0; \n"
"    h += 0.30 * sin(dot(p, vec2(0.90, 0.30)) * 0.35 + t * 1.1); \n"
"    h += 0.20 * sin(dot(p, vec2(-0.40, 1.00)) * 0.55 - t * 1.7); \n"
"    h += 0.12 * sin(dot(p, vec2(0.70, -0.80)) * 0.90 + t * 2.3); \n"
"    h += 0.08 * sin(dot(p, vec2(1.00, 0.60)) * 1.60 - t * 3.1); \n"
"    h += 0.18 * (valueNoise(p * 0.25 + vec2(t * 0.15, t * 0.10)) - 0.5); \n"
"    h += 0.09 * (valueNoise(p * 0.70 - vec2(t * 0.20, 0.0)) - 0.5); \n"
"    return h; \n"
"} \n"
" \n"
"void main() { \n"
"    vec2 p = position; \n"
"    float t = time; \n"
" \n"
"    float h = waveHeight(p, t) * amplitude; \n"
" \n"
"    // Analytic normal via central differences of the same height function. \n"
"    float e = 2.0; \n"
"    float hL = waveHeight(p - vec2(e, 0.0), t) * amplitude; \n"
"    float hR = waveHeight(p + vec2(e, 0.0), t) * amplitude; \n"
"    float hD = waveHeight(p - vec2(0.0, e), t) * amplitude; \n"
"    float hU = waveHeight(p + vec2(0.0, e), t) * amplitude; \n"
"    vec3 N = normalize(vec3(hL - hR, 2.0 * e, hD - hU)); \n"
" \n"
"    vec4 worldPos = model * vec4(p.x, h, p.y, 1.0); \n"
"    vWorldPos = worldPos.xyz; \n"
"    vNormal   = N;   // model is translation-only, so no normal matrix needed \n"
" \n"
"    gl_Position = projection * view * worldPos; \n"
"} \n"
" \n";