Loading resources

286 views
Skip to first unread message

brunocruz

unread,
Jun 2, 2021, 11:26:11 AM6/2/21
to Bonsai Users
Hi,

I am looking for a way to load small clips in bonsai and present, randomly, one of them in every trial. One way to do this is to manually load multiple textures into the "TextureResources" node and number as their name. Later, when presenting the video with DrawVideo, simply externalize the property and randomly generate a number and convert it to a string.

I am wondering if there is a way that makes this process scalable if I need to use several dozens of videos. Two ideas:

1. Have the textures' names and paths in a CSV and, somehow, load them automatically into the TextureResources ?
2. Have a way to index the existing textures such that I can just present one of them randomly. Which would at least save me the trouble of naming each of them :P

Thanks,
Bruno


Gonçalo Lopes

unread,
Jun 13, 2021, 7:48:48 PM6/13/21
to brunocruz, Bonsai Users
Hey Bruno,

Both solutions are a good idea and are possible to do using Bonsai.Shaders in different ways.

1. To load textures dynamically, you can use LoadImage (Shaders) to load a filename into a Texture. The image is loaded to the GPU immediately and can then be attached as a shader texture using BindTexture. So for example:

image.png

This would load a series of filenames stored in the CSV file as textures, pack them all into an array and stored in a Subject to be accessed later. When you need it you can get the array, Index into it and pass it to BindTexture to attach it to a specific shader. This has the advantage that no specific names need to be used anywhere, and only texture handles are passed around.

2. There is another option which is already built into TextureResources, which is to use the ImageSequence resource:

image.png

This resource type loads either a video or an image sequence into an array of textures. To specify loading from a folder of images, a specific syntax needs to be used to indicate the number of digits in the numbering scheme. For example, the name frame-%03d.png looks for images named like frame-000.png, frame-001.png, etc.

The entire sequence is preloaded at startup time and is used as any other texture in Bonsai/BonVision through the operator BindTexture. By default only the first frame is used. However, if you have a texture array you can then use the Index property of BindTexture to indicate exactly which frame you want to display at any given time.

Either option should give you instantaneous frame swaps without any performance cost and are probably the fastest way of changing static images in Bonsai Shaders.

Hope this helps.

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/ceffc91f-12d5-4e09-a629-3c838e787929n%40googlegroups.com.

brunocruz

unread,
Jun 26, 2021, 10:12:59 AM6/26/21
to Bonsai Users
Amazing! thanks for the help
Reply all
Reply to author
Forward
0 new messages