Debian Codegraph @

examples/viewport_and_scenes/shader_effects/code/gameviewmain.pas

raw
1 {
2 Copyright 2020-2023 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 CastleComponentSerialize, CastleUIControls, CastleControls,
23 CastleKeysMouse, CastleScene, X3DNodes, X3DFields;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 ButtonRandomizeColor, ButtonChangeTexture: TCastleButton;
33 SceneForColorEffect, SceneForTextureEffect: TCastleScene;
34 RectCurrentColor: TCastleRectangleControl;
35 private
36 { Control effect affecting SceneForColorEffect }
37 EffectColorField: TSFVec3f;
38
39 { Control effect affecting SceneForTextureEffect }
40 EffectTextureField: TSFNode;
41 TestTexture1: TImageTextureNode;
42 TestTexture2: TImageTextureNode;
43
44 procedure ClickRandomizeColor(Sender: TObject);
45 procedure ClickChangeTexture(Sender: TObject);
46 public
47 constructor Create(AOwner: TComponent); override;
48 procedure Start; override;
49 procedure Stop; override;
50 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
51 end;
52
53 var
54 ViewMain: TViewMain;
55
56 implementation
57
58 uses SysUtils,
59 CastleVectors, CastleRenderOptions;
60
61 { TViewMain ----------------------------------------------------------------- }
62
63 constructor TViewMain.Create(AOwner: TComponent);
64 begin
65 inherited;
66 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
67 end;
68
69 procedure TViewMain.Start;
70
71 { Create Effect changing a color, exposing a 'color' uniform shader variable
72 using X3D field EffectColorField.
73
74 You can change such uniform shader variable at any time.
75 You can even change it very often (e.g. every frame),
76 changing uniform values is cheap.
77 See TViewMain.ClickRandomizeColor . }
78 procedure CreateColorEffect(const Scene: TCastleScene);
79 var
80 Effect: TEffectNode;
81 EffectPart: TEffectPartNode;
82 begin
83 Effect := TEffectNode.Create;
84 Effect.Language := slGLSL;
85
86 { Add custom field (maps to GLSL uniform "color"), initially white }
87 EffectColorField := TSFVec3f.Create(Effect, true, 'color', Vector3(1, 1, 1));
88 Effect.AddCustomField(EffectColorField);
89
90 { Add TEffectPartNode which actually contains shader code.
91 You can load shader code from file,
92 you could also set it explicitly from string using EffectPart.Contents := '...'; }
93 EffectPart := TEffectPartNode.Create;
94 EffectPart.ShaderType := stFragment;
95 EffectPart.SetUrl(['castle-data:/shaders/color_effect.fs']);
96 Effect.SetParts([EffectPart]);
97
98 { Using "RootNode.AddChildren" applies effect for everything in the scene.
99 It is possible to be more selective:
100
101 - You can apply effect only for given TAppearanceNode
102 (will be limited to shapes using this TAppearanceNode).
103
104 - Effects can also be applied on a specific texture or light source.
105 See https://github.com/castle-engine/demo-models/blob/master/compositing_shaders/grayscale_texture_effect.x3dv .
106
107 See https://castle-engine.io/compositing_shaders.php and in particular
108 https://castle-engine.io/compositing_shaders_doc/html/ about shader effects. }
109 Scene.RootNode.AddChildren([Effect]);
110
111 // adding node to Scene turns off animation set by AutoAnimation, run it again
112 Scene.PlayAnimation('Walk', true);
113 end;
114
115 { Create Effect using a texture, provided as uniform shader variable 'testTexture'
116 using X3D field EffectTextureField.
117 The point of it is to show how to pass a texture to a shader in CGE.
118
119 You can change the texture at runtime, it will change the texture passed to shader
120 correctly. See TViewMain.ClickChangeTexture lower for example. }
121 procedure CreateTextureEffect(const Scene: TCastleScene);
122 var
123 Effect: TEffectNode;
124 EffectPartFragment, EffectPartVertex: TEffectPartNode;
125 begin
126 Effect := TEffectNode.Create;
127 Effect.Language := slGLSL;
128
129 TestTexture1 := TImageTextureNode.Create;
130 TestTexture1.SetUrl(['castle-data:/test_textures/handpaintedwall2.png']);
131 TestTexture1.KeepExistingBegin; // do not auto-free when unused
132
133 TestTexture2 := TImageTextureNode.Create;
134 TestTexture2.SetUrl(['castle-data:/test_textures/mountain.png']);
135 TestTexture2.KeepExistingBegin; // do not auto-free when unused
136
137 { Add custom field (maps to GLSL uniform "texture") }
138 EffectTextureField := TSFNode.Create(Effect, true, 'testTexture', [TImageTextureNode], TestTexture1);
139 Effect.AddCustomField(EffectTextureField);
140
141 { Add 2x TEffectPartNode which actually contains shader code.
142 You can load shader code from file,
143 you could also set it explicitly from string using EffectPart.Contents := '...'; }
144 EffectPartFragment := TEffectPartNode.Create;
145 EffectPartFragment.ShaderType := stFragment;
146 EffectPartFragment.SetUrl(['castle-data:/shaders/texture_effect.fs']);
147
148 EffectPartVertex := TEffectPartNode.Create;
149 EffectPartVertex.ShaderType := stVertex;
150 EffectPartVertex.SetUrl(['castle-data:/shaders/texture_effect.vs']);
151
152 Effect.SetParts([EffectPartFragment, EffectPartVertex]);
153
154 { Using "RootNode.AddChildren" applies effect for everything in the scene.
155 It is possible to be more selective:
156
157 - You can apply effect only for given TAppearanceNode
158 (will be limited to shapes using this TAppearanceNode).
159
160 - Effects can also be applied on a specific texture or light source.
161 See https://github.com/castle-engine/demo-models/blob/master/compositing_shaders/grayscale_texture_effect.x3dv .
162
163 See https://castle-engine.io/compositing_shaders.php and in particular
164 https://castle-engine.io/compositing_shaders_doc/html/ about shader effects. }
165 Scene.RootNode.AddChildren([Effect]);
166
167 // adding node to Scene turns off animation set by AutoAnimation, run it again
168 Scene.PlayAnimation('Walk', true);
169 end;
170
171 begin
172 inherited;
173
174 CreateColorEffect(SceneForColorEffect);
175
176 CreateTextureEffect(SceneForTextureEffect);
177
178 ButtonRandomizeColor.OnClick := {$ifdef FPC}@{$endif} ClickRandomizeColor;
179 ButtonChangeTexture.OnClick := {$ifdef FPC}@{$endif} ClickChangeTexture;
180 end;
181
182 procedure TViewMain.Stop;
183 begin
184 TestTexture1.KeepExistingEnd;
185 TestTexture2.KeepExistingEnd;
186 FreeIfUnusedAndNil(TestTexture1);
187 FreeIfUnusedAndNil(TestTexture2);
188 inherited;
189 end;
190
191 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
192 begin
193 inherited;
194 { This virtual method is executed every frame (many times per second). }
195 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
196 end;
197
198 procedure TViewMain.ClickRandomizeColor(Sender: TObject);
199 var
200 NewColor: TVector3;
201 begin
202 NewColor := Vector3(Random, Random, Random);
203 RectCurrentColor.Color := Vector4(NewColor, 1);
204 EffectColorField.Send(NewColor);
205 end;
206
207 procedure TViewMain.ClickChangeTexture(Sender: TObject);
208 begin
209 if EffectTextureField.Value = TestTexture1 then
210 EffectTextureField.Send(TestTexture2)
211 else
212 EffectTextureField.Send(TestTexture1);
213
214 { TODO: The GLContextClose call is needed for now, to load TestTexture2 properly to OpenGL
215 next time the scene is rendered.
216 The GLContextClose should not be necessary, the reload should happen automatically,
217 TCastleSceneCore should realize that shapes using EffectTextureField
218 (and textures used by these shapes) need to be prepared again,
219 i.e. passed to PrepareShape. }
220 SceneForTextureEffect.GLContextClose;
221 end;
222
223 end.