| 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 |
{ Spot light. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Spot light shines a cone of light, from a given location, along a direction |
| 23 |
(-Z in local coordinates). |
| 24 |
|
| 25 |
See @url(https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/lighting.html#SpotLight |
| 26 |
X3D SpotLight specification) for various details. } |
| 27 |
TCastleSpotLight = class(TCastlePunctualLight) |
| 28 |
strict private |
| 29 |
{ Same thing as FLightNode, but with proper type. } |
| 30 |
FSpotLight: TSpotLightNode; |
| 31 |
|
| 32 |
FGizmoCoord: TCoordinateNode; |
| 33 |
|
| 34 |
FColor: TCastleColorRGB; |
| 35 |
FAttenuation: TVector3; |
| 36 |
FIntensity: Single; |
| 37 |
FRadius: Single; |
| 38 |
FBeamWidth: Single; |
| 39 |
FCutOffAngle: Single; |
| 40 |
|
| 41 |
procedure SetColor(const Value: TCastleColorRGB); |
| 42 |
procedure SetAttenuation(const Value: TVector3); |
| 43 |
procedure SetIntensity(const Value: Single); |
| 44 |
procedure SetRadius(const Value: Single); |
| 45 |
procedure SetBeamWidth(const Value: Single); |
| 46 |
procedure SetCutOffAngle(const Value: Single); |
| 47 |
procedure UpdateGizmoCoord; |
| 48 |
public |
| 49 |
const |
| 50 |
DefaultBeamWidth = Pi * 3 / 16; |
| 51 |
DefaultCutOffAngle = Pi / 4; |
| 52 |
DefaultRadius = 100.0; |
| 53 |
DefaultIntensity = 100.0; |
| 54 |
|
| 55 |
constructor Create(AOwner: TComponent); override; |
| 56 |
destructor Destroy; override; |
| 57 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 58 |
|
| 59 |
{ Light color shining over surface. By default white. } |
| 60 |
property Color: TCastleColorRGB read FColor write SetColor; |
| 61 |
|
| 62 |
{ Attenuation determines how quickly light intensity falls off with distance. |
| 63 |
By default Vector3(0, 0, 1), which means light falls off according to inverse square law. } |
| 64 |
property Attenuation: TVector3 read FAttenuation write SetAttenuation; |
| 65 |
published |
| 66 |
{ Intensity, multiplied by color. Can be anything >= 0. |
| 67 |
Intuitively, how bright is the light. |
| 68 |
|
| 69 |
Following @url(https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/README.md glTF lights specification), |
| 70 |
the spot light's intensity physical unit |
| 71 |
is luminous intensity in candela (lm/sr). |
| 72 |
This is exactly like TCastlePointLight, but different than TCastleDirectionalLight. |
| 73 |
|
| 74 |
The default intensity is 100.0, which is a reasonable value for a point/spot light, |
| 75 |
it remains bright for a few meters around the light source. } |
| 76 |
property Intensity: Single read FIntensity write SetIntensity {$ifdef FPC}default DefaultIntensity{$endif}; |
| 77 |
|
| 78 |
{ Light doesn't shine at all outside of a sphere with this radius. |
| 79 |
For performance reasons, set this as small as possible, to avoid even considering |
| 80 |
this light at further shapes. |
| 81 |
Set to -1 to have no limit. } |
| 82 |
property Radius: Single read FRadius write SetRadius {$ifdef FPC}default DefaultRadius{$endif}; |
| 83 |
|
| 84 |
{ Angle within which the spot light has maximum intensity. |
| 85 |
|
| 86 |
If this is >= CutOffAngle (which is the default case), then the light |
| 87 |
has maximum intensity within CutOffAngle, and zero intensity outside CutOffAngle, |
| 88 |
and there it no smooth falloff. |
| 89 |
|
| 90 |
You need to make BeamWidth < CutOffAngle to have a smooth falloff at the egdes |
| 91 |
of the spot light. } |
| 92 |
property BeamWidth: Single read FBeamWidth write SetBeamWidth {$ifdef FPC}default DefaultBeamWidth{$endif}; |
| 93 |
|
| 94 |
{ Angle within which the spot light has any intensity. |
| 95 |
|
| 96 |
If BeamWidth < CutOffAngle then the light intensity falls off smoothly |
| 97 |
between BeamWidth and CutOffAngle. |
| 98 |
|
| 99 |
The light intensity is always zero outside of CutOffAngle, |
| 100 |
regardless of the BeamWidth value. } |
| 101 |
property CutOffAngle: Single read FCutOffAngle write SetCutOffAngle {$ifdef FPC}default DefaultCutOffAngle{$endif}; |
| 102 |
|
| 103 |
{$define read_interface_class} |
| 104 |
{$I auto_generated_persistent_vectors/tcastlespotlight_persistent_vectors.inc} |
| 105 |
{$undef read_interface_class} |
| 106 |
end; |
| 107 |
|
| 108 |
{$endif read_interface} |
| 109 |
|
| 110 |
{$ifdef read_implementation} |
| 111 |
|
| 112 |
constructor TCastleSpotLight.Create(AOwner: TComponent); |
| 113 |
|
| 114 |
function BuildGizmo: TX3DRootNode; |
| 115 |
begin |
| 116 |
Result := TX3DRootNode.Create; |
| 117 |
Result.AddChildren(Gizmo.LinesShape(FGizmoCoord)); |
| 118 |
UpdateGizmoCoord; |
| 119 |
end; |
| 120 |
|
| 121 |
begin |
| 122 |
inherited; |
| 123 |
|
| 124 |
// defaults |
| 125 |
FIntensity := DefaultIntensity; |
| 126 |
FRadius := DefaultRadius; |
| 127 |
FAttenuation := Vector3(0, 0, 1); |
| 128 |
FColor := WhiteRGB; |
| 129 |
FBeamWidth := DefaultBeamWidth; |
| 130 |
FCutOffAngle := DefaultCutOffAngle; |
| 131 |
|
| 132 |
// initialize light node |
| 133 |
FSpotLight := TSpotLightNode.Create; |
| 134 |
LightNode := FSpotLight; |
| 135 |
FPunctualLight := FSpotLight; |
| 136 |
|
| 137 |
// apply TCastleSpotLight defaults to FSpotLight |
| 138 |
FSpotLight.Color := FColor; |
| 139 |
FSpotLight.Attenuation := FAttenuation; |
| 140 |
FSpotLight.Intensity := FIntensity; |
| 141 |
FSpotLight.Radius := FRadius; |
| 142 |
FSpotLight.BeamWidth := FBeamWidth; |
| 143 |
FSpotLight.CutOffAngle := FCutOffAngle; |
| 144 |
|
| 145 |
// initialize Gizmo |
| 146 |
if Gizmo <> nil then |
| 147 |
begin |
| 148 |
if InternalCastleDesignData <> '' then |
| 149 |
Gizmo.SetIconUrl(InternalCastleDesignData + 'gizmos/light/light_spot.png'); |
| 150 |
Gizmo.LoadVisualization(BuildGizmo); |
| 151 |
end; |
| 152 |
|
| 153 |
{$define read_implementation_constructor} |
| 154 |
{$I auto_generated_persistent_vectors/tcastlespotlight_persistent_vectors.inc} |
| 155 |
{$undef read_implementation_constructor} |
| 156 |
end; |
| 157 |
|
| 158 |
destructor TCastleSpotLight.Destroy; |
| 159 |
begin |
| 160 |
{$define read_implementation_destructor} |
| 161 |
{$I auto_generated_persistent_vectors/tcastlespotlight_persistent_vectors.inc} |
| 162 |
{$undef read_implementation_destructor} |
| 163 |
inherited; |
| 164 |
end; |
| 165 |
|
| 166 |
function TCastleSpotLight.PropertySections(const PropertyName: String): TPropertySections; |
| 167 |
begin |
| 168 |
if (PropertyName = 'ColorPersistent') or |
| 169 |
(PropertyName = 'AttenuationPersistent') or |
| 170 |
(PropertyName = 'Intensity') or |
| 171 |
(PropertyName = 'Radius') or |
| 172 |
(PropertyName = 'BeamWidth') or |
| 173 |
(PropertyName = 'CutOffAngle') then |
| 174 |
Result := [psBasic] |
| 175 |
else |
| 176 |
Result := inherited PropertySections(PropertyName); |
| 177 |
end; |
| 178 |
|
| 179 |
procedure TCastleSpotLight.SetColor(const Value: TCastleColorRGB); |
| 180 |
begin |
| 181 |
if not TCastleColorRGB.PerfectlyEquals(FColor, Value) then |
| 182 |
begin |
| 183 |
FColor := Value; |
| 184 |
FSpotLight.Color := Value; |
| 185 |
end; |
| 186 |
end; |
| 187 |
|
| 188 |
procedure TCastleSpotLight.SetAttenuation(const Value: TVector3); |
| 189 |
begin |
| 190 |
if not TVector3.PerfectlyEquals(FAttenuation, Value) then |
| 191 |
begin |
| 192 |
FAttenuation := Value; |
| 193 |
FSpotLight.Attenuation := Value; |
| 194 |
end; |
| 195 |
end; |
| 196 |
|
| 197 |
procedure TCastleSpotLight.SetIntensity(const Value: Single); |
| 198 |
begin |
| 199 |
if FIntensity <> Value then |
| 200 |
begin |
| 201 |
FIntensity := Value; |
| 202 |
FSpotLight.Intensity := Value; |
| 203 |
end; |
| 204 |
end; |
| 205 |
|
| 206 |
procedure TCastleSpotLight.SetRadius(const Value: Single); |
| 207 |
begin |
| 208 |
if FRadius <> Value then |
| 209 |
begin |
| 210 |
FRadius := Value; |
| 211 |
FSpotLight.Radius := Value; |
| 212 |
end; |
| 213 |
end; |
| 214 |
|
| 215 |
procedure TCastleSpotLight.SetBeamWidth(const Value: Single); |
| 216 |
begin |
| 217 |
if FBeamWidth <> Value then |
| 218 |
begin |
| 219 |
FBeamWidth := Value; |
| 220 |
FSpotLight.BeamWidth := Value; |
| 221 |
end; |
| 222 |
end; |
| 223 |
|
| 224 |
procedure TCastleSpotLight.SetCutOffAngle(const Value: Single); |
| 225 |
begin |
| 226 |
if FCutOffAngle <> Value then |
| 227 |
begin |
| 228 |
FCutOffAngle := Value; |
| 229 |
FSpotLight.CutOffAngle := Value; |
| 230 |
if Gizmo <> nil then |
| 231 |
UpdateGizmoCoord; |
| 232 |
end; |
| 233 |
end; |
| 234 |
|
| 235 |
procedure TCastleSpotLight.UpdateGizmoCoord; |
| 236 |
const |
| 237 |
{ Length of gizmo line. |
| 238 |
We need to do AdjustToLength later, otherwise the lines could be ~infinite |
| 239 |
of case of CutOffAngle = Pi / 2. } |
| 240 |
D = 10.0; |
| 241 |
var |
| 242 |
Shift: Single; |
| 243 |
begin |
| 244 |
Shift := Tan(CutOffAngle) * D; |
| 245 |
FGizmoCoord.SetPoint([ |
| 246 |
Vector3(0, 0, 0), Vector3(0, Shift, -D).AdjustToLength(D), |
| 247 |
Vector3(0, 0, 0), Vector3(0, -Shift, -D).AdjustToLength(D), |
| 248 |
Vector3(0, 0, 0), Vector3( Shift, 0, -D).AdjustToLength(D), |
| 249 |
Vector3(0, 0, 0), Vector3(-Shift, 0, -D).AdjustToLength(D) |
| 250 |
]); |
| 251 |
end; |
| 252 |
|
| 253 |
{$define read_implementation_methods} |
| 254 |
{$I auto_generated_persistent_vectors/tcastlespotlight_persistent_vectors.inc} |
| 255 |
{$undef read_implementation_methods} |
| 256 |
|
| 257 |
{$endif read_implementation} |