-
Notifications
You must be signed in to change notification settings - Fork 6
Writing a mesh loader
MaxB3D provides an abstract loading system that allows for later expansion through a "plug 'n play" loader interface. Every included mesh loader utilizes this system.
When a mesh is created, the url is passed to TMeshLoader.Load. This a type function that will begin calling every mesh loader until one signals that it was successful by returning 'True.' If no mesh is loaded, then a null value is returned.
To begin, create a new type extending TMeshLoader and include a 'Run' method. After the type declaration, create a new instance of the type.
Type TMeshLoaderTEST Extends TMeshLoader
Method Run(mesh:TMesh,stream:TStream,url:Object)
End Method
End Type
New TMeshLoaderTEST
The 'Run' method is where all the loader's logic must occur. 'Run' provides a mesh object for the loader to modify as well as (more commonly) load data into TSurface objects. Under no circumstances should a loader free the mesh.
A stream and url object are also passed. The url object is what was passed to LoadMesh/TWorld.AddMesh and the stream is an attempt to load said url. Therefore, the stream may or may not be null. A loader is free to move around the stream, and read data from it, but it should never write/close it. This object is reused with each loader and is reset before being passed to the next.
When a new instance of the type is created, it is registered with MaxB3D for later use. Therefore, only one instance is ever needed.
MaxB3D will call each instances' 'Run' method until a loader returns true.
Type TMeshLoaderTEST Extends TMeshLoader
Method Run(mesh:TMesh,stream:TStream,url:Object)
If url <> "thisisatestmeshloader" Return False
' ...
' Mesh loading code here
' ...`
Return True ' The string matched
End Method
End Type
New TMeshLoaderTEST
If LoadMesh("thisisatestmeshloader") is called, then a valid (but empty) mesh will be returned.
For examples, check:
- MaxB3D.Primitives
- MaxB3D.B3DLoader
- MaxB3D.A3DSLoader
- MaxB3D.HeightmapLoader
Ideally, each loader should be contained to its own module. This way, the user can load only the required loaders, reducing executable size.
It is best practice to avoid importing MaxB3D.Functions as some users will choose to move to the OOP interface and not want the bulk brought along with that module.
As a general rule, MaxB3D.Core will be the only MaxB3D module needed.