Debian Codegraph @

examples/viewport_and_scenes/mesh_update/data/animate_mesh.vs

raw
1 /* Shader effect, in GLSL language, to animate the mesh.
2 This shader changes the vertex position (in object space)
3 based on the time, to make a simple wave-like animation.
4
5 This piece of shader code depends on Castle Game Engine
6 "Shader Effects" feature documented on
7 https://castle-engine.io/compositing_shaders_doc/html/ .
8 The routine name PLUG_vertex_object_space_change is special --
9 it will be recognized by Castle Game Engine and called during regular
10 CGE shaders.
11 */
12
13 uniform float time;
14
15 /* Process the 3D vertex position, changing vertex Y.
16 This is equivalent to what Pascal calculation in
17 TViewMain.UpdateCoordinateNode is doing, when we animate mesh without
18 shader. */
19 vec3 process_vertex(vec3 position)
20 {
21 return vec3(
22 position.x,
23 sin((position.x + time * 20.0) * 0.5) +
24 sin((position.z + time * 15.0) * 0.3) * 0.5,
25 position.z);
26 }
27
28 void PLUG_vertex_object_space_change(
29 inout vec4 vertex_object,
30 inout vec3 normal_object)
31 {
32 /* We assume here vertex_object.w == 1.0, so we don't change it.
33 This is true for the normal vertex shader input from TCoordinateNode
34 where vertex is defined in 3D coordinates.
35 So we only read/write "vertex_object.xyz". */
36 vertex_object.xyz = process_vertex(vertex_object.xyz);
37
38 /* We have to also update normal_object, otherwise light on the object
39 will be wrong -- original normals assume a flat surface, not wavy. */
40 const float shift = 0.1;
41 normal_object = normalize(cross(
42 process_vertex(vertex_object.xyz + vec3(0, 0, shift)) - vertex_object.xyz,
43 process_vertex(vertex_object.xyz + vec3(shift, 0, 0)) - vertex_object.xyz));
44 }