Debian Codegraph @

examples/viewport_and_scenes/shader_effects_on_texture/code/gameviewmain.pas

raw
1 {
2 Copyright 2024-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleVectors, CastleComponentSerialize, CastleTimeUtils, X3DNodes,
23 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene,
24 CastleTransform;
25
26 type
27 { Main view, where most of the application logic takes place. }
28 TViewMain = class(TCastleView)
29 published
30 { Components designed using CGE editor.
31 These fields will be automatically initialized at Start. }
32 LabelFps: TCastleLabel;
33 ScenesParent: TCastleTransform;
34 SceneTexturedSampleThing2, SceneTexturedSampleThing3: TCastleScene;
35 private
36 WorldTime: TFloatTime;
37 { Field needed only to pass information
38 from SceneAddTextureEffect to NodeAddTextureEffect. }
39 EffectShaderUrl: String;
40 procedure NodeAddTextureEffect(Node: TX3DNode);
41 public
42 constructor Create(AOwner: TComponent); override;
43 procedure Start; override;
44 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
45 end;
46
47 var
48 ViewMain: TViewMain;
49
50 implementation
51
52 uses SysUtils,
53 CastleRenderOptions;
54
55 { TViewMain ----------------------------------------------------------------- }
56
57 constructor TViewMain.Create(AOwner: TComponent);
58 begin
59 inherited;
60 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
61 end;
62
63 procedure TViewMain.NodeAddTextureEffect(Node: TX3DNode);
64 var
65 TextureNode: TAbstractSingleTextureNode;
66 Effect: TEffectNode;
67 EffectPart: TEffectPartNode;
68 begin
69 // can be safely cast, as we enumerate only TAbstractSingleTextureNode
70 TextureNode := Node as TAbstractSingleTextureNode;
71
72 Effect := TEffectNode.Create;
73 Effect.Language := slGLSL;
74
75 { Add TEffectPartNode which actually contains shader code.
76 You can load shader code from file,
77 you could also set it explicitly from string using EffectPart.Contents := '...'; }
78 EffectPart := TEffectPartNode.Create;
79 EffectPart.ShaderType := stFragment;
80 EffectPart.SetUrl([EffectShaderUrl]);
81 Effect.SetParts([EffectPart]);
82
83 { This applies the effect to the specific texture,
84 which allows PLUG_texture_color to be available
85 ( see https://castle-engine.io/compositing_shaders_doc/html/section.fragment_plugs.html ). }
86 TextureNode.SetEffects([Effect]);
87 end;
88
89 procedure TViewMain.Start;
90
91 { Add a shader effect that modifies effective texture RGBA. }
92 procedure SceneAddTextureEffect(const Scene: TCastleScene;
93 const AnEffectShaderUrl: String);
94 var
95 SavedAnimation: String;
96 begin
97 if Scene.RootNode <> nil then
98 begin
99 SavedAnimation := Scene.AutoAnimation;
100
101 EffectShaderUrl := AnEffectShaderUrl;
102 Scene.RootNode.EnumerateNodes(TAbstractSingleTextureNode,
103 {$ifdef FPC}@{$endif} NodeAddTextureEffect, false);
104
105 { Adding node to Scene turns off animation set by AutoAnimation, run it again.
106 The need to do this may disappear in future CGE versions. }
107 if SavedAnimation <> '' then
108 Scene.PlayAnimation(SavedAnimation, true);
109 end;
110 end;
111
112 begin
113 inherited;
114 SceneAddTextureEffect(SceneTexturedSampleThing2, 'castle-data:/shaders/texture_effect_sepia.fs');
115 SceneAddTextureEffect(SceneTexturedSampleThing3, 'castle-data:/shaders/texture_effect_invert.fs');
116 end;
117
118 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
119 begin
120 inherited;
121
122 { This virtual method is executed every frame (many times per second). }
123 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
124 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
125
126 { Rotate all 3 scenes (by single parent ScenesParent) around X axis,
127 just to show some interesting lighting. }
128 WorldTime := WorldTime + SecondsPassed;
129 ScenesParent.Rotation := Vector4(1, 0, 0, WorldTime);
130 end;
131
132 end.