An easy way of interactively updating uniform variables of custom GLSL shader is using game properties as the values of uniform variables, and updating the game properties from keyboard input which triggers the update of the game frame. For example, to accumulate a floating point number `verticalSunDirection_cameraSpace` in fragment shader, the steps go as the following:

  • Add a game property `verticalSunDirection` to the object the shader is attached to.

add-game-property

  • Assign the uniform variable `verticalSunDirection_cameraSpace` by the game property `verticalSunDirection`. For debugging purpose, we print the updated value of `verticalSunDirection` when assigning it to the uniform at line 619:

assign-gameproperty-to-uniform

If you are curious about the try-except block, please refer to [1] for explanation in the “Logic” part of “Simple endless scroll” section =)

  • Add a keyboard sensor to add a fixed value 0.1 to the game property `verticalSunDirection` while the game is running (game is triggered key `p`). Here we use the up arrow key:keyboard-sensor
  • Test the updated value of game property `verticalSunDirection`:
    • Run the game by key `p`.
    • Hit the up arrow key, then the game console prints the updated `verticalSunDirection`:

game-console

References

[1] Scrolling Textures with GLSL. https://whatjaysaid.wordpress.com/2015/03/09/bge-scrolling-textures-with-glsl