X3D nodes are the scene graph of our engine. Once you load a 3D or 2D model into TCastleScene
(whether it was in X3D format or anything else, like glTF), you have a graph of X3D nodes. The root of this graph is available as TCastleSceneCore.RootNode
.
For example, consider an X3D node
Box.
This node in X3D has fields:
In Pascal, this node corresponds to the class TBoxNode
, with properties
4.1. X3D node instance lifetime in Pascal
The X3D nodes have a reference-counting mechanism. The node (TX3DNode
or any descendant) is freed when the reference count of it changes from non-zero to zero.
So in the usual case, if you set the node as a value of some property of the other node, then you no longer need to think about releasing this node — it will be released along with the parent. For example:
var
Geometry: TBoxNode;
Shape: TShapeNode;
Root: TX3DRootNode;
begin
Geometry := TBoxNode.Create;
Shape := TShapeNode.Create;
Shape.Geometry := Geometry;
Root := TX3DRootNode.Create;
Root.AddChildren(Shape);
So we can continue the example above:
Scene := TCastleScene.Create(Application);
Scene.Load(Root, true);
And Scene
is a regular Pascal TComponent
, with usual component ownership. In the above example, it is owned by the Application
singleton. In larger application, when using views, it will often be owned by TCastleView.FreeAtStop
. And you can always free the scene explicitly too, like FreeAndNil(Scene);
.
-
If a node is not referenced from anywhere, you are responsible for freeing it manually as usual. For example, if we would not load Root
into Scene
(if we remove the line Scene.Load(Root, true)
from above example) then at some point you should call FreeAndNil(Root);
.
-
If for some reason you do not want the node to be automatically freed, you can use TX3DNode.KeepExistingBegin
and TX3DNode.KeepExistingEnd
.