stride-community-toolkit icon indicating copy to clipboard operation
stride-community-toolkit copied to clipboard

Content Loading at runtime

Open Doprez opened this issue 1 year ago • 4 comments

Currently loading content in Stride at runtime without the windows specific libraries is a pain and not well documented. This is a bit of a personal document I wanted to bring up publicly in case others want to pitch in findings or help out in research and documentation.

Loadable Content

Images or Textures

This one is actually easy and only requires a couple of lines already.

// supported files should be dds, bmp, jpg, png, gif, tiff, wmp, tga
var file = File.OpenRead("test.png");
// graphics device can come from the game class or the ScriptComponent class.
var texture = Texture.Load(GraphicsDevice, file); 

Image is also the same but you most likely want the above.

Models and Skeletons

This one is annoyingly close to being easy but fails when trying to actually add the ModelComponent to a scene.

  1. Add Stride.Importer.3D
  2. create a new MeshConverter class and load a file.
var meshConverter = new MeshConverter(new LoggerResult());
var model = _meshConverter.Convert(_modelPath + "FPS_Arms_GameReadyVer.fbx", _modelPath, false);
  1. Calculate the bounding boxes.
  2. Add the model to a component and to an entity in the scene.
var modelComponent = new ModelComponent()
{
	Model = model,
	BoundingBox = model.BoundingBox,
	BoundingSphere = model.BoundingSphere,
};
var entity = new Entity()
{
	modelComponent
};
entity.Scene = Entity.Scene;
  1. witness the crash in the ModelComponent class :(

Audio

Honestly no clue where to even start here. It seems like the required classes are in the OpenAL wrapper but hidden as internal? I may just be missing something there though.

I did however do some playing around with a custom implementation of audio with the added bonus of Steams audio engine. It's very unfinished though. https://github.com/Doprez/Doprez.Stride.SteamAudio

Shaders

Again not really sure where to start here but it looks like it may be possible using this for direct3d and this for opengl/vulkan

Although Im not sure if those source files expect SDSL or HLSL. Based on my brief research it looks like it should be for hlsls and GLSL but it's a bit of a rabbit hole.

Tasks

  • [ ] Model import extensions (Im feel like I'm annoyingly close to something usable but debugging this is a pain so far.)
  • [ ] Skeleton import extensions
  • [ ] Audio import extensions
  • [ ] Texture import extensions (Its already pretty simple though so maybe not?)
  • [ ] Shader import extensions (May be worth waiting on the shader rewrite?)
  • [ ] Video import extensions (https://github.com/stride3d/stride-community-toolkit/issues/200)

Doprez avatar Nov 12 '24 00:11 Doprez

Coming back to this with some extra info now with a slightly better understanding of the asset compilation system thanks to Eideren. We may be able to create some extensions that reuse the asset compiler code from the stride game engine. This would follow a more similar philosophy to Monogames Asset manager.

Audio: https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Media/SoundAssetCompiler.cs#L27

Video: https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Media/VideoAssetCompiler.cs

SpriteSheets: https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Sprite/SpriteSheetAssetCompiler.cs

RenderTextures: https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Textures/RenderTextureAssetCompiler.cs

Navigation: https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Navigation/NavigationMeshAssetCompiler.cs

Maybe Shaders??: https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Effect/EffectLogAssetCompiler.cs https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Effect/EffectShaderAssetCompiler.cs

Materials: https://github.com/stride3d/stride/blob/7b77b009cac8056cdcceb25cc7c8e0cfbe9db1bd/sources/engine/Stride.Assets/Materials/MaterialAssetCompiler.cs

Note: Some of these are based on windows libraries for compilation so careful with that.

Doprez avatar Mar 12 '25 01:03 Doprez

Some reference https://github.com/VaclavElias/Stride-Code-only-Loading-Content

VaclavElias avatar Mar 12 '25 09:03 VaclavElias

I was doing some Stride code base exploring and the fastest way to get audio working is like the example above, so I did the same for this toolkit's example: https://github.com/stride3d/stride-community-toolkit/tree/main/examples/code-only/Example_CubicleCalamity

VaclavElias avatar Apr 19 '25 23:04 VaclavElias

Saving a texture to be reused at runtime:

    /// <summary>
    /// Saves a texture to the content manager's VFS.
    /// <para>
    /// supported files are dds, bmp, jpg, png, gif, tiff, wmp, tga
    /// </para>
    /// </summary>
    /// <param name="contentManager">content manager that will hold the reference.</param>
    /// <param name="fileName">full path to the file on disk.</param>
    /// <param name="dbPathName">full path the asset will sit in the database.</param>
    public static void AddImage(this IContentManager contentManager, string fileName, string dbPathName)
    {
        using (var file = File.OpenRead(fileName))
        {
            // graphics device can come from the game class or the ScriptComponent class.
            var texture = Image.Load(file);
            contentManager.Save(dbPathName, texture.ToSerializableVersion(), typeof(Texture));
        }
    }

You have to convert to an image so that the CPU has the valid data to be serialized then get the Texture info with the built in helper class to store the serialized info.

Doprez avatar Jul 22 '25 22:07 Doprez