Unit X3DLoad
Description
Loading and saving model formats. All model formats are loaded into a graph of X3D nodes. This allows to use nodes throughout the engine, for all rendering and processing.
To add a new model format handled throughout the engine, register it using RegisterModelFormat. Our LoadNode, SaveNode, TCastleScene, LoadScene_FileFilters and other API will automatically account for the new format.
Uses
- SysUtils
- Classes
- CastleUtils
- CastleVectors
- X3DNodes
- CastleStringUtils
Overview
Classes, Interfaces, Objects and Records
Name | Description |
---|---|
Class TModelFormat |
Information about a model format, used with RegisterModelFormat. |
Functions and Procedures
function LoadNode(const Url: String): TX3DRootNode; overload; |
function LoadNode(const Stream: TStream; BaseUrl: String; const MimeType: String): TX3DRootNode; overload; |
procedure SaveNode(const Node: TX3DRootNode; const Url: String; const Generator: String = ''; const Source: String = ''); overload; |
procedure SaveNode(const Node: TX3DRootNode; const Stream: TStream; const MimeType: String; const Generator: String = ''; const Source: String = ''); overload; |
function LoadScene_FileFilters: String; |
function SaveNode_FileFilters: String; |
procedure RegisterModelFormat(const ModelFormat: TModelFormat); |
Types
TModelLoadEvent = function (const Stream: TStream; const BaseUrl: String): TX3DRootNode; |
TModelSaveEvent = procedure (const Node: TX3DRootNode; const Stream: TStream; const Generator: String; const Source: String); |
Constants
DefaultBakedAnimationSmoothness = 1; |
Variables
BakedAnimationSmoothness: Single = DefaultBakedAnimationSmoothness; |
Description
Functions and Procedures
function LoadNode(const Url: String): TX3DRootNode; overload; |
Load a scene as X3D node. Guesses scene format based on the URL extension. We load a large number of formats, see https://castle-engine.io/creating_data_model_formats.php . All the scene formats are loaded as a graph of X3D nodes. URL is downloaded using the CastleDownload unit, so it supports files, http resources and more. See https://castle-engine.io/manual_network.php about supported URL schemes. If you all you care about is loading normal files, then just pass a normal filename (absolute or relative to the current directory) as the URL parameter. To actually display, animate and do many other things with the loaded model, you usually want to load it to TCastleScene, using the TCastleSceneCore.Load method. Like this: var RootNode: TX3DRootNode; Scene: TCastleScene; begin RootNode := LoadNode('my_model.x3d'); Scene := TCastleScene.Create(Application); Scene.Load(RootNode, true); // The 2nd parameter of Load says that Scene owns RootNode end;
Actually, in most cases you don't need to use LoadNode (and this unit, X3DLoad) at all, and you can simply load from an URL: var Scene: TCastleScene; begin Scene := TCastleScene.Create(Application); Scene.Load('my_model.x3d'); // you can access Scene.RootNode after loading, if needed end;
Note that usually you want to load models from the game data, so you would actually use |
function LoadNode(const Stream: TStream; BaseUrl: String; const MimeType: String): TX3DRootNode; overload; |
Load a scene as X3D node from TStream. Takes a TStream instance with contents to load, and MimeType parameter determines the data content (e.g. X3D or glTF or Spine model). In most cases, instead of this, you should use a simpler The BaseUrl parameter here is used to resolve relative URLs inside the model, e.g. references to textures from various 3D and 2D model formats are resolved relative to this base URL. It generally should be a URL from which you downloaded the model. If you don't know it, you can use the current directory (which can be conveniently expressed by BaseUrl = '', which is a relative empty URL implying current working dir). We do not use BaseUrl to determine file contents (e.g. we don't look at filename extension here) in this routine. Note that some formats (like glTF) require a stream with free seeking capabilities. Call Download with soForceMemoryStream, or wrap the stream in TMemoryStream manually, if unsure. The overloaded Note that this routine assumes that the stream is not gzip-compressed. The overloaded |
procedure SaveNode(const Node: TX3DRootNode; const Url: String; const Generator: String = ''; const Source: String = ''); overload; |
Save model to a file. See SaveNode_FileFilters for all model formats that we can save. If you provide explicit URL, it determines the output format. If you provide a Stream and MimeType, then MimeType determines the output format. E.g. use MimeType = 'model/x3d+vrml' to X3D classic encoding, or MimeType = 'model/x3d+xml' to X3D XML encoding.
Parameters
|
procedure SaveNode(const Node: TX3DRootNode; const Stream: TStream; const MimeType: String; const Generator: String = ''; const Source: String = ''); overload; |
This item has no description. |
function LoadScene_FileFilters: String; |
File filters for files loaded by TCastleSceneCore.Load and LoadNode. Suitable for TFileFilterList.AddFiltersFromString and TCastleWindow.FileDialog. |
function SaveNode_FileFilters: String; |
File filters for files saved by SaveNode. Suitable for TFileFilterList.AddFiltersFromString and TCastleWindow.FileDialog. |
procedure RegisterModelFormat(const ModelFormat: TModelFormat); |
Register given model format, to enable loading and/or saving it using LoadNode, SaveNode and all routines on top of them, like TCastleSceneCore.Load. The ModelFormat instance given here becomes owned by the internal list in this unit. Do not free it, do not modify it after registering it. Here's an example how to register a new model format, USD. This example assumes you want to register the new model format in the function LoadUSD(const Stream: TStream; const BaseUrl: String): TX3DRootNode; begin Result := TX3DRootNode.Create; // TODO: Load USD here end; var ModelFormat: TModelFormat; initialization ModelFormat := TModelFormat.Create; ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadUSD; ModelFormat.MimeTypes.Add('model/vnd.usda'); ModelFormat.MimeTypes.Add('model/vnd.usdz+zip'); ModelFormat.FileFilterName := 'Universal Scene Description'; ModelFormat.Extensions.Add('.usd'); ModelFormat.Extensions.Add('.usda'); ModelFormat.Extensions.Add('.usdc'); ModelFormat.Extensions.Add('.usdz'); RegisterModelFormat(ModelFormat); UriMimeExtensions['.usd'] := 'model/vnd.usda'; UriMimeExtensions['.usda'] := 'model/vnd.usda'; UriMimeExtensions['.usdc'] := 'model/vnd.usda'; UriMimeExtensions['.usdz'] := 'model/vnd.usdz+zip'; end. |
Types
TModelLoadEvent = function (const Stream: TStream; const BaseUrl: String): TX3DRootNode; |
This item has no description. |
TModelSaveEvent = procedure (const Node: TX3DRootNode; const Stream: TStream; const Generator: String; const Source: String); |
This item has no description. |
Constants
DefaultBakedAnimationSmoothness = 1; |
This item has no description. |
Variables
BakedAnimationSmoothness: Single = DefaultBakedAnimationSmoothness; |
A smoothness value for "baked" animations loaded from castle-anim-frames files. This is multiplied by the scenes_per_time value recorded in castle-anim-frames file (30 by default), and determines the number of extra frames we add to the baked animation (between key frames). |
Generated by PasDoc 0.16.0-snapshot.