| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-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 |
{ Base X3D node that can have some metadata. } |
| 19 |
TAbstractNode = class(TX3DNode) |
| 20 |
strict private |
| 21 |
function GetMetadataBooleanArray(const Key: String; const Index: Cardinal): Boolean; |
| 22 |
procedure SetMetadataBooleanArray(const Key: String; const Index: Cardinal; const Value: Boolean); |
| 23 |
function GetMetadataStringArray(const Key: String; const Index: Cardinal): String; |
| 24 |
procedure SetMetadataStringArray(const Key: String; const Index: Cardinal; const Value: String); |
| 25 |
function GetMetadataIntegerArray(const Key: String; const Index: Cardinal): Integer; |
| 26 |
procedure SetMetadataIntegerArray(const Key: String; const Index: Cardinal; const Value: Integer); |
| 27 |
function GetMetadataDoubleArray(const Key: String; const Index: Cardinal): Double; |
| 28 |
procedure SetMetadataDoubleArray(const Key: String; const Index: Cardinal; const Value: Double); |
| 29 |
|
| 30 |
function GetMetadataBoolean(const Key: String): Boolean; |
| 31 |
procedure SetMetadataBoolean(const Key: String; const Value: Boolean); |
| 32 |
function GetMetadataString(const Key: String): String; |
| 33 |
procedure SetMetadataString(const Key: String; const Value: String); |
| 34 |
function GetMetadataInteger(const Key: String): Integer; |
| 35 |
procedure SetMetadataInteger(const Key: String; const Value: Integer); |
| 36 |
function GetMetadataDouble(const Key: String): Double; |
| 37 |
procedure SetMetadataDouble(const Key: String; const Value: Double); |
| 38 |
public |
| 39 |
procedure InternalInsertMetadata(const M: TAbstractMetadataNode); |
| 40 |
|
| 41 |
{ Find a "metadata" information within this node with given key. |
| 42 |
Returns @nil if not found. |
| 43 |
|
| 44 |
This seeks for a TAbstractMetadataNode instance within the X3D "metadata" |
| 45 |
field of this node. Looks for TAbstractMetadataNode with |
| 46 |
@link(TAbstractMetadataNode.NameField) matching the parameter @code(Key). |
| 47 |
Note that X3D spec allows multiple metadata nodes to share the same key, |
| 48 |
this method only finds the first one. |
| 49 |
|
| 50 |
It ignores TMetadataSet when searching, as TMetadataSet doesn't contain |
| 51 |
any simple data, it serves only as container for other metadata nodes. |
| 52 |
} |
| 53 |
function FindMetadata(const Key: String): TAbstractMetadataNode; |
| 54 |
|
| 55 |
{ Get/set metadata, identified by a given Key, as a Boolean. |
| 56 |
"Metadata" is any value you want to save with this node, it has no effect on the rendering/behavior. |
| 57 |
This property hides various complexities of the X3D metadata nodes. |
| 58 |
|
| 59 |
This property looks for a TMetadataBooleanNode node inside the "metadata" of this node |
| 60 |
using @link(FindMetadata). |
| 61 |
If found, it allows to operate @link(TMetadataBooleanNode.FdValue) array. |
| 62 |
The MetadataBooleanArray allows to get/set appropriate items of this array. |
| 63 |
The MetadataBoolean simply gets/sets the first array item. |
| 64 |
|
| 65 |
If such metadata node is not found, |
| 66 |
or it's @link(TMetadataBooleanNode.FdValue) array is too short, |
| 67 |
this returns false. |
| 68 |
When setting, it adds such metadata node if necessary, |
| 69 |
and increases the @link(TMetadataBooleanNode.FdValue) array length if necessary. } |
| 70 |
property MetadataBoolean[const Key: String]: Boolean read GetMetadataBoolean write SetMetadataBoolean; |
| 71 |
property MetadataBooleanArray[const Key: String; const Index: Cardinal]: Boolean read GetMetadataBooleanArray write SetMetadataBooleanArray; |
| 72 |
|
| 73 |
{ Get/set metadata, identified by a given Key, as a String. |
| 74 |
"Metadata" is any value you want to save with this node, it has no effect on the rendering/behavior. |
| 75 |
This property hides various complexities of the X3D metadata nodes. |
| 76 |
|
| 77 |
This property looks for a TMetadataStringNode node inside the "metadata" of this node |
| 78 |
using @link(FindMetadata). |
| 79 |
If found, it allows to operate @link(TMetadataStringNode.FdValue) array. |
| 80 |
The MetadataStringArray allows to get/set appropriate items of this array. |
| 81 |
The MetadataString simply gets/sets the first array item. |
| 82 |
|
| 83 |
If such metadata node is not found, |
| 84 |
or it's @link(TMetadataStringNode.FdValue) array is too short, |
| 85 |
this returns ''. |
| 86 |
When setting, it adds such metadata node if necessary, |
| 87 |
and increases the @link(TMetadataStringNode.FdValue) array length if necessary. } |
| 88 |
property MetadataString[const Key: String]: String read GetMetadataString write SetMetadataString; |
| 89 |
property MetadataStringArray[const Key: String; const Index: Cardinal]: String read GetMetadataStringArray write SetMetadataStringArray; |
| 90 |
|
| 91 |
{ Get/set metadata, identified by a given Key, as a Integer. |
| 92 |
"Metadata" is any value you want to save with this node, it has no effect on the rendering/behavior. |
| 93 |
This property hides various complexities of the X3D metadata nodes. |
| 94 |
|
| 95 |
This property looks for a TMetadataIntegerNode node inside the "metadata" of this node |
| 96 |
using @link(FindMetadata). |
| 97 |
If found, it allows to operate @link(TMetadataIntegerNode.FdValue) array. |
| 98 |
The MetadataIntegerArray allows to get/set appropriate items of this array. |
| 99 |
The MetadataInteger simply gets/sets the first array item. |
| 100 |
|
| 101 |
If such metadata node is not found, |
| 102 |
or it's @link(TMetadataIntegerNode.FdValue) array is too short, |
| 103 |
this returns 0. |
| 104 |
When setting, it adds such metadata node if necessary, |
| 105 |
and increases the @link(TMetadataIntegerNode.FdValue) array length if necessary. } |
| 106 |
property MetadataInteger[const Key: String]: Integer read GetMetadataInteger write SetMetadataInteger; |
| 107 |
property MetadataIntegerArray[const Key: String; const Index: Cardinal]: Integer read GetMetadataIntegerArray write SetMetadataIntegerArray; |
| 108 |
|
| 109 |
{ Get/set metadata, identified by a given Key, as a Double. |
| 110 |
"Metadata" is any value you want to save with this node, it has no effect on the rendering/behavior. |
| 111 |
This property hides various complexities of the X3D metadata nodes. |
| 112 |
|
| 113 |
This property looks for a TMetadataDoubleNode or TMetadataFloatNode |
| 114 |
node inside the "metadata" of this node using @link(FindMetadata). |
| 115 |
If found, it allows to operate @link(TMetadataDoubleNode.FdValue) array |
| 116 |
or @link(TMetadataFloatNode.FdValue) array. |
| 117 |
The MetadataDoubleArray allows to get/set appropriate items of this array. |
| 118 |
The MetadataDouble simply gets/sets the first array item. |
| 119 |
|
| 120 |
If such metadata node is not found, |
| 121 |
or it's @link(TMetadataDoubleNode.FdValue) array is too short, |
| 122 |
this returns 0.0. |
| 123 |
When setting, it adds such metadata node if necessary, |
| 124 |
and increases the @link(TMetadataDoubleNode.FdValue) array length if necessary. } |
| 125 |
property MetadataDouble[const Key: String]: Double read GetMetadataDouble write SetMetadataDouble; |
| 126 |
property MetadataDoubleArray[const Key: String; const Index: Cardinal]: Double read GetMetadataDoubleArray write SetMetadataDoubleArray; |
| 127 |
|
| 128 |
{$I auto_generated_node_helpers/x3dnodes_x3dnode.inc} |
| 129 |
end; |
| 130 |
|
| 131 |
{ Basis for all metadata nodes. } |
| 132 |
TAbstractMetadataNode = class(TAbstractNode) |
| 133 |
protected |
| 134 |
function DefaultContainerFieldInContext(const Version: TX3DVersion; |
| 135 |
const ParentNode: TX3DNode): String; override; |
| 136 |
{$I auto_generated_node_helpers/x3dnodes_x3dmetadatanode.inc} |
| 137 |
end; |
| 138 |
|
| 139 |
{ Abstract node type that indicates that the node may be used as |
| 140 |
a child of a grouping node, |
| 141 |
e.g. inside @link(TAbstractGroupingNode.FdChildren). } |
| 142 |
TAbstractChildNode = class(TAbstractNode) |
| 143 |
{$I auto_generated_node_helpers/x3dnodes_x3dchildnode.inc} |
| 144 |
end; |
| 145 |
|
| 146 |
{ Abstract base type for all bindable children nodes. } |
| 147 |
TAbstractBindableNode = class(TAbstractChildNode) |
| 148 |
strict private |
| 149 |
FBound: Boolean; |
| 150 |
FTransform: TMatrix4; |
| 151 |
FTransformScale: Single; |
| 152 |
FLastBeforeTraverseChangedTransform: Boolean; |
| 153 |
procedure EventIsBoundReceive(const Event: TX3DEvent; const AValue: TX3DField; |
| 154 |
const Time: TX3DTime); |
| 155 |
procedure SetBound(const Value: Boolean); |
| 156 |
protected |
| 157 |
procedure BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); override; |
| 158 |
public |
| 159 |
constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override; |
| 160 |
|
| 161 |
{ "Bound" node is the node currently used, for example the "bound" background |
| 162 |
determines the current background look. |
| 163 |
Setting this to @true means that the node becomes the current node |
| 164 |
of the given type in the scene. |
| 165 |
|
| 166 |
In every scene (TCastleScene), there are four "stacks" of bindable nodes: |
| 167 |
|
| 168 |
@orderedList( |
| 169 |
@item(Viewpoints determine the camera position/orientation, |
| 170 |
field of view, orthographic/perspective projection etc.) |
| 171 |
@item(NavigationInfo determines the current navigation type, |
| 172 |
avatar size etc.) |
| 173 |
@item(Background determines the dispayed background (like a skybox |
| 174 |
or a simple solid color).) |
| 175 |
@item(Fog determines the current fog.) |
| 176 |
) |
| 177 |
|
| 178 |
The top-most node of each stack is called "bound". |
| 179 |
If the scene is set as TCastleRootTransform.MainScene, |
| 180 |
then the "bound" node determines the camera, background or fog settings. |
| 181 |
|
| 182 |
Setting this property to @true sends an X3D event "set_bind := true". |
| 183 |
This adds the node to the stack, and makes sure it is at the top. |
| 184 |
Setting this to @false pops the node from the stack. |
| 185 |
See the X3D specification about the "bindable nodes" foe details: |
| 186 |
http://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/core.html#BindableChildrenNodes |
| 187 |
} |
| 188 |
property Bound: Boolean read FBound write SetBound; |
| 189 |
|
| 190 |
{ Transformation of this bindable node. |
| 191 |
|
| 192 |
Bound nodes cannot be instantiated multiple number of times |
| 193 |
(this would make simple event like "set_bind" not possible, |
| 194 |
as it would not be known in what coordinate space the node is bound), |
| 195 |
so it's perfectly safe and comfortable to just keep their transformation |
| 196 |
here, a their property. |
| 197 |
|
| 198 |
It is gathered during traversing. Last BeforeTraverse call for this |
| 199 |
node sets Transform properties. By default, these represent identity |
| 200 |
transformation. |
| 201 |
|
| 202 |
Note that using TransformScale for bindable nodes like fog |
| 203 |
is a little simplification. Theoretically, the scale can be non-uniform, |
| 204 |
and around an arbitrary axis. So to apply e.g. fog, we should |
| 205 |
transform the 3D world back into local fog coordinate system, |
| 206 |
and calculate the distances there. Instead right now we do |
| 207 |
the opposite: transform stuff like fog VisibilityRange by |
| 208 |
TransformScale, and calculate distances in world coordinate |
| 209 |
system. This is a simplification, but in practice it's perfect |
| 210 |
(who uses non-uniform fog scale?) and it can be expressed for renderers |
| 211 |
(OpenGL) without any problems. |
| 212 |
|
| 213 |
@groupBegin } |
| 214 |
property Transform: TMatrix4 read FTransform; |
| 215 |
property TransformScale: Single read FTransformScale; |
| 216 |
{ @groupEnd } |
| 217 |
|
| 218 |
property InternalLastBeforeTraverseChangedTransform: Boolean |
| 219 |
read FLastBeforeTraverseChangedTransform; |
| 220 |
|
| 221 |
{$I auto_generated_node_helpers/x3dnodes_x3dbindablenode.inc} |
| 222 |
end; |
| 223 |
|
| 224 |
{ Base class for all nodes that contain only information without visual |
| 225 |
semantics. } |
| 226 |
TAbstractInfoNode = class(TAbstractChildNode) |
| 227 |
{$I auto_generated_node_helpers/x3dnodes_x3dinfonode.inc} |
| 228 |
end; |
| 229 |
|
| 230 |
{ Base abstract class for most (but not all) sensors. |
| 231 |
We may introduce TSensorFunctionality in the future if it will be necessary, |
| 232 |
to express X3DSensorNode exactly. } |
| 233 |
TAbstractSensorNode = class(TAbstractChildNode) |
| 234 |
{$I auto_generated_node_helpers/x3dnodes_x3dsensornode.inc} |
| 235 |
end; |
| 236 |
|
| 237 |
{ A metadata (extra, custom information at a node) with a boolean type. } |
| 238 |
TMetadataBooleanNode = class(TAbstractMetadataNode) |
| 239 |
{$I auto_generated_node_helpers/x3dnodes_metadataboolean.inc} |
| 240 |
end; |
| 241 |
|
| 242 |
{ A metadata (extra, custom information at a node) with |
| 243 |
a double-precision floating point number type. } |
| 244 |
TMetadataDoubleNode = class(TAbstractMetadataNode) |
| 245 |
{$I auto_generated_node_helpers/x3dnodes_metadatadouble.inc} |
| 246 |
end; |
| 247 |
|
| 248 |
{ A metadata (extra, custom information at a node) with |
| 249 |
a single-precision floating point number type. } |
| 250 |
TMetadataFloatNode = class(TAbstractMetadataNode) |
| 251 |
{$I auto_generated_node_helpers/x3dnodes_metadatafloat.inc} |
| 252 |
end; |
| 253 |
|
| 254 |
{ A metadata (extra, custom information at a node) with an integer type. } |
| 255 |
TMetadataIntegerNode = class(TAbstractMetadataNode) |
| 256 |
{$I auto_generated_node_helpers/x3dnodes_metadatainteger.inc} |
| 257 |
end; |
| 258 |
|
| 259 |
{ A metadata (extra, custom information at a node) set, |
| 260 |
to reference of collection of other metadata nodes. } |
| 261 |
TMetadataSetNode = class(TAbstractMetadataNode) |
| 262 |
{$I auto_generated_node_helpers/x3dnodes_metadataset.inc} |
| 263 |
end; |
| 264 |
|
| 265 |
{ A metadata (extra, custom information at a node) with a string type. } |
| 266 |
TMetadataStringNode = class(TAbstractMetadataNode) |
| 267 |
{$I auto_generated_node_helpers/x3dnodes_metadatastring.inc} |
| 268 |
end; |
| 269 |
|
| 270 |
{ Information about the world. |
| 271 |
This node is strictly for documentation purposes |
| 272 |
and has no effect on the visual appearance or behavior of the world. } |
| 273 |
TWorldInfoNode = class(TAbstractInfoNode) |
| 274 |
{$I auto_generated_node_helpers/x3dnodes_worldinfo.inc} |
| 275 |
end; |
| 276 |
|
| 277 |
{$endif read_interface} |
| 278 |
|
| 279 |
{$ifdef read_implementation} |
| 280 |
|
| 281 |
{ TAbstractNode -------------------------------------------------------------- } |
| 282 |
|
| 283 |
function TAbstractNode.FindMetadata(const Key: String): TAbstractMetadataNode; |
| 284 |
|
| 285 |
function FindInSet(const MetadataSet: TMetadataSetNode): TAbstractMetadataNode; forward; |
| 286 |
|
| 287 |
{ Search a Metadata node, |
| 288 |
which may be nil, |
| 289 |
may be TMetadataSetNode, |
| 290 |
may be other TAbstractMetadataNode. } |
| 291 |
function FindInSetOrSingle(const PossibleMetadata: TX3DNode): TAbstractMetadataNode; |
| 292 |
begin; |
| 293 |
Result := nil; |
| 294 |
if PossibleMetadata <> nil then |
| 295 |
begin |
| 296 |
if PossibleMetadata is TMetadataSetNode then |
| 297 |
Result := FindInSet(TMetadataSetNode(PossibleMetadata)) |
| 298 |
else |
| 299 |
if (PossibleMetadata is TAbstractMetadataNode) and |
| 300 |
(TAbstractMetadataNode(PossibleMetadata).NameField = Key) then |
| 301 |
Result := TAbstractMetadataNode(PossibleMetadata); |
| 302 |
end; |
| 303 |
end; |
| 304 |
|
| 305 |
{ Search a TMetadataSetNode instance (never nil). } |
| 306 |
function FindInSet(const MetadataSet: TMetadataSetNode): TAbstractMetadataNode; |
| 307 |
var |
| 308 |
I: Integer; |
| 309 |
begin |
| 310 |
Result := nil; |
| 311 |
for I := 0 to MetadataSet.FdValue.Count - 1 do |
| 312 |
begin |
| 313 |
Result := FindInSetOrSingle(MetadataSet.FdValue[I]); |
| 314 |
if Result <> nil then Exit; |
| 315 |
end; |
| 316 |
end; |
| 317 |
|
| 318 |
begin |
| 319 |
{$ifdef CASTLE_SLIM_NODES} |
| 320 |
Result := nil; |
| 321 |
{$else} |
| 322 |
Result := FindInSetOrSingle(FdMetadata.Value); |
| 323 |
{$endif} |
| 324 |
end; |
| 325 |
|
| 326 |
procedure TAbstractNode.InternalInsertMetadata(const M: TAbstractMetadataNode); |
| 327 |
var |
| 328 |
MSet: TMetadataSetNode; |
| 329 |
begin |
| 330 |
{$ifndef CASTLE_SLIM_NODES} |
| 331 |
if FdMetadata.Value is TMetadataSetNode then |
| 332 |
begin |
| 333 |
MSet := TMetadataSetNode(FdMetadata.Value); |
| 334 |
end else |
| 335 |
begin |
| 336 |
MSet := TMetadataSetNode.Create; |
| 337 |
MSet.NameField := 'ContainerForAllMetadataValues'; // set NameField because it is required by X3D |
| 338 |
// preserve previous "metadata" value, by adding it to set |
| 339 |
if FdMetadata.Value <> nil then |
| 340 |
MSet.FdValue.Add(FdMetadata.Value); |
| 341 |
FdMetadata.Value := MSet; |
| 342 |
end; |
| 343 |
MSet.FdValue.Add(M); |
| 344 |
{$endif} |
| 345 |
end; |
| 346 |
|
| 347 |
function TAbstractNode.GetMetadataBooleanArray(const Key: String; const Index: Cardinal): Boolean; |
| 348 |
var |
| 349 |
M: TAbstractMetadataNode; |
| 350 |
MB: TMetadataBooleanNode; |
| 351 |
begin |
| 352 |
M := FindMetadata(Key); |
| 353 |
if M = nil then Exit(false); |
| 354 |
if not (M is TMetadataBooleanNode) then Exit(false); |
| 355 |
|
| 356 |
MB := TMetadataBooleanNode(M); |
| 357 |
if Index < MB.FdValue.Count then |
| 358 |
Result := MB.FdValue.Items[Index] |
| 359 |
else |
| 360 |
Result := false; |
| 361 |
end; |
| 362 |
|
| 363 |
procedure TAbstractNode.SetMetadataBooleanArray(const Key: String; const Index: Cardinal; const Value: Boolean); |
| 364 |
var |
| 365 |
M: TAbstractMetadataNode; |
| 366 |
MB: TMetadataBooleanNode; |
| 367 |
begin |
| 368 |
M := FindMetadata(Key); |
| 369 |
if M = nil then |
| 370 |
begin |
| 371 |
MB := TMetadataBooleanNode.Create; |
| 372 |
MB.NameField := Key; |
| 373 |
InternalInsertMetadata(MB); |
| 374 |
end else |
| 375 |
begin |
| 376 |
if not (M is TMetadataBooleanNode) then |
| 377 |
raise Exception.CreateFmt('Cannot add Boolean metadata with key "%s", there is already metadata of type %s', [ |
| 378 |
Key, |
| 379 |
M.X3DType |
| 380 |
]); |
| 381 |
MB := TMetadataBooleanNode(M); |
| 382 |
end; |
| 383 |
|
| 384 |
MB.FdValue.Count := Max(MB.FdValue.Count, Index + 1); |
| 385 |
MB.FdValue.Items[Index] := Value; |
| 386 |
end; |
| 387 |
|
| 388 |
function TAbstractNode.GetMetadataStringArray(const Key: String; const Index: Cardinal): String; |
| 389 |
var |
| 390 |
M: TAbstractMetadataNode; |
| 391 |
MB: TMetadataStringNode; |
| 392 |
begin |
| 393 |
M := FindMetadata(Key); |
| 394 |
if M = nil then Exit(''); |
| 395 |
if not (M is TMetadataStringNode) then Exit(''); |
| 396 |
|
| 397 |
MB := TMetadataStringNode(M); |
| 398 |
if Index < MB.FdValue.Count then |
| 399 |
Result := MB.FdValue.Items[Index] |
| 400 |
else |
| 401 |
Result := ''; |
| 402 |
end; |
| 403 |
|
| 404 |
procedure TAbstractNode.SetMetadataStringArray(const Key: String; const Index: Cardinal; const Value: String); |
| 405 |
var |
| 406 |
M: TAbstractMetadataNode; |
| 407 |
MB: TMetadataStringNode; |
| 408 |
begin |
| 409 |
M := FindMetadata(Key); |
| 410 |
if M = nil then |
| 411 |
begin |
| 412 |
MB := TMetadataStringNode.Create; |
| 413 |
MB.NameField := Key; |
| 414 |
InternalInsertMetadata(MB); |
| 415 |
end else |
| 416 |
begin |
| 417 |
if not (M is TMetadataStringNode) then |
| 418 |
raise Exception.CreateFmt('Cannot add String metadata with key "%s", there is already metadata of type %s', [ |
| 419 |
Key, |
| 420 |
M.X3DType |
| 421 |
]); |
| 422 |
MB := TMetadataStringNode(M); |
| 423 |
end; |
| 424 |
|
| 425 |
MB.FdValue.Count := Max(MB.FdValue.Count, Index + 1); |
| 426 |
MB.FdValue.Items[Index] := Value; |
| 427 |
end; |
| 428 |
|
| 429 |
function TAbstractNode.GetMetadataIntegerArray(const Key: String; const Index: Cardinal): Integer; |
| 430 |
var |
| 431 |
M: TAbstractMetadataNode; |
| 432 |
MB: TMetadataIntegerNode; |
| 433 |
begin |
| 434 |
M := FindMetadata(Key); |
| 435 |
if M = nil then Exit(0); |
| 436 |
if not (M is TMetadataIntegerNode) then Exit(0); |
| 437 |
|
| 438 |
MB := TMetadataIntegerNode(M); |
| 439 |
if Index < MB.FdValue.Count then |
| 440 |
Result := MB.FdValue.Items[Index] |
| 441 |
else |
| 442 |
Result := 0; |
| 443 |
end; |
| 444 |
|
| 445 |
procedure TAbstractNode.SetMetadataIntegerArray(const Key: String; const Index: Cardinal; const Value: Integer); |
| 446 |
var |
| 447 |
M: TAbstractMetadataNode; |
| 448 |
MB: TMetadataIntegerNode; |
| 449 |
begin |
| 450 |
M := FindMetadata(Key); |
| 451 |
if M = nil then |
| 452 |
begin |
| 453 |
MB := TMetadataIntegerNode.Create; |
| 454 |
MB.NameField := Key; |
| 455 |
InternalInsertMetadata(MB); |
| 456 |
end else |
| 457 |
begin |
| 458 |
if not (M is TMetadataIntegerNode) then |
| 459 |
raise Exception.CreateFmt('Cannot add Integer metadata with key "%s", there is already metadata of type %s', [ |
| 460 |
Key, |
| 461 |
M.X3DType |
| 462 |
]); |
| 463 |
MB := TMetadataIntegerNode(M); |
| 464 |
end; |
| 465 |
|
| 466 |
MB.FdValue.Count := Max(MB.FdValue.Count, Index + 1); |
| 467 |
MB.FdValue.Items[Index] := Value; |
| 468 |
end; |
| 469 |
|
| 470 |
function TAbstractNode.GetMetadataDoubleArray(const Key: String; const Index: Cardinal): Double; |
| 471 |
var |
| 472 |
M: TAbstractMetadataNode; |
| 473 |
MF: TMetadataFloatNode; |
| 474 |
MD: TMetadataDoubleNode; |
| 475 |
begin |
| 476 |
Result := 0.0; |
| 477 |
|
| 478 |
M := FindMetadata(Key); |
| 479 |
if M = nil then Exit; |
| 480 |
|
| 481 |
if M is TMetadataDoubleNode then |
| 482 |
begin |
| 483 |
MD := TMetadataDoubleNode(M); |
| 484 |
if Index < MD.FdValue.Count then |
| 485 |
Result := MD.FdValue.Items[Index]; |
| 486 |
end else |
| 487 |
if M is TMetadataFloatNode then |
| 488 |
begin |
| 489 |
MF := TMetadataFloatNode(M); |
| 490 |
if Index < MF.FdValue.Count then |
| 491 |
Result := MF.FdValue.Items[Index] |
| 492 |
end; |
| 493 |
end; |
| 494 |
|
| 495 |
procedure TAbstractNode.SetMetadataDoubleArray(const Key: String; const Index: Cardinal; const Value: Double); |
| 496 |
var |
| 497 |
M: TAbstractMetadataNode; |
| 498 |
MF: TMetadataFloatNode; |
| 499 |
MD: TMetadataDoubleNode; |
| 500 |
begin |
| 501 |
M := FindMetadata(Key); |
| 502 |
if M = nil then |
| 503 |
begin |
| 504 |
M := TMetadataDoubleNode.Create; |
| 505 |
M.NameField := Key; |
| 506 |
InternalInsertMetadata(M); |
| 507 |
end; |
| 508 |
|
| 509 |
if M is TMetadataFloatNode then |
| 510 |
begin |
| 511 |
MF := TMetadataFloatNode(M); |
| 512 |
MF.FdValue.Count := Max(MF.FdValue.Count, Index + 1); |
| 513 |
MF.FdValue.Items[Index] := Value; |
| 514 |
WritelnWarning('Storing double-precision metadata in single-precision MetadataFloat, some precision may be lost'); |
| 515 |
end else |
| 516 |
if M is TMetadataDoubleNode then |
| 517 |
begin |
| 518 |
MD := TMetadataDoubleNode(M); |
| 519 |
MD.FdValue.Count := Max(MD.FdValue.Count, Index + 1); |
| 520 |
MD.FdValue.Items[Index] := Value; |
| 521 |
end else |
| 522 |
raise Exception.CreateFmt('Cannot add Double metadata with key "%s", there is already metadata of type %s', [ |
| 523 |
Key, |
| 524 |
M.X3DType |
| 525 |
]); |
| 526 |
end; |
| 527 |
|
| 528 |
function TAbstractNode.GetMetadataBoolean(const Key: String): Boolean; |
| 529 |
begin |
| 530 |
Result := MetadataBooleanArray[Key, 0]; |
| 531 |
end; |
| 532 |
|
| 533 |
procedure TAbstractNode.SetMetadataBoolean(const Key: String; const Value: Boolean); |
| 534 |
begin |
| 535 |
MetadataBooleanArray[Key, 0] := Value; |
| 536 |
end; |
| 537 |
|
| 538 |
function TAbstractNode.GetMetadataString(const Key: String): String; |
| 539 |
begin |
| 540 |
Result := MetadataStringArray[Key, 0]; |
| 541 |
end; |
| 542 |
|
| 543 |
procedure TAbstractNode.SetMetadataString(const Key: String; const Value: String); |
| 544 |
begin |
| 545 |
MetadataStringArray[Key, 0] := Value; |
| 546 |
end; |
| 547 |
|
| 548 |
function TAbstractNode.GetMetadataInteger(const Key: String): Integer; |
| 549 |
begin |
| 550 |
Result := MetadataIntegerArray[Key, 0]; |
| 551 |
end; |
| 552 |
|
| 553 |
procedure TAbstractNode.SetMetadataInteger(const Key: String; const Value: Integer); |
| 554 |
begin |
| 555 |
MetadataIntegerArray[Key, 0] := Value; |
| 556 |
end; |
| 557 |
|
| 558 |
function TAbstractNode.GetMetadataDouble(const Key: String): Double; |
| 559 |
begin |
| 560 |
Result := MetadataDoubleArray[Key, 0]; |
| 561 |
end; |
| 562 |
|
| 563 |
procedure TAbstractNode.SetMetadataDouble(const Key: String; const Value: Double); |
| 564 |
begin |
| 565 |
MetadataDoubleArray[Key, 0] := Value; |
| 566 |
end; |
| 567 |
|
| 568 |
function TAbstractMetadataNode.DefaultContainerFieldInContext( |
| 569 |
const Version: TX3DVersion; const ParentNode: TX3DNode): String; |
| 570 |
begin |
| 571 |
{ X3D 4.0 changed the containerField for MetadataXxx nodes, |
| 572 |
see https://github.com/castle-engine/castle-model-viewer/issues/78 . } |
| 573 |
if Version.Major >= 4 then |
| 574 |
Result := 'value' |
| 575 |
else |
| 576 |
Result := 'metadata'; |
| 577 |
end; |
| 578 |
|
| 579 |
{ TAbstractBindableNode ------------------------------------------------------ } |
| 580 |
|
| 581 |
constructor TAbstractBindableNode.Create(const AX3DName, ABaseUrl: String); |
| 582 |
begin |
| 583 |
inherited; |
| 584 |
EventIsBound.AddNotification({$ifdef FPC}@{$endif} EventIsBoundReceive); |
| 585 |
FTransform := TMatrix4.Identity; |
| 586 |
FTransformScale := 1; |
| 587 |
end; |
| 588 |
|
| 589 |
procedure TAbstractBindableNode.BeforeTraverse( |
| 590 |
StateStack: TX3DGraphTraverseStateStack); |
| 591 |
begin |
| 592 |
inherited; |
| 593 |
|
| 594 |
{ We need to compare old and new matrix to determine |
| 595 |
FLastBeforeTraverseChangedTransform. |
| 596 |
Even though this may be wasteful, but FLastBeforeTraverseChangedTransform |
| 597 |
is very desperately needed: otherwise every time |
| 598 |
TTransformChangeHelper.TransformChangeTraverse processes |
| 599 |
the bound viewpoint node (and it is processed in every frame, |
| 600 |
if model has some animation and some Viewpoint node and OptimizeExtensiveTransformations), |
| 601 |
and without FLastBeforeTraverseChangedTransform, |
| 602 |
we'd have to report each frame "possibly viewpoint vectors changed". |
| 603 |
|
| 604 |
Testcase: cat in castle-model-viewer-mobile, |
| 605 |
if FLastBeforeTraverseChangedTransform would be missing (or always true) |
| 606 |
then |
| 607 |
- it would be impossible to rotate the cat model, |
| 608 |
- it would be impossible to zoom it, |
| 609 |
- and every viewpoint switch would be instant. |
| 610 |
|
| 611 |
For this to happen, we need: |
| 612 |
- some animation in the model playing that changes transformation each frame |
| 613 |
(e.g. skeleton animation of the cat) |
| 614 |
- OptimizedExtensiveTransformations = true (to process all nodes each time |
| 615 |
some transformation changes) |
| 616 |
- AutoCamera = true used (otherwise DoBoundViewpointVectorsChanged doesn't |
| 617 |
really cause anything) |
| 618 |
} |
| 619 |
|
| 620 |
FLastBeforeTraverseChangedTransform := |
| 621 |
(not TMatrix4.PerfectlyEquals(FTransform, StateStack.Top.Transformation.Transform)) or |
| 622 |
(FTransformScale <> StateStack.Top.Transformation.Scale); |
| 623 |
if FLastBeforeTraverseChangedTransform then |
| 624 |
begin |
| 625 |
FTransform := StateStack.Top.Transformation.Transform; |
| 626 |
FTransformScale := StateStack.Top.Transformation.Scale; |
| 627 |
end; |
| 628 |
end; |
| 629 |
|
| 630 |
procedure TAbstractBindableNode.EventIsBoundReceive( |
| 631 |
const Event: TX3DEvent; const AValue: TX3DField; const Time: TX3DTime); |
| 632 |
begin |
| 633 |
FBound := (AValue as TSFBool).Value; |
| 634 |
end; |
| 635 |
|
| 636 |
procedure TAbstractBindableNode.SetBound(const Value: Boolean); |
| 637 |
begin |
| 638 |
EventSet_bind.Send(Value); |
| 639 |
end; |
| 640 |
|
| 641 |
{ registration --------------------------------------------------------------- } |
| 642 |
|
| 643 |
procedure RegisterCoreNodes; |
| 644 |
begin |
| 645 |
NodesManager.RegisterNodeClasses([ |
| 646 |
TMetadataBooleanNode, |
| 647 |
TMetadataDoubleNode, |
| 648 |
TMetadataFloatNode, |
| 649 |
TMetadataIntegerNode, |
| 650 |
TMetadataSetNode, |
| 651 |
TMetadataStringNode, |
| 652 |
TWorldInfoNode |
| 653 |
]); |
| 654 |
end; |
| 655 |
|
| 656 |
{$endif read_implementation} |