Debian Codegraph @

examples/terrain/data/water/shaders/animate_normals.fs

raw
1 /* Generate normal vector based on the vertex position, and time,
2 using smooth noise implemented in noise3Dgrad.glsl .
3 */
4
5 varying vec4 water_vertex_object;
6 uniform float time;
7
8 // OpenGLES requires this, otherwise error: precision mismatch between shaders for uniform (named castle_NormalMatrix)
9 uniform highp mat3 castle_NormalMatrix;
10
11 // Declare snoise from noise3Dgrad.glsl .
12 float snoise(vec3 v, out vec3 gradient);
13
14 // Adjust these values freely
15 #define wave_speed 1.0
16 #define wave_size 20.0
17 #define wave_calmness 10.0
18 #define wave_perturb_reflection 0.01
19 //#define wave_faster // uncomment this if you want
20
21 vec3 simple_3d_noise(vec3 noise_input)
22 {
23 vec3 result = vec3(0.0);
24
25 vec3 output1;
26 snoise(noise_input, output1);
27 result += output1;
28
29 #ifndef wave_faster
30 /* Add more noise layers, to make it interesting.
31 The values that we use to multiply below are just increasing powers
32 of two, but disturbed a little, so that their loops are not multiplies
33 of each other.
34 */
35
36 vec3 output2;
37 snoise(noise_input * 2.1, output2);
38 result += output2 / 2.5;
39
40 vec3 output3;
41 snoise(noise_input * 3.8, output3);
42 result += output3 / 4.6;
43 #endif
44
45 return result;
46 }
47
48 vec3 normal_in_object_space;
49
50 void PLUG_fragment_eye_space(const vec4 vertex_eye, inout vec3 normal_eye)
51 {
52 vec3 noise_input = vec3(
53 water_vertex_object.x * wave_size,
54 time * wave_speed,
55 water_vertex_object.z * wave_size);
56 normal_in_object_space = simple_3d_noise(noise_input);
57 normal_in_object_space = normalize(normal_in_object_space);
58
59 // only positive Y makes sense for us, bring Y in [0..1] range
60 normal_in_object_space.y = (normal_in_object_space.y + 1.0) * 0.5;
61
62 // to force calmer waves, just enlarge Y and normalize again
63 //normal_in_object_space.y = normal_in_object_space.y * wave_calmness;
64 normal_in_object_space.y = mix(wave_calmness * 0.5, wave_calmness, normal_in_object_space.y);
65
66 normal_in_object_space = normalize(normal_in_object_space);
67
68 // just to test, dummy generation instead of using simple_3d_noise
69 /*
70 normal_in_object_space = vec3(
71 sin(water_vertex_object.x * 100.0),
72 frac(time),
73 sin(water_vertex_object.z * 100.0));
74 normal_in_object_space = normalize(normal_in_object_space);
75 */
76
77 normal_eye = normalize(castle_NormalMatrix * normal_in_object_space);
78 }
79
80 void PLUG_texture_coord_shift(inout vec2 tex_coord)
81 {
82 /* We perturb reflections simply by shifting a tiny bit tex_coord.xy,
83 which is used for sampling the RenderedTexture that contains the mirror.
84
85 This trick is *not* based in reality.
86 We should instead use normal_in_object_space to calculate
87 the correctly reflected color.
88 However, this is much easier, and looks convincing :)
89 */
90 tex_coord.xy += normal_in_object_space.xz * wave_perturb_reflection;
91 }