Follow this example to learn how to use shader effects in Castle Game Engine.
Access the TCastleScene
instance in your code, by declaring in the published
section of your view a new field with name matching your scene, like MyScene: TCastleScene;
.
Then, create a shader effect that will change the color of any model to shades of red. This is done by creating a TEffectNode
and TEffectPartNode
and attaching them by calling MyScene.SetEffects
method. In this example, we do this in the Start
method of your view class, which is called once, when the view starts.
procedure TMyView.Start;
procedure CreateSimpleEffect;
var
Effect: TEffectNode;
EffectPart: TEffectPartNode;
begin
Effect := TEffectNode.Create;
Effect.Language := slGLSL;
EffectPart := TEffectPartNode.Create;
EffectPart.ShaderType := stFragment;
EffectPart.SetUrl(['castle-data:/shader_color_effect.fs']);
Effect.SetParts([EffectPart]);
MyScene.SetEffects([Effect]);
end;
begin
inherited;
CreateSimpleEffect;
end;
Now we need to define the shader code. Create a file shader_color_effect.fs
in the data subdirectory of your application, and put the following code inside:
void PLUG_fragment_modify(inout vec4 fragment_color)
{
float intensity = (fragment_color.r + fragment_color.g + fragment_color.b) / 3.0;
fragment_color = vec4(intensity, 0.0, 0.0, fragment_color.a);
}
Run the application, and you should see that the model is now rendered with a red color.