Debian Codegraph @

src/scene/castlescene_directionallight.inc

raw
1 {%MainUnit castlescene.pas}
2 {
3 Copyright 2022-2024 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 { Directional light. }
18
19 {$ifdef read_interface}
20
21 type
22 { Directional light shines along a direction in 3D space, simulating
23 a light source far away (like a sun).
24 The direction is -Z (in local coordinates), simply rotate this
25 transformation to change the lighting direction.
26 The position of this transformation doesn't matter for lighting calculation
27 (though it is useful to display the light gizmo at design-time).
28
29 See @url(https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/lighting.html#DirectionalLight
30 X3D DirectionalLight specification) for various details. }
31 TCastleDirectionalLight = class(TCastlePunctualLight)
32 strict private
33 { Same thing as FLightNode, but with proper type. }
34 FDirectionalLight: TDirectionalLightNode;
35
36 FColor: TCastleColorRGB;
37 FIntensity: Single;
38
39 procedure SetColor(const Value: TCastleColorRGB);
40 procedure SetIntensity(const Value: Single);
41 public
42 const
43 DefaultIntensity = 1.0;
44
45 constructor Create(AOwner: TComponent); override;
46 destructor Destroy; override;
47 function PropertySections(const PropertyName: String): TPropertySections; override;
48
49 { Light color shining over surface. By default white. }
50 property Color: TCastleColorRGB read FColor write SetColor;
51 published
52 { Intensity, multiplied by color. Can be anything >= 0.
53 Intuitively, how bright is the light.
54
55 Note that the physical interpretation of what the @italic(intensity)
56 means for a directional light is a bit different than for
57 TCastlePointLight and TCastleSpotLight.
58 Directional light doesn't have a position, consequently it doesn't have
59 attenuation. Following @url(https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/README.md glTF lights specification),
60 the directional light's intensity physical unit
61 is illuminance in lux (lm/m2).
62
63 For the above reason, different values of intensity are "reasonable"
64 for directional light than for point and spot lights.
65 By default directional light intensity is 1.0, unlike point and spot lights
66 which have intensity 100.0 by default. }
67 property Intensity: Single read FIntensity write SetIntensity {$ifdef FPC}default DefaultIntensity{$endif};
68
69 {$define read_interface_class}
70 {$I auto_generated_persistent_vectors/tcastledirectionallight_persistent_vectors.inc}
71 {$undef read_interface_class}
72 end;
73
74 {$endif read_interface}
75
76 {$ifdef read_implementation}
77
78 constructor TCastleDirectionalLight.Create(AOwner: TComponent);
79
80 function BuildGizmo: TX3DRootNode;
81 begin
82 Result := TX3DRootNode.Create;
83 Result.AddChildren(Gizmo.LinesShape([
84 TVector3.Zero,
85 DefaultCameraDirection * 10
86 ]));
87 end;
88
89 begin
90 inherited;
91
92 // defaults
93 FIntensity := DefaultIntensity;
94 FColor := WhiteRGB;
95
96 // initialize light node
97 FDirectionalLight := TDirectionalLightNode.Create;
98 FDirectionalLight.Global := true;
99 LightNode := FDirectionalLight;
100 FPunctualLight := FDirectionalLight;
101
102 // apply TCastleDirectionalLight defaults to FDirectionalLight
103 FDirectionalLight.Color := FColor;
104 FDirectionalLight.Intensity := FIntensity;
105
106 // initialize Gizmo
107 if Gizmo <> nil then
108 begin
109 if InternalCastleDesignData <> '' then
110 Gizmo.SetIconUrl(InternalCastleDesignData + 'gizmos/light/light_directional.png');
111 Gizmo.LoadVisualization(BuildGizmo);
112 end;
113
114 {$define read_implementation_constructor}
115 {$I auto_generated_persistent_vectors/tcastledirectionallight_persistent_vectors.inc}
116 {$undef read_implementation_constructor}
117 end;
118
119 destructor TCastleDirectionalLight.Destroy;
120 begin
121 {$define read_implementation_destructor}
122 {$I auto_generated_persistent_vectors/tcastledirectionallight_persistent_vectors.inc}
123 {$undef read_implementation_destructor}
124 inherited;
125 end;
126
127 function TCastleDirectionalLight.PropertySections(const PropertyName: String): TPropertySections;
128 begin
129 if (PropertyName = 'ColorPersistent') or
130 (PropertyName = 'Intensity') then
131 Result := [psBasic]
132 else
133 Result := inherited PropertySections(PropertyName);
134 end;
135
136 procedure TCastleDirectionalLight.SetColor(const Value: TCastleColorRGB);
137 begin
138 if not TCastleColorRGB.PerfectlyEquals(FColor, Value) then
139 begin
140 FColor := Value;
141 FDirectionalLight.Color := Value;
142 end;
143 end;
144
145 procedure TCastleDirectionalLight.SetIntensity(const Value: Single);
146 begin
147 if FIntensity <> Value then
148 begin
149 FIntensity := Value;
150 FDirectionalLight.Intensity := Value;
151 end;
152 end;
153
154 {$define read_implementation_methods}
155 {$I auto_generated_persistent_vectors/tcastledirectionallight_persistent_vectors.inc}
156 {$undef read_implementation_methods}
157
158 {$endif read_implementation}