| 1 |
{ |
| 2 |
Copyright 2006-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Helpers to visualize debug information about transformation. } |
| 17 |
unit CastleDebugTransform; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleTransform, CastleBoxes, X3DNodes, CastleScene, CastleVectors, CastleColors; |
| 23 |
|
| 24 |
type |
| 25 |
{ 3D axis, as an X3D node, to easily visualize debug things. |
| 26 |
This is useful in connection with your custom TDebugTransform descendants, |
| 27 |
to show an axis to visualize something. |
| 28 |
|
| 29 |
Create it and add the @link(Root) to your X3D scene graph |
| 30 |
within some @link(TCastleSceneCore.RootNode). |
| 31 |
You can change properties like @link(Position) at any time |
| 32 |
(before and after adding the @link(TCastleSceneCore.RootNode) |
| 33 |
to some graph). } |
| 34 |
TDebugAxis = class(TComponent) |
| 35 |
strict private |
| 36 |
FShape: TShapeNode; |
| 37 |
FGeometry: TLineSetNode; |
| 38 |
FCoord: TCoordinateNode; |
| 39 |
FTransform: TTransformNode; |
| 40 |
function GetVisible: boolean; |
| 41 |
procedure SetVisible(const Value: boolean); |
| 42 |
procedure SetPosition(const Value: TVector3); |
| 43 |
procedure SetScaleFromBox(const Value: TBox3D); |
| 44 |
public |
| 45 |
constructor Create(const AOwner: TComponent; const Color: TCastleColorRGB); reintroduce; |
| 46 |
property Root: TTransformNode read FTransform; |
| 47 |
property Visible: boolean read GetVisible write SetVisible; |
| 48 |
property Render: Boolean read GetVisible write SetVisible; {$ifdef FPC} deprecated 'use Visible'; {$endif} |
| 49 |
property Position: TVector3 {read GetPosition} {} write SetPosition; |
| 50 |
property ScaleFromBox: TBox3D {read GetScale} {} write SetScaleFromBox; |
| 51 |
end; |
| 52 |
|
| 53 |
{ 3D box, as an X3D node, to easily visualize debug things. |
| 54 |
This is a ready construction using X3D TBoxNode, TShapeNode, TTransformNode |
| 55 |
to give you a comfortable box visualization. |
| 56 |
|
| 57 |
This is useful in connection with your custom TDebugTransform descendants, |
| 58 |
to show an axis to visualize something. |
| 59 |
|
| 60 |
Create it and add the @link(Root) to your X3D scene graph |
| 61 |
within some @link(TCastleSceneCore.RootNode). |
| 62 |
You can change properties like @link(Box) at any time |
| 63 |
(before and after adding the @link(TCastleSceneCore.RootNode) |
| 64 |
to some graph). } |
| 65 |
TDebugBox = class(TComponent) |
| 66 |
strict private |
| 67 |
FColor: TCastleColor; |
| 68 |
FTransform: TTransformNode; |
| 69 |
FShape: TShapeNode; |
| 70 |
FGeometry: TBoxNode; |
| 71 |
FMaterial: TUnlitMaterialNode; |
| 72 |
procedure SetBox(const Value: TBox3D); |
| 73 |
procedure SetColor(const AValue: TCastleColor); |
| 74 |
public |
| 75 |
constructor Create(AOwner: TComponent); overload; override; |
| 76 |
constructor Create(const AOwner: TComponent; const AColor: TCastleColorRGB); reintroduce; overload; |
| 77 |
deprecated 'use Create(AOwner) and adjust Color property'; |
| 78 |
property Root: TTransformNode read FTransform; |
| 79 |
property Box: TBox3D {read GetBox} {} write SetBox; |
| 80 |
property Color: TCastleColor read FColor write SetColor; |
| 81 |
end; |
| 82 |
|
| 83 |
{ 3D sphere, as an X3D node, to easily visualize debug things. |
| 84 |
This is a ready construction using X3D TSphereNode, TShapeNode, TTransformNode |
| 85 |
to give you a comfortable sphere visualization. |
| 86 |
|
| 87 |
This is useful in connection with your custom TDebugTransform descendants, |
| 88 |
to show an axis to visualize something. |
| 89 |
|
| 90 |
Create it and add the @link(Root) to your X3D scene graph |
| 91 |
within some @link(TCastleSceneCore.RootNode). |
| 92 |
You can change properties like @link(Position) at any time |
| 93 |
(before and after adding the @link(TCastleSceneCore.RootNode) |
| 94 |
to some graph). } |
| 95 |
TDebugSphere = class(TComponent) |
| 96 |
strict private |
| 97 |
FTransform: TTransformNode; |
| 98 |
FShape: TShapeNode; |
| 99 |
FGeometry: TSphereNode; |
| 100 |
function GetVisible: boolean; |
| 101 |
procedure SetVisible(const Value: boolean); |
| 102 |
procedure SetPosition(const Value: TVector3); |
| 103 |
procedure SetRadius(const Value: Single); |
| 104 |
public |
| 105 |
constructor Create(const AOwner: TComponent; const Color: TCastleColorRGB); reintroduce; |
| 106 |
property Root: TTransformNode read FTransform; |
| 107 |
property Visible: boolean read GetVisible write SetVisible; |
| 108 |
property Render: Boolean read GetVisible write SetVisible; {$ifdef FPC} deprecated 'use Visible'; {$endif} |
| 109 |
property Position: TVector3 {read GetPosition} {} write SetPosition; |
| 110 |
property Radius: Single {read GetRadius} {} write SetRadius; |
| 111 |
end; |
| 112 |
|
| 113 |
{ 3D arrow, as an X3D node, to easily visualize debug things. |
| 114 |
|
| 115 |
This is useful in connection with your custom TDebugTransform descendants, |
| 116 |
to show an arrow to visualize something. |
| 117 |
|
| 118 |
Create it and add the @link(Root) to your X3D scene graph |
| 119 |
within some @link(TCastleSceneCore.RootNode). } |
| 120 |
TDebugArrow = class(TComponent) |
| 121 |
strict private |
| 122 |
FTransform: TTransformNode; |
| 123 |
FShape: TShapeNode; |
| 124 |
FOrigin, FDirection: TVector3; |
| 125 |
Coord: TCoordinateNode; |
| 126 |
procedure SetOrigin(const Value: TVector3); |
| 127 |
procedure SetDirection(const Value: TVector3); |
| 128 |
procedure UpdateGeometry; |
| 129 |
function GetVisible: boolean; |
| 130 |
procedure SetVisible(const Value: boolean); |
| 131 |
public |
| 132 |
constructor Create(const AOwner: TComponent; const Color: TCastleColorRGB); reintroduce; |
| 133 |
property Root: TTransformNode read FTransform; |
| 134 |
property Origin: TVector3 read FOrigin write SetOrigin; |
| 135 |
property Direction: TVector3 read FDirection write SetDirection; |
| 136 |
property Visible: boolean read GetVisible write SetVisible; |
| 137 |
property Render: Boolean read GetVisible write SetVisible; {$ifdef FPC} deprecated 'use Visible'; {$endif} |
| 138 |
end; |
| 139 |
|
| 140 |
{ Visualization of a bounding volume of a TCastleTransform instance. |
| 141 |
After constructing this, set @link(Parent) to attach this to some |
| 142 |
@link(TCastleTransform) instance. |
| 143 |
|
| 144 |
Then set @link(Exists) (which is by default @false) to control whether the debug visualization |
| 145 |
should actually be shown. We take care to only actually construct |
| 146 |
internal TCastleScene when the @link(Exists) becomes @true, |
| 147 |
so you can construct TDebugTransform instance always, even in release mode -- |
| 148 |
it does not take up resources if never visible. } |
| 149 |
TDebugTransformBox = class(TComponent) |
| 150 |
strict private |
| 151 |
type |
| 152 |
TInternalScene = class(TCastleScene) |
| 153 |
Container: TDebugTransformBox; |
| 154 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 155 |
procedure LocalRender(const Params: TRenderParams); override; |
| 156 |
end; |
| 157 |
var |
| 158 |
FBox: TDebugBox; |
| 159 |
FTransform: TMatrixTransformNode; |
| 160 |
FParentSpace: TAbstractGroupingNode; |
| 161 |
FParent: TCastleTransform; |
| 162 |
FScene: TInternalScene; |
| 163 |
FExists: boolean; |
| 164 |
FBoxColor: TCastleColor; |
| 165 |
procedure SetBoxColor(const AValue: TCastleColor); |
| 166 |
procedure UpdateSafe; |
| 167 |
procedure SetExists(const Value: boolean); |
| 168 |
procedure SetParent(const Value: TCastleTransform); |
| 169 |
procedure Initialize; |
| 170 |
strict protected |
| 171 |
{ Called when internal scene is constructed. |
| 172 |
You can override it in desdendants to e.g. add more stuff to ParentSpace. } |
| 173 |
procedure InitializeNodes; virtual; |
| 174 |
|
| 175 |
{ Called continuosly when internal scene should be updated. |
| 176 |
You can override it in desdendants to e.g. update the things you added |
| 177 |
in @link(Initialize). } |
| 178 |
procedure Update; virtual; |
| 179 |
protected |
| 180 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 181 |
public |
| 182 |
constructor Create(AOwner: TComponent); override; |
| 183 |
destructor Destroy; override; |
| 184 |
procedure Attach(const AParent: TCastleTransform); deprecated 'set Parent instead'; |
| 185 |
{ Determines what is visualizated by this component. |
| 186 |
May be @nil, which means that nothing is visualized. } |
| 187 |
property Parent: TCastleTransform read FParent write SetParent; |
| 188 |
{ Is the debug visualization visible. } |
| 189 |
property Exists: boolean read FExists write SetExists default false; |
| 190 |
{ Add to this additional things that are expressed in parent coordinate-space. |
| 191 |
Be sure to call @link(ChangedScene) afterwards, unless you do it in InitializeNodes |
| 192 |
(then @link(ChangedScene) is not necessary). } |
| 193 |
property ParentSpace: TAbstractGroupingNode read FParentSpace; |
| 194 |
property BoxColor: TCastleColor read FBoxColor write SetBoxColor; |
| 195 |
procedure ChangedScene; |
| 196 |
end; |
| 197 |
|
| 198 |
{ Like TDebugTransformBox, but visualizes also additional properties. |
| 199 |
This is useful to visualize typical properties of TCastleTransform |
| 200 |
that represents a creature: things that determine how the creature is moving, |
| 201 |
if you use methods like @link(TCastleTransform.MoveAllowed). |
| 202 |
|
| 203 |
Adds visualization of: |
| 204 |
|
| 205 |
@unorderedList( |
| 206 |
@item(@link(TCastleTransform.Middle)) |
| 207 |
@item(@link(TCastleTransform.Sphere), that is usually determined by |
| 208 |
the @link(TCastleTransform.CollisionSphereRadius) property) |
| 209 |
@item(@link(TCastleTransform.Direction)) |
| 210 |
) } |
| 211 |
TDebugTransform = class(TDebugTransformBox) |
| 212 |
strict private |
| 213 |
FDirectionArrow: TDebugArrow; |
| 214 |
FSphere: TDebugSphere; |
| 215 |
FMiddleAxis: TDebugAxis; |
| 216 |
strict protected |
| 217 |
procedure InitializeNodes; override; |
| 218 |
procedure Update; override; |
| 219 |
end; |
| 220 |
|
| 221 |
implementation |
| 222 |
|
| 223 |
uses CastleLog; |
| 224 |
|
| 225 |
{ TDebugAxis ----------------------------------------------------------------- } |
| 226 |
|
| 227 |
constructor TDebugAxis.Create(const AOwner: TComponent; const Color: TCastleColorRGB); |
| 228 |
var |
| 229 |
Material: TUnlitMaterialNode; |
| 230 |
Appearance: TAppearanceNode; |
| 231 |
begin |
| 232 |
inherited Create(AOwner); |
| 233 |
|
| 234 |
FCoord := TCoordinateNode.Create; |
| 235 |
FCoord.SetPoint([ |
| 236 |
Vector3(-1, 0, 0), Vector3(1, 0, 0), |
| 237 |
Vector3( 0, -1, 0), Vector3(0, 1, 0), |
| 238 |
Vector3( 0, 0, -1), Vector3(0, 0, 1) |
| 239 |
]); |
| 240 |
|
| 241 |
FGeometry := TLineSetNode.Create; |
| 242 |
FGeometry.Mode := lmPair; |
| 243 |
FGeometry.Coord := FCoord; |
| 244 |
|
| 245 |
Material := TUnlitMaterialNode.Create; |
| 246 |
Material.EmissiveColor := Color; |
| 247 |
|
| 248 |
Appearance := TAppearanceNode.Create; |
| 249 |
Appearance.ShadowCaster := false; |
| 250 |
Appearance.Material := Material; |
| 251 |
|
| 252 |
FShape := TShapeNode.Create; |
| 253 |
FShape.Geometry := FGeometry; |
| 254 |
FShape.Appearance := Appearance; |
| 255 |
|
| 256 |
FTransform := TTransformNode.Create; |
| 257 |
FTransform.AddChildren(FShape); |
| 258 |
end; |
| 259 |
|
| 260 |
function TDebugAxis.GetVisible: boolean; |
| 261 |
begin |
| 262 |
Result := FShape.Visible; |
| 263 |
end; |
| 264 |
|
| 265 |
procedure TDebugAxis.SetVisible(const Value: boolean); |
| 266 |
begin |
| 267 |
FShape.Visible := Value; |
| 268 |
end; |
| 269 |
|
| 270 |
procedure TDebugAxis.SetPosition(const Value: TVector3); |
| 271 |
begin |
| 272 |
FTransform.Translation := Value; |
| 273 |
end; |
| 274 |
|
| 275 |
procedure TDebugAxis.SetScaleFromBox(const Value: TBox3D); |
| 276 |
var |
| 277 |
ScaleFactor: Single; |
| 278 |
begin |
| 279 |
ScaleFactor := Value.AverageSize(true, 1) / 2; |
| 280 |
FTransform.Scale := Vector3(ScaleFactor, ScaleFactor, ScaleFactor); |
| 281 |
end; |
| 282 |
|
| 283 |
{ TDebugBox ----------------------------------------------------------------- } |
| 284 |
|
| 285 |
constructor TDebugBox.Create(const AOwner: TComponent; const AColor: TCastleColorRGB); |
| 286 |
begin |
| 287 |
Create(AOwner); |
| 288 |
Color := Vector4(AColor, 1); |
| 289 |
end; |
| 290 |
|
| 291 |
constructor TDebugBox.Create(AOwner: TComponent); |
| 292 |
var |
| 293 |
Appearance: TAppearanceNode; |
| 294 |
begin |
| 295 |
inherited Create(AOwner); |
| 296 |
|
| 297 |
FColor := White; |
| 298 |
|
| 299 |
FGeometry := TBoxNode.Create; |
| 300 |
|
| 301 |
FMaterial := TUnlitMaterialNode.Create; |
| 302 |
FMaterial.EmissiveColor := FColor.XYZ; |
| 303 |
FMaterial.Transparency := 1 - FColor.W; |
| 304 |
|
| 305 |
Appearance := TAppearanceNode.Create; |
| 306 |
Appearance.ShadowCaster := false; |
| 307 |
Appearance.Material := FMaterial; |
| 308 |
|
| 309 |
FShape := TShapeNode.Create; |
| 310 |
FShape.Geometry := FGeometry; |
| 311 |
FShape.Shading := shWireframe; |
| 312 |
FShape.Appearance := Appearance; |
| 313 |
|
| 314 |
FTransform := TTransformNode.Create; |
| 315 |
FTransform.AddChildren(FShape); |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TDebugBox.SetBox(const Value: TBox3D); |
| 319 |
begin |
| 320 |
FShape.Visible := not Value.IsEmpty; |
| 321 |
if FShape.Visible then |
| 322 |
begin |
| 323 |
FGeometry.Size := Value.Size; |
| 324 |
FTransform.Translation := Value.Center; |
| 325 |
end; |
| 326 |
end; |
| 327 |
|
| 328 |
procedure TDebugBox.SetColor(const AValue: TCastleColor); |
| 329 |
begin |
| 330 |
if TCastleColor.PerfectlyEquals(FColor, AValue) then Exit; |
| 331 |
FColor := AValue; |
| 332 |
FMaterial.EmissiveColor := FColor.XYZ; |
| 333 |
FMaterial.Transparency := 1 - FColor.W; |
| 334 |
end; |
| 335 |
|
| 336 |
{ TDebugSphere ----------------------------------------------------------------- } |
| 337 |
|
| 338 |
constructor TDebugSphere.Create(const AOwner: TComponent; const Color: TCastleColorRGB); |
| 339 |
var |
| 340 |
Material: TUnlitMaterialNode; |
| 341 |
Appearance: TAppearanceNode; |
| 342 |
begin |
| 343 |
inherited Create(AOwner); |
| 344 |
|
| 345 |
FGeometry := TSphereNode.Create; |
| 346 |
FGeometry.Slices := 10; |
| 347 |
FGeometry.Stacks := 10; |
| 348 |
|
| 349 |
Material := TUnlitMaterialNode.Create; |
| 350 |
Material.EmissiveColor := Color; |
| 351 |
|
| 352 |
Appearance := TAppearanceNode.Create; |
| 353 |
Appearance.ShadowCaster := false; |
| 354 |
Appearance.Material := Material; |
| 355 |
|
| 356 |
FShape := TShapeNode.Create; |
| 357 |
FShape.Geometry := FGeometry; |
| 358 |
FShape.Shading := shWireframe; |
| 359 |
FShape.Appearance := Appearance; |
| 360 |
|
| 361 |
FTransform := TTransformNode.Create; |
| 362 |
FTransform.AddChildren(FShape); |
| 363 |
end; |
| 364 |
|
| 365 |
function TDebugSphere.GetVisible: boolean; |
| 366 |
begin |
| 367 |
Result := FShape.Visible; |
| 368 |
end; |
| 369 |
|
| 370 |
procedure TDebugSphere.SetVisible(const Value: boolean); |
| 371 |
begin |
| 372 |
FShape.Visible := Value; |
| 373 |
end; |
| 374 |
|
| 375 |
procedure TDebugSphere.SetPosition(const Value: TVector3); |
| 376 |
begin |
| 377 |
FTransform.Translation := Value; |
| 378 |
end; |
| 379 |
|
| 380 |
procedure TDebugSphere.SetRadius(const Value: Single); |
| 381 |
begin |
| 382 |
FGeometry.Radius := Value; |
| 383 |
end; |
| 384 |
|
| 385 |
{ TDebugArrow ----------------------------------------------------------------- } |
| 386 |
|
| 387 |
constructor TDebugArrow.Create(const AOwner: TComponent; const Color: TCastleColorRGB); |
| 388 |
var |
| 389 |
Material: TUnlitMaterialNode; |
| 390 |
Appearance: TAppearanceNode; |
| 391 |
FGeometry: TLineSetNode; |
| 392 |
begin |
| 393 |
inherited Create(AOwner); |
| 394 |
|
| 395 |
FGeometry := TLineSetNode.CreateWithTransform(FShape, FTransform); |
| 396 |
|
| 397 |
Material := TUnlitMaterialNode.Create; |
| 398 |
Material.EmissiveColor := Color; |
| 399 |
|
| 400 |
Appearance := TAppearanceNode.Create; |
| 401 |
Appearance.ShadowCaster := false; |
| 402 |
Appearance.Material := Material; |
| 403 |
|
| 404 |
FShape.Appearance := Appearance; |
| 405 |
|
| 406 |
Coord := TCoordinateNode.Create; |
| 407 |
|
| 408 |
FGeometry.Coord := Coord; |
| 409 |
FGeometry.SetVertexCount([2, 2, 2, 2, 2]); |
| 410 |
|
| 411 |
{ Make the initial geometry. Although it is useless, this will avoid warning |
| 412 |
"Too much lines (not enough coordinates) in LineSet". } |
| 413 |
UpdateGeometry; |
| 414 |
end; |
| 415 |
|
| 416 |
procedure TDebugArrow.SetOrigin(const Value: TVector3); |
| 417 |
begin |
| 418 |
if not TVector3.PerfectlyEquals(FOrigin, Value) then |
| 419 |
begin |
| 420 |
FOrigin := Value; |
| 421 |
UpdateGeometry; |
| 422 |
end; |
| 423 |
end; |
| 424 |
|
| 425 |
procedure TDebugArrow.SetDirection(const Value: TVector3); |
| 426 |
begin |
| 427 |
if not TVector3.PerfectlyEquals(FDirection, Value) then |
| 428 |
begin |
| 429 |
FDirection := Value; |
| 430 |
UpdateGeometry; |
| 431 |
end; |
| 432 |
end; |
| 433 |
|
| 434 |
procedure TDebugArrow.UpdateGeometry; |
| 435 |
var |
| 436 |
OrthoDirection, OrthoDirection2: TVector3; |
| 437 |
begin |
| 438 |
OrthoDirection := AnyOrthogonalVector(Direction); |
| 439 |
OrthoDirection2 := TVector3.CrossProduct(Direction, OrthoDirection); |
| 440 |
|
| 441 |
OrthoDirection := OrthoDirection.AdjustToLength(Direction.Length / 4); |
| 442 |
OrthoDirection2 := OrthoDirection2.AdjustToLength(Direction.Length / 4); |
| 443 |
|
| 444 |
Coord.SetPoint([ |
| 445 |
Origin, |
| 446 |
Origin + Direction, |
| 447 |
Origin + Direction, |
| 448 |
Origin + Direction * 0.75 + OrthoDirection, |
| 449 |
Origin + Direction, |
| 450 |
Origin + Direction * 0.75 - OrthoDirection, |
| 451 |
Origin + Direction, |
| 452 |
Origin + Direction * 0.75 + OrthoDirection2, |
| 453 |
Origin + Direction, |
| 454 |
Origin + Direction * 0.75 - OrthoDirection2 |
| 455 |
]); |
| 456 |
end; |
| 457 |
|
| 458 |
function TDebugArrow.GetVisible: boolean; |
| 459 |
begin |
| 460 |
Result := FShape.Visible; |
| 461 |
end; |
| 462 |
|
| 463 |
procedure TDebugArrow.SetVisible(const Value: boolean); |
| 464 |
begin |
| 465 |
FShape.Visible := Value; |
| 466 |
end; |
| 467 |
|
| 468 |
{ TDebugTransform.TInternalScene ---------------------------------------------------- } |
| 469 |
|
| 470 |
procedure TDebugTransformBox.TInternalScene.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 471 |
begin |
| 472 |
inherited; |
| 473 |
Container.UpdateSafe; |
| 474 |
end; |
| 475 |
|
| 476 |
procedure TDebugTransformBox.TInternalScene.LocalRender(const Params: TRenderParams); |
| 477 |
const |
| 478 |
DistanceToHide = 1; |
| 479 |
var |
| 480 |
DistanceToCameraSqr: Single; |
| 481 |
GizmoVisible: Boolean; |
| 482 |
begin |
| 483 |
{ Do not render cameras debug, when their center is equal or very close |
| 484 |
to the rendering camera. |
| 485 |
This avoids weird debug display of camera on top of itself. } |
| 486 |
if Exists and (Parent is TCastleCamera) then |
| 487 |
begin |
| 488 |
DistanceToCameraSqr := PointsDistanceSqr( |
| 489 |
Params.Transformation^.Transform.MultPoint(TVector3.Zero), |
| 490 |
Params.RenderingCamera.View.Translation |
| 491 |
); |
| 492 |
GizmoVisible := DistanceToCameraSqr > Sqr(DistanceToHide); |
| 493 |
if not GizmoVisible then |
| 494 |
Exit; |
| 495 |
end; |
| 496 |
inherited; |
| 497 |
end; |
| 498 |
|
| 499 |
{ TDebugTransformBox ---------------------------------------------------- } |
| 500 |
|
| 501 |
constructor TDebugTransformBox.Create(AOwner: TComponent); |
| 502 |
begin |
| 503 |
inherited Create(AOwner); |
| 504 |
FBoxColor := Gray; |
| 505 |
end; |
| 506 |
|
| 507 |
destructor TDebugTransformBox.Destroy; |
| 508 |
begin |
| 509 |
{ set to nil by SetParent, to detach free notification } |
| 510 |
Parent := nil; |
| 511 |
inherited; |
| 512 |
end; |
| 513 |
|
| 514 |
procedure TDebugTransformBox.Notification(AComponent: TComponent; Operation: TOperation); |
| 515 |
begin |
| 516 |
inherited; |
| 517 |
if (Operation = opRemove) and (AComponent = FParent) then |
| 518 |
{ set to nil by SetParent, to detach free notification } |
| 519 |
Parent := nil; |
| 520 |
end; |
| 521 |
|
| 522 |
procedure TDebugTransformBox.Initialize; |
| 523 |
var |
| 524 |
Root: TX3DRootNode; |
| 525 |
begin |
| 526 |
FTransform := TMatrixTransformNode.Create; |
| 527 |
FParentSpace := FTransform; |
| 528 |
|
| 529 |
FBox := TDebugBox.Create(Self); |
| 530 |
FBox.Color := FBoxColor; |
| 531 |
ParentSpace.AddChildren(FBox.Root); |
| 532 |
|
| 533 |
InitializeNodes; |
| 534 |
|
| 535 |
Root := TX3DRootNode.Create; |
| 536 |
Root.AddChildren(FTransform); |
| 537 |
|
| 538 |
FScene := TInternalScene.Create(Self); |
| 539 |
FScene.Container := Self; |
| 540 |
FScene.Load(Root, true); |
| 541 |
FScene.Collides := false; |
| 542 |
FScene.Pickable := false; |
| 543 |
FScene.CastShadows := false; |
| 544 |
FScene.InternalExcludeFromParentBoundingVolume := true; |
| 545 |
FScene.InternalExistsOnlyInMeaningfulParents := true; |
| 546 |
FScene.Exists := FExists; |
| 547 |
FScene.SetTransient; |
| 548 |
end; |
| 549 |
|
| 550 |
procedure TDebugTransformBox.InitializeNodes; |
| 551 |
begin |
| 552 |
end; |
| 553 |
|
| 554 |
procedure TDebugTransformBox.SetParent(const Value: TCastleTransform); |
| 555 |
begin |
| 556 |
if FParent <> Value then |
| 557 |
begin |
| 558 |
if FParent <> nil then |
| 559 |
begin |
| 560 |
Assert(FScene <> nil); |
| 561 |
// remove self from previous parent children |
| 562 |
if not (csDestroying in FParent.ComponentState) then // testcase why it's needed: fps_game exit |
| 563 |
FParent.Remove(FScene); |
| 564 |
FParent.RemoveFreeNotification(Self); |
| 565 |
end; |
| 566 |
|
| 567 |
FParent := Value; |
| 568 |
|
| 569 |
if FParent <> nil then |
| 570 |
begin |
| 571 |
if FScene = nil then |
| 572 |
Initialize; |
| 573 |
FParent.FreeNotification(Self); |
| 574 |
FParent.Add(FScene); |
| 575 |
UpdateSafe; |
| 576 |
end; |
| 577 |
end; |
| 578 |
end; |
| 579 |
|
| 580 |
procedure TDebugTransformBox.Attach(const AParent: TCastleTransform); |
| 581 |
begin |
| 582 |
Parent := AParent; |
| 583 |
end; |
| 584 |
|
| 585 |
procedure TDebugTransformBox.SetExists(const Value: boolean); |
| 586 |
begin |
| 587 |
if FExists <> Value then |
| 588 |
begin |
| 589 |
FExists := Value; |
| 590 |
if FScene <> nil then |
| 591 |
FScene.Exists := Value; |
| 592 |
if Value then |
| 593 |
UpdateSafe; |
| 594 |
end; |
| 595 |
end; |
| 596 |
|
| 597 |
procedure TDebugTransformBox.UpdateSafe; |
| 598 |
begin |
| 599 |
if Exists and |
| 600 |
(FParent <> nil) then |
| 601 |
begin |
| 602 |
if FScene = nil then |
| 603 |
Initialize; |
| 604 |
Update; |
| 605 |
end; |
| 606 |
end; |
| 607 |
|
| 608 |
procedure TDebugTransformBox.SetBoxColor(const AValue: TCastleColor); |
| 609 |
begin |
| 610 |
FBoxColor := AValue; |
| 611 |
if FBox <> nil then |
| 612 |
FBox.Color := AValue; |
| 613 |
end; |
| 614 |
|
| 615 |
procedure TDebugTransformBox.Update; |
| 616 |
begin |
| 617 |
// update FTransform to cancel parent's transformation |
| 618 |
FTransform.Matrix := FParent.InverseTransform; |
| 619 |
|
| 620 |
// show FParent.BoundingBox |
| 621 |
FBox.Box := FParent.BoundingBox; |
| 622 |
end; |
| 623 |
|
| 624 |
procedure TDebugTransformBox.ChangedScene; |
| 625 |
begin |
| 626 |
FScene.ChangedAll; |
| 627 |
end; |
| 628 |
|
| 629 |
{ TDebugTransform ---------------------------------------------------- } |
| 630 |
|
| 631 |
procedure TDebugTransform.InitializeNodes; |
| 632 |
begin |
| 633 |
inherited; |
| 634 |
|
| 635 |
FDirectionArrow := TDebugArrow.Create(Self, BlueRGB); |
| 636 |
ParentSpace.AddChildren(FDirectionArrow.Root); |
| 637 |
|
| 638 |
FSphere := TDebugSphere.Create(Self, GrayRGB); |
| 639 |
ParentSpace.AddChildren(FSphere.Root); |
| 640 |
|
| 641 |
FMiddleAxis := TDebugAxis.Create(Self, YellowRGB); |
| 642 |
ParentSpace.AddChildren(FMiddleAxis.Root); |
| 643 |
end; |
| 644 |
|
| 645 |
procedure TDebugTransform.Update; |
| 646 |
var |
| 647 |
R: Single; |
| 648 |
NewVisible: Boolean; |
| 649 |
begin |
| 650 |
inherited; |
| 651 |
|
| 652 |
NewVisible := Parent.World <> nil; |
| 653 |
|
| 654 |
// when Parent.World = nil then Parent.Middle and Parent.PreferredHeight cannot be calculated |
| 655 |
FSphere.Visible := NewVisible; |
| 656 |
FDirectionArrow.Visible := NewVisible; |
| 657 |
FMiddleAxis.Visible := NewVisible; |
| 658 |
|
| 659 |
if NewVisible then |
| 660 |
begin |
| 661 |
// show FParent.Sphere |
| 662 |
FSphere.Visible := Parent.Sphere(R); |
| 663 |
if FSphere.Visible then |
| 664 |
begin |
| 665 |
FSphere.Position := Parent.Middle; |
| 666 |
FSphere.Radius := R; |
| 667 |
end; |
| 668 |
|
| 669 |
// show FParent.Direction |
| 670 |
FDirectionArrow.Origin := Parent.Middle; |
| 671 |
FDirectionArrow.Direction := Parent.Direction; |
| 672 |
|
| 673 |
// show FParent.Middle |
| 674 |
FMiddleAxis.Position := Parent.Middle; |
| 675 |
FMiddleAxis.ScaleFromBox := Parent.BoundingBox; |
| 676 |
end; |
| 677 |
end; |
| 678 |
|
| 679 |
end. |