| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2023 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 |
{ Base type for viewpoints in X3D, |
| 19 |
which are locations from which the user may view the scene. } |
| 20 |
TAbstractViewpointNode = class(TAbstractBindableNode) |
| 21 |
strict private |
| 22 |
procedure EventSet_BindReceive( |
| 23 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 24 |
function GetPosition: TVector3; |
| 25 |
procedure SetPosition(const Value: TVector3); |
| 26 |
strict protected |
| 27 |
function PositionField: TSFVec3f; virtual; abstract; |
| 28 |
public |
| 29 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 30 |
function TransformationChange: TNodeTransformationChange; override; |
| 31 |
|
| 32 |
{ Position of the viewpoint. } |
| 33 |
property Position: TVector3 read GetPosition write SetPosition; |
| 34 |
|
| 35 |
class function ProjectionType: TProjectionType; virtual; abstract; |
| 36 |
|
| 37 |
{ Calculate camera vectors (position, direction, up, gravity up). |
| 38 |
Follows VRML/X3D specification: |
| 39 |
|
| 40 |
@unorderedList( |
| 41 |
@item(position is taken directly from FdPosition field,) |
| 42 |
@item(direction and up are (respectively) -Z and +Y rotated by FdOrientation,) |
| 43 |
@item(gravity up is +Y.) |
| 44 |
) |
| 45 |
|
| 46 |
They are all then transformed by the current viewpoint transformation |
| 47 |
(determined by parent nodes like TTransformNode). |
| 48 |
|
| 49 |
One conclusion from the above is that the only way to change the gravity up |
| 50 |
vector (this determines in which direction viewer falls down) |
| 51 |
is to use the Transform node around the viewpoint node. |
| 52 |
|
| 53 |
Additionally, as an extension, we also look at FdDirection and FdUp |
| 54 |
and FdGravityUp vectors. See |
| 55 |
https://castle-engine.io/x3d_extensions.php#section_ext_cameras_alt_orient |
| 56 |
|
| 57 |
Returned CamDir, CamUp, GravityUp are @italic(always normalized). |
| 58 |
|
| 59 |
TODO: This may need transform to world space, not just scene space, |
| 60 |
just like TAbstractLightNode.GetView does. |
| 61 |
Test how it behaves in TCastleScene that has some transformation. } |
| 62 |
procedure GetView(out CamPos, CamDir, CamUp, GravityUp: TVector3); overload; |
| 63 |
procedure GetView(out CamView: TViewVectors); overload; |
| 64 |
|
| 65 |
{ Description generated smart (trying to use FdDescription field, |
| 66 |
falling back on other information to help user identify the node). } |
| 67 |
function SmartDescription: string; virtual; |
| 68 |
|
| 69 |
{ Matrices for projecting texture from this viewpoint, |
| 70 |
for ProjectedTextureCoordinate. |
| 71 |
Override ProjectionMatrix for subclasses (ModelviewMatrix |
| 72 |
is already correctly defined here). |
| 73 |
@groupBegin } |
| 74 |
function ProjectionMatrix: TMatrix4; virtual; |
| 75 |
function ModelviewMatrix: TMatrix4; |
| 76 |
function GetProjectorMatrix: TMatrix4; |
| 77 |
{ @groupEnd } |
| 78 |
|
| 79 |
{$I auto_generated_node_helpers/x3dnodes_x3dviewpointnode.inc} |
| 80 |
end; |
| 81 |
|
| 82 |
TAbstractX3DViewpointNode = TAbstractViewpointNode deprecated 'use TAbstractViewpointNode'; |
| 83 |
|
| 84 |
TX3DViewpointClassNode = class of TAbstractViewpointNode; |
| 85 |
|
| 86 |
{ Grouping node that transforms the coordinate system of its children |
| 87 |
so that they always turn towards the viewer. |
| 88 |
The local Z axis of the children faces the camera, |
| 89 |
rotating around @link(AxisOfRotation) (it it is non-zero) |
| 90 |
or rotating freely in 3D (when @link(AxisOfRotation) is exactly zero). } |
| 91 |
TBillboardNode = class(TAbstractGroupingNode) |
| 92 |
strict private |
| 93 |
type |
| 94 |
TBillboardNodeTransformFunctionality = class(TTransformFunctionality) |
| 95 |
strict private |
| 96 |
FParent: TBillboardNode; |
| 97 |
public |
| 98 |
constructor Create(const AParent: TBillboardNode); |
| 99 |
property Parent: TBillboardNode read FParent; |
| 100 |
procedure ApplyTransform(var Transformation: TTransformation); override; |
| 101 |
end; |
| 102 |
var |
| 103 |
FTransformFunctionality: TBillboardNodeTransformFunctionality; |
| 104 |
FCameraVectors: TViewVectors; |
| 105 |
FCameraVectorsKnown: boolean; |
| 106 |
protected |
| 107 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 108 |
public |
| 109 |
constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override; |
| 110 |
function TransformationChange: TNodeTransformationChange; override; |
| 111 |
|
| 112 |
{ Change transformation to reflect current camera. @exclude } |
| 113 |
procedure InternalCameraChanged(const ACameraVectors: TViewVectors); |
| 114 |
|
| 115 |
{$I auto_generated_node_helpers/x3dnodes_billboard.inc} |
| 116 |
end; |
| 117 |
|
| 118 |
{ Grouping node that specifies the collision detection properties |
| 119 |
for its children. } |
| 120 |
TCollisionNode = class(TAbstractGroupingNode) |
| 121 |
protected |
| 122 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 123 |
function DirectEnumerateActiveForTraverse( |
| 124 |
Func: TEnumerateChildrenFunction; |
| 125 |
StateStack: TX3DGraphTraverseStateStack): Pointer; override; |
| 126 |
public |
| 127 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 128 |
|
| 129 |
{ Setup this Collision node to collide as a Box. |
| 130 |
|
| 131 |
This sets the @link(Proxy) field to a simple box with given bounds. |
| 132 |
|
| 133 |
The @code(Enabled) field is unchanged (by default @true). |
| 134 |
|
| 135 |
The overloaded version with VisibleNode also sets VisibleNode |
| 136 |
as the only displayed child of this collision node. |
| 137 |
The overloaded version without VisibleNode doesn't modify the displayed children. |
| 138 |
|
| 139 |
It honors the case of Box being empty correctly. Proxy is then |
| 140 |
non-nil, but without any geometry. So the collisions are |
| 141 |
effectively disabled, in a consistent way (without changing the |
| 142 |
@code(Enabled) field). |
| 143 |
} |
| 144 |
procedure CollideAsBox(const Box: TBox3D); overload; |
| 145 |
procedure CollideAsBox(const VisibleNode: TX3DNode; const Box: TBox3D); overload; |
| 146 |
|
| 147 |
{$I auto_generated_node_helpers/x3dnodes_collision.inc} |
| 148 |
end; |
| 149 |
|
| 150 |
{ Provides various levels of detail for a given object, |
| 151 |
only one of which will be visible at a given time. |
| 152 |
|
| 153 |
Compatibility note: We follow X3D specifications exactly. |
| 154 |
If use older VRML 97, note: when the level changes, we send |
| 155 |
"children_changed" (following X3D) not "level_changed" (which would follow VRML 97). } |
| 156 |
TLODNode = class(TAbstractGroupingNode) |
| 157 |
protected |
| 158 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 159 |
public |
| 160 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 161 |
function TransformationChange: TNodeTransformationChange; override; |
| 162 |
{$I auto_generated_node_helpers/x3dnodes_lod.inc} |
| 163 |
end; |
| 164 |
|
| 165 |
{ Describe the physical characteristics of the viewer's avatar and navigation. } |
| 166 |
TNavigationInfoNode = class(TAbstractBindableNode) |
| 167 |
strict private |
| 168 |
procedure EventSet_BindReceive( |
| 169 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 170 |
public |
| 171 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 172 |
|
| 173 |
{$I auto_generated_node_helpers/x3dnodes_navigationinfo.inc} |
| 174 |
end; |
| 175 |
|
| 176 |
{ Viewpoint that provides an orthographic view of the scene. } |
| 177 |
TOrthoViewpointNode = class(TAbstractViewpointNode) |
| 178 |
strict private |
| 179 |
function GetFieldOfViewDefault(const Index: Integer): Single; |
| 180 |
function GetFieldOfView(const Index: Integer): Single; |
| 181 |
procedure SetFieldOfView(const Index: Integer; const Value: Single); overload; |
| 182 |
{ Special setter because delphi can't work with overloaded procedures in properties. |
| 183 |
More info: |
| 184 |
https://docwiki.embarcadero.com/RADStudio/Sydney/en/E2271_Property_getters_and_setters_cannot_be_overloaded_(Delphi) } |
| 185 |
procedure SetFieldOfViewDelphiCompat(const Index: Integer; const Value: Single); |
| 186 |
strict protected |
| 187 |
function PositionField: TSFVec3f; override; |
| 188 |
public |
| 189 |
{ Field of view determines how much you see in the camera. |
| 190 |
Use this e.g. to zoom in/out. |
| 191 |
|
| 192 |
This property has comfortable getter and setter, you can |
| 193 |
also get and set the indexes in 0..3 range, where |
| 194 |
|
| 195 |
@unorderedList( |
| 196 |
@itemSpacing Compact |
| 197 |
@item 0 index is "min x" (default value -1) |
| 198 |
@item 1 index is "min y" (default value -1) |
| 199 |
@item 2 index is "max x" (default value 1) |
| 200 |
@item 3 index is "max y" (default value 1) |
| 201 |
) |
| 202 |
} |
| 203 |
property FieldOfView [const Index: Integer]: Single read GetFieldOfView write SetFieldOfView; |
| 204 |
|
| 205 |
{ Field of view - minimum X. -1 by default. @seealso FieldOfView } |
| 206 |
property FieldOfViewMinX: Single index 0 read GetFieldOfView |
| 207 |
write SetFieldOfViewDelphiCompat; |
| 208 |
{ Field of view - minimum Y. -1 by default. @seealso FieldOfView } |
| 209 |
property FieldOfViewMinY: Single index 1 read GetFieldOfView |
| 210 |
write SetFieldOfViewDelphiCompat; |
| 211 |
{ Field of view - maximum X. 1 by default. @seealso FieldOfView } |
| 212 |
property FieldOfViewMaxX: Single index 2 read GetFieldOfView |
| 213 |
write SetFieldOfViewDelphiCompat; |
| 214 |
{ Field of view - maximum Y. 1 by default. @seealso FieldOfView } |
| 215 |
property FieldOfViewMaxY: Single index 3 read GetFieldOfView |
| 216 |
write SetFieldOfViewDelphiCompat; |
| 217 |
|
| 218 |
class function ProjectionType: TProjectionType; override; |
| 219 |
function ProjectionMatrix: TMatrix4; override; |
| 220 |
|
| 221 |
{ Calculate final field of view value, taking into account aspect ratio. |
| 222 |
The idea is that OrthoViewpoint.fieldOfView specifies the minimal |
| 223 |
extents. Depending on your window aspect ratio, you may need to make |
| 224 |
one extent (vertical or horizontal) larger to adjust. } |
| 225 |
class function InternalFieldOfView( |
| 226 |
const AFieldOfView: TFloatRectangle; |
| 227 |
const ViewportWidth, ViewportHeight: Single): TFloatRectangle; |
| 228 |
|
| 229 |
{$I auto_generated_node_helpers/x3dnodes_orthoviewpoint.inc} |
| 230 |
end; |
| 231 |
|
| 232 |
{ Viewpoint that provides a perspective view of the scene. } |
| 233 |
TViewpointNode = class(TAbstractViewpointNode) |
| 234 |
strict protected |
| 235 |
function PositionField: TSFVec3f; override; |
| 236 |
public |
| 237 |
class function ProjectionType: TProjectionType; override; |
| 238 |
|
| 239 |
{ Calculate both perspective angles (in radians), |
| 240 |
knowing |
| 241 |
@link(TCastlePerspective.FieldOfView TCastleCamera.Perspective.FieldOfView) and |
| 242 |
@link(TCastlePerspective.FieldOfViewAxis TCastleCamera.Perspective.FieldOfViewAxis). |
| 243 |
|
| 244 |
In particular when FieldOfViewAxis = faSmallest then we perform |
| 245 |
the calculation requested by X3D specification about |
| 246 |
ViewpointNode fieldOfView behavior. } |
| 247 |
class function InternalFieldOfView(FieldOfView: Single; |
| 248 |
const FieldOfViewAxis: TFieldOfViewAxis; |
| 249 |
const ViewportWidth, ViewportHeight: Single): TVector2; |
| 250 |
|
| 251 |
function ProjectionMatrix: TMatrix4; override; |
| 252 |
|
| 253 |
{$I auto_generated_node_helpers/x3dnodes_viewpoint.inc} |
| 254 |
end; |
| 255 |
|
| 256 |
{ Group of viewpoints. You can (optionally) arrange viewpoints in groups |
| 257 |
to present them nicely in the X3D browser submenus. } |
| 258 |
TViewpointGroupNode = class(TAbstractChildNode) |
| 259 |
protected |
| 260 |
function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override; |
| 261 |
public |
| 262 |
{ Description generated smart (trying to use FdDescription field, |
| 263 |
falling back on other information to help user identify the node). } |
| 264 |
function SmartDescription: string; |
| 265 |
|
| 266 |
{$I auto_generated_node_helpers/x3dnodes_viewpointgroup.inc} |
| 267 |
end; |
| 268 |
{$endif read_interface} |
| 269 |
|
| 270 |
{$ifdef read_implementation} |
| 271 |
|
| 272 |
const |
| 273 |
ShapeSortNames: array [TShapeSort] of string = |
| 274 |
('AUTO', 'NONE', '2D', '3D', '3D_VERTICAL_BILLBOARDS', '3D_ORIGIN', '3D_GROUND', 'CUSTOM'); |
| 275 |
|
| 276 |
{$I auto_generated_node_helpers/x3dnodes_x3dviewpointnode.inc} |
| 277 |
{$I auto_generated_node_helpers/x3dnodes_billboard.inc} |
| 278 |
{$I auto_generated_node_helpers/x3dnodes_collision.inc} |
| 279 |
{$I auto_generated_node_helpers/x3dnodes_lod.inc} |
| 280 |
{$I auto_generated_node_helpers/x3dnodes_navigationinfo.inc} |
| 281 |
{$I auto_generated_node_helpers/x3dnodes_orthoviewpoint.inc} |
| 282 |
{$I auto_generated_node_helpers/x3dnodes_viewpoint.inc} |
| 283 |
{$I auto_generated_node_helpers/x3dnodes_viewpointgroup.inc} |
| 284 |
|
| 285 |
{ TAbstractViewpointNode -------------------------------------------------- } |
| 286 |
|
| 287 |
constructor TAbstractViewpointNode.Create(const AX3DName: string = ''; const ABaseUrl: String = ''); |
| 288 |
begin |
| 289 |
inherited; |
| 290 |
Eventset_bind.AddNotification({$ifdef FPC}@{$endif} EventSet_BindReceive); |
| 291 |
FdOrientation.ChangeAlways := chViewpointVectors; |
| 292 |
FdDirection.ChangeAlways := chViewpointVectors; |
| 293 |
FdUp.ChangeAlways := chViewpointVectors; |
| 294 |
FdGravityUp.ChangeAlways := chViewpointVectors; |
| 295 |
end; |
| 296 |
|
| 297 |
procedure TAbstractViewpointNode.GetView( |
| 298 |
out CamView: TViewVectors); |
| 299 |
var |
| 300 |
IgnoredGravityUp: TVector3; |
| 301 |
begin |
| 302 |
GetView(CamView.Translation, CamView.Direction, CamView.Up, IgnoredGravityUp); |
| 303 |
end; |
| 304 |
|
| 305 |
procedure TAbstractViewpointNode.GetView( |
| 306 |
out CamPos, CamDir, CamUp, GravityUp: TVector3); |
| 307 |
begin |
| 308 |
CamPos := Position; |
| 309 |
|
| 310 |
if FdDirection.Items.Count > 0 then |
| 311 |
begin |
| 312 |
CamDir := FdDirection.Items.L[0]; |
| 313 |
if CamDir.IsZero then |
| 314 |
begin |
| 315 |
WritelnWarning('VRML/X3D', 'Viewpoint "direction" must not be zero, assuming defaults'); |
| 316 |
CamDir := FdOrientation.RotatedPoint( DefaultX3DCameraDirection ); |
| 317 |
end; |
| 318 |
end else |
| 319 |
CamDir := FdOrientation.RotatedPoint( DefaultX3DCameraDirection ); |
| 320 |
|
| 321 |
if FdUp.Items.Count > 0 then |
| 322 |
begin |
| 323 |
CamUp := FdUp.Items.L[0]; |
| 324 |
if CamUp.IsZero then |
| 325 |
begin |
| 326 |
WritelnWarning('VRML/X3D', 'Viewpoint "up" must not be zero, assuming defaults'); |
| 327 |
CamUp := FdOrientation.RotatedPoint( DefaultX3DCameraUp ); |
| 328 |
end; |
| 329 |
end else |
| 330 |
CamUp := FdOrientation.RotatedPoint( DefaultX3DCameraUp ); |
| 331 |
|
| 332 |
GravityUp := FdGravityUp.Value; |
| 333 |
if GravityUp.IsZero then |
| 334 |
GravityUp := DefaultX3DGravityUp; |
| 335 |
|
| 336 |
CamPos := Transform.MultPoint(CamPos); |
| 337 |
{ Since the Transform can contain scale, |
| 338 |
we need to always normalize resulting vectors, |
| 339 |
even if input (like CamDir) was already normalized. } |
| 340 |
CamDir := Transform.MultDirection(CamDir).Normalize; |
| 341 |
CamUp := Transform.MultDirection(CamUp).Normalize; |
| 342 |
|
| 343 |
if GravityTransform then |
| 344 |
GravityUp := Transform.MultDirection(GravityUp).Normalize |
| 345 |
else |
| 346 |
GravityUp := GravityUp.Normalize; |
| 347 |
|
| 348 |
Assert(SameValue(CamDir.LengthSqr, 1.0, 0.0001)); |
| 349 |
Assert(SameValue(CamUp.LengthSqr, 1.0, 0.0001)); |
| 350 |
end; |
| 351 |
|
| 352 |
procedure TAbstractViewpointNode.EventSet_BindReceive( |
| 353 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 354 |
begin |
| 355 |
if Scene <> nil then |
| 356 |
Scene.GetViewpointStack.Set_Bind(Self, (Value as TSFBool).Value); |
| 357 |
end; |
| 358 |
|
| 359 |
function TAbstractViewpointNode.TransformationChange: TNodeTransformationChange; |
| 360 |
begin |
| 361 |
Result := ntcViewpoint; |
| 362 |
end; |
| 363 |
|
| 364 |
function TAbstractViewpointNode.SmartDescription: string; |
| 365 |
begin |
| 366 |
Result := FdDescription.Value; |
| 367 |
{ if node doesn't have a "description" field, or it's left empty, use node name } |
| 368 |
if Result = '' then |
| 369 |
Result := X3DName; |
| 370 |
{ if even the node name is empty, just show node type. } |
| 371 |
if Result = '' then |
| 372 |
Result := X3DType; |
| 373 |
end; |
| 374 |
|
| 375 |
function TAbstractViewpointNode.GetPosition: TVector3; |
| 376 |
begin |
| 377 |
Result := PositionField.Value; |
| 378 |
end; |
| 379 |
|
| 380 |
procedure TAbstractViewpointNode.SetPosition(const Value: TVector3); |
| 381 |
begin |
| 382 |
PositionField.Send(Value); |
| 383 |
end; |
| 384 |
|
| 385 |
function TAbstractViewpointNode.ProjectionMatrix: TMatrix4; |
| 386 |
begin |
| 387 |
Result := TMatrix4.Identity; |
| 388 |
end; |
| 389 |
|
| 390 |
function TAbstractViewpointNode.ModelviewMatrix: TMatrix4; |
| 391 |
var |
| 392 |
CamView: TViewVectors; |
| 393 |
begin |
| 394 |
GetView(CamView); |
| 395 |
Result := CamView.Matrix; |
| 396 |
end; |
| 397 |
|
| 398 |
function TAbstractViewpointNode.GetProjectorMatrix: TMatrix4; |
| 399 |
begin |
| 400 |
Result := ProjectionMatrix * ModelviewMatrix; |
| 401 |
end; |
| 402 |
|
| 403 |
{ TBillboardNode.TBillboardNodeTransformFunctionality ---------------------------- } |
| 404 |
|
| 405 |
constructor TBillboardNode.TBillboardNodeTransformFunctionality.Create(const AParent: TBillboardNode); |
| 406 |
begin |
| 407 |
inherited Create(AParent); |
| 408 |
FParent := AParent; |
| 409 |
end; |
| 410 |
|
| 411 |
procedure TBillboardNode.TBillboardNodeTransformFunctionality.ApplyTransform(var Transformation: TTransformation); |
| 412 |
var |
| 413 |
NewX, NewY, NewZ, BillboardToViewer, P1, P2, LocalDirection, LocalUp: TVector3; |
| 414 |
Angle: Single; |
| 415 |
M, IM: TMatrix4; |
| 416 |
PlaneAxis: TVector4; |
| 417 |
PlaneAxisDir: TVector3 absolute PlaneAxis; |
| 418 |
begin |
| 419 |
if Parent.FCameraVectorsKnown then |
| 420 |
begin |
| 421 |
if Parent.FdAxisOfRotation.Value.IsPerfectlyZero then |
| 422 |
begin |
| 423 |
LocalDirection := Transformation.InverseTransform.MultDirection(Parent.FCameraVectors.Direction); |
| 424 |
LocalUp := Transformation.InverseTransform.MultDirection(Parent.FCameraVectors.Up); |
| 425 |
{ although FCameraVectors.Direction/Up are for sure normalized and orthogonal, |
| 426 |
but Transformation.InverseTransform may contain scaling, |
| 427 |
so be sure to normalize again the result. |
| 428 |
For safety, also call MakeVectorsOrthoOnTheirPlane. } |
| 429 |
LocalDirection := LocalDirection.Normalize; |
| 430 |
LocalUp := LocalUp.Normalize; |
| 431 |
MakeVectorsOrthoOnTheirPlane(LocalDirection, LocalUp); |
| 432 |
|
| 433 |
NewX := TVector3.CrossProduct(LocalDirection, LocalUp); |
| 434 |
NewY := LocalUp; |
| 435 |
NewZ := -LocalDirection; |
| 436 |
|
| 437 |
TransformCoordsMatrices(NewX, NewY, NewZ, M, IM); |
| 438 |
Transformation.Transform := Transformation.Transform * M; |
| 439 |
Transformation.InverseTransform := IM * Transformation.InverseTransform; |
| 440 |
end else |
| 441 |
begin |
| 442 |
{ vector from node origin to FCameraVectors.Position, in local coords } |
| 443 |
BillboardToViewer := Transformation.InverseTransform.MultPoint( |
| 444 |
Parent.FCameraVectors.Translation); |
| 445 |
|
| 446 |
{ plane of axisOfRotation } |
| 447 |
PlaneAxisDir := Parent.FdAxisOfRotation.Value; |
| 448 |
PlaneAxis.W := 0; |
| 449 |
|
| 450 |
{ we want to have a rotation that changes TVector3.One[2] |
| 451 |
into BillboardToViewer. But the rotation axis is fixed |
| 452 |
to axisOfRotation (we cannot just take their TVector3.CrossProduct). |
| 453 |
So project both points on a plane orthogonal to axisOfRotation, |
| 454 |
and calculate angle there. } |
| 455 |
P1 := PointOnPlaneClosestToPoint(PlaneAxis, TVector3.One[2]); |
| 456 |
P2 := PointOnPlaneClosestToPoint(PlaneAxis, BillboardToViewer); |
| 457 |
|
| 458 |
if { axisOfRotation paralell to Z axis? Then nothing sensible to do. } |
| 459 |
P1.IsZero or |
| 460 |
{ billboard-to-viewer vector parallel to axisOfRotation (includes |
| 461 |
the case when billboard-to-viewer vector is zero in local coords, |
| 462 |
which means that camera standing at Billboard origin)? |
| 463 |
Then nothing sensible to do. } |
| 464 |
P2.IsZero then |
| 465 |
Exit; |
| 466 |
|
| 467 |
{ As https://sourceforge.net/p/castle-engine/tickets/38/ shows, |
| 468 |
the above checks for zero are not enough. The more precise check |
| 469 |
(because it replicates what happens in CosAngleBetweenVectors) |
| 470 |
would be to add |
| 471 |
|
| 472 |
(P1.LengthSqr * P2.LengthSqr < SingleEpsilon) |
| 473 |
|
| 474 |
But that's not really a future-proof solution, to repeat this code. |
| 475 |
It's safer (even if a little slower) to capture exception here. } |
| 476 |
try |
| 477 |
Angle := RotationAngleRadBetweenVectors(P1, P2, Parent.FdAxisOfRotation.Value); |
| 478 |
except |
| 479 |
on EVectorInvalidOp do Exit; |
| 480 |
end; |
| 481 |
|
| 482 |
RotationMatricesRad(Angle, Parent.FdAxisOfRotation.Value, M, IM); |
| 483 |
Transformation.Transform := Transformation.Transform * M; |
| 484 |
Transformation.InverseTransform := IM * Transformation.InverseTransform; |
| 485 |
end; |
| 486 |
end; |
| 487 |
end; |
| 488 |
|
| 489 |
{ TBillboardNode ------------------------------------------------------------- } |
| 490 |
|
| 491 |
constructor TBillboardNode.Create(const AX3DName, ABaseUrl: String); |
| 492 |
begin |
| 493 |
inherited; |
| 494 |
FTransformFunctionality := TBillboardNodeTransformFunctionality.Create(Self); |
| 495 |
AddFunctionality(FTransformFunctionality); |
| 496 |
end; |
| 497 |
|
| 498 |
function TBillboardNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 499 |
begin |
| 500 |
Result := FdChildren.Enumerate(Func); |
| 501 |
if Result <> nil then Exit; |
| 502 |
end; |
| 503 |
|
| 504 |
procedure TBillboardNode.InternalCameraChanged(const ACameraVectors: TViewVectors); |
| 505 |
begin |
| 506 |
FCameraVectorsKnown := true; |
| 507 |
FCameraVectors := ACameraVectors; |
| 508 |
end; |
| 509 |
|
| 510 |
function TBillboardNode.TransformationChange: TNodeTransformationChange; |
| 511 |
begin |
| 512 |
Result := ntcTransform; |
| 513 |
end; |
| 514 |
|
| 515 |
{ TCollisionNode ------------------------------------------------------------- } |
| 516 |
|
| 517 |
constructor TCollisionNode.Create(const AX3DName, ABaseUrl: String); |
| 518 |
begin |
| 519 |
inherited; |
| 520 |
|
| 521 |
{ In VRML 2.0, Collision didn't descend from X3DSensorName and had |
| 522 |
special field "collide". In X3D, "enabled" is used for the exact |
| 523 |
same purpose. } |
| 524 |
FdEnabled.AddAlternativeName('collide', 2); |
| 525 |
end; |
| 526 |
|
| 527 |
function TCollisionNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 528 |
begin |
| 529 |
Result := FdProxy.Enumerate(Func); |
| 530 |
if Result <> nil then Exit; |
| 531 |
|
| 532 |
Result := FdChildren.Enumerate(Func); |
| 533 |
if Result <> nil then Exit; |
| 534 |
end; |
| 535 |
|
| 536 |
function TCollisionNode.DirectEnumerateActiveForTraverse( |
| 537 |
Func: TEnumerateChildrenFunction; |
| 538 |
StateStack: TX3DGraphTraverseStateStack): Pointer; |
| 539 |
begin |
| 540 |
Result := nil; |
| 541 |
if FdEnabled.Value then |
| 542 |
begin |
| 543 |
if FdProxy.Value = nil then |
| 544 |
begin |
| 545 |
{ Collision node doesn't do anything in this trivial case, |
| 546 |
children are treated just like by Group. } |
| 547 |
Result := FdChildren.Enumerate(Func); |
| 548 |
if Result <> nil then Exit; |
| 549 |
end else |
| 550 |
begin |
| 551 |
{ This is the interesting case: |
| 552 |
proxy is not visible, |
| 553 |
children are not collidable. } |
| 554 |
|
| 555 |
Inc(StateStack.Top.InsideInvisible); |
| 556 |
try |
| 557 |
Result := FdProxy.Enumerate(Func); |
| 558 |
if Result <> nil then Exit; |
| 559 |
finally Dec(StateStack.Top.InsideInvisible) end; |
| 560 |
|
| 561 |
Inc(StateStack.Top.InsideIgnoreCollision); |
| 562 |
try |
| 563 |
Result := FdChildren.Enumerate(Func); |
| 564 |
if Result <> nil then Exit; |
| 565 |
finally Dec(StateStack.Top.InsideIgnoreCollision) end; |
| 566 |
end; |
| 567 |
end else |
| 568 |
begin |
| 569 |
{ Nothing is collidable in this case. So proxy is just ignored. } |
| 570 |
Inc(StateStack.Top.InsideIgnoreCollision); |
| 571 |
try |
| 572 |
Result := FdChildren.Enumerate(Func); |
| 573 |
if Result <> nil then Exit; |
| 574 |
finally Dec(StateStack.Top.InsideIgnoreCollision) end; |
| 575 |
end; |
| 576 |
end; |
| 577 |
|
| 578 |
procedure TCollisionNode.CollideAsBox(const Box: TBox3D); |
| 579 |
var |
| 580 |
ProxyTransform: TTransformNode; |
| 581 |
ProxyShape: TShapeNode; |
| 582 |
ProxyBox: TBoxNode; |
| 583 |
begin |
| 584 |
{ always create ProxyTransform, even when Box.IsEmpty, |
| 585 |
otherwise X3D Collision.proxy would be ignored when nil. } |
| 586 |
ProxyTransform := TTransformNode.Create('', BaseUrl); |
| 587 |
|
| 588 |
if not Box.IsEmpty then |
| 589 |
begin |
| 590 |
ProxyBox := TBoxNode.Create('', BaseUrl); |
| 591 |
ProxyBox.Size := Box.Size; |
| 592 |
|
| 593 |
ProxyShape := TShapeNode.Create('', BaseUrl); |
| 594 |
ProxyShape.Geometry := ProxyBox; |
| 595 |
|
| 596 |
ProxyTransform.Translation := Box.Center; |
| 597 |
ProxyTransform.AddChildren(ProxyShape); |
| 598 |
end; |
| 599 |
Proxy := ProxyTransform; |
| 600 |
end; |
| 601 |
|
| 602 |
procedure TCollisionNode.CollideAsBox(const VisibleNode: TX3DNode; const Box: TBox3D); |
| 603 |
begin |
| 604 |
CollideAsBox(Box); |
| 605 |
FdChildren.Clear; |
| 606 |
FdChildren.Add(VisibleNode); |
| 607 |
end; |
| 608 |
|
| 609 |
{ TLODNode ----------------------------------------------------------- } |
| 610 |
|
| 611 |
constructor TLODNode.Create(const AX3DName, ABaseUrl: String); |
| 612 |
begin |
| 613 |
inherited; |
| 614 |
{ Testcase that "level" is necessary for VRML 97: |
| 615 |
https://www.web3d.org/x3d/content/examples/Vrml2Sourcebook/Siggraph98Course/aTorchIndex.html } |
| 616 |
FdChildren.AddAlternativeName('level', 2); |
| 617 |
end; |
| 618 |
|
| 619 |
function TLODNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 620 |
begin |
| 621 |
Result := nil; |
| 622 |
{ For now we simply always use the best LOD version, |
| 623 |
avoiding whole issue of choosing proper LOD child. } |
| 624 |
if FdChildren.Count >= 1 then |
| 625 |
begin |
| 626 |
Result := Func(Self, FdChildren[0]); |
| 627 |
if Result <> nil then Exit; |
| 628 |
end; |
| 629 |
end; |
| 630 |
|
| 631 |
function TLODNode.TransformationChange: TNodeTransformationChange; |
| 632 |
begin |
| 633 |
Result := ntcLOD; |
| 634 |
end; |
| 635 |
|
| 636 |
{ TNavigationInfoNode -------------------------------------------------------- } |
| 637 |
|
| 638 |
constructor TNavigationInfoNode.Create(const AX3DName, ABaseUrl: String); |
| 639 |
begin |
| 640 |
inherited; |
| 641 |
Eventset_bind.AddNotification({$ifdef FPC}@{$endif} EventSet_BindReceive); |
| 642 |
end; |
| 643 |
|
| 644 |
procedure TNavigationInfoNode.EventSet_BindReceive( |
| 645 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 646 |
begin |
| 647 |
if Scene <> nil then |
| 648 |
Scene.GetNavigationInfoStack.Set_Bind(Self, (Value as TSFBool).Value); |
| 649 |
end; |
| 650 |
|
| 651 |
{ TOrthoViewpointNode -------------------------------------------------------- } |
| 652 |
|
| 653 |
function TOrthoViewpointNode.PositionField: TSFVec3f; |
| 654 |
begin |
| 655 |
Result := FdPosition; |
| 656 |
end; |
| 657 |
|
| 658 |
class function TOrthoViewpointNode.ProjectionType: TProjectionType; |
| 659 |
begin |
| 660 |
Result := ptOrthographic; |
| 661 |
end; |
| 662 |
|
| 663 |
function TOrthoViewpointNode.ProjectionMatrix: TMatrix4; |
| 664 |
var |
| 665 |
Dimensions: TFloatRectangle; |
| 666 |
begin |
| 667 |
{ default Dimensions, for OrthoViewpoint } |
| 668 |
Dimensions.Left := -1; |
| 669 |
Dimensions.Bottom := -1; |
| 670 |
Dimensions.Width := 2; |
| 671 |
Dimensions.Height := 2; |
| 672 |
|
| 673 |
if FdFieldOfView.Items.Count > 0 then Dimensions.Left := FdFieldOfView.Items[0]; |
| 674 |
if FdFieldOfView.Items.Count > 1 then Dimensions.Bottom := FdFieldOfView.Items[1]; |
| 675 |
if FdFieldOfView.Items.Count > 2 then Dimensions.Width := FdFieldOfView.Items[2] - Dimensions.Left; |
| 676 |
if FdFieldOfView.Items.Count > 3 then Dimensions.Height := FdFieldOfView.Items[3] - Dimensions.Bottom; |
| 677 |
|
| 678 |
{ TODO: for currently bound viewpoint, we should honour |
| 679 |
fieldOfView and aspect ratio of current window, |
| 680 |
by calling InternalFieldOfView. } |
| 681 |
|
| 682 |
Result := OrthoProjectionMatrix(Dimensions, |
| 683 |
1, 100); { TODO: near, far projection testing values } |
| 684 |
end; |
| 685 |
|
| 686 |
function TOrthoViewpointNode.GetFieldOfViewDefault(const Index: Integer): Single; |
| 687 |
begin |
| 688 |
if Index < 2 then |
| 689 |
Result := -1 else |
| 690 |
if Index < 4 then |
| 691 |
Result := 1 else |
| 692 |
Result := 0; |
| 693 |
end; |
| 694 |
|
| 695 |
function TOrthoViewpointNode.GetFieldOfView(const Index: Integer): Single; |
| 696 |
begin |
| 697 |
if Index < FdFieldOfView.Items.Count then |
| 698 |
Result := FdFieldOfView.Items[Index] else |
| 699 |
Result := GetFieldOfViewDefault(Index); |
| 700 |
end; |
| 701 |
|
| 702 |
procedure TOrthoViewpointNode.SetFieldOfView(const Index: Integer; const Value: Single); |
| 703 |
begin |
| 704 |
while Index >= FdFieldOfView.Items.Count do |
| 705 |
FdFieldOfView.Items.Add(GetFieldOfViewDefault(FdFieldOfView.Items.Count)); |
| 706 |
FdFieldOfView.Items[Index] := Value; |
| 707 |
FdFieldOfView.Changed; |
| 708 |
end; |
| 709 |
|
| 710 |
procedure TOrthoViewpointNode.SetFieldOfViewDelphiCompat(const Index: Integer; const Value: Single); |
| 711 |
begin |
| 712 |
SetFieldOfView(Index, Value); |
| 713 |
end; |
| 714 |
|
| 715 |
|
| 716 |
class function TOrthoViewpointNode.InternalFieldOfView( |
| 717 |
const AFieldOfView: TFloatRectangle; |
| 718 |
const ViewportWidth, ViewportHeight: Single): TFloatRectangle; |
| 719 |
|
| 720 |
{ Scale the extent. Since InternalFieldOfView should only make AFieldOfView |
| 721 |
larger (because OrthoViewpoint.fieldOfView gives the minimal extents), |
| 722 |
so given here Scale should always be >= 1. } |
| 723 |
procedure ScaleExtent(const Scale: Single; var Min, Max: Single); |
| 724 |
var |
| 725 |
L, Middle: Single; |
| 726 |
begin |
| 727 |
Middle := (Min + Max) / 2; |
| 728 |
L := Max - Min; |
| 729 |
|
| 730 |
if L < 0 then |
| 731 |
begin |
| 732 |
WritelnLog('OrthoViewpoint', 'OrthoViewpoint.fieldOfView max extent smaller than min extent'); |
| 733 |
Exit; |
| 734 |
end; |
| 735 |
|
| 736 |
Min := Middle - Scale * L / 2; |
| 737 |
Max := Middle + Scale * L / 2; |
| 738 |
end; |
| 739 |
|
| 740 |
var |
| 741 |
FOVAspect, WindowWidthToHeight: Single; |
| 742 |
Min, Max: Single; |
| 743 |
begin |
| 744 |
Result := AFieldOfView; |
| 745 |
|
| 746 |
if (Result.Width <= 0) or |
| 747 |
(Result.Height <= 0) then |
| 748 |
begin |
| 749 |
WritelnLog('OrthoViewpoint', 'OrthoViewpoint.fieldOfView extent (max-min) is zero'); |
| 750 |
Exit; |
| 751 |
end; |
| 752 |
|
| 753 |
FOVAspect := Result.Width / Result.Height; |
| 754 |
WindowWidthToHeight := ViewportWidth / ViewportHeight; |
| 755 |
|
| 756 |
{ The idea is to change FieldOfView, such that at the end the above |
| 757 |
equation would calculate FOVAspect as equal to WindowWidthToHeight. |
| 758 |
|
| 759 |
To do this, multiply above equation by WindowWidthToHeight / FOVAspect. |
| 760 |
We have to transform put this scale into either horizontal or vertical |
| 761 |
extent, since we only want to make FieldOfView larger (never smaller). } |
| 762 |
|
| 763 |
if FOVAspect > WindowWidthToHeight then |
| 764 |
begin |
| 765 |
Min := Result.Bottom; |
| 766 |
Max := Result.Top; |
| 767 |
ScaleExtent(FOVAspect / WindowWidthToHeight, Min, Max); |
| 768 |
Result.Bottom := Min; |
| 769 |
Result.Height := Max - Min; |
| 770 |
end else |
| 771 |
if FOVAspect < WindowWidthToHeight then |
| 772 |
begin |
| 773 |
Min := Result.Left; |
| 774 |
Max := Result.Right; |
| 775 |
ScaleExtent(WindowWidthToHeight / FOVAspect, Min, Max); |
| 776 |
Result.Left := Min; |
| 777 |
Result.Width := Max - Min; |
| 778 |
end; |
| 779 |
end; |
| 780 |
|
| 781 |
{ TViewpointNode ------------------------------------------------------------- } |
| 782 |
|
| 783 |
function TViewpointNode.PositionField: TSFVec3f; |
| 784 |
begin |
| 785 |
Result := FdPosition; |
| 786 |
end; |
| 787 |
|
| 788 |
class function TViewpointNode.ProjectionType: TProjectionType; |
| 789 |
begin |
| 790 |
Result := ptPerspective; |
| 791 |
end; |
| 792 |
|
| 793 |
function TViewpointNode.ProjectionMatrix: TMatrix4; |
| 794 |
begin |
| 795 |
{ TODO: for currently bound viewpoint, we should honour |
| 796 |
fieldOfView and aspect ratio of current window? } |
| 797 |
Result := PerspectiveProjectionMatrixRad( |
| 798 |
FdFieldOfView.Value, 1, |
| 799 |
1, 100); { TODO: near, far projection testing values } |
| 800 |
end; |
| 801 |
|
| 802 |
class function TViewpointNode.InternalFieldOfView(FieldOfView: Single; |
| 803 |
const FieldOfViewAxis: TFieldOfViewAxis; |
| 804 |
const ViewportWidth, ViewportHeight: Single): TVector2; |
| 805 |
|
| 806 |
procedure CalculateFovWidth; |
| 807 |
begin |
| 808 |
Result.X := AdjustViewAngleRadToAspectRatio( |
| 809 |
Result.Y, ViewportWidth / ViewportHeight); |
| 810 |
end; |
| 811 |
|
| 812 |
procedure CalculateFovHeight; |
| 813 |
begin |
| 814 |
Result.Y := AdjustViewAngleRadToAspectRatio( |
| 815 |
Result.X, ViewportHeight / ViewportWidth); |
| 816 |
end; |
| 817 |
|
| 818 |
begin |
| 819 |
case FieldOfViewAxis of |
| 820 |
faSmallest: |
| 821 |
begin |
| 822 |
{ This follows X3D specification about ViewpointNode fieldOfView behavior. } |
| 823 |
ClampVar(FieldOfView, 0.01, Pi - 0.01); |
| 824 |
if ViewportWidth < ViewportHeight then |
| 825 |
begin |
| 826 |
Result.X := FieldOfView; |
| 827 |
CalculateFovHeight; |
| 828 |
if Result.Y > Pi then |
| 829 |
begin |
| 830 |
Result.Y := Pi; |
| 831 |
CalculateFovWidth; |
| 832 |
end; |
| 833 |
end else |
| 834 |
begin |
| 835 |
Result.Y := FieldOfView; |
| 836 |
CalculateFovWidth; |
| 837 |
if Result.X > Pi then |
| 838 |
begin |
| 839 |
Result.X := Pi; |
| 840 |
CalculateFovHeight; |
| 841 |
end; |
| 842 |
end; |
| 843 |
end; |
| 844 |
faLargest: |
| 845 |
begin |
| 846 |
if ViewportWidth > ViewportHeight then |
| 847 |
begin |
| 848 |
Result.X := FieldOfView; |
| 849 |
CalculateFovHeight; |
| 850 |
end else |
| 851 |
begin |
| 852 |
Result.Y := FieldOfView; |
| 853 |
CalculateFovWidth; |
| 854 |
end; |
| 855 |
end; |
| 856 |
faHorizontal: |
| 857 |
begin |
| 858 |
Result.X := FieldOfView; |
| 859 |
CalculateFovHeight; |
| 860 |
end; |
| 861 |
faVertical: |
| 862 |
begin |
| 863 |
Result.Y := FieldOfView; |
| 864 |
CalculateFovWidth; |
| 865 |
end; |
| 866 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 867 |
else raise EInternalError.Create('ViewpointAngleOfView-FieldOfViewAxis?'); |
| 868 |
{$endif} |
| 869 |
end; |
| 870 |
end; |
| 871 |
|
| 872 |
{ TViewpointGroupNode -------------------------------------------------------- } |
| 873 |
|
| 874 |
function TViewpointGroupNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 875 |
begin |
| 876 |
Result := inherited; |
| 877 |
if Result <> nil then Exit; |
| 878 |
|
| 879 |
Result := FdChildren.Enumerate(Func); |
| 880 |
if Result <> nil then Exit; |
| 881 |
end; |
| 882 |
|
| 883 |
function TViewpointGroupNode.SmartDescription: string; |
| 884 |
begin |
| 885 |
Result := FdDescription.Value; |
| 886 |
if Result = '' then |
| 887 |
Result := X3DName; |
| 888 |
if Result = '' then |
| 889 |
Result := X3DType; |
| 890 |
end; |
| 891 |
|
| 892 |
procedure RegisterNavigationNodes; |
| 893 |
begin |
| 894 |
NodesManager.RegisterNodeClasses([ |
| 895 |
TBillboardNode, |
| 896 |
TCollisionNode, |
| 897 |
TLODNode, |
| 898 |
TNavigationInfoNode, |
| 899 |
TOrthoViewpointNode, |
| 900 |
TViewpointNode, |
| 901 |
TViewpointGroupNode |
| 902 |
]); |
| 903 |
end; |
| 904 |
{$endif read_implementation} |