Asset loaders are what drive content in Balder. All files, or assets as they are referred to in Balder, requires a asset loader to exist and recognize the file. By default, Balder has a set of asset loaders for handling the most common formats for images and 3D models, more formats will be included in the out-of-the-box experience as the project evolves. These asset loaders can be found in the Balder.Core project under the namespace AssetLoaders. AssetLoaders are recognized by their file extension support. All files have extensions, and the extension is what drives the engine to decide which loader to chose.

Implementing your own AssetLoader is fairly simple, all you need to do is to create a class in a class library project (Assembly/DLL project) add a reference to the Balder.Core.dll file and inherit your class from something called AssetLoader. The AssetLoader class takes a generic parameter that describes the type of asset you want to load, e.g. Geometry if you want to load 3D data from a fileformat such as DXF, ASE or similar.

using Balder.Core;

namespace MyAssetLoaders
{
    public class MyLoader : AssetLoader<Geometry>
    {
        
    }
}

In addition you need a constructor. The base class' (AssetLoader) constructor has a couple of dependencies on services found in the engine and needs to have these, all you need to do is have the same dependencies on your constructor and forward the input to the base. Balder will make sure to inject the dependencies into your constructor.

using Balder.Core;
using Balder.Core.Services;

namespace MyAssetLoaders
{
    public class MyLoader : AssetLoader<Geometry>
    {
        public MyLoader(IFileLoader fileLoader, IContentManager contentManager)
            : base(fileLoader, contentManager)
        {
        }
    }
}

Then you need to specify what kind of file extensions your loader supports by overriding a virtual property called FileExtensions:

namespace MyAssetLoaders
{
    public class MyLoader : AssetLoader<Geometry>
    {
        public MyLoader(IFileLoader fileLoader, IContentManager contentManager)
            : base(fileLoader, contentManager)
        {
        }

        public override string[] FileExtensions { get { return new[] {"myfile"}; } } 
    }
}

The "myfile" is the extension. If you want to support more than one extension, just add it into the array your create.
To be able to load the data from the file, you need to override a virtual method called Load(). Within the Load() method you can use the FileLoader to get a stream. The assetname that is passed into the Load() method is something one passes along to the FileLoaders GetStream() method.

    public override Geometry[] Load(string assetName)
    {
        using( var stream = FileLoader.GetStream(assetName) )
        {
            // Put your loading code here
        }
    }

The load method must return the asset type specified in the generic parameter when inheriting from the AssetLoader class, in order to create assets one needs to use the ContentManager which is a property inherited from the AssetLoader. The ContentManager has a generic method called CreateAssetPart<>() that takes the asset type to create as a parameter.

    public override Geometry[] Load(string assetName)
    {
        using( var stream = FileLoader.GetStream(assetName) )
        {
            var geometry = ContentManager.CreateAssetPart<Geometry>();
            // Put your loading code here
            return new [] { geometry };
        }
    }

For details on how to handle working with geometries for loading, have a look at the creating geometry data section.

The next thing one needs to do, is to actually make Balder aware of the asset loader. (TODO: Changes need to be done in current codebase to be able to register AssetLoaders)

Last edited Oct 15, 2009 at 6:53 PM by adept, version 9

Comments

No comments yet.