| 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 |
{ Point light. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Point light is a point in 3D space that shines uniformly in all directions. |
| 23 |
The rotation of this transformation doesn't matter. |
| 24 |
|
| 25 |
See @url(https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/lighting.html#PointLight |
| 26 |
X3D PointLight specification) for various details. } |
| 27 |
TCastlePointLight = class(TCastlePunctualLight) |
| 28 |
strict private |
| 29 |
{ Same thing as FLightNode, but with proper type. } |
| 30 |
FPointLight: TPointLightNode; |
| 31 |
|
| 32 |
FColor: TCastleColorRGB; |
| 33 |
FAttenuation: TVector3; |
| 34 |
FIntensity: Single; |
| 35 |
FRadius: Single; |
| 36 |
|
| 37 |
procedure SetColor(const Value: TCastleColorRGB); |
| 38 |
procedure SetAttenuation(const Value: TVector3); |
| 39 |
procedure SetIntensity(const Value: Single); |
| 40 |
procedure SetRadius(const Value: Single); |
| 41 |
public |
| 42 |
const |
| 43 |
DefaultRadius = 100.0; |
| 44 |
DefaultIntensity = 100.0; |
| 45 |
|
| 46 |
constructor Create(AOwner: TComponent); override; |
| 47 |
destructor Destroy; override; |
| 48 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 49 |
|
| 50 |
{ Light color shining over surface. By default white. } |
| 51 |
property Color: TCastleColorRGB read FColor write SetColor; |
| 52 |
|
| 53 |
{ Attenuation determines how quickly light intensity falls off with distance. |
| 54 |
By default Vector3(0, 0, 1), which means light falls off according to inverse square law. } |
| 55 |
property Attenuation: TVector3 read FAttenuation write SetAttenuation; |
| 56 |
published |
| 57 |
{ Intensity, multiplied by color. Can be anything >= 0. |
| 58 |
Intuitively, how bright is the light. |
| 59 |
|
| 60 |
Following @url(https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/README.md glTF lights specification), |
| 61 |
the point light's intensity physical unit |
| 62 |
is luminous intensity in candela (lm/sr). |
| 63 |
This is exactly like TCastleSpotLight, but different than TCastleDirectionalLight. |
| 64 |
|
| 65 |
The default intensity is 100.0, which is a reasonable value for a point/spot light, |
| 66 |
it remains bright for a few meters around the light source. } |
| 67 |
property Intensity: Single read FIntensity write SetIntensity {$ifdef FPC}default DefaultIntensity{$endif}; |
| 68 |
|
| 69 |
{ Light doesn't shine at all outside of a sphere with this radius. |
| 70 |
For performance reasons, set this as small as possible, to avoid even considering |
| 71 |
this light at further shapes. |
| 72 |
Set to -1 to have no limit. } |
| 73 |
property Radius: Single read FRadius write SetRadius {$ifdef FPC}default DefaultRadius{$endif}; |
| 74 |
|
| 75 |
{$define read_interface_class} |
| 76 |
{$I auto_generated_persistent_vectors/tcastlepointlight_persistent_vectors.inc} |
| 77 |
{$undef read_interface_class} |
| 78 |
end; |
| 79 |
|
| 80 |
{$endif read_interface} |
| 81 |
|
| 82 |
{$ifdef read_implementation} |
| 83 |
|
| 84 |
constructor TCastlePointLight.Create(AOwner: TComponent); |
| 85 |
begin |
| 86 |
inherited; |
| 87 |
|
| 88 |
// defaults |
| 89 |
FIntensity := DefaultIntensity; |
| 90 |
FRadius := DefaultRadius; |
| 91 |
FAttenuation := Vector3(0, 0, 1); |
| 92 |
FColor := WhiteRGB; |
| 93 |
|
| 94 |
// initialize light node |
| 95 |
FPointLight := TPointLightNode.Create; |
| 96 |
LightNode := FPointLight; |
| 97 |
FPunctualLight := FPointLight; |
| 98 |
|
| 99 |
// apply TCastlePointLight defaults to FPointLight |
| 100 |
FPointLight.Color := FColor; |
| 101 |
FPointLight.Attenuation := FAttenuation; |
| 102 |
FPointLight.Intensity := FIntensity; |
| 103 |
FPointLight.Radius := FRadius; |
| 104 |
|
| 105 |
// initialize Gizmo |
| 106 |
if Gizmo <> nil then |
| 107 |
if InternalCastleDesignData <> '' then |
| 108 |
Gizmo.SetIconUrl(InternalCastleDesignData + 'gizmos/light/light_point.png'); |
| 109 |
|
| 110 |
{$define read_implementation_constructor} |
| 111 |
{$I auto_generated_persistent_vectors/tcastlepointlight_persistent_vectors.inc} |
| 112 |
{$undef read_implementation_constructor} |
| 113 |
end; |
| 114 |
|
| 115 |
destructor TCastlePointLight.Destroy; |
| 116 |
begin |
| 117 |
{$define read_implementation_destructor} |
| 118 |
{$I auto_generated_persistent_vectors/tcastlepointlight_persistent_vectors.inc} |
| 119 |
{$undef read_implementation_destructor} |
| 120 |
inherited; |
| 121 |
end; |
| 122 |
|
| 123 |
function TCastlePointLight.PropertySections(const PropertyName: String): TPropertySections; |
| 124 |
begin |
| 125 |
if ArrayContainsString(PropertyName, ['ColorPersistent', |
| 126 |
'AttenuationPersistent', 'Intensity', 'Radius']) then |
| 127 |
Result := [psBasic] |
| 128 |
else |
| 129 |
Result := inherited PropertySections(PropertyName); |
| 130 |
end; |
| 131 |
|
| 132 |
procedure TCastlePointLight.SetColor(const Value: TCastleColorRGB); |
| 133 |
begin |
| 134 |
if not TCastleColorRGB.PerfectlyEquals(FColor, Value) then |
| 135 |
begin |
| 136 |
FColor := Value; |
| 137 |
FPointLight.Color := Value; |
| 138 |
end; |
| 139 |
end; |
| 140 |
|
| 141 |
procedure TCastlePointLight.SetAttenuation(const Value: TVector3); |
| 142 |
begin |
| 143 |
if not TVector3.PerfectlyEquals(FAttenuation, Value) then |
| 144 |
begin |
| 145 |
FAttenuation := Value; |
| 146 |
FPointLight.Attenuation := Value; |
| 147 |
end; |
| 148 |
end; |
| 149 |
|
| 150 |
procedure TCastlePointLight.SetIntensity(const Value: Single); |
| 151 |
begin |
| 152 |
if FIntensity <> Value then |
| 153 |
begin |
| 154 |
FIntensity := Value; |
| 155 |
FPointLight.Intensity := Value; |
| 156 |
end; |
| 157 |
end; |
| 158 |
|
| 159 |
procedure TCastlePointLight.SetRadius(const Value: Single); |
| 160 |
begin |
| 161 |
if FRadius <> Value then |
| 162 |
begin |
| 163 |
FRadius := Value; |
| 164 |
FPointLight.Radius := Value; |
| 165 |
end; |
| 166 |
end; |
| 167 |
|
| 168 |
{$define read_implementation_methods} |
| 169 |
{$I auto_generated_persistent_vectors/tcastlepointlight_persistent_vectors.inc} |
| 170 |
{$undef read_implementation_methods} |
| 171 |
|
| 172 |
{$endif read_implementation} |