Shader Effects on a Texture

Posted on

Shader Effects on Texture

First of all, sorry for a relatively silent last month when it comes to the Castle Game Engine news. It was actually an incredibly busy June, and I’m going to prove it by posting lots of news in the upcoming days πŸ™‚

Let’s start with a new example, Shader Effects On Texture (examples/viewport_and_scenes/shader_effects_on_texture). This example demonstrates how you can process colors (and alpha) coming from a texture using a shader, for basically any processing that is conceptually “I wish this texture would look like this”. For example you can tweak texture colors, you can perform any texture transformation, you can mix the texture with basically anything (like another texture or an animated effect), you can calculate some channel based on others (e.g. calculate alpha based on intensity of RGB colors). At the core, you just write a shader using GLSL that defines a function with magic name PLUG_texture_color, so it’s really simple to start with some 1-line snippet, but you can explode it to do any crazy effect that GPU can handle πŸ™‚

The interesting Pascal code of this example is rather simple and limited to the GameViewMain unit, it comes down to processing the scene, finding textures (TAbstractSingleTextureNode instances), and adding the effects. You can add any effect to any texture in any scene. The possibilities for tweaking this to your needs are vast:

  • You can apply the effect to any scene / scenes,

  • You can apply the effect to any textures there (to detect which texture is which, use e.g. TAbstractTexture2DNode.TextureUsedFullUrl once the texture is loaded; set TAbstractTexture2DNode.IsTextureLoaded to true to force loading it now; use TImageTextureNode.FdUrl to look at specified possible URLs of the texture; as you can see there are a few useful texture classes involved, take a look at TImageTextureNode and all its ancestors for all the goodies),

  • You can add multiple effects, no problem. The PLUG_texture_color function name is “magic”, as mentioned before — you absolutely can have multiple effects using it on one texture. The engine will internally transform it into a working shader code, integrated with full shape rendering code.

  • If you would like to ignore the original color read from texture image, sure -> you can.

    We also have a dedicated TShaderTextureNode class, which is a special texture class that does not read its contents from any image, and it’s great if you would like to just “invent” texture contents, procedurally generating it in the shader, based on anything — like a noise equation. TShaderTextureNode is great to define something that is a regular texture for all engine functionality (e.g. can get texture coordinates), yet it’s completely “invented” by the shader code.

As always, follow the README of the example to know all the details.

See also the associated Using Shader Effects to implement rendering effects that enhance the standard rendering (examples/viewport_and_scenes/shader_effects). It shows applying shader effects over shapes and scenes, independently of the textures. All the possibilities are in Shader Effects (Compositing Shaders) documentation, in particular see Reference of available plugs to know all magic PLUG_xxx functions.

As always, have fun and we ask for your support on Patreon! I hope we’ll all have a very fun, and very productive vacation πŸ™‚

Start the discussion at Castle Game Engine Forum