Debian Codegraph @

src/scene/castlescene_roottransform.inc

raw
1 {%MainUnit castlescene.pas}
2 {
3 Copyright 2010-2022 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 { TCastleRootTransform (with MainScene property that refers to TCastleScene). }
18
19 {$ifdef read_interface}
20
21 type
22 { Possible value of @link(TCastleRootTransform.UseHeadlight). }
23 TUseHeadlight = (
24 { Always show a headlight.
25 The headlight properties (color, intensity, shape)
26 are taken from @link(TCastleRootTransform.HeadlightNode). }
27 hlOn,
28
29 { Never show a headlight. }
30 hlOff,
31
32 { Show a headlight following the @link(TCastleRootTransform.MainScene)
33 properties.
34
35 If @link(TCastleRootTransform.MainScene) is @nil,
36 there is no headlight.
37
38 If @link(TCastleRootTransform.MainScene) is assigned,
39 the headlight is shown if
40 @link(TCastleSceneCore.HeadlightOn MainScene.HeadlightOn)
41 is @true.
42 @link(TCastleSceneCore.HeadlightOn MainScene.HeadlightOn) in turn
43 is configutable, and by default looks at X3D NavigationInfo.headlight field.
44 (If no NavigationInfo node is present, the headlight is shown,
45 as NavigationInfo.headlight is @true by default.)
46
47 The headlight properties (color, intensity, shape)
48 follow @link(TCastleSceneCore.CustomHeadlight MainScene.CustomHeadlight),
49 which can be customized in an X3D file using
50 https://castle-engine.io/x3d_implementation_navigation_extensions.php#section_ext_headlight .
51 If no @link(TCastleSceneCore.CustomHeadlight MainScene.CustomHeadlight)
52 is set, we use @link(TCastleRootTransform.HeadlightNode). }
53 hlMainScene
54 );
55
56 { Root of transformations and scenes (tree of TCastleTransform and TCastleScene). }
57 TCastleRootTransform = class(TCastleAbstractRootTransform)
58 strict private
59 FMainScene: TCastleScene;
60 { We observe freeing of MainScene using MainSceneObserver,
61 not using direct MainScene.FreeNotification(Self).
62 This way our RemoveFreeNotification doesn't accidentally remove the notification
63 which should remain because of other reasons
64 (when MainScene is our direct child, or when MainScene is part of our world,
65 which is actually almost always the case). }
66 MainSceneObserver: TFreeNotificationObserver;
67 FUseHeadlight: TUseHeadlight;
68 FHeadlightNode: TAbstractLightNode;
69 FInternalScenesCastGlobalLights: TCastleSceneList;
70 procedure SetMainScene(const Value: TCastleScene);
71 procedure MainScene_BoundViewpointChanged(Sender: TObject);
72 procedure MainScene_BoundViewpointVectorsChanged(Sender: TObject);
73 procedure MainScene_BoundNavigationInfoChanged(Sender: TObject);
74 function GetHeadlightNode: TAbstractLightNode;
75 procedure SetHeadlightNode(const Node: TAbstractLightNode);
76 procedure MainSceneFreeNotification(const Sender: TFreeNotificationObserver);
77 private
78 procedure RegisterCastGlobalLights(const S: TCastleScene);
79 procedure UnregisterCastGlobalLights(const S: TCastleScene);
80 protected
81 function IgnoreTransparentItem(
82 const Sender: TObject; const Triangle: PTriangle): Boolean; override;
83 procedure Loaded; override;
84 public
85 constructor Create(AOwner: TComponent); override;
86 destructor Destroy; override;
87 function PropertySections(const PropertyName: String): TPropertySections; override;
88
89 { @exclude
90 TCastleScene instances that have TCastleScene.CastGlobalLights.
91 May be @nil, equivalent to empty. }
92 property InternalScenesCastGlobalLights: TCastleSceneList read FInternalScenesCastGlobalLights;
93
94 { Determines the headlight look, if we use a headlight
95 (which is determined by the algorithm described at @link(UseHeadlight) and
96 @link(TUseHeadlight)).
97 By default it's a simplest directional headlight,
98 but you can customize it, and thus you can use a point light
99 or a spot light for a headlight.
100 Just like https://castle-engine.io/x3d_implementation_navigation_extensions.php#section_ext_headlight .
101
102 This is never @nil.
103 Assigning here @nil simply causes us to recreate it using
104 the simplest directional headlight. }
105 property HeadlightNode: TAbstractLightNode
106 read GetHeadlightNode write SetHeadlightNode;
107
108 { Headlight used to light the scene.
109
110 Returns non-nil headlight node
111 if the algorithm described at @link(UseHeadlight) and
112 @link(TUseHeadlight) indicates we should use a headlight.
113 Otherwise returns @nil, to indicate we do not show a headlight now.
114
115 @exclude }
116 function InternalHeadlight: TAbstractLightNode;
117 published
118 { The main scene of the world. It's not necessary to set this.
119 It adds some optional features that require a notion of
120 the "main" scene to make sense.
121
122 The scene you set here @italic(must) also be added to our @link(Items).
123
124 The MainScene is used for a couple of things:
125
126 @unorderedList(
127 @item(Determines initial @link(TCastleViewport.Camera) (if @link(TCastleViewport.AutoCamera))
128 and @link(TCastleViewport.Navigation) (if @link(TCastleAutoNavigationViewport.AutoNavigation)) properties.
129 This includes camera position, orientation, projection.
130 This includes navigation type (examine, walk, fly), move speed.
131 These follow the X3D Viewpoint, OrthoViewpoint
132 and NavigationInfo nodes defined inside the MainScene.
133
134 Later changes to nodes Viewpoint/NavigatioInfo using X3D events inside
135 MainScene may also influence the @link(TCastleViewport.Camera) and @link(TCastleViewport.Navigation).
136 For example in X3D you can animate the Viewpoint position,
137 and it will actually animate our @link(TCastleViewport.Camera) position.
138
139 If both @link(TCastleViewport.AutoCamera) and
140 @link(TCastleAutoNavigationViewport.AutoNavigation) are @false,
141 none of these things are applied.
142
143 @bold(Instead of relying on MainScene for this,
144 we advise to leave
145 @link(TCastleViewport.AutoCamera) and
146 @link(TCastleAutoNavigationViewport.AutoNavigation) as @false,
147 and set proper camera and navigation at design-time.
148 If you want to animate the camera usin animation designed in glTF or X3D,
149 then make TCastleCamera a parent of animated transformation exposed using
150 @link(TCastleSceneCore.ExposeTransforms).
151 )
152 )
153
154 @item(Determines what background is rendered.
155 If the MainScene contains an X3D Background node (or other
156 node descending from TAbstractBackgroundNode),
157 it will be used.
158
159 This is overridden if you set @link(TCastleViewport.Background)
160 to non-nil. MainScene's background is ignored in this case.
161
162 When neither MainScene background is present,
163 nor @link(TCastleViewport.Background) is specified,
164 then we render a background using @link(TCastleViewport.BackgroundColor).
165
166 Note that when @link(TCastleViewport.Transparent) is @true,
167 we never render any background (neither from MainScene,
168 nor from @link(TCastleViewport.BackgroundColor)).
169
170 @bold(Instead of relying on MainScene for this,
171 we advise to set @link(TCastleViewport.Background).)
172 )
173
174 @item(Determines whether headlight is used if @link(UseHeadlight)
175 is hlMainScene. The value of
176 @link(TCastleSceneCore.HeadlightOn MainScene.HeadlightOn)
177 then determines the headlight.
178 The initial
179 @link(TCastleSceneCore.HeadlightOn MainScene.HeadlightOn)
180 value depends on the X3D NavigationInfo node inside MainScene.
181
182 @bold(Instead of relying on MainScene for this,
183 we advise to add light instance (like TCastleDirectionalLight) as TCastleCamera child.)
184 )
185
186 @item(Determines the main light casting shadow volumes.)
187
188 @item(Determines lights shining on all scenes,
189 as this scene has by default @link(TCastleScene.CastGlobalLights)
190 when you set it as MainScene.
191 This can also be achieved on any other scene too,
192 by setting @link(TCastleScene.CastGlobalLights).
193
194 @bold(Instead of relying on MainScene for lights,
195 we advise to setup light instances (TCastleDirectionalLight, TCastlePointLight,
196 TCastleSpotLight) in viewport items and/or set @link(TCastleScene.CastGlobalLights)
197 on some scenes.)
198 )
199
200 @item(Determines fog on all scenes, if @link(TCastleViewport.UseGlobalFog).)
201
202 @item(The screen effects defined inside MainScene (TScreenEffectNode)
203 are automatically used.
204
205 @bold(Instead of relying on MainScene for this,
206 add screen effects using @link(TCastleScreenEffects.AddScreenEffect).)
207 )
208
209 @item(Main scene is informed about pointing device move,
210 even when the pointing device is @italic(not) over this scene,
211 and this scene is @italic(not) capturing pointing device events
212 (the scene "capturing" events once you click on it).
213 )
214 )
215
216 Freeing MainScene will automatically set this property to @nil. }
217 property MainScene: TCastleScene read FMainScene write SetMainScene;
218 {$ifdef FPC} deprecated 'leave MainScene unset; almost all useful MainScene features can be now achieved without setting MainScene'; {$endif}
219
220 { Whether the headlight is shown, see @link(TUseHeadlight) for possible values. }
221 property UseHeadlight: TUseHeadlight
222 read FUseHeadlight write FUseHeadlight default hlMainScene;
223 {$ifdef FPC} deprecated 'create TCastleDirectionalLight and add it as TCastleCamera child, for a more flexible headlight'; {$endif}
224 end;
225
226 {$endif read_interface}
227
228 {$ifdef read_implementation}
229
230 { TCastleRootTransform ------------------------------------------------------- }
231
232 constructor TCastleRootTransform.Create(AOwner: TComponent);
233 begin
234 inherited;
235 MainSceneObserver := TFreeNotificationObserver.Create(Self);
236 MainSceneObserver.OnFreeNotification := {$ifdef FPC}@{$endif}MainSceneFreeNotification;
237 FUseHeadlight := hlMainScene;
238 end;
239
240 destructor TCastleRootTransform.Destroy;
241 begin
242 { unregister self from MainScene callbacs,
243 make MainScene.RemoveFreeNotification(Self)... this is all
244 done by SetMainScene(nil) already. }
245 {$warnings off} // using deprecated to keep it working
246 MainScene := nil;
247 {$warnings on}
248
249 FreeIfUnusedAndNil(FHeadlightNode);
250 FreeAndNil(FInternalScenesCastGlobalLights);
251 inherited;
252 end;
253
254 procedure TCastleRootTransform.SetMainScene(const Value: TCastleScene);
255 begin
256 if FMainScene <> Value then
257 begin
258 MainSceneObserver.Observed := Value;
259
260 if FMainScene <> nil then
261 begin
262 FMainScene.OnBoundViewpointVectorsChanged := nil;
263 FMainScene.OnBoundNavigationInfoFieldsChanged := nil;
264 { Check FMainScene.*Stack <> nil,
265 because this may happen from MainScene destruction notification,
266 when *Stack is already freed. }
267 if FMainScene.ViewpointStack <> nil then
268 FMainScene.ViewpointStack.OnBoundChanged := nil;
269 if FMainScene.NavigationInfoStack <> nil then
270 FMainScene.NavigationInfoStack.OnBoundChanged := nil;
271 end;
272
273 FMainScene := Value;
274
275 if FMainScene <> nil then
276 begin
277 FMainScene.OnBoundViewpointVectorsChanged := {$ifdef FPC}@{$endif}MainScene_BoundViewpointVectorsChanged;
278 FMainScene.OnBoundNavigationInfoFieldsChanged := {$ifdef FPC}@{$endif}MainScene_BoundNavigationInfoChanged;
279 FMainScene.ViewpointStack.OnBoundChanged := {$ifdef FPC}@{$endif}MainScene_BoundViewpointChanged;
280 FMainScene.NavigationInfoStack.OnBoundChanged := {$ifdef FPC}@{$endif}MainScene_BoundNavigationInfoChanged;
281 end;
282
283 { Setting MainScene may change lights, if MainScene determines the headlight
284 when UseHeadlight = hlMainScene.
285 So we use vcVisibleNonGeometry.
286 }
287 VisibleChangeHere([vcVisibleNonGeometry]);
288 end;
289 end;
290
291 procedure TCastleRootTransform.MainSceneFreeNotification(
292 const Sender: TFreeNotificationObserver);
293 begin
294 {$warnings off} // using deprecated to keep it working
295 MainScene := nil;
296 {$warnings on}
297 end;
298
299 procedure TCastleRootTransform.MainScene_BoundViewpointChanged(Sender: TObject);
300 begin
301 if (MainCamera <> nil) and
302 Assigned(MainCamera.InternalOnSceneBoundViewpointChanged) then
303 MainCamera.InternalOnSceneBoundViewpointChanged(Sender);
304 end;
305
306 procedure TCastleRootTransform.MainScene_BoundViewpointVectorsChanged(Sender: TObject);
307 begin
308 if (MainCamera <> nil) and
309 Assigned(MainCamera.InternalOnSceneBoundViewpointVectorsChanged) then
310 MainCamera.InternalOnSceneBoundViewpointVectorsChanged(Sender);
311 end;
312
313 procedure TCastleRootTransform.MainScene_BoundNavigationInfoChanged(Sender: TObject);
314 begin
315 if (MainCamera <> nil) and
316 Assigned(MainCamera.InternalOnSceneBoundNavigationInfoChanged) then
317 MainCamera.InternalOnSceneBoundNavigationInfoChanged(Sender);
318 end;
319
320 function TCastleRootTransform.GetHeadlightNode: TAbstractLightNode;
321 begin
322 { HeadlightNode is never nil, so recreate it now if nil. }
323
324 if FHeadlightNode = nil then
325 { Nothing more needed, all DirectionalLight default properties
326 are suitable for default headlight. }
327 FHeadlightNode := TDirectionalLightNode.Create;
328
329 Result := FHeadlightNode;
330 end;
331
332 procedure TCastleRootTransform.SetHeadlightNode(const Node: TAbstractLightNode);
333 begin
334 if FHeadlightNode <> Node then
335 begin
336 FreeIfUnusedAndNil(FHeadlightNode);
337 FHeadlightNode := Node;
338 end;
339 end;
340
341 function TCastleRootTransform.InternalHeadlight: TAbstractLightNode;
342 begin
343 Result := nil;
344
345 {$warnings off} // using deprecated to keep it working
346 case UseHeadlight of
347 hlOn : Result := HeadlightNode;
348 hlOff: Result := nil;
349 hlMainScene:
350 if (MainScene <> nil) and MainScene.HeadlightOn then
351 begin
352 Result := MainScene.CustomHeadlight;
353 if Result = nil then
354 Result := HeadlightNode;
355 Assert(Result <> nil);
356 end;
357 {$ifndef COMPILER_CASE_ANALYSIS}
358 else raise EInternalError.Create(2018081902);
359 {$endif}
360 end;
361 {$warnings on}
362 end;
363
364 function TCastleRootTransform.PropertySections(const PropertyName: String): TPropertySections;
365 begin
366 // These are deprecated now
367 // if (PropertyName = 'MainScene') or
368 // (PropertyName = 'UseHeadlight') then
369 // Result := [psBasic]
370 // else
371
372 Result := inherited PropertySections(PropertyName);
373 end;
374
375 function TCastleRootTransform.IgnoreTransparentItem(
376 const Sender: TObject; const Triangle: PTriangle): Boolean;
377 begin
378 Result := Triangle^.IsTransparent;
379 end;
380
381 procedure TCastleRootTransform.RegisterCastGlobalLights(const S: TCastleScene);
382 begin
383 if FInternalScenesCastGlobalLights = nil then
384 FInternalScenesCastGlobalLights := TCastleSceneList.Create(false);
385 FInternalScenesCastGlobalLights.Add(S);
386 end;
387
388 procedure TCastleRootTransform.UnregisterCastGlobalLights(const S: TCastleScene);
389 var
390 I: Integer;
391 begin
392 if FInternalScenesCastGlobalLights = nil then
393 I := -1
394 else
395 I := FInternalScenesCastGlobalLights.IndexOf(S);
396
397 if I = -1 then
398 begin
399 WritelnWarning('Scene called UnregisterCastGlobalLights, but it was not on the list InternalScenesCastGlobalLights');
400 Exit;
401 end;
402
403 FInternalScenesCastGlobalLights.Delete(I);
404 end;
405
406 procedure TCastleRootTransform.Loaded;
407 begin
408 inherited;
409 {$warnings off} // using deprecated just to make a warning
410 if UseHeadlight = hlOn then
411 WritelnWarning('UseHeadlight is deprecated (on TCastleViewport.Items). Instead: Add light component as a child of TCastleCamera');
412 if MainScene <> nil then
413 WritelnWarning('MainScene is deprecated (on TCastleViewport.Items). Instead: Almost all features are available without the need to set MainScene');
414 {$warnings on}
415 if not (
416 TVector3.Equals(Translation, TVector3.Zero) and
417 (Rotation.W = 0) and
418 TVector3.Equals(Scale, NoScale)
419 ) then
420 WritelnWarning('Viewport.Items (TCastleRootTransform) has some transformation (non-zero translation, rotation or non-identity scale). ' +
421 'While allowed, this is known to cause a bit weird behavior at design-time (as the root transformation affects all, even internal, cameras). ' +
422 'We advise to leave the TCastleRootTransform untransformed in most cases.');
423 end;
424
425 {$endif read_implementation}