[SOLVED] Dynamically creating texture assets from canvas/base64 resource

Hi,

I am creating textures (heightmaps/colormaps) dynamically on app load time using an offscreen canvas. Everything works great and the created image element produces a pc.Texture.

But if I would like this texture to be registered as an asset in the registry, what should I do? I want this mainly for memory management and using the asset unload method.

I tried using a base64 string from the image on the url property of the pc.Asset constructor, with no success (non expected).

Thanks!

This is me sounding naive here, can you not use pc.AssetRegistry.add()? https://developer.playcanvas.com/en/api/pc.AssetRegistry.html#add

Edit: Oh wait, you are having trouble creating the asset
Edit2: If you have pc.Texture, can you not just add it to the registry as is?

Thanks for the suggestion. I will try and assign the texture directly on the resource property on an empty asset.

Thought that creating the asset involves many things in the constructor that are parsed from the resource when downloaded.

Will let you know.

Probably something like this would work:

var texture = ...; // your texture object
var asset = new pc.Asset("asset-name", "texture", {
    url: "", // not sure exactly what you want to put in here, empty string might be fine
});
asset.resource = texture;
asset.loaded = true;

this.app.assets.add(asset);
3 Likes