| 1 |
{%MainUnit castlescene.pas} |
| 2 |
{ |
| 3 |
Copyright 2022-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 |
|
| 19 |
type |
| 20 |
{ Background, a skybox with a color gradient to represent sky and ground behind. |
| 21 |
|
| 22 |
See https://en.wikipedia.org/wiki/Cube_mapping#Skyboxes . |
| 23 |
|
| 24 |
You can find numerous free skyboxes e.g. on |
| 25 |
https://opengameart.org/art-search?keys=skybox and |
| 26 |
http://www.humus.name/index.php?page=Textures . } |
| 27 |
TCastleBackground = class(TCastleComponent) |
| 28 |
strict private |
| 29 |
FBackgroundNode: TBackgroundNode; |
| 30 |
FBackgroundRenderer: TBackgroundRenderer; |
| 31 |
FPendingUpdateRenderer: Boolean; // used by Loaded |
| 32 |
|
| 33 |
{ private fields reflecting public properties } |
| 34 |
FSkyTopColor: TCastleColorRGB; |
| 35 |
FSkyEquatorColor: TCastleColorRGB; |
| 36 |
FGroundBottomColor: TCastleColorRGB; |
| 37 |
FGroundEquatorColor: TCastleColorRGB; |
| 38 |
FTextureNegativeZ, FTexturePositiveZ, FTextureNegativeX, FTexturePositiveX, FTextureNegativeY, FTexturePositiveY: String; |
| 39 |
FSmoothEquator: Single; |
| 40 |
FRotation: TVector4; |
| 41 |
|
| 42 |
procedure UpdateRenderer; |
| 43 |
procedure UpdateRendererOnceLoaded; |
| 44 |
procedure SetSkyTopColor(const Value: TCastleColorRGB); |
| 45 |
procedure SetSkyEquatorColor(const Value: TCastleColorRGB); |
| 46 |
procedure SetGroundBottomColor(const Value: TCastleColorRGB); |
| 47 |
procedure SetGroundEquatorColor(const Value: TCastleColorRGB); |
| 48 |
procedure SetTextureNegativeZ(const Value: String); |
| 49 |
procedure SetTexturePositiveZ(const Value: String); |
| 50 |
procedure SetTextureNegativeX(const Value: String); |
| 51 |
procedure SetTexturePositiveX(const Value: String); |
| 52 |
procedure SetTextureNegativeY(const Value: String); |
| 53 |
procedure SetTexturePositiveY(const Value: String); |
| 54 |
procedure SetSmoothEquator(const Value: Single); |
| 55 |
procedure SetRotation(const Value: TVector4); |
| 56 |
protected |
| 57 |
procedure Loaded; override; |
| 58 |
public |
| 59 |
const |
| 60 |
DefaultSmoothEquator = 0.025; |
| 61 |
|
| 62 |
constructor Create(AOwner: TComponent); override; |
| 63 |
destructor Destroy; override; |
| 64 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 65 |
|
| 66 |
{ Sky color in the zenith. |
| 67 |
SkyTopColor and SkyEquatorColor are displayed |
| 68 |
as a gradient on the upper hemisphere around camera. } |
| 69 |
property SkyTopColor: TCastleColorRGB read FSkyTopColor write SetSkyTopColor; |
| 70 |
|
| 71 |
{ Sky color at the equator. |
| 72 |
SkyTopColor and SkyEquatorColor are displayed |
| 73 |
as a gradient on the upper hemisphere around camera. } |
| 74 |
property SkyEquatorColor: TCastleColorRGB read FSkyEquatorColor write SetSkyEquatorColor; |
| 75 |
|
| 76 |
{ Ground color at nadir (direction looking straight down). |
| 77 |
GroundBottomColor and GroundEquatorColor are displayed |
| 78 |
as a gradient on lower hemisphere around camera. } |
| 79 |
property GroundBottomColor: TCastleColorRGB read FGroundBottomColor write SetGroundBottomColor; |
| 80 |
|
| 81 |
{ Ground color at equator. |
| 82 |
GroundBottomColor and GroundEquatorColor are displayed |
| 83 |
as a gradient on lower hemisphere around camera. } |
| 84 |
property GroundEquatorColor: TCastleColorRGB read FGroundEquatorColor write SetGroundEquatorColor; |
| 85 |
|
| 86 |
{ Rotate the background around any axis easily. |
| 87 |
By default this is (0, 1, 0, 0) and often you will want to just |
| 88 |
change the 4th rotation component, angle, to rotate around the Y axis. |
| 89 |
|
| 90 |
Set it like this: |
| 91 |
@longCode(# |
| 92 |
MyBackground.Rotation := Vector4(0, 1, 0, Pi / 4); |
| 93 |
#) } |
| 94 |
property Rotation: TVector4 read FRotation write SetRotation; |
| 95 |
|
| 96 |
{ Renderer used by viewport. @exclude } |
| 97 |
property InternalBackgroundRenderer: TBackgroundRenderer read FBackgroundRenderer; |
| 98 |
published |
| 99 |
{ Back (-Z) background texture. |
| 100 |
Orientation should match @url(https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-WD2/Part01/components/environmentalEffects.html#Background X3D Background node). } |
| 101 |
property TextureNegativeZ: String read FTextureNegativeZ write SetTextureNegativeZ; |
| 102 |
|
| 103 |
{ Front (+Z) background texture. |
| 104 |
Orientation should match @url(https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-WD2/Part01/components/environmentalEffects.html#Background X3D Background node). } |
| 105 |
property TexturePositiveZ: String read FTexturePositiveZ write SetTexturePositiveZ; |
| 106 |
|
| 107 |
{ Top (+Y) background texture. |
| 108 |
Orientation should match @url(https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-WD2/Part01/components/environmentalEffects.html#Background X3D Background node). } |
| 109 |
property TexturePositiveY: String read FTexturePositiveY write SetTexturePositiveY; |
| 110 |
|
| 111 |
{ Bottom (-Y) background texture. |
| 112 |
Orientation should match @url(https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-WD2/Part01/components/environmentalEffects.html#Background X3D Background node). } |
| 113 |
property TextureNegativeY: String read FTextureNegativeY write SetTextureNegativeY; |
| 114 |
|
| 115 |
{ Left (-X) background texture. |
| 116 |
Orientation should match @url(https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-WD2/Part01/components/environmentalEffects.html#Background X3D Background node). } |
| 117 |
property TextureNegativeX: String read FTextureNegativeX write SetTextureNegativeX; |
| 118 |
|
| 119 |
{ Right (+X) background texture. |
| 120 |
Orientation should match @url(https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-WD2/Part01/components/environmentalEffects.html#Background X3D Background node). } |
| 121 |
property TexturePositiveX: String read FTexturePositiveX write SetTexturePositiveX; |
| 122 |
|
| 123 |
{ Make color transition from sky to ground less abrupt, adding a gradient |
| 124 |
from GroundEquatorColor to SkyEquatorColor. |
| 125 |
This property determines gradient size, in radians -- angle along the sky sphere. } |
| 126 |
property SmoothEquator: Single read FSmoothEquator write SetSmoothEquator {$ifdef FPC}default DefaultSmoothEquator{$endif}; |
| 127 |
|
| 128 |
{$define read_interface_class} |
| 129 |
{$I auto_generated_persistent_vectors/tcastlebackground_persistent_vectors.inc} |
| 130 |
{$undef read_interface_class} |
| 131 |
end; |
| 132 |
|
| 133 |
{$endif read_interface} |
| 134 |
|
| 135 |
{$ifdef read_implementation} |
| 136 |
|
| 137 |
{ TCastleBackground ---------------------------------------------------------------- } |
| 138 |
|
| 139 |
constructor TCastleBackground.Create(AOwner: TComponent); |
| 140 |
begin |
| 141 |
inherited; |
| 142 |
|
| 143 |
{ set default values } |
| 144 |
FSkyTopColor := Vector3(0.33, 0.33, 1.0); |
| 145 |
FSkyEquatorColor := WhiteRGB; |
| 146 |
FGroundBottomColor := BlackRGB; |
| 147 |
FGroundEquatorColor := GrayRGB; |
| 148 |
FSmoothEquator := DefaultSmoothEquator; |
| 149 |
FRotation := Vector4(0, 1, 0, 0); |
| 150 |
|
| 151 |
FBackgroundNode := TBackgroundNode.Create; |
| 152 |
|
| 153 |
UpdateRenderer; |
| 154 |
|
| 155 |
{$define read_implementation_constructor} |
| 156 |
{$I auto_generated_persistent_vectors/tcastlebackground_persistent_vectors.inc} |
| 157 |
{$undef read_implementation_constructor} |
| 158 |
end; |
| 159 |
|
| 160 |
destructor TCastleBackground.Destroy; |
| 161 |
begin |
| 162 |
{$define read_implementation_destructor} |
| 163 |
{$I auto_generated_persistent_vectors/tcastlebackground_persistent_vectors.inc} |
| 164 |
{$undef read_implementation_destructor} |
| 165 |
|
| 166 |
FreeAndNil(FBackgroundRenderer); |
| 167 |
FreeAndNil(FBackgroundNode); |
| 168 |
inherited; |
| 169 |
end; |
| 170 |
|
| 171 |
function TCastleBackground.PropertySections(const PropertyName: String): TPropertySections; |
| 172 |
begin |
| 173 |
if (PropertyName = 'SkyTopColorPersistent') or |
| 174 |
(PropertyName = 'SkyEquatorColorPersistent') or |
| 175 |
(PropertyName = 'GroundBottomColorPersistent') or |
| 176 |
(PropertyName = 'GroundEquatorColorPersistent') or |
| 177 |
(PropertyName = 'RotationPersistent') or |
| 178 |
(PropertyName = 'TextureNegativeZ') or |
| 179 |
(PropertyName = 'TexturePositiveZ') or |
| 180 |
(PropertyName = 'TextureNegativeX') or |
| 181 |
(PropertyName = 'TexturePositiveX') or |
| 182 |
(PropertyName = 'TextureNegativeY') or |
| 183 |
(PropertyName = 'TexturePositiveY') or |
| 184 |
(PropertyName = 'SmoothEquator') then |
| 185 |
Result := [psBasic] |
| 186 |
else |
| 187 |
Result := inherited PropertySections(PropertyName); |
| 188 |
end; |
| 189 |
|
| 190 |
procedure TCastleBackground.Loaded; |
| 191 |
begin |
| 192 |
inherited; |
| 193 |
if FPendingUpdateRenderer then |
| 194 |
UpdateRenderer; |
| 195 |
end; |
| 196 |
|
| 197 |
procedure TCastleBackground.UpdateRenderer; |
| 198 |
begin |
| 199 |
FreeAndNil(FBackgroundRenderer); |
| 200 |
|
| 201 |
if FSmoothEquator <= 0 then |
| 202 |
begin |
| 203 |
FBackgroundNode.SetSkyColor([FSkyTopColor, FSkyEquatorColor]); |
| 204 |
FBackgroundNode.SetGroundColor([FGroundBottomColor, FGroundEquatorColor]); |
| 205 |
FBackgroundNode.SetSkyAngle([ Pi / 2 ]); |
| 206 |
FBackgroundNode.SetGroundAngle([ Pi / 2 ]); |
| 207 |
end else |
| 208 |
begin |
| 209 |
FBackgroundNode.SetSkyColor([FSkyTopColor, FSkyEquatorColor]); |
| 210 |
FBackgroundNode.SetGroundColor([FGroundBottomColor, FGroundEquatorColor, FSkyEquatorColor]); |
| 211 |
FBackgroundNode.SetSkyAngle([ Pi / 2 - FSmoothEquator / 2 ]); |
| 212 |
FBackgroundNode.SetGroundAngle([ |
| 213 |
Pi / 2 - FSmoothEquator / 2, |
| 214 |
Pi / 2 + FSmoothEquator / 2 |
| 215 |
]); |
| 216 |
end; |
| 217 |
|
| 218 |
if TextureNegativeZ <> '' then |
| 219 |
FBackgroundNode.SetBackUrl([TextureNegativeZ]) |
| 220 |
else |
| 221 |
FBackgroundNode.SetBackUrl([]); |
| 222 |
if TexturePositiveZ <> '' then |
| 223 |
FBackgroundNode.SetFrontUrl([TexturePositiveZ]) |
| 224 |
else |
| 225 |
FBackgroundNode.SetFrontUrl([]); |
| 226 |
if TextureNegativeX <> '' then |
| 227 |
FBackgroundNode.SetLeftUrl([TextureNegativeX]) |
| 228 |
else |
| 229 |
FBackgroundNode.SetLeftUrl([]); |
| 230 |
if TexturePositiveX <> '' then |
| 231 |
FBackgroundNode.SetRightUrl([TexturePositiveX]) |
| 232 |
else |
| 233 |
FBackgroundNode.SetRightUrl([]); |
| 234 |
if TextureNegativeY <> '' then |
| 235 |
FBackgroundNode.SetBottomUrl([TextureNegativeY]) |
| 236 |
else |
| 237 |
FBackgroundNode.SetBottomUrl([]); |
| 238 |
if TexturePositiveY <> '' then |
| 239 |
FBackgroundNode.SetTopUrl([TexturePositiveY]) |
| 240 |
else |
| 241 |
FBackgroundNode.SetTopUrl([]); |
| 242 |
|
| 243 |
FBackgroundRenderer := CreateBackgroundRenderer(FBackgroundNode); |
| 244 |
FBackgroundRenderer.UpdateRotation(FRotation); |
| 245 |
FPendingUpdateRenderer := false; |
| 246 |
end; |
| 247 |
|
| 248 |
procedure TCastleBackground.UpdateRendererOnceLoaded; |
| 249 |
begin |
| 250 |
if IsLoading then |
| 251 |
FPendingUpdateRenderer := true |
| 252 |
else |
| 253 |
UpdateRenderer; |
| 254 |
end; |
| 255 |
|
| 256 |
procedure TCastleBackground.SetSkyTopColor(const Value: TCastleColorRGB); |
| 257 |
begin |
| 258 |
if not TCastleColorRGB.PerfectlyEquals(FSkyTopColor, Value) then |
| 259 |
begin |
| 260 |
FSkyTopColor := Value; |
| 261 |
UpdateRendererOnceLoaded; |
| 262 |
end; |
| 263 |
end; |
| 264 |
|
| 265 |
procedure TCastleBackground.SetSkyEquatorColor(const Value: TCastleColorRGB); |
| 266 |
begin |
| 267 |
if not TCastleColorRGB.PerfectlyEquals(FSkyEquatorColor, Value) then |
| 268 |
begin |
| 269 |
FSkyEquatorColor := Value; |
| 270 |
UpdateRendererOnceLoaded; |
| 271 |
end; |
| 272 |
end; |
| 273 |
|
| 274 |
procedure TCastleBackground.SetGroundBottomColor(const Value: TCastleColorRGB); |
| 275 |
begin |
| 276 |
if not TCastleColorRGB.PerfectlyEquals(FGroundBottomColor, Value) then |
| 277 |
begin |
| 278 |
FGroundBottomColor := Value; |
| 279 |
UpdateRendererOnceLoaded; |
| 280 |
end; |
| 281 |
end; |
| 282 |
|
| 283 |
procedure TCastleBackground.SetGroundEquatorColor(const Value: TCastleColorRGB); |
| 284 |
begin |
| 285 |
if not TCastleColorRGB.PerfectlyEquals(FGroundEquatorColor, Value) then |
| 286 |
begin |
| 287 |
FGroundEquatorColor := Value; |
| 288 |
UpdateRendererOnceLoaded; |
| 289 |
end; |
| 290 |
end; |
| 291 |
|
| 292 |
procedure TCastleBackground.SetTextureNegativeZ(const Value: String); |
| 293 |
begin |
| 294 |
if FTextureNegativeZ <> Value then |
| 295 |
begin |
| 296 |
FTextureNegativeZ := Value; |
| 297 |
UpdateRendererOnceLoaded; |
| 298 |
end; |
| 299 |
end; |
| 300 |
|
| 301 |
procedure TCastleBackground.SetTexturePositiveZ(const Value: String); |
| 302 |
begin |
| 303 |
if FTexturePositiveZ <> Value then |
| 304 |
begin |
| 305 |
FTexturePositiveZ := Value; |
| 306 |
UpdateRendererOnceLoaded; |
| 307 |
end; |
| 308 |
end; |
| 309 |
|
| 310 |
procedure TCastleBackground.SetTextureNegativeX(const Value: String); |
| 311 |
begin |
| 312 |
if FTextureNegativeX <> Value then |
| 313 |
begin |
| 314 |
FTextureNegativeX := Value; |
| 315 |
UpdateRendererOnceLoaded; |
| 316 |
end; |
| 317 |
end; |
| 318 |
|
| 319 |
procedure TCastleBackground.SetTexturePositiveX(const Value: String); |
| 320 |
begin |
| 321 |
if FTexturePositiveX <> Value then |
| 322 |
begin |
| 323 |
FTexturePositiveX := Value; |
| 324 |
UpdateRendererOnceLoaded; |
| 325 |
end; |
| 326 |
end; |
| 327 |
|
| 328 |
procedure TCastleBackground.SetTextureNegativeY(const Value: String); |
| 329 |
begin |
| 330 |
if FTextureNegativeY <> Value then |
| 331 |
begin |
| 332 |
FTextureNegativeY := Value; |
| 333 |
UpdateRendererOnceLoaded; |
| 334 |
end; |
| 335 |
end; |
| 336 |
|
| 337 |
procedure TCastleBackground.SetTexturePositiveY(const Value: String); |
| 338 |
begin |
| 339 |
if FTexturePositiveY <> Value then |
| 340 |
begin |
| 341 |
FTexturePositiveY := Value; |
| 342 |
UpdateRendererOnceLoaded; |
| 343 |
end; |
| 344 |
end; |
| 345 |
|
| 346 |
procedure TCastleBackground.SetSmoothEquator(const Value: Single); |
| 347 |
begin |
| 348 |
if FSmoothEquator <> Value then |
| 349 |
begin |
| 350 |
FSmoothEquator := Value; |
| 351 |
UpdateRendererOnceLoaded; |
| 352 |
end; |
| 353 |
end; |
| 354 |
|
| 355 |
procedure TCastleBackground.SetRotation(const Value: TVector4); |
| 356 |
begin |
| 357 |
if not TVector4.PerfectlyEquals(FRotation, Value) then |
| 358 |
begin |
| 359 |
FRotation := Value; |
| 360 |
if FBackgroundRenderer <> nil then |
| 361 |
FBackgroundRenderer.UpdateRotation(Value); |
| 362 |
end; |
| 363 |
end; |
| 364 |
|
| 365 |
{$define read_implementation_methods} |
| 366 |
{$I auto_generated_persistent_vectors/tcastlebackground_persistent_vectors.inc} |
| 367 |
{$undef read_implementation_methods} |
| 368 |
|
| 369 |
{$endif read_implementation} |