Dynamically loaded textures missing their texture data

UPDATED: See the example project here: https://playcanvas.com/editor/scene/1084051

UPDATE #2: Here is the working code, for anyone else having this issue:


/* jshint esversion: 6 */

var DynamicAssetLoader = pc.createScript(‘dynamicAssetLoader’);

DynamicAssetLoader.prototype.initialize = function() {
this.testAssetLoading();
};

DynamicAssetLoader.prototype.testAssetLoading = function() {

const cube01 = this.app.root.findByName("Cube_Example-01");

this.loadAssetResourceCallback("example_preload-off.png", (loadedAsset) => {
    cube02.model.meshInstances[0].material.diffuseMap = loadedAsset.resource;
    cube02.model.meshInstances[0].material.update();    // THE FIX !!!
});

};

DynamicAssetLoader.prototype.loadAssetResourceCallback = function(assetName, callback) {
var loadedAsset = this.app.assets.find(assetName);

var onAssetLoaded = function() {
    callback(loadedAsset);
};

if (loadedAsset.resource) {
    return onAssetLoaded();
} else {
    loadedAsset.ready(onAssetLoaded);
    this.app.assets.load(loadedAsset);
}

};

I am trying to create a generic function that will load and return an asset which I can apply to an entity.

Since I have about 60+ textures but will only use 4 per level, I only want to load those required for my level.

The correct asset are being loaded and passed back, but they don’t have any texture data and display as white.

I’m using code straight from https://developer.playcanvas.com/en/tutorials/using-assets/
and searched the forums, but haven’t found anyone experiencing this exact issue

I’m using code straight from https://developer.playcanvas.com/en/tutorials/using-assets/
and searched the forums, but haven’t found anyone experiencing this exact issue

I’m trying to avoid providing a callback, but is that my only workable option here?

Apologies for the epic post, trying to provide enough data to be helpful. Thanks all!

Any thoughts @yaustar?

Would you be able to provide a simple reproducible project of the issue please? That will really help as it’s a bit hard to follow the logic just from the code in the forum

1 Like

Why are you trying to avoid a callback? Are you looking to get away from ‘callback hell’?

1 Like

Yes, preferably :slight_smile:

I’ll create a working project and post a link for you. Thanks!

1 Like

Okay, here’s a project that has the issue: https://playcanvas.com/editor/scene/1084051

I’ve solved the callback hell issue, but the asset that isn’t preloaded is not loading correctly.

Thanks for your time, @yaustar

Hey @SheepVsGravity, usually on assigning the texture to the material, material update is also called so that material can be updated. I just added the following line and it worked.

cube02.model.meshInstances[0].material.update();

image

3 Likes

One of the GFX programmers may correct me here: The engine uploads the texture to the GPU when it used for the first time. Until the material is updated, it isn’t used, just loaded.

2 Likes

That is correct, the WebGl texture is created only when a mesh with material that uses this textures is visible and rendered.

2 Likes

Ah, now that is a subtle thing. Your fix works a treat, thanks very much.

1 Like

That makes perfect sense, @yaustar and @mvaligursky but not something I had thought of.

Appreciate your detailed insight into the Playcanvas engine inner workings.

Also, kudos for being such an awesomely responsive company, it’s wonderful! :+1:

2 Likes