Debian Codegraph @

src/scene/x3d/x3dnodes_castle.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Nodes that are defined specifically by Castle Game Engine.
18 See [https://castle-engine.io/x3d_extensions.php]. } { }
19
20 {$ifdef read_interface}
21 { Text with 3D depth.
22 @bold(The depth is not implemented now -- this is equivalent to @link(TTextNode) now.) }
23 TText3DNode = class(TTextNode)
24 {$I auto_generated_node_helpers/x3dnodes_text3d.inc}
25 end;
26
27 { Customize blending (partial transparency) mode.
28 See https://castle-engine.io/x3d_extensions.php#section_ext_blending . }
29 TBlendModeNode = class(TAbstractAppearanceChildNode)
30 {$I auto_generated_node_helpers/x3dnodes_blendmode.inc}
31 end;
32
33 { Appearance node with a special name @code(KambiAppearance).
34 Useful if you want to use EXTERNPROTO to declare Castle Game Engine
35 extensions the Appearance in a clean way. }
36 TKambiAppearanceNode = class(TAppearanceNode)
37 {$I auto_generated_node_helpers/x3dnodes_kambiappearance.inc}
38 end;
39
40 { Embed a scene from another file, and replace some of it's nodes.
41 See https://castle-engine.io/x3d_extensions.php#section_ext_kambi_inline . }
42 TKambiInlineNode = class(TInlineNode)
43 strict private
44 { This is internal only for DoReplaceNodes, initialized in AfterLoadInlined. }
45 ReplaceCount: Integer;
46 procedure DoReplaceNodes(ParentNode: TX3DNode; var Node: TX3DNode);
47 protected
48 procedure AfterLoadInlined; override;
49 public
50 {$I auto_generated_node_helpers/x3dnodes_kambiinline.inc}
51 end;
52
53 { Navigation information with some Castle Game Engine extensions.
54 @deprecated Use TNavigationInfoNode, it already has all CGE extensions. }
55 TKambiNavigationInfoNode = class(TNavigationInfoNode)
56 {$I auto_generated_node_helpers/x3dnodes_kambinavigationinfo.inc}
57 end;
58
59 TShadowMapCompareMode = (smCompareRLEqual, smCompareRGEqual, smNone);
60
61 { Generate texture by rendering a depth (shadow) map.
62 See https://castle-engine.io/x3d_extensions_shadow_maps.php }
63 TGeneratedShadowMapNode = class(TAbstractSingleTextureNode)
64 strict private
65 type
66 TGenShadowMapGenTex = class(TGeneratedTextureFunctionality)
67 strict private
68 FParent: TGeneratedShadowMapNode;
69 protected
70 function GetUpdate: TTextureUpdate; override;
71 procedure SetUpdate(const Value: TTextureUpdate); override;
72 public
73 constructor Create(const AParent: TGeneratedShadowMapNode);
74 end;
75 var
76 FGenTexFunctionality: TGenShadowMapGenTex;
77 public
78 const
79 DefaultSize = 128;
80 DefaultScale = 4.0;
81 DefaultBias = 4.0;
82
83 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
84
85 {$I auto_generated_node_helpers/x3dnodes_generatedshadowmap.inc}
86 end;
87
88 { Generate texture coordinates by projection.
89 See https://castle-engine.io/x3d_extensions_shadow_maps.php }
90 TProjectedTextureCoordinateNode = class(TAbstractSingleTextureCoordinateNode)
91 {$I auto_generated_node_helpers/x3dnodes_projectedtexturecoordinate.inc}
92 end;
93
94 { Screen-space shader effect.
95 See https://castle-engine.io/x3d_extensions_screen_effects.php . }
96 TScreenEffectNode = class(TAbstractChildNode)
97 strict private
98 FInternalRendererResource: TInternalRendererResource;
99 FContextCloseEventSetup: Boolean;
100 procedure SetInternalRendererResource(const Value: TInternalRendererResource);
101 procedure GLContextCloseEvent(Sender: TObject);
102 protected
103 function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override;
104 public
105 { Internal data stored here and used by the renderer.
106 Specific to this TX3DNode.
107
108 This holds a reference count to OpenGL resource in RendererCache,
109 freeing this InternalRendererResource decreases the reference count,
110 and it is done when destroying this node or when last GL context
111 is lost.
112
113 @exclude }
114 property InternalRendererResource: TInternalRendererResource
115 read FInternalRendererResource write SetInternalRendererResource;
116
117 { Free InternalRendererResource, if any. }
118 procedure InternalRendererResourceFree;
119
120 destructor Destroy; override;
121
122 {$I auto_generated_node_helpers/x3dnodes_screeneffect.inc}
123 end;
124
125 { Internal notes to MultiGeneratedTextureCoordinate:
126
127 It's implemented as simple MultiTextureCoordinate descendant,
128 this is conforming with the specification. (MultiTextureCoordinate
129 allows a little more children, but this will be detected and ignored
130 by renderer.)
131
132 Also, this way fields like Sphere.texCoord may require just
133 MultiTextureCoordinate, and so I have backward compatibility
134 and also accept MultiGeneratedTextureCoordinate.
135
136 Also, shadow maps processing can just use MultiTextureCoordinate
137 (regardless if we're on primitives or not). }
138
139 { Generated texture coordinates for multi-texturing.
140 See [https://castle-engine.io/x3d_extensions.php#section_ext_tex_coord]. }
141 TMultiGeneratedTextureCoordinateNode = class(TMultiTextureCoordinateNode)
142 {$I auto_generated_node_helpers/x3dnodes_multigeneratedtexturecoordinate.inc}
143 end;
144
145 TEffectPartNode = class;
146
147 { Shader effect, that can be composed with other effect or standard rendering.
148 See https://castle-engine.io/compositing_shaders.php . }
149 TEffectNode = class(TAbstractChildNode)
150 protected
151 procedure GroupBeforeTraverse(const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean); override;
152 public
153 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
154 {$I auto_generated_node_helpers/x3dnodes_effect.inc}
155 end;
156
157 { Part of a shader effect, used with @link(TEffectNode).
158 See https://castle-engine.io/compositing_shaders.php . }
159 TEffectPartNode = class(TAbstractNode)
160 strict private
161 ContentsCached: boolean;
162 ContentsCache: String;
163 procedure EventUrlReceive(
164 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
165 function GetContents: String;
166 procedure SetContents(const Value: String);
167 public
168 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
169
170 { The shader code.
171
172 Getting this automatically loads the shader code pointed by the URL field
173 of this node. The shader code may be specified in an external file,
174 or inline using "data URI", see https://castle-engine.io/manual_network.php .
175 The shader code may also be specified using the XML "CDATA" in X3D XML encoding.
176 Returns '' of no valid URLs are present
177 (loading errors are reported to WritelnWarning).
178
179 Setting this overrides the URL field, to specify only the given shader code. }
180 property Contents: String read GetContents write SetContents;
181
182 {$I auto_generated_node_helpers/x3dnodes_effectpart.inc}
183 end;
184
185 { Procedural texture defined by shaders.
186 See https://castle-engine.io/compositing_shaders.php . }
187 TShaderTextureNode = class(TAbstractSingleTextureNode)
188 {$I auto_generated_node_helpers/x3dnodes_shadertexture.inc}
189 end;
190
191 { Viewpoint for making reflections on flat objects.
192 See https://castle-engine.io/x3d_extensions_mirror_plane.php . }
193 TViewpointMirrorNode = class(TAbstractNode)
194 {$I auto_generated_node_helpers/x3dnodes_viewpointmirror.inc}
195 end;
196
197 { Interpolate (animate) a 2D orientation,
198 during which angle changes but axis remains constant. }
199 TOrientationInterpolator2DNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
200 TSFRotation>)
201 strict protected
202 function KeyValueCount: TListSize; override;
203 procedure InterpolatorLerp(const Target: TSFRotation; const A: Single;
204 const Index1, Index2: Integer); override;
205 procedure InterpolatorSet(const Target: TSFRotation; const Index: Integer); override;
206 function CreateOutputField: TSFRotation; override;
207 {$I auto_generated_node_helpers/x3dnodes_orientationinterpolator2d.inc}
208 end;
209
210 {$endif read_interface}
211
212 {$ifdef read_implementation}
213
214 procedure TKambiInlineNode.AfterLoadInlined;
215 var
216 I: Integer;
217 begin
218 ReplaceCount := Min(FdreplaceNames.Count, FdreplaceNodes.Count);
219 if (FdreplaceNames.Count <> ReplaceCount) or
220 (FdreplaceNodes.Count <> ReplaceCount) then
221 WritelnWarning('VRML/X3D', 'replaceNames and replaceNodes for KambiInline node should have the same number of items');
222
223 { After successful loading, there's always exactly one child. }
224 Assert(VRML1ChildrenCount = 1);
225
226 { First, a special case when Children[0] node has the required name.
227 Needs to be checked explicitly, EnumerateReplaceNodes cannot check this. }
228 for I := 0 to ReplaceCount - 1 do
229 { Ignore empty names, since VRML node cannot have empty name
230 (it's only our implementation detail that we mark "noname" as empty name) }
231 if (FdReplaceNames.Items[I] <> '') and
232 (FdReplaceNames.Items[I] = VRML1Children[0].X3DName) then
233 begin
234 VRML1ChildRemove(0);
235 VRML1ChildAdd(0, FdReplaceNodes[I]);
236 Exit;
237 end;
238
239 VRML1Children[0].EnumerateReplaceChildren({$ifdef FPC}@{$endif} DoReplaceNodes);
240 end;
241
242 procedure TKambiInlineNode.DoReplaceNodes(ParentNode: TX3DNode;
243 var Node: TX3DNode);
244 var
245 I: Integer;
246 begin
247 for I := 0 to ReplaceCount - 1 do
248 if (FdReplaceNames.Items[I] <> '') and
249 (FdReplaceNames.Items[I] = Node.X3DName) then
250 begin
251 Node := FdReplaceNodes[I];
252 Exit;
253 end;
254 end;
255
256 { TGeneratedShadowMapNode.TGenShadowMapGenTex -------------------------------- }
257
258 constructor TGeneratedShadowMapNode.TGenShadowMapGenTex.Create(const AParent: TGeneratedShadowMapNode);
259 begin
260 inherited Create(AParent);
261 FParent := AParent;
262 end;
263
264 function TGeneratedShadowMapNode.TGenShadowMapGenTex.GetUpdate: TTextureUpdate;
265 begin
266 Result := FParent.Update;
267 end;
268
269 procedure TGeneratedShadowMapNode.TGenShadowMapGenTex.SetUpdate(const Value: TTextureUpdate);
270 begin
271 FParent.Update := Value;
272 end;
273
274 { TGeneratedShadowMapNode ---------------------------------------------------- }
275
276 { Note that TGeneratedShadowMapNode.FdLight is not listed in
277 TGeneratedShadowMapNode.DirectEnumerateActive,
278 because the light doesn't shine here. We don't want
279 to override it's transform with transformation of a GeneratedShadowMap. }
280
281 const
282 CompareModeNames: array [TShadowMapCompareMode] of string = (
283 'COMPARE_R_LEQUAL',
284 'COMPARE_R_GEQUAL',
285 'NONE'
286 );
287
288 constructor TGeneratedShadowMapNode.Create(const AX3DName, ABaseUrl: String);
289 begin
290 inherited;
291 FGenTexFunctionality := TGenShadowMapGenTex.Create(Self);
292 AddFunctionality(FGenTexFunctionality);
293 end;
294
295 { TScreenEffectNode ---------------------------------------------------------- }
296
297 function TScreenEffectNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer;
298 begin
299 Result := inherited;
300 if Result <> nil then Exit;
301
302 Result := FFdShaders.Enumerate(Func);
303 if Result <> nil then Exit;
304 end;
305
306 { TODO: TScreenEffectNode stuff related to InternalRendererResource
307 is copy-pasted from TAbstractTextureNode.
308
309 There doesn't seem much opportunity to gain by sharing their code,
310 and we don't want to move this mechanism to TX3DNode for now to not
311 complicate API of all nodes.
312
313 This duplication seems OK for now. }
314
315 procedure TScreenEffectNode.SetInternalRendererResource(const Value: TInternalRendererResource);
316 begin
317 if FInternalRendererResource <> Value then
318 begin
319 FInternalRendererResource := Value;
320 if not FContextCloseEventSetup then
321 begin
322 FContextCloseEventSetup := true;
323 ApplicationProperties.OnGLContextCloseObject.Add(
324 {$ifdef FPC}@{$endif} GLContextCloseEvent);
325 end;
326 end;
327 end;
328
329 procedure TScreenEffectNode.GLContextCloseEvent(Sender: TObject);
330 begin
331 InternalRendererResourceFree;
332 end;
333
334 procedure TScreenEffectNode.InternalRendererResourceFree;
335 begin
336 FreeAndNil(FInternalRendererResource);
337 end;
338
339 destructor TScreenEffectNode.Destroy;
340 begin
341 GLContextCloseEvent(nil);
342 if FContextCloseEventSetup then
343 ApplicationProperties.OnGLContextCloseObject.Remove(
344 {$ifdef FPC}@{$endif} GLContextCloseEvent);
345 inherited;
346 end;
347
348 { TEffectNode ---------------------------------------------------------------- }
349
350 constructor TEffectNode.Create(const AX3DName, ABaseUrl: String);
351 begin
352 inherited;
353 HasInterfaceDeclarations := AllAccessTypes;
354 end;
355
356 procedure TEffectNode.GroupBeforeTraverse(const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean);
357 begin
358 inherited;
359 if State.Effects = nil then
360 State.Effects := TX3DNodeList.Create(false);
361 State.Effects.Add(Self);
362 end;
363
364 { TEffectPartNode ------------------------------------------------------------ }
365
366 constructor TEffectPartNode.Create(const AX3DName, ABaseUrl: String);
367 begin
368 inherited;
369 AddFunctionality(TUrlFunctionality.Create(Self));
370 FdUrl.AddNotification({$ifdef FPC}@{$endif} EventUrlReceive);
371 CDataField := FdUrl;
372 end;
373
374 function TEffectPartNode.GetContents: String;
375 begin
376 if not ContentsCached then
377 begin
378 ContentsCache := ShaderLoadContents(Self, FdUrl);
379 ContentsCached := true;
380 end;
381 Result := ContentsCache;
382 end;
383
384 procedure TEffectPartNode.SetContents(const Value: String);
385 begin
386 SetUrl(['data:text/plain,' + Value]);
387
388 //Assert(Value = GetContents);
389 // an optimized way to initialize ContentsCache
390 ContentsCache := Value;
391 ContentsCached := true;
392 end;
393
394 procedure TEffectPartNode.EventUrlReceive(
395 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
396 begin
397 ContentsCached := false;
398 end;
399
400 { TOrientationInterpolator2DNode ---------------------------------- }
401
402 function TOrientationInterpolator2DNode.KeyValueCount: TListSize;
403 begin
404 Result := FdKeyValue.Items.Count;
405 end;
406
407 procedure TOrientationInterpolator2DNode.InterpolatorLerp(const Target: TSFRotation;
408 const A: Single; const Index1, Index2: Integer);
409 var
410 OutputValueAngle: Single;
411 begin
412 OutputValueAngle := AngleLerp(A,
413 FdKeyValue.Items.L[Index1],
414 FdKeyValue.Items.L[Index2]);
415 Target.Value := Vector4(FdAxis.Value, OutputValueAngle);
416 end;
417
418 procedure TOrientationInterpolator2DNode.InterpolatorSet(const Target: TSFRotation; const Index: Integer);
419 var
420 OutputValueAngle: Single;
421 begin
422 OutputValueAngle := FdKeyValue.Items.L[Index];
423 Target.Value := Vector4(FdAxis.Value, OutputValueAngle);
424 end;
425
426 function TOrientationInterpolator2DNode.CreateOutputField: TSFRotation;
427 begin
428 Result := TSFRotation.CreateUndefined(Self, false, '');
429 end;
430
431 procedure RegisterKambiNodes;
432 begin
433 NodesManager.RegisterNodeClasses([
434 TText3DNode,
435 TBlendModeNode,
436 TKambiAppearanceNode,
437 TKambiInlineNode,
438 TKambiNavigationInfoNode,
439 TGeneratedShadowMapNode,
440 TProjectedTextureCoordinateNode,
441 TScreenEffectNode,
442 TMultiGeneratedTextureCoordinateNode,
443 TEffectNode,
444 TEffectPartNode,
445 TShaderTextureNode,
446 TViewpointMirrorNode,
447 TOrientationInterpolator2DNode
448 ]);
449 end;
450
451 {$endif read_implementation}