Debian Codegraph @

src/scene/x3d/x3dnodes_standard_environmentaleffects.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-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 {$ifdef read_interface}
18 { Side of the background (skybox), used with TBackgroundNode. }
19 TBackgroundSide = (bsBack, bsBottom, bsFront, bsLeft, bsRight, bsTop);
20
21 { Abstract type from which all backgrounds inherit. }
22 TAbstractBackgroundNode = class(TAbstractBindableNode)
23 strict private
24 procedure EventSet_BindReceive(
25 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
26 protected
27 function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override;
28 public
29 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
30
31 { Extract only rotation (axis-angle format) from the transformation. }
32 function TransformRotation: TVector4;
33
34 {$I auto_generated_node_helpers/x3dnodes_x3dbackgroundnode.inc}
35 end;
36
37 { Abstract type from which 3D backgrounds inherit. }
38 TAbstract3DBackgroundNode = class(TAbstractBackgroundNode)
39 public
40 function TransformationChange: TNodeTransformationChange; override;
41
42 { Texture node for given background side.
43 May return @nil if none.
44
45 It is the caller's responsibility to take care of freeing the result,
46 but only if it's not otherwise used. In other words, use TX3DNode.FreeIfUnused
47 (or some routine based on it), unless you're sure that you always
48 make the returned node a child of another node (in this case
49 the returned node is always used, and remains managed by parent node). }
50 function Texture(const Side: TBackgroundSide): TAbstractTextureNode; virtual; abstract;
51
52 {$I auto_generated_node_helpers/x3dnodes_x3d3dbackgroundnode.inc}
53 end;
54
55 TFogTypeOrNone = (ftLinear, ftExponential, ftNone);
56 TFogType = ftLinear..ftExponential;
57
58 { Functionality of node that describes a fog (blend objects with a fog color).
59 Used by various X3D fog nodes, like TLocalFogNode, TFogNode. }
60 TFogFunctionality = class(TNodeFunctionality)
61 public
62 function Color: TCastleColorRGB; virtual; abstract;
63 function FogType: TFogType; virtual; abstract;
64 function VisibilityRange: Single; virtual; abstract;
65 function TransformScale: Single; virtual; abstract;
66 function Volumetric: Boolean; virtual; abstract;
67 function VolumetricDirection: TVector3; virtual; abstract;
68 function VolumetricVisibilityStart: Single; virtual; abstract;
69 end;
70
71 { 3D background of a scene, comprised of sky and ground colors (gradients)
72 and optional six textures (skybox). }
73 TBackgroundNode = class(TAbstract3DBackgroundNode)
74 public
75 function Texture(const Side: TBackgroundSide): TAbstractTextureNode; override;
76 {$I auto_generated_node_helpers/x3dnodes_background.inc}
77 end;
78
79 { Simulate atmospheric fog effects (for the whole scene) by blending
80 with the fog colour, based on the distance from the viewer. }
81 TFogNode = class(TAbstractBindableNode)
82 strict private
83 type
84 TFogNodeFogFunctionality = class(TFogFunctionality)
85 strict private
86 FParent: TFogNode;
87 public
88 constructor Create(const AParent: TFogNode);
89 property Parent: TFogNode read FParent;
90
91 function Color: TCastleColorRGB; override;
92 function FogType: TFogType; override;
93 function VisibilityRange: Single; override;
94 function TransformScale: Single; override;
95 function Volumetric: Boolean; override;
96 function VolumetricDirection: TVector3; override;
97 function VolumetricVisibilityStart: Single; override;
98 end;
99
100 procedure EventSet_BindReceive(
101 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
102 public
103 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
104 function TransformationChange: TNodeTransformationChange; override;
105
106 { Fog type, but may also return ftNone if we are @nil (checks Self <> nil)
107 or visibilityRange = 0. }
108 function FogTypeOrNone: TFogTypeOrNone;
109
110 { Apply fog to the color of the vertex.
111
112 This can be used by software renderers (ray-tracers etc.)
113 to calculate pixel color following VRML/X3D specifications.
114 After all the lighting is summed up (see TX3DGraphTraverseState.Emission and
115 TLightInstance.Contribution), process color by this method to apply fog.
116
117 Does nothing if AFogType = ftNone. In this special case, it's also
118 allowed to call this on @nil instance. }
119 procedure ApplyFog(var Color: TVector3;
120 const Position, VertexPos: TVector3; const AFogType: TFogTypeOrNone);
121
122 {$I auto_generated_node_helpers/x3dnodes_fog.inc}
123 end;
124
125 { Provide explicit fog depths on a per-vertex basis. }
126 TFogCoordinateNode = class(TAbstractGeometricPropertyNode)
127 {$I auto_generated_node_helpers/x3dnodes_fogcoordinate.inc}
128 end;
129
130 { Simulate atmospheric fog effects (for a part of the scene) by blending
131 with the fog colour, based on the distance from the viewer.
132 This is the "local" version of the @link(TFogNode), this affects
133 only the sibling nodes of the @code(LocalFog) node, not the whole scene. }
134 TLocalFogNode = class(TAbstractChildNode)
135 strict private
136 type
137 TLocalFogNodeFogFunctionality = class(TFogFunctionality)
138 strict private
139 FParent: TLocalFogNode;
140 public
141 constructor Create(const AParent: TLocalFogNode);
142 property Parent: TLocalFogNode read FParent;
143
144 function Color: TCastleColorRGB; override;
145 function FogType: TFogType; override;
146 function VisibilityRange: Single; override;
147 function TransformScale: Single; override;
148 function Volumetric: Boolean; override;
149 function VolumetricDirection: TVector3; override;
150 function VolumetricVisibilityStart: Single; override;
151 end;
152
153 var
154 FTransformScale: Single;
155 protected
156 procedure BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); override;
157 procedure GroupBeforeTraverse(const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean); override;
158 public
159 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
160
161 {$I auto_generated_node_helpers/x3dnodes_localfog.inc}
162 end;
163
164 { 3D background of a scene, comprised of sky and ground colors (gradients)
165 and optional six textures (skybox), with flexible texture nodes.
166 This is somewhat more flexible version of the @link(TBackgroundNode),
167 with this you can use e.g. @link(TMovieTextureNode) as the skybox textures. }
168 TTextureBackgroundNode = class(TAbstract3DBackgroundNode)
169 public
170 function Texture(const Side: TBackgroundSide): TAbstractTextureNode; override;
171 {$I auto_generated_node_helpers/x3dnodes_texturebackground.inc}
172 end;
173
174 {$endif read_interface}
175
176 {$ifdef read_implementation}
177
178 { TAbstractBackgroundNode ---------------------------------------------------- }
179
180 constructor TAbstractBackgroundNode.Create(const AX3DName, ABaseUrl: String);
181 begin
182 inherited;
183 Eventset_bind.AddNotification({$ifdef FPC}@{$endif} EventSet_BindReceive);
184 end;
185
186 function TAbstractBackgroundNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer;
187 begin
188 Result := inherited;
189 if Result <> nil then Exit;
190
191 Result := FFdEffects.Enumerate(Func);
192 if Result <> nil then Exit;
193
194 Result := FFdShaders.Enumerate(Func);
195 if Result <> nil then Exit;
196 end;
197
198 procedure TAbstractBackgroundNode.EventSet_BindReceive(
199 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
200 begin
201 if Scene <> nil then
202 begin
203 Scene.GetBackgroundStack.Set_Bind(Self, (Value as TSFBool).Value);
204 { Background is something visible, so we must actually redisplay
205 for user to see the new scene. This is not done by Set_Bind method
206 automatically, as this is specific to Background node. }
207 Scene.VisibleChangeHere([vcVisibleNonGeometry]);
208 end;
209 end;
210
211 function TAbstractBackgroundNode.TransformRotation: TVector4;
212 var
213 IgnoreTranslation, IgnoreScale: TVector3;
214 begin
215 MatrixDecompose(Transform, IgnoreTranslation, Result, IgnoreScale);
216 end;
217
218 { TAbstract3DBackgroundNode -------------------------------------------------- }
219
220 function TAbstract3DBackgroundNode.TransformationChange: TNodeTransformationChange;
221 begin
222 Result := ntcBackground;
223 end;
224
225 { TBackgroundNode ------------------------------------------------------------ }
226
227 function TBackgroundNode.Texture(const Side: TBackgroundSide): TAbstractTextureNode;
228 var
229 TextureField: TMFString;
230 ResultImgTex: TImageTextureNode;
231 begin
232 case Side of
233 bsBack : TextureField := FdBackUrl;
234 bsBottom: TextureField := FdBottomUrl;
235 bsFront : TextureField := FdFrontUrl;
236 bsLeft : TextureField := FdLeftUrl;
237 bsRight : TextureField := FdRightUrl;
238 bsTop : TextureField := FdTopUrl;
239 {$ifndef COMPILER_CASE_ANALYSIS}
240 else raise EInternalError.Create('TTextureBackgroundNode.Texture:Side?');
241 {$endif}
242 end;
243 if TextureField.Items.Count <> 0 then
244 begin
245 ResultImgTex := TImageTextureNode.Create('', BaseUrl);
246 ResultImgTex.FdUrl.Assign(TextureField);
247 Result := ResultImgTex;
248 end else
249 Result := nil;
250 end;
251
252 { TFogNode.TFogNodeFogFunctionality ------------------------------------------ }
253
254 constructor TFogNode.TFogNodeFogFunctionality.Create(const AParent: TFogNode);
255 begin
256 inherited Create(AParent);
257 FParent := AParent;
258 end;
259
260 function TFogNode.TFogNodeFogFunctionality.Color: TCastleColorRGB;
261 begin
262 Result := Parent.Color;
263 end;
264
265 function TFogNode.TFogNodeFogFunctionality.FogType: TFogType;
266 begin
267 Result := Parent.FogType;
268 end;
269
270 function TFogNode.TFogNodeFogFunctionality.VisibilityRange: Single;
271 begin
272 Result := Parent.VisibilityRange;
273 end;
274
275 function TFogNode.TFogNodeFogFunctionality.TransformScale: Single;
276 begin
277 Result := Parent.TransformScale;
278 end;
279
280 function TFogNode.TFogNodeFogFunctionality.Volumetric: Boolean;
281 begin
282 Result := Parent.Volumetric;
283 end;
284
285 function TFogNode.TFogNodeFogFunctionality.VolumetricDirection: TVector3;
286 begin
287 Result := Parent.VolumetricDirection;
288 end;
289
290 function TFogNode.TFogNodeFogFunctionality.VolumetricVisibilityStart: Single;
291 begin
292 Result := Parent.VolumetricVisibilityStart;
293 end;
294
295 { TFogNode ------------------------------------------------------------------- }
296
297 const
298 FogTypeNames: array [TFogType] of String = ('LINEAR', 'EXPONENTIAL');
299
300 constructor TFogNode.Create(const AX3DName, ABaseUrl: String);
301 begin
302 inherited;
303 Eventset_bind.AddNotification({$ifdef FPC}@{$endif} EventSet_BindReceive);
304 AddFunctionality(TFogNode.TFogNodeFogFunctionality.Create(Self));
305 end;
306
307 procedure TFogNode.EventSet_BindReceive(
308 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
309 begin
310 if Scene <> nil then
311 begin
312 Scene.GetFogStack.Set_Bind(Self, (Value as TSFBool).Value);
313 { Fog is something visible, so we must actually redisplay
314 for user to see the new scene. This is not done by Set_Bind method
315 automatically, as this is specific to Fog node. }
316 Scene.VisibleChangeHere([vcVisibleNonGeometry]);
317 end;
318 end;
319
320 function TFogNode.TransformationChange: TNodeTransformationChange;
321 begin
322 Result := ntcFog;
323 end;
324
325 function TFogNode.FogTypeOrNone: TFogTypeOrNone;
326 begin
327 if (Self = nil) or
328 (VisibilityRange = 0.0) then
329 Exit(ftNone);
330
331 Result := FogType;
332 end;
333
334 procedure TFogNode.ApplyFog(var Color: TVector3;
335 const Position, VertexPos: TVector3; const AFogType: TFogTypeOrNone);
336 var
337 FogVisibilityRangeScaled: Single;
338
339 procedure ApplyDistance(const Distance: Single);
340 var
341 F: Single;
342 begin
343 case AFogType of
344 ftLinear : F := (FogVisibilityRangeScaled - Distance) / FogVisibilityRangeScaled;
345 ftExponential: F := Exp(-Distance / (FogVisibilityRangeScaled - Distance));
346 ftNone : F := 1;
347 {$ifndef COMPILER_CASE_ANALYSIS}
348 else raise EInternalError.Create('AFogType?');
349 {$endif}
350 end;
351 Color := Lerp(F, FdColor.Value, Color);
352 end;
353
354 var
355 FogVolumetricVisibilityStart: Single;
356 Distance, DistanceSqr: Single;
357 VertProjected: TVector3;
358 begin
359 if AFogType <> ftNone then
360 begin
361 FogVisibilityRangeScaled := FdVisibilityRange.Value * TransformScale;
362
363 if FdVolumetric.Value then
364 begin
365 FogVolumetricVisibilityStart := FdVolumetricVisibilityStart.Value * TransformScale;
366
367 { Calculation of Distance for volumetric fog below is analogous to
368 GetFogVolumetric inside ArraysGenerator. }
369
370 VertProjected := PointOnLineClosestToPoint(
371 TVector3.Zero, FdVolumetricDirection.Value, VertexPos);
372 Distance := VertProjected.Length;
373 if not AreParallelVectorsSameDirection(
374 VertProjected, FdVolumetricDirection.Value) then
375 Distance := -Distance;
376 { Now I want
377 - Distance = FogVolumetricVisibilityStart -> 0
378 - Distance = FogVolumetricVisibilityStart + X -> X
379 (so that Distance = FogVolumetricVisibilityStart +
380 FogVisibilityRangeScaled -> FogVisibilityRangeScaled) }
381 Distance := Distance - FogVolumetricVisibilityStart;
382
383 { When Distance < 0 our intention is to have no fog.
384 So Distance < 0 should be equivalent to Distance = 0. }
385 MaxVar(Distance, 0);
386
387 if Distance >= FogVisibilityRangeScaled - SingleEpsilon then
388 Color := FdColor.Value else
389 ApplyDistance(Distance);
390 end else
391 begin
392 DistanceSqr := PointsDistanceSqr(Position, VertexPos);
393
394 if DistanceSqr >= Sqr(FogVisibilityRangeScaled - SingleEpsilon) then
395 Color := FdColor.Value else
396 ApplyDistance(Sqrt(DistanceSqr));
397
398 end;
399 end;
400 end;
401
402 { TLocalFogNode.TLocalFogNodeFogFunctionality ------------------------------------------ }
403
404 constructor TLocalFogNode.TLocalFogNodeFogFunctionality.Create(const AParent: TLocalFogNode);
405 begin
406 inherited Create(AParent);
407 FParent := AParent;
408 end;
409
410 function TLocalFogNode.TLocalFogNodeFogFunctionality.Color: TCastleColorRGB;
411 begin
412 Result := Parent.Color;
413 end;
414
415 function TLocalFogNode.TLocalFogNodeFogFunctionality.FogType: TFogType;
416 begin
417 Result := Parent.FogType;
418 end;
419
420 function TLocalFogNode.TLocalFogNodeFogFunctionality.VisibilityRange: Single;
421 begin
422 Result := Parent.VisibilityRange;
423 end;
424
425 function TLocalFogNode.TLocalFogNodeFogFunctionality.TransformScale: Single;
426 begin
427 { TODO: this is actually bad, each LocalFog occurrence may have
428 different scale, depending on parent transform node.
429 We should keep LocalFogScale in State, and pass it around. }
430 Result := Parent.FTransformScale;
431 end;
432
433 function TLocalFogNode.TLocalFogNodeFogFunctionality.Volumetric: Boolean;
434 begin
435 Result := Parent.Volumetric;
436 end;
437
438 function TLocalFogNode.TLocalFogNodeFogFunctionality.VolumetricDirection: TVector3;
439 begin
440 Result := Parent.VolumetricDirection;
441 end;
442
443 function TLocalFogNode.TLocalFogNodeFogFunctionality.VolumetricVisibilityStart: Single;
444 begin
445 Result := Parent.VolumetricVisibilityStart;
446 end;
447
448 { TLocalFogNode -------------------------------------------------------------- }
449
450 constructor TLocalFogNode.Create(const AX3DName, ABaseUrl: String);
451 begin
452 inherited;
453 AddFunctionality(TLocalFogNode.TLocalFogNodeFogFunctionality.Create(Self));
454 FTransformScale := 1;
455 end;
456
457 procedure TLocalFogNode.BeforeTraverse(StateStack: TX3DGraphTraverseStateStack);
458 begin
459 inherited;
460 FTransformScale := StateStack.Top.Transformation.Scale;
461 end;
462
463 procedure TLocalFogNode.GroupBeforeTraverse(const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean);
464 begin
465 inherited;
466 State.LocalFog := Self;
467 end;
468
469 { TTextureBackgroundNode ----------------------------------------------------- }
470
471 function TTextureBackgroundNode.Texture(const Side: TBackgroundSide): TAbstractTextureNode;
472 var
473 TextureField: TSFNode;
474 begin
475 case Side of
476 bsBack : TextureField := FdBackTexture;
477 bsBottom: TextureField := FdBottomTexture;
478 bsFront : TextureField := FdFrontTexture;
479 bsLeft : TextureField := FdLeftTexture;
480 bsRight : TextureField := FdRightTexture;
481 bsTop : TextureField := FdTopTexture;
482 {$ifndef COMPILER_CASE_ANALYSIS}
483 else raise EInternalError.Create('TTextureBackgroundNode.Texture:Side?');
484 {$endif}
485 end;
486 if TextureField.Value is TAbstractTextureNode then
487 Result := TAbstractTextureNode(TextureField.Value) else
488 Result := nil;
489 end;
490
491 { registration --------------------------------------------------------------- }
492
493 procedure RegisterEnvironmentalEffectsNodes;
494 begin
495 NodesManager.RegisterNodeClasses([
496 TBackgroundNode,
497 TFogNode,
498 TFogCoordinateNode,
499 TLocalFogNode,
500 TTextureBackgroundNode
501 ]);
502 end;
503
504 {$endif read_implementation}