React: how to use this useAssetManager for TaskType.Texture

Hi @brianzinn

i use your rbabylonjs-hook.
It works. But I want to upload the texture:

const baseUrl = 'http://localhost:3000';
const textureAssetTasks: Task[] = [
    { name: 'preview2', taskType: TaskType.Texture, url: baseUrl+ "/textures/lightmaps/preview2.jpg", noMipmap: true, invertY: false, samplingMode: 1 }
];
const loadedAssets = useAssetManager(textureAssetTasks);
const assetsManagerTask = loadedAssets.taskNameMap['preview2'] as TextureAssetTask;

assetsManagerTask.onSuccess = function(task) {
                    console.log("task.texture is ok");
                    const currentLightmap = task.texture;
                    if(scene) {
                        const mesh = scene.getMeshByName("Floor");
                        if(mesh && mesh.material) {
                            let pbrMaterial = mesh.material as PBRMaterial;
                            pbrMaterial.lightmapTexture = currentLightmap;
                            pbrMaterial.lightmapTexture.coordinatesIndex = 1;
                            pbrMaterial.useLightmapAsShadowmap = true;
                        }
                    }
                };

assetsManagerTask.onError = function (task, message, exception) {
                    console.log("task.texture is error");
                    console.log(message, exception);
                };

And there it did not work.
Am I doing something wrong?

Do you have any error messages in the console?

ok,
I have the solution::

               const assetsManagerTask = loadedAssets.taskNameMap['preview2'] as TextureAssetTask;
        
                const currentLightmap = assetsManagerTask.texture;
3 Likes

The taskNameMap is a convenience - the order remains unchanged, so you can also:

const assetsManagerTask = loadedAssets[0] as TextureAssetTask;
const currentLightmap = assetsManagerTask.texture;

I think you are using ‘react-babylonjs-loaders’ which is a bit of a failed experiment to share code from a couple of years ago. I think I should just move that code into babylonjs-hook itself. I have archived the other repo and will try to remember to move that. Cheers.

1 Like