How to obtain the size of texture in TCastleScene

How to obtain the height and width of textures in TCastleScene

Note: I give an answer below, but it will be helpful if you can describe in more detail what do you want to achieve in the end. I.e. why do you want to know texture size, how it’s loaded in TCastleScene. Then we can maybe advise better :), there are maybe more direct ways of doing what you need.

The answer: the Scene has RootNode which contains a graph of nodes, see Scene graph (X3D) | Castle Game Engine . You can search it for TImageTextureNode nodes, e.g. using

Scene.RootNode.EnumerateNodes(TImageTextureNode, @MyProc, false);

See e.g. how examples/animations/animate_bones_by_code/ is using EnumerateNodes. It’s using EnumerateNodes for different purpose, but the idea remains. See castle-engine/examples/animations/animate_bones_by_code/code/gameviewmain.pas at master · castle-engine/castle-engine · GitHub .

In MyProc, you get a node, Node: TX3DNode. You can cast it to TImageTextureNode and access properties, like this:

procedure TViewMain.MyProc(Node: TX3DNode);
  TextureNode: TImageTextureNode;
  TextureNode := Node as TImageTextureNode; // safe to cast, as EnumerateNodes only chooses TImageTextureNode
  // Possible URLs from which to load texture are in TextureNode.FdUrl.Items .
  // The URL which was actually used to load the texture, if any, is in TextureNode.TextureUsedFullUrl .
  TextureNode.IsTextureLoaded := true; // load the texture now, if not loaded yet
  if TextureNode.IsTextureImage then
    WritelnLog('loaded from %s', [TextureNode.TextureUsedFullUrl]);
    WritelnLog('loaded size %d %d', [TextureNode.TextureImage.Width, TextureNode.TextureImage.Height]);
  end else
    WritelnLog('not loaded, no valid URLs');

^ I did not actually test the above code, but hopefully it will get you started :slight_smile:

The API docs of TImageTextureNode and ancestor classes:

1 Like