6.2. Pass-through of varying attributes

A common task for geometry shaders is to pass-through varying attributes from vertex shader to fragment shader.

In the simplest case, you can just mark a varying variable in the vertex shader by a "magic" comment /* VARYING-PASSTHROUGH-GEOMETRY-SHADERS */. This will automatically add a proper code to pass given varying variable through the geometry shader, if only you use our geometryVertexXxx functions to set the output vertex attributes.

See the https://github.com/castle-engine/castle-engine/blob/master/src/scene/glsl/README.md for more details.

An example would look like this:

Shape {
  appearance Appearance {
    effects Effect {
      language "GLSL"
      parts [
        EffectPart { type "VERTEX" url "data:text/plain,
          /* VARYING-PASSTHROUGH-GEOMETRY-SHADERS */
          varying float vertex_x;

          void PLUG_vertex_object_space(const in vec4 vertex_object, const in vec3 normal_object)
          {
            vertex_x = vertex_object.x;
          }"
        }

        EffectPart { type "FRAGMENT" url "data:text/plain,
          varying float vertex_x;

          void PLUG_main_texture_apply(inout vec4 fragment_color, const in vec3 normal_eye)
          {
            fragment_color.rgb = vec3(vertex_x, 0.0, 0.0);
          }"
        }
      ]
    }
  }
  geometry Teapot { }
}

You can add to the above example a geometry shader that will pass-through the varying attribute vertex_x to the fragment shader. The GLSL code of the vertex and fragment shaders will remain unchanged.

Fully working example of this is in our demo models, see https://github.com/castle-engine/demo-models/blob/master/compositing_shaders/geometry_shader_optional.x3dv.